Merge in trunk.
[official-gcc.git] / gcc / gimple-ssa-strength-reduction.c
bloba41d9722dae19a5c18e19f8fdccaf7dbf0ef16ac
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 "pointer-set.h"
41 #include "hash-table.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "gimplify-me.h"
50 #include "stor-layout.h"
51 #include "expr.h"
52 #include "tree-pass.h"
53 #include "cfgloop.h"
54 #include "gimple-pretty-print.h"
55 #include "gimple-ssa.h"
56 #include "tree-cfg.h"
57 #include "tree-phinodes.h"
58 #include "ssa-iterators.h"
59 #include "stringpool.h"
60 #include "tree-ssanames.h"
61 #include "domwalk.h"
62 #include "expmed.h"
63 #include "params.h"
64 #include "tree-ssa-address.h"
65 #include "tree-affine.h"
66 #include "wide-int-print.h"
68 /* Information about a strength reduction candidate. Each statement
69 in the candidate table represents an expression of one of the
70 following forms (the special case of CAND_REF will be described
71 later):
73 (CAND_MULT) S1: X = (B + i) * S
74 (CAND_ADD) S1: X = B + (i * S)
76 Here X and B are SSA names, i is an integer constant, and S is
77 either an SSA name or a constant. We call B the "base," i the
78 "index", and S the "stride."
80 Any statement S0 that dominates S1 and is of the form:
82 (CAND_MULT) S0: Y = (B + i') * S
83 (CAND_ADD) S0: Y = B + (i' * S)
85 is called a "basis" for S1. In both cases, S1 may be replaced by
87 S1': X = Y + (i - i') * S,
89 where (i - i') * S is folded to the extent possible.
91 All gimple statements are visited in dominator order, and each
92 statement that may contribute to one of the forms of S1 above is
93 given at least one entry in the candidate table. Such statements
94 include addition, pointer addition, subtraction, multiplication,
95 negation, copies, and nontrivial type casts. If a statement may
96 represent more than one expression of the forms of S1 above,
97 multiple "interpretations" are stored in the table and chained
98 together. Examples:
100 * An add of two SSA names may treat either operand as the base.
101 * A multiply of two SSA names, likewise.
102 * A copy or cast may be thought of as either a CAND_MULT with
103 i = 0 and S = 1, or as a CAND_ADD with i = 0 or S = 0.
105 Candidate records are allocated from an obstack. They are addressed
106 both from a hash table keyed on S1, and from a vector of candidate
107 pointers arranged in predominator order.
109 Opportunity note
110 ----------------
111 Currently we don't recognize:
113 S0: Y = (S * i') - B
114 S1: X = (S * i) - B
116 as a strength reduction opportunity, even though this S1 would
117 also be replaceable by the S1' above. This can be added if it
118 comes up in practice.
120 Strength reduction in addressing
121 --------------------------------
122 There is another kind of candidate known as CAND_REF. A CAND_REF
123 describes a statement containing a memory reference having
124 complex addressing that might benefit from strength reduction.
125 Specifically, we are interested in references for which
126 get_inner_reference returns a base address, offset, and bitpos as
127 follows:
129 base: MEM_REF (T1, C1)
130 offset: MULT_EXPR (PLUS_EXPR (T2, C2), C3)
131 bitpos: C4 * BITS_PER_UNIT
133 Here T1 and T2 are arbitrary trees, and C1, C2, C3, C4 are
134 arbitrary integer constants. Note that C2 may be zero, in which
135 case the offset will be MULT_EXPR (T2, C3).
137 When this pattern is recognized, the original memory reference
138 can be replaced with:
140 MEM_REF (POINTER_PLUS_EXPR (T1, MULT_EXPR (T2, C3)),
141 C1 + (C2 * C3) + C4)
143 which distributes the multiply to allow constant folding. When
144 two or more addressing expressions can be represented by MEM_REFs
145 of this form, differing only in the constants C1, C2, and C4,
146 making this substitution produces more efficient addressing during
147 the RTL phases. When there are not at least two expressions with
148 the same values of T1, T2, and C3, there is nothing to be gained
149 by the replacement.
151 Strength reduction of CAND_REFs uses the same infrastructure as
152 that used by CAND_MULTs and CAND_ADDs. We record T1 in the base (B)
153 field, MULT_EXPR (T2, C3) in the stride (S) field, and
154 C1 + (C2 * C3) + C4 in the index (i) field. A basis for a CAND_REF
155 is thus another CAND_REF with the same B and S values. When at
156 least two CAND_REFs are chained together using the basis relation,
157 each of them is replaced as above, resulting in improved code
158 generation for addressing.
160 Conditional candidates
161 ======================
163 Conditional candidates are best illustrated with an example.
164 Consider the code sequence:
166 (1) x_0 = ...;
167 (2) a_0 = x_0 * 5; MULT (B: x_0; i: 0; S: 5)
168 if (...)
169 (3) x_1 = x_0 + 1; ADD (B: x_0, i: 1; S: 1)
170 (4) x_2 = PHI <x_0, x_1>; PHI (B: x_0, i: 0, S: 1)
171 (5) x_3 = x_2 + 1; ADD (B: x_2, i: 1, S: 1)
172 (6) a_1 = x_3 * 5; MULT (B: x_2, i: 1; S: 5)
174 Here strength reduction is complicated by the uncertain value of x_2.
175 A legitimate transformation is:
177 (1) x_0 = ...;
178 (2) a_0 = x_0 * 5;
179 if (...)
181 (3) [x_1 = x_0 + 1;]
182 (3a) t_1 = a_0 + 5;
184 (4) [x_2 = PHI <x_0, x_1>;]
185 (4a) t_2 = PHI <a_0, t_1>;
186 (5) [x_3 = x_2 + 1;]
187 (6r) a_1 = t_2 + 5;
189 where the bracketed instructions may go dead.
191 To recognize this opportunity, we have to observe that statement (6)
192 has a "hidden basis" (2). The hidden basis is unlike a normal basis
193 in that the statement and the hidden basis have different base SSA
194 names (x_2 and x_0, respectively). The relationship is established
195 when a statement's base name (x_2) is defined by a phi statement (4),
196 each argument of which (x_0, x_1) has an identical "derived base name."
197 If the argument is defined by a candidate (as x_1 is by (3)) that is a
198 CAND_ADD having a stride of 1, the derived base name of the argument is
199 the base name of the candidate (x_0). Otherwise, the argument itself
200 is its derived base name (as is the case with argument x_0).
202 The hidden basis for statement (6) is the nearest dominating candidate
203 whose base name is the derived base name (x_0) of the feeding phi (4),
204 and whose stride is identical to that of the statement. We can then
205 create the new "phi basis" (4a) and feeding adds along incoming arcs (3a),
206 allowing the final replacement of (6) by the strength-reduced (6r).
208 To facilitate this, a new kind of candidate (CAND_PHI) is introduced.
209 A CAND_PHI is not a candidate for replacement, but is maintained in the
210 candidate table to ease discovery of hidden bases. Any phi statement
211 whose arguments share a common derived base name is entered into the
212 table with the derived base name, an (arbitrary) index of zero, and a
213 stride of 1. A statement with a hidden basis can then be detected by
214 simply looking up its feeding phi definition in the candidate table,
215 extracting the derived base name, and searching for a basis in the
216 usual manner after substituting the derived base name.
218 Note that the transformation is only valid when the original phi and
219 the statements that define the phi's arguments are all at the same
220 position in the loop hierarchy. */
223 /* Index into the candidate vector, offset by 1. VECs are zero-based,
224 while cand_idx's are one-based, with zero indicating null. */
225 typedef unsigned cand_idx;
227 /* The kind of candidate. */
228 enum cand_kind
230 CAND_MULT,
231 CAND_ADD,
232 CAND_REF,
233 CAND_PHI
236 struct slsr_cand_d
238 /* The candidate statement S1. */
239 gimple cand_stmt;
241 /* The base expression B: often an SSA name, but not always. */
242 tree base_expr;
244 /* The stride S. */
245 tree stride;
247 /* The index constant i. */
248 widest_int index;
250 /* The type of the candidate. This is normally the type of base_expr,
251 but casts may have occurred when combining feeding instructions.
252 A candidate can only be a basis for candidates of the same final type.
253 (For CAND_REFs, this is the type to be used for operand 1 of the
254 replacement MEM_REF.) */
255 tree cand_type;
257 /* The kind of candidate (CAND_MULT, etc.). */
258 enum cand_kind kind;
260 /* Index of this candidate in the candidate vector. */
261 cand_idx cand_num;
263 /* Index of the next candidate record for the same statement.
264 A statement may be useful in more than one way (e.g., due to
265 commutativity). So we can have multiple "interpretations"
266 of a statement. */
267 cand_idx next_interp;
269 /* Index of the basis statement S0, if any, in the candidate vector. */
270 cand_idx basis;
272 /* First candidate for which this candidate is a basis, if one exists. */
273 cand_idx dependent;
275 /* Next candidate having the same basis as this one. */
276 cand_idx sibling;
278 /* If this is a conditional candidate, the CAND_PHI candidate
279 that defines the base SSA name B. */
280 cand_idx def_phi;
282 /* Savings that can be expected from eliminating dead code if this
283 candidate is replaced. */
284 int dead_savings;
287 typedef struct slsr_cand_d slsr_cand, *slsr_cand_t;
288 typedef const struct slsr_cand_d *const_slsr_cand_t;
290 /* Pointers to candidates are chained together as part of a mapping
291 from base expressions to the candidates that use them. */
293 struct cand_chain_d
295 /* Base expression for the chain of candidates: often, but not
296 always, an SSA name. */
297 tree base_expr;
299 /* Pointer to a candidate. */
300 slsr_cand_t cand;
302 /* Chain pointer. */
303 struct cand_chain_d *next;
307 typedef struct cand_chain_d cand_chain, *cand_chain_t;
308 typedef const struct cand_chain_d *const_cand_chain_t;
310 /* Information about a unique "increment" associated with candidates
311 having an SSA name for a stride. An increment is the difference
312 between the index of the candidate and the index of its basis,
313 i.e., (i - i') as discussed in the module commentary.
315 When we are not going to generate address arithmetic we treat
316 increments that differ only in sign as the same, allowing sharing
317 of the cost of initializers. The absolute value of the increment
318 is stored in the incr_info. */
320 struct incr_info_d
322 /* The increment that relates a candidate to its basis. */
323 widest_int incr;
325 /* How many times the increment occurs in the candidate tree. */
326 unsigned count;
328 /* Cost of replacing candidates using this increment. Negative and
329 zero costs indicate replacement should be performed. */
330 int cost;
332 /* If this increment is profitable but is not -1, 0, or 1, it requires
333 an initializer T_0 = stride * incr to be found or introduced in the
334 nearest common dominator of all candidates. This field holds T_0
335 for subsequent use. */
336 tree initializer;
338 /* If the initializer was found to already exist, this is the block
339 where it was found. */
340 basic_block init_bb;
343 typedef struct incr_info_d incr_info, *incr_info_t;
345 /* Candidates are maintained in a vector. If candidate X dominates
346 candidate Y, then X appears before Y in the vector; but the
347 converse does not necessarily hold. */
348 static vec<slsr_cand_t> cand_vec;
350 enum cost_consts
352 COST_NEUTRAL = 0,
353 COST_INFINITE = 1000
356 enum stride_status
358 UNKNOWN_STRIDE = 0,
359 KNOWN_STRIDE = 1
362 enum phi_adjust_status
364 NOT_PHI_ADJUST = 0,
365 PHI_ADJUST = 1
368 enum count_phis_status
370 DONT_COUNT_PHIS = 0,
371 COUNT_PHIS = 1
374 /* Pointer map embodying a mapping from statements to candidates. */
375 static struct pointer_map_t *stmt_cand_map;
377 /* Obstack for candidates. */
378 static struct obstack cand_obstack;
380 /* Obstack for candidate chains. */
381 static struct obstack chain_obstack;
383 /* An array INCR_VEC of incr_infos is used during analysis of related
384 candidates having an SSA name for a stride. INCR_VEC_LEN describes
385 its current length. MAX_INCR_VEC_LEN is used to avoid costly
386 pathological cases. */
387 static incr_info_t incr_vec;
388 static unsigned incr_vec_len;
389 const int MAX_INCR_VEC_LEN = 16;
391 /* For a chain of candidates with unknown stride, indicates whether or not
392 we must generate pointer arithmetic when replacing statements. */
393 static bool address_arithmetic_p;
395 /* Forward function declarations. */
396 static slsr_cand_t base_cand_from_table (tree);
397 static tree introduce_cast_before_cand (slsr_cand_t, tree, tree);
398 static bool legal_cast_p_1 (tree, tree);
400 /* Produce a pointer to the IDX'th candidate in the candidate vector. */
402 static slsr_cand_t
403 lookup_cand (cand_idx idx)
405 return cand_vec[idx - 1];
408 /* Helper for hashing a candidate chain header. */
410 struct cand_chain_hasher : typed_noop_remove <cand_chain>
412 typedef cand_chain value_type;
413 typedef cand_chain compare_type;
414 static inline hashval_t hash (const value_type *);
415 static inline bool equal (const value_type *, const compare_type *);
418 inline hashval_t
419 cand_chain_hasher::hash (const value_type *p)
421 tree base_expr = p->base_expr;
422 return iterative_hash_expr (base_expr, 0);
425 inline bool
426 cand_chain_hasher::equal (const value_type *chain1, const compare_type *chain2)
428 return operand_equal_p (chain1->base_expr, chain2->base_expr, 0);
431 /* Hash table embodying a mapping from base exprs to chains of candidates. */
432 static hash_table <cand_chain_hasher> base_cand_map;
434 /* Pointer map used by tree_to_aff_combination_expand. */
435 static struct pointer_map_t *name_expansions;
436 /* Pointer map embodying a mapping from bases to alternative bases. */
437 static struct pointer_map_t *alt_base_map;
439 /* Given BASE, use the tree affine combiniation facilities to
440 find the underlying tree expression for BASE, with any
441 immediate offset excluded.
443 N.B. we should eliminate this backtracking with better forward
444 analysis in a future release. */
446 static tree
447 get_alternative_base (tree base)
449 tree *result = (tree *) pointer_map_contains (alt_base_map, base);
451 if (result == NULL)
453 tree expr;
454 aff_tree aff;
456 tree_to_aff_combination_expand (base, TREE_TYPE (base),
457 &aff, &name_expansions);
458 aff.offset = 0;
459 expr = aff_combination_to_tree (&aff);
461 result = (tree *) pointer_map_insert (alt_base_map, base);
462 gcc_assert (!*result);
464 if (expr == base)
465 *result = NULL;
466 else
467 *result = expr;
470 return *result;
473 /* Look in the candidate table for a CAND_PHI that defines BASE and
474 return it if found; otherwise return NULL. */
476 static cand_idx
477 find_phi_def (tree base)
479 slsr_cand_t c;
481 if (TREE_CODE (base) != SSA_NAME)
482 return 0;
484 c = base_cand_from_table (base);
486 if (!c || c->kind != CAND_PHI)
487 return 0;
489 return c->cand_num;
492 /* Helper routine for find_basis_for_candidate. May be called twice:
493 once for the candidate's base expr, and optionally again either for
494 the candidate's phi definition or for a CAND_REF's alternative base
495 expression. */
497 static slsr_cand_t
498 find_basis_for_base_expr (slsr_cand_t c, tree base_expr)
500 cand_chain mapping_key;
501 cand_chain_t chain;
502 slsr_cand_t basis = NULL;
504 // Limit potential of N^2 behavior for long candidate chains.
505 int iters = 0;
506 int max_iters = PARAM_VALUE (PARAM_MAX_SLSR_CANDIDATE_SCAN);
508 mapping_key.base_expr = base_expr;
509 chain = base_cand_map.find (&mapping_key);
511 for (; chain && iters < max_iters; chain = chain->next, ++iters)
513 slsr_cand_t one_basis = chain->cand;
515 if (one_basis->kind != c->kind
516 || one_basis->cand_stmt == c->cand_stmt
517 || !operand_equal_p (one_basis->stride, c->stride, 0)
518 || !types_compatible_p (one_basis->cand_type, c->cand_type)
519 || !dominated_by_p (CDI_DOMINATORS,
520 gimple_bb (c->cand_stmt),
521 gimple_bb (one_basis->cand_stmt)))
522 continue;
524 if (!basis || basis->cand_num < one_basis->cand_num)
525 basis = one_basis;
528 return basis;
531 /* Use the base expr from candidate C to look for possible candidates
532 that can serve as a basis for C. Each potential basis must also
533 appear in a block that dominates the candidate statement and have
534 the same stride and type. If more than one possible basis exists,
535 the one with highest index in the vector is chosen; this will be
536 the most immediately dominating basis. */
538 static int
539 find_basis_for_candidate (slsr_cand_t c)
541 slsr_cand_t basis = find_basis_for_base_expr (c, c->base_expr);
543 /* If a candidate doesn't have a basis using its base expression,
544 it may have a basis hidden by one or more intervening phis. */
545 if (!basis && c->def_phi)
547 basic_block basis_bb, phi_bb;
548 slsr_cand_t phi_cand = lookup_cand (c->def_phi);
549 basis = find_basis_for_base_expr (c, phi_cand->base_expr);
551 if (basis)
553 /* A hidden basis must dominate the phi-definition of the
554 candidate's base name. */
555 phi_bb = gimple_bb (phi_cand->cand_stmt);
556 basis_bb = gimple_bb (basis->cand_stmt);
558 if (phi_bb == basis_bb
559 || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
561 basis = NULL;
562 c->basis = 0;
565 /* If we found a hidden basis, estimate additional dead-code
566 savings if the phi and its feeding statements can be removed. */
567 if (basis && has_single_use (gimple_phi_result (phi_cand->cand_stmt)))
568 c->dead_savings += phi_cand->dead_savings;
572 if (flag_expensive_optimizations && !basis && c->kind == CAND_REF)
574 tree alt_base_expr = get_alternative_base (c->base_expr);
575 if (alt_base_expr)
576 basis = find_basis_for_base_expr (c, alt_base_expr);
579 if (basis)
581 c->sibling = basis->dependent;
582 basis->dependent = c->cand_num;
583 return basis->cand_num;
586 return 0;
589 /* Record a mapping from BASE to C, indicating that C may potentially serve
590 as a basis using that base expression. BASE may be the same as
591 C->BASE_EXPR; alternatively BASE can be a different tree that share the
592 underlining expression of C->BASE_EXPR. */
594 static void
595 record_potential_basis (slsr_cand_t c, tree base)
597 cand_chain_t node;
598 cand_chain **slot;
600 gcc_assert (base);
602 node = (cand_chain_t) obstack_alloc (&chain_obstack, sizeof (cand_chain));
603 node->base_expr = base;
604 node->cand = c;
605 node->next = NULL;
606 slot = base_cand_map.find_slot (node, INSERT);
608 if (*slot)
610 cand_chain_t head = (cand_chain_t) (*slot);
611 node->next = head->next;
612 head->next = node;
614 else
615 *slot = node;
618 /* Allocate storage for a new candidate and initialize its fields.
619 Attempt to find a basis for the candidate.
621 For CAND_REF, an alternative base may also be recorded and used
622 to find a basis. This helps cases where the expression hidden
623 behind BASE (which is usually an SSA_NAME) has immediate offset,
624 e.g.
626 a2[i][j] = 1;
627 a2[i + 20][j] = 2; */
629 static slsr_cand_t
630 alloc_cand_and_find_basis (enum cand_kind kind, gimple gs, tree base,
631 const widest_int &index, tree stride, tree ctype,
632 unsigned savings)
634 slsr_cand_t c = (slsr_cand_t) obstack_alloc (&cand_obstack,
635 sizeof (slsr_cand));
636 c->cand_stmt = gs;
637 c->base_expr = base;
638 c->stride = stride;
639 c->index = index;
640 c->cand_type = ctype;
641 c->kind = kind;
642 c->cand_num = cand_vec.length () + 1;
643 c->next_interp = 0;
644 c->dependent = 0;
645 c->sibling = 0;
646 c->def_phi = kind == CAND_MULT ? find_phi_def (base) : 0;
647 c->dead_savings = savings;
649 cand_vec.safe_push (c);
651 if (kind == CAND_PHI)
652 c->basis = 0;
653 else
654 c->basis = find_basis_for_candidate (c);
656 record_potential_basis (c, base);
657 if (flag_expensive_optimizations && kind == CAND_REF)
659 tree alt_base = get_alternative_base (base);
660 if (alt_base)
661 record_potential_basis (c, alt_base);
664 return c;
667 /* Determine the target cost of statement GS when compiling according
668 to SPEED. */
670 static int
671 stmt_cost (gimple gs, bool speed)
673 tree lhs, rhs1, rhs2;
674 enum machine_mode lhs_mode;
676 gcc_assert (is_gimple_assign (gs));
677 lhs = gimple_assign_lhs (gs);
678 rhs1 = gimple_assign_rhs1 (gs);
679 lhs_mode = TYPE_MODE (TREE_TYPE (lhs));
681 switch (gimple_assign_rhs_code (gs))
683 case MULT_EXPR:
684 rhs2 = gimple_assign_rhs2 (gs);
686 if (tree_fits_shwi_p (rhs2))
687 return mult_by_coeff_cost (tree_to_shwi (rhs2), lhs_mode, speed);
689 gcc_assert (TREE_CODE (rhs1) != INTEGER_CST);
690 return mul_cost (speed, lhs_mode);
692 case PLUS_EXPR:
693 case POINTER_PLUS_EXPR:
694 case MINUS_EXPR:
695 return add_cost (speed, lhs_mode);
697 case NEGATE_EXPR:
698 return neg_cost (speed, lhs_mode);
700 case NOP_EXPR:
701 return convert_cost (lhs_mode, TYPE_MODE (TREE_TYPE (rhs1)), speed);
703 /* Note that we don't assign costs to copies that in most cases
704 will go away. */
705 default:
709 gcc_unreachable ();
710 return 0;
713 /* Look up the defining statement for BASE_IN and return a pointer
714 to its candidate in the candidate table, if any; otherwise NULL.
715 Only CAND_ADD and CAND_MULT candidates are returned. */
717 static slsr_cand_t
718 base_cand_from_table (tree base_in)
720 slsr_cand_t *result;
722 gimple def = SSA_NAME_DEF_STMT (base_in);
723 if (!def)
724 return (slsr_cand_t) NULL;
726 result = (slsr_cand_t *) pointer_map_contains (stmt_cand_map, def);
728 if (result && (*result)->kind != CAND_REF)
729 return *result;
731 return (slsr_cand_t) NULL;
734 /* Add an entry to the statement-to-candidate mapping. */
736 static void
737 add_cand_for_stmt (gimple gs, slsr_cand_t c)
739 void **slot = pointer_map_insert (stmt_cand_map, gs);
740 gcc_assert (!*slot);
741 *slot = c;
744 /* Given PHI which contains a phi statement, determine whether it
745 satisfies all the requirements of a phi candidate. If so, create
746 a candidate. Note that a CAND_PHI never has a basis itself, but
747 is used to help find a basis for subsequent candidates. */
749 static void
750 slsr_process_phi (gimple phi, bool speed)
752 unsigned i;
753 tree arg0_base = NULL_TREE, base_type;
754 slsr_cand_t c;
755 struct loop *cand_loop = gimple_bb (phi)->loop_father;
756 unsigned savings = 0;
758 /* A CAND_PHI requires each of its arguments to have the same
759 derived base name. (See the module header commentary for a
760 definition of derived base names.) Furthermore, all feeding
761 definitions must be in the same position in the loop hierarchy
762 as PHI. */
764 for (i = 0; i < gimple_phi_num_args (phi); i++)
766 slsr_cand_t arg_cand;
767 tree arg = gimple_phi_arg_def (phi, i);
768 tree derived_base_name = NULL_TREE;
769 gimple arg_stmt = NULL;
770 basic_block arg_bb = NULL;
772 if (TREE_CODE (arg) != SSA_NAME)
773 return;
775 arg_cand = base_cand_from_table (arg);
777 if (arg_cand)
779 while (arg_cand->kind != CAND_ADD && arg_cand->kind != CAND_PHI)
781 if (!arg_cand->next_interp)
782 return;
784 arg_cand = lookup_cand (arg_cand->next_interp);
787 if (!integer_onep (arg_cand->stride))
788 return;
790 derived_base_name = arg_cand->base_expr;
791 arg_stmt = arg_cand->cand_stmt;
792 arg_bb = gimple_bb (arg_stmt);
794 /* Gather potential dead code savings if the phi statement
795 can be removed later on. */
796 if (has_single_use (arg))
798 if (gimple_code (arg_stmt) == GIMPLE_PHI)
799 savings += arg_cand->dead_savings;
800 else
801 savings += stmt_cost (arg_stmt, speed);
804 else
806 derived_base_name = arg;
808 if (SSA_NAME_IS_DEFAULT_DEF (arg))
809 arg_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
810 else
811 gimple_bb (SSA_NAME_DEF_STMT (arg));
814 if (!arg_bb || arg_bb->loop_father != cand_loop)
815 return;
817 if (i == 0)
818 arg0_base = derived_base_name;
819 else if (!operand_equal_p (derived_base_name, arg0_base, 0))
820 return;
823 /* Create the candidate. "alloc_cand_and_find_basis" is named
824 misleadingly for this case, as no basis will be sought for a
825 CAND_PHI. */
826 base_type = TREE_TYPE (arg0_base);
828 c = alloc_cand_and_find_basis (CAND_PHI, phi, arg0_base,
829 0, integer_one_node, base_type, savings);
831 /* Add the candidate to the statement-candidate mapping. */
832 add_cand_for_stmt (phi, c);
835 /* Given PBASE which is a pointer to tree, look up the defining
836 statement for it and check whether the candidate is in the
837 form of:
839 X = B + (1 * S), S is integer constant
840 X = B + (i * S), S is integer one
842 If so, set PBASE to the candidate's base_expr and return double
843 int (i * S).
844 Otherwise, just return double int zero. */
846 static widest_int
847 backtrace_base_for_ref (tree *pbase)
849 tree base_in = *pbase;
850 slsr_cand_t base_cand;
852 STRIP_NOPS (base_in);
854 /* Strip off widening conversion(s) to handle cases where
855 e.g. 'B' is widened from an 'int' in order to calculate
856 a 64-bit address. */
857 if (CONVERT_EXPR_P (base_in)
858 && legal_cast_p_1 (base_in, TREE_OPERAND (base_in, 0)))
859 base_in = get_unwidened (base_in, NULL_TREE);
861 if (TREE_CODE (base_in) != SSA_NAME)
862 return 0;
864 base_cand = base_cand_from_table (base_in);
866 while (base_cand && base_cand->kind != CAND_PHI)
868 if (base_cand->kind == CAND_ADD
869 && base_cand->index == 1
870 && TREE_CODE (base_cand->stride) == INTEGER_CST)
872 /* X = B + (1 * S), S is integer constant. */
873 *pbase = base_cand->base_expr;
874 return wi::to_widest (base_cand->stride);
876 else if (base_cand->kind == CAND_ADD
877 && TREE_CODE (base_cand->stride) == INTEGER_CST
878 && integer_onep (base_cand->stride))
880 /* X = B + (i * S), S is integer one. */
881 *pbase = base_cand->base_expr;
882 return base_cand->index;
885 if (base_cand->next_interp)
886 base_cand = lookup_cand (base_cand->next_interp);
887 else
888 base_cand = NULL;
891 return 0;
894 /* Look for the following pattern:
896 *PBASE: MEM_REF (T1, C1)
898 *POFFSET: MULT_EXPR (T2, C3) [C2 is zero]
900 MULT_EXPR (PLUS_EXPR (T2, C2), C3)
902 MULT_EXPR (MINUS_EXPR (T2, -C2), C3)
904 *PINDEX: C4 * BITS_PER_UNIT
906 If not present, leave the input values unchanged and return FALSE.
907 Otherwise, modify the input values as follows and return TRUE:
909 *PBASE: T1
910 *POFFSET: MULT_EXPR (T2, C3)
911 *PINDEX: C1 + (C2 * C3) + C4
913 When T2 is recorded by a CAND_ADD in the form of (T2' + C5), it
914 will be further restructured to:
916 *PBASE: T1
917 *POFFSET: MULT_EXPR (T2', C3)
918 *PINDEX: C1 + (C2 * C3) + C4 + (C5 * C3) */
920 static bool
921 restructure_reference (tree *pbase, tree *poffset, widest_int *pindex,
922 tree *ptype)
924 tree base = *pbase, offset = *poffset;
925 widest_int index = *pindex;
926 tree mult_op0, t1, t2, type;
927 widest_int c1, c2, c3, c4, c5;
929 if (!base
930 || !offset
931 || TREE_CODE (base) != MEM_REF
932 || TREE_CODE (offset) != MULT_EXPR
933 || TREE_CODE (TREE_OPERAND (offset, 1)) != INTEGER_CST
934 || wi::umod_floor (index, BITS_PER_UNIT) != 0)
935 return false;
937 t1 = TREE_OPERAND (base, 0);
938 c1 = widest_int::from (mem_ref_offset (base), SIGNED);
939 type = TREE_TYPE (TREE_OPERAND (base, 1));
941 mult_op0 = TREE_OPERAND (offset, 0);
942 c3 = wi::to_widest (TREE_OPERAND (offset, 1));
944 if (TREE_CODE (mult_op0) == PLUS_EXPR)
946 if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
948 t2 = TREE_OPERAND (mult_op0, 0);
949 c2 = wi::to_widest (TREE_OPERAND (mult_op0, 1));
951 else
952 return false;
954 else if (TREE_CODE (mult_op0) == MINUS_EXPR)
956 if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
958 t2 = TREE_OPERAND (mult_op0, 0);
959 c2 = -wi::to_widest (TREE_OPERAND (mult_op0, 1));
961 else
962 return false;
964 else
966 t2 = mult_op0;
967 c2 = 0;
970 c4 = wi::lrshift (index, LOG2_BITS_PER_UNIT);
971 c5 = backtrace_base_for_ref (&t2);
973 *pbase = t1;
974 *poffset = fold_build2 (MULT_EXPR, sizetype, fold_convert (sizetype, t2),
975 wide_int_to_tree (sizetype, c3));
976 *pindex = c1 + c2 * c3 + c4 + c5 * c3;
977 *ptype = type;
979 return true;
982 /* Given GS which contains a data reference, create a CAND_REF entry in
983 the candidate table and attempt to find a basis. */
985 static void
986 slsr_process_ref (gimple gs)
988 tree ref_expr, base, offset, type;
989 HOST_WIDE_INT bitsize, bitpos;
990 enum machine_mode mode;
991 int unsignedp, volatilep;
992 slsr_cand_t c;
994 if (gimple_vdef (gs))
995 ref_expr = gimple_assign_lhs (gs);
996 else
997 ref_expr = gimple_assign_rhs1 (gs);
999 if (!handled_component_p (ref_expr)
1000 || TREE_CODE (ref_expr) == BIT_FIELD_REF
1001 || (TREE_CODE (ref_expr) == COMPONENT_REF
1002 && DECL_BIT_FIELD (TREE_OPERAND (ref_expr, 1))))
1003 return;
1005 base = get_inner_reference (ref_expr, &bitsize, &bitpos, &offset, &mode,
1006 &unsignedp, &volatilep, false);
1007 widest_int index = bitpos;
1009 if (!restructure_reference (&base, &offset, &index, &type))
1010 return;
1012 c = alloc_cand_and_find_basis (CAND_REF, gs, base, index, offset,
1013 type, 0);
1015 /* Add the candidate to the statement-candidate mapping. */
1016 add_cand_for_stmt (gs, c);
1019 /* Create a candidate entry for a statement GS, where GS multiplies
1020 two SSA names BASE_IN and STRIDE_IN. Propagate any known information
1021 about the two SSA names into the new candidate. Return the new
1022 candidate. */
1024 static slsr_cand_t
1025 create_mul_ssa_cand (gimple gs, tree base_in, tree stride_in, bool speed)
1027 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1028 widest_int index;
1029 unsigned savings = 0;
1030 slsr_cand_t c;
1031 slsr_cand_t base_cand = base_cand_from_table (base_in);
1033 /* Look at all interpretations of the base candidate, if necessary,
1034 to find information to propagate into this candidate. */
1035 while (base_cand && !base && base_cand->kind != CAND_PHI)
1038 if (base_cand->kind == CAND_MULT && integer_onep (base_cand->stride))
1040 /* Y = (B + i') * 1
1041 X = Y * Z
1042 ================
1043 X = (B + i') * Z */
1044 base = base_cand->base_expr;
1045 index = base_cand->index;
1046 stride = stride_in;
1047 ctype = base_cand->cand_type;
1048 if (has_single_use (base_in))
1049 savings = (base_cand->dead_savings
1050 + stmt_cost (base_cand->cand_stmt, speed));
1052 else if (base_cand->kind == CAND_ADD
1053 && TREE_CODE (base_cand->stride) == INTEGER_CST)
1055 /* Y = B + (i' * S), S constant
1056 X = Y * Z
1057 ============================
1058 X = B + ((i' * S) * Z) */
1059 base = base_cand->base_expr;
1060 index = base_cand->index * wi::to_widest (base_cand->stride);
1061 stride = stride_in;
1062 ctype = base_cand->cand_type;
1063 if (has_single_use (base_in))
1064 savings = (base_cand->dead_savings
1065 + stmt_cost (base_cand->cand_stmt, speed));
1068 if (base_cand->next_interp)
1069 base_cand = lookup_cand (base_cand->next_interp);
1070 else
1071 base_cand = NULL;
1074 if (!base)
1076 /* No interpretations had anything useful to propagate, so
1077 produce X = (Y + 0) * Z. */
1078 base = base_in;
1079 index = 0;
1080 stride = stride_in;
1081 ctype = TREE_TYPE (base_in);
1084 c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
1085 ctype, savings);
1086 return c;
1089 /* Create a candidate entry for a statement GS, where GS multiplies
1090 SSA name BASE_IN by constant STRIDE_IN. Propagate any known
1091 information about BASE_IN into the new candidate. Return the new
1092 candidate. */
1094 static slsr_cand_t
1095 create_mul_imm_cand (gimple gs, tree base_in, tree stride_in, bool speed)
1097 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1098 widest_int index, temp;
1099 unsigned savings = 0;
1100 slsr_cand_t c;
1101 slsr_cand_t base_cand = base_cand_from_table (base_in);
1103 /* Look at all interpretations of the base candidate, if necessary,
1104 to find information to propagate into this candidate. */
1105 while (base_cand && !base && base_cand->kind != CAND_PHI)
1107 if (base_cand->kind == CAND_MULT
1108 && TREE_CODE (base_cand->stride) == INTEGER_CST)
1110 /* Y = (B + i') * S, S constant
1111 X = Y * c
1112 ============================
1113 X = (B + i') * (S * c) */
1114 temp = wi::to_widest (base_cand->stride) * wi::to_widest (stride_in);
1115 if (wi::fits_to_tree_p (temp, TREE_TYPE (stride_in)))
1117 base = base_cand->base_expr;
1118 index = base_cand->index;
1119 stride = wide_int_to_tree (TREE_TYPE (stride_in), temp);
1120 ctype = base_cand->cand_type;
1121 if (has_single_use (base_in))
1122 savings = (base_cand->dead_savings
1123 + stmt_cost (base_cand->cand_stmt, speed));
1126 else if (base_cand->kind == CAND_ADD && integer_onep (base_cand->stride))
1128 /* Y = B + (i' * 1)
1129 X = Y * c
1130 ===========================
1131 X = (B + i') * c */
1132 base = base_cand->base_expr;
1133 index = base_cand->index;
1134 stride = stride_in;
1135 ctype = base_cand->cand_type;
1136 if (has_single_use (base_in))
1137 savings = (base_cand->dead_savings
1138 + stmt_cost (base_cand->cand_stmt, speed));
1140 else if (base_cand->kind == CAND_ADD
1141 && base_cand->index == 1
1142 && TREE_CODE (base_cand->stride) == INTEGER_CST)
1144 /* Y = B + (1 * S), S constant
1145 X = Y * c
1146 ===========================
1147 X = (B + S) * c */
1148 base = base_cand->base_expr;
1149 index = wi::to_widest (base_cand->stride);
1150 stride = stride_in;
1151 ctype = base_cand->cand_type;
1152 if (has_single_use (base_in))
1153 savings = (base_cand->dead_savings
1154 + stmt_cost (base_cand->cand_stmt, speed));
1157 if (base_cand->next_interp)
1158 base_cand = lookup_cand (base_cand->next_interp);
1159 else
1160 base_cand = NULL;
1163 if (!base)
1165 /* No interpretations had anything useful to propagate, so
1166 produce X = (Y + 0) * c. */
1167 base = base_in;
1168 index = 0;
1169 stride = stride_in;
1170 ctype = TREE_TYPE (base_in);
1173 c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
1174 ctype, savings);
1175 return c;
1178 /* Given GS which is a multiply of scalar integers, make an appropriate
1179 entry in the candidate table. If this is a multiply of two SSA names,
1180 create two CAND_MULT interpretations and attempt to find a basis for
1181 each of them. Otherwise, create a single CAND_MULT and attempt to
1182 find a basis. */
1184 static void
1185 slsr_process_mul (gimple gs, tree rhs1, tree rhs2, bool speed)
1187 slsr_cand_t c, c2;
1189 /* If this is a multiply of an SSA name with itself, it is highly
1190 unlikely that we will get a strength reduction opportunity, so
1191 don't record it as a candidate. This simplifies the logic for
1192 finding a basis, so if this is removed that must be considered. */
1193 if (rhs1 == rhs2)
1194 return;
1196 if (TREE_CODE (rhs2) == SSA_NAME)
1198 /* Record an interpretation of this statement in the candidate table
1199 assuming RHS1 is the base expression and RHS2 is the stride. */
1200 c = create_mul_ssa_cand (gs, rhs1, rhs2, speed);
1202 /* Add the first interpretation to the statement-candidate mapping. */
1203 add_cand_for_stmt (gs, c);
1205 /* Record another interpretation of this statement assuming RHS1
1206 is the stride and RHS2 is the base expression. */
1207 c2 = create_mul_ssa_cand (gs, rhs2, rhs1, speed);
1208 c->next_interp = c2->cand_num;
1210 else
1212 /* Record an interpretation for the multiply-immediate. */
1213 c = create_mul_imm_cand (gs, rhs1, rhs2, speed);
1215 /* Add the interpretation to the statement-candidate mapping. */
1216 add_cand_for_stmt (gs, c);
1220 /* Create a candidate entry for a statement GS, where GS adds two
1221 SSA names BASE_IN and ADDEND_IN if SUBTRACT_P is false, and
1222 subtracts ADDEND_IN from BASE_IN otherwise. Propagate any known
1223 information about the two SSA names into the new candidate.
1224 Return the new candidate. */
1226 static slsr_cand_t
1227 create_add_ssa_cand (gimple gs, tree base_in, tree addend_in,
1228 bool subtract_p, bool speed)
1230 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL;
1231 widest_int index;
1232 unsigned savings = 0;
1233 slsr_cand_t c;
1234 slsr_cand_t base_cand = base_cand_from_table (base_in);
1235 slsr_cand_t addend_cand = base_cand_from_table (addend_in);
1237 /* The most useful transformation is a multiply-immediate feeding
1238 an add or subtract. Look for that first. */
1239 while (addend_cand && !base && addend_cand->kind != CAND_PHI)
1241 if (addend_cand->kind == CAND_MULT
1242 && addend_cand->index == 0
1243 && TREE_CODE (addend_cand->stride) == INTEGER_CST)
1245 /* Z = (B + 0) * S, S constant
1246 X = Y +/- Z
1247 ===========================
1248 X = Y + ((+/-1 * S) * B) */
1249 base = base_in;
1250 index = wi::to_widest (addend_cand->stride);
1251 if (subtract_p)
1252 index = -index;
1253 stride = addend_cand->base_expr;
1254 ctype = TREE_TYPE (base_in);
1255 if (has_single_use (addend_in))
1256 savings = (addend_cand->dead_savings
1257 + stmt_cost (addend_cand->cand_stmt, speed));
1260 if (addend_cand->next_interp)
1261 addend_cand = lookup_cand (addend_cand->next_interp);
1262 else
1263 addend_cand = NULL;
1266 while (base_cand && !base && base_cand->kind != CAND_PHI)
1268 if (base_cand->kind == CAND_ADD
1269 && (base_cand->index == 0
1270 || operand_equal_p (base_cand->stride,
1271 integer_zero_node, 0)))
1273 /* Y = B + (i' * S), i' * S = 0
1274 X = Y +/- Z
1275 ============================
1276 X = B + (+/-1 * Z) */
1277 base = base_cand->base_expr;
1278 index = subtract_p ? -1 : 1;
1279 stride = addend_in;
1280 ctype = base_cand->cand_type;
1281 if (has_single_use (base_in))
1282 savings = (base_cand->dead_savings
1283 + stmt_cost (base_cand->cand_stmt, speed));
1285 else if (subtract_p)
1287 slsr_cand_t subtrahend_cand = base_cand_from_table (addend_in);
1289 while (subtrahend_cand && !base && subtrahend_cand->kind != CAND_PHI)
1291 if (subtrahend_cand->kind == CAND_MULT
1292 && subtrahend_cand->index == 0
1293 && TREE_CODE (subtrahend_cand->stride) == INTEGER_CST)
1295 /* Z = (B + 0) * S, S constant
1296 X = Y - Z
1297 ===========================
1298 Value: X = Y + ((-1 * S) * B) */
1299 base = base_in;
1300 index = wi::to_widest (subtrahend_cand->stride);
1301 index = -index;
1302 stride = subtrahend_cand->base_expr;
1303 ctype = TREE_TYPE (base_in);
1304 if (has_single_use (addend_in))
1305 savings = (subtrahend_cand->dead_savings
1306 + stmt_cost (subtrahend_cand->cand_stmt, speed));
1309 if (subtrahend_cand->next_interp)
1310 subtrahend_cand = lookup_cand (subtrahend_cand->next_interp);
1311 else
1312 subtrahend_cand = NULL;
1316 if (base_cand->next_interp)
1317 base_cand = lookup_cand (base_cand->next_interp);
1318 else
1319 base_cand = NULL;
1322 if (!base)
1324 /* No interpretations had anything useful to propagate, so
1325 produce X = Y + (1 * Z). */
1326 base = base_in;
1327 index = subtract_p ? -1 : 1;
1328 stride = addend_in;
1329 ctype = TREE_TYPE (base_in);
1332 c = alloc_cand_and_find_basis (CAND_ADD, gs, base, index, stride,
1333 ctype, savings);
1334 return c;
1337 /* Create a candidate entry for a statement GS, where GS adds SSA
1338 name BASE_IN to constant INDEX_IN. Propagate any known information
1339 about BASE_IN into the new candidate. Return the new candidate. */
1341 static slsr_cand_t
1342 create_add_imm_cand (gimple gs, tree base_in, const widest_int &index_in,
1343 bool speed)
1345 enum cand_kind kind = CAND_ADD;
1346 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1347 widest_int index, multiple;
1348 unsigned savings = 0;
1349 slsr_cand_t c;
1350 slsr_cand_t base_cand = base_cand_from_table (base_in);
1352 while (base_cand && !base && base_cand->kind != CAND_PHI)
1354 signop sign = TYPE_SIGN (TREE_TYPE (base_cand->stride));
1356 if (TREE_CODE (base_cand->stride) == INTEGER_CST
1357 && wi::multiple_of_p (index_in, wi::to_widest (base_cand->stride),
1358 sign, &multiple))
1360 /* Y = (B + i') * S, S constant, c = kS for some integer k
1361 X = Y + c
1362 ============================
1363 X = (B + (i'+ k)) * S
1365 Y = B + (i' * S), S constant, c = kS for some integer k
1366 X = Y + c
1367 ============================
1368 X = (B + (i'+ k)) * S */
1369 kind = base_cand->kind;
1370 base = base_cand->base_expr;
1371 index = base_cand->index + multiple;
1372 stride = base_cand->stride;
1373 ctype = base_cand->cand_type;
1374 if (has_single_use (base_in))
1375 savings = (base_cand->dead_savings
1376 + stmt_cost (base_cand->cand_stmt, speed));
1379 if (base_cand->next_interp)
1380 base_cand = lookup_cand (base_cand->next_interp);
1381 else
1382 base_cand = NULL;
1385 if (!base)
1387 /* No interpretations had anything useful to propagate, so
1388 produce X = Y + (c * 1). */
1389 kind = CAND_ADD;
1390 base = base_in;
1391 index = index_in;
1392 stride = integer_one_node;
1393 ctype = TREE_TYPE (base_in);
1396 c = alloc_cand_and_find_basis (kind, gs, base, index, stride,
1397 ctype, savings);
1398 return c;
1401 /* Given GS which is an add or subtract of scalar integers or pointers,
1402 make at least one appropriate entry in the candidate table. */
1404 static void
1405 slsr_process_add (gimple gs, tree rhs1, tree rhs2, bool speed)
1407 bool subtract_p = gimple_assign_rhs_code (gs) == MINUS_EXPR;
1408 slsr_cand_t c = NULL, c2;
1410 if (TREE_CODE (rhs2) == SSA_NAME)
1412 /* First record an interpretation assuming RHS1 is the base expression
1413 and RHS2 is the stride. But it doesn't make sense for the
1414 stride to be a pointer, so don't record a candidate in that case. */
1415 if (!POINTER_TYPE_P (TREE_TYPE (rhs2)))
1417 c = create_add_ssa_cand (gs, rhs1, rhs2, subtract_p, speed);
1419 /* Add the first interpretation to the statement-candidate
1420 mapping. */
1421 add_cand_for_stmt (gs, c);
1424 /* If the two RHS operands are identical, or this is a subtract,
1425 we're done. */
1426 if (operand_equal_p (rhs1, rhs2, 0) || subtract_p)
1427 return;
1429 /* Otherwise, record another interpretation assuming RHS2 is the
1430 base expression and RHS1 is the stride, again provided that the
1431 stride is not a pointer. */
1432 if (!POINTER_TYPE_P (TREE_TYPE (rhs1)))
1434 c2 = create_add_ssa_cand (gs, rhs2, rhs1, false, speed);
1435 if (c)
1436 c->next_interp = c2->cand_num;
1437 else
1438 add_cand_for_stmt (gs, c2);
1441 else
1443 /* Record an interpretation for the add-immediate. */
1444 widest_int index = wi::to_widest (rhs2);
1445 if (subtract_p)
1446 index = -index;
1448 c = create_add_imm_cand (gs, rhs1, index, speed);
1450 /* Add the interpretation to the statement-candidate mapping. */
1451 add_cand_for_stmt (gs, c);
1455 /* Given GS which is a negate of a scalar integer, make an appropriate
1456 entry in the candidate table. A negate is equivalent to a multiply
1457 by -1. */
1459 static void
1460 slsr_process_neg (gimple gs, tree rhs1, bool speed)
1462 /* Record a CAND_MULT interpretation for the multiply by -1. */
1463 slsr_cand_t c = create_mul_imm_cand (gs, rhs1, integer_minus_one_node, speed);
1465 /* Add the interpretation to the statement-candidate mapping. */
1466 add_cand_for_stmt (gs, c);
1469 /* Help function for legal_cast_p, operating on two trees. Checks
1470 whether it's allowable to cast from RHS to LHS. See legal_cast_p
1471 for more details. */
1473 static bool
1474 legal_cast_p_1 (tree lhs, tree rhs)
1476 tree lhs_type, rhs_type;
1477 unsigned lhs_size, rhs_size;
1478 bool lhs_wraps, rhs_wraps;
1480 lhs_type = TREE_TYPE (lhs);
1481 rhs_type = TREE_TYPE (rhs);
1482 lhs_size = TYPE_PRECISION (lhs_type);
1483 rhs_size = TYPE_PRECISION (rhs_type);
1484 lhs_wraps = TYPE_OVERFLOW_WRAPS (lhs_type);
1485 rhs_wraps = TYPE_OVERFLOW_WRAPS (rhs_type);
1487 if (lhs_size < rhs_size
1488 || (rhs_wraps && !lhs_wraps)
1489 || (rhs_wraps && lhs_wraps && rhs_size != lhs_size))
1490 return false;
1492 return true;
1495 /* Return TRUE if GS is a statement that defines an SSA name from
1496 a conversion and is legal for us to combine with an add and multiply
1497 in the candidate table. For example, suppose we have:
1499 A = B + i;
1500 C = (type) A;
1501 D = C * S;
1503 Without the type-cast, we would create a CAND_MULT for D with base B,
1504 index i, and stride S. We want to record this candidate only if it
1505 is equivalent to apply the type cast following the multiply:
1507 A = B + i;
1508 E = A * S;
1509 D = (type) E;
1511 We will record the type with the candidate for D. This allows us
1512 to use a similar previous candidate as a basis. If we have earlier seen
1514 A' = B + i';
1515 C' = (type) A';
1516 D' = C' * S;
1518 we can replace D with
1520 D = D' + (i - i') * S;
1522 But if moving the type-cast would change semantics, we mustn't do this.
1524 This is legitimate for casts from a non-wrapping integral type to
1525 any integral type of the same or larger size. It is not legitimate
1526 to convert a wrapping type to a non-wrapping type, or to a wrapping
1527 type of a different size. I.e., with a wrapping type, we must
1528 assume that the addition B + i could wrap, in which case performing
1529 the multiply before or after one of the "illegal" type casts will
1530 have different semantics. */
1532 static bool
1533 legal_cast_p (gimple gs, tree rhs)
1535 if (!is_gimple_assign (gs)
1536 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs)))
1537 return false;
1539 return legal_cast_p_1 (gimple_assign_lhs (gs), rhs);
1542 /* Given GS which is a cast to a scalar integer type, determine whether
1543 the cast is legal for strength reduction. If so, make at least one
1544 appropriate entry in the candidate table. */
1546 static void
1547 slsr_process_cast (gimple gs, tree rhs1, bool speed)
1549 tree lhs, ctype;
1550 slsr_cand_t base_cand, c, c2;
1551 unsigned savings = 0;
1553 if (!legal_cast_p (gs, rhs1))
1554 return;
1556 lhs = gimple_assign_lhs (gs);
1557 base_cand = base_cand_from_table (rhs1);
1558 ctype = TREE_TYPE (lhs);
1560 if (base_cand && base_cand->kind != CAND_PHI)
1562 while (base_cand)
1564 /* Propagate all data from the base candidate except the type,
1565 which comes from the cast, and the base candidate's cast,
1566 which is no longer applicable. */
1567 if (has_single_use (rhs1))
1568 savings = (base_cand->dead_savings
1569 + stmt_cost (base_cand->cand_stmt, speed));
1571 c = alloc_cand_and_find_basis (base_cand->kind, gs,
1572 base_cand->base_expr,
1573 base_cand->index, base_cand->stride,
1574 ctype, savings);
1575 if (base_cand->next_interp)
1576 base_cand = lookup_cand (base_cand->next_interp);
1577 else
1578 base_cand = NULL;
1581 else
1583 /* If nothing is known about the RHS, create fresh CAND_ADD and
1584 CAND_MULT interpretations:
1586 X = Y + (0 * 1)
1587 X = (Y + 0) * 1
1589 The first of these is somewhat arbitrary, but the choice of
1590 1 for the stride simplifies the logic for propagating casts
1591 into their uses. */
1592 c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1,
1593 0, integer_one_node, ctype, 0);
1594 c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1,
1595 0, integer_one_node, ctype, 0);
1596 c->next_interp = c2->cand_num;
1599 /* Add the first (or only) interpretation to the statement-candidate
1600 mapping. */
1601 add_cand_for_stmt (gs, c);
1604 /* Given GS which is a copy of a scalar integer type, make at least one
1605 appropriate entry in the candidate table.
1607 This interface is included for completeness, but is unnecessary
1608 if this pass immediately follows a pass that performs copy
1609 propagation, such as DOM. */
1611 static void
1612 slsr_process_copy (gimple gs, tree rhs1, bool speed)
1614 slsr_cand_t base_cand, c, c2;
1615 unsigned savings = 0;
1617 base_cand = base_cand_from_table (rhs1);
1619 if (base_cand && base_cand->kind != CAND_PHI)
1621 while (base_cand)
1623 /* Propagate all data from the base candidate. */
1624 if (has_single_use (rhs1))
1625 savings = (base_cand->dead_savings
1626 + stmt_cost (base_cand->cand_stmt, speed));
1628 c = alloc_cand_and_find_basis (base_cand->kind, gs,
1629 base_cand->base_expr,
1630 base_cand->index, base_cand->stride,
1631 base_cand->cand_type, savings);
1632 if (base_cand->next_interp)
1633 base_cand = lookup_cand (base_cand->next_interp);
1634 else
1635 base_cand = NULL;
1638 else
1640 /* If nothing is known about the RHS, create fresh CAND_ADD and
1641 CAND_MULT interpretations:
1643 X = Y + (0 * 1)
1644 X = (Y + 0) * 1
1646 The first of these is somewhat arbitrary, but the choice of
1647 1 for the stride simplifies the logic for propagating casts
1648 into their uses. */
1649 c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1,
1650 0, integer_one_node, TREE_TYPE (rhs1), 0);
1651 c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1,
1652 0, integer_one_node, TREE_TYPE (rhs1), 0);
1653 c->next_interp = c2->cand_num;
1656 /* Add the first (or only) interpretation to the statement-candidate
1657 mapping. */
1658 add_cand_for_stmt (gs, c);
1661 class find_candidates_dom_walker : public dom_walker
1663 public:
1664 find_candidates_dom_walker (cdi_direction direction)
1665 : dom_walker (direction) {}
1666 virtual void before_dom_children (basic_block);
1669 /* Find strength-reduction candidates in block BB. */
1671 void
1672 find_candidates_dom_walker::before_dom_children (basic_block bb)
1674 bool speed = optimize_bb_for_speed_p (bb);
1675 gimple_stmt_iterator gsi;
1677 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1678 slsr_process_phi (gsi_stmt (gsi), speed);
1680 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1682 gimple gs = gsi_stmt (gsi);
1684 if (gimple_vuse (gs) && gimple_assign_single_p (gs))
1685 slsr_process_ref (gs);
1687 else if (is_gimple_assign (gs)
1688 && SCALAR_INT_MODE_P
1689 (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))))
1691 tree rhs1 = NULL_TREE, rhs2 = NULL_TREE;
1693 switch (gimple_assign_rhs_code (gs))
1695 case MULT_EXPR:
1696 case PLUS_EXPR:
1697 rhs1 = gimple_assign_rhs1 (gs);
1698 rhs2 = gimple_assign_rhs2 (gs);
1699 /* Should never happen, but currently some buggy situations
1700 in earlier phases put constants in rhs1. */
1701 if (TREE_CODE (rhs1) != SSA_NAME)
1702 continue;
1703 break;
1705 /* Possible future opportunity: rhs1 of a ptr+ can be
1706 an ADDR_EXPR. */
1707 case POINTER_PLUS_EXPR:
1708 case MINUS_EXPR:
1709 rhs2 = gimple_assign_rhs2 (gs);
1710 /* Fall-through. */
1712 case NOP_EXPR:
1713 case MODIFY_EXPR:
1714 case NEGATE_EXPR:
1715 rhs1 = gimple_assign_rhs1 (gs);
1716 if (TREE_CODE (rhs1) != SSA_NAME)
1717 continue;
1718 break;
1720 default:
1724 switch (gimple_assign_rhs_code (gs))
1726 case MULT_EXPR:
1727 slsr_process_mul (gs, rhs1, rhs2, speed);
1728 break;
1730 case PLUS_EXPR:
1731 case POINTER_PLUS_EXPR:
1732 case MINUS_EXPR:
1733 slsr_process_add (gs, rhs1, rhs2, speed);
1734 break;
1736 case NEGATE_EXPR:
1737 slsr_process_neg (gs, rhs1, speed);
1738 break;
1740 case NOP_EXPR:
1741 slsr_process_cast (gs, rhs1, speed);
1742 break;
1744 case MODIFY_EXPR:
1745 slsr_process_copy (gs, rhs1, speed);
1746 break;
1748 default:
1755 /* Dump a candidate for debug. */
1757 static void
1758 dump_candidate (slsr_cand_t c)
1760 fprintf (dump_file, "%3d [%d] ", c->cand_num,
1761 gimple_bb (c->cand_stmt)->index);
1762 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
1763 switch (c->kind)
1765 case CAND_MULT:
1766 fputs (" MULT : (", dump_file);
1767 print_generic_expr (dump_file, c->base_expr, 0);
1768 fputs (" + ", dump_file);
1769 print_decs (c->index, dump_file);
1770 fputs (") * ", dump_file);
1771 print_generic_expr (dump_file, c->stride, 0);
1772 fputs (" : ", dump_file);
1773 break;
1774 case CAND_ADD:
1775 fputs (" ADD : ", dump_file);
1776 print_generic_expr (dump_file, c->base_expr, 0);
1777 fputs (" + (", dump_file);
1778 print_decs (c->index, dump_file);
1779 fputs (" * ", dump_file);
1780 print_generic_expr (dump_file, c->stride, 0);
1781 fputs (") : ", dump_file);
1782 break;
1783 case CAND_REF:
1784 fputs (" REF : ", dump_file);
1785 print_generic_expr (dump_file, c->base_expr, 0);
1786 fputs (" + (", dump_file);
1787 print_generic_expr (dump_file, c->stride, 0);
1788 fputs (") + ", dump_file);
1789 print_decs (c->index, dump_file);
1790 fputs (" : ", dump_file);
1791 break;
1792 case CAND_PHI:
1793 fputs (" PHI : ", dump_file);
1794 print_generic_expr (dump_file, c->base_expr, 0);
1795 fputs (" + (unknown * ", dump_file);
1796 print_generic_expr (dump_file, c->stride, 0);
1797 fputs (") : ", dump_file);
1798 break;
1799 default:
1800 gcc_unreachable ();
1802 print_generic_expr (dump_file, c->cand_type, 0);
1803 fprintf (dump_file, "\n basis: %d dependent: %d sibling: %d\n",
1804 c->basis, c->dependent, c->sibling);
1805 fprintf (dump_file, " next-interp: %d dead-savings: %d\n",
1806 c->next_interp, c->dead_savings);
1807 if (c->def_phi)
1808 fprintf (dump_file, " phi: %d\n", c->def_phi);
1809 fputs ("\n", dump_file);
1812 /* Dump the candidate vector for debug. */
1814 static void
1815 dump_cand_vec (void)
1817 unsigned i;
1818 slsr_cand_t c;
1820 fprintf (dump_file, "\nStrength reduction candidate vector:\n\n");
1822 FOR_EACH_VEC_ELT (cand_vec, i, c)
1823 dump_candidate (c);
1826 /* Callback used to dump the candidate chains hash table. */
1829 ssa_base_cand_dump_callback (cand_chain **slot, void *ignored ATTRIBUTE_UNUSED)
1831 const_cand_chain_t chain = *slot;
1832 cand_chain_t p;
1834 print_generic_expr (dump_file, chain->base_expr, 0);
1835 fprintf (dump_file, " -> %d", chain->cand->cand_num);
1837 for (p = chain->next; p; p = p->next)
1838 fprintf (dump_file, " -> %d", p->cand->cand_num);
1840 fputs ("\n", dump_file);
1841 return 1;
1844 /* Dump the candidate chains. */
1846 static void
1847 dump_cand_chains (void)
1849 fprintf (dump_file, "\nStrength reduction candidate chains:\n\n");
1850 base_cand_map.traverse_noresize <void *, ssa_base_cand_dump_callback> (NULL);
1851 fputs ("\n", dump_file);
1854 /* Dump the increment vector for debug. */
1856 static void
1857 dump_incr_vec (void)
1859 if (dump_file && (dump_flags & TDF_DETAILS))
1861 unsigned i;
1863 fprintf (dump_file, "\nIncrement vector:\n\n");
1865 for (i = 0; i < incr_vec_len; i++)
1867 fprintf (dump_file, "%3d increment: ", i);
1868 print_decs (incr_vec[i].incr, dump_file);
1869 fprintf (dump_file, "\n count: %d", incr_vec[i].count);
1870 fprintf (dump_file, "\n cost: %d", incr_vec[i].cost);
1871 fputs ("\n initializer: ", dump_file);
1872 print_generic_expr (dump_file, incr_vec[i].initializer, 0);
1873 fputs ("\n\n", dump_file);
1878 /* Replace *EXPR in candidate C with an equivalent strength-reduced
1879 data reference. */
1881 static void
1882 replace_ref (tree *expr, slsr_cand_t c)
1884 tree add_expr, mem_ref, acc_type = TREE_TYPE (*expr);
1885 unsigned HOST_WIDE_INT misalign;
1886 unsigned align;
1888 /* Ensure the memory reference carries the minimum alignment
1889 requirement for the data type. See PR58041. */
1890 get_object_alignment_1 (*expr, &align, &misalign);
1891 if (misalign != 0)
1892 align = (misalign & -misalign);
1893 if (align < TYPE_ALIGN (acc_type))
1894 acc_type = build_aligned_type (acc_type, align);
1896 add_expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (c->base_expr),
1897 c->base_expr, c->stride);
1898 mem_ref = fold_build2 (MEM_REF, acc_type, add_expr,
1899 wide_int_to_tree (c->cand_type, c->index));
1901 /* Gimplify the base addressing expression for the new MEM_REF tree. */
1902 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
1903 TREE_OPERAND (mem_ref, 0)
1904 = force_gimple_operand_gsi (&gsi, TREE_OPERAND (mem_ref, 0),
1905 /*simple_p=*/true, NULL,
1906 /*before=*/true, GSI_SAME_STMT);
1907 copy_ref_info (mem_ref, *expr);
1908 *expr = mem_ref;
1909 update_stmt (c->cand_stmt);
1912 /* Replace CAND_REF candidate C, each sibling of candidate C, and each
1913 dependent of candidate C with an equivalent strength-reduced data
1914 reference. */
1916 static void
1917 replace_refs (slsr_cand_t c)
1919 if (dump_file && (dump_flags & TDF_DETAILS))
1921 fputs ("Replacing reference: ", dump_file);
1922 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
1925 if (gimple_vdef (c->cand_stmt))
1927 tree *lhs = gimple_assign_lhs_ptr (c->cand_stmt);
1928 replace_ref (lhs, c);
1930 else
1932 tree *rhs = gimple_assign_rhs1_ptr (c->cand_stmt);
1933 replace_ref (rhs, c);
1936 if (dump_file && (dump_flags & TDF_DETAILS))
1938 fputs ("With: ", dump_file);
1939 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
1940 fputs ("\n", dump_file);
1943 if (c->sibling)
1944 replace_refs (lookup_cand (c->sibling));
1946 if (c->dependent)
1947 replace_refs (lookup_cand (c->dependent));
1950 /* Return TRUE if candidate C is dependent upon a PHI. */
1952 static bool
1953 phi_dependent_cand_p (slsr_cand_t c)
1955 /* A candidate is not necessarily dependent upon a PHI just because
1956 it has a phi definition for its base name. It may have a basis
1957 that relies upon the same phi definition, in which case the PHI
1958 is irrelevant to this candidate. */
1959 return (c->def_phi
1960 && c->basis
1961 && lookup_cand (c->basis)->def_phi != c->def_phi);
1964 /* Calculate the increment required for candidate C relative to
1965 its basis. */
1967 static widest_int
1968 cand_increment (slsr_cand_t c)
1970 slsr_cand_t basis;
1972 /* If the candidate doesn't have a basis, just return its own
1973 index. This is useful in record_increments to help us find
1974 an existing initializer. Also, if the candidate's basis is
1975 hidden by a phi, then its own index will be the increment
1976 from the newly introduced phi basis. */
1977 if (!c->basis || phi_dependent_cand_p (c))
1978 return c->index;
1980 basis = lookup_cand (c->basis);
1981 gcc_assert (operand_equal_p (c->base_expr, basis->base_expr, 0));
1982 return c->index - basis->index;
1985 /* Calculate the increment required for candidate C relative to
1986 its basis. If we aren't going to generate pointer arithmetic
1987 for this candidate, return the absolute value of that increment
1988 instead. */
1990 static inline widest_int
1991 cand_abs_increment (slsr_cand_t c)
1993 widest_int increment = cand_increment (c);
1995 if (!address_arithmetic_p && wi::neg_p (increment))
1996 increment = -increment;
1998 return increment;
2001 /* Return TRUE iff candidate C has already been replaced under
2002 another interpretation. */
2004 static inline bool
2005 cand_already_replaced (slsr_cand_t c)
2007 return (gimple_bb (c->cand_stmt) == 0);
2010 /* Common logic used by replace_unconditional_candidate and
2011 replace_conditional_candidate. */
2013 static void
2014 replace_mult_candidate (slsr_cand_t c, tree basis_name, widest_int bump)
2016 tree target_type = TREE_TYPE (gimple_assign_lhs (c->cand_stmt));
2017 enum tree_code cand_code = gimple_assign_rhs_code (c->cand_stmt);
2019 /* It is highly unlikely, but possible, that the resulting
2020 bump doesn't fit in a HWI. Abandon the replacement
2021 in this case. This does not affect siblings or dependents
2022 of C. Restriction to signed HWI is conservative for unsigned
2023 types but allows for safe negation without twisted logic. */
2024 if (wi::fits_shwi_p (bump)
2025 && bump.to_shwi () != HOST_WIDE_INT_MIN
2026 /* It is not useful to replace casts, copies, or adds of
2027 an SSA name and a constant. */
2028 && cand_code != MODIFY_EXPR
2029 && cand_code != NOP_EXPR
2030 && cand_code != PLUS_EXPR
2031 && cand_code != POINTER_PLUS_EXPR
2032 && cand_code != MINUS_EXPR)
2034 enum tree_code code = PLUS_EXPR;
2035 tree bump_tree;
2036 gimple stmt_to_print = NULL;
2038 /* If the basis name and the candidate's LHS have incompatible
2039 types, introduce a cast. */
2040 if (!useless_type_conversion_p (target_type, TREE_TYPE (basis_name)))
2041 basis_name = introduce_cast_before_cand (c, target_type, basis_name);
2042 if (wi::neg_p (bump))
2044 code = MINUS_EXPR;
2045 bump = -bump;
2048 bump_tree = wide_int_to_tree (target_type, bump);
2050 if (dump_file && (dump_flags & TDF_DETAILS))
2052 fputs ("Replacing: ", dump_file);
2053 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
2056 if (bump == 0)
2058 tree lhs = gimple_assign_lhs (c->cand_stmt);
2059 gimple copy_stmt = gimple_build_assign (lhs, basis_name);
2060 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
2061 gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
2062 gsi_replace (&gsi, copy_stmt, false);
2063 c->cand_stmt = copy_stmt;
2064 if (dump_file && (dump_flags & TDF_DETAILS))
2065 stmt_to_print = copy_stmt;
2067 else
2069 tree rhs1, rhs2;
2070 if (cand_code != NEGATE_EXPR) {
2071 rhs1 = gimple_assign_rhs1 (c->cand_stmt);
2072 rhs2 = gimple_assign_rhs2 (c->cand_stmt);
2074 if (cand_code != NEGATE_EXPR
2075 && ((operand_equal_p (rhs1, basis_name, 0)
2076 && operand_equal_p (rhs2, bump_tree, 0))
2077 || (operand_equal_p (rhs1, bump_tree, 0)
2078 && operand_equal_p (rhs2, basis_name, 0))))
2080 if (dump_file && (dump_flags & TDF_DETAILS))
2082 fputs ("(duplicate, not actually replacing)", dump_file);
2083 stmt_to_print = c->cand_stmt;
2086 else
2088 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
2089 gimple_assign_set_rhs_with_ops (&gsi, code,
2090 basis_name, bump_tree);
2091 update_stmt (gsi_stmt (gsi));
2092 c->cand_stmt = gsi_stmt (gsi);
2093 if (dump_file && (dump_flags & TDF_DETAILS))
2094 stmt_to_print = gsi_stmt (gsi);
2098 if (dump_file && (dump_flags & TDF_DETAILS))
2100 fputs ("With: ", dump_file);
2101 print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
2102 fputs ("\n", dump_file);
2107 /* Replace candidate C with an add or subtract. Note that we only
2108 operate on CAND_MULTs with known strides, so we will never generate
2109 a POINTER_PLUS_EXPR. Each candidate X = (B + i) * S is replaced by
2110 X = Y + ((i - i') * S), as described in the module commentary. The
2111 folded value ((i - i') * S) is referred to here as the "bump." */
2113 static void
2114 replace_unconditional_candidate (slsr_cand_t c)
2116 slsr_cand_t basis;
2118 if (cand_already_replaced (c))
2119 return;
2121 basis = lookup_cand (c->basis);
2122 widest_int bump = cand_increment (c) * wi::to_widest (c->stride);
2124 replace_mult_candidate (c, gimple_assign_lhs (basis->cand_stmt), bump);
2127 /* Return the index in the increment vector of the given INCREMENT,
2128 or -1 if not found. The latter can occur if more than
2129 MAX_INCR_VEC_LEN increments have been found. */
2131 static inline int
2132 incr_vec_index (const widest_int &increment)
2134 unsigned i;
2136 for (i = 0; i < incr_vec_len && increment != incr_vec[i].incr; i++)
2139 if (i < incr_vec_len)
2140 return i;
2141 else
2142 return -1;
2145 /* Create a new statement along edge E to add BASIS_NAME to the product
2146 of INCREMENT and the stride of candidate C. Create and return a new
2147 SSA name from *VAR to be used as the LHS of the new statement.
2148 KNOWN_STRIDE is true iff C's stride is a constant. */
2150 static tree
2151 create_add_on_incoming_edge (slsr_cand_t c, tree basis_name,
2152 widest_int increment, edge e, location_t loc,
2153 bool known_stride)
2155 basic_block insert_bb;
2156 gimple_stmt_iterator gsi;
2157 tree lhs, basis_type;
2158 gimple new_stmt;
2160 /* If the add candidate along this incoming edge has the same
2161 index as C's hidden basis, the hidden basis represents this
2162 edge correctly. */
2163 if (increment == 0)
2164 return basis_name;
2166 basis_type = TREE_TYPE (basis_name);
2167 lhs = make_temp_ssa_name (basis_type, NULL, "slsr");
2169 if (known_stride)
2171 tree bump_tree;
2172 enum tree_code code = PLUS_EXPR;
2173 widest_int bump = increment * wi::to_widest (c->stride);
2174 if (wi::neg_p (bump))
2176 code = MINUS_EXPR;
2177 bump = -bump;
2180 bump_tree = wide_int_to_tree (basis_type, bump);
2181 new_stmt = gimple_build_assign_with_ops (code, lhs, basis_name,
2182 bump_tree);
2184 else
2186 int i;
2187 bool negate_incr = (!address_arithmetic_p && wi::neg_p (increment));
2188 i = incr_vec_index (negate_incr ? -increment : increment);
2189 gcc_assert (i >= 0);
2191 if (incr_vec[i].initializer)
2193 enum tree_code code = negate_incr ? MINUS_EXPR : PLUS_EXPR;
2194 new_stmt = gimple_build_assign_with_ops (code, lhs, basis_name,
2195 incr_vec[i].initializer);
2197 else if (increment == 1)
2198 new_stmt = gimple_build_assign_with_ops (PLUS_EXPR, lhs, basis_name,
2199 c->stride);
2200 else if (increment == -1)
2201 new_stmt = gimple_build_assign_with_ops (MINUS_EXPR, lhs, basis_name,
2202 c->stride);
2203 else
2204 gcc_unreachable ();
2207 insert_bb = single_succ_p (e->src) ? e->src : split_edge (e);
2208 gsi = gsi_last_bb (insert_bb);
2210 if (!gsi_end_p (gsi) && is_ctrl_stmt (gsi_stmt (gsi)))
2211 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
2212 else
2213 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
2215 gimple_set_location (new_stmt, loc);
2217 if (dump_file && (dump_flags & TDF_DETAILS))
2219 fprintf (dump_file, "Inserting in block %d: ", insert_bb->index);
2220 print_gimple_stmt (dump_file, new_stmt, 0, 0);
2223 return lhs;
2226 /* Given a candidate C with BASIS_NAME being the LHS of C's basis which
2227 is hidden by the phi node FROM_PHI, create a new phi node in the same
2228 block as FROM_PHI. The new phi is suitable for use as a basis by C,
2229 with its phi arguments representing conditional adjustments to the
2230 hidden basis along conditional incoming paths. Those adjustments are
2231 made by creating add statements (and sometimes recursively creating
2232 phis) along those incoming paths. LOC is the location to attach to
2233 the introduced statements. KNOWN_STRIDE is true iff C's stride is a
2234 constant. */
2236 static tree
2237 create_phi_basis (slsr_cand_t c, gimple from_phi, tree basis_name,
2238 location_t loc, bool known_stride)
2240 int i;
2241 tree name, phi_arg;
2242 gimple phi;
2243 vec<tree> phi_args;
2244 slsr_cand_t basis = lookup_cand (c->basis);
2245 int nargs = gimple_phi_num_args (from_phi);
2246 basic_block phi_bb = gimple_bb (from_phi);
2247 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (from_phi));
2248 phi_args.create (nargs);
2250 /* Process each argument of the existing phi that represents
2251 conditionally-executed add candidates. */
2252 for (i = 0; i < nargs; i++)
2254 edge e = (*phi_bb->preds)[i];
2255 tree arg = gimple_phi_arg_def (from_phi, i);
2256 tree feeding_def;
2258 /* If the phi argument is the base name of the CAND_PHI, then
2259 this incoming arc should use the hidden basis. */
2260 if (operand_equal_p (arg, phi_cand->base_expr, 0))
2261 if (basis->index == 0)
2262 feeding_def = gimple_assign_lhs (basis->cand_stmt);
2263 else
2265 widest_int incr = -basis->index;
2266 feeding_def = create_add_on_incoming_edge (c, basis_name, incr,
2267 e, loc, known_stride);
2269 else
2271 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2273 /* If there is another phi along this incoming edge, we must
2274 process it in the same fashion to ensure that all basis
2275 adjustments are made along its incoming edges. */
2276 if (gimple_code (arg_def) == GIMPLE_PHI)
2277 feeding_def = create_phi_basis (c, arg_def, basis_name,
2278 loc, known_stride);
2279 else
2281 slsr_cand_t arg_cand = base_cand_from_table (arg);
2282 widest_int diff = arg_cand->index - basis->index;
2283 feeding_def = create_add_on_incoming_edge (c, basis_name, diff,
2284 e, loc, known_stride);
2288 /* Because of recursion, we need to save the arguments in a vector
2289 so we can create the PHI statement all at once. Otherwise the
2290 storage for the half-created PHI can be reclaimed. */
2291 phi_args.safe_push (feeding_def);
2294 /* Create the new phi basis. */
2295 name = make_temp_ssa_name (TREE_TYPE (basis_name), NULL, "slsr");
2296 phi = create_phi_node (name, phi_bb);
2297 SSA_NAME_DEF_STMT (name) = phi;
2299 FOR_EACH_VEC_ELT (phi_args, i, phi_arg)
2301 edge e = (*phi_bb->preds)[i];
2302 add_phi_arg (phi, phi_arg, e, loc);
2305 update_stmt (phi);
2307 if (dump_file && (dump_flags & TDF_DETAILS))
2309 fputs ("Introducing new phi basis: ", dump_file);
2310 print_gimple_stmt (dump_file, phi, 0, 0);
2313 return name;
2316 /* Given a candidate C whose basis is hidden by at least one intervening
2317 phi, introduce a matching number of new phis to represent its basis
2318 adjusted by conditional increments along possible incoming paths. Then
2319 replace C as though it were an unconditional candidate, using the new
2320 basis. */
2322 static void
2323 replace_conditional_candidate (slsr_cand_t c)
2325 tree basis_name, name;
2326 slsr_cand_t basis;
2327 location_t loc;
2329 /* Look up the LHS SSA name from C's basis. This will be the
2330 RHS1 of the adds we will introduce to create new phi arguments. */
2331 basis = lookup_cand (c->basis);
2332 basis_name = gimple_assign_lhs (basis->cand_stmt);
2334 /* Create a new phi statement which will represent C's true basis
2335 after the transformation is complete. */
2336 loc = gimple_location (c->cand_stmt);
2337 name = create_phi_basis (c, lookup_cand (c->def_phi)->cand_stmt,
2338 basis_name, loc, KNOWN_STRIDE);
2339 /* Replace C with an add of the new basis phi and a constant. */
2340 widest_int bump = c->index * wi::to_widest (c->stride);
2342 replace_mult_candidate (c, name, bump);
2345 /* Compute the expected costs of inserting basis adjustments for
2346 candidate C with phi-definition PHI. The cost of inserting
2347 one adjustment is given by ONE_ADD_COST. If PHI has arguments
2348 which are themselves phi results, recursively calculate costs
2349 for those phis as well. */
2351 static int
2352 phi_add_costs (gimple phi, slsr_cand_t c, int one_add_cost)
2354 unsigned i;
2355 int cost = 0;
2356 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2358 /* If we work our way back to a phi that isn't dominated by the hidden
2359 basis, this isn't a candidate for replacement. Indicate this by
2360 returning an unreasonably high cost. It's not easy to detect
2361 these situations when determining the basis, so we defer the
2362 decision until now. */
2363 basic_block phi_bb = gimple_bb (phi);
2364 slsr_cand_t basis = lookup_cand (c->basis);
2365 basic_block basis_bb = gimple_bb (basis->cand_stmt);
2367 if (phi_bb == basis_bb || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
2368 return COST_INFINITE;
2370 for (i = 0; i < gimple_phi_num_args (phi); i++)
2372 tree arg = gimple_phi_arg_def (phi, i);
2374 if (arg != phi_cand->base_expr)
2376 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2378 if (gimple_code (arg_def) == GIMPLE_PHI)
2379 cost += phi_add_costs (arg_def, c, one_add_cost);
2380 else
2382 slsr_cand_t arg_cand = base_cand_from_table (arg);
2384 if (arg_cand->index != c->index)
2385 cost += one_add_cost;
2390 return cost;
2393 /* For candidate C, each sibling of candidate C, and each dependent of
2394 candidate C, determine whether the candidate is dependent upon a
2395 phi that hides its basis. If not, replace the candidate unconditionally.
2396 Otherwise, determine whether the cost of introducing compensation code
2397 for the candidate is offset by the gains from strength reduction. If
2398 so, replace the candidate and introduce the compensation code. */
2400 static void
2401 replace_uncond_cands_and_profitable_phis (slsr_cand_t c)
2403 if (phi_dependent_cand_p (c))
2405 if (c->kind == CAND_MULT)
2407 /* A candidate dependent upon a phi will replace a multiply by
2408 a constant with an add, and will insert at most one add for
2409 each phi argument. Add these costs with the potential dead-code
2410 savings to determine profitability. */
2411 bool speed = optimize_bb_for_speed_p (gimple_bb (c->cand_stmt));
2412 int mult_savings = stmt_cost (c->cand_stmt, speed);
2413 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
2414 tree phi_result = gimple_phi_result (phi);
2415 int one_add_cost = add_cost (speed,
2416 TYPE_MODE (TREE_TYPE (phi_result)));
2417 int add_costs = one_add_cost + phi_add_costs (phi, c, one_add_cost);
2418 int cost = add_costs - mult_savings - c->dead_savings;
2420 if (dump_file && (dump_flags & TDF_DETAILS))
2422 fprintf (dump_file, " Conditional candidate %d:\n", c->cand_num);
2423 fprintf (dump_file, " add_costs = %d\n", add_costs);
2424 fprintf (dump_file, " mult_savings = %d\n", mult_savings);
2425 fprintf (dump_file, " dead_savings = %d\n", c->dead_savings);
2426 fprintf (dump_file, " cost = %d\n", cost);
2427 if (cost <= COST_NEUTRAL)
2428 fputs (" Replacing...\n", dump_file);
2429 else
2430 fputs (" Not replaced.\n", dump_file);
2433 if (cost <= COST_NEUTRAL)
2434 replace_conditional_candidate (c);
2437 else
2438 replace_unconditional_candidate (c);
2440 if (c->sibling)
2441 replace_uncond_cands_and_profitable_phis (lookup_cand (c->sibling));
2443 if (c->dependent)
2444 replace_uncond_cands_and_profitable_phis (lookup_cand (c->dependent));
2447 /* Count the number of candidates in the tree rooted at C that have
2448 not already been replaced under other interpretations. */
2450 static int
2451 count_candidates (slsr_cand_t c)
2453 unsigned count = cand_already_replaced (c) ? 0 : 1;
2455 if (c->sibling)
2456 count += count_candidates (lookup_cand (c->sibling));
2458 if (c->dependent)
2459 count += count_candidates (lookup_cand (c->dependent));
2461 return count;
2464 /* Increase the count of INCREMENT by one in the increment vector.
2465 INCREMENT is associated with candidate C. If INCREMENT is to be
2466 conditionally executed as part of a conditional candidate replacement,
2467 IS_PHI_ADJUST is true, otherwise false. If an initializer
2468 T_0 = stride * I is provided by a candidate that dominates all
2469 candidates with the same increment, also record T_0 for subsequent use. */
2471 static void
2472 record_increment (slsr_cand_t c, widest_int increment, bool is_phi_adjust)
2474 bool found = false;
2475 unsigned i;
2477 /* Treat increments that differ only in sign as identical so as to
2478 share initializers, unless we are generating pointer arithmetic. */
2479 if (!address_arithmetic_p && wi::neg_p (increment))
2480 increment = -increment;
2482 for (i = 0; i < incr_vec_len; i++)
2484 if (incr_vec[i].incr == increment)
2486 incr_vec[i].count++;
2487 found = true;
2489 /* If we previously recorded an initializer that doesn't
2490 dominate this candidate, it's not going to be useful to
2491 us after all. */
2492 if (incr_vec[i].initializer
2493 && !dominated_by_p (CDI_DOMINATORS,
2494 gimple_bb (c->cand_stmt),
2495 incr_vec[i].init_bb))
2497 incr_vec[i].initializer = NULL_TREE;
2498 incr_vec[i].init_bb = NULL;
2501 break;
2505 if (!found && incr_vec_len < MAX_INCR_VEC_LEN - 1)
2507 /* The first time we see an increment, create the entry for it.
2508 If this is the root candidate which doesn't have a basis, set
2509 the count to zero. We're only processing it so it can possibly
2510 provide an initializer for other candidates. */
2511 incr_vec[incr_vec_len].incr = increment;
2512 incr_vec[incr_vec_len].count = c->basis || is_phi_adjust ? 1 : 0;
2513 incr_vec[incr_vec_len].cost = COST_INFINITE;
2515 /* Optimistically record the first occurrence of this increment
2516 as providing an initializer (if it does); we will revise this
2517 opinion later if it doesn't dominate all other occurrences.
2518 Exception: increments of -1, 0, 1 never need initializers;
2519 and phi adjustments don't ever provide initializers. */
2520 if (c->kind == CAND_ADD
2521 && !is_phi_adjust
2522 && c->index == increment
2523 && (wi::gts_p (increment, 1)
2524 || wi::lts_p (increment, -1))
2525 && (gimple_assign_rhs_code (c->cand_stmt) == PLUS_EXPR
2526 || gimple_assign_rhs_code (c->cand_stmt) == POINTER_PLUS_EXPR))
2528 tree t0 = NULL_TREE;
2529 tree rhs1 = gimple_assign_rhs1 (c->cand_stmt);
2530 tree rhs2 = gimple_assign_rhs2 (c->cand_stmt);
2531 if (operand_equal_p (rhs1, c->base_expr, 0))
2532 t0 = rhs2;
2533 else if (operand_equal_p (rhs2, c->base_expr, 0))
2534 t0 = rhs1;
2535 if (t0
2536 && SSA_NAME_DEF_STMT (t0)
2537 && gimple_bb (SSA_NAME_DEF_STMT (t0)))
2539 incr_vec[incr_vec_len].initializer = t0;
2540 incr_vec[incr_vec_len++].init_bb
2541 = gimple_bb (SSA_NAME_DEF_STMT (t0));
2543 else
2545 incr_vec[incr_vec_len].initializer = NULL_TREE;
2546 incr_vec[incr_vec_len++].init_bb = NULL;
2549 else
2551 incr_vec[incr_vec_len].initializer = NULL_TREE;
2552 incr_vec[incr_vec_len++].init_bb = NULL;
2557 /* Given phi statement PHI that hides a candidate from its BASIS, find
2558 the increments along each incoming arc (recursively handling additional
2559 phis that may be present) and record them. These increments are the
2560 difference in index between the index-adjusting statements and the
2561 index of the basis. */
2563 static void
2564 record_phi_increments (slsr_cand_t basis, gimple phi)
2566 unsigned i;
2567 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2569 for (i = 0; i < gimple_phi_num_args (phi); i++)
2571 tree arg = gimple_phi_arg_def (phi, i);
2573 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
2575 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2577 if (gimple_code (arg_def) == GIMPLE_PHI)
2578 record_phi_increments (basis, arg_def);
2579 else
2581 slsr_cand_t arg_cand = base_cand_from_table (arg);
2582 widest_int diff = arg_cand->index - basis->index;
2583 record_increment (arg_cand, diff, PHI_ADJUST);
2589 /* Determine how many times each unique increment occurs in the set
2590 of candidates rooted at C's parent, recording the data in the
2591 increment vector. For each unique increment I, if an initializer
2592 T_0 = stride * I is provided by a candidate that dominates all
2593 candidates with the same increment, also record T_0 for subsequent
2594 use. */
2596 static void
2597 record_increments (slsr_cand_t c)
2599 if (!cand_already_replaced (c))
2601 if (!phi_dependent_cand_p (c))
2602 record_increment (c, cand_increment (c), NOT_PHI_ADJUST);
2603 else
2605 /* A candidate with a basis hidden by a phi will have one
2606 increment for its relationship to the index represented by
2607 the phi, and potentially additional increments along each
2608 incoming edge. For the root of the dependency tree (which
2609 has no basis), process just the initial index in case it has
2610 an initializer that can be used by subsequent candidates. */
2611 record_increment (c, c->index, NOT_PHI_ADJUST);
2613 if (c->basis)
2614 record_phi_increments (lookup_cand (c->basis),
2615 lookup_cand (c->def_phi)->cand_stmt);
2619 if (c->sibling)
2620 record_increments (lookup_cand (c->sibling));
2622 if (c->dependent)
2623 record_increments (lookup_cand (c->dependent));
2626 /* Add up and return the costs of introducing add statements that
2627 require the increment INCR on behalf of candidate C and phi
2628 statement PHI. Accumulate into *SAVINGS the potential savings
2629 from removing existing statements that feed PHI and have no other
2630 uses. */
2632 static int
2633 phi_incr_cost (slsr_cand_t c, const widest_int &incr, gimple phi, int *savings)
2635 unsigned i;
2636 int cost = 0;
2637 slsr_cand_t basis = lookup_cand (c->basis);
2638 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2640 for (i = 0; i < gimple_phi_num_args (phi); i++)
2642 tree arg = gimple_phi_arg_def (phi, i);
2644 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
2646 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2648 if (gimple_code (arg_def) == GIMPLE_PHI)
2650 int feeding_savings = 0;
2651 cost += phi_incr_cost (c, incr, arg_def, &feeding_savings);
2652 if (has_single_use (gimple_phi_result (arg_def)))
2653 *savings += feeding_savings;
2655 else
2657 slsr_cand_t arg_cand = base_cand_from_table (arg);
2658 widest_int diff = arg_cand->index - basis->index;
2660 if (incr == diff)
2662 tree basis_lhs = gimple_assign_lhs (basis->cand_stmt);
2663 tree lhs = gimple_assign_lhs (arg_cand->cand_stmt);
2664 cost += add_cost (true, TYPE_MODE (TREE_TYPE (basis_lhs)));
2665 if (has_single_use (lhs))
2666 *savings += stmt_cost (arg_cand->cand_stmt, true);
2672 return cost;
2675 /* Return the first candidate in the tree rooted at C that has not
2676 already been replaced, favoring siblings over dependents. */
2678 static slsr_cand_t
2679 unreplaced_cand_in_tree (slsr_cand_t c)
2681 if (!cand_already_replaced (c))
2682 return c;
2684 if (c->sibling)
2686 slsr_cand_t sib = unreplaced_cand_in_tree (lookup_cand (c->sibling));
2687 if (sib)
2688 return sib;
2691 if (c->dependent)
2693 slsr_cand_t dep = unreplaced_cand_in_tree (lookup_cand (c->dependent));
2694 if (dep)
2695 return dep;
2698 return NULL;
2701 /* Return TRUE if the candidates in the tree rooted at C should be
2702 optimized for speed, else FALSE. We estimate this based on the block
2703 containing the most dominant candidate in the tree that has not yet
2704 been replaced. */
2706 static bool
2707 optimize_cands_for_speed_p (slsr_cand_t c)
2709 slsr_cand_t c2 = unreplaced_cand_in_tree (c);
2710 gcc_assert (c2);
2711 return optimize_bb_for_speed_p (gimple_bb (c2->cand_stmt));
2714 /* Add COST_IN to the lowest cost of any dependent path starting at
2715 candidate C or any of its siblings, counting only candidates along
2716 such paths with increment INCR. Assume that replacing a candidate
2717 reduces cost by REPL_SAVINGS. Also account for savings from any
2718 statements that would go dead. If COUNT_PHIS is true, include
2719 costs of introducing feeding statements for conditional candidates. */
2721 static int
2722 lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c,
2723 const widest_int &incr, bool count_phis)
2725 int local_cost, sib_cost, savings = 0;
2726 widest_int cand_incr = cand_abs_increment (c);
2728 if (cand_already_replaced (c))
2729 local_cost = cost_in;
2730 else if (incr == cand_incr)
2731 local_cost = cost_in - repl_savings - c->dead_savings;
2732 else
2733 local_cost = cost_in - c->dead_savings;
2735 if (count_phis
2736 && phi_dependent_cand_p (c)
2737 && !cand_already_replaced (c))
2739 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
2740 local_cost += phi_incr_cost (c, incr, phi, &savings);
2742 if (has_single_use (gimple_phi_result (phi)))
2743 local_cost -= savings;
2746 if (c->dependent)
2747 local_cost = lowest_cost_path (local_cost, repl_savings,
2748 lookup_cand (c->dependent), incr,
2749 count_phis);
2751 if (c->sibling)
2753 sib_cost = lowest_cost_path (cost_in, repl_savings,
2754 lookup_cand (c->sibling), incr,
2755 count_phis);
2756 local_cost = MIN (local_cost, sib_cost);
2759 return local_cost;
2762 /* Compute the total savings that would accrue from all replacements
2763 in the candidate tree rooted at C, counting only candidates with
2764 increment INCR. Assume that replacing a candidate reduces cost
2765 by REPL_SAVINGS. Also account for savings from statements that
2766 would go dead. */
2768 static int
2769 total_savings (int repl_savings, slsr_cand_t c, const widest_int &incr,
2770 bool count_phis)
2772 int savings = 0;
2773 widest_int cand_incr = cand_abs_increment (c);
2775 if (incr == cand_incr && !cand_already_replaced (c))
2776 savings += repl_savings + c->dead_savings;
2778 if (count_phis
2779 && phi_dependent_cand_p (c)
2780 && !cand_already_replaced (c))
2782 int phi_savings = 0;
2783 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
2784 savings -= phi_incr_cost (c, incr, phi, &phi_savings);
2786 if (has_single_use (gimple_phi_result (phi)))
2787 savings += phi_savings;
2790 if (c->dependent)
2791 savings += total_savings (repl_savings, lookup_cand (c->dependent), incr,
2792 count_phis);
2794 if (c->sibling)
2795 savings += total_savings (repl_savings, lookup_cand (c->sibling), incr,
2796 count_phis);
2798 return savings;
2801 /* Use target-specific costs to determine and record which increments
2802 in the current candidate tree are profitable to replace, assuming
2803 MODE and SPEED. FIRST_DEP is the first dependent of the root of
2804 the candidate tree.
2806 One slight limitation here is that we don't account for the possible
2807 introduction of casts in some cases. See replace_one_candidate for
2808 the cases where these are introduced. This should probably be cleaned
2809 up sometime. */
2811 static void
2812 analyze_increments (slsr_cand_t first_dep, enum machine_mode mode, bool speed)
2814 unsigned i;
2816 for (i = 0; i < incr_vec_len; i++)
2818 HOST_WIDE_INT incr = incr_vec[i].incr.to_shwi ();
2820 /* If somehow this increment is bigger than a HWI, we won't
2821 be optimizing candidates that use it. And if the increment
2822 has a count of zero, nothing will be done with it. */
2823 if (!wi::fits_shwi_p (incr_vec[i].incr) || !incr_vec[i].count)
2824 incr_vec[i].cost = COST_INFINITE;
2826 /* Increments of 0, 1, and -1 are always profitable to replace,
2827 because they always replace a multiply or add with an add or
2828 copy, and may cause one or more existing instructions to go
2829 dead. Exception: -1 can't be assumed to be profitable for
2830 pointer addition. */
2831 else if (incr == 0
2832 || incr == 1
2833 || (incr == -1
2834 && (gimple_assign_rhs_code (first_dep->cand_stmt)
2835 != POINTER_PLUS_EXPR)))
2836 incr_vec[i].cost = COST_NEUTRAL;
2838 /* FORNOW: If we need to add an initializer, give up if a cast from
2839 the candidate's type to its stride's type can lose precision.
2840 This could eventually be handled better by expressly retaining the
2841 result of a cast to a wider type in the stride. Example:
2843 short int _1;
2844 _2 = (int) _1;
2845 _3 = _2 * 10;
2846 _4 = x + _3; ADD: x + (10 * _1) : int
2847 _5 = _2 * 15;
2848 _6 = x + _3; ADD: x + (15 * _1) : int
2850 Right now replacing _6 would cause insertion of an initializer
2851 of the form "short int T = _1 * 5;" followed by a cast to
2852 int, which could overflow incorrectly. Had we recorded _2 or
2853 (int)_1 as the stride, this wouldn't happen. However, doing
2854 this breaks other opportunities, so this will require some
2855 care. */
2856 else if (!incr_vec[i].initializer
2857 && TREE_CODE (first_dep->stride) != INTEGER_CST
2858 && !legal_cast_p_1 (first_dep->stride,
2859 gimple_assign_lhs (first_dep->cand_stmt)))
2861 incr_vec[i].cost = COST_INFINITE;
2863 /* If we need to add an initializer, make sure we don't introduce
2864 a multiply by a pointer type, which can happen in certain cast
2865 scenarios. FIXME: When cleaning up these cast issues, we can
2866 afford to introduce the multiply provided we cast out to an
2867 unsigned int of appropriate size. */
2868 else if (!incr_vec[i].initializer
2869 && TREE_CODE (first_dep->stride) != INTEGER_CST
2870 && POINTER_TYPE_P (TREE_TYPE (first_dep->stride)))
2872 incr_vec[i].cost = COST_INFINITE;
2874 /* For any other increment, if this is a multiply candidate, we
2875 must introduce a temporary T and initialize it with
2876 T_0 = stride * increment. When optimizing for speed, walk the
2877 candidate tree to calculate the best cost reduction along any
2878 path; if it offsets the fixed cost of inserting the initializer,
2879 replacing the increment is profitable. When optimizing for
2880 size, instead calculate the total cost reduction from replacing
2881 all candidates with this increment. */
2882 else if (first_dep->kind == CAND_MULT)
2884 int cost = mult_by_coeff_cost (incr, mode, speed);
2885 int repl_savings = mul_cost (speed, mode) - add_cost (speed, mode);
2886 if (speed)
2887 cost = lowest_cost_path (cost, repl_savings, first_dep,
2888 incr_vec[i].incr, COUNT_PHIS);
2889 else
2890 cost -= total_savings (repl_savings, first_dep, incr_vec[i].incr,
2891 COUNT_PHIS);
2893 incr_vec[i].cost = cost;
2896 /* If this is an add candidate, the initializer may already
2897 exist, so only calculate the cost of the initializer if it
2898 doesn't. We are replacing one add with another here, so the
2899 known replacement savings is zero. We will account for removal
2900 of dead instructions in lowest_cost_path or total_savings. */
2901 else
2903 int cost = 0;
2904 if (!incr_vec[i].initializer)
2905 cost = mult_by_coeff_cost (incr, mode, speed);
2907 if (speed)
2908 cost = lowest_cost_path (cost, 0, first_dep, incr_vec[i].incr,
2909 DONT_COUNT_PHIS);
2910 else
2911 cost -= total_savings (0, first_dep, incr_vec[i].incr,
2912 DONT_COUNT_PHIS);
2914 incr_vec[i].cost = cost;
2919 /* Return the nearest common dominator of BB1 and BB2. If the blocks
2920 are identical, return the earlier of C1 and C2 in *WHERE. Otherwise,
2921 if the NCD matches BB1, return C1 in *WHERE; if the NCD matches BB2,
2922 return C2 in *WHERE; and if the NCD matches neither, return NULL in
2923 *WHERE. Note: It is possible for one of C1 and C2 to be NULL. */
2925 static basic_block
2926 ncd_for_two_cands (basic_block bb1, basic_block bb2,
2927 slsr_cand_t c1, slsr_cand_t c2, slsr_cand_t *where)
2929 basic_block ncd;
2931 if (!bb1)
2933 *where = c2;
2934 return bb2;
2937 if (!bb2)
2939 *where = c1;
2940 return bb1;
2943 ncd = nearest_common_dominator (CDI_DOMINATORS, bb1, bb2);
2945 /* If both candidates are in the same block, the earlier
2946 candidate wins. */
2947 if (bb1 == ncd && bb2 == ncd)
2949 if (!c1 || (c2 && c2->cand_num < c1->cand_num))
2950 *where = c2;
2951 else
2952 *where = c1;
2955 /* Otherwise, if one of them produced a candidate in the
2956 dominator, that one wins. */
2957 else if (bb1 == ncd)
2958 *where = c1;
2960 else if (bb2 == ncd)
2961 *where = c2;
2963 /* If neither matches the dominator, neither wins. */
2964 else
2965 *where = NULL;
2967 return ncd;
2970 /* Consider all candidates that feed PHI. Find the nearest common
2971 dominator of those candidates requiring the given increment INCR.
2972 Further find and return the nearest common dominator of this result
2973 with block NCD. If the returned block contains one or more of the
2974 candidates, return the earliest candidate in the block in *WHERE. */
2976 static basic_block
2977 ncd_with_phi (slsr_cand_t c, const widest_int &incr, gimple phi,
2978 basic_block ncd, slsr_cand_t *where)
2980 unsigned i;
2981 slsr_cand_t basis = lookup_cand (c->basis);
2982 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2984 for (i = 0; i < gimple_phi_num_args (phi); i++)
2986 tree arg = gimple_phi_arg_def (phi, i);
2988 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
2990 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2992 if (gimple_code (arg_def) == GIMPLE_PHI)
2993 ncd = ncd_with_phi (c, incr, arg_def, ncd, where);
2994 else
2996 slsr_cand_t arg_cand = base_cand_from_table (arg);
2997 widest_int diff = arg_cand->index - basis->index;
2998 basic_block pred = gimple_phi_arg_edge (phi, i)->src;
3000 if ((incr == diff) || (!address_arithmetic_p && incr == -diff))
3001 ncd = ncd_for_two_cands (ncd, pred, *where, NULL, where);
3006 return ncd;
3009 /* Consider the candidate C together with any candidates that feed
3010 C's phi dependence (if any). Find and return the nearest common
3011 dominator of those candidates requiring the given increment INCR.
3012 If the returned block contains one or more of the candidates,
3013 return the earliest candidate in the block in *WHERE. */
3015 static basic_block
3016 ncd_of_cand_and_phis (slsr_cand_t c, const widest_int &incr, slsr_cand_t *where)
3018 basic_block ncd = NULL;
3020 if (cand_abs_increment (c) == incr)
3022 ncd = gimple_bb (c->cand_stmt);
3023 *where = c;
3026 if (phi_dependent_cand_p (c))
3027 ncd = ncd_with_phi (c, incr, lookup_cand (c->def_phi)->cand_stmt,
3028 ncd, where);
3030 return ncd;
3033 /* Consider all candidates in the tree rooted at C for which INCR
3034 represents the required increment of C relative to its basis.
3035 Find and return the basic block that most nearly dominates all
3036 such candidates. If the returned block contains one or more of
3037 the candidates, return the earliest candidate in the block in
3038 *WHERE. */
3040 static basic_block
3041 nearest_common_dominator_for_cands (slsr_cand_t c, const widest_int &incr,
3042 slsr_cand_t *where)
3044 basic_block sib_ncd = NULL, dep_ncd = NULL, this_ncd = NULL, ncd;
3045 slsr_cand_t sib_where = NULL, dep_where = NULL, this_where = NULL, new_where;
3047 /* First find the NCD of all siblings and dependents. */
3048 if (c->sibling)
3049 sib_ncd = nearest_common_dominator_for_cands (lookup_cand (c->sibling),
3050 incr, &sib_where);
3051 if (c->dependent)
3052 dep_ncd = nearest_common_dominator_for_cands (lookup_cand (c->dependent),
3053 incr, &dep_where);
3054 if (!sib_ncd && !dep_ncd)
3056 new_where = NULL;
3057 ncd = NULL;
3059 else if (sib_ncd && !dep_ncd)
3061 new_where = sib_where;
3062 ncd = sib_ncd;
3064 else if (dep_ncd && !sib_ncd)
3066 new_where = dep_where;
3067 ncd = dep_ncd;
3069 else
3070 ncd = ncd_for_two_cands (sib_ncd, dep_ncd, sib_where,
3071 dep_where, &new_where);
3073 /* If the candidate's increment doesn't match the one we're interested
3074 in (and nor do any increments for feeding defs of a phi-dependence),
3075 then the result depends only on siblings and dependents. */
3076 this_ncd = ncd_of_cand_and_phis (c, incr, &this_where);
3078 if (!this_ncd || cand_already_replaced (c))
3080 *where = new_where;
3081 return ncd;
3084 /* Otherwise, compare this candidate with the result from all siblings
3085 and dependents. */
3086 ncd = ncd_for_two_cands (ncd, this_ncd, new_where, this_where, where);
3088 return ncd;
3091 /* Return TRUE if the increment indexed by INDEX is profitable to replace. */
3093 static inline bool
3094 profitable_increment_p (unsigned index)
3096 return (incr_vec[index].cost <= COST_NEUTRAL);
3099 /* For each profitable increment in the increment vector not equal to
3100 0 or 1 (or -1, for non-pointer arithmetic), find the nearest common
3101 dominator of all statements in the candidate chain rooted at C
3102 that require that increment, and insert an initializer
3103 T_0 = stride * increment at that location. Record T_0 with the
3104 increment record. */
3106 static void
3107 insert_initializers (slsr_cand_t c)
3109 unsigned i;
3111 for (i = 0; i < incr_vec_len; i++)
3113 basic_block bb;
3114 slsr_cand_t where = NULL;
3115 gimple init_stmt;
3116 tree stride_type, new_name, incr_tree;
3117 widest_int incr = incr_vec[i].incr;
3119 if (!profitable_increment_p (i)
3120 || incr == 1
3121 || (incr == -1
3122 && gimple_assign_rhs_code (c->cand_stmt) != POINTER_PLUS_EXPR)
3123 || incr == 0)
3124 continue;
3126 /* We may have already identified an existing initializer that
3127 will suffice. */
3128 if (incr_vec[i].initializer)
3130 if (dump_file && (dump_flags & TDF_DETAILS))
3132 fputs ("Using existing initializer: ", dump_file);
3133 print_gimple_stmt (dump_file,
3134 SSA_NAME_DEF_STMT (incr_vec[i].initializer),
3135 0, 0);
3137 continue;
3140 /* Find the block that most closely dominates all candidates
3141 with this increment. If there is at least one candidate in
3142 that block, the earliest one will be returned in WHERE. */
3143 bb = nearest_common_dominator_for_cands (c, incr, &where);
3145 /* Create a new SSA name to hold the initializer's value. */
3146 stride_type = TREE_TYPE (c->stride);
3147 new_name = make_temp_ssa_name (stride_type, NULL, "slsr");
3148 incr_vec[i].initializer = new_name;
3150 /* Create the initializer and insert it in the latest possible
3151 dominating position. */
3152 incr_tree = wide_int_to_tree (stride_type, incr);
3153 init_stmt = gimple_build_assign_with_ops (MULT_EXPR, new_name,
3154 c->stride, incr_tree);
3155 if (where)
3157 gimple_stmt_iterator gsi = gsi_for_stmt (where->cand_stmt);
3158 gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
3159 gimple_set_location (init_stmt, gimple_location (where->cand_stmt));
3161 else
3163 gimple_stmt_iterator gsi = gsi_last_bb (bb);
3164 gimple basis_stmt = lookup_cand (c->basis)->cand_stmt;
3166 if (!gsi_end_p (gsi) && is_ctrl_stmt (gsi_stmt (gsi)))
3167 gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
3168 else
3169 gsi_insert_after (&gsi, init_stmt, GSI_SAME_STMT);
3171 gimple_set_location (init_stmt, gimple_location (basis_stmt));
3174 if (dump_file && (dump_flags & TDF_DETAILS))
3176 fputs ("Inserting initializer: ", dump_file);
3177 print_gimple_stmt (dump_file, init_stmt, 0, 0);
3182 /* Return TRUE iff all required increments for candidates feeding PHI
3183 are profitable to replace on behalf of candidate C. */
3185 static bool
3186 all_phi_incrs_profitable (slsr_cand_t c, gimple phi)
3188 unsigned i;
3189 slsr_cand_t basis = lookup_cand (c->basis);
3190 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
3192 for (i = 0; i < gimple_phi_num_args (phi); i++)
3194 tree arg = gimple_phi_arg_def (phi, i);
3196 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
3198 gimple arg_def = SSA_NAME_DEF_STMT (arg);
3200 if (gimple_code (arg_def) == GIMPLE_PHI)
3202 if (!all_phi_incrs_profitable (c, arg_def))
3203 return false;
3205 else
3207 int j;
3208 slsr_cand_t arg_cand = base_cand_from_table (arg);
3209 widest_int increment = arg_cand->index - basis->index;
3211 if (!address_arithmetic_p && wi::neg_p (increment))
3212 increment = -increment;
3214 j = incr_vec_index (increment);
3216 if (dump_file && (dump_flags & TDF_DETAILS))
3218 fprintf (dump_file, " Conditional candidate %d, phi: ",
3219 c->cand_num);
3220 print_gimple_stmt (dump_file, phi, 0, 0);
3221 fputs (" increment: ", dump_file);
3222 print_decs (increment, dump_file);
3223 if (j < 0)
3224 fprintf (dump_file,
3225 "\n Not replaced; incr_vec overflow.\n");
3226 else {
3227 fprintf (dump_file, "\n cost: %d\n", incr_vec[j].cost);
3228 if (profitable_increment_p (j))
3229 fputs (" Replacing...\n", dump_file);
3230 else
3231 fputs (" Not replaced.\n", dump_file);
3235 if (j < 0 || !profitable_increment_p (j))
3236 return false;
3241 return true;
3244 /* Create a NOP_EXPR that copies FROM_EXPR into a new SSA name of
3245 type TO_TYPE, and insert it in front of the statement represented
3246 by candidate C. Use *NEW_VAR to create the new SSA name. Return
3247 the new SSA name. */
3249 static tree
3250 introduce_cast_before_cand (slsr_cand_t c, tree to_type, tree from_expr)
3252 tree cast_lhs;
3253 gimple cast_stmt;
3254 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3256 cast_lhs = make_temp_ssa_name (to_type, NULL, "slsr");
3257 cast_stmt = gimple_build_assign_with_ops (NOP_EXPR, cast_lhs,
3258 from_expr, NULL_TREE);
3259 gimple_set_location (cast_stmt, gimple_location (c->cand_stmt));
3260 gsi_insert_before (&gsi, cast_stmt, GSI_SAME_STMT);
3262 if (dump_file && (dump_flags & TDF_DETAILS))
3264 fputs (" Inserting: ", dump_file);
3265 print_gimple_stmt (dump_file, cast_stmt, 0, 0);
3268 return cast_lhs;
3271 /* Replace the RHS of the statement represented by candidate C with
3272 NEW_CODE, NEW_RHS1, and NEW_RHS2, provided that to do so doesn't
3273 leave C unchanged or just interchange its operands. The original
3274 operation and operands are in OLD_CODE, OLD_RHS1, and OLD_RHS2.
3275 If the replacement was made and we are doing a details dump,
3276 return the revised statement, else NULL. */
3278 static gimple
3279 replace_rhs_if_not_dup (enum tree_code new_code, tree new_rhs1, tree new_rhs2,
3280 enum tree_code old_code, tree old_rhs1, tree old_rhs2,
3281 slsr_cand_t c)
3283 if (new_code != old_code
3284 || ((!operand_equal_p (new_rhs1, old_rhs1, 0)
3285 || !operand_equal_p (new_rhs2, old_rhs2, 0))
3286 && (!operand_equal_p (new_rhs1, old_rhs2, 0)
3287 || !operand_equal_p (new_rhs2, old_rhs1, 0))))
3289 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3290 gimple_assign_set_rhs_with_ops (&gsi, new_code, new_rhs1, new_rhs2);
3291 update_stmt (gsi_stmt (gsi));
3292 c->cand_stmt = gsi_stmt (gsi);
3294 if (dump_file && (dump_flags & TDF_DETAILS))
3295 return gsi_stmt (gsi);
3298 else if (dump_file && (dump_flags & TDF_DETAILS))
3299 fputs (" (duplicate, not actually replacing)\n", dump_file);
3301 return NULL;
3304 /* Strength-reduce the statement represented by candidate C by replacing
3305 it with an equivalent addition or subtraction. I is the index into
3306 the increment vector identifying C's increment. NEW_VAR is used to
3307 create a new SSA name if a cast needs to be introduced. BASIS_NAME
3308 is the rhs1 to use in creating the add/subtract. */
3310 static void
3311 replace_one_candidate (slsr_cand_t c, unsigned i, tree basis_name)
3313 gimple stmt_to_print = NULL;
3314 tree orig_rhs1, orig_rhs2;
3315 tree rhs2;
3316 enum tree_code orig_code, repl_code;
3317 widest_int cand_incr;
3319 orig_code = gimple_assign_rhs_code (c->cand_stmt);
3320 orig_rhs1 = gimple_assign_rhs1 (c->cand_stmt);
3321 orig_rhs2 = gimple_assign_rhs2 (c->cand_stmt);
3322 cand_incr = cand_increment (c);
3324 if (dump_file && (dump_flags & TDF_DETAILS))
3326 fputs ("Replacing: ", dump_file);
3327 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
3328 stmt_to_print = c->cand_stmt;
3331 if (address_arithmetic_p)
3332 repl_code = POINTER_PLUS_EXPR;
3333 else
3334 repl_code = PLUS_EXPR;
3336 /* If the increment has an initializer T_0, replace the candidate
3337 statement with an add of the basis name and the initializer. */
3338 if (incr_vec[i].initializer)
3340 tree init_type = TREE_TYPE (incr_vec[i].initializer);
3341 tree orig_type = TREE_TYPE (orig_rhs2);
3343 if (types_compatible_p (orig_type, init_type))
3344 rhs2 = incr_vec[i].initializer;
3345 else
3346 rhs2 = introduce_cast_before_cand (c, orig_type,
3347 incr_vec[i].initializer);
3349 if (incr_vec[i].incr != cand_incr)
3351 gcc_assert (repl_code == PLUS_EXPR);
3352 repl_code = MINUS_EXPR;
3355 stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
3356 orig_code, orig_rhs1, orig_rhs2,
3360 /* Otherwise, the increment is one of -1, 0, and 1. Replace
3361 with a subtract of the stride from the basis name, a copy
3362 from the basis name, or an add of the stride to the basis
3363 name, respectively. It may be necessary to introduce a
3364 cast (or reuse an existing cast). */
3365 else if (cand_incr == 1)
3367 tree stride_type = TREE_TYPE (c->stride);
3368 tree orig_type = TREE_TYPE (orig_rhs2);
3370 if (types_compatible_p (orig_type, stride_type))
3371 rhs2 = c->stride;
3372 else
3373 rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
3375 stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
3376 orig_code, orig_rhs1, orig_rhs2,
3380 else if (cand_incr == -1)
3382 tree stride_type = TREE_TYPE (c->stride);
3383 tree orig_type = TREE_TYPE (orig_rhs2);
3384 gcc_assert (repl_code != POINTER_PLUS_EXPR);
3386 if (types_compatible_p (orig_type, stride_type))
3387 rhs2 = c->stride;
3388 else
3389 rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
3391 if (orig_code != MINUS_EXPR
3392 || !operand_equal_p (basis_name, orig_rhs1, 0)
3393 || !operand_equal_p (rhs2, orig_rhs2, 0))
3395 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3396 gimple_assign_set_rhs_with_ops (&gsi, MINUS_EXPR, basis_name, rhs2);
3397 update_stmt (gsi_stmt (gsi));
3398 c->cand_stmt = gsi_stmt (gsi);
3400 if (dump_file && (dump_flags & TDF_DETAILS))
3401 stmt_to_print = gsi_stmt (gsi);
3403 else if (dump_file && (dump_flags & TDF_DETAILS))
3404 fputs (" (duplicate, not actually replacing)\n", dump_file);
3407 else if (cand_incr == 0)
3409 tree lhs = gimple_assign_lhs (c->cand_stmt);
3410 tree lhs_type = TREE_TYPE (lhs);
3411 tree basis_type = TREE_TYPE (basis_name);
3413 if (types_compatible_p (lhs_type, basis_type))
3415 gimple copy_stmt = gimple_build_assign (lhs, basis_name);
3416 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3417 gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
3418 gsi_replace (&gsi, copy_stmt, false);
3419 c->cand_stmt = copy_stmt;
3421 if (dump_file && (dump_flags & TDF_DETAILS))
3422 stmt_to_print = copy_stmt;
3424 else
3426 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3427 gimple cast_stmt = gimple_build_assign_with_ops (NOP_EXPR, lhs,
3428 basis_name,
3429 NULL_TREE);
3430 gimple_set_location (cast_stmt, gimple_location (c->cand_stmt));
3431 gsi_replace (&gsi, cast_stmt, false);
3432 c->cand_stmt = cast_stmt;
3434 if (dump_file && (dump_flags & TDF_DETAILS))
3435 stmt_to_print = cast_stmt;
3438 else
3439 gcc_unreachable ();
3441 if (dump_file && (dump_flags & TDF_DETAILS) && stmt_to_print)
3443 fputs ("With: ", dump_file);
3444 print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
3445 fputs ("\n", dump_file);
3449 /* For each candidate in the tree rooted at C, replace it with
3450 an increment if such has been shown to be profitable. */
3452 static void
3453 replace_profitable_candidates (slsr_cand_t c)
3455 if (!cand_already_replaced (c))
3457 widest_int increment = cand_abs_increment (c);
3458 enum tree_code orig_code = gimple_assign_rhs_code (c->cand_stmt);
3459 int i;
3461 i = incr_vec_index (increment);
3463 /* Only process profitable increments. Nothing useful can be done
3464 to a cast or copy. */
3465 if (i >= 0
3466 && profitable_increment_p (i)
3467 && orig_code != MODIFY_EXPR
3468 && orig_code != NOP_EXPR)
3470 if (phi_dependent_cand_p (c))
3472 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
3474 if (all_phi_incrs_profitable (c, phi))
3476 /* Look up the LHS SSA name from C's basis. This will be
3477 the RHS1 of the adds we will introduce to create new
3478 phi arguments. */
3479 slsr_cand_t basis = lookup_cand (c->basis);
3480 tree basis_name = gimple_assign_lhs (basis->cand_stmt);
3482 /* Create a new phi statement that will represent C's true
3483 basis after the transformation is complete. */
3484 location_t loc = gimple_location (c->cand_stmt);
3485 tree name = create_phi_basis (c, phi, basis_name,
3486 loc, UNKNOWN_STRIDE);
3488 /* Replace C with an add of the new basis phi and the
3489 increment. */
3490 replace_one_candidate (c, i, name);
3493 else
3495 slsr_cand_t basis = lookup_cand (c->basis);
3496 tree basis_name = gimple_assign_lhs (basis->cand_stmt);
3497 replace_one_candidate (c, i, basis_name);
3502 if (c->sibling)
3503 replace_profitable_candidates (lookup_cand (c->sibling));
3505 if (c->dependent)
3506 replace_profitable_candidates (lookup_cand (c->dependent));
3509 /* Analyze costs of related candidates in the candidate vector,
3510 and make beneficial replacements. */
3512 static void
3513 analyze_candidates_and_replace (void)
3515 unsigned i;
3516 slsr_cand_t c;
3518 /* Each candidate that has a null basis and a non-null
3519 dependent is the root of a tree of related statements.
3520 Analyze each tree to determine a subset of those
3521 statements that can be replaced with maximum benefit. */
3522 FOR_EACH_VEC_ELT (cand_vec, i, c)
3524 slsr_cand_t first_dep;
3526 if (c->basis != 0 || c->dependent == 0)
3527 continue;
3529 if (dump_file && (dump_flags & TDF_DETAILS))
3530 fprintf (dump_file, "\nProcessing dependency tree rooted at %d.\n",
3531 c->cand_num);
3533 first_dep = lookup_cand (c->dependent);
3535 /* If this is a chain of CAND_REFs, unconditionally replace
3536 each of them with a strength-reduced data reference. */
3537 if (c->kind == CAND_REF)
3538 replace_refs (c);
3540 /* If the common stride of all related candidates is a known
3541 constant, each candidate without a phi-dependence can be
3542 profitably replaced. Each replaces a multiply by a single
3543 add, with the possibility that a feeding add also goes dead.
3544 A candidate with a phi-dependence is replaced only if the
3545 compensation code it requires is offset by the strength
3546 reduction savings. */
3547 else if (TREE_CODE (c->stride) == INTEGER_CST)
3548 replace_uncond_cands_and_profitable_phis (first_dep);
3550 /* When the stride is an SSA name, it may still be profitable
3551 to replace some or all of the dependent candidates, depending
3552 on whether the introduced increments can be reused, or are
3553 less expensive to calculate than the replaced statements. */
3554 else
3556 enum machine_mode mode;
3557 bool speed;
3559 /* Determine whether we'll be generating pointer arithmetic
3560 when replacing candidates. */
3561 address_arithmetic_p = (c->kind == CAND_ADD
3562 && POINTER_TYPE_P (c->cand_type));
3564 /* If all candidates have already been replaced under other
3565 interpretations, nothing remains to be done. */
3566 if (!count_candidates (c))
3567 continue;
3569 /* Construct an array of increments for this candidate chain. */
3570 incr_vec = XNEWVEC (incr_info, MAX_INCR_VEC_LEN);
3571 incr_vec_len = 0;
3572 record_increments (c);
3574 /* Determine which increments are profitable to replace. */
3575 mode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (c->cand_stmt)));
3576 speed = optimize_cands_for_speed_p (c);
3577 analyze_increments (first_dep, mode, speed);
3579 /* Insert initializers of the form T_0 = stride * increment
3580 for use in profitable replacements. */
3581 insert_initializers (first_dep);
3582 dump_incr_vec ();
3584 /* Perform the replacements. */
3585 replace_profitable_candidates (first_dep);
3586 free (incr_vec);
3591 namespace {
3593 const pass_data pass_data_strength_reduction =
3595 GIMPLE_PASS, /* type */
3596 "slsr", /* name */
3597 OPTGROUP_NONE, /* optinfo_flags */
3598 true, /* has_execute */
3599 TV_GIMPLE_SLSR, /* tv_id */
3600 ( PROP_cfg | PROP_ssa ), /* properties_required */
3601 0, /* properties_provided */
3602 0, /* properties_destroyed */
3603 0, /* todo_flags_start */
3604 0, /* todo_flags_finish */
3607 class pass_strength_reduction : public gimple_opt_pass
3609 public:
3610 pass_strength_reduction (gcc::context *ctxt)
3611 : gimple_opt_pass (pass_data_strength_reduction, ctxt)
3614 /* opt_pass methods: */
3615 virtual bool gate (function *) { return flag_tree_slsr; }
3616 virtual unsigned int execute (function *);
3618 }; // class pass_strength_reduction
3620 unsigned
3621 pass_strength_reduction::execute (function *fun)
3623 /* Create the obstack where candidates will reside. */
3624 gcc_obstack_init (&cand_obstack);
3626 /* Allocate the candidate vector. */
3627 cand_vec.create (128);
3629 /* Allocate the mapping from statements to candidate indices. */
3630 stmt_cand_map = pointer_map_create ();
3632 /* Create the obstack where candidate chains will reside. */
3633 gcc_obstack_init (&chain_obstack);
3635 /* Allocate the mapping from base expressions to candidate chains. */
3636 base_cand_map.create (500);
3638 /* Allocate the mapping from bases to alternative bases. */
3639 alt_base_map = pointer_map_create ();
3641 /* Initialize the loop optimizer. We need to detect flow across
3642 back edges, and this gives us dominator information as well. */
3643 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
3645 /* Walk the CFG in predominator order looking for strength reduction
3646 candidates. */
3647 find_candidates_dom_walker (CDI_DOMINATORS)
3648 .walk (fun->cfg->x_entry_block_ptr);
3650 if (dump_file && (dump_flags & TDF_DETAILS))
3652 dump_cand_vec ();
3653 dump_cand_chains ();
3656 pointer_map_destroy (alt_base_map);
3657 free_affine_expand_cache (&name_expansions);
3659 /* Analyze costs and make appropriate replacements. */
3660 analyze_candidates_and_replace ();
3662 loop_optimizer_finalize ();
3663 base_cand_map.dispose ();
3664 obstack_free (&chain_obstack, NULL);
3665 pointer_map_destroy (stmt_cand_map);
3666 cand_vec.release ();
3667 obstack_free (&cand_obstack, NULL);
3669 return 0;
3672 } // anon namespace
3674 gimple_opt_pass *
3675 make_pass_strength_reduction (gcc::context *ctxt)
3677 return new pass_strength_reduction (ctxt);