Line data Source code
1 : /* Straight-line strength reduction.
2 : Copyright (C) 2012-2026 Free Software Foundation, Inc.
3 : Contributed by Bill Schmidt, IBM <wschmidt@linux.ibm.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : /* There are many algorithms for performing strength reduction on
22 : loops. This is not one of them. IVOPTS handles strength reduction
23 : of induction variables just fine. This pass is intended to pick
24 : up the crumbs it leaves behind, by considering opportunities for
25 : strength reduction along dominator paths.
26 :
27 : Strength reduction addresses explicit multiplies, and certain
28 : multiplies implicit in addressing expressions. It would also be
29 : possible to apply strength reduction to divisions and modulos,
30 : but such opportunities are relatively uncommon.
31 :
32 : Strength reduction is also currently restricted to integer operations.
33 : If desired, it could be extended to floating-point operations under
34 : control of something like -funsafe-math-optimizations. */
35 :
36 : #include "config.h"
37 : #include "system.h"
38 : #include "coretypes.h"
39 : #include "backend.h"
40 : #include "rtl.h"
41 : #include "tree.h"
42 : #include "gimple.h"
43 : #include "cfghooks.h"
44 : #include "tree-pass.h"
45 : #include "ssa.h"
46 : #include "expmed.h"
47 : #include "gimple-pretty-print.h"
48 : #include "fold-const.h"
49 : #include "gimple-iterator.h"
50 : #include "gimplify-me.h"
51 : #include "stor-layout.h"
52 : #include "cfgloop.h"
53 : #include "tree-cfg.h"
54 : #include "domwalk.h"
55 : #include "tree-ssa-address.h"
56 : #include "tree-affine.h"
57 : #include "tree-eh.h"
58 : #include "builtins.h"
59 : #include "tree-ssa-dce.h"
60 : #include "gimple-fold.h"
61 :
62 : /* Information about a strength reduction candidate. Each statement
63 : in the candidate table represents an expression of one of the
64 : following forms (the special case of CAND_REF will be described
65 : later):
66 :
67 : (CAND_MULT) S1: X = (B + i) * S
68 : (CAND_ADD) S1: X = B + (i * S)
69 :
70 : Here X and B are SSA names, i is an integer constant, and S is
71 : either an SSA name or a constant. We call B the "base," i the
72 : "index", and S the "stride."
73 :
74 : Any statement S0 that dominates S1 and is of the form:
75 :
76 : (CAND_MULT) S0: Y = (B + i') * S
77 : (CAND_ADD) S0: Y = B + (i' * S)
78 :
79 : is called a "basis" for S1. In both cases, S1 may be replaced by
80 :
81 : S1': X = Y + (i - i') * S,
82 :
83 : where (i - i') * S is folded to the extent possible.
84 :
85 : All gimple statements are visited in dominator order, and each
86 : statement that may contribute to one of the forms of S1 above is
87 : given at least one entry in the candidate table. Such statements
88 : include addition, pointer addition, subtraction, multiplication,
89 : negation, copies, and nontrivial type casts. If a statement may
90 : represent more than one expression of the forms of S1 above,
91 : multiple "interpretations" are stored in the table and chained
92 : together. Examples:
93 :
94 : * An add of two SSA names may treat either operand as the base.
95 : * A multiply of two SSA names, likewise.
96 : * A copy or cast may be thought of as either a CAND_MULT with
97 : i = 0 and S = 1, or as a CAND_ADD with i = 0 or S = 0.
98 :
99 : Candidate records are allocated from an obstack. They are addressed
100 : both from a hash table keyed on S1, and from a vector of candidate
101 : pointers arranged in predominator order.
102 :
103 : Opportunity note
104 : ----------------
105 : Currently we don't recognize:
106 :
107 : S0: Y = (S * i') - B
108 : S1: X = (S * i) - B
109 :
110 : as a strength reduction opportunity, even though this S1 would
111 : also be replaceable by the S1' above. This can be added if it
112 : comes up in practice.
113 :
114 : Strength reduction in addressing
115 : --------------------------------
116 : There is another kind of candidate known as CAND_REF. A CAND_REF
117 : describes a statement containing a memory reference having
118 : complex addressing that might benefit from strength reduction.
119 : Specifically, we are interested in references for which
120 : get_inner_reference returns a base address, offset, and bitpos as
121 : follows:
122 :
123 : base: MEM_REF (T1, C1)
124 : offset: MULT_EXPR (PLUS_EXPR (T2, C2), C3)
125 : bitpos: C4 * BITS_PER_UNIT
126 :
127 : Here T1 and T2 are arbitrary trees, and C1, C2, C3, C4 are
128 : arbitrary integer constants. Note that C2 may be zero, in which
129 : case the offset will be MULT_EXPR (T2, C3).
130 :
131 : When this pattern is recognized, the original memory reference
132 : can be replaced with:
133 :
134 : MEM_REF (POINTER_PLUS_EXPR (T1, MULT_EXPR (T2, C3)),
135 : C1 + (C2 * C3) + C4)
136 :
137 : which distributes the multiply to allow constant folding. When
138 : two or more addressing expressions can be represented by MEM_REFs
139 : of this form, differing only in the constants C1, C2, and C4,
140 : making this substitution produces more efficient addressing during
141 : the RTL phases. When there are not at least two expressions with
142 : the same values of T1, T2, and C3, there is nothing to be gained
143 : by the replacement.
144 :
145 : Strength reduction of CAND_REFs uses the same infrastructure as
146 : that used by CAND_MULTs and CAND_ADDs. We record T1 in the base (B)
147 : field, MULT_EXPR (T2, C3) in the stride (S) field, and
148 : C1 + (C2 * C3) + C4 in the index (i) field. A basis for a CAND_REF
149 : is thus another CAND_REF with the same B and S values. When at
150 : least two CAND_REFs are chained together using the basis relation,
151 : each of them is replaced as above, resulting in improved code
152 : generation for addressing.
153 :
154 : Conditional candidates
155 : ======================
156 :
157 : Conditional candidates are best illustrated with an example.
158 : Consider the code sequence:
159 :
160 : (1) x_0 = ...;
161 : (2) a_0 = x_0 * 5; MULT (B: x_0; i: 0; S: 5)
162 : if (...)
163 : (3) x_1 = x_0 + 1; ADD (B: x_0, i: 1; S: 1)
164 : (4) x_2 = PHI <x_0, x_1>; PHI (B: x_0, i: 0, S: 1)
165 : (5) x_3 = x_2 + 1; ADD (B: x_2, i: 1, S: 1)
166 : (6) a_1 = x_3 * 5; MULT (B: x_2, i: 1; S: 5)
167 :
168 : Here strength reduction is complicated by the uncertain value of x_2.
169 : A legitimate transformation is:
170 :
171 : (1) x_0 = ...;
172 : (2) a_0 = x_0 * 5;
173 : if (...)
174 : {
175 : (3) [x_1 = x_0 + 1;]
176 : (3a) t_1 = a_0 + 5;
177 : }
178 : (4) [x_2 = PHI <x_0, x_1>;]
179 : (4a) t_2 = PHI <a_0, t_1>;
180 : (5) [x_3 = x_2 + 1;]
181 : (6r) a_1 = t_2 + 5;
182 :
183 : where the bracketed instructions may go dead.
184 :
185 : To recognize this opportunity, we have to observe that statement (6)
186 : has a "hidden basis" (2). The hidden basis is unlike a normal basis
187 : in that the statement and the hidden basis have different base SSA
188 : names (x_2 and x_0, respectively). The relationship is established
189 : when a statement's base name (x_2) is defined by a phi statement (4),
190 : each argument of which (x_0, x_1) has an identical "derived base name."
191 : If the argument is defined by a candidate (as x_1 is by (3)) that is a
192 : CAND_ADD having a stride of 1, the derived base name of the argument is
193 : the base name of the candidate (x_0). Otherwise, the argument itself
194 : is its derived base name (as is the case with argument x_0).
195 :
196 : The hidden basis for statement (6) is the nearest dominating candidate
197 : whose base name is the derived base name (x_0) of the feeding phi (4),
198 : and whose stride is identical to that of the statement. We can then
199 : create the new "phi basis" (4a) and feeding adds along incoming arcs (3a),
200 : allowing the final replacement of (6) by the strength-reduced (6r).
201 :
202 : To facilitate this, a new kind of candidate (CAND_PHI) is introduced.
203 : A CAND_PHI is not a candidate for replacement, but is maintained in the
204 : candidate table to ease discovery of hidden bases. Any phi statement
205 : whose arguments share a common derived base name is entered into the
206 : table with the derived base name, an (arbitrary) index of zero, and a
207 : stride of 1. A statement with a hidden basis can then be detected by
208 : simply looking up its feeding phi definition in the candidate table,
209 : extracting the derived base name, and searching for a basis in the
210 : usual manner after substituting the derived base name.
211 :
212 : Note that the transformation is only valid when the original phi and
213 : the statements that define the phi's arguments are all at the same
214 : position in the loop hierarchy. */
215 :
216 :
217 : /* Index into the candidate vector, offset by 1. VECs are zero-based,
218 : while cand_idx's are one-based, with zero indicating null. */
219 : typedef unsigned cand_idx;
220 :
221 : /* The kind of candidate. */
222 : enum cand_kind
223 : {
224 : CAND_MULT,
225 : CAND_ADD,
226 : CAND_REF,
227 : CAND_PHI
228 : };
229 :
230 : class slsr_cand_d
231 : {
232 : public:
233 : /* The candidate statement S1. */
234 : gimple *cand_stmt;
235 :
236 : /* The base expression B: often an SSA name, but not always. */
237 : tree base_expr;
238 :
239 : /* The stride S. */
240 : tree stride;
241 :
242 : /* The index constant i. */
243 : offset_int index;
244 :
245 : /* The type of the candidate. This is normally the type of base_expr,
246 : but casts may have occurred when combining feeding instructions.
247 : A candidate can only be a basis for candidates of the same final type.
248 : (For CAND_REFs, this is the type to be used for operand 1 of the
249 : replacement MEM_REF.) */
250 : tree cand_type;
251 :
252 : /* The type to be used to interpret the stride field when the stride
253 : is not a constant. Normally the same as the type of the recorded
254 : stride, but when the stride has been cast we need to maintain that
255 : knowledge in order to make legal substitutions without losing
256 : precision. When the stride is a constant, this will be sizetype. */
257 : tree stride_type;
258 :
259 : /* The kind of candidate (CAND_MULT, etc.). */
260 : enum cand_kind kind;
261 :
262 : /* Index of this candidate in the candidate vector. */
263 : cand_idx cand_num;
264 :
265 : /* Index of the next candidate record for the same statement.
266 : A statement may be useful in more than one way (e.g., due to
267 : commutativity). So we can have multiple "interpretations"
268 : of a statement. */
269 : cand_idx next_interp;
270 :
271 : /* Index of the first candidate record in a chain for the same
272 : statement. */
273 : cand_idx first_interp;
274 :
275 : /* Index of the basis statement S0, if any, in the candidate vector. */
276 : cand_idx basis;
277 :
278 : /* First candidate for which this candidate is a basis, if one exists. */
279 : cand_idx dependent;
280 :
281 : /* Next candidate having the same basis as this one. */
282 : cand_idx sibling;
283 :
284 : /* If this is a conditional candidate, the CAND_PHI candidate
285 : that defines the base SSA name B. */
286 : cand_idx def_phi;
287 :
288 : /* Savings that can be expected from eliminating dead code if this
289 : candidate is replaced. */
290 : int dead_savings;
291 :
292 : /* For PHI candidates, use a visited flag to keep from processing the
293 : same PHI twice from multiple paths. */
294 : int visited;
295 :
296 : /* We sometimes have to cache a phi basis with a phi candidate to
297 : avoid processing it twice. Valid only if visited==1. */
298 : tree cached_basis;
299 : };
300 :
301 : typedef class slsr_cand_d slsr_cand, *slsr_cand_t;
302 : typedef const class slsr_cand_d *const_slsr_cand_t;
303 :
304 : /* Pointers to candidates are chained together as part of a mapping
305 : from base expressions to the candidates that use them. */
306 :
307 : struct cand_chain_d
308 : {
309 : /* Base expression for the chain of candidates: often, but not
310 : always, an SSA name. */
311 : tree base_expr;
312 :
313 : /* Pointer to a candidate. */
314 : slsr_cand_t cand;
315 :
316 : /* Chain pointer. */
317 : struct cand_chain_d *next;
318 :
319 : };
320 :
321 : typedef struct cand_chain_d cand_chain, *cand_chain_t;
322 : typedef const struct cand_chain_d *const_cand_chain_t;
323 :
324 : /* Information about a unique "increment" associated with candidates
325 : having an SSA name for a stride. An increment is the difference
326 : between the index of the candidate and the index of its basis,
327 : i.e., (i - i') as discussed in the module commentary.
328 :
329 : When we are not going to generate address arithmetic we treat
330 : increments that differ only in sign as the same, allowing sharing
331 : of the cost of initializers. The absolute value of the increment
332 : is stored in the incr_info. */
333 :
334 : class incr_info_d
335 : {
336 : public:
337 : /* The increment that relates a candidate to its basis. */
338 : offset_int incr;
339 :
340 : /* How many times the increment occurs in the candidate tree. */
341 : unsigned count;
342 :
343 : /* Cost of replacing candidates using this increment. Negative and
344 : zero costs indicate replacement should be performed. */
345 : int cost;
346 :
347 : /* If this increment is profitable but is not -1, 0, or 1, it requires
348 : an initializer T_0 = stride * incr to be found or introduced in the
349 : nearest common dominator of all candidates. This field holds T_0
350 : for subsequent use. */
351 : tree initializer;
352 :
353 : /* If the initializer was found to already exist, this is the block
354 : where it was found. */
355 : basic_block init_bb;
356 : };
357 :
358 : typedef class incr_info_d incr_info, *incr_info_t;
359 :
360 : /* Candidates are maintained in a vector. If candidate X dominates
361 : candidate Y, then X appears before Y in the vector; but the
362 : converse does not necessarily hold. */
363 : static vec<slsr_cand_t> cand_vec;
364 :
365 : enum cost_consts
366 : {
367 : COST_NEUTRAL = 0,
368 : COST_INFINITE = 1000
369 : };
370 :
371 : enum stride_status
372 : {
373 : UNKNOWN_STRIDE = 0,
374 : KNOWN_STRIDE = 1
375 : };
376 :
377 : enum phi_adjust_status
378 : {
379 : NOT_PHI_ADJUST = 0,
380 : PHI_ADJUST = 1
381 : };
382 :
383 : enum count_phis_status
384 : {
385 : DONT_COUNT_PHIS = 0,
386 : COUNT_PHIS = 1
387 : };
388 :
389 : /* Constrain how many PHI nodes we will visit for a conditional
390 : candidate (depth and breadth). */
391 : const int MAX_SPREAD = 16;
392 :
393 : /* Pointer map embodying a mapping from statements to candidates. */
394 : static hash_map<gimple *, slsr_cand_t> *stmt_cand_map;
395 :
396 : /* Obstack for candidates. */
397 : static struct obstack cand_obstack;
398 :
399 : /* Obstack for candidate chains. */
400 : static struct obstack chain_obstack;
401 :
402 : /* An array INCR_VEC of incr_infos is used during analysis of related
403 : candidates having an SSA name for a stride. INCR_VEC_LEN describes
404 : its current length. MAX_INCR_VEC_LEN is used to avoid costly
405 : pathological cases. */
406 : static incr_info_t incr_vec;
407 : static unsigned incr_vec_len;
408 : const int MAX_INCR_VEC_LEN = 16;
409 :
410 : /* For a chain of candidates with unknown stride, indicates whether or not
411 : we must generate pointer arithmetic when replacing statements. */
412 : static bool address_arithmetic_p;
413 :
414 : /* Forward function declarations. */
415 : static slsr_cand_t base_cand_from_table (tree);
416 : static tree introduce_cast_before_cand (slsr_cand_t, tree, tree);
417 : static bool legal_cast_p_1 (tree, tree);
418 :
419 : /* Produce a pointer to the IDX'th candidate in the candidate vector. */
420 :
421 : static slsr_cand_t
422 8537541 : lookup_cand (cand_idx idx)
423 : {
424 0 : return cand_vec[idx];
425 : }
426 :
427 : /* Helper for hashing a candidate chain header. */
428 :
429 : struct cand_chain_hasher : nofree_ptr_hash <cand_chain>
430 : {
431 : static inline hashval_t hash (const cand_chain *);
432 : static inline bool equal (const cand_chain *, const cand_chain *);
433 : };
434 :
435 : inline hashval_t
436 33510564 : cand_chain_hasher::hash (const cand_chain *p)
437 : {
438 33510564 : tree base_expr = p->base_expr;
439 33510564 : return iterative_hash_expr (base_expr, 0);
440 : }
441 :
442 : inline bool
443 26743613 : cand_chain_hasher::equal (const cand_chain *chain1, const cand_chain *chain2)
444 : {
445 26743613 : return operand_equal_p (chain1->base_expr, chain2->base_expr, 0);
446 : }
447 :
448 : /* Hash table embodying a mapping from base exprs to chains of candidates. */
449 : static hash_table<cand_chain_hasher> *base_cand_map;
450 :
451 : /* Pointer map used by tree_to_aff_combination_expand. */
452 : static hash_map<tree, name_expansion *> *name_expansions;
453 : /* Pointer map embodying a mapping from bases to alternative bases. */
454 : static hash_map<tree, tree> *alt_base_map;
455 :
456 : /* Given BASE, use the tree affine combination facilities to
457 : find the underlying tree expression for BASE, with any
458 : immediate offset excluded.
459 :
460 : N.B. we should eliminate this backtracking with better forward
461 : analysis in a future release. */
462 :
463 : static tree
464 66806 : get_alternative_base (tree base)
465 : {
466 66806 : tree *result = alt_base_map->get (base);
467 :
468 66806 : if (result == NULL)
469 : {
470 11820 : tree expr;
471 11820 : aff_tree aff;
472 :
473 11820 : tree_to_aff_combination_expand (base, TREE_TYPE (base),
474 : &aff, &name_expansions);
475 11820 : aff.offset = 0;
476 11820 : expr = aff_combination_to_tree (&aff);
477 :
478 23502 : bool existed = alt_base_map->put (base, base == expr ? NULL : expr);
479 11820 : gcc_assert (!existed);
480 :
481 11820 : return expr == base ? NULL : expr;
482 11820 : }
483 :
484 54986 : return *result;
485 : }
486 :
487 : /* Look in the candidate table for a CAND_PHI that defines BASE and
488 : return it if found; otherwise return NULL. */
489 :
490 : static cand_idx
491 2639353 : find_phi_def (tree base)
492 : {
493 2639353 : slsr_cand_t c;
494 :
495 2639353 : if (TREE_CODE (base) != SSA_NAME)
496 : return 0;
497 :
498 2639353 : c = base_cand_from_table (base);
499 :
500 195663 : if (!c || c->kind != CAND_PHI
501 2640243 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (c->cand_stmt)))
502 : return 0;
503 :
504 885 : return c->cand_num;
505 : }
506 :
507 : /* Determine whether all uses of NAME are directly or indirectly
508 : used by STMT. That is, we want to know whether if STMT goes
509 : dead, the definition of NAME also goes dead. */
510 : static bool
511 195263 : uses_consumed_by_stmt (tree name, gimple *stmt, unsigned recurse = 0)
512 : {
513 195263 : gimple *use_stmt;
514 195263 : imm_use_iterator iter;
515 195263 : bool retval = true;
516 :
517 366495 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
518 : {
519 275920 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
520 168333 : continue;
521 :
522 107587 : if (!is_gimple_assign (use_stmt)
523 41122 : || !gimple_get_lhs (use_stmt)
524 41122 : || !is_gimple_reg (gimple_get_lhs (use_stmt))
525 34391 : || recurse >= 10
526 141939 : || !uses_consumed_by_stmt (gimple_get_lhs (use_stmt), stmt,
527 : recurse + 1))
528 : {
529 : retval = false;
530 : break;
531 : }
532 195263 : }
533 :
534 195263 : return retval;
535 : }
536 :
537 : /* Helper routine for find_basis_for_candidate. May be called twice:
538 : once for the candidate's base expr, and optionally again either for
539 : the candidate's phi definition or for a CAND_REF's alternative base
540 : expression. */
541 :
542 : static slsr_cand_t
543 8382571 : find_basis_for_base_expr (slsr_cand_t c, tree base_expr)
544 : {
545 8382571 : cand_chain mapping_key;
546 8382571 : cand_chain_t chain;
547 8382571 : slsr_cand_t basis = NULL;
548 :
549 : // Limit potential of N^2 behavior for long candidate chains.
550 8382571 : int iters = 0;
551 8382571 : int max_iters = param_max_slsr_candidate_scan;
552 :
553 8382571 : mapping_key.base_expr = base_expr;
554 8382571 : chain = base_cand_map->find (&mapping_key);
555 :
556 33400762 : for (; chain && iters < max_iters; chain = chain->next, ++iters)
557 : {
558 25018191 : slsr_cand_t one_basis = chain->cand;
559 :
560 44924581 : if (one_basis->kind != c->kind
561 14778663 : || one_basis->cand_stmt == c->cand_stmt
562 14777895 : || !operand_equal_p (one_basis->stride, c->stride, 0)
563 8306878 : || !types_compatible_p (one_basis->cand_type, c->cand_type)
564 7057297 : || !types_compatible_p (one_basis->stride_type, c->stride_type)
565 32075476 : || !dominated_by_p (CDI_DOMINATORS,
566 7057285 : gimple_bb (c->cand_stmt),
567 7057285 : gimple_bb (one_basis->cand_stmt)))
568 19906390 : continue;
569 :
570 5111801 : tree lhs = gimple_assign_lhs (one_basis->cand_stmt);
571 5111801 : if (lhs && TREE_CODE (lhs) == SSA_NAME
572 10204516 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
573 70 : continue;
574 :
575 5111731 : if (!basis || basis->cand_num < one_basis->cand_num)
576 25018191 : basis = one_basis;
577 : }
578 :
579 8382571 : return basis;
580 : }
581 :
582 : /* Use the base expr from candidate C to look for possible candidates
583 : that can serve as a basis for C. Each potential basis must also
584 : appear in a block that dominates the candidate statement and have
585 : the same stride and type. If more than one possible basis exists,
586 : the one with highest index in the vector is chosen; this will be
587 : the most immediately dominating basis. */
588 :
589 : static int
590 8381590 : find_basis_for_candidate (slsr_cand_t c)
591 : {
592 8381590 : slsr_cand_t basis = find_basis_for_base_expr (c, c->base_expr);
593 :
594 : /* If a candidate doesn't have a basis using its base expression,
595 : it may have a basis hidden by one or more intervening phis. */
596 8381590 : if (!basis && c->def_phi)
597 : {
598 811 : basic_block basis_bb, phi_bb;
599 811 : slsr_cand_t phi_cand = lookup_cand (c->def_phi);
600 811 : basis = find_basis_for_base_expr (c, phi_cand->base_expr);
601 :
602 811 : if (basis)
603 : {
604 : /* A hidden basis must dominate the phi-definition of the
605 : candidate's base name. */
606 75 : phi_bb = gimple_bb (phi_cand->cand_stmt);
607 75 : basis_bb = gimple_bb (basis->cand_stmt);
608 :
609 75 : if (phi_bb == basis_bb
610 75 : || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
611 : {
612 13 : basis = NULL;
613 13 : c->basis = 0;
614 : }
615 :
616 : /* If we found a hidden basis, estimate additional dead-code
617 : savings if the phi and its feeding statements can be removed. */
618 75 : tree feeding_var = gimple_phi_result (phi_cand->cand_stmt);
619 75 : if (basis && uses_consumed_by_stmt (feeding_var, c->cand_stmt))
620 27 : c->dead_savings += phi_cand->dead_savings;
621 : }
622 : }
623 :
624 8381590 : if (flag_expensive_optimizations && !basis && c->kind == CAND_REF)
625 : {
626 25891 : tree alt_base_expr = get_alternative_base (c->base_expr);
627 25891 : if (alt_base_expr)
628 170 : basis = find_basis_for_base_expr (c, alt_base_expr);
629 : }
630 :
631 7177631 : if (basis)
632 : {
633 1341516 : c->sibling = basis->dependent;
634 1341516 : basis->dependent = c->cand_num;
635 1341516 : return basis->cand_num;
636 : }
637 :
638 : return 0;
639 : }
640 :
641 : /* Record a mapping from BASE to C, indicating that C may potentially serve
642 : as a basis using that base expression. BASE may be the same as
643 : C->BASE_EXPR; alternatively BASE can be a different tree that share the
644 : underlining expression of C->BASE_EXPR. */
645 :
646 : static void
647 8392620 : record_potential_basis (slsr_cand_t c, tree base)
648 : {
649 8392620 : cand_chain_t node;
650 8392620 : cand_chain **slot;
651 :
652 8392620 : gcc_assert (base);
653 :
654 8392620 : node = (cand_chain_t) obstack_alloc (&chain_obstack, sizeof (cand_chain));
655 8392620 : node->base_expr = base;
656 8392620 : node->cand = c;
657 8392620 : node->next = NULL;
658 8392620 : slot = base_cand_map->find_slot (node, INSERT);
659 :
660 8392620 : if (*slot)
661 : {
662 4040600 : cand_chain_t head = (cand_chain_t) (*slot);
663 4040600 : node->next = head->next;
664 4040600 : head->next = node;
665 : }
666 : else
667 4352020 : *slot = node;
668 8392620 : }
669 :
670 : /* Allocate storage for a new candidate and initialize its fields.
671 : Attempt to find a basis for the candidate.
672 :
673 : For CAND_REF, an alternative base may also be recorded and used
674 : to find a basis. This helps cases where the expression hidden
675 : behind BASE (which is usually an SSA_NAME) has immediate offset,
676 : e.g.
677 :
678 : a2[i][j] = 1;
679 : a2[i + 20][j] = 2; */
680 :
681 : static slsr_cand_t
682 8392286 : alloc_cand_and_find_basis (enum cand_kind kind, gimple *gs, tree base,
683 : const offset_int &index, tree stride, tree ctype,
684 : tree stype, unsigned savings)
685 : {
686 8392286 : slsr_cand_t c = (slsr_cand_t) obstack_alloc (&cand_obstack,
687 : sizeof (slsr_cand));
688 8392286 : c->cand_stmt = gs;
689 8392286 : c->base_expr = base;
690 8392286 : c->stride = stride;
691 8392286 : c->index = index;
692 8392286 : c->cand_type = ctype;
693 8392286 : c->stride_type = stype;
694 8392286 : c->kind = kind;
695 8392286 : c->cand_num = cand_vec.length ();
696 8392286 : c->next_interp = 0;
697 8392286 : c->first_interp = c->cand_num;
698 8392286 : c->dependent = 0;
699 8392286 : c->sibling = 0;
700 8392286 : c->def_phi = kind == CAND_MULT ? find_phi_def (base) : 0;
701 8392286 : c->dead_savings = savings;
702 8392286 : c->visited = 0;
703 8392286 : c->cached_basis = NULL_TREE;
704 :
705 8392286 : cand_vec.safe_push (c);
706 :
707 8392286 : if (kind == CAND_PHI)
708 10696 : c->basis = 0;
709 : else
710 8381590 : c->basis = find_basis_for_candidate (c);
711 :
712 8392286 : record_potential_basis (c, base);
713 8392286 : if (flag_expensive_optimizations && kind == CAND_REF)
714 : {
715 40915 : tree alt_base = get_alternative_base (base);
716 40915 : if (alt_base)
717 334 : record_potential_basis (c, alt_base);
718 : }
719 :
720 8392286 : return c;
721 : }
722 :
723 : /* Determine the target cost of statement GS when compiling according
724 : to SPEED. */
725 :
726 : static int
727 1401850 : stmt_cost (gimple *gs, bool speed)
728 : {
729 1401850 : tree lhs, rhs1, rhs2;
730 1401850 : machine_mode lhs_mode;
731 :
732 1401850 : gcc_assert (is_gimple_assign (gs));
733 1401850 : lhs = gimple_assign_lhs (gs);
734 1401850 : rhs1 = gimple_assign_rhs1 (gs);
735 1401850 : lhs_mode = TYPE_MODE (TREE_TYPE (lhs));
736 :
737 1401850 : switch (gimple_assign_rhs_code (gs))
738 : {
739 409371 : case MULT_EXPR:
740 409371 : rhs2 = gimple_assign_rhs2 (gs);
741 :
742 409371 : if (tree_fits_shwi_p (rhs2))
743 333229 : return mult_by_coeff_cost (tree_to_shwi (rhs2), lhs_mode, speed);
744 :
745 76142 : gcc_assert (TREE_CODE (rhs1) != INTEGER_CST);
746 76142 : return mul_cost (speed, lhs_mode);
747 :
748 235004 : case PLUS_EXPR:
749 235004 : case POINTER_PLUS_EXPR:
750 235004 : case MINUS_EXPR:
751 235004 : return add_cost (speed, lhs_mode);
752 :
753 11299 : case NEGATE_EXPR:
754 11299 : return neg_cost (speed, lhs_mode);
755 :
756 711566 : CASE_CONVERT:
757 1423132 : return convert_cost (lhs_mode, TYPE_MODE (TREE_TYPE (rhs1)), speed);
758 :
759 : /* Note that we don't assign costs to copies that in most cases
760 : will go away. */
761 : case SSA_NAME:
762 : return 0;
763 :
764 0 : default:
765 0 : ;
766 : }
767 :
768 0 : gcc_unreachable ();
769 : }
770 :
771 : /* Look up the defining statement for BASE_IN and return a pointer
772 : to its candidate in the candidate table, if any; otherwise NULL.
773 : Only CAND_ADD and CAND_MULT candidates are returned. */
774 :
775 : static slsr_cand_t
776 15997364 : base_cand_from_table (tree base_in)
777 : {
778 15997364 : slsr_cand_t *result;
779 :
780 15997364 : gimple *def = SSA_NAME_DEF_STMT (base_in);
781 15997364 : if (!def)
782 : return (slsr_cand_t) NULL;
783 :
784 15997364 : result = stmt_cand_map->get (def);
785 :
786 15997364 : if (result && (*result)->kind != CAND_REF)
787 3867037 : return *result;
788 :
789 : return (slsr_cand_t) NULL;
790 : }
791 :
792 : /* Add an entry to the statement-to-candidate mapping. */
793 :
794 : static void
795 6054896 : add_cand_for_stmt (gimple *gs, slsr_cand_t c)
796 : {
797 6054896 : bool existed = stmt_cand_map->put (gs, c);
798 6054896 : gcc_assert (!existed);
799 6054896 : }
800 :
801 : /* Given PHI which contains a phi statement, determine whether it
802 : satisfies all the requirements of a phi candidate. If so, create
803 : a candidate. Note that a CAND_PHI never has a basis itself, but
804 : is used to help find a basis for subsequent candidates. */
805 :
806 : static void
807 4574043 : slsr_process_phi (gphi *phi, bool speed)
808 : {
809 4574043 : unsigned i;
810 4574043 : tree arg0_base = NULL_TREE, base_type;
811 4574043 : slsr_cand_t c;
812 4574043 : class loop *cand_loop = gimple_bb (phi)->loop_father;
813 4574043 : unsigned savings = 0;
814 :
815 : /* A CAND_PHI requires each of its arguments to have the same
816 : derived base name. (See the module header commentary for a
817 : definition of derived base names.) Furthermore, all feeding
818 : definitions must be in the same position in the loop hierarchy
819 : as PHI. */
820 :
821 4832810 : for (i = 0; i < gimple_phi_num_args (phi); i++)
822 : {
823 4822114 : slsr_cand_t arg_cand;
824 4822114 : tree arg = gimple_phi_arg_def (phi, i);
825 4822114 : tree derived_base_name = NULL_TREE;
826 4822114 : gimple *arg_stmt = NULL;
827 4822114 : basic_block arg_bb = NULL;
828 :
829 4822114 : if (TREE_CODE (arg) != SSA_NAME)
830 : return;
831 :
832 4391023 : arg_cand = base_cand_from_table (arg);
833 :
834 4391023 : if (arg_cand)
835 : {
836 288748 : while (arg_cand->kind != CAND_ADD && arg_cand->kind != CAND_PHI)
837 : {
838 31537 : if (!arg_cand->next_interp)
839 : return;
840 :
841 4894 : arg_cand = lookup_cand (arg_cand->next_interp);
842 : }
843 :
844 257211 : if (!integer_onep (arg_cand->stride))
845 : return;
846 :
847 160820 : derived_base_name = arg_cand->base_expr;
848 160820 : arg_stmt = arg_cand->cand_stmt;
849 160820 : arg_bb = gimple_bb (arg_stmt);
850 :
851 : /* Gather potential dead code savings if the phi statement
852 : can be removed later on. */
853 160820 : if (uses_consumed_by_stmt (arg, phi))
854 : {
855 87626 : if (gimple_code (arg_stmt) == GIMPLE_PHI)
856 1904 : savings += arg_cand->dead_savings;
857 : else
858 85722 : savings += stmt_cost (arg_stmt, speed);
859 : }
860 : }
861 4107169 : else if (SSA_NAME_IS_DEFAULT_DEF (arg))
862 : {
863 184100 : derived_base_name = arg;
864 184100 : arg_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
865 : }
866 :
867 344920 : if (!arg_bb || arg_bb->loop_father != cand_loop)
868 : return;
869 :
870 272348 : if (i == 0)
871 : arg0_base = derived_base_name;
872 45886 : else if (!operand_equal_p (derived_base_name, arg0_base, 0))
873 : return;
874 : }
875 :
876 : /* Create the candidate. "alloc_cand_and_find_basis" is named
877 : misleadingly for this case, as no basis will be sought for a
878 : CAND_PHI. */
879 10696 : base_type = TREE_TYPE (arg0_base);
880 :
881 32088 : c = alloc_cand_and_find_basis (CAND_PHI, phi, arg0_base,
882 10696 : 0, integer_one_node, base_type,
883 : sizetype, savings);
884 :
885 : /* Add the candidate to the statement-candidate mapping. */
886 10696 : add_cand_for_stmt (phi, c);
887 : }
888 :
889 : /* Given PBASE which is a pointer to tree, look up the defining
890 : statement for it and check whether the candidate is in the
891 : form of:
892 :
893 : X = B + (1 * S), S is integer constant
894 : X = B + (i * S), S is integer one
895 :
896 : If so, set PBASE to the candidate's base_expr and return double
897 : int (i * S).
898 : Otherwise, just return double int zero. */
899 :
900 : static offset_int
901 53117 : backtrace_base_for_ref (tree *pbase)
902 : {
903 53117 : tree base_in = *pbase;
904 53117 : slsr_cand_t base_cand;
905 :
906 53117 : STRIP_NOPS (base_in);
907 :
908 : /* Strip off widening conversion(s) to handle cases where
909 : e.g. 'B' is widened from an 'int' in order to calculate
910 : a 64-bit address. */
911 49878 : if (CONVERT_EXPR_P (base_in)
912 56356 : && legal_cast_p_1 (TREE_TYPE (base_in),
913 3239 : TREE_TYPE (TREE_OPERAND (base_in, 0))))
914 2507 : base_in = get_unwidened (base_in, NULL_TREE);
915 :
916 53117 : if (TREE_CODE (base_in) != SSA_NAME)
917 1830 : return 0;
918 :
919 51287 : base_cand = base_cand_from_table (base_in);
920 :
921 130650 : while (base_cand && base_cand->kind != CAND_PHI)
922 : {
923 54196 : if (base_cand->kind == CAND_ADD
924 53042 : && base_cand->index == 1
925 86238 : && TREE_CODE (base_cand->stride) == INTEGER_CST)
926 : {
927 : /* X = B + (1 * S), S is integer constant. */
928 5622 : *pbase = base_cand->base_expr;
929 5622 : return wi::to_offset (base_cand->stride);
930 : }
931 48574 : else if (base_cand->kind == CAND_ADD
932 47420 : && TREE_CODE (base_cand->stride) == INTEGER_CST
933 69072 : && integer_onep (base_cand->stride))
934 : {
935 : /* X = B + (i * S), S is integer one. */
936 20498 : *pbase = base_cand->base_expr;
937 20498 : return base_cand->index;
938 : }
939 :
940 28076 : base_cand = lookup_cand (base_cand->next_interp);
941 : }
942 :
943 25167 : return 0;
944 : }
945 :
946 : /* Look for the following pattern:
947 :
948 : *PBASE: MEM_REF (T1, C1)
949 :
950 : *POFFSET: MULT_EXPR (T2, C3) [C2 is zero]
951 : or
952 : MULT_EXPR (PLUS_EXPR (T2, C2), C3)
953 : or
954 : MULT_EXPR (MINUS_EXPR (T2, -C2), C3)
955 :
956 : *PINDEX: C4 * BITS_PER_UNIT
957 :
958 : If not present, leave the input values unchanged and return FALSE.
959 : Otherwise, modify the input values as follows and return TRUE:
960 :
961 : *PBASE: T1
962 : *POFFSET: MULT_EXPR (T2, C3)
963 : *PINDEX: C1 + (C2 * C3) + C4
964 :
965 : When T2 is recorded by a CAND_ADD in the form of (T2' + C5), it
966 : will be further restructured to:
967 :
968 : *PBASE: T1
969 : *POFFSET: MULT_EXPR (T2', C3)
970 : *PINDEX: C1 + (C2 * C3) + C4 + (C5 * C3) */
971 :
972 : static bool
973 5793808 : restructure_reference (tree *pbase, tree *poffset, offset_int *pindex,
974 : tree *ptype)
975 : {
976 5793808 : tree base = *pbase, offset = *poffset;
977 5793808 : offset_int index = *pindex;
978 5793808 : tree mult_op0, t1, t2, type;
979 5793808 : offset_int c1, c2, c3, c4, c5;
980 5793808 : offset_int mem_offset;
981 :
982 5793808 : if (!base
983 5793808 : || !offset
984 207526 : || TREE_CODE (base) != MEM_REF
985 81196 : || !mem_ref_offset (base).is_constant (&mem_offset)
986 81196 : || TREE_CODE (offset) != MULT_EXPR
987 53571 : || TREE_CODE (TREE_OPERAND (offset, 1)) != INTEGER_CST
988 6001334 : || wi::umod_floor (index, BITS_PER_UNIT) != 0)
989 : return false;
990 :
991 53569 : t1 = TREE_OPERAND (base, 0);
992 53569 : c1 = offset_int::from (mem_offset, SIGNED);
993 53569 : type = TREE_TYPE (TREE_OPERAND (base, 1));
994 :
995 53569 : mult_op0 = TREE_OPERAND (offset, 0);
996 53569 : c3 = wi::to_offset (TREE_OPERAND (offset, 1));
997 :
998 53569 : if (TREE_CODE (mult_op0) == PLUS_EXPR)
999 :
1000 3821 : if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
1001 : {
1002 3369 : t2 = TREE_OPERAND (mult_op0, 0);
1003 3369 : c2 = wi::to_offset (TREE_OPERAND (mult_op0, 1));
1004 : }
1005 : else
1006 : return false;
1007 :
1008 49748 : else if (TREE_CODE (mult_op0) == MINUS_EXPR)
1009 :
1010 0 : if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
1011 : {
1012 0 : t2 = TREE_OPERAND (mult_op0, 0);
1013 0 : c2 = -wi::to_offset (TREE_OPERAND (mult_op0, 1));
1014 : }
1015 : else
1016 : return false;
1017 :
1018 : else
1019 : {
1020 49748 : t2 = mult_op0;
1021 49748 : c2 = 0;
1022 : }
1023 :
1024 53117 : c4 = index >> LOG2_BITS_PER_UNIT;
1025 53117 : c5 = backtrace_base_for_ref (&t2);
1026 :
1027 53117 : *pbase = t1;
1028 53117 : *poffset = fold_build2 (MULT_EXPR, sizetype, fold_convert (sizetype, t2),
1029 : wide_int_to_tree (sizetype, c3));
1030 53117 : *pindex = c1 + c2 * c3 + c4 + c5 * c3;
1031 53117 : *ptype = type;
1032 :
1033 53117 : return true;
1034 : }
1035 :
1036 : /* Given GS which contains a data reference, create a CAND_REF entry in
1037 : the candidate table and attempt to find a basis. */
1038 :
1039 : static void
1040 12901274 : slsr_process_ref (gassign *gs)
1041 : {
1042 12901274 : tree ref_expr, base, offset, type;
1043 12901274 : poly_int64 bitsize, bitpos;
1044 12901274 : machine_mode mode;
1045 12901274 : int unsignedp, reversep, volatilep;
1046 12901274 : slsr_cand_t c;
1047 :
1048 25802548 : if (gimple_vdef (gs))
1049 7331953 : ref_expr = gimple_assign_lhs (gs);
1050 : else
1051 5569321 : ref_expr = gimple_assign_rhs1 (gs);
1052 :
1053 12901274 : if (!handled_component_p (ref_expr)
1054 5886724 : || TREE_CODE (ref_expr) == BIT_FIELD_REF
1055 5859457 : || (TREE_CODE (ref_expr) == COMPONENT_REF
1056 5004436 : && DECL_BIT_FIELD (TREE_OPERAND (ref_expr, 1))))
1057 12848157 : return;
1058 :
1059 5794476 : base = get_inner_reference (ref_expr, &bitsize, &bitpos, &offset, &mode,
1060 : &unsignedp, &reversep, &volatilep);
1061 5794476 : HOST_WIDE_INT cbitpos;
1062 5794476 : if (reversep || !bitpos.is_constant (&cbitpos))
1063 : return;
1064 5793808 : offset_int index = cbitpos;
1065 :
1066 5793808 : if (!restructure_reference (&base, &offset, &index, &type))
1067 : return;
1068 :
1069 53117 : c = alloc_cand_and_find_basis (CAND_REF, gs, base, index, offset,
1070 : type, sizetype, 0);
1071 :
1072 : /* Add the candidate to the statement-candidate mapping. */
1073 53117 : add_cand_for_stmt (gs, c);
1074 : }
1075 :
1076 : /* Create a candidate entry for a statement GS, where GS multiplies
1077 : two SSA names BASE_IN and STRIDE_IN. Propagate any known information
1078 : about the two SSA names into the new candidate. Return the new
1079 : candidate. */
1080 :
1081 : static slsr_cand_t
1082 365726 : create_mul_ssa_cand (gimple *gs, tree base_in, tree stride_in, bool speed)
1083 : {
1084 365726 : tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1085 365726 : tree stype = NULL_TREE;
1086 365726 : offset_int index;
1087 365726 : unsigned savings = 0;
1088 365726 : slsr_cand_t c;
1089 365726 : slsr_cand_t base_cand = base_cand_from_table (base_in);
1090 :
1091 : /* Look at all interpretations of the base candidate, if necessary,
1092 : to find information to propagate into this candidate. */
1093 884452 : while (base_cand && !base && base_cand->kind != CAND_PHI)
1094 : {
1095 :
1096 153000 : if (base_cand->kind == CAND_MULT && integer_onep (base_cand->stride))
1097 : {
1098 : /* Y = (B + i') * 1
1099 : X = Y * Z
1100 : ================
1101 : X = (B + i') * Z */
1102 130 : base = base_cand->base_expr;
1103 130 : index = base_cand->index;
1104 130 : stride = stride_in;
1105 130 : ctype = base_cand->cand_type;
1106 130 : stype = TREE_TYPE (stride_in);
1107 130 : if (has_single_use (base_in))
1108 128 : savings = (base_cand->dead_savings
1109 64 : + stmt_cost (base_cand->cand_stmt, speed));
1110 : }
1111 152870 : else if (base_cand->kind == CAND_ADD
1112 110152 : && TREE_CODE (base_cand->stride) == INTEGER_CST)
1113 : {
1114 : /* Y = B + (i' * S), S constant
1115 : X = Y * Z
1116 : ============================
1117 : X = B + ((i' * S) * Z) */
1118 77586 : base = base_cand->base_expr;
1119 77586 : index = base_cand->index * wi::to_offset (base_cand->stride);
1120 77586 : stride = stride_in;
1121 77586 : ctype = base_cand->cand_type;
1122 77586 : stype = TREE_TYPE (stride_in);
1123 77586 : if (has_single_use (base_in))
1124 102430 : savings = (base_cand->dead_savings
1125 51215 : + stmt_cost (base_cand->cand_stmt, speed));
1126 : }
1127 :
1128 153000 : base_cand = lookup_cand (base_cand->next_interp);
1129 : }
1130 :
1131 365726 : if (!base)
1132 : {
1133 : /* No interpretations had anything useful to propagate, so
1134 : produce X = (Y + 0) * Z. */
1135 288010 : base = base_in;
1136 288010 : index = 0;
1137 288010 : stride = stride_in;
1138 288010 : ctype = TREE_TYPE (base_in);
1139 288010 : stype = TREE_TYPE (stride_in);
1140 : }
1141 :
1142 365726 : c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
1143 : ctype, stype, savings);
1144 365726 : return c;
1145 : }
1146 :
1147 : /* Create a candidate entry for a statement GS, where GS multiplies
1148 : SSA name BASE_IN by constant STRIDE_IN. Propagate any known
1149 : information about BASE_IN into the new candidate. Return the new
1150 : candidate. */
1151 :
1152 : static slsr_cand_t
1153 714863 : create_mul_imm_cand (gimple *gs, tree base_in, tree stride_in, bool speed)
1154 : {
1155 714863 : tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1156 714863 : offset_int index, temp;
1157 714863 : unsigned savings = 0;
1158 714863 : slsr_cand_t c;
1159 714863 : slsr_cand_t base_cand = base_cand_from_table (base_in);
1160 :
1161 : /* Look at all interpretations of the base candidate, if necessary,
1162 : to find information to propagate into this candidate. */
1163 1856818 : while (base_cand && !base && base_cand->kind != CAND_PHI)
1164 : {
1165 427092 : if (base_cand->kind == CAND_MULT
1166 38287 : && TREE_CODE (base_cand->stride) == INTEGER_CST)
1167 : {
1168 : /* Y = (B + i') * S, S constant
1169 : X = Y * c
1170 : ============================
1171 : X = (B + i') * (S * c) */
1172 15667 : temp = wi::to_offset (base_cand->stride) * wi::to_offset (stride_in);
1173 15667 : if (wi::fits_to_tree_p (temp, TREE_TYPE (stride_in)))
1174 : {
1175 15011 : base = base_cand->base_expr;
1176 15011 : index = base_cand->index;
1177 15011 : stride = wide_int_to_tree (TREE_TYPE (stride_in), temp);
1178 15011 : ctype = base_cand->cand_type;
1179 15011 : if (has_single_use (base_in))
1180 15718 : savings = (base_cand->dead_savings
1181 7859 : + stmt_cost (base_cand->cand_stmt, speed));
1182 : }
1183 : }
1184 411425 : else if (base_cand->kind == CAND_ADD && integer_onep (base_cand->stride))
1185 : {
1186 : /* Y = B + (i' * 1)
1187 : X = Y * c
1188 : ===========================
1189 : X = (B + i') * c */
1190 286066 : base = base_cand->base_expr;
1191 286066 : index = base_cand->index;
1192 286066 : stride = stride_in;
1193 286066 : ctype = base_cand->cand_type;
1194 286066 : if (has_single_use (base_in))
1195 450538 : savings = (base_cand->dead_savings
1196 225269 : + stmt_cost (base_cand->cand_stmt, speed));
1197 : }
1198 125359 : else if (base_cand->kind == CAND_ADD
1199 203269 : && base_cand->index == 1
1200 203269 : && TREE_CODE (base_cand->stride) == INTEGER_CST)
1201 : {
1202 : /* Y = B + (1 * S), S constant
1203 : X = Y * c
1204 : ===========================
1205 : X = (B + S) * c */
1206 0 : base = base_cand->base_expr;
1207 0 : index = wi::to_offset (base_cand->stride);
1208 0 : stride = stride_in;
1209 0 : ctype = base_cand->cand_type;
1210 0 : if (has_single_use (base_in))
1211 0 : savings = (base_cand->dead_savings
1212 0 : + stmt_cost (base_cand->cand_stmt, speed));
1213 : }
1214 :
1215 427092 : base_cand = lookup_cand (base_cand->next_interp);
1216 : }
1217 :
1218 714863 : if (!base)
1219 : {
1220 : /* No interpretations had anything useful to propagate, so
1221 : produce X = (Y + 0) * c. */
1222 413786 : base = base_in;
1223 413786 : index = 0;
1224 413786 : stride = stride_in;
1225 413786 : ctype = TREE_TYPE (base_in);
1226 : }
1227 :
1228 714863 : c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
1229 : ctype, sizetype, savings);
1230 714863 : return c;
1231 : }
1232 :
1233 : /* Given GS which is a multiply of scalar integers, make an appropriate
1234 : entry in the candidate table. If this is a multiply of two SSA names,
1235 : create two CAND_MULT interpretations and attempt to find a basis for
1236 : each of them. Otherwise, create a single CAND_MULT and attempt to
1237 : find a basis. */
1238 :
1239 : static void
1240 807298 : slsr_process_mul (gimple *gs, tree rhs1, tree rhs2, bool speed)
1241 : {
1242 807298 : slsr_cand_t c, c2;
1243 :
1244 : /* If this is a multiply of an SSA name with itself, it is highly
1245 : unlikely that we will get a strength reduction opportunity, so
1246 : don't record it as a candidate. This simplifies the logic for
1247 : finding a basis, so if this is removed that must be considered. */
1248 807298 : if (rhs1 == rhs2)
1249 : return;
1250 :
1251 800279 : if (TREE_CODE (rhs2) == SSA_NAME)
1252 : {
1253 : /* Record an interpretation of this statement in the candidate table
1254 : assuming RHS1 is the base expression and RHS2 is the stride. */
1255 182863 : c = create_mul_ssa_cand (gs, rhs1, rhs2, speed);
1256 :
1257 : /* Add the first interpretation to the statement-candidate mapping. */
1258 182863 : add_cand_for_stmt (gs, c);
1259 :
1260 : /* Record another interpretation of this statement assuming RHS1
1261 : is the stride and RHS2 is the base expression. */
1262 182863 : c2 = create_mul_ssa_cand (gs, rhs2, rhs1, speed);
1263 182863 : c->next_interp = c2->cand_num;
1264 182863 : c2->first_interp = c->cand_num;
1265 : }
1266 617416 : else if (TREE_CODE (rhs2) == INTEGER_CST && !integer_zerop (rhs2))
1267 : {
1268 : /* Record an interpretation for the multiply-immediate. */
1269 617416 : c = create_mul_imm_cand (gs, rhs1, rhs2, speed);
1270 :
1271 : /* Add the interpretation to the statement-candidate mapping. */
1272 617416 : add_cand_for_stmt (gs, c);
1273 : }
1274 : }
1275 :
1276 : /* Create a candidate entry for a statement GS, where GS adds two
1277 : SSA names BASE_IN and ADDEND_IN if SUBTRACT_P is false, and
1278 : subtracts ADDEND_IN from BASE_IN otherwise. Propagate any known
1279 : information about the two SSA names into the new candidate.
1280 : Return the new candidate. */
1281 :
1282 : static slsr_cand_t
1283 2012946 : create_add_ssa_cand (gimple *gs, tree base_in, tree addend_in,
1284 : bool subtract_p, bool speed)
1285 : {
1286 2012946 : tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1287 2012946 : tree stype = NULL_TREE;
1288 2012946 : offset_int index;
1289 2012946 : unsigned savings = 0;
1290 2012946 : slsr_cand_t c;
1291 2012946 : slsr_cand_t base_cand = base_cand_from_table (base_in);
1292 2012946 : slsr_cand_t addend_cand = base_cand_from_table (addend_in);
1293 :
1294 : /* The most useful transformation is a multiply-immediate feeding
1295 : an add or subtract. Look for that first. */
1296 5780877 : while (addend_cand && !base && addend_cand->kind != CAND_PHI)
1297 : {
1298 1754985 : if (addend_cand->kind == CAND_MULT
1299 1931396 : && addend_cand->index == 0
1300 2614587 : && TREE_CODE (addend_cand->stride) == INTEGER_CST)
1301 : {
1302 : /* Z = (B + 0) * S, S constant
1303 : X = Y +/- Z
1304 : ===========================
1305 : X = Y + ((+/-1 * S) * B) */
1306 683191 : base = base_in;
1307 683191 : index = wi::to_offset (addend_cand->stride);
1308 683191 : if (subtract_p)
1309 52129 : index = -index;
1310 683191 : stride = addend_cand->base_expr;
1311 683191 : ctype = TREE_TYPE (base_in);
1312 683191 : stype = addend_cand->cand_type;
1313 683191 : if (has_single_use (addend_in))
1314 1062214 : savings = (addend_cand->dead_savings
1315 531107 : + stmt_cost (addend_cand->cand_stmt, speed));
1316 : }
1317 :
1318 1754985 : addend_cand = lookup_cand (addend_cand->next_interp);
1319 : }
1320 :
1321 2823562 : while (base_cand && !base && base_cand->kind != CAND_PHI)
1322 : {
1323 810616 : if (base_cand->kind == CAND_ADD
1324 810616 : && (base_cand->index == 0
1325 441266 : || operand_equal_p (base_cand->stride,
1326 441266 : integer_zero_node, 0)))
1327 : {
1328 : /* Y = B + (i' * S), i' * S = 0
1329 : X = Y +/- Z
1330 : ============================
1331 : X = B + (+/-1 * Z) */
1332 113974 : base = base_cand->base_expr;
1333 216085 : index = subtract_p ? -1 : 1;
1334 113974 : stride = addend_in;
1335 113974 : ctype = base_cand->cand_type;
1336 227948 : stype = (TREE_CODE (addend_in) == INTEGER_CST ? sizetype
1337 113974 : : TREE_TYPE (addend_in));
1338 113974 : if (has_single_use (base_in))
1339 176760 : savings = (base_cand->dead_savings
1340 88380 : + stmt_cost (base_cand->cand_stmt, speed));
1341 : }
1342 696642 : else if (subtract_p)
1343 : {
1344 69235 : slsr_cand_t subtrahend_cand = base_cand_from_table (addend_in);
1345 :
1346 168036 : while (subtrahend_cand && !base && subtrahend_cand->kind != CAND_PHI)
1347 : {
1348 29566 : if (subtrahend_cand->kind == CAND_MULT
1349 40626 : && subtrahend_cand->index == 0
1350 40626 : && TREE_CODE (subtrahend_cand->stride) == INTEGER_CST)
1351 : {
1352 : /* Z = (B + 0) * S, S constant
1353 : X = Y - Z
1354 : ===========================
1355 : Value: X = Y + ((-1 * S) * B) */
1356 0 : base = base_in;
1357 0 : index = wi::to_offset (subtrahend_cand->stride);
1358 0 : index = -index;
1359 0 : stride = subtrahend_cand->base_expr;
1360 0 : ctype = TREE_TYPE (base_in);
1361 0 : stype = subtrahend_cand->cand_type;
1362 0 : if (has_single_use (addend_in))
1363 0 : savings = (subtrahend_cand->dead_savings
1364 0 : + stmt_cost (subtrahend_cand->cand_stmt, speed));
1365 : }
1366 :
1367 29566 : subtrahend_cand = lookup_cand (subtrahend_cand->next_interp);
1368 : }
1369 : }
1370 :
1371 810616 : base_cand = lookup_cand (base_cand->next_interp);
1372 : }
1373 :
1374 2012946 : if (!base)
1375 : {
1376 : /* No interpretations had anything useful to propagate, so
1377 : produce X = Y + (1 * Z). */
1378 1215781 : base = base_in;
1379 2242032 : index = subtract_p ? -1 : 1;
1380 1215781 : stride = addend_in;
1381 1215781 : ctype = TREE_TYPE (base_in);
1382 2431562 : stype = (TREE_CODE (addend_in) == INTEGER_CST ? sizetype
1383 1215781 : : TREE_TYPE (addend_in));
1384 : }
1385 :
1386 2012946 : c = alloc_cand_and_find_basis (CAND_ADD, gs, base, index, stride,
1387 : ctype, stype, savings);
1388 2012946 : return c;
1389 : }
1390 :
1391 : /* Create a candidate entry for a statement GS, where GS adds SSA
1392 : name BASE_IN to constant INDEX_IN. Propagate any known information
1393 : about BASE_IN into the new candidate. Return the new candidate. */
1394 :
1395 : static slsr_cand_t
1396 2018059 : create_add_imm_cand (gimple *gs, tree base_in, const offset_int &index_in,
1397 : bool speed)
1398 : {
1399 2018059 : enum cand_kind kind = CAND_ADD;
1400 2018059 : tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1401 2018059 : tree stype = NULL_TREE;
1402 2018059 : offset_int index, multiple;
1403 2018059 : unsigned savings = 0;
1404 2018059 : slsr_cand_t c;
1405 2018059 : slsr_cand_t base_cand = base_cand_from_table (base_in);
1406 :
1407 4501730 : while (base_cand && !base && base_cand->kind != CAND_PHI)
1408 : {
1409 465612 : signop sign = TYPE_SIGN (TREE_TYPE (base_cand->stride));
1410 :
1411 465612 : if (TREE_CODE (base_cand->stride) == INTEGER_CST
1412 465612 : && wi::multiple_of_p (index_in, wi::to_offset (base_cand->stride),
1413 : sign, &multiple))
1414 : {
1415 : /* Y = (B + i') * S, S constant, c = kS for some integer k
1416 : X = Y + c
1417 : ============================
1418 : X = (B + (i'+ k)) * S
1419 : OR
1420 : Y = B + (i' * S), S constant, c = kS for some integer k
1421 : X = Y + c
1422 : ============================
1423 : X = (B + (i'+ k)) * S */
1424 267443 : kind = base_cand->kind;
1425 267443 : base = base_cand->base_expr;
1426 267443 : index = base_cand->index + multiple;
1427 267443 : stride = base_cand->stride;
1428 267443 : ctype = base_cand->cand_type;
1429 267443 : stype = base_cand->stride_type;
1430 267443 : if (has_single_use (base_in))
1431 319380 : savings = (base_cand->dead_savings
1432 159690 : + stmt_cost (base_cand->cand_stmt, speed));
1433 : }
1434 :
1435 465612 : base_cand = lookup_cand (base_cand->next_interp);
1436 : }
1437 :
1438 2018059 : if (!base)
1439 : {
1440 : /* No interpretations had anything useful to propagate, so
1441 : produce X = Y + (c * 1). */
1442 1750616 : kind = CAND_ADD;
1443 1750616 : base = base_in;
1444 1750616 : index = index_in;
1445 1750616 : stride = integer_one_node;
1446 1750616 : ctype = TREE_TYPE (base_in);
1447 1750616 : stype = sizetype;
1448 : }
1449 :
1450 2018059 : c = alloc_cand_and_find_basis (kind, gs, base, index, stride,
1451 : ctype, stype, savings);
1452 2018059 : return c;
1453 : }
1454 :
1455 : /* Given GS which is an add or subtract of scalar integers or pointers,
1456 : make at least one appropriate entry in the candidate table. */
1457 :
1458 : static void
1459 3371567 : slsr_process_add (gimple *gs, tree rhs1, tree rhs2, bool speed)
1460 : {
1461 3371567 : bool subtract_p = gimple_assign_rhs_code (gs) == MINUS_EXPR;
1462 3371567 : slsr_cand_t c = NULL, c2;
1463 :
1464 3371567 : if (TREE_CODE (rhs2) == SSA_NAME)
1465 : {
1466 : /* First record an interpretation assuming RHS1 is the base expression
1467 : and RHS2 is the stride. But it doesn't make sense for the
1468 : stride to be a pointer, so don't record a candidate in that case. */
1469 1353508 : if (!POINTER_TYPE_P (TREE_TYPE (rhs2)))
1470 : {
1471 1353508 : c = create_add_ssa_cand (gs, rhs1, rhs2, subtract_p, speed);
1472 :
1473 : /* Add the first interpretation to the statement-candidate
1474 : mapping. */
1475 1353508 : add_cand_for_stmt (gs, c);
1476 : }
1477 :
1478 : /* If the two RHS operands are identical, or this is a subtract,
1479 : we're done. */
1480 1353508 : if (operand_equal_p (rhs1, rhs2, 0) || subtract_p)
1481 : return;
1482 :
1483 : /* Otherwise, record another interpretation assuming RHS2 is the
1484 : base expression and RHS1 is the stride, again provided that the
1485 : stride is not a pointer. */
1486 1099980 : if (!POINTER_TYPE_P (TREE_TYPE (rhs1)))
1487 : {
1488 659438 : c2 = create_add_ssa_cand (gs, rhs2, rhs1, false, speed);
1489 659438 : if (c)
1490 : {
1491 659438 : c->next_interp = c2->cand_num;
1492 659438 : c2->first_interp = c->cand_num;
1493 : }
1494 : else
1495 0 : add_cand_for_stmt (gs, c2);
1496 : }
1497 : }
1498 2018059 : else if (TREE_CODE (rhs2) == INTEGER_CST)
1499 : {
1500 : /* Record an interpretation for the add-immediate. */
1501 2018059 : offset_int index = wi::to_offset (rhs2);
1502 2018059 : if (subtract_p)
1503 27754 : index = -index;
1504 :
1505 2018059 : c = create_add_imm_cand (gs, rhs1, index, speed);
1506 :
1507 : /* Add the interpretation to the statement-candidate mapping. */
1508 2018059 : add_cand_for_stmt (gs, c);
1509 : }
1510 : }
1511 :
1512 : /* Given GS which is a negate of a scalar integer, make an appropriate
1513 : entry in the candidate table. A negate is equivalent to a multiply
1514 : by -1. */
1515 :
1516 : static void
1517 97447 : slsr_process_neg (gimple *gs, tree rhs1, bool speed)
1518 : {
1519 : /* Record a CAND_MULT interpretation for the multiply by -1. */
1520 97447 : slsr_cand_t c = create_mul_imm_cand (gs, rhs1, integer_minus_one_node, speed);
1521 :
1522 : /* Add the interpretation to the statement-candidate mapping. */
1523 97447 : add_cand_for_stmt (gs, c);
1524 97447 : }
1525 :
1526 : /* Help function for legal_cast_p, operating on two trees. Checks
1527 : whether it's allowable to cast from RHS to LHS. See legal_cast_p
1528 : for more details. */
1529 :
1530 : static bool
1531 2977051 : legal_cast_p_1 (tree lhs_type, tree rhs_type)
1532 : {
1533 2977051 : unsigned lhs_size, rhs_size;
1534 2977051 : bool lhs_wraps, rhs_wraps;
1535 :
1536 2977051 : lhs_size = TYPE_PRECISION (lhs_type);
1537 2977051 : rhs_size = TYPE_PRECISION (rhs_type);
1538 2977051 : lhs_wraps = ANY_INTEGRAL_TYPE_P (lhs_type) && TYPE_OVERFLOW_WRAPS (lhs_type);
1539 2977051 : rhs_wraps = ANY_INTEGRAL_TYPE_P (rhs_type) && TYPE_OVERFLOW_WRAPS (rhs_type);
1540 :
1541 2977051 : if (lhs_size < rhs_size
1542 2707602 : || (rhs_wraps && !lhs_wraps)
1543 1758083 : || (rhs_wraps && lhs_wraps && rhs_size != lhs_size))
1544 1408744 : return false;
1545 :
1546 : return true;
1547 : }
1548 :
1549 : /* Return TRUE if GS is a statement that defines an SSA name from
1550 : a conversion and is legal for us to combine with an add and multiply
1551 : in the candidate table. For example, suppose we have:
1552 :
1553 : A = B + i;
1554 : C = (type) A;
1555 : D = C * S;
1556 :
1557 : Without the type-cast, we would create a CAND_MULT for D with base B,
1558 : index i, and stride S. We want to record this candidate only if it
1559 : is equivalent to apply the type cast following the multiply:
1560 :
1561 : A = B + i;
1562 : E = A * S;
1563 : D = (type) E;
1564 :
1565 : We will record the type with the candidate for D. This allows us
1566 : to use a similar previous candidate as a basis. If we have earlier seen
1567 :
1568 : A' = B + i';
1569 : C' = (type) A';
1570 : D' = C' * S;
1571 :
1572 : we can replace D with
1573 :
1574 : D = D' + (i - i') * S;
1575 :
1576 : But if moving the type-cast would change semantics, we mustn't do this.
1577 :
1578 : This is legitimate for casts from a non-wrapping integral type to
1579 : any integral type of the same or larger size. It is not legitimate
1580 : to convert a wrapping type to a non-wrapping type, or to a wrapping
1581 : type of a different size. I.e., with a wrapping type, we must
1582 : assume that the addition B + i could wrap, in which case performing
1583 : the multiply before or after one of the "illegal" type casts will
1584 : have different semantics. */
1585 :
1586 : static bool
1587 2971786 : legal_cast_p (gassign *gs, tree rhs)
1588 : {
1589 2971786 : if (!is_gimple_assign (gs)
1590 5943572 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs)))
1591 : return false;
1592 :
1593 2971786 : return legal_cast_p_1 (TREE_TYPE (gimple_assign_lhs (gs)), TREE_TYPE (rhs));
1594 : }
1595 :
1596 : /* Given GS which is a cast to a scalar integer type, determine whether
1597 : the cast is legal for strength reduction. If so, make at least one
1598 : appropriate entry in the candidate table. */
1599 :
1600 : static void
1601 2971786 : slsr_process_cast (gassign *gs, tree rhs1, bool speed)
1602 : {
1603 2971786 : tree lhs, ctype;
1604 2971786 : slsr_cand_t base_cand, c = NULL, c2;
1605 2971786 : unsigned savings = 0;
1606 :
1607 2971786 : if (!legal_cast_p (gs, rhs1))
1608 : return;
1609 :
1610 1563829 : lhs = gimple_assign_lhs (gs);
1611 1563829 : base_cand = base_cand_from_table (rhs1);
1612 1563829 : ctype = TREE_TYPE (lhs);
1613 :
1614 1563829 : if (base_cand && base_cand->kind != CAND_PHI)
1615 : {
1616 : slsr_cand_t first_cand = NULL;
1617 :
1618 674191 : while (base_cand)
1619 : {
1620 : /* Propagate all data from the base candidate except the type,
1621 : which comes from the cast, and the base candidate's cast,
1622 : which is no longer applicable. */
1623 383408 : if (has_single_use (rhs1))
1624 424348 : savings = (base_cand->dead_savings
1625 212174 : + stmt_cost (base_cand->cand_stmt, speed));
1626 :
1627 766816 : c = alloc_cand_and_find_basis (base_cand->kind, gs,
1628 : base_cand->base_expr,
1629 383408 : base_cand->index, base_cand->stride,
1630 : ctype, base_cand->stride_type,
1631 : savings);
1632 383408 : if (!first_cand)
1633 290783 : first_cand = c;
1634 :
1635 383408 : if (first_cand != c)
1636 92625 : c->first_interp = first_cand->cand_num;
1637 :
1638 383408 : base_cand = lookup_cand (base_cand->next_interp);
1639 : }
1640 : }
1641 : else
1642 : {
1643 : /* If nothing is known about the RHS, create fresh CAND_ADD and
1644 : CAND_MULT interpretations:
1645 :
1646 : X = Y + (0 * 1)
1647 : X = (Y + 0) * 1
1648 :
1649 : The first of these is somewhat arbitrary, but the choice of
1650 : 1 for the stride simplifies the logic for propagating casts
1651 : into their uses. */
1652 1273046 : c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1, 0,
1653 : integer_one_node, ctype, sizetype, 0);
1654 1273046 : c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1, 0,
1655 : integer_one_node, ctype, sizetype, 0);
1656 1273046 : c->next_interp = c2->cand_num;
1657 1273046 : c2->first_interp = c->cand_num;
1658 : }
1659 :
1660 : /* Add the first (or only) interpretation to the statement-candidate
1661 : mapping. */
1662 1563829 : add_cand_for_stmt (gs, c);
1663 : }
1664 :
1665 : /* Given GS which is a copy of a scalar integer type, make at least one
1666 : appropriate entry in the candidate table.
1667 :
1668 : This interface is included for completeness, but is unnecessary
1669 : if this pass immediately follows a pass that performs copy
1670 : propagation, such as DOM. */
1671 :
1672 : static void
1673 157961 : slsr_process_copy (gassign *gs, tree rhs1, bool speed)
1674 : {
1675 157961 : slsr_cand_t base_cand, c = NULL, c2;
1676 157961 : unsigned savings = 0;
1677 :
1678 157961 : base_cand = base_cand_from_table (rhs1);
1679 :
1680 157961 : if (base_cand && base_cand->kind != CAND_PHI)
1681 : {
1682 : slsr_cand_t first_cand = NULL;
1683 :
1684 90887 : while (base_cand)
1685 : {
1686 : /* Propagate all data from the base candidate. */
1687 51077 : if (has_single_use (rhs1))
1688 80666 : savings = (base_cand->dead_savings
1689 40333 : + stmt_cost (base_cand->cand_stmt, speed));
1690 :
1691 102154 : c = alloc_cand_and_find_basis (base_cand->kind, gs,
1692 : base_cand->base_expr,
1693 51077 : base_cand->index, base_cand->stride,
1694 : base_cand->cand_type,
1695 : base_cand->stride_type, savings);
1696 51077 : if (!first_cand)
1697 39810 : first_cand = c;
1698 :
1699 51077 : if (first_cand != c)
1700 11267 : c->first_interp = first_cand->cand_num;
1701 :
1702 51077 : base_cand = lookup_cand (base_cand->next_interp);
1703 : }
1704 : }
1705 : else
1706 : {
1707 : /* If nothing is known about the RHS, create fresh CAND_ADD and
1708 : CAND_MULT interpretations:
1709 :
1710 : X = Y + (0 * 1)
1711 : X = (Y + 0) * 1
1712 :
1713 : The first of these is somewhat arbitrary, but the choice of
1714 : 1 for the stride simplifies the logic for propagating casts
1715 : into their uses. */
1716 118151 : c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1, 0,
1717 118151 : integer_one_node, TREE_TYPE (rhs1),
1718 : sizetype, 0);
1719 118151 : c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1, 0,
1720 118151 : integer_one_node, TREE_TYPE (rhs1),
1721 : sizetype, 0);
1722 118151 : c->next_interp = c2->cand_num;
1723 118151 : c2->first_interp = c->cand_num;
1724 : }
1725 :
1726 : /* Add the first (or only) interpretation to the statement-candidate
1727 : mapping. */
1728 157961 : add_cand_for_stmt (gs, c);
1729 157961 : }
1730 :
1731 2136520 : class find_candidates_dom_walker : public dom_walker
1732 : {
1733 : public:
1734 1068260 : find_candidates_dom_walker (cdi_direction direction)
1735 2136520 : : dom_walker (direction) {}
1736 : edge before_dom_children (basic_block) final override;
1737 : };
1738 :
1739 : /* Find strength-reduction candidates in block BB. */
1740 :
1741 : edge
1742 11287971 : find_candidates_dom_walker::before_dom_children (basic_block bb)
1743 : {
1744 11287971 : bool speed = optimize_bb_for_speed_p (bb);
1745 :
1746 15862014 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1747 4574043 : gsi_next (&gsi))
1748 4574043 : slsr_process_phi (gsi.phi (), speed);
1749 :
1750 113108391 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1751 90532449 : gsi_next (&gsi))
1752 : {
1753 90532449 : auto *gs = dyn_cast<gassign *> (gsi_stmt (gsi));
1754 25791991 : if (!gs || stmt_could_throw_p (cfun, gs))
1755 65761432 : continue;
1756 :
1757 49542034 : if (gimple_vuse (gs) && gimple_assign_single_p (gs))
1758 12901274 : slsr_process_ref (gs);
1759 :
1760 23702742 : else if ((INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (gs)))
1761 15136951 : || POINTER_TYPE_P (TREE_TYPE (gimple_assign_lhs (gs)))))
1762 : {
1763 10362771 : tree rhs1 = NULL_TREE, rhs2 = NULL_TREE;
1764 :
1765 11373680 : switch (gimple_assign_rhs_code (gs))
1766 : {
1767 3134853 : case MULT_EXPR:
1768 3134853 : case PLUS_EXPR:
1769 3134853 : rhs1 = gimple_assign_rhs1 (gs);
1770 3134853 : rhs2 = gimple_assign_rhs2 (gs);
1771 : /* Should never happen, but currently some buggy situations
1772 : in earlier phases put constants in rhs1. */
1773 3134853 : if (TREE_CODE (rhs1) != SSA_NAME)
1774 1 : continue;
1775 : break;
1776 :
1777 : /* Possible future opportunity: rhs1 of a ptr+ can be
1778 : an ADDR_EXPR. */
1779 1147410 : case POINTER_PLUS_EXPR:
1780 1147410 : case MINUS_EXPR:
1781 1147410 : rhs2 = gimple_assign_rhs2 (gs);
1782 4616389 : gcc_fallthrough ();
1783 :
1784 4616389 : CASE_CONVERT:
1785 4616389 : case SSA_NAME:
1786 4616389 : case NEGATE_EXPR:
1787 4616389 : rhs1 = gimple_assign_rhs1 (gs);
1788 4616389 : if (TREE_CODE (rhs1) != SSA_NAME)
1789 345182 : continue;
1790 : break;
1791 :
1792 10017588 : default:
1793 10017588 : ;
1794 : }
1795 :
1796 10017588 : switch (gimple_assign_rhs_code (gs))
1797 : {
1798 807298 : case MULT_EXPR:
1799 807298 : slsr_process_mul (gs, rhs1, rhs2, speed);
1800 807298 : break;
1801 :
1802 3371567 : case PLUS_EXPR:
1803 3371567 : case POINTER_PLUS_EXPR:
1804 3371567 : case MINUS_EXPR:
1805 3371567 : slsr_process_add (gs, rhs1, rhs2, speed);
1806 3371567 : break;
1807 :
1808 97447 : case NEGATE_EXPR:
1809 97447 : slsr_process_neg (gs, rhs1, speed);
1810 97447 : break;
1811 :
1812 2971786 : CASE_CONVERT:
1813 2971786 : slsr_process_cast (gs, rhs1, speed);
1814 2971786 : break;
1815 :
1816 157961 : case SSA_NAME:
1817 157961 : slsr_process_copy (gs, rhs1, speed);
1818 157961 : break;
1819 :
1820 : default:
1821 : ;
1822 : }
1823 : }
1824 : }
1825 11287971 : return NULL;
1826 : }
1827 :
1828 : /* Dump a candidate for debug. */
1829 :
1830 : static void
1831 33 : dump_candidate (slsr_cand_t c)
1832 : {
1833 33 : fprintf (dump_file, "%3d [%d] ", c->cand_num,
1834 33 : gimple_bb (c->cand_stmt)->index);
1835 33 : print_gimple_stmt (dump_file, c->cand_stmt, 0);
1836 33 : switch (c->kind)
1837 : {
1838 5 : case CAND_MULT:
1839 5 : fputs (" MULT : (", dump_file);
1840 5 : print_generic_expr (dump_file, c->base_expr);
1841 5 : fputs (" + ", dump_file);
1842 5 : print_decs (c->index, dump_file);
1843 5 : fputs (") * ", dump_file);
1844 5 : if (TREE_CODE (c->stride) != INTEGER_CST
1845 5 : && c->stride_type != TREE_TYPE (c->stride))
1846 : {
1847 0 : fputs ("(", dump_file);
1848 0 : print_generic_expr (dump_file, c->stride_type);
1849 0 : fputs (")", dump_file);
1850 : }
1851 5 : print_generic_expr (dump_file, c->stride);
1852 5 : fputs (" : ", dump_file);
1853 5 : break;
1854 17 : case CAND_ADD:
1855 17 : fputs (" ADD : ", dump_file);
1856 17 : print_generic_expr (dump_file, c->base_expr);
1857 17 : fputs (" + (", dump_file);
1858 17 : print_decs (c->index, dump_file);
1859 17 : fputs (" * ", dump_file);
1860 17 : if (TREE_CODE (c->stride) != INTEGER_CST
1861 17 : && c->stride_type != TREE_TYPE (c->stride))
1862 : {
1863 0 : fputs ("(", dump_file);
1864 0 : print_generic_expr (dump_file, c->stride_type);
1865 0 : fputs (")", dump_file);
1866 : }
1867 17 : print_generic_expr (dump_file, c->stride);
1868 17 : fputs (") : ", dump_file);
1869 17 : break;
1870 11 : case CAND_REF:
1871 11 : fputs (" REF : ", dump_file);
1872 11 : print_generic_expr (dump_file, c->base_expr);
1873 11 : fputs (" + (", dump_file);
1874 11 : print_generic_expr (dump_file, c->stride);
1875 11 : fputs (") + ", dump_file);
1876 11 : print_decs (c->index, dump_file);
1877 11 : fputs (" : ", dump_file);
1878 11 : break;
1879 0 : case CAND_PHI:
1880 0 : fputs (" PHI : ", dump_file);
1881 0 : print_generic_expr (dump_file, c->base_expr);
1882 0 : fputs (" + (unknown * ", dump_file);
1883 0 : print_generic_expr (dump_file, c->stride);
1884 0 : fputs (") : ", dump_file);
1885 0 : break;
1886 0 : default:
1887 0 : gcc_unreachable ();
1888 : }
1889 33 : print_generic_expr (dump_file, c->cand_type);
1890 33 : fprintf (dump_file, "\n basis: %d dependent: %d sibling: %d\n",
1891 : c->basis, c->dependent, c->sibling);
1892 33 : fprintf (dump_file,
1893 : " next-interp: %d first-interp: %d dead-savings: %d\n",
1894 : c->next_interp, c->first_interp, c->dead_savings);
1895 33 : if (c->def_phi)
1896 0 : fprintf (dump_file, " phi: %d\n", c->def_phi);
1897 33 : fputs ("\n", dump_file);
1898 33 : }
1899 :
1900 : /* Dump the candidate vector for debug. */
1901 :
1902 : static void
1903 3 : dump_cand_vec (void)
1904 : {
1905 3 : unsigned i;
1906 3 : slsr_cand_t c;
1907 :
1908 3 : fprintf (dump_file, "\nStrength reduction candidate vector:\n\n");
1909 :
1910 42 : FOR_EACH_VEC_ELT (cand_vec, i, c)
1911 36 : if (c != NULL)
1912 33 : dump_candidate (c);
1913 3 : }
1914 :
1915 : /* Callback used to dump the candidate chains hash table. */
1916 :
1917 : int
1918 14 : ssa_base_cand_dump_callback (cand_chain **slot, void *ignored ATTRIBUTE_UNUSED)
1919 : {
1920 14 : const_cand_chain_t chain = *slot;
1921 14 : cand_chain_t p;
1922 :
1923 14 : print_generic_expr (dump_file, chain->base_expr);
1924 14 : fprintf (dump_file, " -> %d", chain->cand->cand_num);
1925 :
1926 42 : for (p = chain->next; p; p = p->next)
1927 28 : fprintf (dump_file, " -> %d", p->cand->cand_num);
1928 :
1929 14 : fputs ("\n", dump_file);
1930 14 : return 1;
1931 : }
1932 :
1933 : /* Dump the candidate chains. */
1934 :
1935 : static void
1936 3 : dump_cand_chains (void)
1937 : {
1938 3 : fprintf (dump_file, "\nStrength reduction candidate chains:\n\n");
1939 3 : base_cand_map->traverse_noresize <void *, ssa_base_cand_dump_callback>
1940 17 : (NULL);
1941 3 : fputs ("\n", dump_file);
1942 3 : }
1943 :
1944 : /* Dump the increment vector for debug. */
1945 :
1946 : static void
1947 37153 : dump_incr_vec (void)
1948 : {
1949 37153 : if (dump_file && (dump_flags & TDF_DETAILS))
1950 : {
1951 0 : unsigned i;
1952 :
1953 0 : fprintf (dump_file, "\nIncrement vector:\n\n");
1954 :
1955 0 : for (i = 0; i < incr_vec_len; i++)
1956 : {
1957 0 : fprintf (dump_file, "%3d increment: ", i);
1958 0 : print_decs (incr_vec[i].incr, dump_file);
1959 0 : fprintf (dump_file, "\n count: %d", incr_vec[i].count);
1960 0 : fprintf (dump_file, "\n cost: %d", incr_vec[i].cost);
1961 0 : fputs ("\n initializer: ", dump_file);
1962 0 : print_generic_expr (dump_file, incr_vec[i].initializer);
1963 0 : fputs ("\n\n", dump_file);
1964 : }
1965 : }
1966 37153 : }
1967 :
1968 : /* Replace *EXPR in candidate C with an equivalent strength-reduced
1969 : data reference. */
1970 :
1971 : static void
1972 19653 : replace_ref (tree *expr, slsr_cand_t c)
1973 : {
1974 19653 : tree add_expr, mem_ref, acc_type = TREE_TYPE (*expr);
1975 19653 : unsigned HOST_WIDE_INT misalign;
1976 19653 : unsigned align;
1977 :
1978 : /* Ensure the memory reference carries the minimum alignment
1979 : requirement for the data type. See PR58041. */
1980 19653 : get_object_alignment_1 (*expr, &align, &misalign);
1981 19653 : if (misalign != 0)
1982 146 : align = least_bit_hwi (misalign);
1983 19653 : if (align < TYPE_ALIGN (acc_type))
1984 240 : acc_type = build_aligned_type (acc_type, align);
1985 :
1986 19653 : add_expr = fold_build2 (POINTER_PLUS_EXPR, c->cand_type,
1987 : c->base_expr, c->stride);
1988 19653 : mem_ref = fold_build2 (MEM_REF, acc_type, add_expr,
1989 : wide_int_to_tree (c->cand_type, c->index));
1990 :
1991 : /* Gimplify the base addressing expression for the new MEM_REF tree. */
1992 19653 : gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
1993 19653 : TREE_OPERAND (mem_ref, 0)
1994 19653 : = force_gimple_operand_gsi (&gsi, TREE_OPERAND (mem_ref, 0),
1995 : /*simple_p=*/true, NULL,
1996 : /*before=*/true, GSI_SAME_STMT);
1997 19653 : copy_ref_info (mem_ref, *expr);
1998 19653 : *expr = mem_ref;
1999 19653 : update_stmt (c->cand_stmt);
2000 19653 : }
2001 :
2002 : /* Return true if CAND_REF candidate C is a valid memory reference. */
2003 :
2004 : static bool
2005 9088 : valid_mem_ref_cand_p (slsr_cand_t c)
2006 : {
2007 9088 : if (TREE_CODE (TREE_OPERAND (c->stride, 1)) != INTEGER_CST)
2008 : return false;
2009 :
2010 9088 : struct mem_address addr
2011 18176 : = { NULL_TREE, c->base_expr, TREE_OPERAND (c->stride, 0),
2012 9088 : TREE_OPERAND (c->stride, 1), wide_int_to_tree (sizetype, c->index) };
2013 :
2014 9088 : return
2015 9088 : valid_mem_ref_p (TYPE_MODE (c->cand_type), TYPE_ADDR_SPACE (c->cand_type),
2016 : &addr);
2017 : }
2018 :
2019 : /* Replace CAND_REF candidate C, each sibling of candidate C, and each
2020 : dependent of candidate C with an equivalent strength-reduced data
2021 : reference. */
2022 :
2023 : static void
2024 8943 : replace_refs (slsr_cand_t c)
2025 : {
2026 : /* Replacing a chain of only 2 candidates which are valid memory references
2027 : is generally counter-productive because you cannot recoup the additional
2028 : calculation added in front of them. */
2029 23805 : if (c->basis == 0
2030 8451 : && c->dependent
2031 8451 : && !lookup_cand (c->dependent)->dependent
2032 4936 : && valid_mem_ref_cand_p (c)
2033 27957 : && valid_mem_ref_cand_p (lookup_cand (c->dependent)))
2034 : return;
2035 :
2036 19653 : if (dump_file && (dump_flags & TDF_DETAILS))
2037 : {
2038 9 : fputs ("Replacing reference: ", dump_file);
2039 9 : print_gimple_stmt (dump_file, c->cand_stmt, 0);
2040 : }
2041 :
2042 39306 : if (gimple_vdef (c->cand_stmt))
2043 : {
2044 6722 : tree *lhs = gimple_assign_lhs_ptr (c->cand_stmt);
2045 6722 : replace_ref (lhs, c);
2046 : }
2047 : else
2048 : {
2049 12931 : tree *rhs = gimple_assign_rhs1_ptr (c->cand_stmt);
2050 12931 : replace_ref (rhs, c);
2051 : }
2052 :
2053 19653 : if (dump_file && (dump_flags & TDF_DETAILS))
2054 : {
2055 9 : fputs ("With: ", dump_file);
2056 9 : print_gimple_stmt (dump_file, c->cand_stmt, 0);
2057 9 : fputs ("\n", dump_file);
2058 : }
2059 :
2060 19653 : if (c->sibling)
2061 492 : replace_refs (lookup_cand (c->sibling));
2062 :
2063 19653 : if (c->dependent)
2064 14862 : replace_refs (lookup_cand (c->dependent));
2065 : }
2066 :
2067 : /* Return TRUE if candidate C is dependent upon a PHI. */
2068 :
2069 : static bool
2070 2805235 : phi_dependent_cand_p (slsr_cand_t c)
2071 : {
2072 : /* A candidate is not necessarily dependent upon a PHI just because
2073 : it has a phi definition for its base name. It may have a basis
2074 : that relies upon the same phi definition, in which case the PHI
2075 : is irrelevant to this candidate. */
2076 2805235 : return (c->def_phi
2077 288 : && c->basis
2078 2805523 : && lookup_cand (c->basis)->def_phi != c->def_phi);
2079 : }
2080 :
2081 : /* Calculate the increment required for candidate C relative to
2082 : its basis. */
2083 :
2084 : static offset_int
2085 1460627 : cand_increment (slsr_cand_t c)
2086 : {
2087 1460627 : slsr_cand_t basis;
2088 :
2089 : /* If the candidate doesn't have a basis, just return its own
2090 : index. This is useful in record_increments to help us find
2091 : an existing initializer. Also, if the candidate's basis is
2092 : hidden by a phi, then its own index will be the increment
2093 : from the newly introduced phi basis. */
2094 1460627 : if (!c->basis || phi_dependent_cand_p (c))
2095 37196 : return c->index;
2096 :
2097 1423431 : basis = lookup_cand (c->basis);
2098 1423431 : gcc_assert (operand_equal_p (c->base_expr, basis->base_expr, 0));
2099 1423431 : return c->index - basis->index;
2100 : }
2101 :
2102 : /* Calculate the increment required for candidate C relative to
2103 : its basis. If we aren't going to generate pointer arithmetic
2104 : for this candidate, return the absolute value of that increment
2105 : instead. */
2106 :
2107 : static inline offset_int
2108 80627 : cand_abs_increment (slsr_cand_t c)
2109 : {
2110 80627 : offset_int increment = cand_increment (c);
2111 :
2112 80627 : if (!address_arithmetic_p && wi::neg_p (increment))
2113 2107 : increment = -increment;
2114 :
2115 80627 : return increment;
2116 : }
2117 :
2118 : /* Return TRUE iff candidate C has already been replaced under
2119 : another interpretation. */
2120 :
2121 : static inline bool
2122 1587920 : cand_already_replaced (slsr_cand_t c)
2123 : {
2124 732 : return (gimple_bb (c->cand_stmt) == 0);
2125 : }
2126 :
2127 : /* Common logic used by replace_unconditional_candidate and
2128 : replace_conditional_candidate. */
2129 :
2130 : static void
2131 1247225 : replace_mult_candidate (slsr_cand_t c, tree basis_name, offset_int bump,
2132 : auto_bitmap &sdce_worklist)
2133 : {
2134 1247225 : tree target_type = TREE_TYPE (gimple_assign_lhs (c->cand_stmt));
2135 1247225 : enum tree_code cand_code = gimple_assign_rhs_code (c->cand_stmt);
2136 :
2137 : /* It is not useful to replace casts, copies, negates, or adds of
2138 : an SSA name and a constant. */
2139 1247225 : if (cand_code == SSA_NAME
2140 : || CONVERT_EXPR_CODE_P (cand_code)
2141 : || cand_code == PLUS_EXPR
2142 : || cand_code == POINTER_PLUS_EXPR
2143 : || cand_code == MINUS_EXPR
2144 : || cand_code == NEGATE_EXPR)
2145 : return;
2146 :
2147 84903 : enum tree_code code = PLUS_EXPR;
2148 84903 : tree bump_tree;
2149 84903 : gimple *stmt_to_print = NULL;
2150 :
2151 84903 : if (wi::neg_p (bump))
2152 : {
2153 6994 : code = MINUS_EXPR;
2154 6994 : bump = -bump;
2155 : }
2156 :
2157 : /* It is possible that the resulting bump doesn't fit in target_type.
2158 : Abandon the replacement in this case. This does not affect
2159 : siblings or dependents of C. */
2160 84903 : if (bump != wi::ext (bump, TYPE_PRECISION (target_type),
2161 84903 : TYPE_SIGN (target_type)))
2162 : return;
2163 :
2164 83730 : bump_tree = wide_int_to_tree (target_type, bump);
2165 :
2166 : /* If the basis name and the candidate's LHS have incompatible types,
2167 : introduce a cast. */
2168 83730 : if (!useless_type_conversion_p (target_type, TREE_TYPE (basis_name)))
2169 4685 : basis_name = introduce_cast_before_cand (c, target_type, basis_name);
2170 :
2171 83730 : if (dump_file && (dump_flags & TDF_DETAILS))
2172 : {
2173 0 : fputs ("Replacing: ", dump_file);
2174 0 : print_gimple_stmt (dump_file, c->cand_stmt, 0);
2175 : }
2176 :
2177 83730 : if (bump == 0)
2178 : {
2179 62032 : tree lhs = gimple_assign_lhs (c->cand_stmt);
2180 62032 : gassign *copy_stmt = gimple_build_assign (lhs, basis_name);
2181 62032 : gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
2182 62032 : slsr_cand_t cc = lookup_cand (c->first_interp);
2183 62032 : gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
2184 62032 : gsi_replace (&gsi, copy_stmt, false);
2185 186096 : while (cc)
2186 : {
2187 62032 : cc->cand_stmt = copy_stmt;
2188 62032 : cc = lookup_cand (cc->next_interp);
2189 : }
2190 62032 : if (dump_file && (dump_flags & TDF_DETAILS))
2191 : stmt_to_print = copy_stmt;
2192 : }
2193 : else
2194 : {
2195 21698 : tree rhs1, rhs2;
2196 21698 : if (cand_code != NEGATE_EXPR) {
2197 21698 : rhs1 = gimple_assign_rhs1 (c->cand_stmt);
2198 21698 : rhs2 = gimple_assign_rhs2 (c->cand_stmt);
2199 : /* Mark the 2 original rhs for maybe DCEing. */
2200 21698 : if (TREE_CODE (rhs1) == SSA_NAME)
2201 21698 : bitmap_set_bit (sdce_worklist, SSA_NAME_VERSION (rhs1));
2202 21698 : if (TREE_CODE (rhs2) == SSA_NAME)
2203 0 : bitmap_set_bit (sdce_worklist, SSA_NAME_VERSION (rhs2));
2204 : }
2205 21698 : if (cand_code != NEGATE_EXPR
2206 21698 : && ((operand_equal_p (rhs1, basis_name, 0)
2207 0 : && operand_equal_p (rhs2, bump_tree, 0))
2208 21698 : || (operand_equal_p (rhs1, bump_tree, 0)
2209 0 : && operand_equal_p (rhs2, basis_name, 0))))
2210 : {
2211 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2212 : {
2213 0 : fputs ("(duplicate, not actually replacing)", dump_file);
2214 0 : stmt_to_print = c->cand_stmt;
2215 : }
2216 : }
2217 : else
2218 : {
2219 21698 : gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
2220 21698 : slsr_cand_t cc = lookup_cand (c->first_interp);
2221 21698 : tree lhs = gimple_assign_lhs (c->cand_stmt);
2222 21698 : basis_name = gimple_convert (&gsi, true, GSI_SAME_STMT,
2223 : UNKNOWN_LOCATION,
2224 21698 : TREE_TYPE (lhs), basis_name);
2225 21698 : bump_tree = gimple_convert (&gsi, true, GSI_SAME_STMT,
2226 : UNKNOWN_LOCATION,
2227 21698 : TREE_TYPE (lhs), bump_tree);
2228 21698 : gimple_assign_set_rhs_with_ops (&gsi, code, basis_name, bump_tree);
2229 21698 : update_stmt (gsi_stmt (gsi));
2230 65094 : while (cc)
2231 : {
2232 21698 : cc->cand_stmt = gsi_stmt (gsi);
2233 21698 : cc = lookup_cand (cc->next_interp);
2234 : }
2235 21698 : if (gimple_needing_rewrite_undefined (gsi_stmt (gsi)))
2236 7873 : rewrite_to_defined_unconditional (&gsi);
2237 :
2238 21698 : if (dump_file && (dump_flags & TDF_DETAILS))
2239 0 : stmt_to_print = gsi_stmt (gsi);
2240 : }
2241 : }
2242 :
2243 83730 : if (dump_file && (dump_flags & TDF_DETAILS))
2244 : {
2245 0 : fputs ("With: ", dump_file);
2246 0 : print_gimple_stmt (dump_file, stmt_to_print, 0);
2247 0 : fputs ("\n", dump_file);
2248 : }
2249 : }
2250 :
2251 : /* Replace candidate C with an add or subtract. Note that we only
2252 : operate on CAND_MULTs with known strides, so we will never generate
2253 : a POINTER_PLUS_EXPR. Each candidate X = (B + i) * S is replaced by
2254 : X = Y + ((i - i') * S), as described in the module commentary. The
2255 : folded value ((i - i') * S) is referred to here as the "bump." */
2256 :
2257 : static void
2258 1247217 : replace_unconditional_candidate (slsr_cand_t c, auto_bitmap &sdce_worklist)
2259 : {
2260 1247217 : slsr_cand_t basis;
2261 :
2262 1247217 : if (cand_already_replaced (c))
2263 : return;
2264 :
2265 1247217 : basis = lookup_cand (c->basis);
2266 1247217 : offset_int bump = cand_increment (c) * wi::to_offset (c->stride);
2267 :
2268 1247217 : replace_mult_candidate (c, gimple_assign_lhs (basis->cand_stmt), bump,
2269 : sdce_worklist);
2270 : }
2271 :
2272 : /* Return the index in the increment vector of the given INCREMENT,
2273 : or -1 if not found. The latter can occur if more than
2274 : MAX_INCR_VEC_LEN increments have been found. */
2275 :
2276 : static inline int
2277 74731 : incr_vec_index (const offset_int &increment)
2278 : {
2279 74731 : unsigned i;
2280 :
2281 133513 : for (i = 0; i < incr_vec_len && increment != incr_vec[i].incr; i++)
2282 : ;
2283 :
2284 74731 : if (i < incr_vec_len)
2285 74731 : return i;
2286 : else
2287 0 : return -1;
2288 : }
2289 :
2290 : /* Create a new statement along edge E to add BASIS_NAME to the product
2291 : of INCREMENT and the stride of candidate C. Create and return a new
2292 : SSA name from *VAR to be used as the LHS of the new statement.
2293 : KNOWN_STRIDE is true iff C's stride is a constant. */
2294 :
2295 : static tree
2296 22 : create_add_on_incoming_edge (slsr_cand_t c, tree basis_name,
2297 : offset_int increment, edge e, location_t loc,
2298 : bool known_stride)
2299 : {
2300 22 : tree lhs, basis_type;
2301 22 : gassign *new_stmt, *cast_stmt = NULL;
2302 :
2303 : /* If the add candidate along this incoming edge has the same
2304 : index as C's hidden basis, the hidden basis represents this
2305 : edge correctly. */
2306 22 : if (increment == 0)
2307 : return basis_name;
2308 :
2309 18 : basis_type = TREE_TYPE (basis_name);
2310 18 : lhs = make_temp_ssa_name (basis_type, NULL, "slsr");
2311 :
2312 : /* Occasionally people convert integers to pointers without a
2313 : cast, leading us into trouble if we aren't careful. */
2314 18 : enum tree_code plus_code
2315 18 : = POINTER_TYPE_P (basis_type) ? POINTER_PLUS_EXPR : PLUS_EXPR;
2316 :
2317 18 : if (known_stride)
2318 : {
2319 10 : tree bump_tree;
2320 10 : enum tree_code code = plus_code;
2321 10 : offset_int bump = increment * wi::to_offset (c->stride);
2322 10 : if (wi::neg_p (bump) && !POINTER_TYPE_P (basis_type))
2323 : {
2324 5 : code = MINUS_EXPR;
2325 5 : bump = -bump;
2326 : }
2327 :
2328 10 : tree stride_type = POINTER_TYPE_P (basis_type) ? sizetype : basis_type;
2329 10 : bump_tree = wide_int_to_tree (stride_type, bump);
2330 10 : new_stmt = gimple_build_assign (lhs, code, basis_name, bump_tree);
2331 : }
2332 : else
2333 : {
2334 8 : int i;
2335 8 : bool negate_incr = !POINTER_TYPE_P (basis_type) && wi::neg_p (increment);
2336 8 : i = incr_vec_index (negate_incr ? -increment : increment);
2337 8 : gcc_assert (i >= 0);
2338 :
2339 8 : if (incr_vec[i].initializer)
2340 : {
2341 8 : tree init = incr_vec[i].initializer;
2342 8 : tree wanted_type = POINTER_TYPE_P (basis_type) ? c->stride_type : basis_type;
2343 :
2344 8 : if (!types_compatible_p (TREE_TYPE (c->stride), wanted_type))
2345 : {
2346 2 : tree cast_stride = make_temp_ssa_name (wanted_type, NULL,
2347 : "slsr");
2348 2 : cast_stmt = gimple_build_assign (cast_stride, NOP_EXPR,
2349 : init);
2350 2 : init = cast_stride;
2351 : }
2352 8 : enum tree_code code = negate_incr ? MINUS_EXPR : plus_code;
2353 8 : new_stmt = gimple_build_assign (lhs, code, basis_name, init);
2354 : }
2355 : else {
2356 0 : tree stride;
2357 0 : tree wanted_type = POINTER_TYPE_P (basis_type) ? c->stride_type : basis_type;
2358 :
2359 0 : if (!types_compatible_p (TREE_TYPE (c->stride), wanted_type))
2360 : {
2361 0 : tree cast_stride = make_temp_ssa_name (wanted_type, NULL,
2362 : "slsr");
2363 0 : cast_stmt = gimple_build_assign (cast_stride, NOP_EXPR,
2364 : c->stride);
2365 0 : stride = cast_stride;
2366 : }
2367 : else
2368 0 : stride = c->stride;
2369 :
2370 0 : if (increment == 1)
2371 0 : new_stmt = gimple_build_assign (lhs, plus_code, basis_name, stride);
2372 0 : else if (increment == -1)
2373 0 : new_stmt = gimple_build_assign (lhs, MINUS_EXPR, basis_name, stride);
2374 : else
2375 0 : gcc_unreachable ();
2376 : }
2377 : }
2378 :
2379 18 : if (cast_stmt)
2380 : {
2381 2 : gimple_set_location (cast_stmt, loc);
2382 2 : gsi_insert_on_edge (e, cast_stmt);
2383 : }
2384 :
2385 18 : gimple_set_location (new_stmt, loc);
2386 18 : if (gimple_needing_rewrite_undefined (new_stmt))
2387 : {
2388 3 : gimple_seq stmts;
2389 3 : stmts = rewrite_to_defined_unconditional (new_stmt);
2390 3 : gsi_insert_seq_on_edge (e, stmts);
2391 : }
2392 : else
2393 15 : gsi_insert_on_edge (e, new_stmt);
2394 :
2395 18 : if (dump_file && (dump_flags & TDF_DETAILS))
2396 : {
2397 0 : if (cast_stmt)
2398 : {
2399 0 : fprintf (dump_file, "Inserting cast on edge %d->%d: ",
2400 0 : e->src->index, e->dest->index);
2401 0 : print_gimple_stmt (dump_file, cast_stmt, 0);
2402 : }
2403 0 : fprintf (dump_file, "Inserting on edge %d->%d: ", e->src->index,
2404 0 : e->dest->index);
2405 0 : print_gimple_stmt (dump_file, new_stmt, 0);
2406 : }
2407 :
2408 : return lhs;
2409 : }
2410 :
2411 : /* Clear the visited field for a tree of PHI candidates. */
2412 :
2413 : static void
2414 78 : clear_visited (gphi *phi)
2415 : {
2416 78 : unsigned i;
2417 78 : slsr_cand_t phi_cand = *stmt_cand_map->get (phi);
2418 :
2419 78 : if (phi_cand->visited)
2420 : {
2421 78 : phi_cand->visited = 0;
2422 :
2423 236 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2424 : {
2425 158 : tree arg = gimple_phi_arg_def (phi, i);
2426 158 : gimple *arg_def = SSA_NAME_DEF_STMT (arg);
2427 158 : if (gimple_code (arg_def) == GIMPLE_PHI)
2428 0 : clear_visited (as_a <gphi *> (arg_def));
2429 : }
2430 : }
2431 78 : }
2432 :
2433 : /* Recursive helper function for create_phi_basis. */
2434 :
2435 : static tree
2436 16 : create_phi_basis_1 (slsr_cand_t c, gimple *from_phi, tree basis_name,
2437 : location_t loc, bool known_stride)
2438 : {
2439 16 : int i;
2440 16 : tree name, phi_arg;
2441 16 : gphi *phi;
2442 16 : slsr_cand_t basis = lookup_cand (c->basis);
2443 16 : int nargs = gimple_phi_num_args (from_phi);
2444 16 : basic_block phi_bb = gimple_bb (from_phi);
2445 16 : slsr_cand_t phi_cand = *stmt_cand_map->get (from_phi);
2446 16 : auto_vec<tree> phi_args (nargs);
2447 :
2448 16 : if (phi_cand->visited)
2449 0 : return phi_cand->cached_basis;
2450 16 : phi_cand->visited = 1;
2451 :
2452 : /* Process each argument of the existing phi that represents
2453 : conditionally-executed add candidates. */
2454 45 : for (i = 0; i < nargs; i++)
2455 : {
2456 29 : edge e = (*phi_bb->preds)[i];
2457 29 : tree arg = gimple_phi_arg_def (from_phi, i);
2458 29 : tree feeding_def;
2459 :
2460 : /* If the phi argument is the base name of the CAND_PHI, then
2461 : this incoming arc should use the hidden basis. */
2462 29 : if (operand_equal_p (arg, phi_cand->base_expr, 0))
2463 7 : if (basis->index == 0)
2464 7 : feeding_def = gimple_assign_lhs (basis->cand_stmt);
2465 : else
2466 : {
2467 0 : offset_int incr = -basis->index;
2468 0 : feeding_def = create_add_on_incoming_edge (c, basis_name, incr,
2469 : e, loc, known_stride);
2470 : }
2471 : else
2472 : {
2473 22 : gimple *arg_def = SSA_NAME_DEF_STMT (arg);
2474 :
2475 : /* If there is another phi along this incoming edge, we must
2476 : process it in the same fashion to ensure that all basis
2477 : adjustments are made along its incoming edges. */
2478 22 : if (gimple_code (arg_def) == GIMPLE_PHI)
2479 0 : feeding_def = create_phi_basis_1 (c, arg_def, basis_name,
2480 : loc, known_stride);
2481 : else
2482 : {
2483 22 : slsr_cand_t arg_cand = base_cand_from_table (arg);
2484 22 : offset_int diff = arg_cand->index - basis->index;
2485 22 : feeding_def = create_add_on_incoming_edge (c, basis_name, diff,
2486 : e, loc, known_stride);
2487 : }
2488 : }
2489 :
2490 : /* Because of recursion, we need to save the arguments in a vector
2491 : so we can create the PHI statement all at once. Otherwise the
2492 : storage for the half-created PHI can be reclaimed. */
2493 29 : phi_args.safe_push (feeding_def);
2494 : }
2495 :
2496 : /* Create the new phi basis. */
2497 16 : name = make_temp_ssa_name (TREE_TYPE (basis_name), NULL, "slsr");
2498 16 : phi = create_phi_node (name, phi_bb);
2499 16 : SSA_NAME_DEF_STMT (name) = phi;
2500 :
2501 45 : FOR_EACH_VEC_ELT (phi_args, i, phi_arg)
2502 : {
2503 29 : edge e = (*phi_bb->preds)[i];
2504 29 : add_phi_arg (phi, phi_arg, e, loc);
2505 : }
2506 :
2507 16 : update_stmt (phi);
2508 :
2509 16 : if (dump_file && (dump_flags & TDF_DETAILS))
2510 : {
2511 0 : fputs ("Introducing new phi basis: ", dump_file);
2512 0 : print_gimple_stmt (dump_file, phi, 0);
2513 : }
2514 :
2515 16 : phi_cand->cached_basis = name;
2516 16 : return name;
2517 16 : }
2518 :
2519 : /* Given a candidate C with BASIS_NAME being the LHS of C's basis which
2520 : is hidden by the phi node FROM_PHI, create a new phi node in the same
2521 : block as FROM_PHI. The new phi is suitable for use as a basis by C,
2522 : with its phi arguments representing conditional adjustments to the
2523 : hidden basis along conditional incoming paths. Those adjustments are
2524 : made by creating add statements (and sometimes recursively creating
2525 : phis) along those incoming paths. LOC is the location to attach to
2526 : the introduced statements. KNOWN_STRIDE is true iff C's stride is a
2527 : constant. */
2528 :
2529 : static tree
2530 16 : create_phi_basis (slsr_cand_t c, gimple *from_phi, tree basis_name,
2531 : location_t loc, bool known_stride)
2532 : {
2533 16 : tree retval = create_phi_basis_1 (c, from_phi, basis_name, loc,
2534 : known_stride);
2535 16 : gcc_assert (retval);
2536 16 : clear_visited (as_a <gphi *> (from_phi));
2537 16 : return retval;
2538 : }
2539 :
2540 : /* Given a candidate C whose basis is hidden by at least one intervening
2541 : phi, introduce a matching number of new phis to represent its basis
2542 : adjusted by conditional increments along possible incoming paths. Then
2543 : replace C as though it were an unconditional candidate, using the new
2544 : basis. */
2545 :
2546 : static void
2547 8 : replace_conditional_candidate (slsr_cand_t c, auto_bitmap &sdce_worklist)
2548 :
2549 : {
2550 8 : tree basis_name, name;
2551 8 : slsr_cand_t basis;
2552 8 : location_t loc;
2553 :
2554 : /* Look up the LHS SSA name from C's basis. This will be the
2555 : RHS1 of the adds we will introduce to create new phi arguments. */
2556 8 : basis = lookup_cand (c->basis);
2557 8 : basis_name = gimple_assign_lhs (basis->cand_stmt);
2558 :
2559 : /* Create a new phi statement which will represent C's true basis
2560 : after the transformation is complete. */
2561 8 : loc = gimple_location (c->cand_stmt);
2562 8 : name = create_phi_basis (c, lookup_cand (c->def_phi)->cand_stmt,
2563 : basis_name, loc, KNOWN_STRIDE);
2564 :
2565 : /* Replace C with an add of the new basis phi and a constant. */
2566 8 : offset_int bump = c->index * wi::to_offset (c->stride);
2567 :
2568 8 : replace_mult_candidate (c, name, bump, sdce_worklist);
2569 8 : }
2570 :
2571 : /* Recursive helper function for phi_add_costs. SPREAD is a measure of
2572 : how many PHI nodes we have visited at this point in the tree walk. */
2573 :
2574 : static int
2575 23 : phi_add_costs_1 (gimple *phi, slsr_cand_t c, int one_add_cost, int *spread)
2576 : {
2577 23 : unsigned i;
2578 23 : int cost = 0;
2579 23 : slsr_cand_t phi_cand = *stmt_cand_map->get (phi);
2580 :
2581 23 : if (phi_cand->visited)
2582 : return 0;
2583 :
2584 23 : phi_cand->visited = 1;
2585 23 : (*spread)++;
2586 :
2587 : /* If we work our way back to a phi that isn't dominated by the hidden
2588 : basis, this isn't a candidate for replacement. Indicate this by
2589 : returning an unreasonably high cost. It's not easy to detect
2590 : these situations when determining the basis, so we defer the
2591 : decision until now. */
2592 23 : basic_block phi_bb = gimple_bb (phi);
2593 23 : slsr_cand_t basis = lookup_cand (c->basis);
2594 23 : basic_block basis_bb = gimple_bb (basis->cand_stmt);
2595 :
2596 23 : if (phi_bb == basis_bb || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
2597 : return COST_INFINITE;
2598 :
2599 74 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2600 : {
2601 51 : tree arg = gimple_phi_arg_def (phi, i);
2602 :
2603 51 : if (arg != phi_cand->base_expr)
2604 : {
2605 50 : gimple *arg_def = SSA_NAME_DEF_STMT (arg);
2606 :
2607 50 : if (gimple_code (arg_def) == GIMPLE_PHI)
2608 : {
2609 0 : cost += phi_add_costs_1 (arg_def, c, one_add_cost, spread);
2610 :
2611 0 : if (cost >= COST_INFINITE || *spread > MAX_SPREAD)
2612 : return COST_INFINITE;
2613 : }
2614 : else
2615 : {
2616 50 : slsr_cand_t arg_cand = base_cand_from_table (arg);
2617 :
2618 50 : if (arg_cand->index != c->index)
2619 47 : cost += one_add_cost;
2620 : }
2621 : }
2622 : }
2623 :
2624 : return cost;
2625 : }
2626 :
2627 : /* Compute the expected costs of inserting basis adjustments for
2628 : candidate C with phi-definition PHI. The cost of inserting
2629 : one adjustment is given by ONE_ADD_COST. If PHI has arguments
2630 : which are themselves phi results, recursively calculate costs
2631 : for those phis as well. */
2632 :
2633 : static int
2634 23 : phi_add_costs (gimple *phi, slsr_cand_t c, int one_add_cost)
2635 : {
2636 23 : int spread = 0;
2637 23 : int retval = phi_add_costs_1 (phi, c, one_add_cost, &spread);
2638 23 : clear_visited (as_a <gphi *> (phi));
2639 23 : return retval;
2640 : }
2641 : /* For candidate C, each sibling of candidate C, and each dependent of
2642 : candidate C, determine whether the candidate is dependent upon a
2643 : phi that hides its basis. If not, replace the candidate unconditionally.
2644 : Otherwise, determine whether the cost of introducing compensation code
2645 : for the candidate is offset by the gains from strength reduction. If
2646 : so, replace the candidate and introduce the compensation code. */
2647 :
2648 : static void
2649 649762 : replace_uncond_cands_and_profitable_phis (slsr_cand_t c,
2650 : auto_bitmap &sdce_worklist)
2651 : {
2652 1247267 : if (phi_dependent_cand_p (c))
2653 : {
2654 : /* A multiply candidate with a stride of 1 is just an artifice
2655 : of a copy or cast; there is no value in replacing it. */
2656 50 : if (c->kind == CAND_MULT && wi::to_offset (c->stride) != 1)
2657 : {
2658 : /* A candidate dependent upon a phi will replace a multiply by
2659 : a constant with an add, and will insert at most one add for
2660 : each phi argument. Add these costs with the potential dead-code
2661 : savings to determine profitability. */
2662 23 : bool speed = optimize_bb_for_speed_p (gimple_bb (c->cand_stmt));
2663 23 : int mult_savings = stmt_cost (c->cand_stmt, speed);
2664 23 : gimple *phi = lookup_cand (c->def_phi)->cand_stmt;
2665 23 : tree phi_result = gimple_phi_result (phi);
2666 92 : int one_add_cost = add_cost (speed,
2667 23 : TYPE_MODE (TREE_TYPE (phi_result)));
2668 23 : int add_costs = one_add_cost + phi_add_costs (phi, c, one_add_cost);
2669 23 : int cost = add_costs - mult_savings - c->dead_savings;
2670 :
2671 23 : if (dump_file && (dump_flags & TDF_DETAILS))
2672 : {
2673 0 : fprintf (dump_file, " Conditional candidate %d:\n", c->cand_num);
2674 0 : fprintf (dump_file, " add_costs = %d\n", add_costs);
2675 0 : fprintf (dump_file, " mult_savings = %d\n", mult_savings);
2676 0 : fprintf (dump_file, " dead_savings = %d\n", c->dead_savings);
2677 0 : fprintf (dump_file, " cost = %d\n", cost);
2678 0 : if (cost <= COST_NEUTRAL)
2679 0 : fputs (" Replacing...\n", dump_file);
2680 : else
2681 0 : fputs (" Not replaced.\n", dump_file);
2682 : }
2683 :
2684 23 : if (cost <= COST_NEUTRAL)
2685 8 : replace_conditional_candidate (c, sdce_worklist);
2686 : }
2687 : }
2688 : else
2689 1247217 : replace_unconditional_candidate (c, sdce_worklist);
2690 :
2691 1247267 : if (c->sibling)
2692 45393 : replace_uncond_cands_and_profitable_phis (lookup_cand (c->sibling),
2693 : sdce_worklist);
2694 :
2695 1247267 : if (c->dependent)
2696 597505 : replace_uncond_cands_and_profitable_phis (lookup_cand (c->dependent),
2697 : sdce_worklist);
2698 649762 : }
2699 :
2700 : /* Count the number of candidates in the tree rooted at C that have
2701 : not already been replaced under other interpretations. */
2702 :
2703 : static int
2704 40562 : count_candidates (slsr_cand_t c)
2705 : {
2706 111855 : unsigned count = cand_already_replaced (c) ? 0 : 1;
2707 :
2708 111855 : if (c->sibling)
2709 3409 : count += count_candidates (lookup_cand (c->sibling));
2710 :
2711 111855 : if (c->dependent)
2712 71293 : count += count_candidates (lookup_cand (c->dependent));
2713 :
2714 40562 : return count;
2715 : }
2716 :
2717 : /* Increase the count of INCREMENT by one in the increment vector.
2718 : INCREMENT is associated with candidate C. If INCREMENT is to be
2719 : conditionally executed as part of a conditional candidate replacement,
2720 : IS_PHI_ADJUST is true, otherwise false. If an initializer
2721 : T_0 = stride * I is provided by a candidate that dominates all
2722 : candidates with the same increment, also record T_0 for subsequent use. */
2723 :
2724 : static void
2725 111879 : record_increment (slsr_cand_t c, offset_int increment, bool is_phi_adjust)
2726 : {
2727 111879 : bool found = false;
2728 111879 : unsigned i;
2729 :
2730 : /* Treat increments that differ only in sign as identical so as to
2731 : share initializers, unless we are generating pointer arithmetic. */
2732 111879 : if (!address_arithmetic_p && wi::neg_p (increment))
2733 6632 : increment = -increment;
2734 :
2735 170654 : for (i = 0; i < incr_vec_len; i++)
2736 : {
2737 103933 : if (incr_vec[i].incr == increment)
2738 : {
2739 45158 : incr_vec[i].count++;
2740 45158 : found = true;
2741 :
2742 : /* If we previously recorded an initializer that doesn't
2743 : dominate this candidate, it's not going to be useful to
2744 : us after all. */
2745 45158 : if (incr_vec[i].initializer
2746 45158 : && !dominated_by_p (CDI_DOMINATORS,
2747 1372 : gimple_bb (c->cand_stmt),
2748 1372 : incr_vec[i].init_bb))
2749 : {
2750 0 : incr_vec[i].initializer = NULL_TREE;
2751 0 : incr_vec[i].init_bb = NULL;
2752 : }
2753 :
2754 : break;
2755 : }
2756 : }
2757 :
2758 66721 : if (!found && incr_vec_len < MAX_INCR_VEC_LEN - 1)
2759 : {
2760 : /* The first time we see an increment, create the entry for it.
2761 : If this is the root candidate which doesn't have a basis, set
2762 : the count to zero. We're only processing it so it can possibly
2763 : provide an initializer for other candidates. */
2764 66721 : incr_vec[incr_vec_len].incr = increment;
2765 66721 : incr_vec[incr_vec_len].count = c->basis || is_phi_adjust ? 1 : 0;
2766 66721 : incr_vec[incr_vec_len].cost = COST_INFINITE;
2767 :
2768 : /* Optimistically record the first occurrence of this increment
2769 : as providing an initializer (if it does); we will revise this
2770 : opinion later if it doesn't dominate all other occurrences.
2771 : Exception: increments of 0, 1 never need initializers;
2772 : and phi adjustments don't ever provide initializers. */
2773 66721 : if (c->kind == CAND_ADD
2774 55278 : && !is_phi_adjust
2775 55266 : && c->index == increment
2776 25791 : && (increment > 1 || increment < 0)
2777 71335 : && (gimple_assign_rhs_code (c->cand_stmt) == PLUS_EXPR
2778 3541 : || gimple_assign_rhs_code (c->cand_stmt) == POINTER_PLUS_EXPR))
2779 : {
2780 2784 : tree t0 = NULL_TREE;
2781 2784 : tree rhs1 = gimple_assign_rhs1 (c->cand_stmt);
2782 2784 : tree rhs2 = gimple_assign_rhs2 (c->cand_stmt);
2783 2784 : if (operand_equal_p (rhs1, c->base_expr, 0))
2784 : t0 = rhs2;
2785 576 : else if (operand_equal_p (rhs2, c->base_expr, 0))
2786 : t0 = rhs1;
2787 2778 : if (t0
2788 2778 : && SSA_NAME_DEF_STMT (t0)
2789 5556 : && gimple_bb (SSA_NAME_DEF_STMT (t0)))
2790 : {
2791 2778 : incr_vec[incr_vec_len].initializer = t0;
2792 2778 : incr_vec[incr_vec_len++].init_bb
2793 2778 : = gimple_bb (SSA_NAME_DEF_STMT (t0));
2794 : }
2795 : else
2796 : {
2797 6 : incr_vec[incr_vec_len].initializer = NULL_TREE;
2798 6 : incr_vec[incr_vec_len++].init_bb = NULL;
2799 : }
2800 : }
2801 : else
2802 : {
2803 63937 : incr_vec[incr_vec_len].initializer = NULL_TREE;
2804 63937 : incr_vec[incr_vec_len++].init_bb = NULL;
2805 : }
2806 : }
2807 111879 : }
2808 :
2809 : /* Recursive helper function for record_phi_increments. */
2810 :
2811 : static void
2812 12 : record_phi_increments_1 (slsr_cand_t basis, gimple *phi)
2813 : {
2814 12 : unsigned i;
2815 12 : slsr_cand_t phi_cand = *stmt_cand_map->get (phi);
2816 :
2817 12 : if (phi_cand->visited)
2818 : return;
2819 12 : phi_cand->visited = 1;
2820 :
2821 36 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2822 : {
2823 24 : tree arg = gimple_phi_arg_def (phi, i);
2824 24 : gimple *arg_def = SSA_NAME_DEF_STMT (arg);
2825 :
2826 24 : if (gimple_code (arg_def) == GIMPLE_PHI)
2827 0 : record_phi_increments_1 (basis, arg_def);
2828 : else
2829 : {
2830 24 : offset_int diff;
2831 :
2832 24 : if (operand_equal_p (arg, phi_cand->base_expr, 0))
2833 : {
2834 7 : diff = -basis->index;
2835 7 : record_increment (phi_cand, diff, PHI_ADJUST);
2836 : }
2837 : else
2838 : {
2839 17 : slsr_cand_t arg_cand = base_cand_from_table (arg);
2840 17 : diff = arg_cand->index - basis->index;
2841 17 : record_increment (arg_cand, diff, PHI_ADJUST);
2842 : }
2843 : }
2844 : }
2845 : }
2846 :
2847 : /* Given phi statement PHI that hides a candidate from its BASIS, find
2848 : the increments along each incoming arc (recursively handling additional
2849 : phis that may be present) and record them. These increments are the
2850 : difference in index between the index-adjusting statements and the
2851 : index of the basis. */
2852 :
2853 : static void
2854 12 : record_phi_increments (slsr_cand_t basis, gimple *phi)
2855 : {
2856 12 : record_phi_increments_1 (basis, phi);
2857 12 : clear_visited (as_a <gphi *> (phi));
2858 12 : }
2859 :
2860 : /* Determine how many times each unique increment occurs in the set
2861 : of candidates rooted at C's parent, recording the data in the
2862 : increment vector. For each unique increment I, if an initializer
2863 : T_0 = stride * I is provided by a candidate that dominates all
2864 : candidates with the same increment, also record T_0 for subsequent
2865 : use. */
2866 :
2867 : static void
2868 40562 : record_increments (slsr_cand_t c)
2869 : {
2870 111855 : if (!cand_already_replaced (c))
2871 : {
2872 111855 : if (!phi_dependent_cand_p (c))
2873 111843 : record_increment (c, cand_increment (c), NOT_PHI_ADJUST);
2874 : else
2875 : {
2876 : /* A candidate with a basis hidden by a phi will have one
2877 : increment for its relationship to the index represented by
2878 : the phi, and potentially additional increments along each
2879 : incoming edge. For the root of the dependency tree (which
2880 : has no basis), process just the initial index in case it has
2881 : an initializer that can be used by subsequent candidates. */
2882 12 : record_increment (c, c->index, NOT_PHI_ADJUST);
2883 :
2884 12 : if (c->basis)
2885 12 : record_phi_increments (lookup_cand (c->basis),
2886 12 : lookup_cand (c->def_phi)->cand_stmt);
2887 : }
2888 : }
2889 :
2890 111855 : if (c->sibling)
2891 3409 : record_increments (lookup_cand (c->sibling));
2892 :
2893 111855 : if (c->dependent)
2894 71293 : record_increments (lookup_cand (c->dependent));
2895 40562 : }
2896 :
2897 : /* Recursive helper function for phi_incr_cost. */
2898 :
2899 : static int
2900 15 : phi_incr_cost_1 (slsr_cand_t c, const offset_int &incr, gimple *phi,
2901 : int *savings)
2902 : {
2903 15 : unsigned i;
2904 15 : int cost = 0;
2905 15 : slsr_cand_t basis = lookup_cand (c->basis);
2906 15 : slsr_cand_t phi_cand = *stmt_cand_map->get (phi);
2907 :
2908 15 : if (phi_cand->visited)
2909 : return 0;
2910 15 : phi_cand->visited = 1;
2911 :
2912 45 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2913 : {
2914 30 : tree arg = gimple_phi_arg_def (phi, i);
2915 30 : gimple *arg_def = SSA_NAME_DEF_STMT (arg);
2916 :
2917 30 : if (gimple_code (arg_def) == GIMPLE_PHI)
2918 : {
2919 0 : int feeding_savings = 0;
2920 0 : tree feeding_var = gimple_phi_result (arg_def);
2921 0 : cost += phi_incr_cost_1 (c, incr, arg_def, &feeding_savings);
2922 0 : if (uses_consumed_by_stmt (feeding_var, phi))
2923 0 : *savings += feeding_savings;
2924 : }
2925 : else
2926 : {
2927 30 : offset_int diff;
2928 30 : slsr_cand_t arg_cand;
2929 :
2930 : /* When the PHI argument is just a pass-through to the base
2931 : expression of the hidden basis, the difference is zero minus
2932 : the index of the basis. There is no potential savings by
2933 : eliminating a statement in this case. */
2934 30 : if (operand_equal_p (arg, phi_cand->base_expr, 0))
2935 : {
2936 7 : arg_cand = (slsr_cand_t)NULL;
2937 7 : diff = -basis->index;
2938 : }
2939 : else
2940 : {
2941 23 : arg_cand = base_cand_from_table (arg);
2942 23 : diff = arg_cand->index - basis->index;
2943 : }
2944 :
2945 30 : if (incr == diff)
2946 : {
2947 14 : tree basis_lhs = gimple_assign_lhs (basis->cand_stmt);
2948 14 : cost += add_cost (true, TYPE_MODE (TREE_TYPE (basis_lhs)));
2949 14 : if (arg_cand)
2950 : {
2951 14 : tree lhs = gimple_assign_lhs (arg_cand->cand_stmt);
2952 14 : if (uses_consumed_by_stmt (lhs, phi))
2953 14 : *savings += stmt_cost (arg_cand->cand_stmt, true);
2954 : }
2955 : }
2956 : }
2957 : }
2958 :
2959 : return cost;
2960 : }
2961 :
2962 : /* Add up and return the costs of introducing add statements that
2963 : require the increment INCR on behalf of candidate C and phi
2964 : statement PHI. Accumulate into *SAVINGS the potential savings
2965 : from removing existing statements that feed PHI and have no other
2966 : uses. */
2967 :
2968 : static int
2969 15 : phi_incr_cost (slsr_cand_t c, const offset_int &incr, gimple *phi,
2970 : int *savings)
2971 : {
2972 15 : int retval = phi_incr_cost_1 (c, incr, phi, savings);
2973 15 : clear_visited (as_a <gphi *> (phi));
2974 15 : return retval;
2975 : }
2976 :
2977 : /* Return the first candidate in the tree rooted at C that has not
2978 : already been replaced, favoring siblings over dependents. */
2979 :
2980 : static slsr_cand_t
2981 37153 : unreplaced_cand_in_tree (slsr_cand_t c)
2982 : {
2983 37153 : if (!cand_already_replaced (c))
2984 : return c;
2985 :
2986 0 : if (c->sibling)
2987 : {
2988 0 : slsr_cand_t sib = unreplaced_cand_in_tree (lookup_cand (c->sibling));
2989 0 : if (sib)
2990 : return sib;
2991 : }
2992 :
2993 0 : if (c->dependent)
2994 : {
2995 0 : slsr_cand_t dep = unreplaced_cand_in_tree (lookup_cand (c->dependent));
2996 : if (dep)
2997 : return dep;
2998 : }
2999 :
3000 : return NULL;
3001 : }
3002 :
3003 : /* Return TRUE if the candidates in the tree rooted at C should be
3004 : optimized for speed, else FALSE. We estimate this based on the block
3005 : containing the most dominant candidate in the tree that has not yet
3006 : been replaced. */
3007 :
3008 : static bool
3009 37153 : optimize_cands_for_speed_p (slsr_cand_t c)
3010 : {
3011 37153 : slsr_cand_t c2 = unreplaced_cand_in_tree (c);
3012 37153 : gcc_assert (c2);
3013 37153 : return optimize_bb_for_speed_p (gimple_bb (c2->cand_stmt));
3014 : }
3015 :
3016 : /* Add COST_IN to the lowest cost of any dependent path starting at
3017 : candidate C or any of its siblings, counting only candidates along
3018 : such paths with increment INCR. Assume that replacing a candidate
3019 : reduces cost by REPL_SAVINGS. Also account for savings from any
3020 : statements that would go dead. If COUNT_PHIS is true, include
3021 : costs of introducing feeding statements for conditional candidates. */
3022 :
3023 : static int
3024 4406 : lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c,
3025 : const offset_int &incr, bool count_phis)
3026 : {
3027 4406 : int local_cost, sib_cost, savings = 0;
3028 4406 : offset_int cand_incr = cand_abs_increment (c);
3029 :
3030 4406 : if (cand_already_replaced (c))
3031 : local_cost = cost_in;
3032 4406 : else if (incr == cand_incr)
3033 3330 : local_cost = cost_in - repl_savings - c->dead_savings;
3034 : else
3035 1076 : local_cost = cost_in - c->dead_savings;
3036 :
3037 4406 : if (count_phis
3038 325 : && phi_dependent_cand_p (c)
3039 4420 : && !cand_already_replaced (c))
3040 : {
3041 14 : gimple *phi = lookup_cand (c->def_phi)->cand_stmt;
3042 14 : local_cost += phi_incr_cost (c, incr, phi, &savings);
3043 :
3044 14 : if (uses_consumed_by_stmt (gimple_phi_result (phi), c->cand_stmt))
3045 8 : local_cost -= savings;
3046 : }
3047 :
3048 4406 : if (c->dependent)
3049 1810 : local_cost = lowest_cost_path (local_cost, repl_savings,
3050 : lookup_cand (c->dependent), incr,
3051 : count_phis);
3052 :
3053 4406 : if (c->sibling)
3054 : {
3055 92 : sib_cost = lowest_cost_path (cost_in, repl_savings,
3056 : lookup_cand (c->sibling), incr,
3057 : count_phis);
3058 92 : local_cost = MIN (local_cost, sib_cost);
3059 : }
3060 :
3061 4406 : return local_cost;
3062 : }
3063 :
3064 : /* Compute the total savings that would accrue from all replacements
3065 : in the candidate tree rooted at C, counting only candidates with
3066 : increment INCR. Assume that replacing a candidate reduces cost
3067 : by REPL_SAVINGS. Also account for savings from statements that
3068 : would go dead. */
3069 :
3070 : static int
3071 253 : total_savings (int repl_savings, slsr_cand_t c, const offset_int &incr,
3072 : bool count_phis)
3073 : {
3074 253 : int savings = 0;
3075 253 : offset_int cand_incr = cand_abs_increment (c);
3076 :
3077 253 : if (incr == cand_incr && !cand_already_replaced (c))
3078 195 : savings += repl_savings + c->dead_savings;
3079 :
3080 253 : if (count_phis
3081 104 : && phi_dependent_cand_p (c)
3082 254 : && !cand_already_replaced (c))
3083 : {
3084 1 : int phi_savings = 0;
3085 1 : gimple *phi = lookup_cand (c->def_phi)->cand_stmt;
3086 1 : savings -= phi_incr_cost (c, incr, phi, &phi_savings);
3087 :
3088 1 : if (uses_consumed_by_stmt (gimple_phi_result (phi), c->cand_stmt))
3089 1 : savings += phi_savings;
3090 : }
3091 :
3092 253 : if (c->dependent)
3093 137 : savings += total_savings (repl_savings, lookup_cand (c->dependent), incr,
3094 : count_phis);
3095 :
3096 253 : if (c->sibling)
3097 0 : savings += total_savings (repl_savings, lookup_cand (c->sibling), incr,
3098 : count_phis);
3099 :
3100 253 : return savings;
3101 : }
3102 :
3103 : /* Use target-specific costs to determine and record which increments
3104 : in the current candidate tree are profitable to replace, assuming
3105 : MODE and SPEED. FIRST_DEP is the first dependent of the root of
3106 : the candidate tree.
3107 :
3108 : One slight limitation here is that we don't account for the possible
3109 : introduction of casts in some cases. See replace_one_candidate for
3110 : the cases where these are introduced. This should probably be cleaned
3111 : up sometime. */
3112 :
3113 : static void
3114 37153 : analyze_increments (slsr_cand_t first_dep, machine_mode mode, bool speed)
3115 : {
3116 37153 : unsigned i;
3117 :
3118 103874 : for (i = 0; i < incr_vec_len; i++)
3119 : {
3120 66721 : HOST_WIDE_INT incr = incr_vec[i].incr.to_shwi ();
3121 :
3122 : /* If somehow this increment is bigger than a HWI, we won't
3123 : be optimizing candidates that use it. And if the increment
3124 : has a count of zero, nothing will be done with it. */
3125 66721 : if (!wi::fits_shwi_p (incr_vec[i].incr) || !incr_vec[i].count)
3126 29026 : incr_vec[i].cost = COST_INFINITE;
3127 :
3128 : /* Increments of 0, 1, and -1 are always profitable to replace,
3129 : because they always replace a multiply or add with an add or
3130 : copy, and may cause one or more existing instructions to go
3131 : dead. Exception: -1 can't be assumed to be profitable for
3132 : pointer addition. */
3133 37695 : else if (incr == 0
3134 37695 : || incr == 1
3135 2675 : || (incr == -1
3136 16 : && !POINTER_TYPE_P (first_dep->cand_type)))
3137 35020 : incr_vec[i].cost = COST_NEUTRAL;
3138 :
3139 : /* If we need to add an initializer, give up if a cast from the
3140 : candidate's type to its stride's type can lose precision.
3141 : Note that this already takes into account that the stride may
3142 : have been cast to a wider type, in which case this test won't
3143 : fire. Example:
3144 :
3145 : short int _1;
3146 : _2 = (int) _1;
3147 : _3 = _2 * 10;
3148 : _4 = x + _3; ADD: x + (10 * (int)_1) : int
3149 : _5 = _2 * 15;
3150 : _6 = x + _5; ADD: x + (15 * (int)_1) : int
3151 :
3152 : Although the stride was a short int initially, the stride
3153 : used in the analysis has been widened to an int, and such
3154 : widening will be done in the initializer as well. */
3155 2675 : else if (!incr_vec[i].initializer
3156 2026 : && TREE_CODE (first_dep->stride) != INTEGER_CST
3157 4701 : && !legal_cast_p_1 (first_dep->stride_type,
3158 2026 : TREE_TYPE (gimple_assign_lhs
3159 : (first_dep->cand_stmt))))
3160 55 : incr_vec[i].cost = COST_INFINITE;
3161 :
3162 : /* If we need to add an initializer, make sure we don't introduce
3163 : a multiply by a pointer type, which can happen in certain cast
3164 : scenarios. */
3165 2620 : else if (!incr_vec[i].initializer
3166 1971 : && TREE_CODE (first_dep->stride) != INTEGER_CST
3167 1971 : && POINTER_TYPE_P (first_dep->stride_type))
3168 0 : incr_vec[i].cost = COST_INFINITE;
3169 :
3170 : /* For any other increment, if this is a multiply candidate, we
3171 : must introduce a temporary T and initialize it with
3172 : T_0 = stride * increment. When optimizing for speed, walk the
3173 : candidate tree to calculate the best cost reduction along any
3174 : path; if it offsets the fixed cost of inserting the initializer,
3175 : replacing the increment is profitable. When optimizing for
3176 : size, instead calculate the total cost reduction from replacing
3177 : all candidates with this increment. */
3178 2620 : else if (first_dep->kind == CAND_MULT)
3179 : {
3180 152 : int cost = mult_by_coeff_cost (incr, mode, speed);
3181 152 : int repl_savings;
3182 :
3183 152 : if (tree_fits_shwi_p (first_dep->stride))
3184 : {
3185 0 : HOST_WIDE_INT hwi_stride = tree_to_shwi (first_dep->stride);
3186 0 : repl_savings = mult_by_coeff_cost (hwi_stride, mode, speed);
3187 : }
3188 : else
3189 152 : repl_savings = mul_cost (speed, mode);
3190 152 : repl_savings -= add_cost (speed, mode);
3191 :
3192 152 : if (speed)
3193 145 : cost = lowest_cost_path (cost, repl_savings, first_dep,
3194 145 : incr_vec[i].incr, COUNT_PHIS);
3195 : else
3196 7 : cost -= total_savings (repl_savings, first_dep, incr_vec[i].incr,
3197 : COUNT_PHIS);
3198 :
3199 152 : incr_vec[i].cost = cost;
3200 : }
3201 :
3202 : /* If this is an add candidate, the initializer may already
3203 : exist, so only calculate the cost of the initializer if it
3204 : doesn't. We are replacing one add with another here, so the
3205 : known replacement savings is zero. We will account for removal
3206 : of dead instructions in lowest_cost_path or total_savings. */
3207 : else
3208 : {
3209 2468 : int cost = 0;
3210 2468 : if (!incr_vec[i].initializer)
3211 1819 : cost = mult_by_coeff_cost (incr, mode, speed);
3212 :
3213 2468 : if (speed)
3214 2359 : cost = lowest_cost_path (cost, 0, first_dep, incr_vec[i].incr,
3215 : DONT_COUNT_PHIS);
3216 : else
3217 109 : cost -= total_savings (0, first_dep, incr_vec[i].incr,
3218 : DONT_COUNT_PHIS);
3219 :
3220 2468 : incr_vec[i].cost = cost;
3221 : }
3222 : }
3223 37153 : }
3224 :
3225 : /* Return the nearest common dominator of BB1 and BB2. If the blocks
3226 : are identical, return the earlier of C1 and C2 in *WHERE. Otherwise,
3227 : if the NCD matches BB1, return C1 in *WHERE; if the NCD matches BB2,
3228 : return C2 in *WHERE; and if the NCD matches neither, return NULL in
3229 : *WHERE. Note: It is possible for one of C1 and C2 to be NULL. */
3230 :
3231 : static basic_block
3232 544 : ncd_for_two_cands (basic_block bb1, basic_block bb2,
3233 : slsr_cand_t c1, slsr_cand_t c2, slsr_cand_t *where)
3234 : {
3235 544 : basic_block ncd;
3236 :
3237 544 : if (!bb1)
3238 : {
3239 363 : *where = c2;
3240 363 : return bb2;
3241 : }
3242 :
3243 181 : if (!bb2)
3244 : {
3245 0 : *where = c1;
3246 0 : return bb1;
3247 : }
3248 :
3249 181 : ncd = nearest_common_dominator (CDI_DOMINATORS, bb1, bb2);
3250 :
3251 : /* If both candidates are in the same block, the earlier
3252 : candidate wins. */
3253 181 : if (bb1 == ncd && bb2 == ncd)
3254 : {
3255 148 : if (!c1 || (c2 && c2->cand_num < c1->cand_num))
3256 148 : *where = c2;
3257 : else
3258 0 : *where = c1;
3259 : }
3260 :
3261 : /* Otherwise, if one of them produced a candidate in the
3262 : dominator, that one wins. */
3263 33 : else if (bb1 == ncd)
3264 8 : *where = c1;
3265 :
3266 25 : else if (bb2 == ncd)
3267 14 : *where = c2;
3268 :
3269 : /* If neither matches the dominator, neither wins. */
3270 : else
3271 11 : *where = NULL;
3272 :
3273 : return ncd;
3274 : }
3275 :
3276 : /* Consider all candidates that feed PHI. Find the nearest common
3277 : dominator of those candidates requiring the given increment INCR.
3278 : Further find and return the nearest common dominator of this result
3279 : with block NCD. If the returned block contains one or more of the
3280 : candidates, return the earliest candidate in the block in *WHERE. */
3281 :
3282 : static basic_block
3283 8 : ncd_with_phi (slsr_cand_t c, const offset_int &incr, gphi *phi,
3284 : basic_block ncd, slsr_cand_t *where)
3285 : {
3286 8 : unsigned i;
3287 8 : slsr_cand_t basis = lookup_cand (c->basis);
3288 8 : slsr_cand_t phi_cand = *stmt_cand_map->get (phi);
3289 :
3290 24 : for (i = 0; i < gimple_phi_num_args (phi); i++)
3291 : {
3292 16 : tree arg = gimple_phi_arg_def (phi, i);
3293 16 : gimple *arg_def = SSA_NAME_DEF_STMT (arg);
3294 :
3295 16 : if (gimple_code (arg_def) == GIMPLE_PHI)
3296 0 : ncd = ncd_with_phi (c, incr, as_a <gphi *> (arg_def), ncd, where);
3297 : else
3298 : {
3299 16 : offset_int diff;
3300 :
3301 16 : if (operand_equal_p (arg, phi_cand->base_expr, 0))
3302 6 : diff = -basis->index;
3303 : else
3304 : {
3305 10 : slsr_cand_t arg_cand = base_cand_from_table (arg);
3306 10 : diff = arg_cand->index - basis->index;
3307 : }
3308 :
3309 16 : basic_block pred = gimple_phi_arg_edge (phi, i)->src;
3310 :
3311 25 : if ((incr == diff) || (!address_arithmetic_p && incr == -diff))
3312 8 : ncd = ncd_for_two_cands (ncd, pred, *where, NULL, where);
3313 : }
3314 : }
3315 :
3316 8 : return ncd;
3317 : }
3318 :
3319 : /* Consider the candidate C together with any candidates that feed
3320 : C's phi dependence (if any). Find and return the nearest common
3321 : dominator of those candidates requiring the given increment INCR.
3322 : If the returned block contains one or more of the candidates,
3323 : return the earliest candidate in the block in *WHERE. */
3324 :
3325 : static basic_block
3326 1266 : ncd_of_cand_and_phis (slsr_cand_t c, const offset_int &incr, slsr_cand_t *where)
3327 : {
3328 1266 : basic_block ncd = NULL;
3329 :
3330 1266 : if (cand_abs_increment (c) == incr)
3331 : {
3332 531 : ncd = gimple_bb (c->cand_stmt);
3333 531 : *where = c;
3334 : }
3335 :
3336 1266 : if (phi_dependent_cand_p (c))
3337 8 : ncd = ncd_with_phi (c, incr,
3338 8 : as_a <gphi *> (lookup_cand (c->def_phi)->cand_stmt),
3339 : ncd, where);
3340 :
3341 1266 : return ncd;
3342 : }
3343 :
3344 : /* Consider all candidates in the tree rooted at C for which INCR
3345 : represents the required increment of C relative to its basis.
3346 : Find and return the basic block that most nearly dominates all
3347 : such candidates. If the returned block contains one or more of
3348 : the candidates, return the earliest candidate in the block in
3349 : *WHERE. */
3350 :
3351 : static basic_block
3352 1266 : nearest_common_dominator_for_cands (slsr_cand_t c, const offset_int &incr,
3353 : slsr_cand_t *where)
3354 : {
3355 1266 : basic_block sib_ncd = NULL, dep_ncd = NULL, this_ncd = NULL, ncd;
3356 1266 : slsr_cand_t sib_where = NULL, dep_where = NULL, this_where = NULL, new_where;
3357 :
3358 : /* First find the NCD of all siblings and dependents. */
3359 1266 : if (c->sibling)
3360 57 : sib_ncd = nearest_common_dominator_for_cands (lookup_cand (c->sibling),
3361 : incr, &sib_where);
3362 1266 : if (c->dependent)
3363 851 : dep_ncd = nearest_common_dominator_for_cands (lookup_cand (c->dependent),
3364 : incr, &dep_where);
3365 1266 : if (!sib_ncd && !dep_ncd)
3366 : {
3367 642 : new_where = NULL;
3368 642 : ncd = NULL;
3369 : }
3370 624 : else if (sib_ncd && !dep_ncd)
3371 : {
3372 49 : new_where = sib_where;
3373 49 : ncd = sib_ncd;
3374 : }
3375 575 : else if (dep_ncd && !sib_ncd)
3376 : {
3377 575 : new_where = dep_where;
3378 575 : ncd = dep_ncd;
3379 : }
3380 : else
3381 0 : ncd = ncd_for_two_cands (sib_ncd, dep_ncd, sib_where,
3382 : dep_where, &new_where);
3383 :
3384 : /* If the candidate's increment doesn't match the one we're interested
3385 : in (and nor do any increments for feeding defs of a phi-dependence),
3386 : then the result depends only on siblings and dependents. */
3387 1266 : this_ncd = ncd_of_cand_and_phis (c, incr, &this_where);
3388 :
3389 1266 : if (!this_ncd || cand_already_replaced (c))
3390 : {
3391 730 : *where = new_where;
3392 730 : return ncd;
3393 : }
3394 :
3395 : /* Otherwise, compare this candidate with the result from all siblings
3396 : and dependents. */
3397 536 : ncd = ncd_for_two_cands (ncd, this_ncd, new_where, this_where, where);
3398 :
3399 536 : return ncd;
3400 : }
3401 :
3402 : /* Return TRUE if the increment indexed by INDEX is profitable to replace. */
3403 :
3404 : static inline bool
3405 141444 : profitable_increment_p (unsigned index)
3406 : {
3407 141444 : return (incr_vec[index].cost <= COST_NEUTRAL);
3408 : }
3409 :
3410 : /* For each profitable increment in the increment vector not equal to
3411 : 0 or 1 (or -1, for non-pointer arithmetic), find the nearest common
3412 : dominator of all statements in the candidate chain rooted at C
3413 : that require that increment, and insert an initializer
3414 : T_0 = stride * increment at that location. Record T_0 with the
3415 : increment record. */
3416 :
3417 : static void
3418 37153 : insert_initializers (slsr_cand_t c)
3419 : {
3420 37153 : unsigned i;
3421 :
3422 103874 : for (i = 0; i < incr_vec_len; i++)
3423 : {
3424 66721 : basic_block bb;
3425 66721 : slsr_cand_t where = NULL;
3426 66721 : gassign *init_stmt;
3427 66721 : gassign *cast_stmt = NULL;
3428 66721 : tree new_name, incr_tree, init_stride;
3429 66721 : offset_int incr = incr_vec[i].incr;
3430 :
3431 132435 : if (!profitable_increment_p (i)
3432 36027 : || incr == 1
3433 32081 : || (incr == -1
3434 4 : && (!POINTER_TYPE_P (lookup_cand (c->basis)->cand_type)))
3435 102748 : || incr == 0)
3436 66363 : continue;
3437 :
3438 : /* We may have already identified an existing initializer that
3439 : will suffice. */
3440 1007 : if (incr_vec[i].initializer)
3441 : {
3442 649 : if (dump_file && (dump_flags & TDF_DETAILS))
3443 : {
3444 0 : fputs ("Using existing initializer: ", dump_file);
3445 0 : print_gimple_stmt (dump_file,
3446 0 : SSA_NAME_DEF_STMT (incr_vec[i].initializer),
3447 : 0, TDF_NONE);
3448 : }
3449 649 : continue;
3450 : }
3451 :
3452 : /* Find the block that most closely dominates all candidates
3453 : with this increment. If there is at least one candidate in
3454 : that block, the earliest one will be returned in WHERE. */
3455 358 : bb = nearest_common_dominator_for_cands (c, incr, &where);
3456 :
3457 : /* If the NCD is not dominated by the block containing the
3458 : definition of the stride, we can't legally insert a
3459 : single initializer. Mark the increment as unprofitable
3460 : so we don't make any replacements. FIXME: Multiple
3461 : initializers could be placed with more analysis. */
3462 358 : gimple *stride_def = SSA_NAME_DEF_STMT (c->stride);
3463 358 : basic_block stride_bb = gimple_bb (stride_def);
3464 :
3465 358 : if (stride_bb && !dominated_by_p (CDI_DOMINATORS, bb, stride_bb))
3466 : {
3467 0 : if (dump_file && (dump_flags & TDF_DETAILS))
3468 0 : fprintf (dump_file,
3469 : "Initializer #%d cannot be legally placed\n", i);
3470 0 : incr_vec[i].cost = COST_INFINITE;
3471 0 : continue;
3472 : }
3473 :
3474 : /* If the nominal stride has a different type than the recorded
3475 : stride type, build a cast from the nominal stride to that type. */
3476 358 : if (!types_compatible_p (TREE_TYPE (c->stride), c->stride_type))
3477 : {
3478 130 : init_stride = make_temp_ssa_name (c->stride_type, NULL, "slsr");
3479 130 : cast_stmt = gimple_build_assign (init_stride, NOP_EXPR, c->stride);
3480 : }
3481 : else
3482 228 : init_stride = c->stride;
3483 :
3484 : /* Create a new SSA name to hold the initializer's value. */
3485 358 : new_name = make_temp_ssa_name (c->stride_type, NULL, "slsr");
3486 358 : incr_vec[i].initializer = new_name;
3487 :
3488 : /* Create the initializer and insert it in the latest possible
3489 : dominating position. */
3490 358 : incr_tree = wide_int_to_tree (c->stride_type, incr);
3491 358 : init_stmt = gimple_build_assign (new_name, MULT_EXPR,
3492 : init_stride, incr_tree);
3493 358 : if (where)
3494 : {
3495 344 : gimple_stmt_iterator gsi = gsi_for_stmt (where->cand_stmt);
3496 344 : location_t loc = gimple_location (where->cand_stmt);
3497 :
3498 344 : if (cast_stmt)
3499 : {
3500 130 : gsi_insert_before (&gsi, cast_stmt, GSI_SAME_STMT);
3501 130 : gimple_set_location (cast_stmt, loc);
3502 : }
3503 :
3504 344 : gimple_set_location (init_stmt, loc);
3505 344 : if (gimple_needing_rewrite_undefined (init_stmt))
3506 : {
3507 206 : gimple_seq seq;
3508 206 : seq = rewrite_to_defined_unconditional (init_stmt);
3509 206 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
3510 : }
3511 : else
3512 138 : gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
3513 : }
3514 : else
3515 : {
3516 14 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
3517 14 : gimple *basis_stmt = lookup_cand (c->basis)->cand_stmt;
3518 14 : location_t loc = gimple_location (basis_stmt);
3519 :
3520 14 : gimple_set_location (init_stmt, gimple_location (basis_stmt));
3521 14 : if (!gsi_end_p (gsi) && stmt_ends_bb_p (gsi_stmt (gsi)))
3522 : {
3523 14 : if (cast_stmt)
3524 : {
3525 0 : gsi_insert_before (&gsi, cast_stmt, GSI_SAME_STMT);
3526 0 : gimple_set_location (cast_stmt, loc);
3527 : }
3528 14 : if (gimple_needing_rewrite_undefined (init_stmt))
3529 : {
3530 9 : gimple_seq seq;
3531 9 : seq = rewrite_to_defined_unconditional (init_stmt);
3532 9 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
3533 : }
3534 : else
3535 5 : gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
3536 : }
3537 : else
3538 : {
3539 0 : if (cast_stmt)
3540 : {
3541 0 : gsi_insert_after (&gsi, cast_stmt, GSI_NEW_STMT);
3542 0 : gimple_set_location (cast_stmt, loc);
3543 : }
3544 0 : if (gimple_needing_rewrite_undefined (init_stmt))
3545 : {
3546 0 : gimple_seq seq;
3547 0 : seq = rewrite_to_defined_unconditional (init_stmt);
3548 0 : gsi_insert_seq_after (&gsi, seq, GSI_SAME_STMT);
3549 : }
3550 : else
3551 0 : gsi_insert_after (&gsi, init_stmt, GSI_SAME_STMT);
3552 : }
3553 : }
3554 :
3555 358 : if (dump_file && (dump_flags & TDF_DETAILS))
3556 : {
3557 0 : if (cast_stmt)
3558 : {
3559 0 : fputs ("Inserting stride cast: ", dump_file);
3560 0 : print_gimple_stmt (dump_file, cast_stmt, 0);
3561 : }
3562 0 : fputs ("Inserting initializer: ", dump_file);
3563 0 : print_gimple_stmt (dump_file, init_stmt, 0);
3564 : }
3565 : }
3566 37153 : }
3567 :
3568 : /* Recursive helper function for all_phi_incrs_profitable. */
3569 :
3570 : static bool
3571 12 : all_phi_incrs_profitable_1 (slsr_cand_t c, gphi *phi, int *spread)
3572 : {
3573 12 : unsigned i;
3574 12 : slsr_cand_t basis = lookup_cand (c->basis);
3575 12 : slsr_cand_t phi_cand = *stmt_cand_map->get (phi);
3576 :
3577 12 : if (phi_cand->visited)
3578 : return true;
3579 :
3580 12 : phi_cand->visited = 1;
3581 12 : (*spread)++;
3582 :
3583 : /* If the basis doesn't dominate the PHI (including when the PHI is
3584 : in the same block as the basis), we won't be able to create a PHI
3585 : using the basis here. */
3586 12 : basic_block basis_bb = gimple_bb (basis->cand_stmt);
3587 12 : basic_block phi_bb = gimple_bb (phi);
3588 :
3589 12 : if (phi_bb == basis_bb
3590 12 : || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
3591 : return false;
3592 :
3593 29 : for (i = 0; i < gimple_phi_num_args (phi); i++)
3594 : {
3595 : /* If the PHI arg resides in a block not dominated by the basis,
3596 : we won't be able to create a PHI using the basis here. */
3597 21 : basic_block pred_bb = gimple_phi_arg_edge (phi, i)->src;
3598 :
3599 21 : if (!dominated_by_p (CDI_DOMINATORS, pred_bb, basis_bb))
3600 : return false;
3601 :
3602 21 : tree arg = gimple_phi_arg_def (phi, i);
3603 21 : gimple *arg_def = SSA_NAME_DEF_STMT (arg);
3604 :
3605 21 : if (gimple_code (arg_def) == GIMPLE_PHI)
3606 : {
3607 0 : if (!all_phi_incrs_profitable_1 (c, as_a <gphi *> (arg_def), spread)
3608 0 : || *spread > MAX_SPREAD)
3609 : return false;
3610 : }
3611 : else
3612 : {
3613 21 : int j;
3614 21 : offset_int increment;
3615 :
3616 21 : if (operand_equal_p (arg, phi_cand->base_expr, 0))
3617 7 : increment = -basis->index;
3618 : else
3619 : {
3620 14 : slsr_cand_t arg_cand = base_cand_from_table (arg);
3621 14 : increment = arg_cand->index - basis->index;
3622 : }
3623 :
3624 21 : if (!address_arithmetic_p && wi::neg_p (increment))
3625 1 : increment = -increment;
3626 :
3627 21 : j = incr_vec_index (increment);
3628 :
3629 21 : if (dump_file && (dump_flags & TDF_DETAILS))
3630 : {
3631 0 : fprintf (dump_file, " Conditional candidate %d, phi: ",
3632 : c->cand_num);
3633 0 : print_gimple_stmt (dump_file, phi, 0);
3634 0 : fputs (" increment: ", dump_file);
3635 0 : print_decs (increment, dump_file);
3636 0 : if (j < 0)
3637 0 : fprintf (dump_file,
3638 : "\n Not replaced; incr_vec overflow.\n");
3639 : else {
3640 0 : fprintf (dump_file, "\n cost: %d\n", incr_vec[j].cost);
3641 0 : if (profitable_increment_p (j))
3642 0 : fputs (" Replacing...\n", dump_file);
3643 : else
3644 0 : fputs (" Not replaced.\n", dump_file);
3645 : }
3646 : }
3647 :
3648 21 : if (j < 0 || !profitable_increment_p (j))
3649 4 : return false;
3650 : }
3651 : }
3652 :
3653 : return true;
3654 : }
3655 :
3656 : /* Return TRUE iff all required increments for candidates feeding PHI
3657 : are profitable (and legal!) to replace on behalf of candidate C. */
3658 :
3659 : static bool
3660 12 : all_phi_incrs_profitable (slsr_cand_t c, gphi *phi)
3661 : {
3662 12 : int spread = 0;
3663 12 : bool retval = all_phi_incrs_profitable_1 (c, phi, &spread);
3664 12 : clear_visited (phi);
3665 12 : return retval;
3666 : }
3667 :
3668 : /* Create a NOP_EXPR that copies FROM_EXPR into a new SSA name of
3669 : type TO_TYPE, and insert it in front of the statement represented
3670 : by candidate C. Use *NEW_VAR to create the new SSA name. Return
3671 : the new SSA name. */
3672 :
3673 : static tree
3674 5867 : introduce_cast_before_cand (slsr_cand_t c, tree to_type, tree from_expr)
3675 : {
3676 5867 : tree cast_lhs;
3677 5867 : gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3678 :
3679 11734 : cast_lhs = gimple_convert (&gsi, true, GSI_SAME_STMT,
3680 5867 : gimple_location (c->cand_stmt),
3681 : to_type, from_expr);
3682 :
3683 5867 : if (dump_file && (dump_flags & TDF_DETAILS))
3684 : {
3685 0 : fputs (" Inserting(cast): ", dump_file);
3686 0 : print_generic_expr (dump_file, cast_lhs);
3687 0 : fprintf (dump_file, "\n");
3688 : }
3689 :
3690 5867 : return cast_lhs;
3691 : }
3692 :
3693 : /* Replace the RHS of the statement represented by candidate C with
3694 : NEW_CODE, NEW_RHS1, and NEW_RHS2, provided that to do so doesn't
3695 : leave C unchanged or just interchange its operands. The original
3696 : operation and operands are in OLD_CODE, OLD_RHS1, and OLD_RHS2.
3697 : If the replacement was made and we are doing a details dump,
3698 : return the revised statement, else NULL. */
3699 :
3700 : static gimple *
3701 9950 : replace_rhs_if_not_dup (enum tree_code new_code, tree new_rhs1, tree new_rhs2,
3702 : enum tree_code old_code, tree old_rhs1, tree old_rhs2,
3703 : slsr_cand_t c)
3704 : {
3705 9950 : if (new_code != old_code
3706 9950 : || ((!operand_equal_p (new_rhs1, old_rhs1, 0)
3707 0 : || !operand_equal_p (new_rhs2, old_rhs2, 0))
3708 4558 : && (!operand_equal_p (new_rhs1, old_rhs2, 0)
3709 0 : || !operand_equal_p (new_rhs2, old_rhs1, 0))))
3710 : {
3711 9950 : tree lhs = gimple_assign_lhs (c->cand_stmt);
3712 9950 : gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3713 9950 : tree rhs2_type = TREE_TYPE (lhs);
3714 9950 : if (POINTER_TYPE_P (rhs2_type))
3715 1961 : rhs2_type = sizetype;
3716 9950 : new_rhs1 = gimple_convert (&gsi, true, GSI_SAME_STMT,
3717 : UNKNOWN_LOCATION,
3718 9950 : TREE_TYPE (lhs), new_rhs1);
3719 9950 : new_rhs2 = gimple_convert (&gsi, true, GSI_SAME_STMT,
3720 : UNKNOWN_LOCATION,
3721 : rhs2_type, new_rhs2);
3722 9950 : slsr_cand_t cc = lookup_cand (c->first_interp);
3723 9950 : gimple_assign_set_rhs_with_ops (&gsi, new_code, new_rhs1, new_rhs2);
3724 9950 : update_stmt (gsi_stmt (gsi));
3725 37808 : while (cc)
3726 : {
3727 17908 : cc->cand_stmt = gsi_stmt (gsi);
3728 17908 : cc = lookup_cand (cc->next_interp);
3729 : }
3730 :
3731 9950 : if (gimple_needing_rewrite_undefined (gsi_stmt (gsi)))
3732 9047 : rewrite_to_defined_unconditional (&gsi);
3733 9950 : if (dump_file && (dump_flags & TDF_DETAILS))
3734 0 : return gsi_stmt (gsi);
3735 : }
3736 :
3737 0 : else if (dump_file && (dump_flags & TDF_DETAILS))
3738 0 : fputs (" (duplicate, not actually replacing)\n", dump_file);
3739 :
3740 : return NULL;
3741 : }
3742 :
3743 : /* Strength-reduce the statement represented by candidate C by replacing
3744 : it with an equivalent addition or subtraction. I is the index into
3745 : the increment vector identifying C's increment. NEW_VAR is used to
3746 : create a new SSA name if a cast needs to be introduced. BASIS_NAME
3747 : is the rhs1 to use in creating the add/subtract. */
3748 :
3749 : static void
3750 20940 : replace_one_candidate (slsr_cand_t c, unsigned i, tree basis_name,
3751 : auto_bitmap &sdce_worklist)
3752 : {
3753 20940 : gimple *stmt_to_print = NULL;
3754 20940 : tree orig_rhs1, orig_rhs2;
3755 20940 : tree rhs2;
3756 20940 : enum tree_code orig_code, repl_code;
3757 20940 : offset_int cand_incr;
3758 :
3759 20940 : orig_code = gimple_assign_rhs_code (c->cand_stmt);
3760 20940 : orig_rhs1 = gimple_assign_rhs1 (c->cand_stmt);
3761 20940 : orig_rhs2 = gimple_assign_rhs2 (c->cand_stmt);
3762 20940 : cand_incr = cand_increment (c);
3763 :
3764 : /* If orig_rhs2 is NULL, we have already replaced this in situ with
3765 : a copy statement under another interpretation. */
3766 20940 : if (!orig_rhs2)
3767 0 : return;
3768 :
3769 : /* Mark the 2 original rhs for maybe DCEing. */
3770 20940 : if (TREE_CODE (orig_rhs1) == SSA_NAME)
3771 20940 : bitmap_set_bit (sdce_worklist, SSA_NAME_VERSION (orig_rhs1));
3772 20940 : if (TREE_CODE (orig_rhs2) == SSA_NAME)
3773 20940 : bitmap_set_bit (sdce_worklist, SSA_NAME_VERSION (orig_rhs2));
3774 :
3775 20940 : if (dump_file && (dump_flags & TDF_DETAILS))
3776 : {
3777 0 : fputs ("Replacing: ", dump_file);
3778 0 : print_gimple_stmt (dump_file, c->cand_stmt, 0);
3779 0 : stmt_to_print = c->cand_stmt;
3780 : }
3781 :
3782 20940 : if (address_arithmetic_p)
3783 : repl_code = POINTER_PLUS_EXPR;
3784 : else
3785 18370 : repl_code = PLUS_EXPR;
3786 :
3787 : /* If the increment has an initializer T_0, replace the candidate
3788 : statement with an add of the basis name and the initializer. */
3789 20940 : if (incr_vec[i].initializer)
3790 : {
3791 1895 : tree init_type = TREE_TYPE (incr_vec[i].initializer);
3792 1895 : tree orig_type = TREE_TYPE (orig_rhs2);
3793 :
3794 1895 : if (types_compatible_p (orig_type, init_type))
3795 1895 : rhs2 = incr_vec[i].initializer;
3796 : else
3797 0 : rhs2 = introduce_cast_before_cand (c, orig_type,
3798 0 : incr_vec[i].initializer);
3799 :
3800 1895 : if (incr_vec[i].incr != cand_incr)
3801 : {
3802 158 : gcc_assert (repl_code == PLUS_EXPR);
3803 : repl_code = MINUS_EXPR;
3804 : }
3805 :
3806 1895 : stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
3807 : orig_code, orig_rhs1, orig_rhs2,
3808 : c);
3809 : }
3810 :
3811 : /* Otherwise, the increment is one of -1, 0, and 1. Replace
3812 : with a subtract of the stride from the basis name, a copy
3813 : from the basis name, or an add of the stride to the basis
3814 : name, respectively. It may be necessary to introduce a
3815 : cast (or reuse an existing cast). */
3816 19045 : else if (cand_incr == 1)
3817 : {
3818 8055 : tree stride_type = TREE_TYPE (c->stride);
3819 8055 : tree orig_type = TREE_TYPE (orig_rhs2);
3820 :
3821 8055 : if (types_compatible_p (orig_type, stride_type))
3822 6943 : rhs2 = c->stride;
3823 : else
3824 1112 : rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
3825 :
3826 8055 : stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
3827 : orig_code, orig_rhs1, orig_rhs2,
3828 : c);
3829 : }
3830 :
3831 10990 : else if (cand_incr == -1)
3832 : {
3833 413 : tree stride_type = TREE_TYPE (c->stride);
3834 413 : tree orig_type = TREE_TYPE (orig_rhs2);
3835 413 : gcc_assert (repl_code != POINTER_PLUS_EXPR);
3836 :
3837 413 : if (types_compatible_p (orig_type, stride_type))
3838 343 : rhs2 = c->stride;
3839 : else
3840 70 : rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
3841 :
3842 413 : if (orig_code != MINUS_EXPR
3843 48 : || !operand_equal_p (basis_name, orig_rhs1, 0)
3844 413 : || !operand_equal_p (rhs2, orig_rhs2, 0))
3845 : {
3846 413 : gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3847 413 : tree lhs = gimple_assign_lhs (c->cand_stmt);
3848 413 : slsr_cand_t cc = lookup_cand (c->first_interp);
3849 413 : basis_name = gimple_convert (&gsi, true, GSI_SAME_STMT,
3850 : UNKNOWN_LOCATION,
3851 413 : TREE_TYPE (lhs), basis_name);
3852 413 : rhs2 = gimple_convert (&gsi, true, GSI_SAME_STMT,
3853 : UNKNOWN_LOCATION,
3854 413 : TREE_TYPE (lhs), rhs2);
3855 413 : gimple_assign_set_rhs_with_ops (&gsi, MINUS_EXPR, basis_name, rhs2);
3856 413 : update_stmt (gsi_stmt (gsi));
3857 1604 : while (cc)
3858 : {
3859 778 : cc->cand_stmt = gsi_stmt (gsi);
3860 778 : cc = lookup_cand (cc->next_interp);
3861 : }
3862 :
3863 413 : if (dump_file && (dump_flags & TDF_DETAILS))
3864 0 : stmt_to_print = gsi_stmt (gsi);
3865 : }
3866 0 : else if (dump_file && (dump_flags & TDF_DETAILS))
3867 0 : fputs (" (duplicate, not actually replacing)\n", dump_file);
3868 : }
3869 :
3870 10577 : else if (cand_incr == 0)
3871 : {
3872 10577 : tree lhs = gimple_assign_lhs (c->cand_stmt);
3873 10577 : tree lhs_type = TREE_TYPE (lhs);
3874 10577 : tree basis_type = TREE_TYPE (basis_name);
3875 :
3876 10577 : if (types_compatible_p (lhs_type, basis_type))
3877 : {
3878 10521 : gassign *copy_stmt = gimple_build_assign (lhs, basis_name);
3879 10521 : gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3880 10521 : slsr_cand_t cc = lookup_cand (c->first_interp);
3881 10521 : gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
3882 10521 : gsi_replace (&gsi, copy_stmt, false);
3883 39505 : while (cc)
3884 : {
3885 18463 : cc->cand_stmt = copy_stmt;
3886 18463 : cc = lookup_cand (cc->next_interp);
3887 : }
3888 :
3889 10521 : if (dump_file && (dump_flags & TDF_DETAILS))
3890 : stmt_to_print = copy_stmt;
3891 : }
3892 : else
3893 : {
3894 56 : gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3895 56 : gassign *cast_stmt = gimple_build_assign (lhs, NOP_EXPR, basis_name);
3896 56 : slsr_cand_t cc = lookup_cand (c->first_interp);
3897 56 : gimple_set_location (cast_stmt, gimple_location (c->cand_stmt));
3898 56 : gsi_replace (&gsi, cast_stmt, false);
3899 173 : while (cc)
3900 : {
3901 61 : cc->cand_stmt = cast_stmt;
3902 61 : cc = lookup_cand (cc->next_interp);
3903 : }
3904 :
3905 56 : if (dump_file && (dump_flags & TDF_DETAILS))
3906 : stmt_to_print = cast_stmt;
3907 : }
3908 : }
3909 : else
3910 0 : gcc_unreachable ();
3911 :
3912 20940 : if (dump_file && (dump_flags & TDF_DETAILS) && stmt_to_print)
3913 : {
3914 0 : fputs ("With: ", dump_file);
3915 0 : print_gimple_stmt (dump_file, stmt_to_print, 0);
3916 0 : fputs ("\n", dump_file);
3917 : }
3918 : }
3919 :
3920 : /* For each candidate in the tree rooted at C, replace it with
3921 : an increment if such has been shown to be profitable. */
3922 :
3923 : static void
3924 40562 : replace_profitable_candidates (slsr_cand_t c, auto_bitmap &sdce_worklist)
3925 : {
3926 74702 : if (!cand_already_replaced (c))
3927 : {
3928 74702 : offset_int increment = cand_abs_increment (c);
3929 74702 : enum tree_code orig_code = gimple_assign_rhs_code (c->cand_stmt);
3930 74702 : int i;
3931 :
3932 74702 : i = incr_vec_index (increment);
3933 :
3934 : /* Only process profitable increments. Nothing useful can be done
3935 : to a cast or copy. */
3936 74702 : if (i >= 0
3937 74702 : && profitable_increment_p (i)
3938 72999 : && orig_code != SSA_NAME
3939 133003 : && !CONVERT_EXPR_CODE_P (orig_code))
3940 : {
3941 20944 : if (phi_dependent_cand_p (c))
3942 : {
3943 12 : gphi *phi = as_a <gphi *> (lookup_cand (c->def_phi)->cand_stmt);
3944 :
3945 12 : if (all_phi_incrs_profitable (c, phi))
3946 : {
3947 : /* Look up the LHS SSA name from C's basis. This will be
3948 : the RHS1 of the adds we will introduce to create new
3949 : phi arguments. */
3950 8 : slsr_cand_t basis = lookup_cand (c->basis);
3951 8 : tree basis_name = gimple_assign_lhs (basis->cand_stmt);
3952 :
3953 : /* Create a new phi statement that will represent C's true
3954 : basis after the transformation is complete. */
3955 8 : location_t loc = gimple_location (c->cand_stmt);
3956 8 : tree name = create_phi_basis (c, phi, basis_name,
3957 : loc, UNKNOWN_STRIDE);
3958 :
3959 : /* Replace C with an add of the new basis phi and the
3960 : increment. */
3961 8 : replace_one_candidate (c, i, name, sdce_worklist);
3962 : }
3963 : }
3964 : else
3965 : {
3966 20932 : slsr_cand_t basis = lookup_cand (c->basis);
3967 20932 : tree basis_name = gimple_assign_lhs (basis->cand_stmt);
3968 20932 : replace_one_candidate (c, i, basis_name, sdce_worklist);
3969 : }
3970 : }
3971 : }
3972 :
3973 74702 : if (c->sibling)
3974 3409 : replace_profitable_candidates (lookup_cand (c->sibling), sdce_worklist);
3975 :
3976 74702 : if (c->dependent)
3977 34140 : replace_profitable_candidates (lookup_cand (c->dependent), sdce_worklist);
3978 40562 : }
3979 :
3980 : /* Analyze costs of related candidates in the candidate vector,
3981 : and make beneficial replacements. */
3982 :
3983 : static void
3984 1068260 : analyze_candidates_and_replace (void)
3985 : {
3986 1068260 : unsigned i;
3987 1068260 : slsr_cand_t c;
3988 1068260 : auto_bitmap simple_dce_worklist;
3989 :
3990 : /* Each candidate that has a null basis and a non-null
3991 : dependent is the root of a tree of related statements.
3992 : Analyze each tree to determine a subset of those
3993 : statements that can be replaced with maximum benefit.
3994 :
3995 : Note the first NULL element is skipped. */
3996 9460546 : FOR_EACH_VEC_ELT_FROM (cand_vec, i, c, 1)
3997 : {
3998 8392286 : slsr_cand_t first_dep;
3999 :
4000 8392286 : if (c->basis != 0 || c->dependent == 0)
4001 7742313 : continue;
4002 :
4003 649973 : if (dump_file && (dump_flags & TDF_DETAILS))
4004 7 : fprintf (dump_file, "\nProcessing dependency tree rooted at %d.\n",
4005 : c->cand_num);
4006 :
4007 649973 : first_dep = lookup_cand (c->dependent);
4008 :
4009 : /* If this is a chain of CAND_REFs, unconditionally replace
4010 : each of them with a strength-reduced data reference. */
4011 649973 : if (c->kind == CAND_REF)
4012 8451 : replace_refs (c);
4013 :
4014 : /* If the common stride of all related candidates is a known
4015 : constant, each candidate without a phi-dependence can be
4016 : profitably replaced. Each replaces a multiply by a single
4017 : add, with the possibility that a feeding add also goes dead.
4018 : A candidate with a phi-dependence is replaced only if the
4019 : compensation code it requires is offset by the strength
4020 : reduction savings. */
4021 641522 : else if (TREE_CODE (c->stride) == INTEGER_CST)
4022 604369 : replace_uncond_cands_and_profitable_phis (first_dep,
4023 : simple_dce_worklist);
4024 :
4025 : /* When the stride is an SSA name, it may still be profitable
4026 : to replace some or all of the dependent candidates, depending
4027 : on whether the introduced increments can be reused, or are
4028 : less expensive to calculate than the replaced statements. */
4029 : else
4030 : {
4031 37153 : machine_mode mode;
4032 37153 : bool speed;
4033 :
4034 : /* Determine whether we'll be generating pointer arithmetic
4035 : when replacing candidates. */
4036 74306 : address_arithmetic_p = (c->kind == CAND_ADD
4037 37153 : && POINTER_TYPE_P (c->cand_type));
4038 :
4039 : /* If all candidates have already been replaced under other
4040 : interpretations, nothing remains to be done. */
4041 37153 : if (!count_candidates (c))
4042 0 : continue;
4043 :
4044 : /* Construct an array of increments for this candidate chain. */
4045 37153 : incr_vec = XNEWVEC (incr_info, MAX_INCR_VEC_LEN);
4046 37153 : incr_vec_len = 0;
4047 37153 : record_increments (c);
4048 :
4049 : /* Determine which increments are profitable to replace. */
4050 37153 : mode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (c->cand_stmt)));
4051 37153 : speed = optimize_cands_for_speed_p (c);
4052 37153 : analyze_increments (first_dep, mode, speed);
4053 :
4054 : /* Insert initializers of the form T_0 = stride * increment
4055 : for use in profitable replacements. */
4056 37153 : insert_initializers (first_dep);
4057 37153 : dump_incr_vec ();
4058 :
4059 : /* Perform the replacements. */
4060 37153 : replace_profitable_candidates (first_dep, simple_dce_worklist);
4061 37153 : free (incr_vec);
4062 : }
4063 : }
4064 :
4065 : /* For conditional candidates, we may have uncommitted insertions
4066 : on edges to clean up. */
4067 1068260 : gsi_commit_edge_inserts ();
4068 :
4069 1068260 : simple_dce_from_worklist (simple_dce_worklist);
4070 1068260 : }
4071 :
4072 : namespace {
4073 :
4074 : const pass_data pass_data_strength_reduction =
4075 : {
4076 : GIMPLE_PASS, /* type */
4077 : "slsr", /* name */
4078 : OPTGROUP_NONE, /* optinfo_flags */
4079 : TV_GIMPLE_SLSR, /* tv_id */
4080 : ( PROP_cfg | PROP_ssa ), /* properties_required */
4081 : 0, /* properties_provided */
4082 : 0, /* properties_destroyed */
4083 : 0, /* todo_flags_start */
4084 : 0, /* todo_flags_finish */
4085 : };
4086 :
4087 : class pass_strength_reduction : public gimple_opt_pass
4088 : {
4089 : public:
4090 302985 : pass_strength_reduction (gcc::context *ctxt)
4091 605970 : : gimple_opt_pass (pass_data_strength_reduction, ctxt)
4092 : {}
4093 :
4094 : /* opt_pass methods: */
4095 1068301 : bool gate (function *) final override { return flag_tree_slsr; }
4096 : unsigned int execute (function *) final override;
4097 :
4098 : }; // class pass_strength_reduction
4099 :
4100 : unsigned
4101 1068260 : pass_strength_reduction::execute (function *fun)
4102 : {
4103 : /* Create the obstack where candidates will reside. */
4104 1068260 : gcc_obstack_init (&cand_obstack);
4105 :
4106 : /* Allocate the candidate vector and initialize the first NULL element. */
4107 1068260 : cand_vec.create (128);
4108 1068260 : cand_vec.safe_push (NULL);
4109 :
4110 : /* Allocate the mapping from statements to candidate indices. */
4111 1068260 : stmt_cand_map = new hash_map<gimple *, slsr_cand_t>;
4112 :
4113 : /* Create the obstack where candidate chains will reside. */
4114 1068260 : gcc_obstack_init (&chain_obstack);
4115 :
4116 : /* Allocate the mapping from base expressions to candidate chains. */
4117 1068260 : base_cand_map = new hash_table<cand_chain_hasher> (500);
4118 :
4119 : /* Allocate the mapping from bases to alternative bases. */
4120 1068260 : alt_base_map = new hash_map<tree, tree>;
4121 :
4122 : /* Initialize the loop optimizer. We need to detect flow across
4123 : back edges, and this gives us dominator information as well. */
4124 1068260 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4125 :
4126 : /* Walk the CFG in predominator order looking for strength reduction
4127 : candidates. */
4128 1068260 : find_candidates_dom_walker (CDI_DOMINATORS)
4129 1068260 : .walk (fun->cfg->x_entry_block_ptr);
4130 :
4131 1068260 : if (dump_file && (dump_flags & TDF_DETAILS))
4132 : {
4133 3 : dump_cand_vec ();
4134 3 : dump_cand_chains ();
4135 : }
4136 :
4137 2136520 : delete alt_base_map;
4138 1068260 : free_affine_expand_cache (&name_expansions);
4139 :
4140 : /* Analyze costs and make appropriate replacements. */
4141 1068260 : analyze_candidates_and_replace ();
4142 :
4143 1068260 : loop_optimizer_finalize ();
4144 1068260 : delete base_cand_map;
4145 1068260 : base_cand_map = NULL;
4146 1068260 : obstack_free (&chain_obstack, NULL);
4147 2136520 : delete stmt_cand_map;
4148 1068260 : cand_vec.release ();
4149 1068260 : obstack_free (&cand_obstack, NULL);
4150 :
4151 1068260 : return 0;
4152 : }
4153 :
4154 : } // anon namespace
4155 :
4156 : gimple_opt_pass *
4157 302985 : make_pass_strength_reduction (gcc::context *ctxt)
4158 : {
4159 302985 : return new pass_strength_reduction (ctxt);
4160 : }
|