Simplify convert_modes, ignoring invalid old modes for CONST_INTs.
[official-gcc.git] / gcc / gimple-ssa-strength-reduction.c
blob4d9c1f8d68dc2cefbfd54ebec9c26fd4aa09f36c
1 /* Straight-line strength reduction.
2 Copyright (C) 2012-2013 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 "gimple.h"
41 #include "basic-block.h"
42 #include "tree-pass.h"
43 #include "cfgloop.h"
44 #include "gimple-pretty-print.h"
45 #include "tree-ssa.h"
46 #include "domwalk.h"
47 #include "pointer-set.h"
48 #include "expmed.h"
49 #include "params.h"
50 #include "hash-table.h"
51 #include "tree-ssa-address.h"
52 #include "wide-int-print.h"
54 /* Information about a strength reduction candidate. Each statement
55 in the candidate table represents an expression of one of the
56 following forms (the special case of CAND_REF will be described
57 later):
59 (CAND_MULT) S1: X = (B + i) * S
60 (CAND_ADD) S1: X = B + (i * S)
62 Here X and B are SSA names, i is an integer constant, and S is
63 either an SSA name or a constant. We call B the "base," i the
64 "index", and S the "stride."
66 Any statement S0 that dominates S1 and is of the form:
68 (CAND_MULT) S0: Y = (B + i') * S
69 (CAND_ADD) S0: Y = B + (i' * S)
71 is called a "basis" for S1. In both cases, S1 may be replaced by
73 S1': X = Y + (i - i') * S,
75 where (i - i') * S is folded to the extent possible.
77 All gimple statements are visited in dominator order, and each
78 statement that may contribute to one of the forms of S1 above is
79 given at least one entry in the candidate table. Such statements
80 include addition, pointer addition, subtraction, multiplication,
81 negation, copies, and nontrivial type casts. If a statement may
82 represent more than one expression of the forms of S1 above,
83 multiple "interpretations" are stored in the table and chained
84 together. Examples:
86 * An add of two SSA names may treat either operand as the base.
87 * A multiply of two SSA names, likewise.
88 * A copy or cast may be thought of as either a CAND_MULT with
89 i = 0 and S = 1, or as a CAND_ADD with i = 0 or S = 0.
91 Candidate records are allocated from an obstack. They are addressed
92 both from a hash table keyed on S1, and from a vector of candidate
93 pointers arranged in predominator order.
95 Opportunity note
96 ----------------
97 Currently we don't recognize:
99 S0: Y = (S * i') - B
100 S1: X = (S * i) - B
102 as a strength reduction opportunity, even though this S1 would
103 also be replaceable by the S1' above. This can be added if it
104 comes up in practice.
106 Strength reduction in addressing
107 --------------------------------
108 There is another kind of candidate known as CAND_REF. A CAND_REF
109 describes a statement containing a memory reference having
110 complex addressing that might benefit from strength reduction.
111 Specifically, we are interested in references for which
112 get_inner_reference returns a base address, offset, and bitpos as
113 follows:
115 base: MEM_REF (T1, C1)
116 offset: MULT_EXPR (PLUS_EXPR (T2, C2), C3)
117 bitpos: C4 * BITS_PER_UNIT
119 Here T1 and T2 are arbitrary trees, and C1, C2, C3, C4 are
120 arbitrary integer constants. Note that C2 may be zero, in which
121 case the offset will be MULT_EXPR (T2, C3).
123 When this pattern is recognized, the original memory reference
124 can be replaced with:
126 MEM_REF (POINTER_PLUS_EXPR (T1, MULT_EXPR (T2, C3)),
127 C1 + (C2 * C3) + C4)
129 which distributes the multiply to allow constant folding. When
130 two or more addressing expressions can be represented by MEM_REFs
131 of this form, differing only in the constants C1, C2, and C4,
132 making this substitution produces more efficient addressing during
133 the RTL phases. When there are not at least two expressions with
134 the same values of T1, T2, and C3, there is nothing to be gained
135 by the replacement.
137 Strength reduction of CAND_REFs uses the same infrastructure as
138 that used by CAND_MULTs and CAND_ADDs. We record T1 in the base (B)
139 field, MULT_EXPR (T2, C3) in the stride (S) field, and
140 C1 + (C2 * C3) + C4 in the index (i) field. A basis for a CAND_REF
141 is thus another CAND_REF with the same B and S values. When at
142 least two CAND_REFs are chained together using the basis relation,
143 each of them is replaced as above, resulting in improved code
144 generation for addressing.
146 Conditional candidates
147 ======================
149 Conditional candidates are best illustrated with an example.
150 Consider the code sequence:
152 (1) x_0 = ...;
153 (2) a_0 = x_0 * 5; MULT (B: x_0; i: 0; S: 5)
154 if (...)
155 (3) x_1 = x_0 + 1; ADD (B: x_0, i: 1; S: 1)
156 (4) x_2 = PHI <x_0, x_1>; PHI (B: x_0, i: 0, S: 1)
157 (5) x_3 = x_2 + 1; ADD (B: x_2, i: 1, S: 1)
158 (6) a_1 = x_3 * 5; MULT (B: x_2, i: 1; S: 5)
160 Here strength reduction is complicated by the uncertain value of x_2.
161 A legitimate transformation is:
163 (1) x_0 = ...;
164 (2) a_0 = x_0 * 5;
165 if (...)
167 (3) [x_1 = x_0 + 1;]
168 (3a) t_1 = a_0 + 5;
170 (4) [x_2 = PHI <x_0, x_1>;]
171 (4a) t_2 = PHI <a_0, t_1>;
172 (5) [x_3 = x_2 + 1;]
173 (6r) a_1 = t_2 + 5;
175 where the bracketed instructions may go dead.
177 To recognize this opportunity, we have to observe that statement (6)
178 has a "hidden basis" (2). The hidden basis is unlike a normal basis
179 in that the statement and the hidden basis have different base SSA
180 names (x_2 and x_0, respectively). The relationship is established
181 when a statement's base name (x_2) is defined by a phi statement (4),
182 each argument of which (x_0, x_1) has an identical "derived base name."
183 If the argument is defined by a candidate (as x_1 is by (3)) that is a
184 CAND_ADD having a stride of 1, the derived base name of the argument is
185 the base name of the candidate (x_0). Otherwise, the argument itself
186 is its derived base name (as is the case with argument x_0).
188 The hidden basis for statement (6) is the nearest dominating candidate
189 whose base name is the derived base name (x_0) of the feeding phi (4),
190 and whose stride is identical to that of the statement. We can then
191 create the new "phi basis" (4a) and feeding adds along incoming arcs (3a),
192 allowing the final replacement of (6) by the strength-reduced (6r).
194 To facilitate this, a new kind of candidate (CAND_PHI) is introduced.
195 A CAND_PHI is not a candidate for replacement, but is maintained in the
196 candidate table to ease discovery of hidden bases. Any phi statement
197 whose arguments share a common derived base name is entered into the
198 table with the derived base name, an (arbitrary) index of zero, and a
199 stride of 1. A statement with a hidden basis can then be detected by
200 simply looking up its feeding phi definition in the candidate table,
201 extracting the derived base name, and searching for a basis in the
202 usual manner after substituting the derived base name.
204 Note that the transformation is only valid when the original phi and
205 the statements that define the phi's arguments are all at the same
206 position in the loop hierarchy. */
209 /* Index into the candidate vector, offset by 1. VECs are zero-based,
210 while cand_idx's are one-based, with zero indicating null. */
211 typedef unsigned cand_idx;
213 /* The kind of candidate. */
214 enum cand_kind
216 CAND_MULT,
217 CAND_ADD,
218 CAND_REF,
219 CAND_PHI
222 struct slsr_cand_d
224 /* The candidate statement S1. */
225 gimple cand_stmt;
227 /* The base expression B: often an SSA name, but not always. */
228 tree base_expr;
230 /* The stride S. */
231 tree stride;
233 /* The index constant i. */
234 widest_int index;
236 /* The type of the candidate. This is normally the type of base_expr,
237 but casts may have occurred when combining feeding instructions.
238 A candidate can only be a basis for candidates of the same final type.
239 (For CAND_REFs, this is the type to be used for operand 1 of the
240 replacement MEM_REF.) */
241 tree cand_type;
243 /* The kind of candidate (CAND_MULT, etc.). */
244 enum cand_kind kind;
246 /* Index of this candidate in the candidate vector. */
247 cand_idx cand_num;
249 /* Index of the next candidate record for the same statement.
250 A statement may be useful in more than one way (e.g., due to
251 commutativity). So we can have multiple "interpretations"
252 of a statement. */
253 cand_idx next_interp;
255 /* Index of the basis statement S0, if any, in the candidate vector. */
256 cand_idx basis;
258 /* First candidate for which this candidate is a basis, if one exists. */
259 cand_idx dependent;
261 /* Next candidate having the same basis as this one. */
262 cand_idx sibling;
264 /* If this is a conditional candidate, the CAND_PHI candidate
265 that defines the base SSA name B. */
266 cand_idx def_phi;
268 /* Savings that can be expected from eliminating dead code if this
269 candidate is replaced. */
270 int dead_savings;
273 typedef struct slsr_cand_d slsr_cand, *slsr_cand_t;
274 typedef const struct slsr_cand_d *const_slsr_cand_t;
276 /* Pointers to candidates are chained together as part of a mapping
277 from base expressions to the candidates that use them. */
279 struct cand_chain_d
281 /* Base expression for the chain of candidates: often, but not
282 always, an SSA name. */
283 tree base_expr;
285 /* Pointer to a candidate. */
286 slsr_cand_t cand;
288 /* Chain pointer. */
289 struct cand_chain_d *next;
293 typedef struct cand_chain_d cand_chain, *cand_chain_t;
294 typedef const struct cand_chain_d *const_cand_chain_t;
296 /* Information about a unique "increment" associated with candidates
297 having an SSA name for a stride. An increment is the difference
298 between the index of the candidate and the index of its basis,
299 i.e., (i - i') as discussed in the module commentary.
301 When we are not going to generate address arithmetic we treat
302 increments that differ only in sign as the same, allowing sharing
303 of the cost of initializers. The absolute value of the increment
304 is stored in the incr_info. */
306 struct incr_info_d
308 /* The increment that relates a candidate to its basis. */
309 widest_int incr;
311 /* How many times the increment occurs in the candidate tree. */
312 unsigned count;
314 /* Cost of replacing candidates using this increment. Negative and
315 zero costs indicate replacement should be performed. */
316 int cost;
318 /* If this increment is profitable but is not -1, 0, or 1, it requires
319 an initializer T_0 = stride * incr to be found or introduced in the
320 nearest common dominator of all candidates. This field holds T_0
321 for subsequent use. */
322 tree initializer;
324 /* If the initializer was found to already exist, this is the block
325 where it was found. */
326 basic_block init_bb;
329 typedef struct incr_info_d incr_info, *incr_info_t;
331 /* Candidates are maintained in a vector. If candidate X dominates
332 candidate Y, then X appears before Y in the vector; but the
333 converse does not necessarily hold. */
334 static vec<slsr_cand_t> cand_vec;
336 enum cost_consts
338 COST_NEUTRAL = 0,
339 COST_INFINITE = 1000
342 enum stride_status
344 UNKNOWN_STRIDE = 0,
345 KNOWN_STRIDE = 1
348 enum phi_adjust_status
350 NOT_PHI_ADJUST = 0,
351 PHI_ADJUST = 1
354 enum count_phis_status
356 DONT_COUNT_PHIS = 0,
357 COUNT_PHIS = 1
360 /* Pointer map embodying a mapping from statements to candidates. */
361 static struct pointer_map_t *stmt_cand_map;
363 /* Obstack for candidates. */
364 static struct obstack cand_obstack;
366 /* Obstack for candidate chains. */
367 static struct obstack chain_obstack;
369 /* An array INCR_VEC of incr_infos is used during analysis of related
370 candidates having an SSA name for a stride. INCR_VEC_LEN describes
371 its current length. MAX_INCR_VEC_LEN is used to avoid costly
372 pathological cases. */
373 static incr_info_t incr_vec;
374 static unsigned incr_vec_len;
375 const int MAX_INCR_VEC_LEN = 16;
377 /* For a chain of candidates with unknown stride, indicates whether or not
378 we must generate pointer arithmetic when replacing statements. */
379 static bool address_arithmetic_p;
381 /* Forward function declarations. */
382 static slsr_cand_t base_cand_from_table (tree);
383 static tree introduce_cast_before_cand (slsr_cand_t, tree, tree);
384 static bool legal_cast_p_1 (tree, tree);
386 /* Produce a pointer to the IDX'th candidate in the candidate vector. */
388 static slsr_cand_t
389 lookup_cand (cand_idx idx)
391 return cand_vec[idx - 1];
394 /* Helper for hashing a candidate chain header. */
396 struct cand_chain_hasher : typed_noop_remove <cand_chain>
398 typedef cand_chain value_type;
399 typedef cand_chain compare_type;
400 static inline hashval_t hash (const value_type *);
401 static inline bool equal (const value_type *, const compare_type *);
404 inline hashval_t
405 cand_chain_hasher::hash (const value_type *p)
407 tree base_expr = p->base_expr;
408 return iterative_hash_expr (base_expr, 0);
411 inline bool
412 cand_chain_hasher::equal (const value_type *chain1, const compare_type *chain2)
414 return operand_equal_p (chain1->base_expr, chain2->base_expr, 0);
417 /* Hash table embodying a mapping from base exprs to chains of candidates. */
418 static hash_table <cand_chain_hasher> base_cand_map;
420 /* Look in the candidate table for a CAND_PHI that defines BASE and
421 return it if found; otherwise return NULL. */
423 static cand_idx
424 find_phi_def (tree base)
426 slsr_cand_t c;
428 if (TREE_CODE (base) != SSA_NAME)
429 return 0;
431 c = base_cand_from_table (base);
433 if (!c || c->kind != CAND_PHI)
434 return 0;
436 return c->cand_num;
439 /* Helper routine for find_basis_for_candidate. May be called twice:
440 once for the candidate's base expr, and optionally again for the
441 candidate's phi definition. */
443 static slsr_cand_t
444 find_basis_for_base_expr (slsr_cand_t c, tree base_expr)
446 cand_chain mapping_key;
447 cand_chain_t chain;
448 slsr_cand_t basis = NULL;
450 // Limit potential of N^2 behavior for long candidate chains.
451 int iters = 0;
452 int max_iters = PARAM_VALUE (PARAM_MAX_SLSR_CANDIDATE_SCAN);
454 mapping_key.base_expr = base_expr;
455 chain = base_cand_map.find (&mapping_key);
457 for (; chain && iters < max_iters; chain = chain->next, ++iters)
459 slsr_cand_t one_basis = chain->cand;
461 if (one_basis->kind != c->kind
462 || one_basis->cand_stmt == c->cand_stmt
463 || !operand_equal_p (one_basis->stride, c->stride, 0)
464 || !types_compatible_p (one_basis->cand_type, c->cand_type)
465 || !dominated_by_p (CDI_DOMINATORS,
466 gimple_bb (c->cand_stmt),
467 gimple_bb (one_basis->cand_stmt)))
468 continue;
470 if (!basis || basis->cand_num < one_basis->cand_num)
471 basis = one_basis;
474 return basis;
477 /* Use the base expr from candidate C to look for possible candidates
478 that can serve as a basis for C. Each potential basis must also
479 appear in a block that dominates the candidate statement and have
480 the same stride and type. If more than one possible basis exists,
481 the one with highest index in the vector is chosen; this will be
482 the most immediately dominating basis. */
484 static int
485 find_basis_for_candidate (slsr_cand_t c)
487 slsr_cand_t basis = find_basis_for_base_expr (c, c->base_expr);
489 /* If a candidate doesn't have a basis using its base expression,
490 it may have a basis hidden by one or more intervening phis. */
491 if (!basis && c->def_phi)
493 basic_block basis_bb, phi_bb;
494 slsr_cand_t phi_cand = lookup_cand (c->def_phi);
495 basis = find_basis_for_base_expr (c, phi_cand->base_expr);
497 if (basis)
499 /* A hidden basis must dominate the phi-definition of the
500 candidate's base name. */
501 phi_bb = gimple_bb (phi_cand->cand_stmt);
502 basis_bb = gimple_bb (basis->cand_stmt);
504 if (phi_bb == basis_bb
505 || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
507 basis = NULL;
508 c->basis = 0;
511 /* If we found a hidden basis, estimate additional dead-code
512 savings if the phi and its feeding statements can be removed. */
513 if (basis && has_single_use (gimple_phi_result (phi_cand->cand_stmt)))
514 c->dead_savings += phi_cand->dead_savings;
518 if (basis)
520 c->sibling = basis->dependent;
521 basis->dependent = c->cand_num;
522 return basis->cand_num;
525 return 0;
528 /* Record a mapping from the base expression of C to C itself, indicating that
529 C may potentially serve as a basis using that base expression. */
531 static void
532 record_potential_basis (slsr_cand_t c)
534 cand_chain_t node;
535 cand_chain **slot;
537 node = (cand_chain_t) obstack_alloc (&chain_obstack, sizeof (cand_chain));
538 node->base_expr = c->base_expr;
539 node->cand = c;
540 node->next = NULL;
541 slot = base_cand_map.find_slot (node, INSERT);
543 if (*slot)
545 cand_chain_t head = (cand_chain_t) (*slot);
546 node->next = head->next;
547 head->next = node;
549 else
550 *slot = node;
553 /* Allocate storage for a new candidate and initialize its fields.
554 Attempt to find a basis for the candidate. */
556 static slsr_cand_t
557 alloc_cand_and_find_basis (enum cand_kind kind, gimple gs, tree base,
558 const widest_int &index, tree stride, tree ctype,
559 unsigned savings)
561 slsr_cand_t c = (slsr_cand_t) obstack_alloc (&cand_obstack,
562 sizeof (slsr_cand));
563 c->cand_stmt = gs;
564 c->base_expr = base;
565 c->stride = stride;
566 c->index = index;
567 c->cand_type = ctype;
568 c->kind = kind;
569 c->cand_num = cand_vec.length () + 1;
570 c->next_interp = 0;
571 c->dependent = 0;
572 c->sibling = 0;
573 c->def_phi = kind == CAND_MULT ? find_phi_def (base) : 0;
574 c->dead_savings = savings;
576 cand_vec.safe_push (c);
578 if (kind == CAND_PHI)
579 c->basis = 0;
580 else
581 c->basis = find_basis_for_candidate (c);
583 record_potential_basis (c);
585 return c;
588 /* Determine the target cost of statement GS when compiling according
589 to SPEED. */
591 static int
592 stmt_cost (gimple gs, bool speed)
594 tree lhs, rhs1, rhs2;
595 enum machine_mode lhs_mode;
597 gcc_assert (is_gimple_assign (gs));
598 lhs = gimple_assign_lhs (gs);
599 rhs1 = gimple_assign_rhs1 (gs);
600 lhs_mode = TYPE_MODE (TREE_TYPE (lhs));
602 switch (gimple_assign_rhs_code (gs))
604 case MULT_EXPR:
605 rhs2 = gimple_assign_rhs2 (gs);
607 if (tree_fits_shwi_p (rhs2))
608 return mult_by_coeff_cost (tree_to_shwi (rhs2), lhs_mode, speed);
610 gcc_assert (TREE_CODE (rhs1) != INTEGER_CST);
611 return mul_cost (speed, lhs_mode);
613 case PLUS_EXPR:
614 case POINTER_PLUS_EXPR:
615 case MINUS_EXPR:
616 return add_cost (speed, lhs_mode);
618 case NEGATE_EXPR:
619 return neg_cost (speed, lhs_mode);
621 case NOP_EXPR:
622 return convert_cost (lhs_mode, TYPE_MODE (TREE_TYPE (rhs1)), speed);
624 /* Note that we don't assign costs to copies that in most cases
625 will go away. */
626 default:
630 gcc_unreachable ();
631 return 0;
634 /* Look up the defining statement for BASE_IN and return a pointer
635 to its candidate in the candidate table, if any; otherwise NULL.
636 Only CAND_ADD and CAND_MULT candidates are returned. */
638 static slsr_cand_t
639 base_cand_from_table (tree base_in)
641 slsr_cand_t *result;
643 gimple def = SSA_NAME_DEF_STMT (base_in);
644 if (!def)
645 return (slsr_cand_t) NULL;
647 result = (slsr_cand_t *) pointer_map_contains (stmt_cand_map, def);
649 if (result && (*result)->kind != CAND_REF)
650 return *result;
652 return (slsr_cand_t) NULL;
655 /* Add an entry to the statement-to-candidate mapping. */
657 static void
658 add_cand_for_stmt (gimple gs, slsr_cand_t c)
660 void **slot = pointer_map_insert (stmt_cand_map, gs);
661 gcc_assert (!*slot);
662 *slot = c;
665 /* Given PHI which contains a phi statement, determine whether it
666 satisfies all the requirements of a phi candidate. If so, create
667 a candidate. Note that a CAND_PHI never has a basis itself, but
668 is used to help find a basis for subsequent candidates. */
670 static void
671 slsr_process_phi (gimple phi, bool speed)
673 unsigned i;
674 tree arg0_base = NULL_TREE, base_type;
675 slsr_cand_t c;
676 struct loop *cand_loop = gimple_bb (phi)->loop_father;
677 unsigned savings = 0;
679 /* A CAND_PHI requires each of its arguments to have the same
680 derived base name. (See the module header commentary for a
681 definition of derived base names.) Furthermore, all feeding
682 definitions must be in the same position in the loop hierarchy
683 as PHI. */
685 for (i = 0; i < gimple_phi_num_args (phi); i++)
687 slsr_cand_t arg_cand;
688 tree arg = gimple_phi_arg_def (phi, i);
689 tree derived_base_name = NULL_TREE;
690 gimple arg_stmt = NULL;
691 basic_block arg_bb = NULL;
693 if (TREE_CODE (arg) != SSA_NAME)
694 return;
696 arg_cand = base_cand_from_table (arg);
698 if (arg_cand)
700 while (arg_cand->kind != CAND_ADD && arg_cand->kind != CAND_PHI)
702 if (!arg_cand->next_interp)
703 return;
705 arg_cand = lookup_cand (arg_cand->next_interp);
708 if (!integer_onep (arg_cand->stride))
709 return;
711 derived_base_name = arg_cand->base_expr;
712 arg_stmt = arg_cand->cand_stmt;
713 arg_bb = gimple_bb (arg_stmt);
715 /* Gather potential dead code savings if the phi statement
716 can be removed later on. */
717 if (has_single_use (arg))
719 if (gimple_code (arg_stmt) == GIMPLE_PHI)
720 savings += arg_cand->dead_savings;
721 else
722 savings += stmt_cost (arg_stmt, speed);
725 else
727 derived_base_name = arg;
729 if (SSA_NAME_IS_DEFAULT_DEF (arg))
730 arg_bb = single_succ (ENTRY_BLOCK_PTR);
731 else
732 gimple_bb (SSA_NAME_DEF_STMT (arg));
735 if (!arg_bb || arg_bb->loop_father != cand_loop)
736 return;
738 if (i == 0)
739 arg0_base = derived_base_name;
740 else if (!operand_equal_p (derived_base_name, arg0_base, 0))
741 return;
744 /* Create the candidate. "alloc_cand_and_find_basis" is named
745 misleadingly for this case, as no basis will be sought for a
746 CAND_PHI. */
747 base_type = TREE_TYPE (arg0_base);
749 c = alloc_cand_and_find_basis (CAND_PHI, phi, arg0_base,
750 0, integer_one_node, base_type, savings);
752 /* Add the candidate to the statement-candidate mapping. */
753 add_cand_for_stmt (phi, c);
756 /* Given PBASE which is a pointer to tree, look up the defining
757 statement for it and check whether the candidate is in the
758 form of:
760 X = B + (1 * S), S is integer constant
761 X = B + (i * S), S is integer one
763 If so, set PBASE to the candidate's base_expr and return double
764 int (i * S).
765 Otherwise, just return double int zero. */
767 static widest_int
768 backtrace_base_for_ref (tree *pbase)
770 tree base_in = *pbase;
771 slsr_cand_t base_cand;
773 STRIP_NOPS (base_in);
775 /* Strip off widening conversion(s) to handle cases where
776 e.g. 'B' is widened from an 'int' in order to calculate
777 a 64-bit address. */
778 if (CONVERT_EXPR_P (base_in)
779 && legal_cast_p_1 (base_in, TREE_OPERAND (base_in, 0)))
780 base_in = get_unwidened (base_in, NULL_TREE);
782 if (TREE_CODE (base_in) != SSA_NAME)
783 return 0;
785 base_cand = base_cand_from_table (base_in);
787 while (base_cand && base_cand->kind != CAND_PHI)
789 if (base_cand->kind == CAND_ADD
790 && base_cand->index == 1
791 && TREE_CODE (base_cand->stride) == INTEGER_CST)
793 /* X = B + (1 * S), S is integer constant. */
794 *pbase = base_cand->base_expr;
795 return wi::to_widest (base_cand->stride);
797 else if (base_cand->kind == CAND_ADD
798 && TREE_CODE (base_cand->stride) == INTEGER_CST
799 && integer_onep (base_cand->stride))
801 /* X = B + (i * S), S is integer one. */
802 *pbase = base_cand->base_expr;
803 return base_cand->index;
806 if (base_cand->next_interp)
807 base_cand = lookup_cand (base_cand->next_interp);
808 else
809 base_cand = NULL;
812 return 0;
815 /* Look for the following pattern:
817 *PBASE: MEM_REF (T1, C1)
819 *POFFSET: MULT_EXPR (T2, C3) [C2 is zero]
821 MULT_EXPR (PLUS_EXPR (T2, C2), C3)
823 MULT_EXPR (MINUS_EXPR (T2, -C2), C3)
825 *PINDEX: C4 * BITS_PER_UNIT
827 If not present, leave the input values unchanged and return FALSE.
828 Otherwise, modify the input values as follows and return TRUE:
830 *PBASE: T1
831 *POFFSET: MULT_EXPR (T2, C3)
832 *PINDEX: C1 + (C2 * C3) + C4
834 When T2 is recorded by a CAND_ADD in the form of (T2' + C5), it
835 will be further restructured to:
837 *PBASE: T1
838 *POFFSET: MULT_EXPR (T2', C3)
839 *PINDEX: C1 + (C2 * C3) + C4 + (C5 * C3) */
841 static bool
842 restructure_reference (tree *pbase, tree *poffset, widest_int *pindex,
843 tree *ptype)
845 tree base = *pbase, offset = *poffset;
846 widest_int index = *pindex;
847 tree mult_op0, t1, t2, type;
848 widest_int c1, c2, c3, c4, c5;
850 if (!base
851 || !offset
852 || TREE_CODE (base) != MEM_REF
853 || TREE_CODE (offset) != MULT_EXPR
854 || TREE_CODE (TREE_OPERAND (offset, 1)) != INTEGER_CST
855 || wi::umod_floor (index, BITS_PER_UNIT) != 0)
856 return false;
858 t1 = TREE_OPERAND (base, 0);
859 c1 = widest_int::from (mem_ref_offset (base), SIGNED);
860 type = TREE_TYPE (TREE_OPERAND (base, 1));
862 mult_op0 = TREE_OPERAND (offset, 0);
863 c3 = wi::to_widest (TREE_OPERAND (offset, 1));
865 if (TREE_CODE (mult_op0) == PLUS_EXPR)
867 if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
869 t2 = TREE_OPERAND (mult_op0, 0);
870 c2 = wi::to_widest (TREE_OPERAND (mult_op0, 1));
872 else
873 return false;
875 else if (TREE_CODE (mult_op0) == MINUS_EXPR)
877 if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
879 t2 = TREE_OPERAND (mult_op0, 0);
880 c2 = -wi::to_widest (TREE_OPERAND (mult_op0, 1));
882 else
883 return false;
885 else
887 t2 = mult_op0;
888 c2 = 0;
891 c4 = wi::udiv_floor (index, BITS_PER_UNIT);
892 c5 = backtrace_base_for_ref (&t2);
894 *pbase = t1;
895 *poffset = fold_build2 (MULT_EXPR, sizetype, fold_convert (sizetype, t2),
896 wide_int_to_tree (sizetype, c3));
897 *pindex = c1 + c2 * c3 + c4 + c5 * c3;
898 *ptype = type;
900 return true;
903 /* Given GS which contains a data reference, create a CAND_REF entry in
904 the candidate table and attempt to find a basis. */
906 static void
907 slsr_process_ref (gimple gs)
909 tree ref_expr, base, offset, type;
910 HOST_WIDE_INT bitsize, bitpos;
911 enum machine_mode mode;
912 int unsignedp, volatilep;
913 slsr_cand_t c;
915 if (gimple_vdef (gs))
916 ref_expr = gimple_assign_lhs (gs);
917 else
918 ref_expr = gimple_assign_rhs1 (gs);
920 if (!handled_component_p (ref_expr)
921 || TREE_CODE (ref_expr) == BIT_FIELD_REF
922 || (TREE_CODE (ref_expr) == COMPONENT_REF
923 && DECL_BIT_FIELD (TREE_OPERAND (ref_expr, 1))))
924 return;
926 base = get_inner_reference (ref_expr, &bitsize, &bitpos, &offset, &mode,
927 &unsignedp, &volatilep, false);
928 widest_int index = bitpos;
930 if (!restructure_reference (&base, &offset, &index, &type))
931 return;
933 c = alloc_cand_and_find_basis (CAND_REF, gs, base, index, offset,
934 type, 0);
936 /* Add the candidate to the statement-candidate mapping. */
937 add_cand_for_stmt (gs, c);
940 /* Create a candidate entry for a statement GS, where GS multiplies
941 two SSA names BASE_IN and STRIDE_IN. Propagate any known information
942 about the two SSA names into the new candidate. Return the new
943 candidate. */
945 static slsr_cand_t
946 create_mul_ssa_cand (gimple gs, tree base_in, tree stride_in, bool speed)
948 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
949 widest_int index;
950 unsigned savings = 0;
951 slsr_cand_t c;
952 slsr_cand_t base_cand = base_cand_from_table (base_in);
954 /* Look at all interpretations of the base candidate, if necessary,
955 to find information to propagate into this candidate. */
956 while (base_cand && !base && base_cand->kind != CAND_PHI)
959 if (base_cand->kind == CAND_MULT && integer_onep (base_cand->stride))
961 /* Y = (B + i') * 1
962 X = Y * Z
963 ================
964 X = (B + i') * Z */
965 base = base_cand->base_expr;
966 index = base_cand->index;
967 stride = stride_in;
968 ctype = base_cand->cand_type;
969 if (has_single_use (base_in))
970 savings = (base_cand->dead_savings
971 + stmt_cost (base_cand->cand_stmt, speed));
973 else if (base_cand->kind == CAND_ADD
974 && TREE_CODE (base_cand->stride) == INTEGER_CST)
976 /* Y = B + (i' * S), S constant
977 X = Y * Z
978 ============================
979 X = B + ((i' * S) * Z) */
980 base = base_cand->base_expr;
981 index = base_cand->index * wi::to_widest (base_cand->stride);
982 stride = stride_in;
983 ctype = base_cand->cand_type;
984 if (has_single_use (base_in))
985 savings = (base_cand->dead_savings
986 + stmt_cost (base_cand->cand_stmt, speed));
989 if (base_cand->next_interp)
990 base_cand = lookup_cand (base_cand->next_interp);
991 else
992 base_cand = NULL;
995 if (!base)
997 /* No interpretations had anything useful to propagate, so
998 produce X = (Y + 0) * Z. */
999 base = base_in;
1000 index = 0;
1001 stride = stride_in;
1002 ctype = TREE_TYPE (base_in);
1005 c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
1006 ctype, savings);
1007 return c;
1010 /* Create a candidate entry for a statement GS, where GS multiplies
1011 SSA name BASE_IN by constant STRIDE_IN. Propagate any known
1012 information about BASE_IN into the new candidate. Return the new
1013 candidate. */
1015 static slsr_cand_t
1016 create_mul_imm_cand (gimple gs, tree base_in, tree stride_in, bool speed)
1018 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1019 widest_int index, temp;
1020 unsigned savings = 0;
1021 slsr_cand_t c;
1022 slsr_cand_t base_cand = base_cand_from_table (base_in);
1024 /* Look at all interpretations of the base candidate, if necessary,
1025 to find information to propagate into this candidate. */
1026 while (base_cand && !base && base_cand->kind != CAND_PHI)
1028 if (base_cand->kind == CAND_MULT
1029 && TREE_CODE (base_cand->stride) == INTEGER_CST)
1031 /* Y = (B + i') * S, S constant
1032 X = Y * c
1033 ============================
1034 X = (B + i') * (S * c) */
1035 base = base_cand->base_expr;
1036 index = base_cand->index;
1037 temp = wi::to_widest (base_cand->stride) * wi::to_widest (stride_in);
1038 stride = wide_int_to_tree (TREE_TYPE (stride_in), temp);
1039 ctype = base_cand->cand_type;
1040 if (has_single_use (base_in))
1041 savings = (base_cand->dead_savings
1042 + stmt_cost (base_cand->cand_stmt, speed));
1044 else if (base_cand->kind == CAND_ADD && integer_onep (base_cand->stride))
1046 /* Y = B + (i' * 1)
1047 X = Y * c
1048 ===========================
1049 X = (B + i') * c */
1050 base = base_cand->base_expr;
1051 index = base_cand->index;
1052 stride = stride_in;
1053 ctype = base_cand->cand_type;
1054 if (has_single_use (base_in))
1055 savings = (base_cand->dead_savings
1056 + stmt_cost (base_cand->cand_stmt, speed));
1058 else if (base_cand->kind == CAND_ADD
1059 && base_cand->index == 1
1060 && TREE_CODE (base_cand->stride) == INTEGER_CST)
1062 /* Y = B + (1 * S), S constant
1063 X = Y * c
1064 ===========================
1065 X = (B + S) * c */
1066 base = base_cand->base_expr;
1067 index = wi::to_widest (base_cand->stride);
1068 stride = stride_in;
1069 ctype = base_cand->cand_type;
1070 if (has_single_use (base_in))
1071 savings = (base_cand->dead_savings
1072 + stmt_cost (base_cand->cand_stmt, speed));
1075 if (base_cand->next_interp)
1076 base_cand = lookup_cand (base_cand->next_interp);
1077 else
1078 base_cand = NULL;
1081 if (!base)
1083 /* No interpretations had anything useful to propagate, so
1084 produce X = (Y + 0) * c. */
1085 base = base_in;
1086 index = 0;
1087 stride = stride_in;
1088 ctype = TREE_TYPE (base_in);
1091 c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
1092 ctype, savings);
1093 return c;
1096 /* Given GS which is a multiply of scalar integers, make an appropriate
1097 entry in the candidate table. If this is a multiply of two SSA names,
1098 create two CAND_MULT interpretations and attempt to find a basis for
1099 each of them. Otherwise, create a single CAND_MULT and attempt to
1100 find a basis. */
1102 static void
1103 slsr_process_mul (gimple gs, tree rhs1, tree rhs2, bool speed)
1105 slsr_cand_t c, c2;
1107 /* If this is a multiply of an SSA name with itself, it is highly
1108 unlikely that we will get a strength reduction opportunity, so
1109 don't record it as a candidate. This simplifies the logic for
1110 finding a basis, so if this is removed that must be considered. */
1111 if (rhs1 == rhs2)
1112 return;
1114 if (TREE_CODE (rhs2) == SSA_NAME)
1116 /* Record an interpretation of this statement in the candidate table
1117 assuming RHS1 is the base expression and RHS2 is the stride. */
1118 c = create_mul_ssa_cand (gs, rhs1, rhs2, speed);
1120 /* Add the first interpretation to the statement-candidate mapping. */
1121 add_cand_for_stmt (gs, c);
1123 /* Record another interpretation of this statement assuming RHS1
1124 is the stride and RHS2 is the base expression. */
1125 c2 = create_mul_ssa_cand (gs, rhs2, rhs1, speed);
1126 c->next_interp = c2->cand_num;
1128 else
1130 /* Record an interpretation for the multiply-immediate. */
1131 c = create_mul_imm_cand (gs, rhs1, rhs2, speed);
1133 /* Add the interpretation to the statement-candidate mapping. */
1134 add_cand_for_stmt (gs, c);
1138 /* Create a candidate entry for a statement GS, where GS adds two
1139 SSA names BASE_IN and ADDEND_IN if SUBTRACT_P is false, and
1140 subtracts ADDEND_IN from BASE_IN otherwise. Propagate any known
1141 information about the two SSA names into the new candidate.
1142 Return the new candidate. */
1144 static slsr_cand_t
1145 create_add_ssa_cand (gimple gs, tree base_in, tree addend_in,
1146 bool subtract_p, bool speed)
1148 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL;
1149 widest_int index;
1150 unsigned savings = 0;
1151 slsr_cand_t c;
1152 slsr_cand_t base_cand = base_cand_from_table (base_in);
1153 slsr_cand_t addend_cand = base_cand_from_table (addend_in);
1155 /* The most useful transformation is a multiply-immediate feeding
1156 an add or subtract. Look for that first. */
1157 while (addend_cand && !base && addend_cand->kind != CAND_PHI)
1159 if (addend_cand->kind == CAND_MULT
1160 && addend_cand->index == 0
1161 && TREE_CODE (addend_cand->stride) == INTEGER_CST)
1163 /* Z = (B + 0) * S, S constant
1164 X = Y +/- Z
1165 ===========================
1166 X = Y + ((+/-1 * S) * B) */
1167 base = base_in;
1168 index = wi::to_widest (addend_cand->stride);
1169 if (subtract_p)
1170 index = -index;
1171 stride = addend_cand->base_expr;
1172 ctype = TREE_TYPE (base_in);
1173 if (has_single_use (addend_in))
1174 savings = (addend_cand->dead_savings
1175 + stmt_cost (addend_cand->cand_stmt, speed));
1178 if (addend_cand->next_interp)
1179 addend_cand = lookup_cand (addend_cand->next_interp);
1180 else
1181 addend_cand = NULL;
1184 while (base_cand && !base && base_cand->kind != CAND_PHI)
1186 if (base_cand->kind == CAND_ADD
1187 && (base_cand->index == 0
1188 || operand_equal_p (base_cand->stride,
1189 integer_zero_node, 0)))
1191 /* Y = B + (i' * S), i' * S = 0
1192 X = Y +/- Z
1193 ============================
1194 X = B + (+/-1 * Z) */
1195 base = base_cand->base_expr;
1196 index = subtract_p ? -1 : 1;
1197 stride = addend_in;
1198 ctype = base_cand->cand_type;
1199 if (has_single_use (base_in))
1200 savings = (base_cand->dead_savings
1201 + stmt_cost (base_cand->cand_stmt, speed));
1203 else if (subtract_p)
1205 slsr_cand_t subtrahend_cand = base_cand_from_table (addend_in);
1207 while (subtrahend_cand && !base && subtrahend_cand->kind != CAND_PHI)
1209 if (subtrahend_cand->kind == CAND_MULT
1210 && subtrahend_cand->index == 0
1211 && TREE_CODE (subtrahend_cand->stride) == INTEGER_CST)
1213 /* Z = (B + 0) * S, S constant
1214 X = Y - Z
1215 ===========================
1216 Value: X = Y + ((-1 * S) * B) */
1217 base = base_in;
1218 index = wi::to_widest (subtrahend_cand->stride);
1219 index = -index;
1220 stride = subtrahend_cand->base_expr;
1221 ctype = TREE_TYPE (base_in);
1222 if (has_single_use (addend_in))
1223 savings = (subtrahend_cand->dead_savings
1224 + stmt_cost (subtrahend_cand->cand_stmt, speed));
1227 if (subtrahend_cand->next_interp)
1228 subtrahend_cand = lookup_cand (subtrahend_cand->next_interp);
1229 else
1230 subtrahend_cand = NULL;
1234 if (base_cand->next_interp)
1235 base_cand = lookup_cand (base_cand->next_interp);
1236 else
1237 base_cand = NULL;
1240 if (!base)
1242 /* No interpretations had anything useful to propagate, so
1243 produce X = Y + (1 * Z). */
1244 base = base_in;
1245 index = subtract_p ? -1 : 1;
1246 stride = addend_in;
1247 ctype = TREE_TYPE (base_in);
1250 c = alloc_cand_and_find_basis (CAND_ADD, gs, base, index, stride,
1251 ctype, savings);
1252 return c;
1255 /* Create a candidate entry for a statement GS, where GS adds SSA
1256 name BASE_IN to constant INDEX_IN. Propagate any known information
1257 about BASE_IN into the new candidate. Return the new candidate. */
1259 static slsr_cand_t
1260 create_add_imm_cand (gimple gs, tree base_in, const widest_int &index_in,
1261 bool speed)
1263 enum cand_kind kind = CAND_ADD;
1264 tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
1265 widest_int index, multiple;
1266 unsigned savings = 0;
1267 slsr_cand_t c;
1268 slsr_cand_t base_cand = base_cand_from_table (base_in);
1270 while (base_cand && !base && base_cand->kind != CAND_PHI)
1272 signop sign = TYPE_SIGN (TREE_TYPE (base_cand->stride));
1274 if (TREE_CODE (base_cand->stride) == INTEGER_CST
1275 && wi::multiple_of_p (index_in, wi::to_widest (base_cand->stride),
1276 sign, &multiple))
1278 /* Y = (B + i') * S, S constant, c = kS for some integer k
1279 X = Y + c
1280 ============================
1281 X = (B + (i'+ k)) * S
1283 Y = B + (i' * S), S constant, c = kS for some integer k
1284 X = Y + c
1285 ============================
1286 X = (B + (i'+ k)) * S */
1287 kind = base_cand->kind;
1288 base = base_cand->base_expr;
1289 index = base_cand->index + multiple;
1290 stride = base_cand->stride;
1291 ctype = base_cand->cand_type;
1292 if (has_single_use (base_in))
1293 savings = (base_cand->dead_savings
1294 + stmt_cost (base_cand->cand_stmt, speed));
1297 if (base_cand->next_interp)
1298 base_cand = lookup_cand (base_cand->next_interp);
1299 else
1300 base_cand = NULL;
1303 if (!base)
1305 /* No interpretations had anything useful to propagate, so
1306 produce X = Y + (c * 1). */
1307 kind = CAND_ADD;
1308 base = base_in;
1309 index = index_in;
1310 stride = integer_one_node;
1311 ctype = TREE_TYPE (base_in);
1314 c = alloc_cand_and_find_basis (kind, gs, base, index, stride,
1315 ctype, savings);
1316 return c;
1319 /* Given GS which is an add or subtract of scalar integers or pointers,
1320 make at least one appropriate entry in the candidate table. */
1322 static void
1323 slsr_process_add (gimple gs, tree rhs1, tree rhs2, bool speed)
1325 bool subtract_p = gimple_assign_rhs_code (gs) == MINUS_EXPR;
1326 slsr_cand_t c = NULL, c2;
1328 if (TREE_CODE (rhs2) == SSA_NAME)
1330 /* First record an interpretation assuming RHS1 is the base expression
1331 and RHS2 is the stride. But it doesn't make sense for the
1332 stride to be a pointer, so don't record a candidate in that case. */
1333 if (!POINTER_TYPE_P (TREE_TYPE (rhs2)))
1335 c = create_add_ssa_cand (gs, rhs1, rhs2, subtract_p, speed);
1337 /* Add the first interpretation to the statement-candidate
1338 mapping. */
1339 add_cand_for_stmt (gs, c);
1342 /* If the two RHS operands are identical, or this is a subtract,
1343 we're done. */
1344 if (operand_equal_p (rhs1, rhs2, 0) || subtract_p)
1345 return;
1347 /* Otherwise, record another interpretation assuming RHS2 is the
1348 base expression and RHS1 is the stride, again provided that the
1349 stride is not a pointer. */
1350 if (!POINTER_TYPE_P (TREE_TYPE (rhs1)))
1352 c2 = create_add_ssa_cand (gs, rhs2, rhs1, false, speed);
1353 if (c)
1354 c->next_interp = c2->cand_num;
1355 else
1356 add_cand_for_stmt (gs, c2);
1359 else
1361 /* Record an interpretation for the add-immediate. */
1362 widest_int index = wi::to_widest (rhs2);
1363 if (subtract_p)
1364 index = -index;
1366 c = create_add_imm_cand (gs, rhs1, index, speed);
1368 /* Add the interpretation to the statement-candidate mapping. */
1369 add_cand_for_stmt (gs, c);
1373 /* Given GS which is a negate of a scalar integer, make an appropriate
1374 entry in the candidate table. A negate is equivalent to a multiply
1375 by -1. */
1377 static void
1378 slsr_process_neg (gimple gs, tree rhs1, bool speed)
1380 /* Record a CAND_MULT interpretation for the multiply by -1. */
1381 slsr_cand_t c = create_mul_imm_cand (gs, rhs1, integer_minus_one_node, speed);
1383 /* Add the interpretation to the statement-candidate mapping. */
1384 add_cand_for_stmt (gs, c);
1387 /* Help function for legal_cast_p, operating on two trees. Checks
1388 whether it's allowable to cast from RHS to LHS. See legal_cast_p
1389 for more details. */
1391 static bool
1392 legal_cast_p_1 (tree lhs, tree rhs)
1394 tree lhs_type, rhs_type;
1395 unsigned lhs_size, rhs_size;
1396 bool lhs_wraps, rhs_wraps;
1398 lhs_type = TREE_TYPE (lhs);
1399 rhs_type = TREE_TYPE (rhs);
1400 lhs_size = TYPE_PRECISION (lhs_type);
1401 rhs_size = TYPE_PRECISION (rhs_type);
1402 lhs_wraps = TYPE_OVERFLOW_WRAPS (lhs_type);
1403 rhs_wraps = TYPE_OVERFLOW_WRAPS (rhs_type);
1405 if (lhs_size < rhs_size
1406 || (rhs_wraps && !lhs_wraps)
1407 || (rhs_wraps && lhs_wraps && rhs_size != lhs_size))
1408 return false;
1410 return true;
1413 /* Return TRUE if GS is a statement that defines an SSA name from
1414 a conversion and is legal for us to combine with an add and multiply
1415 in the candidate table. For example, suppose we have:
1417 A = B + i;
1418 C = (type) A;
1419 D = C * S;
1421 Without the type-cast, we would create a CAND_MULT for D with base B,
1422 index i, and stride S. We want to record this candidate only if it
1423 is equivalent to apply the type cast following the multiply:
1425 A = B + i;
1426 E = A * S;
1427 D = (type) E;
1429 We will record the type with the candidate for D. This allows us
1430 to use a similar previous candidate as a basis. If we have earlier seen
1432 A' = B + i';
1433 C' = (type) A';
1434 D' = C' * S;
1436 we can replace D with
1438 D = D' + (i - i') * S;
1440 But if moving the type-cast would change semantics, we mustn't do this.
1442 This is legitimate for casts from a non-wrapping integral type to
1443 any integral type of the same or larger size. It is not legitimate
1444 to convert a wrapping type to a non-wrapping type, or to a wrapping
1445 type of a different size. I.e., with a wrapping type, we must
1446 assume that the addition B + i could wrap, in which case performing
1447 the multiply before or after one of the "illegal" type casts will
1448 have different semantics. */
1450 static bool
1451 legal_cast_p (gimple gs, tree rhs)
1453 if (!is_gimple_assign (gs)
1454 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs)))
1455 return false;
1457 return legal_cast_p_1 (gimple_assign_lhs (gs), rhs);
1460 /* Given GS which is a cast to a scalar integer type, determine whether
1461 the cast is legal for strength reduction. If so, make at least one
1462 appropriate entry in the candidate table. */
1464 static void
1465 slsr_process_cast (gimple gs, tree rhs1, bool speed)
1467 tree lhs, ctype;
1468 slsr_cand_t base_cand, c, c2;
1469 unsigned savings = 0;
1471 if (!legal_cast_p (gs, rhs1))
1472 return;
1474 lhs = gimple_assign_lhs (gs);
1475 base_cand = base_cand_from_table (rhs1);
1476 ctype = TREE_TYPE (lhs);
1478 if (base_cand && base_cand->kind != CAND_PHI)
1480 while (base_cand)
1482 /* Propagate all data from the base candidate except the type,
1483 which comes from the cast, and the base candidate's cast,
1484 which is no longer applicable. */
1485 if (has_single_use (rhs1))
1486 savings = (base_cand->dead_savings
1487 + stmt_cost (base_cand->cand_stmt, speed));
1489 c = alloc_cand_and_find_basis (base_cand->kind, gs,
1490 base_cand->base_expr,
1491 base_cand->index, base_cand->stride,
1492 ctype, savings);
1493 if (base_cand->next_interp)
1494 base_cand = lookup_cand (base_cand->next_interp);
1495 else
1496 base_cand = NULL;
1499 else
1501 /* If nothing is known about the RHS, create fresh CAND_ADD and
1502 CAND_MULT interpretations:
1504 X = Y + (0 * 1)
1505 X = (Y + 0) * 1
1507 The first of these is somewhat arbitrary, but the choice of
1508 1 for the stride simplifies the logic for propagating casts
1509 into their uses. */
1510 c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1,
1511 0, integer_one_node, ctype, 0);
1512 c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1,
1513 0, integer_one_node, ctype, 0);
1514 c->next_interp = c2->cand_num;
1517 /* Add the first (or only) interpretation to the statement-candidate
1518 mapping. */
1519 add_cand_for_stmt (gs, c);
1522 /* Given GS which is a copy of a scalar integer type, make at least one
1523 appropriate entry in the candidate table.
1525 This interface is included for completeness, but is unnecessary
1526 if this pass immediately follows a pass that performs copy
1527 propagation, such as DOM. */
1529 static void
1530 slsr_process_copy (gimple gs, tree rhs1, bool speed)
1532 slsr_cand_t base_cand, c, c2;
1533 unsigned savings = 0;
1535 base_cand = base_cand_from_table (rhs1);
1537 if (base_cand && base_cand->kind != CAND_PHI)
1539 while (base_cand)
1541 /* Propagate all data from the base candidate. */
1542 if (has_single_use (rhs1))
1543 savings = (base_cand->dead_savings
1544 + stmt_cost (base_cand->cand_stmt, speed));
1546 c = alloc_cand_and_find_basis (base_cand->kind, gs,
1547 base_cand->base_expr,
1548 base_cand->index, base_cand->stride,
1549 base_cand->cand_type, savings);
1550 if (base_cand->next_interp)
1551 base_cand = lookup_cand (base_cand->next_interp);
1552 else
1553 base_cand = NULL;
1556 else
1558 /* If nothing is known about the RHS, create fresh CAND_ADD and
1559 CAND_MULT interpretations:
1561 X = Y + (0 * 1)
1562 X = (Y + 0) * 1
1564 The first of these is somewhat arbitrary, but the choice of
1565 1 for the stride simplifies the logic for propagating casts
1566 into their uses. */
1567 c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1,
1568 0, integer_one_node, TREE_TYPE (rhs1), 0);
1569 c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1,
1570 0, integer_one_node, TREE_TYPE (rhs1), 0);
1571 c->next_interp = c2->cand_num;
1574 /* Add the first (or only) interpretation to the statement-candidate
1575 mapping. */
1576 add_cand_for_stmt (gs, c);
1579 class find_candidates_dom_walker : public dom_walker
1581 public:
1582 find_candidates_dom_walker (cdi_direction direction)
1583 : dom_walker (direction) {}
1584 virtual void before_dom_children (basic_block);
1587 /* Find strength-reduction candidates in block BB. */
1589 void
1590 find_candidates_dom_walker::before_dom_children (basic_block bb)
1592 bool speed = optimize_bb_for_speed_p (bb);
1593 gimple_stmt_iterator gsi;
1595 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1596 slsr_process_phi (gsi_stmt (gsi), speed);
1598 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1600 gimple gs = gsi_stmt (gsi);
1602 if (gimple_vuse (gs) && gimple_assign_single_p (gs))
1603 slsr_process_ref (gs);
1605 else if (is_gimple_assign (gs)
1606 && SCALAR_INT_MODE_P
1607 (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))))
1609 tree rhs1 = NULL_TREE, rhs2 = NULL_TREE;
1611 switch (gimple_assign_rhs_code (gs))
1613 case MULT_EXPR:
1614 case PLUS_EXPR:
1615 rhs1 = gimple_assign_rhs1 (gs);
1616 rhs2 = gimple_assign_rhs2 (gs);
1617 /* Should never happen, but currently some buggy situations
1618 in earlier phases put constants in rhs1. */
1619 if (TREE_CODE (rhs1) != SSA_NAME)
1620 continue;
1621 break;
1623 /* Possible future opportunity: rhs1 of a ptr+ can be
1624 an ADDR_EXPR. */
1625 case POINTER_PLUS_EXPR:
1626 case MINUS_EXPR:
1627 rhs2 = gimple_assign_rhs2 (gs);
1628 /* Fall-through. */
1630 case NOP_EXPR:
1631 case MODIFY_EXPR:
1632 case NEGATE_EXPR:
1633 rhs1 = gimple_assign_rhs1 (gs);
1634 if (TREE_CODE (rhs1) != SSA_NAME)
1635 continue;
1636 break;
1638 default:
1642 switch (gimple_assign_rhs_code (gs))
1644 case MULT_EXPR:
1645 slsr_process_mul (gs, rhs1, rhs2, speed);
1646 break;
1648 case PLUS_EXPR:
1649 case POINTER_PLUS_EXPR:
1650 case MINUS_EXPR:
1651 slsr_process_add (gs, rhs1, rhs2, speed);
1652 break;
1654 case NEGATE_EXPR:
1655 slsr_process_neg (gs, rhs1, speed);
1656 break;
1658 case NOP_EXPR:
1659 slsr_process_cast (gs, rhs1, speed);
1660 break;
1662 case MODIFY_EXPR:
1663 slsr_process_copy (gs, rhs1, speed);
1664 break;
1666 default:
1673 /* Dump a candidate for debug. */
1675 static void
1676 dump_candidate (slsr_cand_t c)
1678 fprintf (dump_file, "%3d [%d] ", c->cand_num,
1679 gimple_bb (c->cand_stmt)->index);
1680 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
1681 switch (c->kind)
1683 case CAND_MULT:
1684 fputs (" MULT : (", dump_file);
1685 print_generic_expr (dump_file, c->base_expr, 0);
1686 fputs (" + ", dump_file);
1687 print_decs (c->index, dump_file);
1688 fputs (") * ", dump_file);
1689 print_generic_expr (dump_file, c->stride, 0);
1690 fputs (" : ", dump_file);
1691 break;
1692 case CAND_ADD:
1693 fputs (" ADD : ", dump_file);
1694 print_generic_expr (dump_file, c->base_expr, 0);
1695 fputs (" + (", dump_file);
1696 print_decs (c->index, dump_file);
1697 fputs (" * ", dump_file);
1698 print_generic_expr (dump_file, c->stride, 0);
1699 fputs (") : ", dump_file);
1700 break;
1701 case CAND_REF:
1702 fputs (" REF : ", dump_file);
1703 print_generic_expr (dump_file, c->base_expr, 0);
1704 fputs (" + (", dump_file);
1705 print_generic_expr (dump_file, c->stride, 0);
1706 fputs (") + ", dump_file);
1707 print_decs (c->index, dump_file);
1708 fputs (" : ", dump_file);
1709 break;
1710 case CAND_PHI:
1711 fputs (" PHI : ", dump_file);
1712 print_generic_expr (dump_file, c->base_expr, 0);
1713 fputs (" + (unknown * ", dump_file);
1714 print_generic_expr (dump_file, c->stride, 0);
1715 fputs (") : ", dump_file);
1716 break;
1717 default:
1718 gcc_unreachable ();
1720 print_generic_expr (dump_file, c->cand_type, 0);
1721 fprintf (dump_file, "\n basis: %d dependent: %d sibling: %d\n",
1722 c->basis, c->dependent, c->sibling);
1723 fprintf (dump_file, " next-interp: %d dead-savings: %d\n",
1724 c->next_interp, c->dead_savings);
1725 if (c->def_phi)
1726 fprintf (dump_file, " phi: %d\n", c->def_phi);
1727 fputs ("\n", dump_file);
1730 /* Dump the candidate vector for debug. */
1732 static void
1733 dump_cand_vec (void)
1735 unsigned i;
1736 slsr_cand_t c;
1738 fprintf (dump_file, "\nStrength reduction candidate vector:\n\n");
1740 FOR_EACH_VEC_ELT (cand_vec, i, c)
1741 dump_candidate (c);
1744 /* Callback used to dump the candidate chains hash table. */
1747 ssa_base_cand_dump_callback (cand_chain **slot, void *ignored ATTRIBUTE_UNUSED)
1749 const_cand_chain_t chain = *slot;
1750 cand_chain_t p;
1752 print_generic_expr (dump_file, chain->base_expr, 0);
1753 fprintf (dump_file, " -> %d", chain->cand->cand_num);
1755 for (p = chain->next; p; p = p->next)
1756 fprintf (dump_file, " -> %d", p->cand->cand_num);
1758 fputs ("\n", dump_file);
1759 return 1;
1762 /* Dump the candidate chains. */
1764 static void
1765 dump_cand_chains (void)
1767 fprintf (dump_file, "\nStrength reduction candidate chains:\n\n");
1768 base_cand_map.traverse_noresize <void *, ssa_base_cand_dump_callback> (NULL);
1769 fputs ("\n", dump_file);
1772 /* Dump the increment vector for debug. */
1774 static void
1775 dump_incr_vec (void)
1777 if (dump_file && (dump_flags & TDF_DETAILS))
1779 unsigned i;
1781 fprintf (dump_file, "\nIncrement vector:\n\n");
1783 for (i = 0; i < incr_vec_len; i++)
1785 fprintf (dump_file, "%3d increment: ", i);
1786 print_decs (incr_vec[i].incr, dump_file);
1787 fprintf (dump_file, "\n count: %d", incr_vec[i].count);
1788 fprintf (dump_file, "\n cost: %d", incr_vec[i].cost);
1789 fputs ("\n initializer: ", dump_file);
1790 print_generic_expr (dump_file, incr_vec[i].initializer, 0);
1791 fputs ("\n\n", dump_file);
1796 /* Replace *EXPR in candidate C with an equivalent strength-reduced
1797 data reference. */
1799 static void
1800 replace_ref (tree *expr, slsr_cand_t c)
1802 tree add_expr, mem_ref, acc_type = TREE_TYPE (*expr);
1803 unsigned HOST_WIDE_INT misalign;
1804 unsigned align;
1806 /* Ensure the memory reference carries the minimum alignment
1807 requirement for the data type. See PR58041. */
1808 get_object_alignment_1 (*expr, &align, &misalign);
1809 if (misalign != 0)
1810 align = (misalign & -misalign);
1811 if (align < TYPE_ALIGN (acc_type))
1812 acc_type = build_aligned_type (acc_type, align);
1814 add_expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (c->base_expr),
1815 c->base_expr, c->stride);
1816 mem_ref = fold_build2 (MEM_REF, acc_type, add_expr,
1817 wide_int_to_tree (c->cand_type, c->index));
1819 /* Gimplify the base addressing expression for the new MEM_REF tree. */
1820 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
1821 TREE_OPERAND (mem_ref, 0)
1822 = force_gimple_operand_gsi (&gsi, TREE_OPERAND (mem_ref, 0),
1823 /*simple_p=*/true, NULL,
1824 /*before=*/true, GSI_SAME_STMT);
1825 copy_ref_info (mem_ref, *expr);
1826 *expr = mem_ref;
1827 update_stmt (c->cand_stmt);
1830 /* Replace CAND_REF candidate C, each sibling of candidate C, and each
1831 dependent of candidate C with an equivalent strength-reduced data
1832 reference. */
1834 static void
1835 replace_refs (slsr_cand_t c)
1837 if (gimple_vdef (c->cand_stmt))
1839 tree *lhs = gimple_assign_lhs_ptr (c->cand_stmt);
1840 replace_ref (lhs, c);
1842 else
1844 tree *rhs = gimple_assign_rhs1_ptr (c->cand_stmt);
1845 replace_ref (rhs, c);
1848 if (c->sibling)
1849 replace_refs (lookup_cand (c->sibling));
1851 if (c->dependent)
1852 replace_refs (lookup_cand (c->dependent));
1855 /* Return TRUE if candidate C is dependent upon a PHI. */
1857 static bool
1858 phi_dependent_cand_p (slsr_cand_t c)
1860 /* A candidate is not necessarily dependent upon a PHI just because
1861 it has a phi definition for its base name. It may have a basis
1862 that relies upon the same phi definition, in which case the PHI
1863 is irrelevant to this candidate. */
1864 return (c->def_phi
1865 && c->basis
1866 && lookup_cand (c->basis)->def_phi != c->def_phi);
1869 /* Calculate the increment required for candidate C relative to
1870 its basis. */
1872 static widest_int
1873 cand_increment (slsr_cand_t c)
1875 slsr_cand_t basis;
1877 /* If the candidate doesn't have a basis, just return its own
1878 index. This is useful in record_increments to help us find
1879 an existing initializer. Also, if the candidate's basis is
1880 hidden by a phi, then its own index will be the increment
1881 from the newly introduced phi basis. */
1882 if (!c->basis || phi_dependent_cand_p (c))
1883 return c->index;
1885 basis = lookup_cand (c->basis);
1886 gcc_assert (operand_equal_p (c->base_expr, basis->base_expr, 0));
1887 return c->index - basis->index;
1890 /* Calculate the increment required for candidate C relative to
1891 its basis. If we aren't going to generate pointer arithmetic
1892 for this candidate, return the absolute value of that increment
1893 instead. */
1895 static inline widest_int
1896 cand_abs_increment (slsr_cand_t c)
1898 widest_int increment = cand_increment (c);
1900 if (!address_arithmetic_p && wi::neg_p (increment))
1901 increment = -increment;
1903 return increment;
1906 /* Return TRUE iff candidate C has already been replaced under
1907 another interpretation. */
1909 static inline bool
1910 cand_already_replaced (slsr_cand_t c)
1912 return (gimple_bb (c->cand_stmt) == 0);
1915 /* Common logic used by replace_unconditional_candidate and
1916 replace_conditional_candidate. */
1918 static void
1919 replace_mult_candidate (slsr_cand_t c, tree basis_name, widest_int bump)
1921 tree target_type = TREE_TYPE (gimple_assign_lhs (c->cand_stmt));
1922 enum tree_code cand_code = gimple_assign_rhs_code (c->cand_stmt);
1924 /* It is highly unlikely, but possible, that the resulting
1925 bump doesn't fit in a HWI. Abandon the replacement
1926 in this case. This does not affect siblings or dependents
1927 of C. Restriction to signed HWI is conservative for unsigned
1928 types but allows for safe negation without twisted logic. */
1929 if (wi::fits_shwi_p (bump)
1930 && bump.to_shwi () != HOST_WIDE_INT_MIN
1931 /* It is not useful to replace casts, copies, or adds of
1932 an SSA name and a constant. */
1933 && cand_code != MODIFY_EXPR
1934 && cand_code != NOP_EXPR
1935 && cand_code != PLUS_EXPR
1936 && cand_code != POINTER_PLUS_EXPR
1937 && cand_code != MINUS_EXPR)
1939 enum tree_code code = PLUS_EXPR;
1940 tree bump_tree;
1941 gimple stmt_to_print = NULL;
1943 /* If the basis name and the candidate's LHS have incompatible
1944 types, introduce a cast. */
1945 if (!useless_type_conversion_p (target_type, TREE_TYPE (basis_name)))
1946 basis_name = introduce_cast_before_cand (c, target_type, basis_name);
1947 if (wi::neg_p (bump))
1949 code = MINUS_EXPR;
1950 bump = -bump;
1953 bump_tree = wide_int_to_tree (target_type, bump);
1955 if (dump_file && (dump_flags & TDF_DETAILS))
1957 fputs ("Replacing: ", dump_file);
1958 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
1961 if (bump == 0)
1963 tree lhs = gimple_assign_lhs (c->cand_stmt);
1964 gimple copy_stmt = gimple_build_assign (lhs, basis_name);
1965 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
1966 gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
1967 gsi_replace (&gsi, copy_stmt, false);
1968 c->cand_stmt = copy_stmt;
1969 if (dump_file && (dump_flags & TDF_DETAILS))
1970 stmt_to_print = copy_stmt;
1972 else
1974 tree rhs1, rhs2;
1975 if (cand_code != NEGATE_EXPR) {
1976 rhs1 = gimple_assign_rhs1 (c->cand_stmt);
1977 rhs2 = gimple_assign_rhs2 (c->cand_stmt);
1979 if (cand_code != NEGATE_EXPR
1980 && ((operand_equal_p (rhs1, basis_name, 0)
1981 && operand_equal_p (rhs2, bump_tree, 0))
1982 || (operand_equal_p (rhs1, bump_tree, 0)
1983 && operand_equal_p (rhs2, basis_name, 0))))
1985 if (dump_file && (dump_flags & TDF_DETAILS))
1987 fputs ("(duplicate, not actually replacing)", dump_file);
1988 stmt_to_print = c->cand_stmt;
1991 else
1993 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
1994 gimple_assign_set_rhs_with_ops (&gsi, code,
1995 basis_name, bump_tree);
1996 update_stmt (gsi_stmt (gsi));
1997 c->cand_stmt = gsi_stmt (gsi);
1998 if (dump_file && (dump_flags & TDF_DETAILS))
1999 stmt_to_print = gsi_stmt (gsi);
2003 if (dump_file && (dump_flags & TDF_DETAILS))
2005 fputs ("With: ", dump_file);
2006 print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
2007 fputs ("\n", dump_file);
2012 /* Replace candidate C with an add or subtract. Note that we only
2013 operate on CAND_MULTs with known strides, so we will never generate
2014 a POINTER_PLUS_EXPR. Each candidate X = (B + i) * S is replaced by
2015 X = Y + ((i - i') * S), as described in the module commentary. The
2016 folded value ((i - i') * S) is referred to here as the "bump." */
2018 static void
2019 replace_unconditional_candidate (slsr_cand_t c)
2021 slsr_cand_t basis;
2023 if (cand_already_replaced (c))
2024 return;
2026 basis = lookup_cand (c->basis);
2027 widest_int bump = cand_increment (c) * wi::to_widest (c->stride);
2029 replace_mult_candidate (c, gimple_assign_lhs (basis->cand_stmt), bump);
2032 /* Return the index in the increment vector of the given INCREMENT,
2033 or -1 if not found. The latter can occur if more than
2034 MAX_INCR_VEC_LEN increments have been found. */
2036 static inline int
2037 incr_vec_index (widest_int increment)
2039 unsigned i;
2041 for (i = 0; i < incr_vec_len && increment != incr_vec[i].incr; i++)
2044 if (i < incr_vec_len)
2045 return i;
2046 else
2047 return -1;
2050 /* Create a new statement along edge E to add BASIS_NAME to the product
2051 of INCREMENT and the stride of candidate C. Create and return a new
2052 SSA name from *VAR to be used as the LHS of the new statement.
2053 KNOWN_STRIDE is true iff C's stride is a constant. */
2055 static tree
2056 create_add_on_incoming_edge (slsr_cand_t c, tree basis_name,
2057 widest_int increment, edge e, location_t loc,
2058 bool known_stride)
2060 basic_block insert_bb;
2061 gimple_stmt_iterator gsi;
2062 tree lhs, basis_type;
2063 gimple new_stmt;
2065 /* If the add candidate along this incoming edge has the same
2066 index as C's hidden basis, the hidden basis represents this
2067 edge correctly. */
2068 if (increment == 0)
2069 return basis_name;
2071 basis_type = TREE_TYPE (basis_name);
2072 lhs = make_temp_ssa_name (basis_type, NULL, "slsr");
2074 if (known_stride)
2076 tree bump_tree;
2077 enum tree_code code = PLUS_EXPR;
2078 widest_int bump = increment * wi::to_widest (c->stride);
2079 if (wi::neg_p (bump))
2081 code = MINUS_EXPR;
2082 bump = -bump;
2085 bump_tree = wide_int_to_tree (basis_type, bump);
2086 new_stmt = gimple_build_assign_with_ops (code, lhs, basis_name,
2087 bump_tree);
2089 else
2091 int i;
2092 bool negate_incr = (!address_arithmetic_p && wi::neg_p (increment));
2093 i = incr_vec_index (negate_incr ? -increment : increment);
2094 gcc_assert (i >= 0);
2096 if (incr_vec[i].initializer)
2098 enum tree_code code = negate_incr ? MINUS_EXPR : PLUS_EXPR;
2099 new_stmt = gimple_build_assign_with_ops (code, lhs, basis_name,
2100 incr_vec[i].initializer);
2102 else if (increment == 1)
2103 new_stmt = gimple_build_assign_with_ops (PLUS_EXPR, lhs, basis_name,
2104 c->stride);
2105 else if (increment == -1)
2106 new_stmt = gimple_build_assign_with_ops (MINUS_EXPR, lhs, basis_name,
2107 c->stride);
2108 else
2109 gcc_unreachable ();
2112 insert_bb = single_succ_p (e->src) ? e->src : split_edge (e);
2113 gsi = gsi_last_bb (insert_bb);
2115 if (!gsi_end_p (gsi) && is_ctrl_stmt (gsi_stmt (gsi)))
2116 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
2117 else
2118 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
2120 gimple_set_location (new_stmt, loc);
2122 if (dump_file && (dump_flags & TDF_DETAILS))
2124 fprintf (dump_file, "Inserting in block %d: ", insert_bb->index);
2125 print_gimple_stmt (dump_file, new_stmt, 0, 0);
2128 return lhs;
2131 /* Given a candidate C with BASIS_NAME being the LHS of C's basis which
2132 is hidden by the phi node FROM_PHI, create a new phi node in the same
2133 block as FROM_PHI. The new phi is suitable for use as a basis by C,
2134 with its phi arguments representing conditional adjustments to the
2135 hidden basis along conditional incoming paths. Those adjustments are
2136 made by creating add statements (and sometimes recursively creating
2137 phis) along those incoming paths. LOC is the location to attach to
2138 the introduced statements. KNOWN_STRIDE is true iff C's stride is a
2139 constant. */
2141 static tree
2142 create_phi_basis (slsr_cand_t c, gimple from_phi, tree basis_name,
2143 location_t loc, bool known_stride)
2145 int i;
2146 tree name, phi_arg;
2147 gimple phi;
2148 vec<tree> phi_args;
2149 slsr_cand_t basis = lookup_cand (c->basis);
2150 int nargs = gimple_phi_num_args (from_phi);
2151 basic_block phi_bb = gimple_bb (from_phi);
2152 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (from_phi));
2153 phi_args.create (nargs);
2155 /* Process each argument of the existing phi that represents
2156 conditionally-executed add candidates. */
2157 for (i = 0; i < nargs; i++)
2159 edge e = (*phi_bb->preds)[i];
2160 tree arg = gimple_phi_arg_def (from_phi, i);
2161 tree feeding_def;
2163 /* If the phi argument is the base name of the CAND_PHI, then
2164 this incoming arc should use the hidden basis. */
2165 if (operand_equal_p (arg, phi_cand->base_expr, 0))
2166 if (basis->index == 0)
2167 feeding_def = gimple_assign_lhs (basis->cand_stmt);
2168 else
2170 widest_int incr = -basis->index;
2171 feeding_def = create_add_on_incoming_edge (c, basis_name, incr,
2172 e, loc, known_stride);
2174 else
2176 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2178 /* If there is another phi along this incoming edge, we must
2179 process it in the same fashion to ensure that all basis
2180 adjustments are made along its incoming edges. */
2181 if (gimple_code (arg_def) == GIMPLE_PHI)
2182 feeding_def = create_phi_basis (c, arg_def, basis_name,
2183 loc, known_stride);
2184 else
2186 slsr_cand_t arg_cand = base_cand_from_table (arg);
2187 widest_int diff = arg_cand->index - basis->index;
2188 feeding_def = create_add_on_incoming_edge (c, basis_name, diff,
2189 e, loc, known_stride);
2193 /* Because of recursion, we need to save the arguments in a vector
2194 so we can create the PHI statement all at once. Otherwise the
2195 storage for the half-created PHI can be reclaimed. */
2196 phi_args.safe_push (feeding_def);
2199 /* Create the new phi basis. */
2200 name = make_temp_ssa_name (TREE_TYPE (basis_name), NULL, "slsr");
2201 phi = create_phi_node (name, phi_bb);
2202 SSA_NAME_DEF_STMT (name) = phi;
2204 FOR_EACH_VEC_ELT (phi_args, i, phi_arg)
2206 edge e = (*phi_bb->preds)[i];
2207 add_phi_arg (phi, phi_arg, e, loc);
2210 update_stmt (phi);
2212 if (dump_file && (dump_flags & TDF_DETAILS))
2214 fputs ("Introducing new phi basis: ", dump_file);
2215 print_gimple_stmt (dump_file, phi, 0, 0);
2218 return name;
2221 /* Given a candidate C whose basis is hidden by at least one intervening
2222 phi, introduce a matching number of new phis to represent its basis
2223 adjusted by conditional increments along possible incoming paths. Then
2224 replace C as though it were an unconditional candidate, using the new
2225 basis. */
2227 static void
2228 replace_conditional_candidate (slsr_cand_t c)
2230 tree basis_name, name;
2231 slsr_cand_t basis;
2232 location_t loc;
2234 /* Look up the LHS SSA name from C's basis. This will be the
2235 RHS1 of the adds we will introduce to create new phi arguments. */
2236 basis = lookup_cand (c->basis);
2237 basis_name = gimple_assign_lhs (basis->cand_stmt);
2239 /* Create a new phi statement which will represent C's true basis
2240 after the transformation is complete. */
2241 loc = gimple_location (c->cand_stmt);
2242 name = create_phi_basis (c, lookup_cand (c->def_phi)->cand_stmt,
2243 basis_name, loc, KNOWN_STRIDE);
2244 /* Replace C with an add of the new basis phi and a constant. */
2245 widest_int bump = c->index * wi::to_widest (c->stride);
2247 replace_mult_candidate (c, name, bump);
2250 /* Compute the expected costs of inserting basis adjustments for
2251 candidate C with phi-definition PHI. The cost of inserting
2252 one adjustment is given by ONE_ADD_COST. If PHI has arguments
2253 which are themselves phi results, recursively calculate costs
2254 for those phis as well. */
2256 static int
2257 phi_add_costs (gimple phi, slsr_cand_t c, int one_add_cost)
2259 unsigned i;
2260 int cost = 0;
2261 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2263 /* If we work our way back to a phi that isn't dominated by the hidden
2264 basis, this isn't a candidate for replacement. Indicate this by
2265 returning an unreasonably high cost. It's not easy to detect
2266 these situations when determining the basis, so we defer the
2267 decision until now. */
2268 basic_block phi_bb = gimple_bb (phi);
2269 slsr_cand_t basis = lookup_cand (c->basis);
2270 basic_block basis_bb = gimple_bb (basis->cand_stmt);
2272 if (phi_bb == basis_bb || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
2273 return COST_INFINITE;
2275 for (i = 0; i < gimple_phi_num_args (phi); i++)
2277 tree arg = gimple_phi_arg_def (phi, i);
2279 if (arg != phi_cand->base_expr)
2281 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2283 if (gimple_code (arg_def) == GIMPLE_PHI)
2284 cost += phi_add_costs (arg_def, c, one_add_cost);
2285 else
2287 slsr_cand_t arg_cand = base_cand_from_table (arg);
2289 if (arg_cand->index != c->index)
2290 cost += one_add_cost;
2295 return cost;
2298 /* For candidate C, each sibling of candidate C, and each dependent of
2299 candidate C, determine whether the candidate is dependent upon a
2300 phi that hides its basis. If not, replace the candidate unconditionally.
2301 Otherwise, determine whether the cost of introducing compensation code
2302 for the candidate is offset by the gains from strength reduction. If
2303 so, replace the candidate and introduce the compensation code. */
2305 static void
2306 replace_uncond_cands_and_profitable_phis (slsr_cand_t c)
2308 if (phi_dependent_cand_p (c))
2310 if (c->kind == CAND_MULT)
2312 /* A candidate dependent upon a phi will replace a multiply by
2313 a constant with an add, and will insert at most one add for
2314 each phi argument. Add these costs with the potential dead-code
2315 savings to determine profitability. */
2316 bool speed = optimize_bb_for_speed_p (gimple_bb (c->cand_stmt));
2317 int mult_savings = stmt_cost (c->cand_stmt, speed);
2318 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
2319 tree phi_result = gimple_phi_result (phi);
2320 int one_add_cost = add_cost (speed,
2321 TYPE_MODE (TREE_TYPE (phi_result)));
2322 int add_costs = one_add_cost + phi_add_costs (phi, c, one_add_cost);
2323 int cost = add_costs - mult_savings - c->dead_savings;
2325 if (dump_file && (dump_flags & TDF_DETAILS))
2327 fprintf (dump_file, " Conditional candidate %d:\n", c->cand_num);
2328 fprintf (dump_file, " add_costs = %d\n", add_costs);
2329 fprintf (dump_file, " mult_savings = %d\n", mult_savings);
2330 fprintf (dump_file, " dead_savings = %d\n", c->dead_savings);
2331 fprintf (dump_file, " cost = %d\n", cost);
2332 if (cost <= COST_NEUTRAL)
2333 fputs (" Replacing...\n", dump_file);
2334 else
2335 fputs (" Not replaced.\n", dump_file);
2338 if (cost <= COST_NEUTRAL)
2339 replace_conditional_candidate (c);
2342 else
2343 replace_unconditional_candidate (c);
2345 if (c->sibling)
2346 replace_uncond_cands_and_profitable_phis (lookup_cand (c->sibling));
2348 if (c->dependent)
2349 replace_uncond_cands_and_profitable_phis (lookup_cand (c->dependent));
2352 /* Count the number of candidates in the tree rooted at C that have
2353 not already been replaced under other interpretations. */
2355 static int
2356 count_candidates (slsr_cand_t c)
2358 unsigned count = cand_already_replaced (c) ? 0 : 1;
2360 if (c->sibling)
2361 count += count_candidates (lookup_cand (c->sibling));
2363 if (c->dependent)
2364 count += count_candidates (lookup_cand (c->dependent));
2366 return count;
2369 /* Increase the count of INCREMENT by one in the increment vector.
2370 INCREMENT is associated with candidate C. If INCREMENT is to be
2371 conditionally executed as part of a conditional candidate replacement,
2372 IS_PHI_ADJUST is true, otherwise false. If an initializer
2373 T_0 = stride * I is provided by a candidate that dominates all
2374 candidates with the same increment, also record T_0 for subsequent use. */
2376 static void
2377 record_increment (slsr_cand_t c, widest_int increment, bool is_phi_adjust)
2379 bool found = false;
2380 unsigned i;
2382 /* Treat increments that differ only in sign as identical so as to
2383 share initializers, unless we are generating pointer arithmetic. */
2384 if (!address_arithmetic_p && wi::neg_p (increment))
2385 increment = -increment;
2387 for (i = 0; i < incr_vec_len; i++)
2389 if (incr_vec[i].incr == increment)
2391 incr_vec[i].count++;
2392 found = true;
2394 /* If we previously recorded an initializer that doesn't
2395 dominate this candidate, it's not going to be useful to
2396 us after all. */
2397 if (incr_vec[i].initializer
2398 && !dominated_by_p (CDI_DOMINATORS,
2399 gimple_bb (c->cand_stmt),
2400 incr_vec[i].init_bb))
2402 incr_vec[i].initializer = NULL_TREE;
2403 incr_vec[i].init_bb = NULL;
2406 break;
2410 if (!found && incr_vec_len < MAX_INCR_VEC_LEN - 1)
2412 /* The first time we see an increment, create the entry for it.
2413 If this is the root candidate which doesn't have a basis, set
2414 the count to zero. We're only processing it so it can possibly
2415 provide an initializer for other candidates. */
2416 incr_vec[incr_vec_len].incr = increment;
2417 incr_vec[incr_vec_len].count = c->basis || is_phi_adjust ? 1 : 0;
2418 incr_vec[incr_vec_len].cost = COST_INFINITE;
2420 /* Optimistically record the first occurrence of this increment
2421 as providing an initializer (if it does); we will revise this
2422 opinion later if it doesn't dominate all other occurrences.
2423 Exception: increments of -1, 0, 1 never need initializers;
2424 and phi adjustments don't ever provide initializers. */
2425 if (c->kind == CAND_ADD
2426 && !is_phi_adjust
2427 && c->index == increment
2428 && (wi::gts_p (increment, 1)
2429 || wi::lts_p (increment, -1))
2430 && (gimple_assign_rhs_code (c->cand_stmt) == PLUS_EXPR
2431 || gimple_assign_rhs_code (c->cand_stmt) == POINTER_PLUS_EXPR))
2433 tree t0 = NULL_TREE;
2434 tree rhs1 = gimple_assign_rhs1 (c->cand_stmt);
2435 tree rhs2 = gimple_assign_rhs2 (c->cand_stmt);
2436 if (operand_equal_p (rhs1, c->base_expr, 0))
2437 t0 = rhs2;
2438 else if (operand_equal_p (rhs2, c->base_expr, 0))
2439 t0 = rhs1;
2440 if (t0
2441 && SSA_NAME_DEF_STMT (t0)
2442 && gimple_bb (SSA_NAME_DEF_STMT (t0)))
2444 incr_vec[incr_vec_len].initializer = t0;
2445 incr_vec[incr_vec_len++].init_bb
2446 = gimple_bb (SSA_NAME_DEF_STMT (t0));
2448 else
2450 incr_vec[incr_vec_len].initializer = NULL_TREE;
2451 incr_vec[incr_vec_len++].init_bb = NULL;
2454 else
2456 incr_vec[incr_vec_len].initializer = NULL_TREE;
2457 incr_vec[incr_vec_len++].init_bb = NULL;
2462 /* Given phi statement PHI that hides a candidate from its BASIS, find
2463 the increments along each incoming arc (recursively handling additional
2464 phis that may be present) and record them. These increments are the
2465 difference in index between the index-adjusting statements and the
2466 index of the basis. */
2468 static void
2469 record_phi_increments (slsr_cand_t basis, gimple phi)
2471 unsigned i;
2472 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2474 for (i = 0; i < gimple_phi_num_args (phi); i++)
2476 tree arg = gimple_phi_arg_def (phi, i);
2478 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
2480 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2482 if (gimple_code (arg_def) == GIMPLE_PHI)
2483 record_phi_increments (basis, arg_def);
2484 else
2486 slsr_cand_t arg_cand = base_cand_from_table (arg);
2487 widest_int diff = arg_cand->index - basis->index;
2488 record_increment (arg_cand, diff, PHI_ADJUST);
2494 /* Determine how many times each unique increment occurs in the set
2495 of candidates rooted at C's parent, recording the data in the
2496 increment vector. For each unique increment I, if an initializer
2497 T_0 = stride * I is provided by a candidate that dominates all
2498 candidates with the same increment, also record T_0 for subsequent
2499 use. */
2501 static void
2502 record_increments (slsr_cand_t c)
2504 if (!cand_already_replaced (c))
2506 if (!phi_dependent_cand_p (c))
2507 record_increment (c, cand_increment (c), NOT_PHI_ADJUST);
2508 else
2510 /* A candidate with a basis hidden by a phi will have one
2511 increment for its relationship to the index represented by
2512 the phi, and potentially additional increments along each
2513 incoming edge. For the root of the dependency tree (which
2514 has no basis), process just the initial index in case it has
2515 an initializer that can be used by subsequent candidates. */
2516 record_increment (c, c->index, NOT_PHI_ADJUST);
2518 if (c->basis)
2519 record_phi_increments (lookup_cand (c->basis),
2520 lookup_cand (c->def_phi)->cand_stmt);
2524 if (c->sibling)
2525 record_increments (lookup_cand (c->sibling));
2527 if (c->dependent)
2528 record_increments (lookup_cand (c->dependent));
2531 /* Add up and return the costs of introducing add statements that
2532 require the increment INCR on behalf of candidate C and phi
2533 statement PHI. Accumulate into *SAVINGS the potential savings
2534 from removing existing statements that feed PHI and have no other
2535 uses. */
2537 static int
2538 phi_incr_cost (slsr_cand_t c, const widest_int &incr, gimple phi, int *savings)
2540 unsigned i;
2541 int cost = 0;
2542 slsr_cand_t basis = lookup_cand (c->basis);
2543 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2545 for (i = 0; i < gimple_phi_num_args (phi); i++)
2547 tree arg = gimple_phi_arg_def (phi, i);
2549 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
2551 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2553 if (gimple_code (arg_def) == GIMPLE_PHI)
2555 int feeding_savings = 0;
2556 cost += phi_incr_cost (c, incr, arg_def, &feeding_savings);
2557 if (has_single_use (gimple_phi_result (arg_def)))
2558 *savings += feeding_savings;
2560 else
2562 slsr_cand_t arg_cand = base_cand_from_table (arg);
2563 widest_int diff = arg_cand->index - basis->index;
2565 if (incr == diff)
2567 tree basis_lhs = gimple_assign_lhs (basis->cand_stmt);
2568 tree lhs = gimple_assign_lhs (arg_cand->cand_stmt);
2569 cost += add_cost (true, TYPE_MODE (TREE_TYPE (basis_lhs)));
2570 if (has_single_use (lhs))
2571 *savings += stmt_cost (arg_cand->cand_stmt, true);
2577 return cost;
2580 /* Return the first candidate in the tree rooted at C that has not
2581 already been replaced, favoring siblings over dependents. */
2583 static slsr_cand_t
2584 unreplaced_cand_in_tree (slsr_cand_t c)
2586 if (!cand_already_replaced (c))
2587 return c;
2589 if (c->sibling)
2591 slsr_cand_t sib = unreplaced_cand_in_tree (lookup_cand (c->sibling));
2592 if (sib)
2593 return sib;
2596 if (c->dependent)
2598 slsr_cand_t dep = unreplaced_cand_in_tree (lookup_cand (c->dependent));
2599 if (dep)
2600 return dep;
2603 return NULL;
2606 /* Return TRUE if the candidates in the tree rooted at C should be
2607 optimized for speed, else FALSE. We estimate this based on the block
2608 containing the most dominant candidate in the tree that has not yet
2609 been replaced. */
2611 static bool
2612 optimize_cands_for_speed_p (slsr_cand_t c)
2614 slsr_cand_t c2 = unreplaced_cand_in_tree (c);
2615 gcc_assert (c2);
2616 return optimize_bb_for_speed_p (gimple_bb (c2->cand_stmt));
2619 /* Add COST_IN to the lowest cost of any dependent path starting at
2620 candidate C or any of its siblings, counting only candidates along
2621 such paths with increment INCR. Assume that replacing a candidate
2622 reduces cost by REPL_SAVINGS. Also account for savings from any
2623 statements that would go dead. If COUNT_PHIS is true, include
2624 costs of introducing feeding statements for conditional candidates. */
2626 static int
2627 lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c,
2628 const widest_int &incr, bool count_phis)
2630 int local_cost, sib_cost, savings = 0;
2631 widest_int cand_incr = cand_abs_increment (c);
2633 if (cand_already_replaced (c))
2634 local_cost = cost_in;
2635 else if (incr == cand_incr)
2636 local_cost = cost_in - repl_savings - c->dead_savings;
2637 else
2638 local_cost = cost_in - c->dead_savings;
2640 if (count_phis
2641 && phi_dependent_cand_p (c)
2642 && !cand_already_replaced (c))
2644 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
2645 local_cost += phi_incr_cost (c, incr, phi, &savings);
2647 if (has_single_use (gimple_phi_result (phi)))
2648 local_cost -= savings;
2651 if (c->dependent)
2652 local_cost = lowest_cost_path (local_cost, repl_savings,
2653 lookup_cand (c->dependent), incr,
2654 count_phis);
2656 if (c->sibling)
2658 sib_cost = lowest_cost_path (cost_in, repl_savings,
2659 lookup_cand (c->sibling), incr,
2660 count_phis);
2661 local_cost = MIN (local_cost, sib_cost);
2664 return local_cost;
2667 /* Compute the total savings that would accrue from all replacements
2668 in the candidate tree rooted at C, counting only candidates with
2669 increment INCR. Assume that replacing a candidate reduces cost
2670 by REPL_SAVINGS. Also account for savings from statements that
2671 would go dead. */
2673 static int
2674 total_savings (int repl_savings, slsr_cand_t c, const widest_int &incr,
2675 bool count_phis)
2677 int savings = 0;
2678 widest_int cand_incr = cand_abs_increment (c);
2680 if (incr == cand_incr && !cand_already_replaced (c))
2681 savings += repl_savings + c->dead_savings;
2683 if (count_phis
2684 && phi_dependent_cand_p (c)
2685 && !cand_already_replaced (c))
2687 int phi_savings = 0;
2688 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
2689 savings -= phi_incr_cost (c, incr, phi, &phi_savings);
2691 if (has_single_use (gimple_phi_result (phi)))
2692 savings += phi_savings;
2695 if (c->dependent)
2696 savings += total_savings (repl_savings, lookup_cand (c->dependent), incr,
2697 count_phis);
2699 if (c->sibling)
2700 savings += total_savings (repl_savings, lookup_cand (c->sibling), incr,
2701 count_phis);
2703 return savings;
2706 /* Use target-specific costs to determine and record which increments
2707 in the current candidate tree are profitable to replace, assuming
2708 MODE and SPEED. FIRST_DEP is the first dependent of the root of
2709 the candidate tree.
2711 One slight limitation here is that we don't account for the possible
2712 introduction of casts in some cases. See replace_one_candidate for
2713 the cases where these are introduced. This should probably be cleaned
2714 up sometime. */
2716 static void
2717 analyze_increments (slsr_cand_t first_dep, enum machine_mode mode, bool speed)
2719 unsigned i;
2721 for (i = 0; i < incr_vec_len; i++)
2723 HOST_WIDE_INT incr = incr_vec[i].incr.to_shwi ();
2725 /* If somehow this increment is bigger than a HWI, we won't
2726 be optimizing candidates that use it. And if the increment
2727 has a count of zero, nothing will be done with it. */
2728 if (!wi::fits_shwi_p (incr_vec[i].incr) || !incr_vec[i].count)
2729 incr_vec[i].cost = COST_INFINITE;
2731 /* Increments of 0, 1, and -1 are always profitable to replace,
2732 because they always replace a multiply or add with an add or
2733 copy, and may cause one or more existing instructions to go
2734 dead. Exception: -1 can't be assumed to be profitable for
2735 pointer addition. */
2736 else if (incr == 0
2737 || incr == 1
2738 || (incr == -1
2739 && (gimple_assign_rhs_code (first_dep->cand_stmt)
2740 != POINTER_PLUS_EXPR)))
2741 incr_vec[i].cost = COST_NEUTRAL;
2743 /* FORNOW: If we need to add an initializer, give up if a cast from
2744 the candidate's type to its stride's type can lose precision.
2745 This could eventually be handled better by expressly retaining the
2746 result of a cast to a wider type in the stride. Example:
2748 short int _1;
2749 _2 = (int) _1;
2750 _3 = _2 * 10;
2751 _4 = x + _3; ADD: x + (10 * _1) : int
2752 _5 = _2 * 15;
2753 _6 = x + _3; ADD: x + (15 * _1) : int
2755 Right now replacing _6 would cause insertion of an initializer
2756 of the form "short int T = _1 * 5;" followed by a cast to
2757 int, which could overflow incorrectly. Had we recorded _2 or
2758 (int)_1 as the stride, this wouldn't happen. However, doing
2759 this breaks other opportunities, so this will require some
2760 care. */
2761 else if (!incr_vec[i].initializer
2762 && TREE_CODE (first_dep->stride) != INTEGER_CST
2763 && !legal_cast_p_1 (first_dep->stride,
2764 gimple_assign_lhs (first_dep->cand_stmt)))
2766 incr_vec[i].cost = COST_INFINITE;
2768 /* If we need to add an initializer, make sure we don't introduce
2769 a multiply by a pointer type, which can happen in certain cast
2770 scenarios. FIXME: When cleaning up these cast issues, we can
2771 afford to introduce the multiply provided we cast out to an
2772 unsigned int of appropriate size. */
2773 else if (!incr_vec[i].initializer
2774 && TREE_CODE (first_dep->stride) != INTEGER_CST
2775 && POINTER_TYPE_P (TREE_TYPE (first_dep->stride)))
2777 incr_vec[i].cost = COST_INFINITE;
2779 /* For any other increment, if this is a multiply candidate, we
2780 must introduce a temporary T and initialize it with
2781 T_0 = stride * increment. When optimizing for speed, walk the
2782 candidate tree to calculate the best cost reduction along any
2783 path; if it offsets the fixed cost of inserting the initializer,
2784 replacing the increment is profitable. When optimizing for
2785 size, instead calculate the total cost reduction from replacing
2786 all candidates with this increment. */
2787 else if (first_dep->kind == CAND_MULT)
2789 int cost = mult_by_coeff_cost (incr, mode, speed);
2790 int repl_savings = mul_cost (speed, mode) - add_cost (speed, mode);
2791 if (speed)
2792 cost = lowest_cost_path (cost, repl_savings, first_dep,
2793 incr_vec[i].incr, COUNT_PHIS);
2794 else
2795 cost -= total_savings (repl_savings, first_dep, incr_vec[i].incr,
2796 COUNT_PHIS);
2798 incr_vec[i].cost = cost;
2801 /* If this is an add candidate, the initializer may already
2802 exist, so only calculate the cost of the initializer if it
2803 doesn't. We are replacing one add with another here, so the
2804 known replacement savings is zero. We will account for removal
2805 of dead instructions in lowest_cost_path or total_savings. */
2806 else
2808 int cost = 0;
2809 if (!incr_vec[i].initializer)
2810 cost = mult_by_coeff_cost (incr, mode, speed);
2812 if (speed)
2813 cost = lowest_cost_path (cost, 0, first_dep, incr_vec[i].incr,
2814 DONT_COUNT_PHIS);
2815 else
2816 cost -= total_savings (0, first_dep, incr_vec[i].incr,
2817 DONT_COUNT_PHIS);
2819 incr_vec[i].cost = cost;
2824 /* Return the nearest common dominator of BB1 and BB2. If the blocks
2825 are identical, return the earlier of C1 and C2 in *WHERE. Otherwise,
2826 if the NCD matches BB1, return C1 in *WHERE; if the NCD matches BB2,
2827 return C2 in *WHERE; and if the NCD matches neither, return NULL in
2828 *WHERE. Note: It is possible for one of C1 and C2 to be NULL. */
2830 static basic_block
2831 ncd_for_two_cands (basic_block bb1, basic_block bb2,
2832 slsr_cand_t c1, slsr_cand_t c2, slsr_cand_t *where)
2834 basic_block ncd;
2836 if (!bb1)
2838 *where = c2;
2839 return bb2;
2842 if (!bb2)
2844 *where = c1;
2845 return bb1;
2848 ncd = nearest_common_dominator (CDI_DOMINATORS, bb1, bb2);
2850 /* If both candidates are in the same block, the earlier
2851 candidate wins. */
2852 if (bb1 == ncd && bb2 == ncd)
2854 if (!c1 || (c2 && c2->cand_num < c1->cand_num))
2855 *where = c2;
2856 else
2857 *where = c1;
2860 /* Otherwise, if one of them produced a candidate in the
2861 dominator, that one wins. */
2862 else if (bb1 == ncd)
2863 *where = c1;
2865 else if (bb2 == ncd)
2866 *where = c2;
2868 /* If neither matches the dominator, neither wins. */
2869 else
2870 *where = NULL;
2872 return ncd;
2875 /* Consider all candidates that feed PHI. Find the nearest common
2876 dominator of those candidates requiring the given increment INCR.
2877 Further find and return the nearest common dominator of this result
2878 with block NCD. If the returned block contains one or more of the
2879 candidates, return the earliest candidate in the block in *WHERE. */
2881 static basic_block
2882 ncd_with_phi (slsr_cand_t c, const widest_int &incr, gimple phi,
2883 basic_block ncd, slsr_cand_t *where)
2885 unsigned i;
2886 slsr_cand_t basis = lookup_cand (c->basis);
2887 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
2889 for (i = 0; i < gimple_phi_num_args (phi); i++)
2891 tree arg = gimple_phi_arg_def (phi, i);
2893 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
2895 gimple arg_def = SSA_NAME_DEF_STMT (arg);
2897 if (gimple_code (arg_def) == GIMPLE_PHI)
2898 ncd = ncd_with_phi (c, incr, arg_def, ncd, where);
2899 else
2901 slsr_cand_t arg_cand = base_cand_from_table (arg);
2902 widest_int diff = arg_cand->index - basis->index;
2904 if ((incr == diff) || (!address_arithmetic_p && incr == -diff))
2905 ncd = ncd_for_two_cands (ncd, gimple_bb (arg_cand->cand_stmt),
2906 *where, arg_cand, where);
2911 return ncd;
2914 /* Consider the candidate C together with any candidates that feed
2915 C's phi dependence (if any). Find and return the nearest common
2916 dominator of those candidates requiring the given increment INCR.
2917 If the returned block contains one or more of the candidates,
2918 return the earliest candidate in the block in *WHERE. */
2920 static basic_block
2921 ncd_of_cand_and_phis (slsr_cand_t c, const widest_int &incr, slsr_cand_t *where)
2923 basic_block ncd = NULL;
2925 if (cand_abs_increment (c) == incr)
2927 ncd = gimple_bb (c->cand_stmt);
2928 *where = c;
2931 if (phi_dependent_cand_p (c))
2932 ncd = ncd_with_phi (c, incr, lookup_cand (c->def_phi)->cand_stmt,
2933 ncd, where);
2935 return ncd;
2938 /* Consider all candidates in the tree rooted at C for which INCR
2939 represents the required increment of C relative to its basis.
2940 Find and return the basic block that most nearly dominates all
2941 such candidates. If the returned block contains one or more of
2942 the candidates, return the earliest candidate in the block in
2943 *WHERE. */
2945 static basic_block
2946 nearest_common_dominator_for_cands (slsr_cand_t c, const widest_int &incr,
2947 slsr_cand_t *where)
2949 basic_block sib_ncd = NULL, dep_ncd = NULL, this_ncd = NULL, ncd;
2950 slsr_cand_t sib_where = NULL, dep_where = NULL, this_where = NULL, new_where;
2952 /* First find the NCD of all siblings and dependents. */
2953 if (c->sibling)
2954 sib_ncd = nearest_common_dominator_for_cands (lookup_cand (c->sibling),
2955 incr, &sib_where);
2956 if (c->dependent)
2957 dep_ncd = nearest_common_dominator_for_cands (lookup_cand (c->dependent),
2958 incr, &dep_where);
2959 if (!sib_ncd && !dep_ncd)
2961 new_where = NULL;
2962 ncd = NULL;
2964 else if (sib_ncd && !dep_ncd)
2966 new_where = sib_where;
2967 ncd = sib_ncd;
2969 else if (dep_ncd && !sib_ncd)
2971 new_where = dep_where;
2972 ncd = dep_ncd;
2974 else
2975 ncd = ncd_for_two_cands (sib_ncd, dep_ncd, sib_where,
2976 dep_where, &new_where);
2978 /* If the candidate's increment doesn't match the one we're interested
2979 in (and nor do any increments for feeding defs of a phi-dependence),
2980 then the result depends only on siblings and dependents. */
2981 this_ncd = ncd_of_cand_and_phis (c, incr, &this_where);
2983 if (!this_ncd || cand_already_replaced (c))
2985 *where = new_where;
2986 return ncd;
2989 /* Otherwise, compare this candidate with the result from all siblings
2990 and dependents. */
2991 ncd = ncd_for_two_cands (ncd, this_ncd, new_where, this_where, where);
2993 return ncd;
2996 /* Return TRUE if the increment indexed by INDEX is profitable to replace. */
2998 static inline bool
2999 profitable_increment_p (unsigned index)
3001 return (incr_vec[index].cost <= COST_NEUTRAL);
3004 /* For each profitable increment in the increment vector not equal to
3005 0 or 1 (or -1, for non-pointer arithmetic), find the nearest common
3006 dominator of all statements in the candidate chain rooted at C
3007 that require that increment, and insert an initializer
3008 T_0 = stride * increment at that location. Record T_0 with the
3009 increment record. */
3011 static void
3012 insert_initializers (slsr_cand_t c)
3014 unsigned i;
3016 for (i = 0; i < incr_vec_len; i++)
3018 basic_block bb;
3019 slsr_cand_t where = NULL;
3020 gimple init_stmt;
3021 tree stride_type, new_name, incr_tree;
3022 widest_int incr = incr_vec[i].incr;
3024 if (!profitable_increment_p (i)
3025 || incr == 1
3026 || (incr == -1
3027 && gimple_assign_rhs_code (c->cand_stmt) != POINTER_PLUS_EXPR)
3028 || incr == 0)
3029 continue;
3031 /* We may have already identified an existing initializer that
3032 will suffice. */
3033 if (incr_vec[i].initializer)
3035 if (dump_file && (dump_flags & TDF_DETAILS))
3037 fputs ("Using existing initializer: ", dump_file);
3038 print_gimple_stmt (dump_file,
3039 SSA_NAME_DEF_STMT (incr_vec[i].initializer),
3040 0, 0);
3042 continue;
3045 /* Find the block that most closely dominates all candidates
3046 with this increment. If there is at least one candidate in
3047 that block, the earliest one will be returned in WHERE. */
3048 bb = nearest_common_dominator_for_cands (c, incr, &where);
3050 /* Create a new SSA name to hold the initializer's value. */
3051 stride_type = TREE_TYPE (c->stride);
3052 new_name = make_temp_ssa_name (stride_type, NULL, "slsr");
3053 incr_vec[i].initializer = new_name;
3055 /* Create the initializer and insert it in the latest possible
3056 dominating position. */
3057 incr_tree = wide_int_to_tree (stride_type, incr);
3058 init_stmt = gimple_build_assign_with_ops (MULT_EXPR, new_name,
3059 c->stride, incr_tree);
3060 if (where)
3062 gimple_stmt_iterator gsi = gsi_for_stmt (where->cand_stmt);
3063 gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
3064 gimple_set_location (init_stmt, gimple_location (where->cand_stmt));
3066 else
3068 gimple_stmt_iterator gsi = gsi_last_bb (bb);
3069 gimple basis_stmt = lookup_cand (c->basis)->cand_stmt;
3071 if (!gsi_end_p (gsi) && is_ctrl_stmt (gsi_stmt (gsi)))
3072 gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
3073 else
3074 gsi_insert_after (&gsi, init_stmt, GSI_SAME_STMT);
3076 gimple_set_location (init_stmt, gimple_location (basis_stmt));
3079 if (dump_file && (dump_flags & TDF_DETAILS))
3081 fputs ("Inserting initializer: ", dump_file);
3082 print_gimple_stmt (dump_file, init_stmt, 0, 0);
3087 /* Return TRUE iff all required increments for candidates feeding PHI
3088 are profitable to replace on behalf of candidate C. */
3090 static bool
3091 all_phi_incrs_profitable (slsr_cand_t c, gimple phi)
3093 unsigned i;
3094 slsr_cand_t basis = lookup_cand (c->basis);
3095 slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
3097 for (i = 0; i < gimple_phi_num_args (phi); i++)
3099 tree arg = gimple_phi_arg_def (phi, i);
3101 if (!operand_equal_p (arg, phi_cand->base_expr, 0))
3103 gimple arg_def = SSA_NAME_DEF_STMT (arg);
3105 if (gimple_code (arg_def) == GIMPLE_PHI)
3107 if (!all_phi_incrs_profitable (c, arg_def))
3108 return false;
3110 else
3112 int j;
3113 slsr_cand_t arg_cand = base_cand_from_table (arg);
3114 widest_int increment = arg_cand->index - basis->index;
3116 if (!address_arithmetic_p && wi::neg_p (increment))
3117 increment = -increment;
3119 j = incr_vec_index (increment);
3121 if (dump_file && (dump_flags & TDF_DETAILS))
3123 fprintf (dump_file, " Conditional candidate %d, phi: ",
3124 c->cand_num);
3125 print_gimple_stmt (dump_file, phi, 0, 0);
3126 fputs (" increment: ", dump_file);
3127 print_decs (increment, dump_file);
3128 if (j < 0)
3129 fprintf (dump_file,
3130 "\n Not replaced; incr_vec overflow.\n");
3131 else {
3132 fprintf (dump_file, "\n cost: %d\n", incr_vec[j].cost);
3133 if (profitable_increment_p (j))
3134 fputs (" Replacing...\n", dump_file);
3135 else
3136 fputs (" Not replaced.\n", dump_file);
3140 if (j < 0 || !profitable_increment_p (j))
3141 return false;
3146 return true;
3149 /* Create a NOP_EXPR that copies FROM_EXPR into a new SSA name of
3150 type TO_TYPE, and insert it in front of the statement represented
3151 by candidate C. Use *NEW_VAR to create the new SSA name. Return
3152 the new SSA name. */
3154 static tree
3155 introduce_cast_before_cand (slsr_cand_t c, tree to_type, tree from_expr)
3157 tree cast_lhs;
3158 gimple cast_stmt;
3159 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3161 cast_lhs = make_temp_ssa_name (to_type, NULL, "slsr");
3162 cast_stmt = gimple_build_assign_with_ops (NOP_EXPR, cast_lhs,
3163 from_expr, NULL_TREE);
3164 gimple_set_location (cast_stmt, gimple_location (c->cand_stmt));
3165 gsi_insert_before (&gsi, cast_stmt, GSI_SAME_STMT);
3167 if (dump_file && (dump_flags & TDF_DETAILS))
3169 fputs (" Inserting: ", dump_file);
3170 print_gimple_stmt (dump_file, cast_stmt, 0, 0);
3173 return cast_lhs;
3176 /* Replace the RHS of the statement represented by candidate C with
3177 NEW_CODE, NEW_RHS1, and NEW_RHS2, provided that to do so doesn't
3178 leave C unchanged or just interchange its operands. The original
3179 operation and operands are in OLD_CODE, OLD_RHS1, and OLD_RHS2.
3180 If the replacement was made and we are doing a details dump,
3181 return the revised statement, else NULL. */
3183 static gimple
3184 replace_rhs_if_not_dup (enum tree_code new_code, tree new_rhs1, tree new_rhs2,
3185 enum tree_code old_code, tree old_rhs1, tree old_rhs2,
3186 slsr_cand_t c)
3188 if (new_code != old_code
3189 || ((!operand_equal_p (new_rhs1, old_rhs1, 0)
3190 || !operand_equal_p (new_rhs2, old_rhs2, 0))
3191 && (!operand_equal_p (new_rhs1, old_rhs2, 0)
3192 || !operand_equal_p (new_rhs2, old_rhs1, 0))))
3194 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3195 gimple_assign_set_rhs_with_ops (&gsi, new_code, new_rhs1, new_rhs2);
3196 update_stmt (gsi_stmt (gsi));
3197 c->cand_stmt = gsi_stmt (gsi);
3199 if (dump_file && (dump_flags & TDF_DETAILS))
3200 return gsi_stmt (gsi);
3203 else if (dump_file && (dump_flags & TDF_DETAILS))
3204 fputs (" (duplicate, not actually replacing)\n", dump_file);
3206 return NULL;
3209 /* Strength-reduce the statement represented by candidate C by replacing
3210 it with an equivalent addition or subtraction. I is the index into
3211 the increment vector identifying C's increment. NEW_VAR is used to
3212 create a new SSA name if a cast needs to be introduced. BASIS_NAME
3213 is the rhs1 to use in creating the add/subtract. */
3215 static void
3216 replace_one_candidate (slsr_cand_t c, unsigned i, tree basis_name)
3218 gimple stmt_to_print = NULL;
3219 tree orig_rhs1, orig_rhs2;
3220 tree rhs2;
3221 enum tree_code orig_code, repl_code;
3222 widest_int cand_incr;
3224 orig_code = gimple_assign_rhs_code (c->cand_stmt);
3225 orig_rhs1 = gimple_assign_rhs1 (c->cand_stmt);
3226 orig_rhs2 = gimple_assign_rhs2 (c->cand_stmt);
3227 cand_incr = cand_increment (c);
3229 if (dump_file && (dump_flags & TDF_DETAILS))
3231 fputs ("Replacing: ", dump_file);
3232 print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
3233 stmt_to_print = c->cand_stmt;
3236 if (address_arithmetic_p)
3237 repl_code = POINTER_PLUS_EXPR;
3238 else
3239 repl_code = PLUS_EXPR;
3241 /* If the increment has an initializer T_0, replace the candidate
3242 statement with an add of the basis name and the initializer. */
3243 if (incr_vec[i].initializer)
3245 tree init_type = TREE_TYPE (incr_vec[i].initializer);
3246 tree orig_type = TREE_TYPE (orig_rhs2);
3248 if (types_compatible_p (orig_type, init_type))
3249 rhs2 = incr_vec[i].initializer;
3250 else
3251 rhs2 = introduce_cast_before_cand (c, orig_type,
3252 incr_vec[i].initializer);
3254 if (incr_vec[i].incr != cand_incr)
3256 gcc_assert (repl_code == PLUS_EXPR);
3257 repl_code = MINUS_EXPR;
3260 stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
3261 orig_code, orig_rhs1, orig_rhs2,
3265 /* Otherwise, the increment is one of -1, 0, and 1. Replace
3266 with a subtract of the stride from the basis name, a copy
3267 from the basis name, or an add of the stride to the basis
3268 name, respectively. It may be necessary to introduce a
3269 cast (or reuse an existing cast). */
3270 else if (cand_incr == 1)
3272 tree stride_type = TREE_TYPE (c->stride);
3273 tree orig_type = TREE_TYPE (orig_rhs2);
3275 if (types_compatible_p (orig_type, stride_type))
3276 rhs2 = c->stride;
3277 else
3278 rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
3280 stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
3281 orig_code, orig_rhs1, orig_rhs2,
3285 else if (cand_incr == -1)
3287 tree stride_type = TREE_TYPE (c->stride);
3288 tree orig_type = TREE_TYPE (orig_rhs2);
3289 gcc_assert (repl_code != POINTER_PLUS_EXPR);
3291 if (types_compatible_p (orig_type, stride_type))
3292 rhs2 = c->stride;
3293 else
3294 rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
3296 if (orig_code != MINUS_EXPR
3297 || !operand_equal_p (basis_name, orig_rhs1, 0)
3298 || !operand_equal_p (rhs2, orig_rhs2, 0))
3300 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3301 gimple_assign_set_rhs_with_ops (&gsi, MINUS_EXPR, basis_name, rhs2);
3302 update_stmt (gsi_stmt (gsi));
3303 c->cand_stmt = gsi_stmt (gsi);
3305 if (dump_file && (dump_flags & TDF_DETAILS))
3306 stmt_to_print = gsi_stmt (gsi);
3308 else if (dump_file && (dump_flags & TDF_DETAILS))
3309 fputs (" (duplicate, not actually replacing)\n", dump_file);
3312 else if (cand_incr == 0)
3314 tree lhs = gimple_assign_lhs (c->cand_stmt);
3315 tree lhs_type = TREE_TYPE (lhs);
3316 tree basis_type = TREE_TYPE (basis_name);
3318 if (types_compatible_p (lhs_type, basis_type))
3320 gimple copy_stmt = gimple_build_assign (lhs, basis_name);
3321 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3322 gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
3323 gsi_replace (&gsi, copy_stmt, false);
3324 c->cand_stmt = copy_stmt;
3326 if (dump_file && (dump_flags & TDF_DETAILS))
3327 stmt_to_print = copy_stmt;
3329 else
3331 gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
3332 gimple cast_stmt = gimple_build_assign_with_ops (NOP_EXPR, lhs,
3333 basis_name,
3334 NULL_TREE);
3335 gimple_set_location (cast_stmt, gimple_location (c->cand_stmt));
3336 gsi_replace (&gsi, cast_stmt, false);
3337 c->cand_stmt = cast_stmt;
3339 if (dump_file && (dump_flags & TDF_DETAILS))
3340 stmt_to_print = cast_stmt;
3343 else
3344 gcc_unreachable ();
3346 if (dump_file && (dump_flags & TDF_DETAILS) && stmt_to_print)
3348 fputs ("With: ", dump_file);
3349 print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
3350 fputs ("\n", dump_file);
3354 /* For each candidate in the tree rooted at C, replace it with
3355 an increment if such has been shown to be profitable. */
3357 static void
3358 replace_profitable_candidates (slsr_cand_t c)
3360 if (!cand_already_replaced (c))
3362 widest_int increment = cand_abs_increment (c);
3363 enum tree_code orig_code = gimple_assign_rhs_code (c->cand_stmt);
3364 int i;
3366 i = incr_vec_index (increment);
3368 /* Only process profitable increments. Nothing useful can be done
3369 to a cast or copy. */
3370 if (i >= 0
3371 && profitable_increment_p (i)
3372 && orig_code != MODIFY_EXPR
3373 && orig_code != NOP_EXPR)
3375 if (phi_dependent_cand_p (c))
3377 gimple phi = lookup_cand (c->def_phi)->cand_stmt;
3379 if (all_phi_incrs_profitable (c, phi))
3381 /* Look up the LHS SSA name from C's basis. This will be
3382 the RHS1 of the adds we will introduce to create new
3383 phi arguments. */
3384 slsr_cand_t basis = lookup_cand (c->basis);
3385 tree basis_name = gimple_assign_lhs (basis->cand_stmt);
3387 /* Create a new phi statement that will represent C's true
3388 basis after the transformation is complete. */
3389 location_t loc = gimple_location (c->cand_stmt);
3390 tree name = create_phi_basis (c, phi, basis_name,
3391 loc, UNKNOWN_STRIDE);
3393 /* Replace C with an add of the new basis phi and the
3394 increment. */
3395 replace_one_candidate (c, i, name);
3398 else
3400 slsr_cand_t basis = lookup_cand (c->basis);
3401 tree basis_name = gimple_assign_lhs (basis->cand_stmt);
3402 replace_one_candidate (c, i, basis_name);
3407 if (c->sibling)
3408 replace_profitable_candidates (lookup_cand (c->sibling));
3410 if (c->dependent)
3411 replace_profitable_candidates (lookup_cand (c->dependent));
3414 /* Analyze costs of related candidates in the candidate vector,
3415 and make beneficial replacements. */
3417 static void
3418 analyze_candidates_and_replace (void)
3420 unsigned i;
3421 slsr_cand_t c;
3423 /* Each candidate that has a null basis and a non-null
3424 dependent is the root of a tree of related statements.
3425 Analyze each tree to determine a subset of those
3426 statements that can be replaced with maximum benefit. */
3427 FOR_EACH_VEC_ELT (cand_vec, i, c)
3429 slsr_cand_t first_dep;
3431 if (c->basis != 0 || c->dependent == 0)
3432 continue;
3434 if (dump_file && (dump_flags & TDF_DETAILS))
3435 fprintf (dump_file, "\nProcessing dependency tree rooted at %d.\n",
3436 c->cand_num);
3438 first_dep = lookup_cand (c->dependent);
3440 /* If this is a chain of CAND_REFs, unconditionally replace
3441 each of them with a strength-reduced data reference. */
3442 if (c->kind == CAND_REF)
3443 replace_refs (c);
3445 /* If the common stride of all related candidates is a known
3446 constant, each candidate without a phi-dependence can be
3447 profitably replaced. Each replaces a multiply by a single
3448 add, with the possibility that a feeding add also goes dead.
3449 A candidate with a phi-dependence is replaced only if the
3450 compensation code it requires is offset by the strength
3451 reduction savings. */
3452 else if (TREE_CODE (c->stride) == INTEGER_CST)
3453 replace_uncond_cands_and_profitable_phis (first_dep);
3455 /* When the stride is an SSA name, it may still be profitable
3456 to replace some or all of the dependent candidates, depending
3457 on whether the introduced increments can be reused, or are
3458 less expensive to calculate than the replaced statements. */
3459 else
3461 enum machine_mode mode;
3462 bool speed;
3464 /* Determine whether we'll be generating pointer arithmetic
3465 when replacing candidates. */
3466 address_arithmetic_p = (c->kind == CAND_ADD
3467 && POINTER_TYPE_P (c->cand_type));
3469 /* If all candidates have already been replaced under other
3470 interpretations, nothing remains to be done. */
3471 if (!count_candidates (c))
3472 continue;
3474 /* Construct an array of increments for this candidate chain. */
3475 incr_vec = XNEWVEC (incr_info, MAX_INCR_VEC_LEN);
3476 incr_vec_len = 0;
3477 record_increments (c);
3479 /* Determine which increments are profitable to replace. */
3480 mode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (c->cand_stmt)));
3481 speed = optimize_cands_for_speed_p (c);
3482 analyze_increments (first_dep, mode, speed);
3484 /* Insert initializers of the form T_0 = stride * increment
3485 for use in profitable replacements. */
3486 insert_initializers (first_dep);
3487 dump_incr_vec ();
3489 /* Perform the replacements. */
3490 replace_profitable_candidates (first_dep);
3491 free (incr_vec);
3496 static unsigned
3497 execute_strength_reduction (void)
3499 /* Create the obstack where candidates will reside. */
3500 gcc_obstack_init (&cand_obstack);
3502 /* Allocate the candidate vector. */
3503 cand_vec.create (128);
3505 /* Allocate the mapping from statements to candidate indices. */
3506 stmt_cand_map = pointer_map_create ();
3508 /* Create the obstack where candidate chains will reside. */
3509 gcc_obstack_init (&chain_obstack);
3511 /* Allocate the mapping from base expressions to candidate chains. */
3512 base_cand_map.create (500);
3514 /* Initialize the loop optimizer. We need to detect flow across
3515 back edges, and this gives us dominator information as well. */
3516 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
3518 /* Walk the CFG in predominator order looking for strength reduction
3519 candidates. */
3520 find_candidates_dom_walker (CDI_DOMINATORS)
3521 .walk (cfun->cfg->x_entry_block_ptr);
3523 if (dump_file && (dump_flags & TDF_DETAILS))
3525 dump_cand_vec ();
3526 dump_cand_chains ();
3529 /* Analyze costs and make appropriate replacements. */
3530 analyze_candidates_and_replace ();
3532 loop_optimizer_finalize ();
3533 base_cand_map.dispose ();
3534 obstack_free (&chain_obstack, NULL);
3535 pointer_map_destroy (stmt_cand_map);
3536 cand_vec.release ();
3537 obstack_free (&cand_obstack, NULL);
3539 return 0;
3542 static bool
3543 gate_strength_reduction (void)
3545 return flag_tree_slsr;
3548 namespace {
3550 const pass_data pass_data_strength_reduction =
3552 GIMPLE_PASS, /* type */
3553 "slsr", /* name */
3554 OPTGROUP_NONE, /* optinfo_flags */
3555 true, /* has_gate */
3556 true, /* has_execute */
3557 TV_GIMPLE_SLSR, /* tv_id */
3558 ( PROP_cfg | PROP_ssa ), /* properties_required */
3559 0, /* properties_provided */
3560 0, /* properties_destroyed */
3561 0, /* todo_flags_start */
3562 TODO_verify_ssa, /* todo_flags_finish */
3565 class pass_strength_reduction : public gimple_opt_pass
3567 public:
3568 pass_strength_reduction (gcc::context *ctxt)
3569 : gimple_opt_pass (pass_data_strength_reduction, ctxt)
3572 /* opt_pass methods: */
3573 bool gate () { return gate_strength_reduction (); }
3574 unsigned int execute () { return execute_strength_reduction (); }
3576 }; // class pass_strength_reduction
3578 } // anon namespace
3580 gimple_opt_pass *
3581 make_pass_strength_reduction (gcc::context *ctxt)
3583 return new pass_strength_reduction (ctxt);