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