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