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