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