[ARM][doc] Remove mention of Advanced RISC Machines
[official-gcc.git] / gcc / gimple-ssa-strength-reduction.c
blob945075c2517633217a41715789f30d57c3f34de6
1 /* Straight-line strength reduction.
2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
3 Contributed by Bill Schmidt, IBM <wschmidt@linux.ibm.com>
5 This file is part of GCC.
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.
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.
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/>. */
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.
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.
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. */
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tree.h"
40 #include "hash-map.h"
41 #include "hash-table.h"
42 #include "predict.h"
43 #include "vec.h"
44 #include "hashtab.h"
45 #include "hash-set.h"
46 #include "machmode.h"
47 #include "tm.h"
48 #include "hard-reg-set.h"
49 #include "input.h"
50 #include "function.h"
51 #include "dominance.h"
52 #include "cfg.h"
53 #include "basic-block.h"
54 #include "tree-ssa-alias.h"
55 #include "internal-fn.h"
56 #include "gimple-expr.h"
57 #include "is-a.h"
58 #include "gimple.h"
59 #include "gimple-iterator.h"
60 #include "gimplify-me.h"
61 #include "stor-layout.h"
62 #include "expr.h"
63 #include "tree-pass.h"
64 #include "cfgloop.h"
65 #include "gimple-pretty-print.h"
66 #include "gimple-ssa.h"
67 #include "tree-cfg.h"
68 #include "tree-phinodes.h"
69 #include "ssa-iterators.h"
70 #include "stringpool.h"
71 #include "tree-ssanames.h"
72 #include "domwalk.h"
73 #include "expmed.h"
74 #include "params.h"
75 #include "tree-ssa-address.h"
76 #include "tree-affine.h"
77 #include "wide-int-print.h"
78 #include "builtins.h"
80 /* Information about a strength reduction candidate. Each statement
81 in the candidate table represents an expression of one of the
82 following forms (the special case of CAND_REF will be described
83 later):
85 (CAND_MULT) S1: X = (B + i) * S
86 (CAND_ADD) S1: X = B + (i * S)
88 Here X and B are SSA names, i is an integer constant, and S is
89 either an SSA name or a constant. We call B the "base," i the
90 "index", and S the "stride."
92 Any statement S0 that dominates S1 and is of the form:
94 (CAND_MULT) S0: Y = (B + i') * S
95 (CAND_ADD) S0: Y = B + (i' * S)
97 is called a "basis" for S1. In both cases, S1 may be replaced by
99 S1': X = Y + (i - i') * S,
101 where (i - i') * S is folded to the extent possible.
103 All gimple statements are visited in dominator order, and each
104 statement that may contribute to one of the forms of S1 above is
105 given at least one entry in the candidate table. Such statements
106 include addition, pointer addition, subtraction, multiplication,
107 negation, copies, and nontrivial type casts. If a statement may
108 represent more than one expression of the forms of S1 above,
109 multiple "interpretations" are stored in the table and chained
110 together. Examples:
112 * An add of two SSA names may treat either operand as the base.
113 * A multiply of two SSA names, likewise.
114 * A copy or cast may be thought of as either a CAND_MULT with
115 i = 0 and S = 1, or as a CAND_ADD with i = 0 or S = 0.
117 Candidate records are allocated from an obstack. They are addressed
118 both from a hash table keyed on S1, and from a vector of candidate
119 pointers arranged in predominator order.
121 Opportunity note
122 ----------------
123 Currently we don't recognize:
125 S0: Y = (S * i') - B
126 S1: X = (S * i) - B
128 as a strength reduction opportunity, even though this S1 would
129 also be replaceable by the S1' above. This can be added if it
130 comes up in practice.
132 Strength reduction in addressing
133 --------------------------------
134 There is another kind of candidate known as CAND_REF. A CAND_REF
135 describes a statement containing a memory reference having
136 complex addressing that might benefit from strength reduction.
137 Specifically, we are interested in references for which
138 get_inner_reference returns a base address, offset, and bitpos as
139 follows:
141 base: MEM_REF (T1, C1)
142 offset: MULT_EXPR (PLUS_EXPR (T2, C2), C3)
143 bitpos: C4 * BITS_PER_UNIT
145 Here T1 and T2 are arbitrary trees, and C1, C2, C3, C4 are
146 arbitrary integer constants. Note that C2 may be zero, in which
147 case the offset will be MULT_EXPR (T2, C3).
149 When this pattern is recognized, the original memory reference
150 can be replaced with:
152 MEM_REF (POINTER_PLUS_EXPR (T1, MULT_EXPR (T2, C3)),
153 C1 + (C2 * C3) + C4)
155 which distributes the multiply to allow constant folding. When
156 two or more addressing expressions can be represented by MEM_REFs
157 of this form, differing only in the constants C1, C2, and C4,
158 making this substitution produces more efficient addressing during
159 the RTL phases. When there are not at least two expressions with
160 the same values of T1, T2, and C3, there is nothing to be gained
161 by the replacement.
163 Strength reduction of CAND_REFs uses the same infrastructure as
164 that used by CAND_MULTs and CAND_ADDs. We record T1 in the base (B)
165 field, MULT_EXPR (T2, C3) in the stride (S) field, and
166 C1 + (C2 * C3) + C4 in the index (i) field. A basis for a CAND_REF
167 is thus another CAND_REF with the same B and S values. When at
168 least two CAND_REFs are chained together using the basis relation,
169 each of them is replaced as above, resulting in improved code
170 generation for addressing.
172 Conditional candidates
173 ======================
175 Conditional candidates are best illustrated with an example.
176 Consider the code sequence:
178 (1) x_0 = ...;
179 (2) a_0 = x_0 * 5; MULT (B: x_0; i: 0; S: 5)
180 if (...)
181 (3) x_1 = x_0 + 1; ADD (B: x_0, i: 1; S: 1)
182 (4) x_2 = PHI <x_0, x_1>; PHI (B: x_0, i: 0, S: 1)
183 (5) x_3 = x_2 + 1; ADD (B: x_2, i: 1, S: 1)
184 (6) a_1 = x_3 * 5; MULT (B: x_2, i: 1; S: 5)
186 Here strength reduction is complicated by the uncertain value of x_2.
187 A legitimate transformation is:
189 (1) x_0 = ...;
190 (2) a_0 = x_0 * 5;
191 if (...)
193 (3) [x_1 = x_0 + 1;]
194 (3a) t_1 = a_0 + 5;
196 (4) [x_2 = PHI <x_0, x_1>;]
197 (4a) t_2 = PHI <a_0, t_1>;
198 (5) [x_3 = x_2 + 1;]
199 (6r) a_1 = t_2 + 5;
201 where the bracketed instructions may go dead.
203 To recognize this opportunity, we have to observe that statement (6)
204 has a "hidden basis" (2). The hidden basis is unlike a normal basis
205 in that the statement and the hidden basis have different base SSA
206 names (x_2 and x_0, respectively). The relationship is established
207 when a statement's base name (x_2) is defined by a phi statement (4),
208 each argument of which (x_0, x_1) has an identical "derived base name."
209 If the argument is defined by a candidate (as x_1 is by (3)) that is a
210 CAND_ADD having a stride of 1, the derived base name of the argument is
211 the base name of the candidate (x_0). Otherwise, the argument itself
212 is its derived base name (as is the case with argument x_0).
214 The hidden basis for statement (6) is the nearest dominating candidate
215 whose base name is the derived base name (x_0) of the feeding phi (4),
216 and whose stride is identical to that of the statement. We can then
217 create the new "phi basis" (4a) and feeding adds along incoming arcs (3a),
218 allowing the final replacement of (6) by the strength-reduced (6r).
220 To facilitate this, a new kind of candidate (CAND_PHI) is introduced.
221 A CAND_PHI is not a candidate for replacement, but is maintained in the
222 candidate table to ease discovery of hidden bases. Any phi statement
223 whose arguments share a common derived base name is entered into the
224 table with the derived base name, an (arbitrary) index of zero, and a
225 stride of 1. A statement with a hidden basis can then be detected by
226 simply looking up its feeding phi definition in the candidate table,
227 extracting the derived base name, and searching for a basis in the
228 usual manner after substituting the derived base name.
230 Note that the transformation is only valid when the original phi and
231 the statements that define the phi's arguments are all at the same
232 position in the loop hierarchy. */
235 /* Index into the candidate vector, offset by 1. VECs are zero-based,
236 while cand_idx's are one-based, with zero indicating null. */
237 typedef unsigned cand_idx;
239 /* The kind of candidate. */
240 enum cand_kind
242 CAND_MULT,
243 CAND_ADD,
244 CAND_REF,
245 CAND_PHI
248 struct slsr_cand_d
250 /* The candidate statement S1. */
251 gimple cand_stmt;
253 /* The base expression B: often an SSA name, but not always. */
254 tree base_expr;
256 /* The stride S. */
257 tree stride;
259 /* The index constant i. */
260 widest_int index;
262 /* The type of the candidate. This is normally the type of base_expr,
263 but casts may have occurred when combining feeding instructions.
264 A candidate can only be a basis for candidates of the same final type.
265 (For CAND_REFs, this is the type to be used for operand 1 of the
266 replacement MEM_REF.) */
267 tree cand_type;
269 /* The kind of candidate (CAND_MULT, etc.). */
270 enum cand_kind kind;
272 /* Index of this candidate in the candidate vector. */
273 cand_idx cand_num;
275 /* Index of the next candidate record for the same statement.
276 A statement may be useful in more than one way (e.g., due to
277 commutativity). So we can have multiple "interpretations"
278 of a statement. */
279 cand_idx next_interp;
281 /* Index of the basis statement S0, if any, in the candidate vector. */
282 cand_idx basis;
284 /* First candidate for which this candidate is a basis, if one exists. */
285 cand_idx dependent;
287 /* Next candidate having the same basis as this one. */
288 cand_idx sibling;
290 /* If this is a conditional candidate, the CAND_PHI candidate
291 that defines the base SSA name B. */
292 cand_idx def_phi;
294 /* Savings that can be expected from eliminating dead code if this
295 candidate is replaced. */
296 int dead_savings;
299 typedef struct slsr_cand_d slsr_cand, *slsr_cand_t;
300 typedef const struct slsr_cand_d *const_slsr_cand_t;
302 /* Pointers to candidates are chained together as part of a mapping
303 from base expressions to the candidates that use them. */
305 struct cand_chain_d
307 /* Base expression for the chain of candidates: often, but not
308 always, an SSA name. */
309 tree base_expr;
311 /* Pointer to a candidate. */
312 slsr_cand_t cand;
314 /* Chain pointer. */
315 struct cand_chain_d *next;
319 typedef struct cand_chain_d cand_chain, *cand_chain_t;
320 typedef const struct cand_chain_d *const_cand_chain_t;
322 /* Information about a unique "increment" associated with candidates
323 having an SSA name for a stride. An increment is the difference
324 between the index of the candidate and the index of its basis,
325 i.e., (i - i') as discussed in the module commentary.
327 When we are not going to generate address arithmetic we treat
328 increments that differ only in sign as the same, allowing sharing
329 of the cost of initializers. The absolute value of the increment
330 is stored in the incr_info. */
332 struct incr_info_d
334 /* The increment that relates a candidate to its basis. */
335 widest_int incr;
337 /* How many times the increment occurs in the candidate tree. */
338 unsigned count;
340 /* Cost of replacing candidates using this increment. Negative and
341 zero costs indicate replacement should be performed. */
342 int cost;
344 /* If this increment is profitable but is not -1, 0, or 1, it requires
345 an initializer T_0 = stride * incr to be found or introduced in the
346 nearest common dominator of all candidates. This field holds T_0
347 for subsequent use. */
348 tree initializer;
350 /* If the initializer was found to already exist, this is the block
351 where it was found. */
352 basic_block init_bb;
355 typedef struct incr_info_d incr_info, *incr_info_t;
357 /* Candidates are maintained in a vector. If candidate X dominates
358 candidate Y, then X appears before Y in the vector; but the
359 converse does not necessarily hold. */
360 static vec<slsr_cand_t> cand_vec;
362 enum cost_consts
364 COST_NEUTRAL = 0,
365 COST_INFINITE = 1000
368 enum stride_status
370 UNKNOWN_STRIDE = 0,
371 KNOWN_STRIDE = 1
374 enum phi_adjust_status
376 NOT_PHI_ADJUST = 0,
377 PHI_ADJUST = 1
380 enum count_phis_status
382 DONT_COUNT_PHIS = 0,
383 COUNT_PHIS = 1
386 /* Pointer map embodying a mapping from statements to candidates. */
387 static hash_map<gimple, slsr_cand_t> *stmt_cand_map;
389 /* Obstack for candidates. */
390 static struct obstack cand_obstack;
392 /* Obstack for candidate chains. */
393 static struct obstack chain_obstack;
395 /* An array INCR_VEC of incr_infos is used during analysis of related
396 candidates having an SSA name for a stride. INCR_VEC_LEN describes
397 its current length. MAX_INCR_VEC_LEN is used to avoid costly
398 pathological cases. */
399 static incr_info_t incr_vec;
400 static unsigned incr_vec_len;
401 const int MAX_INCR_VEC_LEN = 16;
403 /* For a chain of candidates with unknown stride, indicates whether or not
404 we must generate pointer arithmetic when replacing statements. */
405 static bool address_arithmetic_p;
407 /* Forward function declarations. */
408 static slsr_cand_t base_cand_from_table (tree);
409 static tree introduce_cast_before_cand (slsr_cand_t, tree, tree);
410 static bool legal_cast_p_1 (tree, tree);
412 /* Produce a pointer to the IDX'th candidate in the candidate vector. */
414 static slsr_cand_t
415 lookup_cand (cand_idx idx)
417 return cand_vec[idx - 1];
420 /* Helper for hashing a candidate chain header. */
422 struct cand_chain_hasher : typed_noop_remove <cand_chain>
424 typedef cand_chain value_type;
425 typedef cand_chain compare_type;
426 static inline hashval_t hash (const value_type *);
427 static inline bool equal (const value_type *, const compare_type *);
430 inline hashval_t
431 cand_chain_hasher::hash (const value_type *p)
433 tree base_expr = p->base_expr;
434 return iterative_hash_expr (base_expr, 0);
437 inline bool
438 cand_chain_hasher::equal (const value_type *chain1, const compare_type *chain2)
440 return operand_equal_p (chain1->base_expr, chain2->base_expr, 0);
443 /* Hash table embodying a mapping from base exprs to chains of candidates. */
444 static hash_table<cand_chain_hasher> *base_cand_map;
446 /* Pointer map used by tree_to_aff_combination_expand. */
447 static hash_map<tree, name_expansion *> *name_expansions;
448 /* Pointer map embodying a mapping from bases to alternative bases. */
449 static hash_map<tree, tree> *alt_base_map;
451 /* Given BASE, use the tree affine combiniation facilities to
452 find the underlying tree expression for BASE, with any
453 immediate offset excluded.
455 N.B. we should eliminate this backtracking with better forward
456 analysis in a future release. */
458 static tree
459 get_alternative_base (tree base)
461 tree *result = alt_base_map->get (base);
463 if (result == NULL)
465 tree expr;
466 aff_tree aff;
468 tree_to_aff_combination_expand (base, TREE_TYPE (base),
469 &aff, &name_expansions);
470 aff.offset = 0;
471 expr = aff_combination_to_tree (&aff);
473 gcc_assert (!alt_base_map->put (base, base == expr ? NULL : expr));
475 return expr == base ? NULL : expr;
478 return *result;
481 /* Look in the candidate table for a CAND_PHI that defines BASE and
482 return it if found; otherwise return NULL. */
484 static cand_idx
485 find_phi_def (tree base)
487 slsr_cand_t c;
489 if (TREE_CODE (base) != SSA_NAME)
490 return 0;
492 c = base_cand_from_table (base);
494 if (!c || c->kind != CAND_PHI)
495 return 0;
497 return c->cand_num;
500 /* Helper routine for find_basis_for_candidate. May be called twice:
501 once for the candidate's base expr, and optionally again either for
502 the candidate's phi definition or for a CAND_REF's alternative base
503 expression. */
505 static slsr_cand_t
506 find_basis_for_base_expr (slsr_cand_t c, tree base_expr)
508 cand_chain mapping_key;
509 cand_chain_t chain;
510 slsr_cand_t basis = NULL;
512 // Limit potential of N^2 behavior for long candidate chains.
513 int iters = 0;
514 int max_iters = PARAM_VALUE (PARAM_MAX_SLSR_CANDIDATE_SCAN);
516 mapping_key.base_expr = base_expr;
517 chain = base_cand_map->find (&mapping_key);
519 for (; chain && iters < max_iters; chain = chain->next, ++iters)
521 slsr_cand_t one_basis = chain->cand;
523 if (one_basis->kind != c->kind
524 || one_basis->cand_stmt == c->cand_stmt
525 || !operand_equal_p (one_basis->stride, c->stride, 0)
526 || !types_compatible_p (one_basis->cand_type, c->cand_type)
527 || !dominated_by_p (CDI_DOMINATORS,
528 gimple_bb (c->cand_stmt),
529 gimple_bb (one_basis->cand_stmt)))
530 continue;
532 if (!basis || basis->cand_num < one_basis->cand_num)
533 basis = one_basis;
536 return basis;
539 /* Use the base expr from candidate C to look for possible candidates
540 that can serve as a basis for C. Each potential basis must also
541 appear in a block that dominates the candidate statement and have
542 the same stride and type. If more than one possible basis exists,
543 the one with highest index in the vector is chosen; this will be
544 the most immediately dominating basis. */
546 static int
547 find_basis_for_candidate (slsr_cand_t c)
549 slsr_cand_t basis = find_basis_for_base_expr (c, c->base_expr);
551 /* If a candidate doesn't have a basis using its base expression,
552 it may have a basis hidden by one or more intervening phis. */
553 if (!basis && c->def_phi)
555 basic_block basis_bb, phi_bb;
556 slsr_cand_t phi_cand = lookup_cand (c->def_phi);
557 basis = find_basis_for_base_expr (c, phi_cand->base_expr);
559 if (basis)
561 /* A hidden basis must dominate the phi-definition of the
562 candidate's base name. */
563 phi_bb = gimple_bb (phi_cand->cand_stmt);
564 basis_bb = gimple_bb (basis->cand_stmt);
566 if (phi_bb == basis_bb
567 || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
569 basis = NULL;
570 c->basis = 0;
573 /* If we found a hidden basis, estimate additional dead-code
574 savings if the phi and its feeding statements can be removed. */
575 if (basis && has_single_use (gimple_phi_result (phi_cand->cand_stmt)))
576 c->dead_savings += phi_cand->dead_savings;
580 if (flag_expensive_optimizations && !basis && c->kind == CAND_REF)
582 tree alt_base_expr = get_alternative_base (c->base_expr);
583 if (alt_base_expr)
584 basis = find_basis_for_base_expr (c, alt_base_expr);
587 if (basis)
589 c->sibling = basis->dependent;
590 basis->dependent = c->cand_num;
591 return basis->cand_num;
594 return 0;
597 /* Record a mapping from BASE to C, indicating that C may potentially serve
598 as a basis using that base expression. BASE may be the same as
599 C->BASE_EXPR; alternatively BASE can be a different tree that share the
600 underlining expression of C->BASE_EXPR. */
602 static void
603 record_potential_basis (slsr_cand_t c, tree base)
605 cand_chain_t node;
606 cand_chain **slot;
608 gcc_assert (base);
610 node = (cand_chain_t) obstack_alloc (&chain_obstack, sizeof (cand_chain));
611 node->base_expr = base;
612 node->cand = c;
613 node->next = NULL;
614 slot = base_cand_map->find_slot (node, INSERT);
616 if (*slot)
618 cand_chain_t head = (cand_chain_t) (*slot);
619 node->next = head->next;
620 head->next = node;
622 else
623 *slot = node;
626 /* Allocate storage for a new candidate and initialize its fields.
627 Attempt to find a basis for the candidate.
629 For CAND_REF, an alternative base may also be recorded and used
630 to find a basis. This helps cases where the expression hidden
631 behind BASE (which is usually an SSA_NAME) has immediate offset,
632 e.g.
634 a2[i][j] = 1;
635 a2[i + 20][j] = 2; */
637 static slsr_cand_t
638 alloc_cand_and_find_basis (enum cand_kind kind, gimple gs, tree base,
639 const widest_int &index, tree stride, tree ctype,
640 unsigned savings)
642 slsr_cand_t c = (slsr_cand_t) obstack_alloc (&cand_obstack,
643 sizeof (slsr_cand));
644 c->cand_stmt = gs;
645 c->base_expr = base;
646 c->stride = stride;
647 c->index = index;
648 c->cand_type = ctype;
649 c->kind = kind;
650 c->cand_num = cand_vec.length () + 1;
651 c->next_interp = 0;
652 c->dependent = 0;
653 c->sibling = 0;
654 c->def_phi = kind == CAND_MULT ? find_phi_def (base) : 0;
655 c->dead_savings = savings;
657 cand_vec.safe_push (c);
659 if (kind == CAND_PHI)
660 c->basis = 0;
661 else
662 c->basis = find_basis_for_candidate (c);
664 record_potential_basis (c, base);
665 if (flag_expensive_optimizations && kind == CAND_REF)
667 tree alt_base = get_alternative_base (base);
668 if (alt_base)
669 record_potential_basis (c, alt_base);
672 return c;
675 /* Determine the target cost of statement GS when compiling according
676 to SPEED. */
678 static int
679 stmt_cost (gimple gs, bool speed)
681 tree lhs, rhs1, rhs2;
682 machine_mode lhs_mode;
684 gcc_assert (is_gimple_assign (gs));
685 lhs = gimple_assign_lhs (gs);
686 rhs1 = gimple_assign_rhs1 (gs);
687 lhs_mode = TYPE_MODE (TREE_TYPE (lhs));
689 switch (gimple_assign_rhs_code (gs))
691 case MULT_EXPR:
692 rhs2 = gimple_assign_rhs2 (gs);
694 if (tree_fits_shwi_p (rhs2))
695 return mult_by_coeff_cost (tree_to_shwi (rhs2), lhs_mode, speed);
697 gcc_assert (TREE_CODE (rhs1) != INTEGER_CST);
698 return mul_cost (speed, lhs_mode);
700 case PLUS_EXPR:
701 case POINTER_PLUS_EXPR:
702 case MINUS_EXPR:
703 return add_cost (speed, lhs_mode);
705 case NEGATE_EXPR:
706 return neg_cost (speed, lhs_mode);
708 CASE_CONVERT:
709 return convert_cost (lhs_mode, TYPE_MODE (TREE_TYPE (rhs1)), speed);
711 /* Note that we don't assign costs to copies that in most cases
712 will go away. */
713 default:
717 gcc_unreachable ();
718 return 0;
721 /* Look up the defining statement for BASE_IN and return a pointer
722 to its candidate in the candidate table, if any; otherwise NULL.
723 Only CAND_ADD and CAND_MULT candidates are returned. */
725 static slsr_cand_t
726 base_cand_from_table (tree base_in)
728 slsr_cand_t *result;
730 gimple def = SSA_NAME_DEF_STMT (base_in);
731 if (!def)
732 return (slsr_cand_t) NULL;
734 result = stmt_cand_map->get (def);
736 if (result && (*result)->kind != CAND_REF)
737 return *result;
739 return (slsr_cand_t) NULL;
742 /* Add an entry to the statement-to-candidate mapping. */
744 static void
745 add_cand_for_stmt (gimple gs, slsr_cand_t c)
747 gcc_assert (!stmt_cand_map->put (gs, c));
750 /* Given PHI which contains a phi statement, determine whether it
751 satisfies all the requirements of a phi candidate. If so, create
752 a candidate. Note that a CAND_PHI never has a basis itself, but
753 is used to help find a basis for subsequent candidates. */
755 static void
756 slsr_process_phi (gphi *phi, bool speed)
758 unsigned i;
759 tree arg0_base = NULL_TREE, base_type;
760 slsr_cand_t c;
761 struct loop *cand_loop = gimple_bb (phi)->loop_father;
762 unsigned savings = 0;
764 /* A CAND_PHI requires each of its arguments to have the same
765 derived base name. (See the module header commentary for a
766 definition of derived base names.) Furthermore, all feeding
767 definitions must be in the same position in the loop hierarchy
768 as PHI. */
770 for (i = 0; i < gimple_phi_num_args (phi); i++)
772 slsr_cand_t arg_cand;
773 tree arg = gimple_phi_arg_def (phi, i);
774 tree derived_base_name = NULL_TREE;
775 gimple arg_stmt = NULL;
776 basic_block arg_bb = NULL;
778 if (TREE_CODE (arg) != SSA_NAME)
779 return;
781 arg_cand = base_cand_from_table (arg);
783 if (arg_cand)
785 while (arg_cand->kind != CAND_ADD && arg_cand->kind != CAND_PHI)
787 if (!arg_cand->next_interp)
788 return;
790 arg_cand = lookup_cand (arg_cand->next_interp);
793 if (!integer_onep (arg_cand->stride))
794 return;
796 derived_base_name = arg_cand->base_expr;
797 arg_stmt = arg_cand->cand_stmt;
798 arg_bb = gimple_bb (arg_stmt);
800 /* Gather potential dead code savings if the phi statement
801 can be removed later on. */
802 if (has_single_use (arg))
804 if (gimple_code (arg_stmt) == GIMPLE_PHI)
805 savings += arg_cand->dead_savings;
806 else
807 savings += stmt_cost (arg_stmt, speed);
810 else
812 derived_base_name = arg;
814 if (SSA_NAME_IS_DEFAULT_DEF (arg))
815 arg_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
816 else
817 gimple_bb (SSA_NAME_DEF_STMT (arg));
820 if (!arg_bb || arg_bb->loop_father != cand_loop)
821 return;
823 if (i == 0)
824 arg0_base = derived_base_name;
825 else if (!operand_equal_p (derived_base_name, arg0_base, 0))
826 return;
829 /* Create the candidate. "alloc_cand_and_find_basis" is named
830 misleadingly for this case, as no basis will be sought for a
831 CAND_PHI. */
832 base_type = TREE_TYPE (arg0_base);
834 c = alloc_cand_and_find_basis (CAND_PHI, phi, arg0_base,
835 0, integer_one_node, base_type, savings);
837 /* Add the candidate to the statement-candidate mapping. */
838 add_cand_for_stmt (phi, c);
841 /* Given PBASE which is a pointer to tree, look up the defining
842 statement for it and check whether the candidate is in the
843 form of:
845 X = B + (1 * S), S is integer constant
846 X = B + (i * S), S is integer one
848 If so, set PBASE to the candidate's base_expr and return double
849 int (i * S).
850 Otherwise, just return double int zero. */
852 static widest_int
853 backtrace_base_for_ref (tree *pbase)
855 tree base_in = *pbase;
856 slsr_cand_t base_cand;
858 STRIP_NOPS (base_in);
860 /* Strip off widening conversion(s) to handle cases where
861 e.g. 'B' is widened from an 'int' in order to calculate
862 a 64-bit address. */
863 if (CONVERT_EXPR_P (base_in)
864 && legal_cast_p_1 (base_in, TREE_OPERAND (base_in, 0)))
865 base_in = get_unwidened (base_in, NULL_TREE);
867 if (TREE_CODE (base_in) != SSA_NAME)
868 return 0;
870 base_cand = base_cand_from_table (base_in);
872 while (base_cand && base_cand->kind != CAND_PHI)
874 if (base_cand->kind == CAND_ADD
875 && base_cand->index == 1
876 && TREE_CODE (base_cand->stride) == INTEGER_CST)
878 /* X = B + (1 * S), S is integer constant. */
879 *pbase = base_cand->base_expr;
880 return wi::to_widest (base_cand->stride);
882 else if (base_cand->kind == CAND_ADD
883 && TREE_CODE (base_cand->stride) == INTEGER_CST
884 && integer_onep (base_cand->stride))
886 /* X = B + (i * S), S is integer one. */
887 *pbase = base_cand->base_expr;
888 return base_cand->index;
891 if (base_cand->next_interp)
892 base_cand = lookup_cand (base_cand->next_interp);
893 else
894 base_cand = NULL;
897 return 0;
900 /* Look for the following pattern:
902 *PBASE: MEM_REF (T1, C1)
904 *POFFSET: MULT_EXPR (T2, C3) [C2 is zero]
906 MULT_EXPR (PLUS_EXPR (T2, C2), C3)
908 MULT_EXPR (MINUS_EXPR (T2, -C2), C3)
910 *PINDEX: C4 * BITS_PER_UNIT
912 If not present, leave the input values unchanged and return FALSE.
913 Otherwise, modify the input values as follows and return TRUE:
915 *PBASE: T1
916 *POFFSET: MULT_EXPR (T2, C3)
917 *PINDEX: C1 + (C2 * C3) + C4
919 When T2 is recorded by a CAND_ADD in the form of (T2' + C5), it
920 will be further restructured to:
922 *PBASE: T1
923 *POFFSET: MULT_EXPR (T2', C3)
924 *PINDEX: C1 + (C2 * C3) + C4 + (C5 * C3) */
926 static bool
927 restructure_reference (tree *pbase, tree *poffset, widest_int *pindex,
928 tree *ptype)
930 tree base = *pbase, offset = *poffset;
931 widest_int index = *pindex;
932 tree mult_op0, t1, t2, type;
933 widest_int c1, c2, c3, c4, c5;
935 if (!base
936 || !offset
937 || TREE_CODE (base) != MEM_REF
938 || TREE_CODE (offset) != MULT_EXPR
939 || TREE_CODE (TREE_OPERAND (offset, 1)) != INTEGER_CST
940 || wi::umod_floor (index, BITS_PER_UNIT) != 0)
941 return false;
943 t1 = TREE_OPERAND (base, 0);
944 c1 = widest_int::from (mem_ref_offset (base), SIGNED);
945 type = TREE_TYPE (TREE_OPERAND (base, 1));
947 mult_op0 = TREE_OPERAND (offset, 0);
948 c3 = wi::to_widest (TREE_OPERAND (offset, 1));
950 if (TREE_CODE (mult_op0) == PLUS_EXPR)
952 if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
954 t2 = TREE_OPERAND (mult_op0, 0);
955 c2 = wi::to_widest (TREE_OPERAND (mult_op0, 1));
957 else
958 return false;
960 else if (TREE_CODE (mult_op0) == MINUS_EXPR)
962 if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
964 t2 = TREE_OPERAND (mult_op0, 0);
965 c2 = -wi::to_widest (TREE_OPERAND (mult_op0, 1));
967 else
968 return false;
970 else
972 t2 = mult_op0;
973 c2 = 0;
976 c4 = wi::lrshift (index, LOG2_BITS_PER_UNIT);
977 c5 = backtrace_base_for_ref (&t2);
979 *pbase = t1;
980 *poffset = fold_build2 (MULT_EXPR, sizetype, fold_convert (sizetype, t2),
981 wide_int_to_tree (sizetype, c3));
982 *pindex = c1 + c2 * c3 + c4 + c5 * c3;
983 *ptype = type;
985 return true;
988 /* Given GS which contains a data reference, create a CAND_REF entry in
989 the candidate table and attempt to find a basis. */
991 static void
992 slsr_process_ref (gimple gs)
994 tree ref_expr, base, offset, type;
995 HOST_WIDE_INT bitsize, bitpos;
996 machine_mode mode;
997 int unsignedp, volatilep;
998 slsr_cand_t c;
1000 if (gimple_vdef (gs))
1001 ref_expr = gimple_assign_lhs (gs);
1002 else
1003 ref_expr = gimple_assign_rhs1 (gs);
1005 if (!handled_component_p (ref_expr)
1006 || TREE_CODE (ref_expr) == BIT_FIELD_REF
1007 || (TREE_CODE (ref_expr) == COMPONENT_REF
1008 && DECL_BIT_FIELD (TREE_OPERAND (ref_expr, 1))))
1009 return;
1011 base = get_inner_reference (ref_expr, &bitsize, &bitpos, &offset, &mode,
1012 &unsignedp, &volatilep, false);
1013 widest_int index = bitpos;
1015 if (!restructure_reference (&base, &offset, &index, &type))
1016 return;
1018 c = alloc_cand_and_find_basis (CAND_REF, gs, base, index, offset,
1019 type, 0);
1021 /* Add the candidate to the statement-candidate mapping. */
1022 add_cand_for_stmt (gs, c);
1025 /* Create a candidate entry for a statement GS, where GS multiplies
1026 two SSA names BASE_IN and STRIDE_IN. Propagate any known information
1027 about the two SSA names into the new candidate. Return the new
1028 candidate. */
1030 static slsr_cand_t
1031 create_mul_ssa_cand (gimple gs, tree base_in, tree stride_in, bool speed)
1033 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1034 widest_int index;
1035 unsigned savings = 0;
1036 slsr_cand_t c;
1037 slsr_cand_t base_cand = base_cand_from_table (base_in);
1039 /* Look at all interpretations of the base candidate, if necessary,
1040 to find information to propagate into this candidate. */
1041 while (base_cand && !base && base_cand->kind != CAND_PHI)
1044 if (base_cand->kind == CAND_MULT && integer_onep (base_cand->stride))
1046 /* Y = (B + i') * 1
1047 X = Y * Z
1048 ================
1049 X = (B + i') * Z */
1050 base = base_cand->base_expr;
1051 index = base_cand->index;
1052 stride = stride_in;
1053 ctype = base_cand->cand_type;
1054 if (has_single_use (base_in))
1055 savings = (base_cand->dead_savings
1056 + stmt_cost (base_cand->cand_stmt, speed));
1058 else if (base_cand->kind == CAND_ADD
1059 && TREE_CODE (base_cand->stride) == INTEGER_CST)
1061 /* Y = B + (i' * S), S constant
1062 X = Y * Z
1063 ============================
1064 X = B + ((i' * S) * Z) */
1065 base = base_cand->base_expr;
1066 index = base_cand->index * wi::to_widest (base_cand->stride);
1067 stride = stride_in;
1068 ctype = base_cand->cand_type;
1069 if (has_single_use (base_in))
1070 savings = (base_cand->dead_savings
1071 + stmt_cost (base_cand->cand_stmt, speed));
1074 if (base_cand->next_interp)
1075 base_cand = lookup_cand (base_cand->next_interp);
1076 else
1077 base_cand = NULL;
1080 if (!base)
1082 /* No interpretations had anything useful to propagate, so
1083 produce X = (Y + 0) * Z. */
1084 base = base_in;
1085 index = 0;
1086 stride = stride_in;
1087 ctype = TREE_TYPE (base_in);
1090 c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
1091 ctype, savings);
1092 return c;
1095 /* Create a candidate entry for a statement GS, where GS multiplies
1096 SSA name BASE_IN by constant STRIDE_IN. Propagate any known
1097 information about BASE_IN into the new candidate. Return the new
1098 candidate. */
1100 static slsr_cand_t
1101 create_mul_imm_cand (gimple gs, tree base_in, tree stride_in, bool speed)
1103 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1104 widest_int index, temp;
1105 unsigned savings = 0;
1106 slsr_cand_t c;
1107 slsr_cand_t base_cand = base_cand_from_table (base_in);
1109 /* Look at all interpretations of the base candidate, if necessary,
1110 to find information to propagate into this candidate. */
1111 while (base_cand && !base && base_cand->kind != CAND_PHI)
1113 if (base_cand->kind == CAND_MULT
1114 && TREE_CODE (base_cand->stride) == INTEGER_CST)
1116 /* Y = (B + i') * S, S constant
1117 X = Y * c
1118 ============================
1119 X = (B + i') * (S * c) */
1120 temp = wi::to_widest (base_cand->stride) * wi::to_widest (stride_in);
1121 if (wi::fits_to_tree_p (temp, TREE_TYPE (stride_in)))
1123 base = base_cand->base_expr;
1124 index = base_cand->index;
1125 stride = wide_int_to_tree (TREE_TYPE (stride_in), temp);
1126 ctype = base_cand->cand_type;
1127 if (has_single_use (base_in))
1128 savings = (base_cand->dead_savings
1129 + stmt_cost (base_cand->cand_stmt, speed));
1132 else if (base_cand->kind == CAND_ADD && integer_onep (base_cand->stride))
1134 /* Y = B + (i' * 1)
1135 X = Y * c
1136 ===========================
1137 X = (B + i') * c */
1138 base = base_cand->base_expr;
1139 index = base_cand->index;
1140 stride = stride_in;
1141 ctype = base_cand->cand_type;
1142 if (has_single_use (base_in))
1143 savings = (base_cand->dead_savings
1144 + stmt_cost (base_cand->cand_stmt, speed));
1146 else if (base_cand->kind == CAND_ADD
1147 && base_cand->index == 1
1148 && TREE_CODE (base_cand->stride) == INTEGER_CST)
1150 /* Y = B + (1 * S), S constant
1151 X = Y * c
1152 ===========================
1153 X = (B + S) * c */
1154 base = base_cand->base_expr;
1155 index = wi::to_widest (base_cand->stride);
1156 stride = stride_in;
1157 ctype = base_cand->cand_type;
1158 if (has_single_use (base_in))
1159 savings = (base_cand->dead_savings
1160 + stmt_cost (base_cand->cand_stmt, speed));
1163 if (base_cand->next_interp)
1164 base_cand = lookup_cand (base_cand->next_interp);
1165 else
1166 base_cand = NULL;
1169 if (!base)
1171 /* No interpretations had anything useful to propagate, so
1172 produce X = (Y + 0) * c. */
1173 base = base_in;
1174 index = 0;
1175 stride = stride_in;
1176 ctype = TREE_TYPE (base_in);
1179 c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
1180 ctype, savings);
1181 return c;
1184 /* Given GS which is a multiply of scalar integers, make an appropriate
1185 entry in the candidate table. If this is a multiply of two SSA names,
1186 create two CAND_MULT interpretations and attempt to find a basis for
1187 each of them. Otherwise, create a single CAND_MULT and attempt to
1188 find a basis. */
1190 static void
1191 slsr_process_mul (gimple gs, tree rhs1, tree rhs2, bool speed)
1193 slsr_cand_t c, c2;
1195 /* If this is a multiply of an SSA name with itself, it is highly
1196 unlikely that we will get a strength reduction opportunity, so
1197 don't record it as a candidate. This simplifies the logic for
1198 finding a basis, so if this is removed that must be considered. */
1199 if (rhs1 == rhs2)
1200 return;
1202 if (TREE_CODE (rhs2) == SSA_NAME)
1204 /* Record an interpretation of this statement in the candidate table
1205 assuming RHS1 is the base expression and RHS2 is the stride. */
1206 c = create_mul_ssa_cand (gs, rhs1, rhs2, speed);
1208 /* Add the first interpretation to the statement-candidate mapping. */
1209 add_cand_for_stmt (gs, c);
1211 /* Record another interpretation of this statement assuming RHS1
1212 is the stride and RHS2 is the base expression. */
1213 c2 = create_mul_ssa_cand (gs, rhs2, rhs1, speed);
1214 c->next_interp = c2->cand_num;
1216 else
1218 /* Record an interpretation for the multiply-immediate. */
1219 c = create_mul_imm_cand (gs, rhs1, rhs2, speed);
1221 /* Add the interpretation to the statement-candidate mapping. */
1222 add_cand_for_stmt (gs, c);
1226 /* Create a candidate entry for a statement GS, where GS adds two
1227 SSA names BASE_IN and ADDEND_IN if SUBTRACT_P is false, and
1228 subtracts ADDEND_IN from BASE_IN otherwise. Propagate any known
1229 information about the two SSA names into the new candidate.
1230 Return the new candidate. */
1232 static slsr_cand_t
1233 create_add_ssa_cand (gimple gs, tree base_in, tree addend_in,
1234 bool subtract_p, bool speed)
1236 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL;
1237 widest_int index;
1238 unsigned savings = 0;
1239 slsr_cand_t c;
1240 slsr_cand_t base_cand = base_cand_from_table (base_in);
1241 slsr_cand_t addend_cand = base_cand_from_table (addend_in);
1243 /* The most useful transformation is a multiply-immediate feeding
1244 an add or subtract. Look for that first. */
1245 while (addend_cand && !base && addend_cand->kind != CAND_PHI)
1247 if (addend_cand->kind == CAND_MULT
1248 && addend_cand->index == 0
1249 && TREE_CODE (addend_cand->stride) == INTEGER_CST)
1251 /* Z = (B + 0) * S, S constant
1252 X = Y +/- Z
1253 ===========================
1254 X = Y + ((+/-1 * S) * B) */
1255 base = base_in;
1256 index = wi::to_widest (addend_cand->stride);
1257 if (subtract_p)
1258 index = -index;
1259 stride = addend_cand->base_expr;
1260 ctype = TREE_TYPE (base_in);
1261 if (has_single_use (addend_in))
1262 savings = (addend_cand->dead_savings
1263 + stmt_cost (addend_cand->cand_stmt, speed));
1266 if (addend_cand->next_interp)
1267 addend_cand = lookup_cand (addend_cand->next_interp);
1268 else
1269 addend_cand = NULL;
1272 while (base_cand && !base && base_cand->kind != CAND_PHI)
1274 if (base_cand->kind == CAND_ADD
1275 && (base_cand->index == 0
1276 || operand_equal_p (base_cand->stride,
1277 integer_zero_node, 0)))
1279 /* Y = B + (i' * S), i' * S = 0
1280 X = Y +/- Z
1281 ============================
1282 X = B + (+/-1 * Z) */
1283 base = base_cand->base_expr;
1284 index = subtract_p ? -1 : 1;
1285 stride = addend_in;
1286 ctype = base_cand->cand_type;
1287 if (has_single_use (base_in))
1288 savings = (base_cand->dead_savings
1289 + stmt_cost (base_cand->cand_stmt, speed));
1291 else if (subtract_p)
1293 slsr_cand_t subtrahend_cand = base_cand_from_table (addend_in);
1295 while (subtrahend_cand && !base && subtrahend_cand->kind != CAND_PHI)
1297 if (subtrahend_cand->kind == CAND_MULT
1298 && subtrahend_cand->index == 0
1299 && TREE_CODE (subtrahend_cand->stride) == INTEGER_CST)
1301 /* Z = (B + 0) * S, S constant
1302 X = Y - Z
1303 ===========================
1304 Value: X = Y + ((-1 * S) * B) */
1305 base = base_in;
1306 index = wi::to_widest (subtrahend_cand->stride);
1307 index = -index;
1308 stride = subtrahend_cand->base_expr;
1309 ctype = TREE_TYPE (base_in);
1310 if (has_single_use (addend_in))
1311 savings = (subtrahend_cand->dead_savings
1312 + stmt_cost (subtrahend_cand->cand_stmt, speed));
1315 if (subtrahend_cand->next_interp)
1316 subtrahend_cand = lookup_cand (subtrahend_cand->next_interp);
1317 else
1318 subtrahend_cand = NULL;
1322 if (base_cand->next_interp)
1323 base_cand = lookup_cand (base_cand->next_interp);
1324 else
1325 base_cand = NULL;
1328 if (!base)
1330 /* No interpretations had anything useful to propagate, so
1331 produce X = Y + (1 * Z). */
1332 base = base_in;
1333 index = subtract_p ? -1 : 1;
1334 stride = addend_in;
1335 ctype = TREE_TYPE (base_in);
1338 c = alloc_cand_and_find_basis (CAND_ADD, gs, base, index, stride,
1339 ctype, savings);
1340 return c;
1343 /* Create a candidate entry for a statement GS, where GS adds SSA
1344 name BASE_IN to constant INDEX_IN. Propagate any known information
1345 about BASE_IN into the new candidate. Return the new candidate. */
1347 static slsr_cand_t
1348 create_add_imm_cand (gimple gs, tree base_in, const widest_int &index_in,
1349 bool speed)
1351 enum cand_kind kind = CAND_ADD;
1352 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1353 widest_int index, multiple;
1354 unsigned savings = 0;
1355 slsr_cand_t c;
1356 slsr_cand_t base_cand = base_cand_from_table (base_in);
1358 while (base_cand && !base && base_cand->kind != CAND_PHI)
1360 signop sign = TYPE_SIGN (TREE_TYPE (base_cand->stride));
1362 if (TREE_CODE (base_cand->stride) == INTEGER_CST
1363 && wi::multiple_of_p (index_in, wi::to_widest (base_cand->stride),
1364 sign, &multiple))
1366 /* Y = (B + i') * S, S constant, c = kS for some integer k
1367 X = Y + c
1368 ============================
1369 X = (B + (i'+ k)) * S
1371 Y = B + (i' * S), S constant, c = kS for some integer k
1372 X = Y + c
1373 ============================
1374 X = (B + (i'+ k)) * S */
1375 kind = base_cand->kind;
1376 base = base_cand->base_expr;
1377 index = base_cand->index + multiple;
1378 stride = base_cand->stride;
1379 ctype = base_cand->cand_type;
1380 if (has_single_use (base_in))
1381 savings = (base_cand->dead_savings
1382 + stmt_cost (base_cand->cand_stmt, speed));
1385 if (base_cand->next_interp)
1386 base_cand = lookup_cand (base_cand->next_interp);
1387 else
1388 base_cand = NULL;
1391 if (!base)
1393 /* No interpretations had anything useful to propagate, so
1394 produce X = Y + (c * 1). */
1395 kind = CAND_ADD;
1396 base = base_in;
1397 index = index_in;
1398 stride = integer_one_node;
1399 ctype = TREE_TYPE (base_in);
1402 c = alloc_cand_and_find_basis (kind, gs, base, index, stride,
1403 ctype, savings);
1404 return c;
1407 /* Given GS which is an add or subtract of scalar integers or pointers,
1408 make at least one appropriate entry in the candidate table. */
1410 static void
1411 slsr_process_add (gimple gs, tree rhs1, tree rhs2, bool speed)
1413 bool subtract_p = gimple_assign_rhs_code (gs) == MINUS_EXPR;
1414 slsr_cand_t c = NULL, c2;
1416 if (TREE_CODE (rhs2) == SSA_NAME)
1418 /* First record an interpretation assuming RHS1 is the base expression
1419 and RHS2 is the stride. But it doesn't make sense for the
1420 stride to be a pointer, so don't record a candidate in that case. */
1421 if (!POINTER_TYPE_P (TREE_TYPE (rhs2)))
1423 c = create_add_ssa_cand (gs, rhs1, rhs2, subtract_p, speed);
1425 /* Add the first interpretation to the statement-candidate
1426 mapping. */
1427 add_cand_for_stmt (gs, c);
1430 /* If the two RHS operands are identical, or this is a subtract,
1431 we're done. */
1432 if (operand_equal_p (rhs1, rhs2, 0) || subtract_p)
1433 return;
1435 /* Otherwise, record another interpretation assuming RHS2 is the
1436 base expression and RHS1 is the stride, again provided that the
1437 stride is not a pointer. */
1438 if (!POINTER_TYPE_P (TREE_TYPE (rhs1)))
1440 c2 = create_add_ssa_cand (gs, rhs2, rhs1, false, speed);
1441 if (c)
1442 c->next_interp = c2->cand_num;
1443 else
1444 add_cand_for_stmt (gs, c2);
1447 else
1449 /* Record an interpretation for the add-immediate. */
1450 widest_int index = wi::to_widest (rhs2);
1451 if (subtract_p)
1452 index = -index;
1454 c = create_add_imm_cand (gs, rhs1, index, speed);
1456 /* Add the interpretation to the statement-candidate mapping. */
1457 add_cand_for_stmt (gs, c);
1461 /* Given GS which is a negate of a scalar integer, make an appropriate
1462 entry in the candidate table. A negate is equivalent to a multiply
1463 by -1. */
1465 static void
1466 slsr_process_neg (gimple gs, tree rhs1, bool speed)
1468 /* Record a CAND_MULT interpretation for the multiply by -1. */
1469 slsr_cand_t c = create_mul_imm_cand (gs, rhs1, integer_minus_one_node, speed);
1471 /* Add the interpretation to the statement-candidate mapping. */
1472 add_cand_for_stmt (gs, c);
1475 /* Help function for legal_cast_p, operating on two trees. Checks
1476 whether it's allowable to cast from RHS to LHS. See legal_cast_p
1477 for more details. */
1479 static bool
1480 legal_cast_p_1 (tree lhs, tree rhs)
1482 tree lhs_type, rhs_type;
1483 unsigned lhs_size, rhs_size;
1484 bool lhs_wraps, rhs_wraps;
1486 lhs_type = TREE_TYPE (lhs);
1487 rhs_type = TREE_TYPE (rhs);
1488 lhs_size = TYPE_PRECISION (lhs_type);
1489 rhs_size = TYPE_PRECISION (rhs_type);
1490 lhs_wraps = ANY_INTEGRAL_TYPE_P (lhs_type) && TYPE_OVERFLOW_WRAPS (lhs_type);
1491 rhs_wraps = ANY_INTEGRAL_TYPE_P (rhs_type) && TYPE_OVERFLOW_WRAPS (rhs_type);
1493 if (lhs_size < rhs_size
1494 || (rhs_wraps && !lhs_wraps)
1495 || (rhs_wraps && lhs_wraps && rhs_size != lhs_size))
1496 return false;
1498 return true;
1501 /* Return TRUE if GS is a statement that defines an SSA name from
1502 a conversion and is legal for us to combine with an add and multiply
1503 in the candidate table. For example, suppose we have:
1505 A = B + i;
1506 C = (type) A;
1507 D = C * S;
1509 Without the type-cast, we would create a CAND_MULT for D with base B,
1510 index i, and stride S. We want to record this candidate only if it
1511 is equivalent to apply the type cast following the multiply:
1513 A = B + i;
1514 E = A * S;
1515 D = (type) E;
1517 We will record the type with the candidate for D. This allows us
1518 to use a similar previous candidate as a basis. If we have earlier seen
1520 A' = B + i';
1521 C' = (type) A';
1522 D' = C' * S;
1524 we can replace D with
1526 D = D' + (i - i') * S;
1528 But if moving the type-cast would change semantics, we mustn't do this.
1530 This is legitimate for casts from a non-wrapping integral type to
1531 any integral type of the same or larger size. It is not legitimate
1532 to convert a wrapping type to a non-wrapping type, or to a wrapping
1533 type of a different size. I.e., with a wrapping type, we must
1534 assume that the addition B + i could wrap, in which case performing
1535 the multiply before or after one of the "illegal" type casts will
1536 have different semantics. */
1538 static bool
1539 legal_cast_p (gimple gs, tree rhs)
1541 if (!is_gimple_assign (gs)
1542 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs)))
1543 return false;
1545 return legal_cast_p_1 (gimple_assign_lhs (gs), rhs);
1548 /* Given GS which is a cast to a scalar integer type, determine whether
1549 the cast is legal for strength reduction. If so, make at least one
1550 appropriate entry in the candidate table. */
1552 static void
1553 slsr_process_cast (gimple gs, tree rhs1, bool speed)
1555 tree lhs, ctype;
1556 slsr_cand_t base_cand, c, c2;
1557 unsigned savings = 0;
1559 if (!legal_cast_p (gs, rhs1))
1560 return;
1562 lhs = gimple_assign_lhs (gs);
1563 base_cand = base_cand_from_table (rhs1);
1564 ctype = TREE_TYPE (lhs);
1566 if (base_cand && base_cand->kind != CAND_PHI)
1568 while (base_cand)
1570 /* Propagate all data from the base candidate except the type,
1571 which comes from the cast, and the base candidate's cast,
1572 which is no longer applicable. */
1573 if (has_single_use (rhs1))
1574 savings = (base_cand->dead_savings
1575 + stmt_cost (base_cand->cand_stmt, speed));
1577 c = alloc_cand_and_find_basis (base_cand->kind, gs,
1578 base_cand->base_expr,
1579 base_cand->index, base_cand->stride,
1580 ctype, savings);
1581 if (base_cand->next_interp)
1582 base_cand = lookup_cand (base_cand->next_interp);
1583 else
1584 base_cand = NULL;
1587 else
1589 /* If nothing is known about the RHS, create fresh CAND_ADD and
1590 CAND_MULT interpretations:
1592 X = Y + (0 * 1)
1593 X = (Y + 0) * 1
1595 The first of these is somewhat arbitrary, but the choice of
1596 1 for the stride simplifies the logic for propagating casts
1597 into their uses. */
1598 c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1,
1599 0, integer_one_node, ctype, 0);
1600 c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1,
1601 0, integer_one_node, ctype, 0);
1602 c->next_interp = c2->cand_num;
1605 /* Add the first (or only) interpretation to the statement-candidate
1606 mapping. */
1607 add_cand_for_stmt (gs, c);
1610 /* Given GS which is a copy of a scalar integer type, make at least one
1611 appropriate entry in the candidate table.
1613 This interface is included for completeness, but is unnecessary
1614 if this pass immediately follows a pass that performs copy
1615 propagation, such as DOM. */
1617 static void
1618 slsr_process_copy (gimple gs, tree rhs1, bool speed)
1620 slsr_cand_t base_cand, c, c2;
1621 unsigned savings = 0;
1623 base_cand = base_cand_from_table (rhs1);
1625 if (base_cand && base_cand->kind != CAND_PHI)
1627 while (base_cand)
1629 /* Propagate all data from the base candidate. */
1630 if (has_single_use (rhs1))
1631 savings = (base_cand->dead_savings
1632 + stmt_cost (base_cand->cand_stmt, speed));
1634 c = alloc_cand_and_find_basis (base_cand->kind, gs,
1635 base_cand->base_expr,
1636 base_cand->index, base_cand->stride,
1637 base_cand->cand_type, savings);
1638 if (base_cand->next_interp)
1639 base_cand = lookup_cand (base_cand->next_interp);
1640 else
1641 base_cand = NULL;
1644 else
1646 /* If nothing is known about the RHS, create fresh CAND_ADD and
1647 CAND_MULT interpretations:
1649 X = Y + (0 * 1)
1650 X = (Y + 0) * 1
1652 The first of these is somewhat arbitrary, but the choice of
1653 1 for the stride simplifies the logic for propagating casts
1654 into their uses. */
1655 c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1,
1656 0, integer_one_node, TREE_TYPE (rhs1), 0);
1657 c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1,
1658 0, integer_one_node, TREE_TYPE (rhs1), 0);
1659 c->next_interp = c2->cand_num;
1662 /* Add the first (or only) interpretation to the statement-candidate
1663 mapping. */
1664 add_cand_for_stmt (gs, c);
1667 class find_candidates_dom_walker : public dom_walker
1669 public:
1670 find_candidates_dom_walker (cdi_direction direction)
1671 : dom_walker (direction) {}
1672 virtual void before_dom_children (basic_block);
1675 /* Find strength-reduction candidates in block BB. */
1677 void
1678 find_candidates_dom_walker::before_dom_children (basic_block bb)
1680 bool speed = optimize_bb_for_speed_p (bb);
1682 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1683 gsi_next (&gsi))
1684 slsr_process_phi (gsi.phi (), speed);
1686 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1687 gsi_next (&gsi))
1689 gimple gs = gsi_stmt (gsi);
1691 if (gimple_vuse (gs) && gimple_assign_single_p (gs))
1692 slsr_process_ref (gs);
1694 else if (is_gimple_assign (gs)
1695 && SCALAR_INT_MODE_P
1696 (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))))
1698 tree rhs1 = NULL_TREE, rhs2 = NULL_TREE;
1700 switch (gimple_assign_rhs_code (gs))
1702 case MULT_EXPR:
1703 case PLUS_EXPR:
1704 rhs1 = gimple_assign_rhs1 (gs);
1705 rhs2 = gimple_assign_rhs2 (gs);
1706 /* Should never happen, but currently some buggy situations
1707 in earlier phases put constants in rhs1. */
1708 if (TREE_CODE (rhs1) != SSA_NAME)
1709 continue;
1710 break;
1712 /* Possible future opportunity: rhs1 of a ptr+ can be
1713 an ADDR_EXPR. */
1714 case POINTER_PLUS_EXPR:
1715 case MINUS_EXPR:
1716 rhs2 = gimple_assign_rhs2 (gs);
1717 /* Fall-through. */
1719 CASE_CONVERT:
1720 case MODIFY_EXPR:
1721 case NEGATE_EXPR:
1722 rhs1 = gimple_assign_rhs1 (gs);
1723 if (TREE_CODE (rhs1) != SSA_NAME)
1724 continue;
1725 break;
1727 default:
1731 switch (gimple_assign_rhs_code (gs))
1733 case MULT_EXPR:
1734 slsr_process_mul (gs, rhs1, rhs2, speed);
1735 break;
1737 case PLUS_EXPR:
1738 case POINTER_PLUS_EXPR:
1739 case MINUS_EXPR:
1740 slsr_process_add (gs, rhs1, rhs2, speed);
1741 break;
1743 case NEGATE_EXPR:
1744 slsr_process_neg (gs, rhs1, speed);
1745 break;
1747 CASE_CONVERT:
1748 slsr_process_cast (gs, rhs1, speed);
1749 break;
1751 case MODIFY_EXPR:
1752 slsr_process_copy (gs, rhs1, speed);
1753 break;
1755 default:
1762 /* Dump a candidate for debug. */
1764 static void
1765 dump_candidate (slsr_cand_t c)
1767 fprintf (dump_file, "%3d [%d] ", c->cand_num,
1768 gimple_bb (c->cand_stmt)->index);
1769 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
1770 switch (c->kind)
1772 case CAND_MULT:
1773 fputs (" MULT : (", dump_file);
1774 print_generic_expr (dump_file, c->base_expr, 0);
1775 fputs (" + ", dump_file);
1776 print_decs (c->index, dump_file);
1777 fputs (") * ", dump_file);
1778 print_generic_expr (dump_file, c->stride, 0);
1779 fputs (" : ", dump_file);
1780 break;
1781 case CAND_ADD:
1782 fputs (" ADD : ", dump_file);
1783 print_generic_expr (dump_file, c->base_expr, 0);
1784 fputs (" + (", dump_file);
1785 print_decs (c->index, dump_file);
1786 fputs (" * ", dump_file);
1787 print_generic_expr (dump_file, c->stride, 0);
1788 fputs (") : ", dump_file);
1789 break;
1790 case CAND_REF:
1791 fputs (" REF : ", dump_file);
1792 print_generic_expr (dump_file, c->base_expr, 0);
1793 fputs (" + (", dump_file);
1794 print_generic_expr (dump_file, c->stride, 0);
1795 fputs (") + ", dump_file);
1796 print_decs (c->index, dump_file);
1797 fputs (" : ", dump_file);
1798 break;
1799 case CAND_PHI:
1800 fputs (" PHI : ", dump_file);
1801 print_generic_expr (dump_file, c->base_expr, 0);
1802 fputs (" + (unknown * ", dump_file);
1803 print_generic_expr (dump_file, c->stride, 0);
1804 fputs (") : ", dump_file);
1805 break;
1806 default:
1807 gcc_unreachable ();
1809 print_generic_expr (dump_file, c->cand_type, 0);
1810 fprintf (dump_file, "\n basis: %d dependent: %d sibling: %d\n",
1811 c->basis, c->dependent, c->sibling);
1812 fprintf (dump_file, " next-interp: %d dead-savings: %d\n",
1813 c->next_interp, c->dead_savings);
1814 if (c->def_phi)
1815 fprintf (dump_file, " phi: %d\n", c->def_phi);
1816 fputs ("\n", dump_file);
1819 /* Dump the candidate vector for debug. */
1821 static void
1822 dump_cand_vec (void)
1824 unsigned i;
1825 slsr_cand_t c;
1827 fprintf (dump_file, "\nStrength reduction candidate vector:\n\n");
1829 FOR_EACH_VEC_ELT (cand_vec, i, c)
1830 dump_candidate (c);
1833 /* Callback used to dump the candidate chains hash table. */
1836 ssa_base_cand_dump_callback (cand_chain **slot, void *ignored ATTRIBUTE_UNUSED)
1838 const_cand_chain_t chain = *slot;
1839 cand_chain_t p;
1841 print_generic_expr (dump_file, chain->base_expr, 0);
1842 fprintf (dump_file, " -> %d", chain->cand->cand_num);
1844 for (p = chain->next; p; p = p->next)
1845 fprintf (dump_file, " -> %d", p->cand->cand_num);
1847 fputs ("\n", dump_file);
1848 return 1;
1851 /* Dump the candidate chains. */
1853 static void
1854 dump_cand_chains (void)
1856 fprintf (dump_file, "\nStrength reduction candidate chains:\n\n");
1857 base_cand_map->traverse_noresize <void *, ssa_base_cand_dump_callback>
1858 (NULL);
1859 fputs ("\n", dump_file);
1862 /* Dump the increment vector for debug. */
1864 static void
1865 dump_incr_vec (void)
1867 if (dump_file && (dump_flags & TDF_DETAILS))
1869 unsigned i;
1871 fprintf (dump_file, "\nIncrement vector:\n\n");
1873 for (i = 0; i < incr_vec_len; i++)
1875 fprintf (dump_file, "%3d increment: ", i);
1876 print_decs (incr_vec[i].incr, dump_file);
1877 fprintf (dump_file, "\n count: %d", incr_vec[i].count);
1878 fprintf (dump_file, "\n cost: %d", incr_vec[i].cost);
1879 fputs ("\n initializer: ", dump_file);
1880 print_generic_expr (dump_file, incr_vec[i].initializer, 0);
1881 fputs ("\n\n", dump_file);
1886 /* Replace *EXPR in candidate C with an equivalent strength-reduced
1887 data reference. */
1889 static void
1890 replace_ref (tree *expr, slsr_cand_t c)
1892 tree add_expr, mem_ref, acc_type = TREE_TYPE (*expr);
1893 unsigned HOST_WIDE_INT misalign;
1894 unsigned align;
1896 /* Ensure the memory reference carries the minimum alignment
1897 requirement for the data type. See PR58041. */
1898 get_object_alignment_1 (*expr, &align, &misalign);
1899 if (misalign != 0)
1900 align = (misalign & -misalign);
1901 if (align < TYPE_ALIGN (acc_type))
1902 acc_type = build_aligned_type (acc_type, align);
1904 add_expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (c->base_expr),
1905 c->base_expr, c->stride);
1906 mem_ref = fold_build2 (MEM_REF, acc_type, add_expr,
1907 wide_int_to_tree (c->cand_type, c->index));
1909 /* Gimplify the base addressing expression for the new MEM_REF tree. */
1910 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
1911 TREE_OPERAND (mem_ref, 0)
1912 = force_gimple_operand_gsi (&gsi, TREE_OPERAND (mem_ref, 0),
1913 /*simple_p=*/true, NULL,
1914 /*before=*/true, GSI_SAME_STMT);
1915 copy_ref_info (mem_ref, *expr);
1916 *expr = mem_ref;
1917 update_stmt (c->cand_stmt);
1920 /* Replace CAND_REF candidate C, each sibling of candidate C, and each
1921 dependent of candidate C with an equivalent strength-reduced data
1922 reference. */
1924 static void
1925 replace_refs (slsr_cand_t c)
1927 if (dump_file && (dump_flags & TDF_DETAILS))
1929 fputs ("Replacing reference: ", dump_file);
1930 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
1933 if (gimple_vdef (c->cand_stmt))
1935 tree *lhs = gimple_assign_lhs_ptr (c->cand_stmt);
1936 replace_ref (lhs, c);
1938 else
1940 tree *rhs = gimple_assign_rhs1_ptr (c->cand_stmt);
1941 replace_ref (rhs, c);
1944 if (dump_file && (dump_flags & TDF_DETAILS))
1946 fputs ("With: ", dump_file);
1947 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
1948 fputs ("\n", dump_file);
1951 if (c->sibling)
1952 replace_refs (lookup_cand (c->sibling));
1954 if (c->dependent)
1955 replace_refs (lookup_cand (c->dependent));
1958 /* Return TRUE if candidate C is dependent upon a PHI. */
1960 static bool
1961 phi_dependent_cand_p (slsr_cand_t c)
1963 /* A candidate is not necessarily dependent upon a PHI just because
1964 it has a phi definition for its base name. It may have a basis
1965 that relies upon the same phi definition, in which case the PHI
1966 is irrelevant to this candidate. */
1967 return (c->def_phi
1968 && c->basis
1969 && lookup_cand (c->basis)->def_phi != c->def_phi);
1972 /* Calculate the increment required for candidate C relative to
1973 its basis. */
1975 static widest_int
1976 cand_increment (slsr_cand_t c)
1978 slsr_cand_t basis;
1980 /* If the candidate doesn't have a basis, just return its own
1981 index. This is useful in record_increments to help us find
1982 an existing initializer. Also, if the candidate's basis is
1983 hidden by a phi, then its own index will be the increment
1984 from the newly introduced phi basis. */
1985 if (!c->basis || phi_dependent_cand_p (c))
1986 return c->index;
1988 basis = lookup_cand (c->basis);
1989 gcc_assert (operand_equal_p (c->base_expr, basis->base_expr, 0));
1990 return c->index - basis->index;
1993 /* Calculate the increment required for candidate C relative to
1994 its basis. If we aren't going to generate pointer arithmetic
1995 for this candidate, return the absolute value of that increment
1996 instead. */
1998 static inline widest_int
1999 cand_abs_increment (slsr_cand_t c)
2001 widest_int increment = cand_increment (c);
2003 if (!address_arithmetic_p && wi::neg_p (increment))
2004 increment = -increment;
2006 return increment;
2009 /* Return TRUE iff candidate C has already been replaced under
2010 another interpretation. */
2012 static inline bool
2013 cand_already_replaced (slsr_cand_t c)
2015 return (gimple_bb (c->cand_stmt) == 0);
2018 /* Common logic used by replace_unconditional_candidate and
2019 replace_conditional_candidate. */
2021 static void
2022 replace_mult_candidate (slsr_cand_t c, tree basis_name, widest_int bump)
2024 tree target_type = TREE_TYPE (gimple_assign_lhs (c->cand_stmt));
2025 enum tree_code cand_code = gimple_assign_rhs_code (c->cand_stmt);
2027 /* It is highly unlikely, but possible, that the resulting
2028 bump doesn't fit in a HWI. Abandon the replacement
2029 in this case. This does not affect siblings or dependents
2030 of C. Restriction to signed HWI is conservative for unsigned
2031 types but allows for safe negation without twisted logic. */
2032 if (wi::fits_shwi_p (bump)
2033 && bump.to_shwi () != HOST_WIDE_INT_MIN
2034 /* It is not useful to replace casts, copies, or adds of
2035 an SSA name and a constant. */
2036 && cand_code != MODIFY_EXPR
2037 && !CONVERT_EXPR_CODE_P (cand_code)
2038 && cand_code != PLUS_EXPR
2039 && cand_code != POINTER_PLUS_EXPR
2040 && cand_code != MINUS_EXPR)
2042 enum tree_code code = PLUS_EXPR;
2043 tree bump_tree;
2044 gimple stmt_to_print = NULL;
2046 /* If the basis name and the candidate's LHS have incompatible
2047 types, introduce a cast. */
2048 if (!useless_type_conversion_p (target_type, TREE_TYPE (basis_name)))
2049 basis_name = introduce_cast_before_cand (c, target_type, basis_name);
2050 if (wi::neg_p (bump))
2052 code = MINUS_EXPR;
2053 bump = -bump;
2056 bump_tree = wide_int_to_tree (target_type, bump);
2058 if (dump_file && (dump_flags & TDF_DETAILS))
2060 fputs ("Replacing: ", dump_file);
2061 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
2064 if (bump == 0)
2066 tree lhs = gimple_assign_lhs (c->cand_stmt);
2067 gassign *copy_stmt = gimple_build_assign (lhs, basis_name);
2068 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
2069 gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
2070 gsi_replace (&gsi, copy_stmt, false);
2071 c->cand_stmt = copy_stmt;
2072 if (dump_file && (dump_flags & TDF_DETAILS))
2073 stmt_to_print = copy_stmt;
2075 else
2077 tree rhs1, rhs2;
2078 if (cand_code != NEGATE_EXPR) {
2079 rhs1 = gimple_assign_rhs1 (c->cand_stmt);
2080 rhs2 = gimple_assign_rhs2 (c->cand_stmt);
2082 if (cand_code != NEGATE_EXPR
2083 && ((operand_equal_p (rhs1, basis_name, 0)
2084 && operand_equal_p (rhs2, bump_tree, 0))
2085 || (operand_equal_p (rhs1, bump_tree, 0)
2086 && operand_equal_p (rhs2, basis_name, 0))))
2088 if (dump_file && (dump_flags & TDF_DETAILS))
2090 fputs ("(duplicate, not actually replacing)", dump_file);
2091 stmt_to_print = c->cand_stmt;
2094 else
2096 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
2097 gimple_assign_set_rhs_with_ops (&gsi, code,
2098 basis_name, bump_tree);
2099 update_stmt (gsi_stmt (gsi));
2100 c->cand_stmt = gsi_stmt (gsi);
2101 if (dump_file && (dump_flags & TDF_DETAILS))
2102 stmt_to_print = gsi_stmt (gsi);
2106 if (dump_file && (dump_flags & TDF_DETAILS))
2108 fputs ("With: ", dump_file);
2109 print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
2110 fputs ("\n", dump_file);
2115 /* Replace candidate C with an add or subtract. Note that we only
2116 operate on CAND_MULTs with known strides, so we will never generate
2117 a POINTER_PLUS_EXPR. Each candidate X = (B + i) * S is replaced by
2118 X = Y + ((i - i') * S), as described in the module commentary. The
2119 folded value ((i - i') * S) is referred to here as the "bump." */
2121 static void
2122 replace_unconditional_candidate (slsr_cand_t c)
2124 slsr_cand_t basis;
2126 if (cand_already_replaced (c))
2127 return;
2129 basis = lookup_cand (c->basis);
2130 widest_int bump = cand_increment (c) * wi::to_widest (c->stride);
2132 replace_mult_candidate (c, gimple_assign_lhs (basis->cand_stmt), bump);
2135 /* Return the index in the increment vector of the given INCREMENT,
2136 or -1 if not found. The latter can occur if more than
2137 MAX_INCR_VEC_LEN increments have been found. */
2139 static inline int
2140 incr_vec_index (const widest_int &increment)
2142 unsigned i;
2144 for (i = 0; i < incr_vec_len && increment != incr_vec[i].incr; i++)
2147 if (i < incr_vec_len)
2148 return i;
2149 else
2150 return -1;
2153 /* Create a new statement along edge E to add BASIS_NAME to the product
2154 of INCREMENT and the stride of candidate C. Create and return a new
2155 SSA name from *VAR to be used as the LHS of the new statement.
2156 KNOWN_STRIDE is true iff C's stride is a constant. */
2158 static tree
2159 create_add_on_incoming_edge (slsr_cand_t c, tree basis_name,
2160 widest_int increment, edge e, location_t loc,
2161 bool known_stride)
2163 basic_block insert_bb;
2164 gimple_stmt_iterator gsi;
2165 tree lhs, basis_type;
2166 gassign *new_stmt;
2168 /* If the add candidate along this incoming edge has the same
2169 index as C's hidden basis, the hidden basis represents this
2170 edge correctly. */
2171 if (increment == 0)
2172 return basis_name;
2174 basis_type = TREE_TYPE (basis_name);
2175 lhs = make_temp_ssa_name (basis_type, NULL, "slsr");
2177 if (known_stride)
2179 tree bump_tree;
2180 enum tree_code code = PLUS_EXPR;
2181 widest_int bump = increment * wi::to_widest (c->stride);
2182 if (wi::neg_p (bump))
2184 code = MINUS_EXPR;
2185 bump = -bump;
2188 bump_tree = wide_int_to_tree (basis_type, bump);
2189 new_stmt = gimple_build_assign (lhs, code, basis_name, bump_tree);
2191 else
2193 int i;
2194 bool negate_incr = (!address_arithmetic_p && wi::neg_p (increment));
2195 i = incr_vec_index (negate_incr ? -increment : increment);
2196 gcc_assert (i >= 0);
2198 if (incr_vec[i].initializer)
2200 enum tree_code code = negate_incr ? MINUS_EXPR : PLUS_EXPR;
2201 new_stmt = gimple_build_assign (lhs, code, basis_name,
2202 incr_vec[i].initializer);
2204 else if (increment == 1)
2205 new_stmt = gimple_build_assign (lhs, PLUS_EXPR, basis_name, c->stride);
2206 else if (increment == -1)
2207 new_stmt = gimple_build_assign (lhs, MINUS_EXPR, basis_name,
2208 c->stride);
2209 else
2210 gcc_unreachable ();
2213 insert_bb = single_succ_p (e->src) ? e->src : split_edge (e);
2214 gsi = gsi_last_bb (insert_bb);
2216 if (!gsi_end_p (gsi) && is_ctrl_stmt (gsi_stmt (gsi)))
2217 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
2218 else
2219 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
2221 gimple_set_location (new_stmt, loc);
2223 if (dump_file && (dump_flags & TDF_DETAILS))
2225 fprintf (dump_file, "Inserting in block %d: ", insert_bb->index);
2226 print_gimple_stmt (dump_file, new_stmt, 0, 0);
2229 return lhs;
2232 /* Given a candidate C with BASIS_NAME being the LHS of C's basis which
2233 is hidden by the phi node FROM_PHI, create a new phi node in the same
2234 block as FROM_PHI. The new phi is suitable for use as a basis by C,
2235 with its phi arguments representing conditional adjustments to the
2236 hidden basis along conditional incoming paths. Those adjustments are
2237 made by creating add statements (and sometimes recursively creating
2238 phis) along those incoming paths. LOC is the location to attach to
2239 the introduced statements. KNOWN_STRIDE is true iff C's stride is a
2240 constant. */
2242 static tree
2243 create_phi_basis (slsr_cand_t c, gimple from_phi, tree basis_name,
2244 location_t loc, bool known_stride)
2246 int i;
2247 tree name, phi_arg;
2248 gphi *phi;
2249 vec<tree> phi_args;
2250 slsr_cand_t basis = lookup_cand (c->basis);
2251 int nargs = gimple_phi_num_args (from_phi);
2252 basic_block phi_bb = gimple_bb (from_phi);
2253 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (from_phi));
2254 phi_args.create (nargs);
2256 /* Process each argument of the existing phi that represents
2257 conditionally-executed add candidates. */
2258 for (i = 0; i < nargs; i++)
2260 edge e = (*phi_bb->preds)[i];
2261 tree arg = gimple_phi_arg_def (from_phi, i);
2262 tree feeding_def;
2264 /* If the phi argument is the base name of the CAND_PHI, then
2265 this incoming arc should use the hidden basis. */
2266 if (operand_equal_p (arg, phi_cand->base_expr, 0))
2267 if (basis->index == 0)
2268 feeding_def = gimple_assign_lhs (basis->cand_stmt);
2269 else
2271 widest_int incr = -basis->index;
2272 feeding_def = create_add_on_incoming_edge (c, basis_name, incr,
2273 e, loc, known_stride);
2275 else
2277 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2279 /* If there is another phi along this incoming edge, we must
2280 process it in the same fashion to ensure that all basis
2281 adjustments are made along its incoming edges. */
2282 if (gimple_code (arg_def) == GIMPLE_PHI)
2283 feeding_def = create_phi_basis (c, arg_def, basis_name,
2284 loc, known_stride);
2285 else
2287 slsr_cand_t arg_cand = base_cand_from_table (arg);
2288 widest_int diff = arg_cand->index - basis->index;
2289 feeding_def = create_add_on_incoming_edge (c, basis_name, diff,
2290 e, loc, known_stride);
2294 /* Because of recursion, we need to save the arguments in a vector
2295 so we can create the PHI statement all at once. Otherwise the
2296 storage for the half-created PHI can be reclaimed. */
2297 phi_args.safe_push (feeding_def);
2300 /* Create the new phi basis. */
2301 name = make_temp_ssa_name (TREE_TYPE (basis_name), NULL, "slsr");
2302 phi = create_phi_node (name, phi_bb);
2303 SSA_NAME_DEF_STMT (name) = phi;
2305 FOR_EACH_VEC_ELT (phi_args, i, phi_arg)
2307 edge e = (*phi_bb->preds)[i];
2308 add_phi_arg (phi, phi_arg, e, loc);
2311 update_stmt (phi);
2313 if (dump_file && (dump_flags & TDF_DETAILS))
2315 fputs ("Introducing new phi basis: ", dump_file);
2316 print_gimple_stmt (dump_file, phi, 0, 0);
2319 return name;
2322 /* Given a candidate C whose basis is hidden by at least one intervening
2323 phi, introduce a matching number of new phis to represent its basis
2324 adjusted by conditional increments along possible incoming paths. Then
2325 replace C as though it were an unconditional candidate, using the new
2326 basis. */
2328 static void
2329 replace_conditional_candidate (slsr_cand_t c)
2331 tree basis_name, name;
2332 slsr_cand_t basis;
2333 location_t loc;
2335 /* Look up the LHS SSA name from C's basis. This will be the
2336 RHS1 of the adds we will introduce to create new phi arguments. */
2337 basis = lookup_cand (c->basis);
2338 basis_name = gimple_assign_lhs (basis->cand_stmt);
2340 /* Create a new phi statement which will represent C's true basis
2341 after the transformation is complete. */
2342 loc = gimple_location (c->cand_stmt);
2343 name = create_phi_basis (c, lookup_cand (c->def_phi)->cand_stmt,
2344 basis_name, loc, KNOWN_STRIDE);
2345 /* Replace C with an add of the new basis phi and a constant. */
2346 widest_int bump = c->index * wi::to_widest (c->stride);
2348 replace_mult_candidate (c, name, bump);
2351 /* Compute the expected costs of inserting basis adjustments for
2352 candidate C with phi-definition PHI. The cost of inserting
2353 one adjustment is given by ONE_ADD_COST. If PHI has arguments
2354 which are themselves phi results, recursively calculate costs
2355 for those phis as well. */
2357 static int
2358 phi_add_costs (gimple phi, slsr_cand_t c, int one_add_cost)
2360 unsigned i;
2361 int cost = 0;
2362 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2364 /* If we work our way back to a phi that isn't dominated by the hidden
2365 basis, this isn't a candidate for replacement. Indicate this by
2366 returning an unreasonably high cost. It's not easy to detect
2367 these situations when determining the basis, so we defer the
2368 decision until now. */
2369 basic_block phi_bb = gimple_bb (phi);
2370 slsr_cand_t basis = lookup_cand (c->basis);
2371 basic_block basis_bb = gimple_bb (basis->cand_stmt);
2373 if (phi_bb == basis_bb || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
2374 return COST_INFINITE;
2376 for (i = 0; i < gimple_phi_num_args (phi); i++)
2378 tree arg = gimple_phi_arg_def (phi, i);
2380 if (arg != phi_cand->base_expr)
2382 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2384 if (gimple_code (arg_def) == GIMPLE_PHI)
2385 cost += phi_add_costs (arg_def, c, one_add_cost);
2386 else
2388 slsr_cand_t arg_cand = base_cand_from_table (arg);
2390 if (arg_cand->index != c->index)
2391 cost += one_add_cost;
2396 return cost;
2399 /* For candidate C, each sibling of candidate C, and each dependent of
2400 candidate C, determine whether the candidate is dependent upon a
2401 phi that hides its basis. If not, replace the candidate unconditionally.
2402 Otherwise, determine whether the cost of introducing compensation code
2403 for the candidate is offset by the gains from strength reduction. If
2404 so, replace the candidate and introduce the compensation code. */
2406 static void
2407 replace_uncond_cands_and_profitable_phis (slsr_cand_t c)
2409 if (phi_dependent_cand_p (c))
2411 if (c->kind == CAND_MULT)
2413 /* A candidate dependent upon a phi will replace a multiply by
2414 a constant with an add, and will insert at most one add for
2415 each phi argument. Add these costs with the potential dead-code
2416 savings to determine profitability. */
2417 bool speed = optimize_bb_for_speed_p (gimple_bb (c->cand_stmt));
2418 int mult_savings = stmt_cost (c->cand_stmt, speed);
2419 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
2420 tree phi_result = gimple_phi_result (phi);
2421 int one_add_cost = add_cost (speed,
2422 TYPE_MODE (TREE_TYPE (phi_result)));
2423 int add_costs = one_add_cost + phi_add_costs (phi, c, one_add_cost);
2424 int cost = add_costs - mult_savings - c->dead_savings;
2426 if (dump_file && (dump_flags & TDF_DETAILS))
2428 fprintf (dump_file, " Conditional candidate %d:\n", c->cand_num);
2429 fprintf (dump_file, " add_costs = %d\n", add_costs);
2430 fprintf (dump_file, " mult_savings = %d\n", mult_savings);
2431 fprintf (dump_file, " dead_savings = %d\n", c->dead_savings);
2432 fprintf (dump_file, " cost = %d\n", cost);
2433 if (cost <= COST_NEUTRAL)
2434 fputs (" Replacing...\n", dump_file);
2435 else
2436 fputs (" Not replaced.\n", dump_file);
2439 if (cost <= COST_NEUTRAL)
2440 replace_conditional_candidate (c);
2443 else
2444 replace_unconditional_candidate (c);
2446 if (c->sibling)
2447 replace_uncond_cands_and_profitable_phis (lookup_cand (c->sibling));
2449 if (c->dependent)
2450 replace_uncond_cands_and_profitable_phis (lookup_cand (c->dependent));
2453 /* Count the number of candidates in the tree rooted at C that have
2454 not already been replaced under other interpretations. */
2456 static int
2457 count_candidates (slsr_cand_t c)
2459 unsigned count = cand_already_replaced (c) ? 0 : 1;
2461 if (c->sibling)
2462 count += count_candidates (lookup_cand (c->sibling));
2464 if (c->dependent)
2465 count += count_candidates (lookup_cand (c->dependent));
2467 return count;
2470 /* Increase the count of INCREMENT by one in the increment vector.
2471 INCREMENT is associated with candidate C. If INCREMENT is to be
2472 conditionally executed as part of a conditional candidate replacement,
2473 IS_PHI_ADJUST is true, otherwise false. If an initializer
2474 T_0 = stride * I is provided by a candidate that dominates all
2475 candidates with the same increment, also record T_0 for subsequent use. */
2477 static void
2478 record_increment (slsr_cand_t c, widest_int increment, bool is_phi_adjust)
2480 bool found = false;
2481 unsigned i;
2483 /* Treat increments that differ only in sign as identical so as to
2484 share initializers, unless we are generating pointer arithmetic. */
2485 if (!address_arithmetic_p && wi::neg_p (increment))
2486 increment = -increment;
2488 for (i = 0; i < incr_vec_len; i++)
2490 if (incr_vec[i].incr == increment)
2492 incr_vec[i].count++;
2493 found = true;
2495 /* If we previously recorded an initializer that doesn't
2496 dominate this candidate, it's not going to be useful to
2497 us after all. */
2498 if (incr_vec[i].initializer
2499 && !dominated_by_p (CDI_DOMINATORS,
2500 gimple_bb (c->cand_stmt),
2501 incr_vec[i].init_bb))
2503 incr_vec[i].initializer = NULL_TREE;
2504 incr_vec[i].init_bb = NULL;
2507 break;
2511 if (!found && incr_vec_len < MAX_INCR_VEC_LEN - 1)
2513 /* The first time we see an increment, create the entry for it.
2514 If this is the root candidate which doesn't have a basis, set
2515 the count to zero. We're only processing it so it can possibly
2516 provide an initializer for other candidates. */
2517 incr_vec[incr_vec_len].incr = increment;
2518 incr_vec[incr_vec_len].count = c->basis || is_phi_adjust ? 1 : 0;
2519 incr_vec[incr_vec_len].cost = COST_INFINITE;
2521 /* Optimistically record the first occurrence of this increment
2522 as providing an initializer (if it does); we will revise this
2523 opinion later if it doesn't dominate all other occurrences.
2524 Exception: increments of -1, 0, 1 never need initializers;
2525 and phi adjustments don't ever provide initializers. */
2526 if (c->kind == CAND_ADD
2527 && !is_phi_adjust
2528 && c->index == increment
2529 && (wi::gts_p (increment, 1)
2530 || wi::lts_p (increment, -1))
2531 && (gimple_assign_rhs_code (c->cand_stmt) == PLUS_EXPR
2532 || gimple_assign_rhs_code (c->cand_stmt) == POINTER_PLUS_EXPR))
2534 tree t0 = NULL_TREE;
2535 tree rhs1 = gimple_assign_rhs1 (c->cand_stmt);
2536 tree rhs2 = gimple_assign_rhs2 (c->cand_stmt);
2537 if (operand_equal_p (rhs1, c->base_expr, 0))
2538 t0 = rhs2;
2539 else if (operand_equal_p (rhs2, c->base_expr, 0))
2540 t0 = rhs1;
2541 if (t0
2542 && SSA_NAME_DEF_STMT (t0)
2543 && gimple_bb (SSA_NAME_DEF_STMT (t0)))
2545 incr_vec[incr_vec_len].initializer = t0;
2546 incr_vec[incr_vec_len++].init_bb
2547 = gimple_bb (SSA_NAME_DEF_STMT (t0));
2549 else
2551 incr_vec[incr_vec_len].initializer = NULL_TREE;
2552 incr_vec[incr_vec_len++].init_bb = NULL;
2555 else
2557 incr_vec[incr_vec_len].initializer = NULL_TREE;
2558 incr_vec[incr_vec_len++].init_bb = NULL;
2563 /* Given phi statement PHI that hides a candidate from its BASIS, find
2564 the increments along each incoming arc (recursively handling additional
2565 phis that may be present) and record them. These increments are the
2566 difference in index between the index-adjusting statements and the
2567 index of the basis. */
2569 static void
2570 record_phi_increments (slsr_cand_t basis, gimple phi)
2572 unsigned i;
2573 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2575 for (i = 0; i < gimple_phi_num_args (phi); i++)
2577 tree arg = gimple_phi_arg_def (phi, i);
2579 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
2581 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2583 if (gimple_code (arg_def) == GIMPLE_PHI)
2584 record_phi_increments (basis, arg_def);
2585 else
2587 slsr_cand_t arg_cand = base_cand_from_table (arg);
2588 widest_int diff = arg_cand->index - basis->index;
2589 record_increment (arg_cand, diff, PHI_ADJUST);
2595 /* Determine how many times each unique increment occurs in the set
2596 of candidates rooted at C's parent, recording the data in the
2597 increment vector. For each unique increment I, if an initializer
2598 T_0 = stride * I is provided by a candidate that dominates all
2599 candidates with the same increment, also record T_0 for subsequent
2600 use. */
2602 static void
2603 record_increments (slsr_cand_t c)
2605 if (!cand_already_replaced (c))
2607 if (!phi_dependent_cand_p (c))
2608 record_increment (c, cand_increment (c), NOT_PHI_ADJUST);
2609 else
2611 /* A candidate with a basis hidden by a phi will have one
2612 increment for its relationship to the index represented by
2613 the phi, and potentially additional increments along each
2614 incoming edge. For the root of the dependency tree (which
2615 has no basis), process just the initial index in case it has
2616 an initializer that can be used by subsequent candidates. */
2617 record_increment (c, c->index, NOT_PHI_ADJUST);
2619 if (c->basis)
2620 record_phi_increments (lookup_cand (c->basis),
2621 lookup_cand (c->def_phi)->cand_stmt);
2625 if (c->sibling)
2626 record_increments (lookup_cand (c->sibling));
2628 if (c->dependent)
2629 record_increments (lookup_cand (c->dependent));
2632 /* Add up and return the costs of introducing add statements that
2633 require the increment INCR on behalf of candidate C and phi
2634 statement PHI. Accumulate into *SAVINGS the potential savings
2635 from removing existing statements that feed PHI and have no other
2636 uses. */
2638 static int
2639 phi_incr_cost (slsr_cand_t c, const widest_int &incr, gimple phi, int *savings)
2641 unsigned i;
2642 int cost = 0;
2643 slsr_cand_t basis = lookup_cand (c->basis);
2644 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2646 for (i = 0; i < gimple_phi_num_args (phi); i++)
2648 tree arg = gimple_phi_arg_def (phi, i);
2650 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
2652 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2654 if (gimple_code (arg_def) == GIMPLE_PHI)
2656 int feeding_savings = 0;
2657 cost += phi_incr_cost (c, incr, arg_def, &feeding_savings);
2658 if (has_single_use (gimple_phi_result (arg_def)))
2659 *savings += feeding_savings;
2661 else
2663 slsr_cand_t arg_cand = base_cand_from_table (arg);
2664 widest_int diff = arg_cand->index - basis->index;
2666 if (incr == diff)
2668 tree basis_lhs = gimple_assign_lhs (basis->cand_stmt);
2669 tree lhs = gimple_assign_lhs (arg_cand->cand_stmt);
2670 cost += add_cost (true, TYPE_MODE (TREE_TYPE (basis_lhs)));
2671 if (has_single_use (lhs))
2672 *savings += stmt_cost (arg_cand->cand_stmt, true);
2678 return cost;
2681 /* Return the first candidate in the tree rooted at C that has not
2682 already been replaced, favoring siblings over dependents. */
2684 static slsr_cand_t
2685 unreplaced_cand_in_tree (slsr_cand_t c)
2687 if (!cand_already_replaced (c))
2688 return c;
2690 if (c->sibling)
2692 slsr_cand_t sib = unreplaced_cand_in_tree (lookup_cand (c->sibling));
2693 if (sib)
2694 return sib;
2697 if (c->dependent)
2699 slsr_cand_t dep = unreplaced_cand_in_tree (lookup_cand (c->dependent));
2700 if (dep)
2701 return dep;
2704 return NULL;
2707 /* Return TRUE if the candidates in the tree rooted at C should be
2708 optimized for speed, else FALSE. We estimate this based on the block
2709 containing the most dominant candidate in the tree that has not yet
2710 been replaced. */
2712 static bool
2713 optimize_cands_for_speed_p (slsr_cand_t c)
2715 slsr_cand_t c2 = unreplaced_cand_in_tree (c);
2716 gcc_assert (c2);
2717 return optimize_bb_for_speed_p (gimple_bb (c2->cand_stmt));
2720 /* Add COST_IN to the lowest cost of any dependent path starting at
2721 candidate C or any of its siblings, counting only candidates along
2722 such paths with increment INCR. Assume that replacing a candidate
2723 reduces cost by REPL_SAVINGS. Also account for savings from any
2724 statements that would go dead. If COUNT_PHIS is true, include
2725 costs of introducing feeding statements for conditional candidates. */
2727 static int
2728 lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c,
2729 const widest_int &incr, bool count_phis)
2731 int local_cost, sib_cost, savings = 0;
2732 widest_int cand_incr = cand_abs_increment (c);
2734 if (cand_already_replaced (c))
2735 local_cost = cost_in;
2736 else if (incr == cand_incr)
2737 local_cost = cost_in - repl_savings - c->dead_savings;
2738 else
2739 local_cost = cost_in - c->dead_savings;
2741 if (count_phis
2742 && phi_dependent_cand_p (c)
2743 && !cand_already_replaced (c))
2745 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
2746 local_cost += phi_incr_cost (c, incr, phi, &savings);
2748 if (has_single_use (gimple_phi_result (phi)))
2749 local_cost -= savings;
2752 if (c->dependent)
2753 local_cost = lowest_cost_path (local_cost, repl_savings,
2754 lookup_cand (c->dependent), incr,
2755 count_phis);
2757 if (c->sibling)
2759 sib_cost = lowest_cost_path (cost_in, repl_savings,
2760 lookup_cand (c->sibling), incr,
2761 count_phis);
2762 local_cost = MIN (local_cost, sib_cost);
2765 return local_cost;
2768 /* Compute the total savings that would accrue from all replacements
2769 in the candidate tree rooted at C, counting only candidates with
2770 increment INCR. Assume that replacing a candidate reduces cost
2771 by REPL_SAVINGS. Also account for savings from statements that
2772 would go dead. */
2774 static int
2775 total_savings (int repl_savings, slsr_cand_t c, const widest_int &incr,
2776 bool count_phis)
2778 int savings = 0;
2779 widest_int cand_incr = cand_abs_increment (c);
2781 if (incr == cand_incr && !cand_already_replaced (c))
2782 savings += repl_savings + c->dead_savings;
2784 if (count_phis
2785 && phi_dependent_cand_p (c)
2786 && !cand_already_replaced (c))
2788 int phi_savings = 0;
2789 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
2790 savings -= phi_incr_cost (c, incr, phi, &phi_savings);
2792 if (has_single_use (gimple_phi_result (phi)))
2793 savings += phi_savings;
2796 if (c->dependent)
2797 savings += total_savings (repl_savings, lookup_cand (c->dependent), incr,
2798 count_phis);
2800 if (c->sibling)
2801 savings += total_savings (repl_savings, lookup_cand (c->sibling), incr,
2802 count_phis);
2804 return savings;
2807 /* Use target-specific costs to determine and record which increments
2808 in the current candidate tree are profitable to replace, assuming
2809 MODE and SPEED. FIRST_DEP is the first dependent of the root of
2810 the candidate tree.
2812 One slight limitation here is that we don't account for the possible
2813 introduction of casts in some cases. See replace_one_candidate for
2814 the cases where these are introduced. This should probably be cleaned
2815 up sometime. */
2817 static void
2818 analyze_increments (slsr_cand_t first_dep, machine_mode mode, bool speed)
2820 unsigned i;
2822 for (i = 0; i < incr_vec_len; i++)
2824 HOST_WIDE_INT incr = incr_vec[i].incr.to_shwi ();
2826 /* If somehow this increment is bigger than a HWI, we won't
2827 be optimizing candidates that use it. And if the increment
2828 has a count of zero, nothing will be done with it. */
2829 if (!wi::fits_shwi_p (incr_vec[i].incr) || !incr_vec[i].count)
2830 incr_vec[i].cost = COST_INFINITE;
2832 /* Increments of 0, 1, and -1 are always profitable to replace,
2833 because they always replace a multiply or add with an add or
2834 copy, and may cause one or more existing instructions to go
2835 dead. Exception: -1 can't be assumed to be profitable for
2836 pointer addition. */
2837 else if (incr == 0
2838 || incr == 1
2839 || (incr == -1
2840 && (gimple_assign_rhs_code (first_dep->cand_stmt)
2841 != POINTER_PLUS_EXPR)))
2842 incr_vec[i].cost = COST_NEUTRAL;
2844 /* FORNOW: If we need to add an initializer, give up if a cast from
2845 the candidate's type to its stride's type can lose precision.
2846 This could eventually be handled better by expressly retaining the
2847 result of a cast to a wider type in the stride. Example:
2849 short int _1;
2850 _2 = (int) _1;
2851 _3 = _2 * 10;
2852 _4 = x + _3; ADD: x + (10 * _1) : int
2853 _5 = _2 * 15;
2854 _6 = x + _3; ADD: x + (15 * _1) : int
2856 Right now replacing _6 would cause insertion of an initializer
2857 of the form "short int T = _1 * 5;" followed by a cast to
2858 int, which could overflow incorrectly. Had we recorded _2 or
2859 (int)_1 as the stride, this wouldn't happen. However, doing
2860 this breaks other opportunities, so this will require some
2861 care. */
2862 else if (!incr_vec[i].initializer
2863 && TREE_CODE (first_dep->stride) != INTEGER_CST
2864 && !legal_cast_p_1 (first_dep->stride,
2865 gimple_assign_lhs (first_dep->cand_stmt)))
2867 incr_vec[i].cost = COST_INFINITE;
2869 /* If we need to add an initializer, make sure we don't introduce
2870 a multiply by a pointer type, which can happen in certain cast
2871 scenarios. FIXME: When cleaning up these cast issues, we can
2872 afford to introduce the multiply provided we cast out to an
2873 unsigned int of appropriate size. */
2874 else if (!incr_vec[i].initializer
2875 && TREE_CODE (first_dep->stride) != INTEGER_CST
2876 && POINTER_TYPE_P (TREE_TYPE (first_dep->stride)))
2878 incr_vec[i].cost = COST_INFINITE;
2880 /* For any other increment, if this is a multiply candidate, we
2881 must introduce a temporary T and initialize it with
2882 T_0 = stride * increment. When optimizing for speed, walk the
2883 candidate tree to calculate the best cost reduction along any
2884 path; if it offsets the fixed cost of inserting the initializer,
2885 replacing the increment is profitable. When optimizing for
2886 size, instead calculate the total cost reduction from replacing
2887 all candidates with this increment. */
2888 else if (first_dep->kind == CAND_MULT)
2890 int cost = mult_by_coeff_cost (incr, mode, speed);
2891 int repl_savings = mul_cost (speed, mode) - add_cost (speed, mode);
2892 if (speed)
2893 cost = lowest_cost_path (cost, repl_savings, first_dep,
2894 incr_vec[i].incr, COUNT_PHIS);
2895 else
2896 cost -= total_savings (repl_savings, first_dep, incr_vec[i].incr,
2897 COUNT_PHIS);
2899 incr_vec[i].cost = cost;
2902 /* If this is an add candidate, the initializer may already
2903 exist, so only calculate the cost of the initializer if it
2904 doesn't. We are replacing one add with another here, so the
2905 known replacement savings is zero. We will account for removal
2906 of dead instructions in lowest_cost_path or total_savings. */
2907 else
2909 int cost = 0;
2910 if (!incr_vec[i].initializer)
2911 cost = mult_by_coeff_cost (incr, mode, speed);
2913 if (speed)
2914 cost = lowest_cost_path (cost, 0, first_dep, incr_vec[i].incr,
2915 DONT_COUNT_PHIS);
2916 else
2917 cost -= total_savings (0, first_dep, incr_vec[i].incr,
2918 DONT_COUNT_PHIS);
2920 incr_vec[i].cost = cost;
2925 /* Return the nearest common dominator of BB1 and BB2. If the blocks
2926 are identical, return the earlier of C1 and C2 in *WHERE. Otherwise,
2927 if the NCD matches BB1, return C1 in *WHERE; if the NCD matches BB2,
2928 return C2 in *WHERE; and if the NCD matches neither, return NULL in
2929 *WHERE. Note: It is possible for one of C1 and C2 to be NULL. */
2931 static basic_block
2932 ncd_for_two_cands (basic_block bb1, basic_block bb2,
2933 slsr_cand_t c1, slsr_cand_t c2, slsr_cand_t *where)
2935 basic_block ncd;
2937 if (!bb1)
2939 *where = c2;
2940 return bb2;
2943 if (!bb2)
2945 *where = c1;
2946 return bb1;
2949 ncd = nearest_common_dominator (CDI_DOMINATORS, bb1, bb2);
2951 /* If both candidates are in the same block, the earlier
2952 candidate wins. */
2953 if (bb1 == ncd && bb2 == ncd)
2955 if (!c1 || (c2 && c2->cand_num < c1->cand_num))
2956 *where = c2;
2957 else
2958 *where = c1;
2961 /* Otherwise, if one of them produced a candidate in the
2962 dominator, that one wins. */
2963 else if (bb1 == ncd)
2964 *where = c1;
2966 else if (bb2 == ncd)
2967 *where = c2;
2969 /* If neither matches the dominator, neither wins. */
2970 else
2971 *where = NULL;
2973 return ncd;
2976 /* Consider all candidates that feed PHI. Find the nearest common
2977 dominator of those candidates requiring the given increment INCR.
2978 Further find and return the nearest common dominator of this result
2979 with block NCD. If the returned block contains one or more of the
2980 candidates, return the earliest candidate in the block in *WHERE. */
2982 static basic_block
2983 ncd_with_phi (slsr_cand_t c, const widest_int &incr, gphi *phi,
2984 basic_block ncd, slsr_cand_t *where)
2986 unsigned i;
2987 slsr_cand_t basis = lookup_cand (c->basis);
2988 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2990 for (i = 0; i < gimple_phi_num_args (phi); i++)
2992 tree arg = gimple_phi_arg_def (phi, i);
2994 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
2996 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2998 if (gimple_code (arg_def) == GIMPLE_PHI)
2999 ncd = ncd_with_phi (c, incr, as_a <gphi *> (arg_def), ncd,
3000 where);
3001 else
3003 slsr_cand_t arg_cand = base_cand_from_table (arg);
3004 widest_int diff = arg_cand->index - basis->index;
3005 basic_block pred = gimple_phi_arg_edge (phi, i)->src;
3007 if ((incr == diff) || (!address_arithmetic_p && incr == -diff))
3008 ncd = ncd_for_two_cands (ncd, pred, *where, NULL, where);
3013 return ncd;
3016 /* Consider the candidate C together with any candidates that feed
3017 C's phi dependence (if any). Find and return the nearest common
3018 dominator of those candidates requiring the given increment INCR.
3019 If the returned block contains one or more of the candidates,
3020 return the earliest candidate in the block in *WHERE. */
3022 static basic_block
3023 ncd_of_cand_and_phis (slsr_cand_t c, const widest_int &incr, slsr_cand_t *where)
3025 basic_block ncd = NULL;
3027 if (cand_abs_increment (c) == incr)
3029 ncd = gimple_bb (c->cand_stmt);
3030 *where = c;
3033 if (phi_dependent_cand_p (c))
3034 ncd = ncd_with_phi (c, incr,
3035 as_a <gphi *> (lookup_cand (c->def_phi)->cand_stmt),
3036 ncd, where);
3038 return ncd;
3041 /* Consider all candidates in the tree rooted at C for which INCR
3042 represents the required increment of C relative to its basis.
3043 Find and return the basic block that most nearly dominates all
3044 such candidates. If the returned block contains one or more of
3045 the candidates, return the earliest candidate in the block in
3046 *WHERE. */
3048 static basic_block
3049 nearest_common_dominator_for_cands (slsr_cand_t c, const widest_int &incr,
3050 slsr_cand_t *where)
3052 basic_block sib_ncd = NULL, dep_ncd = NULL, this_ncd = NULL, ncd;
3053 slsr_cand_t sib_where = NULL, dep_where = NULL, this_where = NULL, new_where;
3055 /* First find the NCD of all siblings and dependents. */
3056 if (c->sibling)
3057 sib_ncd = nearest_common_dominator_for_cands (lookup_cand (c->sibling),
3058 incr, &sib_where);
3059 if (c->dependent)
3060 dep_ncd = nearest_common_dominator_for_cands (lookup_cand (c->dependent),
3061 incr, &dep_where);
3062 if (!sib_ncd && !dep_ncd)
3064 new_where = NULL;
3065 ncd = NULL;
3067 else if (sib_ncd && !dep_ncd)
3069 new_where = sib_where;
3070 ncd = sib_ncd;
3072 else if (dep_ncd && !sib_ncd)
3074 new_where = dep_where;
3075 ncd = dep_ncd;
3077 else
3078 ncd = ncd_for_two_cands (sib_ncd, dep_ncd, sib_where,
3079 dep_where, &new_where);
3081 /* If the candidate's increment doesn't match the one we're interested
3082 in (and nor do any increments for feeding defs of a phi-dependence),
3083 then the result depends only on siblings and dependents. */
3084 this_ncd = ncd_of_cand_and_phis (c, incr, &this_where);
3086 if (!this_ncd || cand_already_replaced (c))
3088 *where = new_where;
3089 return ncd;
3092 /* Otherwise, compare this candidate with the result from all siblings
3093 and dependents. */
3094 ncd = ncd_for_two_cands (ncd, this_ncd, new_where, this_where, where);
3096 return ncd;
3099 /* Return TRUE if the increment indexed by INDEX is profitable to replace. */
3101 static inline bool
3102 profitable_increment_p (unsigned index)
3104 return (incr_vec[index].cost <= COST_NEUTRAL);
3107 /* For each profitable increment in the increment vector not equal to
3108 0 or 1 (or -1, for non-pointer arithmetic), find the nearest common
3109 dominator of all statements in the candidate chain rooted at C
3110 that require that increment, and insert an initializer
3111 T_0 = stride * increment at that location. Record T_0 with the
3112 increment record. */
3114 static void
3115 insert_initializers (slsr_cand_t c)
3117 unsigned i;
3119 for (i = 0; i < incr_vec_len; i++)
3121 basic_block bb;
3122 slsr_cand_t where = NULL;
3123 gassign *init_stmt;
3124 tree stride_type, new_name, incr_tree;
3125 widest_int incr = incr_vec[i].incr;
3127 if (!profitable_increment_p (i)
3128 || incr == 1
3129 || (incr == -1
3130 && gimple_assign_rhs_code (c->cand_stmt) != POINTER_PLUS_EXPR)
3131 || incr == 0)
3132 continue;
3134 /* We may have already identified an existing initializer that
3135 will suffice. */
3136 if (incr_vec[i].initializer)
3138 if (dump_file && (dump_flags & TDF_DETAILS))
3140 fputs ("Using existing initializer: ", dump_file);
3141 print_gimple_stmt (dump_file,
3142 SSA_NAME_DEF_STMT (incr_vec[i].initializer),
3143 0, 0);
3145 continue;
3148 /* Find the block that most closely dominates all candidates
3149 with this increment. If there is at least one candidate in
3150 that block, the earliest one will be returned in WHERE. */
3151 bb = nearest_common_dominator_for_cands (c, incr, &where);
3153 /* Create a new SSA name to hold the initializer's value. */
3154 stride_type = TREE_TYPE (c->stride);
3155 new_name = make_temp_ssa_name (stride_type, NULL, "slsr");
3156 incr_vec[i].initializer = new_name;
3158 /* Create the initializer and insert it in the latest possible
3159 dominating position. */
3160 incr_tree = wide_int_to_tree (stride_type, incr);
3161 init_stmt = gimple_build_assign (new_name, MULT_EXPR,
3162 c->stride, incr_tree);
3163 if (where)
3165 gimple_stmt_iterator gsi = gsi_for_stmt (where->cand_stmt);
3166 gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
3167 gimple_set_location (init_stmt, gimple_location (where->cand_stmt));
3169 else
3171 gimple_stmt_iterator gsi = gsi_last_bb (bb);
3172 gimple basis_stmt = lookup_cand (c->basis)->cand_stmt;
3174 if (!gsi_end_p (gsi) && is_ctrl_stmt (gsi_stmt (gsi)))
3175 gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
3176 else
3177 gsi_insert_after (&gsi, init_stmt, GSI_SAME_STMT);
3179 gimple_set_location (init_stmt, gimple_location (basis_stmt));
3182 if (dump_file && (dump_flags & TDF_DETAILS))
3184 fputs ("Inserting initializer: ", dump_file);
3185 print_gimple_stmt (dump_file, init_stmt, 0, 0);
3190 /* Return TRUE iff all required increments for candidates feeding PHI
3191 are profitable to replace on behalf of candidate C. */
3193 static bool
3194 all_phi_incrs_profitable (slsr_cand_t c, gimple phi)
3196 unsigned i;
3197 slsr_cand_t basis = lookup_cand (c->basis);
3198 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
3200 for (i = 0; i < gimple_phi_num_args (phi); i++)
3202 tree arg = gimple_phi_arg_def (phi, i);
3204 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
3206 gimple arg_def = SSA_NAME_DEF_STMT (arg);
3208 if (gimple_code (arg_def) == GIMPLE_PHI)
3210 if (!all_phi_incrs_profitable (c, arg_def))
3211 return false;
3213 else
3215 int j;
3216 slsr_cand_t arg_cand = base_cand_from_table (arg);
3217 widest_int increment = arg_cand->index - basis->index;
3219 if (!address_arithmetic_p && wi::neg_p (increment))
3220 increment = -increment;
3222 j = incr_vec_index (increment);
3224 if (dump_file && (dump_flags & TDF_DETAILS))
3226 fprintf (dump_file, " Conditional candidate %d, phi: ",
3227 c->cand_num);
3228 print_gimple_stmt (dump_file, phi, 0, 0);
3229 fputs (" increment: ", dump_file);
3230 print_decs (increment, dump_file);
3231 if (j < 0)
3232 fprintf (dump_file,
3233 "\n Not replaced; incr_vec overflow.\n");
3234 else {
3235 fprintf (dump_file, "\n cost: %d\n", incr_vec[j].cost);
3236 if (profitable_increment_p (j))
3237 fputs (" Replacing...\n", dump_file);
3238 else
3239 fputs (" Not replaced.\n", dump_file);
3243 if (j < 0 || !profitable_increment_p (j))
3244 return false;
3249 return true;
3252 /* Create a NOP_EXPR that copies FROM_EXPR into a new SSA name of
3253 type TO_TYPE, and insert it in front of the statement represented
3254 by candidate C. Use *NEW_VAR to create the new SSA name. Return
3255 the new SSA name. */
3257 static tree
3258 introduce_cast_before_cand (slsr_cand_t c, tree to_type, tree from_expr)
3260 tree cast_lhs;
3261 gassign *cast_stmt;
3262 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3264 cast_lhs = make_temp_ssa_name (to_type, NULL, "slsr");
3265 cast_stmt = gimple_build_assign (cast_lhs, NOP_EXPR, from_expr);
3266 gimple_set_location (cast_stmt, gimple_location (c->cand_stmt));
3267 gsi_insert_before (&gsi, cast_stmt, GSI_SAME_STMT);
3269 if (dump_file && (dump_flags & TDF_DETAILS))
3271 fputs (" Inserting: ", dump_file);
3272 print_gimple_stmt (dump_file, cast_stmt, 0, 0);
3275 return cast_lhs;
3278 /* Replace the RHS of the statement represented by candidate C with
3279 NEW_CODE, NEW_RHS1, and NEW_RHS2, provided that to do so doesn't
3280 leave C unchanged or just interchange its operands. The original
3281 operation and operands are in OLD_CODE, OLD_RHS1, and OLD_RHS2.
3282 If the replacement was made and we are doing a details dump,
3283 return the revised statement, else NULL. */
3285 static gimple
3286 replace_rhs_if_not_dup (enum tree_code new_code, tree new_rhs1, tree new_rhs2,
3287 enum tree_code old_code, tree old_rhs1, tree old_rhs2,
3288 slsr_cand_t c)
3290 if (new_code != old_code
3291 || ((!operand_equal_p (new_rhs1, old_rhs1, 0)
3292 || !operand_equal_p (new_rhs2, old_rhs2, 0))
3293 && (!operand_equal_p (new_rhs1, old_rhs2, 0)
3294 || !operand_equal_p (new_rhs2, old_rhs1, 0))))
3296 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3297 gimple_assign_set_rhs_with_ops (&gsi, new_code, new_rhs1, new_rhs2);
3298 update_stmt (gsi_stmt (gsi));
3299 c->cand_stmt = gsi_stmt (gsi);
3301 if (dump_file && (dump_flags & TDF_DETAILS))
3302 return gsi_stmt (gsi);
3305 else if (dump_file && (dump_flags & TDF_DETAILS))
3306 fputs (" (duplicate, not actually replacing)\n", dump_file);
3308 return NULL;
3311 /* Strength-reduce the statement represented by candidate C by replacing
3312 it with an equivalent addition or subtraction. I is the index into
3313 the increment vector identifying C's increment. NEW_VAR is used to
3314 create a new SSA name if a cast needs to be introduced. BASIS_NAME
3315 is the rhs1 to use in creating the add/subtract. */
3317 static void
3318 replace_one_candidate (slsr_cand_t c, unsigned i, tree basis_name)
3320 gimple stmt_to_print = NULL;
3321 tree orig_rhs1, orig_rhs2;
3322 tree rhs2;
3323 enum tree_code orig_code, repl_code;
3324 widest_int cand_incr;
3326 orig_code = gimple_assign_rhs_code (c->cand_stmt);
3327 orig_rhs1 = gimple_assign_rhs1 (c->cand_stmt);
3328 orig_rhs2 = gimple_assign_rhs2 (c->cand_stmt);
3329 cand_incr = cand_increment (c);
3331 if (dump_file && (dump_flags & TDF_DETAILS))
3333 fputs ("Replacing: ", dump_file);
3334 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
3335 stmt_to_print = c->cand_stmt;
3338 if (address_arithmetic_p)
3339 repl_code = POINTER_PLUS_EXPR;
3340 else
3341 repl_code = PLUS_EXPR;
3343 /* If the increment has an initializer T_0, replace the candidate
3344 statement with an add of the basis name and the initializer. */
3345 if (incr_vec[i].initializer)
3347 tree init_type = TREE_TYPE (incr_vec[i].initializer);
3348 tree orig_type = TREE_TYPE (orig_rhs2);
3350 if (types_compatible_p (orig_type, init_type))
3351 rhs2 = incr_vec[i].initializer;
3352 else
3353 rhs2 = introduce_cast_before_cand (c, orig_type,
3354 incr_vec[i].initializer);
3356 if (incr_vec[i].incr != cand_incr)
3358 gcc_assert (repl_code == PLUS_EXPR);
3359 repl_code = MINUS_EXPR;
3362 stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
3363 orig_code, orig_rhs1, orig_rhs2,
3367 /* Otherwise, the increment is one of -1, 0, and 1. Replace
3368 with a subtract of the stride from the basis name, a copy
3369 from the basis name, or an add of the stride to the basis
3370 name, respectively. It may be necessary to introduce a
3371 cast (or reuse an existing cast). */
3372 else if (cand_incr == 1)
3374 tree stride_type = TREE_TYPE (c->stride);
3375 tree orig_type = TREE_TYPE (orig_rhs2);
3377 if (types_compatible_p (orig_type, stride_type))
3378 rhs2 = c->stride;
3379 else
3380 rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
3382 stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
3383 orig_code, orig_rhs1, orig_rhs2,
3387 else if (cand_incr == -1)
3389 tree stride_type = TREE_TYPE (c->stride);
3390 tree orig_type = TREE_TYPE (orig_rhs2);
3391 gcc_assert (repl_code != POINTER_PLUS_EXPR);
3393 if (types_compatible_p (orig_type, stride_type))
3394 rhs2 = c->stride;
3395 else
3396 rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
3398 if (orig_code != MINUS_EXPR
3399 || !operand_equal_p (basis_name, orig_rhs1, 0)
3400 || !operand_equal_p (rhs2, orig_rhs2, 0))
3402 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3403 gimple_assign_set_rhs_with_ops (&gsi, MINUS_EXPR, basis_name, rhs2);
3404 update_stmt (gsi_stmt (gsi));
3405 c->cand_stmt = gsi_stmt (gsi);
3407 if (dump_file && (dump_flags & TDF_DETAILS))
3408 stmt_to_print = gsi_stmt (gsi);
3410 else if (dump_file && (dump_flags & TDF_DETAILS))
3411 fputs (" (duplicate, not actually replacing)\n", dump_file);
3414 else if (cand_incr == 0)
3416 tree lhs = gimple_assign_lhs (c->cand_stmt);
3417 tree lhs_type = TREE_TYPE (lhs);
3418 tree basis_type = TREE_TYPE (basis_name);
3420 if (types_compatible_p (lhs_type, basis_type))
3422 gassign *copy_stmt = gimple_build_assign (lhs, basis_name);
3423 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3424 gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
3425 gsi_replace (&gsi, copy_stmt, false);
3426 c->cand_stmt = copy_stmt;
3428 if (dump_file && (dump_flags & TDF_DETAILS))
3429 stmt_to_print = copy_stmt;
3431 else
3433 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3434 gassign *cast_stmt = gimple_build_assign (lhs, NOP_EXPR, basis_name);
3435 gimple_set_location (cast_stmt, gimple_location (c->cand_stmt));
3436 gsi_replace (&gsi, cast_stmt, false);
3437 c->cand_stmt = cast_stmt;
3439 if (dump_file && (dump_flags & TDF_DETAILS))
3440 stmt_to_print = cast_stmt;
3443 else
3444 gcc_unreachable ();
3446 if (dump_file && (dump_flags & TDF_DETAILS) && stmt_to_print)
3448 fputs ("With: ", dump_file);
3449 print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
3450 fputs ("\n", dump_file);
3454 /* For each candidate in the tree rooted at C, replace it with
3455 an increment if such has been shown to be profitable. */
3457 static void
3458 replace_profitable_candidates (slsr_cand_t c)
3460 if (!cand_already_replaced (c))
3462 widest_int increment = cand_abs_increment (c);
3463 enum tree_code orig_code = gimple_assign_rhs_code (c->cand_stmt);
3464 int i;
3466 i = incr_vec_index (increment);
3468 /* Only process profitable increments. Nothing useful can be done
3469 to a cast or copy. */
3470 if (i >= 0
3471 && profitable_increment_p (i)
3472 && orig_code != MODIFY_EXPR
3473 && !CONVERT_EXPR_CODE_P (orig_code))
3475 if (phi_dependent_cand_p (c))
3477 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
3479 if (all_phi_incrs_profitable (c, phi))
3481 /* Look up the LHS SSA name from C's basis. This will be
3482 the RHS1 of the adds we will introduce to create new
3483 phi arguments. */
3484 slsr_cand_t basis = lookup_cand (c->basis);
3485 tree basis_name = gimple_assign_lhs (basis->cand_stmt);
3487 /* Create a new phi statement that will represent C's true
3488 basis after the transformation is complete. */
3489 location_t loc = gimple_location (c->cand_stmt);
3490 tree name = create_phi_basis (c, phi, basis_name,
3491 loc, UNKNOWN_STRIDE);
3493 /* Replace C with an add of the new basis phi and the
3494 increment. */
3495 replace_one_candidate (c, i, name);
3498 else
3500 slsr_cand_t basis = lookup_cand (c->basis);
3501 tree basis_name = gimple_assign_lhs (basis->cand_stmt);
3502 replace_one_candidate (c, i, basis_name);
3507 if (c->sibling)
3508 replace_profitable_candidates (lookup_cand (c->sibling));
3510 if (c->dependent)
3511 replace_profitable_candidates (lookup_cand (c->dependent));
3514 /* Analyze costs of related candidates in the candidate vector,
3515 and make beneficial replacements. */
3517 static void
3518 analyze_candidates_and_replace (void)
3520 unsigned i;
3521 slsr_cand_t c;
3523 /* Each candidate that has a null basis and a non-null
3524 dependent is the root of a tree of related statements.
3525 Analyze each tree to determine a subset of those
3526 statements that can be replaced with maximum benefit. */
3527 FOR_EACH_VEC_ELT (cand_vec, i, c)
3529 slsr_cand_t first_dep;
3531 if (c->basis != 0 || c->dependent == 0)
3532 continue;
3534 if (dump_file && (dump_flags & TDF_DETAILS))
3535 fprintf (dump_file, "\nProcessing dependency tree rooted at %d.\n",
3536 c->cand_num);
3538 first_dep = lookup_cand (c->dependent);
3540 /* If this is a chain of CAND_REFs, unconditionally replace
3541 each of them with a strength-reduced data reference. */
3542 if (c->kind == CAND_REF)
3543 replace_refs (c);
3545 /* If the common stride of all related candidates is a known
3546 constant, each candidate without a phi-dependence can be
3547 profitably replaced. Each replaces a multiply by a single
3548 add, with the possibility that a feeding add also goes dead.
3549 A candidate with a phi-dependence is replaced only if the
3550 compensation code it requires is offset by the strength
3551 reduction savings. */
3552 else if (TREE_CODE (c->stride) == INTEGER_CST)
3553 replace_uncond_cands_and_profitable_phis (first_dep);
3555 /* When the stride is an SSA name, it may still be profitable
3556 to replace some or all of the dependent candidates, depending
3557 on whether the introduced increments can be reused, or are
3558 less expensive to calculate than the replaced statements. */
3559 else
3561 machine_mode mode;
3562 bool speed;
3564 /* Determine whether we'll be generating pointer arithmetic
3565 when replacing candidates. */
3566 address_arithmetic_p = (c->kind == CAND_ADD
3567 && POINTER_TYPE_P (c->cand_type));
3569 /* If all candidates have already been replaced under other
3570 interpretations, nothing remains to be done. */
3571 if (!count_candidates (c))
3572 continue;
3574 /* Construct an array of increments for this candidate chain. */
3575 incr_vec = XNEWVEC (incr_info, MAX_INCR_VEC_LEN);
3576 incr_vec_len = 0;
3577 record_increments (c);
3579 /* Determine which increments are profitable to replace. */
3580 mode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (c->cand_stmt)));
3581 speed = optimize_cands_for_speed_p (c);
3582 analyze_increments (first_dep, mode, speed);
3584 /* Insert initializers of the form T_0 = stride * increment
3585 for use in profitable replacements. */
3586 insert_initializers (first_dep);
3587 dump_incr_vec ();
3589 /* Perform the replacements. */
3590 replace_profitable_candidates (first_dep);
3591 free (incr_vec);
3596 namespace {
3598 const pass_data pass_data_strength_reduction =
3600 GIMPLE_PASS, /* type */
3601 "slsr", /* name */
3602 OPTGROUP_NONE, /* optinfo_flags */
3603 TV_GIMPLE_SLSR, /* tv_id */
3604 ( PROP_cfg | PROP_ssa ), /* properties_required */
3605 0, /* properties_provided */
3606 0, /* properties_destroyed */
3607 0, /* todo_flags_start */
3608 0, /* todo_flags_finish */
3611 class pass_strength_reduction : public gimple_opt_pass
3613 public:
3614 pass_strength_reduction (gcc::context *ctxt)
3615 : gimple_opt_pass (pass_data_strength_reduction, ctxt)
3618 /* opt_pass methods: */
3619 virtual bool gate (function *) { return flag_tree_slsr; }
3620 virtual unsigned int execute (function *);
3622 }; // class pass_strength_reduction
3624 unsigned
3625 pass_strength_reduction::execute (function *fun)
3627 /* Create the obstack where candidates will reside. */
3628 gcc_obstack_init (&cand_obstack);
3630 /* Allocate the candidate vector. */
3631 cand_vec.create (128);
3633 /* Allocate the mapping from statements to candidate indices. */
3634 stmt_cand_map = new hash_map<gimple, slsr_cand_t>;
3636 /* Create the obstack where candidate chains will reside. */
3637 gcc_obstack_init (&chain_obstack);
3639 /* Allocate the mapping from base expressions to candidate chains. */
3640 base_cand_map = new hash_table<cand_chain_hasher> (500);
3642 /* Allocate the mapping from bases to alternative bases. */
3643 alt_base_map = new hash_map<tree, tree>;
3645 /* Initialize the loop optimizer. We need to detect flow across
3646 back edges, and this gives us dominator information as well. */
3647 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
3649 /* Walk the CFG in predominator order looking for strength reduction
3650 candidates. */
3651 find_candidates_dom_walker (CDI_DOMINATORS)
3652 .walk (fun->cfg->x_entry_block_ptr);
3654 if (dump_file && (dump_flags & TDF_DETAILS))
3656 dump_cand_vec ();
3657 dump_cand_chains ();
3660 delete alt_base_map;
3661 free_affine_expand_cache (&name_expansions);
3663 /* Analyze costs and make appropriate replacements. */
3664 analyze_candidates_and_replace ();
3666 loop_optimizer_finalize ();
3667 delete base_cand_map;
3668 base_cand_map = NULL;
3669 obstack_free (&chain_obstack, NULL);
3670 delete stmt_cand_map;
3671 cand_vec.release ();
3672 obstack_free (&cand_obstack, NULL);
3674 return 0;
3677 } // anon namespace
3679 gimple_opt_pass *
3680 make_pass_strength_reduction (gcc::context *ctxt)
3682 return new pass_strength_reduction (ctxt);