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