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