]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* SSA-PRE for trees. |
5624e564 | 2 | Copyright (C) 2001-2015 Free Software Foundation, Inc. |
7e6eb623 | 3 | Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher |
b9c5e484 | 4 | <stevenb@suse.de> |
6de9cd9a DN |
5 | |
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
9dcd6f09 | 10 | the Free Software Foundation; either version 3, or (at your option) |
6de9cd9a DN |
11 | any later version. |
12 | ||
13 | GCC is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
33c94679 | 21 | |
6de9cd9a DN |
22 | #include "config.h" |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
c7131fb2 | 25 | #include "backend.h" |
9fdcd34e | 26 | #include "predict.h" |
40e23961 | 27 | #include "tree.h" |
c7131fb2 AM |
28 | #include "gimple.h" |
29 | #include "rtl.h" | |
30 | #include "ssa.h" | |
31 | #include "alias.h" | |
40e23961 | 32 | #include "fold-const.h" |
60393bbc | 33 | #include "cfganal.h" |
cf835838 | 34 | #include "gimple-pretty-print.h" |
6de9cd9a | 35 | #include "tree-inline.h" |
2fb9a547 AM |
36 | #include "internal-fn.h" |
37 | #include "gimple-fold.h" | |
38 | #include "tree-eh.h" | |
45b0be94 | 39 | #include "gimplify.h" |
5be5c238 | 40 | #include "gimple-iterator.h" |
18f429e2 | 41 | #include "gimplify-me.h" |
442b4905 | 42 | #include "tree-cfg.h" |
442b4905 AM |
43 | #include "tree-ssa-loop.h" |
44 | #include "tree-into-ssa.h" | |
36566b39 | 45 | #include "flags.h" |
36566b39 PK |
46 | #include "insn-config.h" |
47 | #include "expmed.h" | |
48 | #include "dojump.h" | |
49 | #include "explow.h" | |
50 | #include "calls.h" | |
51 | #include "emit-rtl.h" | |
52 | #include "varasm.h" | |
53 | #include "stmt.h" | |
d8a2d370 | 54 | #include "expr.h" |
442b4905 AM |
55 | #include "tree-dfa.h" |
56 | #include "tree-ssa.h" | |
6de9cd9a | 57 | #include "tree-iterator.h" |
6de9cd9a DN |
58 | #include "alloc-pool.h" |
59 | #include "tree-pass.h" | |
7e6eb623 | 60 | #include "langhooks.h" |
0fc6c492 | 61 | #include "cfgloop.h" |
89fb70a3 | 62 | #include "tree-ssa-sccvn.h" |
a8338640 | 63 | #include "tree-scalar-evolution.h" |
f0ed4cfb | 64 | #include "params.h" |
c9145754 | 65 | #include "dbgcnt.h" |
40b178f4 | 66 | #include "domwalk.h" |
c582198b | 67 | #include "cgraph.h" |
dd912cb8 | 68 | #include "symbol-summary.h" |
e248d83f | 69 | #include "ipa-prop.h" |
744730a4 | 70 | #include "tree-ssa-propagate.h" |
7d0aa05b | 71 | #include "ipa-utils.h" |
2a5671ee | 72 | #include "tree-cfgcleanup.h" |
33c94679 | 73 | |
7e6eb623 | 74 | /* TODO: |
b9c5e484 | 75 | |
bdee7684 | 76 | 1. Avail sets can be shared by making an avail_find_leader that |
7e6eb623 DB |
77 | walks up the dominator tree and looks in those avail sets. |
78 | This might affect code optimality, it's unclear right now. | |
c90186eb | 79 | 2. Strength reduction can be performed by anticipating expressions |
7e6eb623 | 80 | we can repair later on. |
c90186eb | 81 | 3. We can do back-substitution or smarter value numbering to catch |
0fc6c492 | 82 | commutative expressions split up over multiple statements. |
b9c5e484 | 83 | */ |
7e6eb623 DB |
84 | |
85 | /* For ease of terminology, "expression node" in the below refers to | |
726a989a | 86 | every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs |
07beea0d AH |
87 | represent the actual statement containing the expressions we care about, |
88 | and we cache the value number by putting it in the expression. */ | |
7e6eb623 DB |
89 | |
90 | /* Basic algorithm | |
b9c5e484 | 91 | |
56db793a DB |
92 | First we walk the statements to generate the AVAIL sets, the |
93 | EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the | |
94 | generation of values/expressions by a given block. We use them | |
95 | when computing the ANTIC sets. The AVAIL sets consist of | |
96 | SSA_NAME's that represent values, so we know what values are | |
97 | available in what blocks. AVAIL is a forward dataflow problem. In | |
98 | SSA, values are never killed, so we don't need a kill set, or a | |
99 | fixpoint iteration, in order to calculate the AVAIL sets. In | |
100 | traditional parlance, AVAIL sets tell us the downsafety of the | |
7e6eb623 | 101 | expressions/values. |
b9c5e484 | 102 | |
56db793a DB |
103 | Next, we generate the ANTIC sets. These sets represent the |
104 | anticipatable expressions. ANTIC is a backwards dataflow | |
d75dbccd | 105 | problem. An expression is anticipatable in a given block if it could |
56db793a DB |
106 | be generated in that block. This means that if we had to perform |
107 | an insertion in that block, of the value of that expression, we | |
108 | could. Calculating the ANTIC sets requires phi translation of | |
109 | expressions, because the flow goes backwards through phis. We must | |
110 | iterate to a fixpoint of the ANTIC sets, because we have a kill | |
111 | set. Even in SSA form, values are not live over the entire | |
112 | function, only from their definition point onwards. So we have to | |
113 | remove values from the ANTIC set once we go past the definition | |
114 | point of the leaders that make them up. | |
115 | compute_antic/compute_antic_aux performs this computation. | |
7e6eb623 DB |
116 | |
117 | Third, we perform insertions to make partially redundant | |
118 | expressions fully redundant. | |
119 | ||
120 | An expression is partially redundant (excluding partial | |
121 | anticipation) if: | |
122 | ||
123 | 1. It is AVAIL in some, but not all, of the predecessors of a | |
124 | given block. | |
125 | 2. It is ANTIC in all the predecessors. | |
126 | ||
127 | In order to make it fully redundant, we insert the expression into | |
128 | the predecessors where it is not available, but is ANTIC. | |
d75dbccd DB |
129 | |
130 | For the partial anticipation case, we only perform insertion if it | |
131 | is partially anticipated in some block, and fully available in all | |
132 | of the predecessors. | |
133 | ||
134 | insert/insert_aux/do_regular_insertion/do_partial_partial_insertion | |
135 | performs these steps. | |
7e6eb623 DB |
136 | |
137 | Fourth, we eliminate fully redundant expressions. | |
138 | This is a simple statement walk that replaces redundant | |
070b797d | 139 | calculations with the now available values. */ |
7e6eb623 DB |
140 | |
141 | /* Representations of value numbers: | |
142 | ||
c9145754 DB |
143 | Value numbers are represented by a representative SSA_NAME. We |
144 | will create fake SSA_NAME's in situations where we need a | |
145 | representative but do not have one (because it is a complex | |
146 | expression). In order to facilitate storing the value numbers in | |
147 | bitmaps, and keep the number of wasted SSA_NAME's down, we also | |
148 | associate a value_id with each value number, and create full blown | |
149 | ssa_name's only where we actually need them (IE in operands of | |
150 | existing expressions). | |
151 | ||
152 | Theoretically you could replace all the value_id's with | |
153 | SSA_NAME_VERSION, but this would allocate a large number of | |
154 | SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number. | |
155 | It would also require an additional indirection at each point we | |
156 | use the value id. */ | |
7e6eb623 | 157 | |
b9c5e484 | 158 | /* Representation of expressions on value numbers: |
7e6eb623 | 159 | |
249eb506 | 160 | Expressions consisting of value numbers are represented the same |
c9145754 DB |
161 | way as our VN internally represents them, with an additional |
162 | "pre_expr" wrapping around them in order to facilitate storing all | |
163 | of the expressions in the same sets. */ | |
7e6eb623 | 164 | |
c9145754 | 165 | /* Representation of sets: |
7e6eb623 | 166 | |
c9145754 DB |
167 | The dataflow sets do not need to be sorted in any particular order |
168 | for the majority of their lifetime, are simply represented as two | |
169 | bitmaps, one that keeps track of values present in the set, and one | |
170 | that keeps track of expressions present in the set. | |
7e6eb623 | 171 | |
c9145754 DB |
172 | When we need them in topological order, we produce it on demand by |
173 | transforming the bitmap into an array and sorting it into topo | |
174 | order. */ | |
7e6eb623 | 175 | |
c9145754 DB |
176 | /* Type of expression, used to know which member of the PRE_EXPR union |
177 | is valid. */ | |
7e6eb623 | 178 | |
c9145754 DB |
179 | enum pre_expr_kind |
180 | { | |
181 | NAME, | |
182 | NARY, | |
183 | REFERENCE, | |
184 | CONSTANT | |
185 | }; | |
186 | ||
a79683d5 | 187 | union pre_expr_union |
c9145754 DB |
188 | { |
189 | tree name; | |
190 | tree constant; | |
191 | vn_nary_op_t nary; | |
192 | vn_reference_t reference; | |
a79683d5 | 193 | }; |
b9c5e484 | 194 | |
8d67ee55 | 195 | typedef struct pre_expr_d : nofree_ptr_hash <pre_expr_d> |
c9145754 DB |
196 | { |
197 | enum pre_expr_kind kind; | |
198 | unsigned int id; | |
199 | pre_expr_union u; | |
5deac340 RG |
200 | |
201 | /* hash_table support. */ | |
5deac340 RG |
202 | static inline hashval_t hash (const pre_expr_d *); |
203 | static inline int equal (const pre_expr_d *, const pre_expr_d *); | |
c9145754 | 204 | } *pre_expr; |
7e6eb623 | 205 | |
c9145754 DB |
206 | #define PRE_EXPR_NAME(e) (e)->u.name |
207 | #define PRE_EXPR_NARY(e) (e)->u.nary | |
208 | #define PRE_EXPR_REFERENCE(e) (e)->u.reference | |
209 | #define PRE_EXPR_CONSTANT(e) (e)->u.constant | |
7e6eb623 | 210 | |
0823efed | 211 | /* Compare E1 and E1 for equality. */ |
7e6eb623 | 212 | |
0823efed | 213 | inline int |
67f58944 | 214 | pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2) |
0823efed | 215 | { |
c9145754 DB |
216 | if (e1->kind != e2->kind) |
217 | return false; | |
218 | ||
219 | switch (e1->kind) | |
220 | { | |
221 | case CONSTANT: | |
726a989a RB |
222 | return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1), |
223 | PRE_EXPR_CONSTANT (e2)); | |
c9145754 DB |
224 | case NAME: |
225 | return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2); | |
226 | case NARY: | |
227 | return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2)); | |
228 | case REFERENCE: | |
229 | return vn_reference_eq (PRE_EXPR_REFERENCE (e1), | |
230 | PRE_EXPR_REFERENCE (e2)); | |
231 | default: | |
9708c51d | 232 | gcc_unreachable (); |
c9145754 DB |
233 | } |
234 | } | |
235 | ||
0823efed DN |
236 | /* Hash E. */ |
237 | ||
238 | inline hashval_t | |
67f58944 | 239 | pre_expr_d::hash (const pre_expr_d *e) |
c9145754 | 240 | { |
c9145754 DB |
241 | switch (e->kind) |
242 | { | |
243 | case CONSTANT: | |
726a989a | 244 | return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e)); |
c9145754 | 245 | case NAME: |
9708c51d | 246 | return SSA_NAME_VERSION (PRE_EXPR_NAME (e)); |
c9145754 | 247 | case NARY: |
85169114 | 248 | return PRE_EXPR_NARY (e)->hashcode; |
c9145754 | 249 | case REFERENCE: |
85169114 | 250 | return PRE_EXPR_REFERENCE (e)->hashcode; |
c9145754 | 251 | default: |
9708c51d | 252 | gcc_unreachable (); |
c9145754 DB |
253 | } |
254 | } | |
83737db2 | 255 | |
c9145754 DB |
256 | /* Next global expression id number. */ |
257 | static unsigned int next_expression_id; | |
070b797d | 258 | |
83737db2 | 259 | /* Mapping from expression to id number we can use in bitmap sets. */ |
9771b263 | 260 | static vec<pre_expr> expressions; |
c203e8a7 | 261 | static hash_table<pre_expr_d> *expression_to_id; |
9771b263 | 262 | static vec<unsigned> name_to_id; |
81def1b7 | 263 | |
83737db2 DB |
264 | /* Allocate an expression id for EXPR. */ |
265 | ||
266 | static inline unsigned int | |
c9145754 | 267 | alloc_expression_id (pre_expr expr) |
6de9cd9a | 268 | { |
0823efed | 269 | struct pre_expr_d **slot; |
83737db2 DB |
270 | /* Make sure we won't overflow. */ |
271 | gcc_assert (next_expression_id + 1 > next_expression_id); | |
c9145754 | 272 | expr->id = next_expression_id++; |
9771b263 | 273 | expressions.safe_push (expr); |
13de9095 RG |
274 | if (expr->kind == NAME) |
275 | { | |
276 | unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr)); | |
9771b263 | 277 | /* vec::safe_grow_cleared allocates no headroom. Avoid frequent |
52e4630c | 278 | re-allocations by using vec::reserve upfront. */ |
9771b263 DN |
279 | unsigned old_len = name_to_id.length (); |
280 | name_to_id.reserve (num_ssa_names - old_len); | |
52e4630c | 281 | name_to_id.quick_grow_cleared (num_ssa_names); |
9771b263 DN |
282 | gcc_assert (name_to_id[version] == 0); |
283 | name_to_id[version] = expr->id; | |
13de9095 RG |
284 | } |
285 | else | |
286 | { | |
c203e8a7 | 287 | slot = expression_to_id->find_slot (expr, INSERT); |
13de9095 RG |
288 | gcc_assert (!*slot); |
289 | *slot = expr; | |
290 | } | |
83737db2 DB |
291 | return next_expression_id - 1; |
292 | } | |
293 | ||
294 | /* Return the expression id for tree EXPR. */ | |
295 | ||
296 | static inline unsigned int | |
c9145754 DB |
297 | get_expression_id (const pre_expr expr) |
298 | { | |
299 | return expr->id; | |
300 | } | |
301 | ||
302 | static inline unsigned int | |
303 | lookup_expression_id (const pre_expr expr) | |
7e6eb623 | 304 | { |
0823efed | 305 | struct pre_expr_d **slot; |
b71b4522 | 306 | |
13de9095 RG |
307 | if (expr->kind == NAME) |
308 | { | |
309 | unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr)); | |
9771b263 | 310 | if (name_to_id.length () <= version) |
13de9095 | 311 | return 0; |
9771b263 | 312 | return name_to_id[version]; |
13de9095 RG |
313 | } |
314 | else | |
315 | { | |
c203e8a7 | 316 | slot = expression_to_id->find_slot (expr, NO_INSERT); |
13de9095 RG |
317 | if (!slot) |
318 | return 0; | |
319 | return ((pre_expr)*slot)->id; | |
320 | } | |
83737db2 | 321 | } |
b9c5e484 | 322 | |
83737db2 DB |
323 | /* Return the existing expression id for EXPR, or create one if one |
324 | does not exist yet. */ | |
b9c5e484 | 325 | |
83737db2 | 326 | static inline unsigned int |
c9145754 | 327 | get_or_alloc_expression_id (pre_expr expr) |
83737db2 | 328 | { |
c9145754 DB |
329 | unsigned int id = lookup_expression_id (expr); |
330 | if (id == 0) | |
83737db2 | 331 | return alloc_expression_id (expr); |
c9145754 | 332 | return expr->id = id; |
83737db2 DB |
333 | } |
334 | ||
335 | /* Return the expression that has expression id ID */ | |
336 | ||
c9145754 | 337 | static inline pre_expr |
83737db2 DB |
338 | expression_for_id (unsigned int id) |
339 | { | |
9771b263 | 340 | return expressions[id]; |
c5830edf DB |
341 | } |
342 | ||
b71b4522 DB |
343 | /* Free the expression id field in all of our expressions, |
344 | and then destroy the expressions array. */ | |
345 | ||
346 | static void | |
347 | clear_expression_ids (void) | |
348 | { | |
9771b263 | 349 | expressions.release (); |
c9145754 | 350 | } |
b71b4522 | 351 | |
fcb87c50 | 352 | static object_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes"); |
c9145754 DB |
353 | |
354 | /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */ | |
355 | ||
356 | static pre_expr | |
357 | get_or_alloc_expr_for_name (tree name) | |
358 | { | |
5f5126d6 RG |
359 | struct pre_expr_d expr; |
360 | pre_expr result; | |
c9145754 DB |
361 | unsigned int result_id; |
362 | ||
5f5126d6 RG |
363 | expr.kind = NAME; |
364 | expr.id = 0; | |
365 | PRE_EXPR_NAME (&expr) = name; | |
366 | result_id = lookup_expression_id (&expr); | |
367 | if (result_id != 0) | |
368 | return expression_for_id (result_id); | |
369 | ||
971540bd | 370 | result = pre_expr_pool.allocate (); |
c9145754 | 371 | result->kind = NAME; |
c9145754 | 372 | PRE_EXPR_NAME (result) = name; |
5f5126d6 | 373 | alloc_expression_id (result); |
c9145754 | 374 | return result; |
b71b4522 DB |
375 | } |
376 | ||
bdee7684 | 377 | /* An unordered bitmap set. One bitmap tracks values, the other, |
8c27b7d4 | 378 | expressions. */ |
bdee7684 DB |
379 | typedef struct bitmap_set |
380 | { | |
5c72d561 JH |
381 | bitmap_head expressions; |
382 | bitmap_head values; | |
bdee7684 DB |
383 | } *bitmap_set_t; |
384 | ||
83737db2 | 385 | #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \ |
c3284718 | 386 | EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi)) |
c9145754 | 387 | |
85169114 | 388 | #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \ |
c3284718 | 389 | EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi)) |
85169114 | 390 | |
c9145754 | 391 | /* Mapping from value id to expressions with that value_id. */ |
9771b263 | 392 | static vec<bitmap> value_expressions; |
83737db2 | 393 | |
6b416da1 | 394 | /* Sets that we need to keep track of. */ |
83737db2 | 395 | typedef struct bb_bitmap_sets |
6de9cd9a | 396 | { |
ca072a31 DB |
397 | /* The EXP_GEN set, which represents expressions/values generated in |
398 | a basic block. */ | |
83737db2 | 399 | bitmap_set_t exp_gen; |
ca072a31 DB |
400 | |
401 | /* The PHI_GEN set, which represents PHI results generated in a | |
402 | basic block. */ | |
6b416da1 | 403 | bitmap_set_t phi_gen; |
ca072a31 | 404 | |
f6fe65dc | 405 | /* The TMP_GEN set, which represents results/temporaries generated |
ca072a31 | 406 | in a basic block. IE the LHS of an expression. */ |
6b416da1 | 407 | bitmap_set_t tmp_gen; |
ca072a31 DB |
408 | |
409 | /* The AVAIL_OUT set, which represents which values are available in | |
410 | a given basic block. */ | |
bdee7684 | 411 | bitmap_set_t avail_out; |
ca072a31 | 412 | |
c90186eb | 413 | /* The ANTIC_IN set, which represents which values are anticipatable |
ca072a31 | 414 | in a given basic block. */ |
83737db2 | 415 | bitmap_set_t antic_in; |
ca072a31 | 416 | |
d75dbccd DB |
417 | /* The PA_IN set, which represents which values are |
418 | partially anticipatable in a given basic block. */ | |
419 | bitmap_set_t pa_in; | |
420 | ||
ca072a31 DB |
421 | /* The NEW_SETS set, which is used during insertion to augment the |
422 | AVAIL_OUT set of blocks with the new insertions performed during | |
423 | the current iteration. */ | |
6b416da1 | 424 | bitmap_set_t new_sets; |
c90186eb | 425 | |
5006671f RG |
426 | /* A cache for value_dies_in_block_x. */ |
427 | bitmap expr_dies; | |
428 | ||
81231d13 RB |
429 | /* The live virtual operand on successor edges. */ |
430 | tree vop_on_exit; | |
431 | ||
d75dbccd | 432 | /* True if we have visited this block during ANTIC calculation. */ |
a19eb9d2 | 433 | unsigned int visited : 1; |
d75dbccd | 434 | |
a19eb9d2 RG |
435 | /* True when the block contains a call that might not return. */ |
436 | unsigned int contains_may_not_return_call : 1; | |
d75dbccd DB |
437 | } *bb_value_sets_t; |
438 | ||
439 | #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen | |
440 | #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen | |
441 | #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen | |
442 | #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out | |
443 | #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in | |
444 | #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in | |
d75dbccd | 445 | #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets |
5006671f RG |
446 | #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies |
447 | #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited | |
a19eb9d2 | 448 | #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call |
81231d13 | 449 | #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit |
d75dbccd | 450 | |
c9145754 | 451 | |
83737db2 DB |
452 | /* Basic block list in postorder. */ |
453 | static int *postorder; | |
585d0dc4 | 454 | static int postorder_num; |
6de9cd9a | 455 | |
ca072a31 DB |
456 | /* This structure is used to keep track of statistics on what |
457 | optimization PRE was able to perform. */ | |
7e6eb623 | 458 | static struct |
6de9cd9a | 459 | { |
ca072a31 | 460 | /* The number of RHS computations eliminated by PRE. */ |
7e6eb623 | 461 | int eliminations; |
ca072a31 DB |
462 | |
463 | /* The number of new expressions/temporaries generated by PRE. */ | |
7e6eb623 | 464 | int insertions; |
ca072a31 | 465 | |
d75dbccd DB |
466 | /* The number of inserts found due to partial anticipation */ |
467 | int pa_insert; | |
468 | ||
ca072a31 | 469 | /* The number of new PHI nodes added by PRE. */ |
7e6eb623 DB |
470 | int phis; |
471 | } pre_stats; | |
6de9cd9a | 472 | |
d75dbccd | 473 | static bool do_partial_partial; |
e076271b | 474 | static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int); |
c9145754 DB |
475 | static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr); |
476 | static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr); | |
bdee7684 | 477 | static void bitmap_set_copy (bitmap_set_t, bitmap_set_t); |
c9145754 DB |
478 | static bool bitmap_set_contains_value (bitmap_set_t, unsigned int); |
479 | static void bitmap_insert_into_set (bitmap_set_t, pre_expr); | |
9708c51d RG |
480 | static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr, |
481 | unsigned int, bool); | |
bdee7684 | 482 | static bitmap_set_t bitmap_set_new (void); |
726a989a | 483 | static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *, |
e076271b RG |
484 | tree); |
485 | static tree find_or_generate_expression (basic_block, tree, gimple_seq *); | |
de081cfd | 486 | static unsigned int get_expr_value_id (pre_expr); |
6de9cd9a | 487 | |
7e6eb623 DB |
488 | /* We can add and remove elements and entries to and from sets |
489 | and hash tables, so we use alloc pools for them. */ | |
6de9cd9a | 490 | |
fcb87c50 | 491 | static object_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets"); |
7932a3db | 492 | static bitmap_obstack grand_bitmap_obstack; |
6de9cd9a | 493 | |
30fd5881 | 494 | /* Set of blocks with statements that have had their EH properties changed. */ |
53b4bf74 DN |
495 | static bitmap need_eh_cleanup; |
496 | ||
30fd5881 EB |
497 | /* Set of blocks with statements that have had their AB properties changed. */ |
498 | static bitmap need_ab_cleanup; | |
499 | ||
7e6eb623 DB |
500 | /* A three tuple {e, pred, v} used to cache phi translations in the |
501 | phi_translate_table. */ | |
6de9cd9a | 502 | |
95fbe13e | 503 | typedef struct expr_pred_trans_d : free_ptr_hash<expr_pred_trans_d> |
6de9cd9a | 504 | { |
8c27b7d4 | 505 | /* The expression. */ |
c9145754 | 506 | pre_expr e; |
ca072a31 DB |
507 | |
508 | /* The predecessor block along which we translated the expression. */ | |
7e6eb623 | 509 | basic_block pred; |
ca072a31 DB |
510 | |
511 | /* The value that resulted from the translation. */ | |
c9145754 | 512 | pre_expr v; |
ca072a31 DB |
513 | |
514 | /* The hashcode for the expression, pred pair. This is cached for | |
515 | speed reasons. */ | |
7e6eb623 | 516 | hashval_t hashcode; |
5deac340 RG |
517 | |
518 | /* hash_table support. */ | |
67f58944 TS |
519 | static inline hashval_t hash (const expr_pred_trans_d *); |
520 | static inline int equal (const expr_pred_trans_d *, const expr_pred_trans_d *); | |
7e6eb623 | 521 | } *expr_pred_trans_t; |
741ac903 | 522 | typedef const struct expr_pred_trans_d *const_expr_pred_trans_t; |
6de9cd9a | 523 | |
0823efed | 524 | inline hashval_t |
5deac340 | 525 | expr_pred_trans_d::hash (const expr_pred_trans_d *e) |
7e6eb623 | 526 | { |
5deac340 | 527 | return e->hashcode; |
7e6eb623 | 528 | } |
6de9cd9a | 529 | |
0823efed | 530 | inline int |
67f58944 TS |
531 | expr_pred_trans_d::equal (const expr_pred_trans_d *ve1, |
532 | const expr_pred_trans_d *ve2) | |
7e6eb623 | 533 | { |
7e6eb623 DB |
534 | basic_block b1 = ve1->pred; |
535 | basic_block b2 = ve2->pred; | |
b9c5e484 | 536 | |
ca072a31 DB |
537 | /* If they are not translations for the same basic block, they can't |
538 | be equal. */ | |
7e6eb623 | 539 | if (b1 != b2) |
6de9cd9a | 540 | return false; |
5deac340 | 541 | return pre_expr_d::equal (ve1->e, ve2->e); |
6de9cd9a DN |
542 | } |
543 | ||
0823efed DN |
544 | /* The phi_translate_table caches phi translations for a given |
545 | expression and predecessor. */ | |
c203e8a7 | 546 | static hash_table<expr_pred_trans_d> *phi_translate_table; |
0823efed | 547 | |
c9145754 | 548 | /* Add the tuple mapping from {expression E, basic block PRED} to |
984af6ac | 549 | the phi translation table and return whether it pre-existed. */ |
7e6eb623 | 550 | |
984af6ac RB |
551 | static inline bool |
552 | phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred) | |
7e6eb623 | 553 | { |
0823efed | 554 | expr_pred_trans_t *slot; |
984af6ac RB |
555 | expr_pred_trans_d tem; |
556 | hashval_t hash = iterative_hash_hashval_t (pre_expr_d::hash (e), | |
557 | pred->index); | |
558 | tem.e = e; | |
559 | tem.pred = pred; | |
560 | tem.hashcode = hash; | |
c203e8a7 | 561 | slot = phi_translate_table->find_slot_with_hash (&tem, hash, INSERT); |
984af6ac RB |
562 | if (*slot) |
563 | { | |
564 | *entry = *slot; | |
565 | return true; | |
566 | } | |
c9145754 | 567 | |
984af6ac RB |
568 | *entry = *slot = XNEW (struct expr_pred_trans_d); |
569 | (*entry)->e = e; | |
570 | (*entry)->pred = pred; | |
571 | (*entry)->hashcode = hash; | |
572 | return false; | |
6de9cd9a DN |
573 | } |
574 | ||
ff2ad0f7 | 575 | |
c9145754 | 576 | /* Add expression E to the expression set of value id V. */ |
33c94679 | 577 | |
6bcfb753 | 578 | static void |
c9145754 | 579 | add_to_value (unsigned int v, pre_expr e) |
7e6eb623 | 580 | { |
4d4b1b30 | 581 | bitmap set; |
c9145754 | 582 | |
4d4b1b30 | 583 | gcc_checking_assert (get_expr_value_id (e) == v); |
de081cfd | 584 | |
9771b263 | 585 | if (v >= value_expressions.length ()) |
c9145754 | 586 | { |
9771b263 | 587 | value_expressions.safe_grow_cleared (v + 1); |
c9145754 | 588 | } |
33c94679 | 589 | |
9771b263 | 590 | set = value_expressions[v]; |
c9145754 DB |
591 | if (!set) |
592 | { | |
4d4b1b30 | 593 | set = BITMAP_ALLOC (&grand_bitmap_obstack); |
9771b263 | 594 | value_expressions[v] = set; |
c9145754 | 595 | } |
33c94679 | 596 | |
4d4b1b30 | 597 | bitmap_set_bit (set, get_or_alloc_expression_id (e)); |
7e6eb623 | 598 | } |
6de9cd9a | 599 | |
bdee7684 DB |
600 | /* Create a new bitmap set and return it. */ |
601 | ||
b9c5e484 | 602 | static bitmap_set_t |
bdee7684 DB |
603 | bitmap_set_new (void) |
604 | { | |
971540bd | 605 | bitmap_set_t ret = bitmap_set_pool.allocate (); |
5c72d561 JH |
606 | bitmap_initialize (&ret->expressions, &grand_bitmap_obstack); |
607 | bitmap_initialize (&ret->values, &grand_bitmap_obstack); | |
bdee7684 DB |
608 | return ret; |
609 | } | |
610 | ||
c9145754 DB |
611 | /* Return the value id for a PRE expression EXPR. */ |
612 | ||
613 | static unsigned int | |
614 | get_expr_value_id (pre_expr expr) | |
615 | { | |
e3815735 | 616 | unsigned int id; |
c9145754 DB |
617 | switch (expr->kind) |
618 | { | |
619 | case CONSTANT: | |
5ba5e8ec | 620 | id = get_constant_value_id (PRE_EXPR_CONSTANT (expr)); |
e3815735 | 621 | break; |
c9145754 | 622 | case NAME: |
e3815735 RB |
623 | id = VN_INFO (PRE_EXPR_NAME (expr))->value_id; |
624 | break; | |
c9145754 | 625 | case NARY: |
e3815735 RB |
626 | id = PRE_EXPR_NARY (expr)->value_id; |
627 | break; | |
c9145754 | 628 | case REFERENCE: |
e3815735 RB |
629 | id = PRE_EXPR_REFERENCE (expr)->value_id; |
630 | break; | |
c9145754 DB |
631 | default: |
632 | gcc_unreachable (); | |
633 | } | |
e3815735 RB |
634 | /* ??? We cannot assert that expr has a value-id (it can be 0), because |
635 | we assign value-ids only to expressions that have a result | |
636 | in set_hashtable_value_ids. */ | |
637 | return id; | |
c9145754 DB |
638 | } |
639 | ||
40b178f4 RG |
640 | /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */ |
641 | ||
642 | static tree | |
643 | sccvn_valnum_from_value_id (unsigned int val) | |
644 | { | |
645 | bitmap_iterator bi; | |
646 | unsigned int i; | |
9771b263 | 647 | bitmap exprset = value_expressions[val]; |
40b178f4 RG |
648 | EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi) |
649 | { | |
650 | pre_expr vexpr = expression_for_id (i); | |
651 | if (vexpr->kind == NAME) | |
652 | return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum; | |
653 | else if (vexpr->kind == CONSTANT) | |
654 | return PRE_EXPR_CONSTANT (vexpr); | |
655 | } | |
656 | return NULL_TREE; | |
657 | } | |
658 | ||
83737db2 | 659 | /* Remove an expression EXPR from a bitmapped set. */ |
bdee7684 DB |
660 | |
661 | static void | |
c9145754 | 662 | bitmap_remove_from_set (bitmap_set_t set, pre_expr expr) |
bdee7684 | 663 | { |
c9145754 DB |
664 | unsigned int val = get_expr_value_id (expr); |
665 | if (!value_id_constant_p (val)) | |
83737db2 | 666 | { |
5c72d561 JH |
667 | bitmap_clear_bit (&set->values, val); |
668 | bitmap_clear_bit (&set->expressions, get_expression_id (expr)); | |
83737db2 | 669 | } |
bdee7684 | 670 | } |
6de9cd9a | 671 | |
7e6eb623 | 672 | static void |
c9145754 | 673 | bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr, |
9708c51d | 674 | unsigned int val, bool allow_constants) |
7e6eb623 | 675 | { |
c9145754 | 676 | if (allow_constants || !value_id_constant_p (val)) |
7e6eb623 | 677 | { |
c9145754 DB |
678 | /* We specifically expect this and only this function to be able to |
679 | insert constants into a set. */ | |
5c72d561 JH |
680 | bitmap_set_bit (&set->values, val); |
681 | bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr)); | |
7e6eb623 DB |
682 | } |
683 | } | |
6de9cd9a | 684 | |
c9145754 DB |
685 | /* Insert an expression EXPR into a bitmapped set. */ |
686 | ||
687 | static void | |
688 | bitmap_insert_into_set (bitmap_set_t set, pre_expr expr) | |
689 | { | |
9708c51d | 690 | bitmap_insert_into_set_1 (set, expr, get_expr_value_id (expr), false); |
c9145754 DB |
691 | } |
692 | ||
bdee7684 DB |
693 | /* Copy a bitmapped set ORIG, into bitmapped set DEST. */ |
694 | ||
695 | static void | |
696 | bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig) | |
697 | { | |
5c72d561 JH |
698 | bitmap_copy (&dest->expressions, &orig->expressions); |
699 | bitmap_copy (&dest->values, &orig->values); | |
bdee7684 DB |
700 | } |
701 | ||
ff3fdad2 | 702 | |
83737db2 | 703 | /* Free memory used up by SET. */ |
ff3fdad2 | 704 | static void |
83737db2 | 705 | bitmap_set_free (bitmap_set_t set) |
ff3fdad2 | 706 | { |
5c72d561 JH |
707 | bitmap_clear (&set->expressions); |
708 | bitmap_clear (&set->values); | |
ff3fdad2 DB |
709 | } |
710 | ||
ff3fdad2 | 711 | |
83737db2 | 712 | /* Generate an topological-ordered array of bitmap set SET. */ |
ff3fdad2 | 713 | |
9771b263 | 714 | static vec<pre_expr> |
83737db2 | 715 | sorted_array_from_bitmap_set (bitmap_set_t set) |
ff3fdad2 | 716 | { |
85169114 PB |
717 | unsigned int i, j; |
718 | bitmap_iterator bi, bj; | |
9771b263 | 719 | vec<pre_expr> result; |
a7d04a53 | 720 | |
dee29e84 RB |
721 | /* Pre-allocate enough space for the array. */ |
722 | result.create (bitmap_count_bits (&set->expressions)); | |
ff3fdad2 | 723 | |
85169114 PB |
724 | FOR_EACH_VALUE_ID_IN_SET (set, i, bi) |
725 | { | |
726 | /* The number of expressions having a given value is usually | |
727 | relatively small. Thus, rather than making a vector of all | |
728 | the expressions and sorting it by value-id, we walk the values | |
729 | and check in the reverse mapping that tells us what expressions | |
730 | have a given value, to filter those in our set. As a result, | |
731 | the expressions are inserted in value-id order, which means | |
732 | topological order. | |
733 | ||
734 | If this is somehow a significant lose for some cases, we can | |
735 | choose which set to walk based on the set size. */ | |
9771b263 | 736 | bitmap exprset = value_expressions[i]; |
4d4b1b30 | 737 | EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj) |
85169114 | 738 | { |
5c72d561 | 739 | if (bitmap_bit_p (&set->expressions, j)) |
dee29e84 | 740 | result.quick_push (expression_for_id (j)); |
85169114 PB |
741 | } |
742 | } | |
6de9cd9a | 743 | |
83737db2 | 744 | return result; |
7e6eb623 | 745 | } |
6de9cd9a | 746 | |
83737db2 | 747 | /* Perform bitmapped set operation DEST &= ORIG. */ |
6de9cd9a DN |
748 | |
749 | static void | |
83737db2 | 750 | bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig) |
6de9cd9a | 751 | { |
83737db2 DB |
752 | bitmap_iterator bi; |
753 | unsigned int i; | |
6de9cd9a | 754 | |
d75dbccd | 755 | if (dest != orig) |
83737db2 | 756 | { |
5c72d561 JH |
757 | bitmap_head temp; |
758 | bitmap_initialize (&temp, &grand_bitmap_obstack); | |
89fb70a3 | 759 | |
5c72d561 JH |
760 | bitmap_and_into (&dest->values, &orig->values); |
761 | bitmap_copy (&temp, &dest->expressions); | |
762 | EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi) | |
d75dbccd | 763 | { |
c9145754 DB |
764 | pre_expr expr = expression_for_id (i); |
765 | unsigned int value_id = get_expr_value_id (expr); | |
5c72d561 JH |
766 | if (!bitmap_bit_p (&dest->values, value_id)) |
767 | bitmap_clear_bit (&dest->expressions, i); | |
d75dbccd | 768 | } |
5c72d561 | 769 | bitmap_clear (&temp); |
6de9cd9a DN |
770 | } |
771 | } | |
772 | ||
83737db2 | 773 | /* Subtract all values and expressions contained in ORIG from DEST. */ |
7e6eb623 | 774 | |
83737db2 DB |
775 | static bitmap_set_t |
776 | bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig) | |
7e6eb623 | 777 | { |
83737db2 DB |
778 | bitmap_set_t result = bitmap_set_new (); |
779 | bitmap_iterator bi; | |
780 | unsigned int i; | |
b9c5e484 | 781 | |
5c72d561 JH |
782 | bitmap_and_compl (&result->expressions, &dest->expressions, |
783 | &orig->expressions); | |
6de9cd9a | 784 | |
83737db2 DB |
785 | FOR_EACH_EXPR_ID_IN_SET (result, i, bi) |
786 | { | |
c9145754 DB |
787 | pre_expr expr = expression_for_id (i); |
788 | unsigned int value_id = get_expr_value_id (expr); | |
5c72d561 | 789 | bitmap_set_bit (&result->values, value_id); |
83737db2 | 790 | } |
af75a7ea | 791 | |
83737db2 | 792 | return result; |
6b416da1 DB |
793 | } |
794 | ||
d75dbccd DB |
795 | /* Subtract all the values in bitmap set B from bitmap set A. */ |
796 | ||
797 | static void | |
798 | bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b) | |
799 | { | |
800 | unsigned int i; | |
801 | bitmap_iterator bi; | |
5c72d561 | 802 | bitmap_head temp; |
d75dbccd | 803 | |
5c72d561 JH |
804 | bitmap_initialize (&temp, &grand_bitmap_obstack); |
805 | ||
806 | bitmap_copy (&temp, &a->expressions); | |
807 | EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi) | |
d75dbccd | 808 | { |
c9145754 DB |
809 | pre_expr expr = expression_for_id (i); |
810 | if (bitmap_set_contains_value (b, get_expr_value_id (expr))) | |
d75dbccd DB |
811 | bitmap_remove_from_set (a, expr); |
812 | } | |
5c72d561 | 813 | bitmap_clear (&temp); |
d75dbccd DB |
814 | } |
815 | ||
816 | ||
c9145754 | 817 | /* Return true if bitmapped set SET contains the value VALUE_ID. */ |
6de9cd9a | 818 | |
bdee7684 | 819 | static bool |
c9145754 | 820 | bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id) |
6de9cd9a | 821 | { |
c9145754 | 822 | if (value_id_constant_p (value_id)) |
bdee7684 | 823 | return true; |
83737db2 | 824 | |
5c72d561 | 825 | if (!set || bitmap_empty_p (&set->expressions)) |
83737db2 DB |
826 | return false; |
827 | ||
5c72d561 | 828 | return bitmap_bit_p (&set->values, value_id); |
bdee7684 | 829 | } |
7e6eb623 | 830 | |
d75dbccd | 831 | static inline bool |
c9145754 | 832 | bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr) |
d75dbccd | 833 | { |
5c72d561 | 834 | return bitmap_bit_p (&set->expressions, get_expression_id (expr)); |
d75dbccd DB |
835 | } |
836 | ||
bdee7684 | 837 | /* Replace an instance of value LOOKFOR with expression EXPR in SET. */ |
7e6eb623 | 838 | |
bdee7684 | 839 | static void |
c9145754 DB |
840 | bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor, |
841 | const pre_expr expr) | |
bdee7684 | 842 | { |
4d4b1b30 | 843 | bitmap exprset; |
83737db2 DB |
844 | unsigned int i; |
845 | bitmap_iterator bi; | |
846 | ||
c9145754 | 847 | if (value_id_constant_p (lookfor)) |
bdee7684 | 848 | return; |
83737db2 | 849 | |
bdee7684 DB |
850 | if (!bitmap_set_contains_value (set, lookfor)) |
851 | return; | |
e9284566 | 852 | |
6b416da1 DB |
853 | /* The number of expressions having a given value is usually |
854 | significantly less than the total number of expressions in SET. | |
855 | Thus, rather than check, for each expression in SET, whether it | |
856 | has the value LOOKFOR, we walk the reverse mapping that tells us | |
857 | what expressions have a given value, and see if any of those | |
858 | expressions are in our set. For large testcases, this is about | |
859 | 5-10x faster than walking the bitmap. If this is somehow a | |
860 | significant lose for some cases, we can choose which set to walk | |
861 | based on the set size. */ | |
9771b263 | 862 | exprset = value_expressions[lookfor]; |
4d4b1b30 | 863 | EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi) |
6de9cd9a | 864 | { |
fcaa4ca4 | 865 | if (bitmap_clear_bit (&set->expressions, i)) |
6de9cd9a | 866 | { |
5c72d561 | 867 | bitmap_set_bit (&set->expressions, get_expression_id (expr)); |
83737db2 | 868 | return; |
6de9cd9a | 869 | } |
6de9cd9a | 870 | } |
4bcc5786 RB |
871 | |
872 | gcc_unreachable (); | |
6de9cd9a DN |
873 | } |
874 | ||
83737db2 | 875 | /* Return true if two bitmap sets are equal. */ |
6de9cd9a | 876 | |
7e6eb623 | 877 | static bool |
83737db2 | 878 | bitmap_set_equal (bitmap_set_t a, bitmap_set_t b) |
6de9cd9a | 879 | { |
5c72d561 | 880 | return bitmap_equal_p (&a->values, &b->values); |
6de9cd9a DN |
881 | } |
882 | ||
e9284566 | 883 | /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists, |
6c6cfbfd | 884 | and add it otherwise. */ |
bdee7684 | 885 | |
7e6eb623 | 886 | static void |
c9145754 | 887 | bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr) |
7e6eb623 | 888 | { |
c9145754 | 889 | unsigned int val = get_expr_value_id (expr); |
83737db2 | 890 | |
e9284566 DB |
891 | if (bitmap_set_contains_value (set, val)) |
892 | bitmap_set_replace_value (set, val, expr); | |
893 | else | |
894 | bitmap_insert_into_set (set, expr); | |
bdee7684 | 895 | } |
7e6eb623 | 896 | |
bdee7684 DB |
897 | /* Insert EXPR into SET if EXPR's value is not already present in |
898 | SET. */ | |
899 | ||
900 | static void | |
c9145754 | 901 | bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr) |
bdee7684 | 902 | { |
c9145754 | 903 | unsigned int val = get_expr_value_id (expr); |
af75a7ea | 904 | |
77a74ed7 | 905 | gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr)); |
1befacc8 RG |
906 | |
907 | /* Constant values are always considered to be part of the set. */ | |
908 | if (value_id_constant_p (val)) | |
909 | return; | |
910 | ||
911 | /* If the value membership changed, add the expression. */ | |
5c72d561 JH |
912 | if (bitmap_set_bit (&set->values, val)) |
913 | bitmap_set_bit (&set->expressions, expr->id); | |
7e6eb623 | 914 | } |
6de9cd9a | 915 | |
c9145754 DB |
916 | /* Print out EXPR to outfile. */ |
917 | ||
918 | static void | |
919 | print_pre_expr (FILE *outfile, const pre_expr expr) | |
920 | { | |
921 | switch (expr->kind) | |
922 | { | |
923 | case CONSTANT: | |
924 | print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0); | |
925 | break; | |
926 | case NAME: | |
927 | print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0); | |
928 | break; | |
929 | case NARY: | |
930 | { | |
931 | unsigned int i; | |
932 | vn_nary_op_t nary = PRE_EXPR_NARY (expr); | |
5806f481 | 933 | fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode)); |
c9145754 DB |
934 | for (i = 0; i < nary->length; i++) |
935 | { | |
936 | print_generic_expr (outfile, nary->op[i], 0); | |
937 | if (i != (unsigned) nary->length - 1) | |
938 | fprintf (outfile, ","); | |
939 | } | |
940 | fprintf (outfile, "}"); | |
941 | } | |
942 | break; | |
943 | ||
944 | case REFERENCE: | |
945 | { | |
946 | vn_reference_op_t vro; | |
947 | unsigned int i; | |
948 | vn_reference_t ref = PRE_EXPR_REFERENCE (expr); | |
949 | fprintf (outfile, "{"); | |
950 | for (i = 0; | |
9771b263 | 951 | ref->operands.iterate (i, &vro); |
c9145754 DB |
952 | i++) |
953 | { | |
5006671f | 954 | bool closebrace = false; |
c9145754 DB |
955 | if (vro->opcode != SSA_NAME |
956 | && TREE_CODE_CLASS (vro->opcode) != tcc_declaration) | |
5006671f | 957 | { |
5806f481 | 958 | fprintf (outfile, "%s", get_tree_code_name (vro->opcode)); |
5006671f RG |
959 | if (vro->op0) |
960 | { | |
961 | fprintf (outfile, "<"); | |
962 | closebrace = true; | |
963 | } | |
964 | } | |
c9145754 DB |
965 | if (vro->op0) |
966 | { | |
c9145754 DB |
967 | print_generic_expr (outfile, vro->op0, 0); |
968 | if (vro->op1) | |
969 | { | |
970 | fprintf (outfile, ","); | |
971 | print_generic_expr (outfile, vro->op1, 0); | |
972 | } | |
5006671f RG |
973 | if (vro->op2) |
974 | { | |
975 | fprintf (outfile, ","); | |
976 | print_generic_expr (outfile, vro->op2, 0); | |
977 | } | |
c9145754 | 978 | } |
5006671f RG |
979 | if (closebrace) |
980 | fprintf (outfile, ">"); | |
9771b263 | 981 | if (i != ref->operands.length () - 1) |
c9145754 DB |
982 | fprintf (outfile, ","); |
983 | } | |
984 | fprintf (outfile, "}"); | |
5006671f RG |
985 | if (ref->vuse) |
986 | { | |
987 | fprintf (outfile, "@"); | |
988 | print_generic_expr (outfile, ref->vuse, 0); | |
989 | } | |
c9145754 DB |
990 | } |
991 | break; | |
992 | } | |
993 | } | |
994 | void debug_pre_expr (pre_expr); | |
995 | ||
996 | /* Like print_pre_expr but always prints to stderr. */ | |
24e47c76 | 997 | DEBUG_FUNCTION void |
c9145754 DB |
998 | debug_pre_expr (pre_expr e) |
999 | { | |
1000 | print_pre_expr (stderr, e); | |
1001 | fprintf (stderr, "\n"); | |
1002 | } | |
1003 | ||
bdee7684 DB |
1004 | /* Print out SET to OUTFILE. */ |
1005 | ||
1006 | static void | |
83737db2 DB |
1007 | print_bitmap_set (FILE *outfile, bitmap_set_t set, |
1008 | const char *setname, int blockindex) | |
bdee7684 DB |
1009 | { |
1010 | fprintf (outfile, "%s[%d] := { ", setname, blockindex); | |
1011 | if (set) | |
1012 | { | |
cf6b9ef1 | 1013 | bool first = true; |
3cd8c58a | 1014 | unsigned i; |
87c476a2 ZD |
1015 | bitmap_iterator bi; |
1016 | ||
83737db2 | 1017 | FOR_EACH_EXPR_ID_IN_SET (set, i, bi) |
87c476a2 | 1018 | { |
c9145754 | 1019 | const pre_expr expr = expression_for_id (i); |
83737db2 | 1020 | |
65a6f342 NS |
1021 | if (!first) |
1022 | fprintf (outfile, ", "); | |
1023 | first = false; | |
c9145754 | 1024 | print_pre_expr (outfile, expr); |
b9c5e484 | 1025 | |
c9145754 | 1026 | fprintf (outfile, " (%04d)", get_expr_value_id (expr)); |
87c476a2 | 1027 | } |
bdee7684 DB |
1028 | } |
1029 | fprintf (outfile, " }\n"); | |
1030 | } | |
6de9cd9a | 1031 | |
83737db2 | 1032 | void debug_bitmap_set (bitmap_set_t); |
6de9cd9a | 1033 | |
24e47c76 | 1034 | DEBUG_FUNCTION void |
83737db2 DB |
1035 | debug_bitmap_set (bitmap_set_t set) |
1036 | { | |
1037 | print_bitmap_set (stderr, set, "debug", 0); | |
7e6eb623 DB |
1038 | } |
1039 | ||
19372838 RG |
1040 | void debug_bitmap_sets_for (basic_block); |
1041 | ||
1042 | DEBUG_FUNCTION void | |
1043 | debug_bitmap_sets_for (basic_block bb) | |
1044 | { | |
1045 | print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index); | |
40b178f4 RG |
1046 | print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index); |
1047 | print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index); | |
1048 | print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index); | |
1049 | print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index); | |
1050 | if (do_partial_partial) | |
1051 | print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index); | |
1052 | print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index); | |
19372838 RG |
1053 | } |
1054 | ||
7e6eb623 | 1055 | /* Print out the expressions that have VAL to OUTFILE. */ |
33c94679 | 1056 | |
6bcfb753 | 1057 | static void |
c9145754 | 1058 | print_value_expressions (FILE *outfile, unsigned int val) |
7e6eb623 | 1059 | { |
9771b263 | 1060 | bitmap set = value_expressions[val]; |
c9145754 | 1061 | if (set) |
33c94679 | 1062 | { |
4d4b1b30 | 1063 | bitmap_set x; |
33c94679 | 1064 | char s[10]; |
c9145754 | 1065 | sprintf (s, "%04d", val); |
4d4b1b30 RG |
1066 | x.expressions = *set; |
1067 | print_bitmap_set (outfile, &x, s, 0); | |
33c94679 | 1068 | } |
6de9cd9a DN |
1069 | } |
1070 | ||
6de9cd9a | 1071 | |
24e47c76 | 1072 | DEBUG_FUNCTION void |
c9145754 | 1073 | debug_value_expressions (unsigned int val) |
6de9cd9a | 1074 | { |
7e6eb623 DB |
1075 | print_value_expressions (stderr, val); |
1076 | } | |
1077 | ||
c9145754 DB |
1078 | /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to |
1079 | represent it. */ | |
0995a441 | 1080 | |
c9145754 DB |
1081 | static pre_expr |
1082 | get_or_alloc_expr_for_constant (tree constant) | |
b9c5e484 | 1083 | { |
c9145754 DB |
1084 | unsigned int result_id; |
1085 | unsigned int value_id; | |
5f5126d6 RG |
1086 | struct pre_expr_d expr; |
1087 | pre_expr newexpr; | |
1088 | ||
1089 | expr.kind = CONSTANT; | |
1090 | PRE_EXPR_CONSTANT (&expr) = constant; | |
1091 | result_id = lookup_expression_id (&expr); | |
1092 | if (result_id != 0) | |
1093 | return expression_for_id (result_id); | |
1094 | ||
971540bd | 1095 | newexpr = pre_expr_pool.allocate (); |
c9145754 DB |
1096 | newexpr->kind = CONSTANT; |
1097 | PRE_EXPR_CONSTANT (newexpr) = constant; | |
5f5126d6 | 1098 | alloc_expression_id (newexpr); |
c9145754 | 1099 | value_id = get_or_alloc_constant_value_id (constant); |
c9145754 DB |
1100 | add_to_value (value_id, newexpr); |
1101 | return newexpr; | |
0995a441 SB |
1102 | } |
1103 | ||
c9145754 DB |
1104 | /* Given a value id V, find the actual tree representing the constant |
1105 | value if there is one, and return it. Return NULL if we can't find | |
1106 | a constant. */ | |
43da81be DB |
1107 | |
1108 | static tree | |
726a989a | 1109 | get_constant_for_value_id (unsigned int v) |
43da81be | 1110 | { |
c9145754 DB |
1111 | if (value_id_constant_p (v)) |
1112 | { | |
1113 | unsigned int i; | |
1114 | bitmap_iterator bi; | |
9771b263 | 1115 | bitmap exprset = value_expressions[v]; |
c9145754 | 1116 | |
4d4b1b30 | 1117 | EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi) |
c9145754 DB |
1118 | { |
1119 | pre_expr expr = expression_for_id (i); | |
726a989a | 1120 | if (expr->kind == CONSTANT) |
c9145754 DB |
1121 | return PRE_EXPR_CONSTANT (expr); |
1122 | } | |
1123 | } | |
1124 | return NULL; | |
1125 | } | |
1126 | ||
1127 | /* Get or allocate a pre_expr for a piece of GIMPLE, and return it. | |
1128 | Currently only supports constants and SSA_NAMES. */ | |
1129 | static pre_expr | |
1130 | get_or_alloc_expr_for (tree t) | |
1131 | { | |
1132 | if (TREE_CODE (t) == SSA_NAME) | |
1133 | return get_or_alloc_expr_for_name (t); | |
1d65f45c | 1134 | else if (is_gimple_min_invariant (t)) |
c9145754 | 1135 | return get_or_alloc_expr_for_constant (t); |
726a989a RB |
1136 | else |
1137 | { | |
1138 | /* More complex expressions can result from SCCVN expression | |
1139 | simplification that inserts values for them. As they all | |
1140 | do not have VOPs the get handled by the nary ops struct. */ | |
1141 | vn_nary_op_t result; | |
1142 | unsigned int result_id; | |
1143 | vn_nary_op_lookup (t, &result); | |
1144 | if (result != NULL) | |
1145 | { | |
971540bd | 1146 | pre_expr e = pre_expr_pool.allocate (); |
726a989a RB |
1147 | e->kind = NARY; |
1148 | PRE_EXPR_NARY (e) = result; | |
1149 | result_id = lookup_expression_id (e); | |
1150 | if (result_id != 0) | |
1151 | { | |
971540bd | 1152 | pre_expr_pool.remove (e); |
726a989a RB |
1153 | e = expression_for_id (result_id); |
1154 | return e; | |
1155 | } | |
1156 | alloc_expression_id (e); | |
1157 | return e; | |
1158 | } | |
1159 | } | |
c9145754 DB |
1160 | return NULL; |
1161 | } | |
1162 | ||
1163 | /* Return the folded version of T if T, when folded, is a gimple | |
1164 | min_invariant. Otherwise, return T. */ | |
1165 | ||
1166 | static pre_expr | |
1167 | fully_constant_expression (pre_expr e) | |
1168 | { | |
1169 | switch (e->kind) | |
1170 | { | |
1171 | case CONSTANT: | |
1172 | return e; | |
1173 | case NARY: | |
1174 | { | |
1175 | vn_nary_op_t nary = PRE_EXPR_NARY (e); | |
1176 | switch (TREE_CODE_CLASS (nary->opcode)) | |
1177 | { | |
1178 | case tcc_binary: | |
40f64141 | 1179 | case tcc_comparison: |
c9145754 DB |
1180 | { |
1181 | /* We have to go from trees to pre exprs to value ids to | |
1182 | constants. */ | |
1183 | tree naryop0 = nary->op[0]; | |
1184 | tree naryop1 = nary->op[1]; | |
40f64141 RG |
1185 | tree result; |
1186 | if (!is_gimple_min_invariant (naryop0)) | |
726a989a RB |
1187 | { |
1188 | pre_expr rep0 = get_or_alloc_expr_for (naryop0); | |
1189 | unsigned int vrep0 = get_expr_value_id (rep0); | |
40f64141 RG |
1190 | tree const0 = get_constant_for_value_id (vrep0); |
1191 | if (const0) | |
1192 | naryop0 = fold_convert (TREE_TYPE (naryop0), const0); | |
726a989a | 1193 | } |
40f64141 | 1194 | if (!is_gimple_min_invariant (naryop1)) |
726a989a RB |
1195 | { |
1196 | pre_expr rep1 = get_or_alloc_expr_for (naryop1); | |
1197 | unsigned int vrep1 = get_expr_value_id (rep1); | |
40f64141 RG |
1198 | tree const1 = get_constant_for_value_id (vrep1); |
1199 | if (const1) | |
1200 | naryop1 = fold_convert (TREE_TYPE (naryop1), const1); | |
b463e8de | 1201 | } |
40f64141 RG |
1202 | result = fold_binary (nary->opcode, nary->type, |
1203 | naryop0, naryop1); | |
c9145754 DB |
1204 | if (result && is_gimple_min_invariant (result)) |
1205 | return get_or_alloc_expr_for_constant (result); | |
40f64141 RG |
1206 | /* We might have simplified the expression to a |
1207 | SSA_NAME for example from x_1 * 1. But we cannot | |
1208 | insert a PHI for x_1 unconditionally as x_1 might | |
1209 | not be available readily. */ | |
c9145754 DB |
1210 | return e; |
1211 | } | |
40f64141 RG |
1212 | case tcc_reference: |
1213 | if (nary->opcode != REALPART_EXPR | |
b8698a0f | 1214 | && nary->opcode != IMAGPART_EXPR |
40f64141 RG |
1215 | && nary->opcode != VIEW_CONVERT_EXPR) |
1216 | return e; | |
1217 | /* Fallthrough. */ | |
c9145754 DB |
1218 | case tcc_unary: |
1219 | { | |
40f64141 RG |
1220 | /* We have to go from trees to pre exprs to value ids to |
1221 | constants. */ | |
c9145754 | 1222 | tree naryop0 = nary->op[0]; |
726a989a RB |
1223 | tree const0, result; |
1224 | if (is_gimple_min_invariant (naryop0)) | |
1225 | const0 = naryop0; | |
1226 | else | |
1227 | { | |
1228 | pre_expr rep0 = get_or_alloc_expr_for (naryop0); | |
1229 | unsigned int vrep0 = get_expr_value_id (rep0); | |
1230 | const0 = get_constant_for_value_id (vrep0); | |
1231 | } | |
1232 | result = NULL; | |
c9145754 | 1233 | if (const0) |
b463e8de DB |
1234 | { |
1235 | tree type1 = TREE_TYPE (nary->op[0]); | |
1236 | const0 = fold_convert (type1, const0); | |
1237 | result = fold_unary (nary->opcode, nary->type, const0); | |
1238 | } | |
c9145754 DB |
1239 | if (result && is_gimple_min_invariant (result)) |
1240 | return get_or_alloc_expr_for_constant (result); | |
1241 | return e; | |
1242 | } | |
1243 | default: | |
1244 | return e; | |
1245 | } | |
1246 | } | |
47af7a5c RG |
1247 | case REFERENCE: |
1248 | { | |
1249 | vn_reference_t ref = PRE_EXPR_REFERENCE (e); | |
12bd5a1e RG |
1250 | tree folded; |
1251 | if ((folded = fully_constant_vn_reference_p (ref))) | |
1252 | return get_or_alloc_expr_for_constant (folded); | |
1253 | return e; | |
1254 | } | |
c9145754 DB |
1255 | default: |
1256 | return e; | |
1257 | } | |
1258 | return e; | |
43da81be DB |
1259 | } |
1260 | ||
5006671f | 1261 | /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that |
84280917 MM |
1262 | it has the value it would have in BLOCK. Set *SAME_VALID to true |
1263 | in case the new vuse doesn't change the value id of the OPERANDS. */ | |
c90186eb | 1264 | |
5006671f | 1265 | static tree |
9771b263 | 1266 | translate_vuse_through_block (vec<vn_reference_op_s> operands, |
b45d2719 | 1267 | alias_set_type set, tree type, tree vuse, |
5006671f | 1268 | basic_block phiblock, |
84280917 | 1269 | basic_block block, bool *same_valid) |
c90186eb | 1270 | { |
355fe088 | 1271 | gimple *phi = SSA_NAME_DEF_STMT (vuse); |
b45d2719 | 1272 | ao_ref ref; |
84280917 MM |
1273 | edge e = NULL; |
1274 | bool use_oracle; | |
1275 | ||
1276 | *same_valid = true; | |
43da81be | 1277 | |
5006671f RG |
1278 | if (gimple_bb (phi) != phiblock) |
1279 | return vuse; | |
1280 | ||
84280917 | 1281 | use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands); |
5006671f RG |
1282 | |
1283 | /* Use the alias-oracle to find either the PHI node in this block, | |
1284 | the first VUSE used in this block that is equivalent to vuse or | |
1285 | the first VUSE which definition in this block kills the value. */ | |
84280917 MM |
1286 | if (gimple_code (phi) == GIMPLE_PHI) |
1287 | e = find_edge (block, phiblock); | |
1288 | else if (use_oracle) | |
1289 | while (!stmt_may_clobber_ref_p_1 (phi, &ref)) | |
1290 | { | |
1291 | vuse = gimple_vuse (phi); | |
1292 | phi = SSA_NAME_DEF_STMT (vuse); | |
1293 | if (gimple_bb (phi) != phiblock) | |
1294 | return vuse; | |
1295 | if (gimple_code (phi) == GIMPLE_PHI) | |
1296 | { | |
1297 | e = find_edge (block, phiblock); | |
1298 | break; | |
1299 | } | |
1300 | } | |
1301 | else | |
1302 | return NULL_TREE; | |
1303 | ||
1304 | if (e) | |
c90186eb | 1305 | { |
84280917 | 1306 | if (use_oracle) |
5006671f | 1307 | { |
84280917 | 1308 | bitmap visited = NULL; |
9bb06c2a | 1309 | unsigned int cnt; |
84280917 MM |
1310 | /* Try to find a vuse that dominates this phi node by skipping |
1311 | non-clobbering statements. */ | |
50f0aa20 RB |
1312 | vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false, |
1313 | NULL, NULL); | |
84280917 MM |
1314 | if (visited) |
1315 | BITMAP_FREE (visited); | |
5006671f | 1316 | } |
84280917 MM |
1317 | else |
1318 | vuse = NULL_TREE; | |
1319 | if (!vuse) | |
1320 | { | |
1321 | /* If we didn't find any, the value ID can't stay the same, | |
1322 | but return the translated vuse. */ | |
1323 | *same_valid = false; | |
1324 | vuse = PHI_ARG_DEF (phi, e->dest_idx); | |
1325 | } | |
1326 | /* ??? We would like to return vuse here as this is the canonical | |
1327 | upmost vdef that this reference is associated with. But during | |
1328 | insertion of the references into the hash tables we only ever | |
1329 | directly insert with their direct gimple_vuse, hence returning | |
1330 | something else would make us not find the other expression. */ | |
1331 | return PHI_ARG_DEF (phi, e->dest_idx); | |
c90186eb | 1332 | } |
c90186eb | 1333 | |
5006671f | 1334 | return NULL_TREE; |
c90186eb | 1335 | } |
83737db2 | 1336 | |
249eb506 | 1337 | /* Like bitmap_find_leader, but checks for the value existing in SET1 *or* |
83737db2 DB |
1338 | SET2. This is used to avoid making a set consisting of the union |
1339 | of PA_IN and ANTIC_IN during insert. */ | |
1340 | ||
c9145754 DB |
1341 | static inline pre_expr |
1342 | find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2) | |
83737db2 | 1343 | { |
c9145754 | 1344 | pre_expr result; |
83737db2 | 1345 | |
e076271b | 1346 | result = bitmap_find_leader (set1, val); |
83737db2 | 1347 | if (!result && set2) |
e076271b | 1348 | result = bitmap_find_leader (set2, val); |
83737db2 DB |
1349 | return result; |
1350 | } | |
1351 | ||
c9145754 DB |
1352 | /* Get the tree type for our PRE expression e. */ |
1353 | ||
1354 | static tree | |
1355 | get_expr_type (const pre_expr e) | |
1356 | { | |
1357 | switch (e->kind) | |
1358 | { | |
1359 | case NAME: | |
1360 | return TREE_TYPE (PRE_EXPR_NAME (e)); | |
1361 | case CONSTANT: | |
1362 | return TREE_TYPE (PRE_EXPR_CONSTANT (e)); | |
1363 | case REFERENCE: | |
b45d2719 | 1364 | return PRE_EXPR_REFERENCE (e)->type; |
c9145754 DB |
1365 | case NARY: |
1366 | return PRE_EXPR_NARY (e)->type; | |
1367 | } | |
c3284718 | 1368 | gcc_unreachable (); |
c9145754 DB |
1369 | } |
1370 | ||
1371 | /* Get a representative SSA_NAME for a given expression. | |
1372 | Since all of our sub-expressions are treated as values, we require | |
1373 | them to be SSA_NAME's for simplicity. | |
1374 | Prior versions of GVNPRE used to use "value handles" here, so that | |
1375 | an expression would be VH.11 + VH.10 instead of d_3 + e_6. In | |
1376 | either case, the operands are really values (IE we do not expect | |
1377 | them to be usable without finding leaders). */ | |
1378 | ||
1379 | static tree | |
1380 | get_representative_for (const pre_expr e) | |
1381 | { | |
c9145754 DB |
1382 | tree name; |
1383 | unsigned int value_id = get_expr_value_id (e); | |
1384 | ||
1385 | switch (e->kind) | |
1386 | { | |
1387 | case NAME: | |
1388 | return PRE_EXPR_NAME (e); | |
1389 | case CONSTANT: | |
726a989a | 1390 | return PRE_EXPR_CONSTANT (e); |
c9145754 DB |
1391 | case NARY: |
1392 | case REFERENCE: | |
1393 | { | |
1394 | /* Go through all of the expressions representing this value | |
1395 | and pick out an SSA_NAME. */ | |
1396 | unsigned int i; | |
1397 | bitmap_iterator bi; | |
9771b263 | 1398 | bitmap exprs = value_expressions[value_id]; |
4d4b1b30 | 1399 | EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi) |
c9145754 DB |
1400 | { |
1401 | pre_expr rep = expression_for_id (i); | |
1402 | if (rep->kind == NAME) | |
1403 | return PRE_EXPR_NAME (rep); | |
a044f0e7 RB |
1404 | else if (rep->kind == CONSTANT) |
1405 | return PRE_EXPR_CONSTANT (rep); | |
c9145754 DB |
1406 | } |
1407 | } | |
1408 | break; | |
1409 | } | |
a044f0e7 | 1410 | |
c9145754 DB |
1411 | /* If we reached here we couldn't find an SSA_NAME. This can |
1412 | happen when we've discovered a value that has never appeared in | |
a044f0e7 RB |
1413 | the program as set to an SSA_NAME, as the result of phi translation. |
1414 | Create one here. | |
1415 | ??? We should be able to re-use this when we insert the statement | |
1416 | to compute it. */ | |
83d5977e | 1417 | name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp"); |
c9145754 | 1418 | VN_INFO_GET (name)->value_id = value_id; |
a044f0e7 RB |
1419 | VN_INFO (name)->valnum = name; |
1420 | /* ??? For now mark this SSA name for release by SCCVN. */ | |
1421 | VN_INFO (name)->needs_insertion = true; | |
c9145754 | 1422 | add_to_value (value_id, get_or_alloc_expr_for_name (name)); |
a044f0e7 | 1423 | if (dump_file && (dump_flags & TDF_DETAILS)) |
c9145754 DB |
1424 | { |
1425 | fprintf (dump_file, "Created SSA_NAME representative "); | |
1426 | print_generic_expr (dump_file, name, 0); | |
1427 | fprintf (dump_file, " for expression:"); | |
1428 | print_pre_expr (dump_file, e); | |
984af6ac | 1429 | fprintf (dump_file, " (%04d)\n", value_id); |
c9145754 DB |
1430 | } |
1431 | ||
1432 | return name; | |
1433 | } | |
1434 | ||
1435 | ||
1436 | ||
3e999e7b RG |
1437 | static pre_expr |
1438 | phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, | |
1439 | basic_block pred, basic_block phiblock); | |
c9145754 | 1440 | |
7e6eb623 | 1441 | /* Translate EXPR using phis in PHIBLOCK, so that it has the values of |
788d04b2 RG |
1442 | the phis in PRED. Return NULL if we can't find a leader for each part |
1443 | of the translated expression. */ | |
6de9cd9a | 1444 | |
c9145754 | 1445 | static pre_expr |
3e999e7b RG |
1446 | phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, |
1447 | basic_block pred, basic_block phiblock) | |
6de9cd9a | 1448 | { |
c9145754 | 1449 | switch (expr->kind) |
6de9cd9a | 1450 | { |
c9145754 | 1451 | case NARY: |
43da81be | 1452 | { |
c9145754 DB |
1453 | unsigned int i; |
1454 | bool changed = false; | |
1455 | vn_nary_op_t nary = PRE_EXPR_NARY (expr); | |
5a7d7f9c RG |
1456 | vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s, |
1457 | sizeof_vn_nary_op (nary->length)); | |
1458 | memcpy (newnary, nary, sizeof_vn_nary_op (nary->length)); | |
c9145754 | 1459 | |
5a7d7f9c | 1460 | for (i = 0; i < newnary->length; i++) |
43da81be | 1461 | { |
5a7d7f9c | 1462 | if (TREE_CODE (newnary->op[i]) != SSA_NAME) |
c9145754 DB |
1463 | continue; |
1464 | else | |
43da81be | 1465 | { |
af078bb0 | 1466 | pre_expr leader, result; |
5a7d7f9c | 1467 | unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id; |
af078bb0 | 1468 | leader = find_leader_in_sets (op_val_id, set1, set2); |
788d04b2 | 1469 | result = phi_translate (leader, set1, set2, pred, phiblock); |
c9145754 | 1470 | if (result && result != leader) |
43da81be | 1471 | { |
c9145754 DB |
1472 | tree name = get_representative_for (result); |
1473 | if (!name) | |
85300b46 | 1474 | return NULL; |
5a7d7f9c | 1475 | newnary->op[i] = name; |
43da81be | 1476 | } |
c9145754 DB |
1477 | else if (!result) |
1478 | return NULL; | |
0778d4e8 | 1479 | |
5a7d7f9c | 1480 | changed |= newnary->op[i] != nary->op[i]; |
0778d4e8 | 1481 | } |
c9145754 DB |
1482 | } |
1483 | if (changed) | |
1484 | { | |
1485 | pre_expr constant; | |
5f5126d6 | 1486 | unsigned int new_val_id; |
c9145754 | 1487 | |
5a7d7f9c RG |
1488 | tree result = vn_nary_op_lookup_pieces (newnary->length, |
1489 | newnary->opcode, | |
1490 | newnary->type, | |
1491 | &newnary->op[0], | |
c9145754 | 1492 | &nary); |
5f5126d6 RG |
1493 | if (result && is_gimple_min_invariant (result)) |
1494 | return get_or_alloc_expr_for_constant (result); | |
c9145754 | 1495 | |
971540bd | 1496 | expr = pre_expr_pool.allocate (); |
c9145754 DB |
1497 | expr->kind = NARY; |
1498 | expr->id = 0; | |
c9145754 DB |
1499 | if (nary) |
1500 | { | |
1501 | PRE_EXPR_NARY (expr) = nary; | |
1502 | constant = fully_constant_expression (expr); | |
1503 | if (constant != expr) | |
1504 | return constant; | |
0778d4e8 | 1505 | |
c9145754 DB |
1506 | new_val_id = nary->value_id; |
1507 | get_or_alloc_expression_id (expr); | |
1508 | } | |
1509 | else | |
43da81be | 1510 | { |
c9145754 | 1511 | new_val_id = get_next_value_id (); |
c3284718 | 1512 | value_expressions.safe_grow_cleared (get_max_value_id () + 1); |
5a7d7f9c RG |
1513 | nary = vn_nary_op_insert_pieces (newnary->length, |
1514 | newnary->opcode, | |
1515 | newnary->type, | |
1516 | &newnary->op[0], | |
c9145754 DB |
1517 | result, new_val_id); |
1518 | PRE_EXPR_NARY (expr) = nary; | |
1519 | constant = fully_constant_expression (expr); | |
1520 | if (constant != expr) | |
1521 | return constant; | |
1522 | get_or_alloc_expression_id (expr); | |
43da81be | 1523 | } |
b0a0ab2d | 1524 | add_to_value (new_val_id, expr); |
c5830edf | 1525 | } |
c9145754 | 1526 | return expr; |
c90186eb | 1527 | } |
c9145754 | 1528 | break; |
47af7a5c | 1529 | |
c9145754 | 1530 | case REFERENCE: |
c90186eb | 1531 | { |
c9145754 | 1532 | vn_reference_t ref = PRE_EXPR_REFERENCE (expr); |
9771b263 | 1533 | vec<vn_reference_op_s> operands = ref->operands; |
5006671f RG |
1534 | tree vuse = ref->vuse; |
1535 | tree newvuse = vuse; | |
6e1aa848 | 1536 | vec<vn_reference_op_s> newoperands = vNULL; |
84280917 | 1537 | bool changed = false, same_valid = true; |
27d79c21 | 1538 | unsigned int i, n; |
c9145754 DB |
1539 | vn_reference_op_t operand; |
1540 | vn_reference_t newref; | |
1541 | ||
27d79c21 | 1542 | for (i = 0; operands.iterate (i, &operand); i++) |
e13f1c14 | 1543 | { |
c9145754 DB |
1544 | pre_expr opresult; |
1545 | pre_expr leader; | |
4da80bfb | 1546 | tree op[3]; |
c9145754 DB |
1547 | tree type = operand->type; |
1548 | vn_reference_op_s newop = *operand; | |
4da80bfb RG |
1549 | op[0] = operand->op0; |
1550 | op[1] = operand->op1; | |
1551 | op[2] = operand->op2; | |
1552 | for (n = 0; n < 3; ++n) | |
e13f1c14 | 1553 | { |
4da80bfb RG |
1554 | unsigned int op_val_id; |
1555 | if (!op[n]) | |
1556 | continue; | |
1557 | if (TREE_CODE (op[n]) != SSA_NAME) | |
c9145754 | 1558 | { |
4da80bfb RG |
1559 | /* We can't possibly insert these. */ |
1560 | if (n != 0 | |
1561 | && !is_gimple_min_invariant (op[n])) | |
b5d76df4 | 1562 | break; |
4da80bfb | 1563 | continue; |
c9145754 | 1564 | } |
4da80bfb | 1565 | op_val_id = VN_INFO (op[n])->value_id; |
c9145754 | 1566 | leader = find_leader_in_sets (op_val_id, set1, set2); |
4da80bfb RG |
1567 | if (!leader) |
1568 | break; | |
984af6ac RB |
1569 | opresult = phi_translate (leader, set1, set2, pred, phiblock); |
1570 | if (!opresult) | |
1571 | break; | |
1572 | if (opresult != leader) | |
c9145754 | 1573 | { |
984af6ac RB |
1574 | tree name = get_representative_for (opresult); |
1575 | if (!name) | |
b5d76df4 | 1576 | break; |
984af6ac RB |
1577 | changed |= name != op[n]; |
1578 | op[n] = name; | |
c9145754 | 1579 | } |
e13f1c14 | 1580 | } |
4da80bfb | 1581 | if (n != 3) |
e13f1c14 | 1582 | { |
9771b263 | 1583 | newoperands.release (); |
4da80bfb | 1584 | return NULL; |
e13f1c14 | 1585 | } |
27d79c21 RB |
1586 | if (!changed) |
1587 | continue; | |
9771b263 DN |
1588 | if (!newoperands.exists ()) |
1589 | newoperands = operands.copy (); | |
c9145754 | 1590 | /* We may have changed from an SSA_NAME to a constant */ |
4da80bfb RG |
1591 | if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME) |
1592 | newop.opcode = TREE_CODE (op[0]); | |
c9145754 | 1593 | newop.type = type; |
4da80bfb RG |
1594 | newop.op0 = op[0]; |
1595 | newop.op1 = op[1]; | |
1596 | newop.op2 = op[2]; | |
27d79c21 | 1597 | newoperands[i] = newop; |
b5d76df4 | 1598 | } |
27d79c21 | 1599 | gcc_checking_assert (i == operands.length ()); |
c90186eb | 1600 | |
5006671f RG |
1601 | if (vuse) |
1602 | { | |
27d79c21 RB |
1603 | newvuse = translate_vuse_through_block (newoperands.exists () |
1604 | ? newoperands : operands, | |
b45d2719 | 1605 | ref->set, ref->type, |
84280917 MM |
1606 | vuse, phiblock, pred, |
1607 | &same_valid); | |
5006671f RG |
1608 | if (newvuse == NULL_TREE) |
1609 | { | |
9771b263 | 1610 | newoperands.release (); |
5006671f RG |
1611 | return NULL; |
1612 | } | |
1613 | } | |
b9c5e484 | 1614 | |
84280917 | 1615 | if (changed || newvuse != vuse) |
c90186eb | 1616 | { |
47af7a5c RG |
1617 | unsigned int new_val_id; |
1618 | pre_expr constant; | |
1619 | ||
b45d2719 RG |
1620 | tree result = vn_reference_lookup_pieces (newvuse, ref->set, |
1621 | ref->type, | |
27d79c21 RB |
1622 | newoperands.exists () |
1623 | ? newoperands : operands, | |
3bc27de7 | 1624 | &newref, VN_WALK); |
12bd5a1e | 1625 | if (result) |
9771b263 | 1626 | newoperands.release (); |
c90186eb | 1627 | |
efe7068b RG |
1628 | /* We can always insert constants, so if we have a partial |
1629 | redundant constant load of another type try to translate it | |
1630 | to a constant of appropriate type. */ | |
1631 | if (result && is_gimple_min_invariant (result)) | |
70f34814 | 1632 | { |
efe7068b RG |
1633 | tree tem = result; |
1634 | if (!useless_type_conversion_p (ref->type, TREE_TYPE (result))) | |
1635 | { | |
1636 | tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result); | |
1637 | if (tem && !is_gimple_min_invariant (tem)) | |
1638 | tem = NULL_TREE; | |
1639 | } | |
1640 | if (tem) | |
1641 | return get_or_alloc_expr_for_constant (tem); | |
70f34814 | 1642 | } |
efe7068b RG |
1643 | |
1644 | /* If we'd have to convert things we would need to validate | |
1645 | if we can insert the translated expression. So fail | |
1646 | here for now - we cannot insert an alias with a different | |
1647 | type in the VN tables either, as that would assert. */ | |
1648 | if (result | |
1649 | && !useless_type_conversion_p (ref->type, TREE_TYPE (result))) | |
1650 | return NULL; | |
48feba28 RG |
1651 | else if (!result && newref |
1652 | && !useless_type_conversion_p (ref->type, newref->type)) | |
1653 | { | |
9771b263 | 1654 | newoperands.release (); |
48feba28 RG |
1655 | return NULL; |
1656 | } | |
70f34814 | 1657 | |
971540bd | 1658 | expr = pre_expr_pool.allocate (); |
c9145754 DB |
1659 | expr->kind = REFERENCE; |
1660 | expr->id = 0; | |
6615c446 | 1661 | |
efe7068b | 1662 | if (newref) |
0995a441 | 1663 | { |
c9145754 | 1664 | PRE_EXPR_REFERENCE (expr) = newref; |
47af7a5c RG |
1665 | constant = fully_constant_expression (expr); |
1666 | if (constant != expr) | |
1667 | return constant; | |
1668 | ||
c9145754 DB |
1669 | new_val_id = newref->value_id; |
1670 | get_or_alloc_expression_id (expr); | |
0995a441 SB |
1671 | } |
1672 | else | |
1673 | { | |
84280917 MM |
1674 | if (changed || !same_valid) |
1675 | { | |
1676 | new_val_id = get_next_value_id (); | |
c3284718 RS |
1677 | value_expressions.safe_grow_cleared |
1678 | (get_max_value_id () + 1); | |
84280917 MM |
1679 | } |
1680 | else | |
1681 | new_val_id = ref->value_id; | |
27d79c21 RB |
1682 | if (!newoperands.exists ()) |
1683 | newoperands = operands.copy (); | |
b45d2719 RG |
1684 | newref = vn_reference_insert_pieces (newvuse, ref->set, |
1685 | ref->type, | |
c9145754 DB |
1686 | newoperands, |
1687 | result, new_val_id); | |
27d79c21 | 1688 | newoperands = vNULL; |
c9145754 | 1689 | PRE_EXPR_REFERENCE (expr) = newref; |
47af7a5c RG |
1690 | constant = fully_constant_expression (expr); |
1691 | if (constant != expr) | |
1692 | return constant; | |
c9145754 | 1693 | get_or_alloc_expression_id (expr); |
0995a441 | 1694 | } |
b0a0ab2d | 1695 | add_to_value (new_val_id, expr); |
7e6eb623 | 1696 | } |
9771b263 | 1697 | newoperands.release (); |
c9145754 | 1698 | return expr; |
7e6eb623 | 1699 | } |
c9145754 | 1700 | break; |
47af7a5c | 1701 | |
c9145754 | 1702 | case NAME: |
7e6eb623 | 1703 | { |
c9145754 | 1704 | tree name = PRE_EXPR_NAME (expr); |
355fe088 | 1705 | gimple *def_stmt = SSA_NAME_DEF_STMT (name); |
e076271b RG |
1706 | /* If the SSA name is defined by a PHI node in this block, |
1707 | translate it. */ | |
726a989a RB |
1708 | if (gimple_code (def_stmt) == GIMPLE_PHI |
1709 | && gimple_bb (def_stmt) == phiblock) | |
9323afae | 1710 | { |
e076271b RG |
1711 | edge e = find_edge (pred, gimple_bb (def_stmt)); |
1712 | tree def = PHI_ARG_DEF (def_stmt, e->dest_idx); | |
73a63870 | 1713 | |
c9145754 | 1714 | /* Handle constant. */ |
89fb70a3 | 1715 | if (is_gimple_min_invariant (def)) |
c9145754 | 1716 | return get_or_alloc_expr_for_constant (def); |
070b797d | 1717 | |
e076271b | 1718 | return get_or_alloc_expr_for_name (def); |
9323afae | 1719 | } |
dee29e84 RB |
1720 | /* Otherwise return it unchanged - it will get removed if its |
1721 | value is not available in PREDs AVAIL_OUT set of expressions | |
1722 | by the subtraction of TMP_GEN. */ | |
e076271b | 1723 | return expr; |
7e6eb623 | 1724 | } |
6615c446 JO |
1725 | |
1726 | default: | |
1727 | gcc_unreachable (); | |
6de9cd9a DN |
1728 | } |
1729 | } | |
f8b04195 | 1730 | |
3e999e7b RG |
1731 | /* Wrapper around phi_translate_1 providing caching functionality. */ |
1732 | ||
1733 | static pre_expr | |
1734 | phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, | |
1735 | basic_block pred, basic_block phiblock) | |
1736 | { | |
984af6ac | 1737 | expr_pred_trans_t slot = NULL; |
3e999e7b RG |
1738 | pre_expr phitrans; |
1739 | ||
1740 | if (!expr) | |
1741 | return NULL; | |
1742 | ||
1743 | /* Constants contain no values that need translation. */ | |
1744 | if (expr->kind == CONSTANT) | |
1745 | return expr; | |
1746 | ||
1747 | if (value_id_constant_p (get_expr_value_id (expr))) | |
1748 | return expr; | |
1749 | ||
984af6ac | 1750 | /* Don't add translations of NAMEs as those are cheap to translate. */ |
3e999e7b RG |
1751 | if (expr->kind != NAME) |
1752 | { | |
984af6ac RB |
1753 | if (phi_trans_add (&slot, expr, pred)) |
1754 | return slot->v; | |
1755 | /* Store NULL for the value we want to return in the case of | |
1756 | recursing. */ | |
1757 | slot->v = NULL; | |
3e999e7b RG |
1758 | } |
1759 | ||
1760 | /* Translate. */ | |
1761 | phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock); | |
1762 | ||
984af6ac | 1763 | if (slot) |
e2c2fde2 RB |
1764 | { |
1765 | if (phitrans) | |
1766 | slot->v = phitrans; | |
1767 | else | |
1768 | /* Remove failed translations again, they cause insert | |
1769 | iteration to not pick up new opportunities reliably. */ | |
c203e8a7 | 1770 | phi_translate_table->remove_elt_with_hash (slot, slot->hashcode); |
e2c2fde2 | 1771 | } |
3e999e7b RG |
1772 | |
1773 | return phitrans; | |
1774 | } | |
1775 | ||
1776 | ||
c9145754 | 1777 | /* For each expression in SET, translate the values through phi nodes |
f5594471 DB |
1778 | in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting |
1779 | expressions in DEST. */ | |
1780 | ||
6de9cd9a | 1781 | static void |
83737db2 | 1782 | phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred, |
7e6eb623 | 1783 | basic_block phiblock) |
6de9cd9a | 1784 | { |
9771b263 | 1785 | vec<pre_expr> exprs; |
c9145754 | 1786 | pre_expr expr; |
83737db2 DB |
1787 | int i; |
1788 | ||
9adf0570 | 1789 | if (gimple_seq_empty_p (phi_nodes (phiblock))) |
6de9cd9a | 1790 | { |
83737db2 DB |
1791 | bitmap_set_copy (dest, set); |
1792 | return; | |
1793 | } | |
b9c5e484 | 1794 | |
83737db2 | 1795 | exprs = sorted_array_from_bitmap_set (set); |
9771b263 | 1796 | FOR_EACH_VEC_ELT (exprs, i, expr) |
83737db2 | 1797 | { |
c9145754 | 1798 | pre_expr translated; |
f8b04195 | 1799 | translated = phi_translate (expr, set, NULL, pred, phiblock); |
3ed7d068 RG |
1800 | if (!translated) |
1801 | continue; | |
c90186eb | 1802 | |
3ed7d068 RG |
1803 | /* We might end up with multiple expressions from SET being |
1804 | translated to the same value. In this case we do not want | |
1805 | to retain the NARY or REFERENCE expression but prefer a NAME | |
1806 | which would be the leader. */ | |
1807 | if (translated->kind == NAME) | |
1808 | bitmap_value_replace_in_set (dest, translated); | |
1809 | else | |
83737db2 | 1810 | bitmap_value_insert_into_set (dest, translated); |
b9c5e484 | 1811 | } |
9771b263 | 1812 | exprs.release (); |
6de9cd9a DN |
1813 | } |
1814 | ||
bdee7684 | 1815 | /* Find the leader for a value (i.e., the name representing that |
984af6ac RB |
1816 | value) in a given set, and return it. Return NULL if no leader |
1817 | is found. */ | |
bdee7684 | 1818 | |
c9145754 | 1819 | static pre_expr |
e076271b | 1820 | bitmap_find_leader (bitmap_set_t set, unsigned int val) |
bdee7684 | 1821 | { |
c9145754 DB |
1822 | if (value_id_constant_p (val)) |
1823 | { | |
1824 | unsigned int i; | |
1825 | bitmap_iterator bi; | |
9771b263 | 1826 | bitmap exprset = value_expressions[val]; |
83737db2 | 1827 | |
4d4b1b30 | 1828 | EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi) |
c9145754 DB |
1829 | { |
1830 | pre_expr expr = expression_for_id (i); | |
1831 | if (expr->kind == CONSTANT) | |
1832 | return expr; | |
1833 | } | |
1834 | } | |
bdee7684 DB |
1835 | if (bitmap_set_contains_value (set, val)) |
1836 | { | |
6b416da1 DB |
1837 | /* Rather than walk the entire bitmap of expressions, and see |
1838 | whether any of them has the value we are looking for, we look | |
1839 | at the reverse mapping, which tells us the set of expressions | |
1840 | that have a given value (IE value->expressions with that | |
1841 | value) and see if any of those expressions are in our set. | |
1842 | The number of expressions per value is usually significantly | |
1843 | less than the number of expressions in the set. In fact, for | |
1844 | large testcases, doing it this way is roughly 5-10x faster | |
1845 | than walking the bitmap. | |
1846 | If this is somehow a significant lose for some cases, we can | |
b9c5e484 | 1847 | choose which set to walk based on which set is smaller. */ |
83737db2 DB |
1848 | unsigned int i; |
1849 | bitmap_iterator bi; | |
9771b263 | 1850 | bitmap exprset = value_expressions[val]; |
b9c5e484 | 1851 | |
4d4b1b30 | 1852 | EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi) |
e076271b | 1853 | return expression_for_id (i); |
6de9cd9a | 1854 | } |
7e6eb623 | 1855 | return NULL; |
6de9cd9a DN |
1856 | } |
1857 | ||
cea618ac | 1858 | /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of |
1e4816bc DB |
1859 | BLOCK by seeing if it is not killed in the block. Note that we are |
1860 | only determining whether there is a store that kills it. Because | |
1861 | of the order in which clean iterates over values, we are guaranteed | |
1862 | that altered operands will have caused us to be eliminated from the | |
1863 | ANTIC_IN set already. */ | |
c90186eb DB |
1864 | |
1865 | static bool | |
c9145754 | 1866 | value_dies_in_block_x (pre_expr expr, basic_block block) |
c90186eb | 1867 | { |
5006671f RG |
1868 | tree vuse = PRE_EXPR_REFERENCE (expr)->vuse; |
1869 | vn_reference_t refx = PRE_EXPR_REFERENCE (expr); | |
355fe088 | 1870 | gimple *def; |
5006671f RG |
1871 | gimple_stmt_iterator gsi; |
1872 | unsigned id = get_expression_id (expr); | |
1873 | bool res = false; | |
b45d2719 | 1874 | ao_ref ref; |
89fb70a3 | 1875 | |
5006671f RG |
1876 | if (!vuse) |
1877 | return false; | |
1878 | ||
1879 | /* Lookup a previously calculated result. */ | |
1880 | if (EXPR_DIES (block) | |
1881 | && bitmap_bit_p (EXPR_DIES (block), id * 2)) | |
1882 | return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1); | |
1883 | ||
1884 | /* A memory expression {e, VUSE} dies in the block if there is a | |
1885 | statement that may clobber e. If, starting statement walk from the | |
1886 | top of the basic block, a statement uses VUSE there can be no kill | |
1887 | inbetween that use and the original statement that loaded {e, VUSE}, | |
1888 | so we can stop walking. */ | |
b45d2719 | 1889 | ref.base = NULL_TREE; |
5006671f | 1890 | for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi)) |
c90186eb | 1891 | { |
5006671f RG |
1892 | tree def_vuse, def_vdef; |
1893 | def = gsi_stmt (gsi); | |
1894 | def_vuse = gimple_vuse (def); | |
1895 | def_vdef = gimple_vdef (def); | |
89fb70a3 | 1896 | |
5006671f RG |
1897 | /* Not a memory statement. */ |
1898 | if (!def_vuse) | |
1e4816bc | 1899 | continue; |
5006671f RG |
1900 | |
1901 | /* Not a may-def. */ | |
1902 | if (!def_vdef) | |
1903 | { | |
1904 | /* A load with the same VUSE, we're done. */ | |
1905 | if (def_vuse == vuse) | |
1906 | break; | |
1907 | ||
1908 | continue; | |
1909 | } | |
1910 | ||
1911 | /* Init ref only if we really need it. */ | |
b45d2719 RG |
1912 | if (ref.base == NULL_TREE |
1913 | && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type, | |
1914 | refx->operands)) | |
5006671f | 1915 | { |
b45d2719 RG |
1916 | res = true; |
1917 | break; | |
5006671f RG |
1918 | } |
1919 | /* If the statement may clobber expr, it dies. */ | |
b45d2719 | 1920 | if (stmt_may_clobber_ref_p_1 (def, &ref)) |
5006671f RG |
1921 | { |
1922 | res = true; | |
1923 | break; | |
1924 | } | |
c90186eb | 1925 | } |
5006671f RG |
1926 | |
1927 | /* Remember the result. */ | |
1928 | if (!EXPR_DIES (block)) | |
1929 | EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack); | |
1930 | bitmap_set_bit (EXPR_DIES (block), id * 2); | |
1931 | if (res) | |
1932 | bitmap_set_bit (EXPR_DIES (block), id * 2 + 1); | |
1933 | ||
1934 | return res; | |
c90186eb DB |
1935 | } |
1936 | ||
6de9cd9a | 1937 | |
19372838 RG |
1938 | /* Determine if OP is valid in SET1 U SET2, which it is when the union |
1939 | contains its value-id. */ | |
83737db2 | 1940 | |
6de9cd9a | 1941 | static bool |
19372838 | 1942 | op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op) |
6de9cd9a | 1943 | { |
19372838 | 1944 | if (op && TREE_CODE (op) == SSA_NAME) |
c9145754 | 1945 | { |
19372838 | 1946 | unsigned int value_id = VN_INFO (op)->value_id; |
92290a18 RG |
1947 | if (!(bitmap_set_contains_value (set1, value_id) |
1948 | || (set2 && bitmap_set_contains_value (set2, value_id)))) | |
c9145754 DB |
1949 | return false; |
1950 | } | |
c9145754 DB |
1951 | return true; |
1952 | } | |
b9c5e484 | 1953 | |
c9145754 DB |
1954 | /* Determine if the expression EXPR is valid in SET1 U SET2. |
1955 | ONLY SET2 CAN BE NULL. | |
1956 | This means that we have a leader for each part of the expression | |
1957 | (if it consists of values), or the expression is an SSA_NAME. | |
7763473e | 1958 | For loads/calls, we also see if the vuse is killed in this block. */ |
5039610b | 1959 | |
c9145754 | 1960 | static bool |
dee29e84 | 1961 | valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr) |
c9145754 DB |
1962 | { |
1963 | switch (expr->kind) | |
1964 | { | |
1965 | case NAME: | |
dee29e84 RB |
1966 | /* By construction all NAMEs are available. Non-available |
1967 | NAMEs are removed by subtracting TMP_GEN from the sets. */ | |
1968 | return true; | |
c9145754 | 1969 | case NARY: |
43da81be | 1970 | { |
c9145754 DB |
1971 | unsigned int i; |
1972 | vn_nary_op_t nary = PRE_EXPR_NARY (expr); | |
1973 | for (i = 0; i < nary->length; i++) | |
19372838 RG |
1974 | if (!op_valid_in_sets (set1, set2, nary->op[i])) |
1975 | return false; | |
c9145754 | 1976 | return true; |
43da81be | 1977 | } |
c9145754 DB |
1978 | break; |
1979 | case REFERENCE: | |
c90186eb | 1980 | { |
c9145754 DB |
1981 | vn_reference_t ref = PRE_EXPR_REFERENCE (expr); |
1982 | vn_reference_op_t vro; | |
1983 | unsigned int i; | |
1984 | ||
9771b263 | 1985 | FOR_EACH_VEC_ELT (ref->operands, i, vro) |
c90186eb | 1986 | { |
19372838 RG |
1987 | if (!op_valid_in_sets (set1, set2, vro->op0) |
1988 | || !op_valid_in_sets (set1, set2, vro->op1) | |
1989 | || !op_valid_in_sets (set1, set2, vro->op2)) | |
e13f1c14 | 1990 | return false; |
c90186eb | 1991 | } |
cd32bb90 | 1992 | return true; |
c90186eb | 1993 | } |
6615c446 | 1994 | default: |
b9c5e484 | 1995 | gcc_unreachable (); |
c9145754 | 1996 | } |
6de9cd9a DN |
1997 | } |
1998 | ||
d75dbccd DB |
1999 | /* Clean the set of expressions that are no longer valid in SET1 or |
2000 | SET2. This means expressions that are made up of values we have no | |
2001 | leaders for in SET1 or SET2. This version is used for partial | |
2002 | anticipation, which means it is not valid in either ANTIC_IN or | |
2003 | PA_IN. */ | |
2004 | ||
2005 | static void | |
dee29e84 | 2006 | dependent_clean (bitmap_set_t set1, bitmap_set_t set2) |
d75dbccd | 2007 | { |
9771b263 | 2008 | vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1); |
c9145754 | 2009 | pre_expr expr; |
d75dbccd DB |
2010 | int i; |
2011 | ||
9771b263 | 2012 | FOR_EACH_VEC_ELT (exprs, i, expr) |
d75dbccd | 2013 | { |
dee29e84 | 2014 | if (!valid_in_sets (set1, set2, expr)) |
d75dbccd DB |
2015 | bitmap_remove_from_set (set1, expr); |
2016 | } | |
9771b263 | 2017 | exprs.release (); |
d75dbccd DB |
2018 | } |
2019 | ||
ca072a31 DB |
2020 | /* Clean the set of expressions that are no longer valid in SET. This |
2021 | means expressions that are made up of values we have no leaders for | |
2022 | in SET. */ | |
6de9cd9a DN |
2023 | |
2024 | static void | |
dee29e84 | 2025 | clean (bitmap_set_t set) |
6de9cd9a | 2026 | { |
9771b263 | 2027 | vec<pre_expr> exprs = sorted_array_from_bitmap_set (set); |
c9145754 | 2028 | pre_expr expr; |
83737db2 DB |
2029 | int i; |
2030 | ||
9771b263 | 2031 | FOR_EACH_VEC_ELT (exprs, i, expr) |
6de9cd9a | 2032 | { |
dee29e84 | 2033 | if (!valid_in_sets (set, NULL, expr)) |
83737db2 | 2034 | bitmap_remove_from_set (set, expr); |
6de9cd9a | 2035 | } |
9771b263 | 2036 | exprs.release (); |
6de9cd9a DN |
2037 | } |
2038 | ||
cd32bb90 | 2039 | /* Clean the set of expressions that are no longer valid in SET because |
bea966c2 | 2040 | they are clobbered in BLOCK or because they trap and may not be executed. */ |
cd32bb90 RG |
2041 | |
2042 | static void | |
2043 | prune_clobbered_mems (bitmap_set_t set, basic_block block) | |
2044 | { | |
bea966c2 RG |
2045 | bitmap_iterator bi; |
2046 | unsigned i; | |
cd32bb90 | 2047 | |
bea966c2 | 2048 | FOR_EACH_EXPR_ID_IN_SET (set, i, bi) |
cd32bb90 | 2049 | { |
bea966c2 RG |
2050 | pre_expr expr = expression_for_id (i); |
2051 | if (expr->kind == REFERENCE) | |
2052 | { | |
2053 | vn_reference_t ref = PRE_EXPR_REFERENCE (expr); | |
2054 | if (ref->vuse) | |
2055 | { | |
355fe088 | 2056 | gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse); |
bea966c2 RG |
2057 | if (!gimple_nop_p (def_stmt) |
2058 | && ((gimple_bb (def_stmt) != block | |
2059 | && !dominated_by_p (CDI_DOMINATORS, | |
2060 | block, gimple_bb (def_stmt))) | |
2061 | || (gimple_bb (def_stmt) == block | |
2062 | && value_dies_in_block_x (expr, block)))) | |
2063 | bitmap_remove_from_set (set, expr); | |
2064 | } | |
2065 | } | |
2066 | else if (expr->kind == NARY) | |
cd32bb90 | 2067 | { |
bea966c2 RG |
2068 | vn_nary_op_t nary = PRE_EXPR_NARY (expr); |
2069 | /* If the NARY may trap make sure the block does not contain | |
2070 | a possible exit point. | |
2071 | ??? This is overly conservative if we translate AVAIL_OUT | |
2072 | as the available expression might be after the exit point. */ | |
2073 | if (BB_MAY_NOTRETURN (block) | |
2074 | && vn_nary_may_trap (nary)) | |
cd32bb90 RG |
2075 | bitmap_remove_from_set (set, expr); |
2076 | } | |
2077 | } | |
cd32bb90 RG |
2078 | } |
2079 | ||
d4222d43 | 2080 | static sbitmap has_abnormal_preds; |
89fb70a3 | 2081 | |
83737db2 DB |
2082 | /* List of blocks that may have changed during ANTIC computation and |
2083 | thus need to be iterated over. */ | |
2084 | ||
2085 | static sbitmap changed_blocks; | |
1e4816bc | 2086 | |
7e6eb623 | 2087 | /* Compute the ANTIC set for BLOCK. |
6de9cd9a | 2088 | |
665fcad8 SB |
2089 | If succs(BLOCK) > 1 then |
2090 | ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK) | |
2091 | else if succs(BLOCK) == 1 then | |
2092 | ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)]) | |
6de9cd9a | 2093 | |
665fcad8 | 2094 | ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK]) |
83737db2 | 2095 | */ |
6de9cd9a | 2096 | |
7e6eb623 | 2097 | static bool |
a28fee03 | 2098 | compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge) |
6de9cd9a | 2099 | { |
7e6eb623 | 2100 | bool changed = false; |
83737db2 DB |
2101 | bitmap_set_t S, old, ANTIC_OUT; |
2102 | bitmap_iterator bi; | |
2103 | unsigned int bii; | |
2104 | edge e; | |
2105 | edge_iterator ei; | |
a28fee03 | 2106 | |
83737db2 | 2107 | old = ANTIC_OUT = S = NULL; |
d75dbccd | 2108 | BB_VISITED (block) = 1; |
a28fee03 SB |
2109 | |
2110 | /* If any edges from predecessors are abnormal, antic_in is empty, | |
2111 | so do nothing. */ | |
2112 | if (block_has_abnormal_pred_edge) | |
2113 | goto maybe_dump_sets; | |
6de9cd9a | 2114 | |
83737db2 DB |
2115 | old = ANTIC_IN (block); |
2116 | ANTIC_OUT = bitmap_set_new (); | |
6de9cd9a | 2117 | |
a28fee03 SB |
2118 | /* If the block has no successors, ANTIC_OUT is empty. */ |
2119 | if (EDGE_COUNT (block->succs) == 0) | |
2120 | ; | |
7e6eb623 DB |
2121 | /* If we have one successor, we could have some phi nodes to |
2122 | translate through. */ | |
c5cbcccf | 2123 | else if (single_succ_p (block)) |
6de9cd9a | 2124 | { |
83737db2 | 2125 | basic_block succ_bb = single_succ (block); |
52e4630c RB |
2126 | gcc_assert (BB_VISITED (succ_bb)); |
2127 | phi_translate_set (ANTIC_OUT, ANTIC_IN (succ_bb), block, succ_bb); | |
6de9cd9a | 2128 | } |
7e6eb623 | 2129 | /* If we have multiple successors, we take the intersection of all of |
1e4816bc DB |
2130 | them. Note that in the case of loop exit phi nodes, we may have |
2131 | phis to translate through. */ | |
7e6eb623 | 2132 | else |
6de9cd9a | 2133 | { |
7e6eb623 | 2134 | size_t i; |
70a6b17e | 2135 | basic_block bprime, first = NULL; |
7e6eb623 | 2136 | |
ef062b13 | 2137 | auto_vec<basic_block> worklist (EDGE_COUNT (block->succs)); |
628f6a4e | 2138 | FOR_EACH_EDGE (e, ei, block->succs) |
1e4816bc | 2139 | { |
70a6b17e RG |
2140 | if (!first |
2141 | && BB_VISITED (e->dest)) | |
2142 | first = e->dest; | |
2143 | else if (BB_VISITED (e->dest)) | |
9771b263 | 2144 | worklist.quick_push (e->dest); |
1e4816bc | 2145 | } |
70a6b17e | 2146 | |
52e4630c RB |
2147 | /* Of multiple successors we have to have visited one already |
2148 | which is guaranteed by iteration order. */ | |
2149 | gcc_assert (first != NULL); | |
c90186eb | 2150 | |
52e4630c | 2151 | phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first); |
70a6b17e | 2152 | |
9771b263 | 2153 | FOR_EACH_VEC_ELT (worklist, i, bprime) |
d75dbccd | 2154 | { |
9adf0570 | 2155 | if (!gimple_seq_empty_p (phi_nodes (bprime))) |
1e4816bc DB |
2156 | { |
2157 | bitmap_set_t tmp = bitmap_set_new (); | |
70a6b17e | 2158 | phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime); |
1e4816bc DB |
2159 | bitmap_set_and (ANTIC_OUT, tmp); |
2160 | bitmap_set_free (tmp); | |
2161 | } | |
89fb70a3 | 2162 | else |
70a6b17e | 2163 | bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime)); |
6de9cd9a DN |
2164 | } |
2165 | } | |
6de9cd9a | 2166 | |
cd32bb90 RG |
2167 | /* Prune expressions that are clobbered in block and thus become |
2168 | invalid if translated from ANTIC_OUT to ANTIC_IN. */ | |
2169 | prune_clobbered_mems (ANTIC_OUT, block); | |
2170 | ||
ea4b7848 | 2171 | /* Generate ANTIC_OUT - TMP_GEN. */ |
83737db2 | 2172 | S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block)); |
6de9cd9a | 2173 | |
d75dbccd | 2174 | /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */ |
83737db2 DB |
2175 | ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block), |
2176 | TMP_GEN (block)); | |
c33bae88 | 2177 | |
a28fee03 SB |
2178 | /* Then union in the ANTIC_OUT - TMP_GEN values, |
2179 | to get ANTIC_OUT U EXP_GEN - TMP_GEN */ | |
83737db2 DB |
2180 | FOR_EACH_EXPR_ID_IN_SET (S, bii, bi) |
2181 | bitmap_value_insert_into_set (ANTIC_IN (block), | |
2182 | expression_for_id (bii)); | |
6de9cd9a | 2183 | |
dee29e84 | 2184 | clean (ANTIC_IN (block)); |
d75dbccd | 2185 | |
5c72d561 | 2186 | if (!bitmap_set_equal (old, ANTIC_IN (block))) |
83737db2 DB |
2187 | { |
2188 | changed = true; | |
d7c028c0 | 2189 | bitmap_set_bit (changed_blocks, block->index); |
83737db2 | 2190 | FOR_EACH_EDGE (e, ei, block->preds) |
d7c028c0 | 2191 | bitmap_set_bit (changed_blocks, e->src->index); |
83737db2 DB |
2192 | } |
2193 | else | |
d7c028c0 | 2194 | bitmap_clear_bit (changed_blocks, block->index); |
6de9cd9a | 2195 | |
a28fee03 | 2196 | maybe_dump_sets: |
7e6eb623 DB |
2197 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2198 | { | |
52e4630c RB |
2199 | if (ANTIC_OUT) |
2200 | print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index); | |
85300b46 | 2201 | |
52e4630c RB |
2202 | print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN", |
2203 | block->index); | |
85300b46 | 2204 | |
52e4630c RB |
2205 | if (S) |
2206 | print_bitmap_set (dump_file, S, "S", block->index); | |
83737db2 DB |
2207 | } |
2208 | if (old) | |
2209 | bitmap_set_free (old); | |
2210 | if (S) | |
2211 | bitmap_set_free (S); | |
2212 | if (ANTIC_OUT) | |
2213 | bitmap_set_free (ANTIC_OUT); | |
7e6eb623 | 2214 | return changed; |
6de9cd9a DN |
2215 | } |
2216 | ||
d75dbccd DB |
2217 | /* Compute PARTIAL_ANTIC for BLOCK. |
2218 | ||
2219 | If succs(BLOCK) > 1 then | |
2220 | PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not | |
2221 | in ANTIC_OUT for all succ(BLOCK) | |
2222 | else if succs(BLOCK) == 1 then | |
2223 | PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)]) | |
2224 | ||
2225 | PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] | |
2226 | - ANTIC_IN[BLOCK]) | |
2227 | ||
2228 | */ | |
2229 | static bool | |
2230 | compute_partial_antic_aux (basic_block block, | |
2231 | bool block_has_abnormal_pred_edge) | |
2232 | { | |
2233 | bool changed = false; | |
2234 | bitmap_set_t old_PA_IN; | |
2235 | bitmap_set_t PA_OUT; | |
2236 | edge e; | |
2237 | edge_iterator ei; | |
f0ed4cfb | 2238 | unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH); |
d75dbccd DB |
2239 | |
2240 | old_PA_IN = PA_OUT = NULL; | |
2241 | ||
2242 | /* If any edges from predecessors are abnormal, antic_in is empty, | |
2243 | so do nothing. */ | |
2244 | if (block_has_abnormal_pred_edge) | |
2245 | goto maybe_dump_sets; | |
2246 | ||
f0ed4cfb NC |
2247 | /* If there are too many partially anticipatable values in the |
2248 | block, phi_translate_set can take an exponential time: stop | |
2249 | before the translation starts. */ | |
2250 | if (max_pa | |
2251 | && single_succ_p (block) | |
5c72d561 | 2252 | && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa) |
f0ed4cfb NC |
2253 | goto maybe_dump_sets; |
2254 | ||
d75dbccd DB |
2255 | old_PA_IN = PA_IN (block); |
2256 | PA_OUT = bitmap_set_new (); | |
2257 | ||
2258 | /* If the block has no successors, ANTIC_OUT is empty. */ | |
2259 | if (EDGE_COUNT (block->succs) == 0) | |
2260 | ; | |
2261 | /* If we have one successor, we could have some phi nodes to | |
2262 | translate through. Note that we can't phi translate across DFS | |
89fb70a3 DB |
2263 | back edges in partial antic, because it uses a union operation on |
2264 | the successors. For recurrences like IV's, we will end up | |
2265 | generating a new value in the set on each go around (i + 3 (VH.1) | |
2266 | VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */ | |
d75dbccd DB |
2267 | else if (single_succ_p (block)) |
2268 | { | |
2269 | basic_block succ = single_succ (block); | |
2270 | if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK)) | |
2271 | phi_translate_set (PA_OUT, PA_IN (succ), block, succ); | |
2272 | } | |
2273 | /* If we have multiple successors, we take the union of all of | |
2274 | them. */ | |
2275 | else | |
2276 | { | |
d75dbccd DB |
2277 | size_t i; |
2278 | basic_block bprime; | |
2279 | ||
ef062b13 | 2280 | auto_vec<basic_block> worklist (EDGE_COUNT (block->succs)); |
d75dbccd DB |
2281 | FOR_EACH_EDGE (e, ei, block->succs) |
2282 | { | |
2283 | if (e->flags & EDGE_DFS_BACK) | |
2284 | continue; | |
9771b263 | 2285 | worklist.quick_push (e->dest); |
d75dbccd | 2286 | } |
9771b263 | 2287 | if (worklist.length () > 0) |
d75dbccd | 2288 | { |
9771b263 | 2289 | FOR_EACH_VEC_ELT (worklist, i, bprime) |
d75dbccd DB |
2290 | { |
2291 | unsigned int i; | |
2292 | bitmap_iterator bi; | |
2293 | ||
2294 | FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi) | |
2295 | bitmap_value_insert_into_set (PA_OUT, | |
2296 | expression_for_id (i)); | |
9adf0570 | 2297 | if (!gimple_seq_empty_p (phi_nodes (bprime))) |
1e4816bc DB |
2298 | { |
2299 | bitmap_set_t pa_in = bitmap_set_new (); | |
2300 | phi_translate_set (pa_in, PA_IN (bprime), block, bprime); | |
2301 | FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi) | |
2302 | bitmap_value_insert_into_set (PA_OUT, | |
2303 | expression_for_id (i)); | |
2304 | bitmap_set_free (pa_in); | |
2305 | } | |
2306 | else | |
2307 | FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi) | |
2308 | bitmap_value_insert_into_set (PA_OUT, | |
2309 | expression_for_id (i)); | |
d75dbccd DB |
2310 | } |
2311 | } | |
d75dbccd DB |
2312 | } |
2313 | ||
cd32bb90 RG |
2314 | /* Prune expressions that are clobbered in block and thus become |
2315 | invalid if translated from PA_OUT to PA_IN. */ | |
2316 | prune_clobbered_mems (PA_OUT, block); | |
2317 | ||
d75dbccd DB |
2318 | /* PA_IN starts with PA_OUT - TMP_GEN. |
2319 | Then we subtract things from ANTIC_IN. */ | |
2320 | PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block)); | |
2321 | ||
2322 | /* For partial antic, we want to put back in the phi results, since | |
2323 | we will properly avoid making them partially antic over backedges. */ | |
5c72d561 JH |
2324 | bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values); |
2325 | bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions); | |
d75dbccd DB |
2326 | |
2327 | /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */ | |
2328 | bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block)); | |
2329 | ||
dee29e84 | 2330 | dependent_clean (PA_IN (block), ANTIC_IN (block)); |
d75dbccd DB |
2331 | |
2332 | if (!bitmap_set_equal (old_PA_IN, PA_IN (block))) | |
2333 | { | |
2334 | changed = true; | |
d7c028c0 | 2335 | bitmap_set_bit (changed_blocks, block->index); |
d75dbccd | 2336 | FOR_EACH_EDGE (e, ei, block->preds) |
d7c028c0 | 2337 | bitmap_set_bit (changed_blocks, e->src->index); |
d75dbccd DB |
2338 | } |
2339 | else | |
d7c028c0 | 2340 | bitmap_clear_bit (changed_blocks, block->index); |
d75dbccd DB |
2341 | |
2342 | maybe_dump_sets: | |
2343 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2344 | { | |
2345 | if (PA_OUT) | |
2346 | print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index); | |
2347 | ||
2348 | print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index); | |
2349 | } | |
2350 | if (old_PA_IN) | |
2351 | bitmap_set_free (old_PA_IN); | |
2352 | if (PA_OUT) | |
2353 | bitmap_set_free (PA_OUT); | |
2354 | return changed; | |
2355 | } | |
2356 | ||
83737db2 | 2357 | /* Compute ANTIC and partial ANTIC sets. */ |
6de9cd9a DN |
2358 | |
2359 | static void | |
7e6eb623 | 2360 | compute_antic (void) |
6de9cd9a | 2361 | { |
c33bae88 | 2362 | bool changed = true; |
7e6eb623 | 2363 | int num_iterations = 0; |
c33bae88 | 2364 | basic_block block; |
83737db2 | 2365 | int i; |
a28fee03 SB |
2366 | |
2367 | /* If any predecessor edges are abnormal, we punt, so antic_in is empty. | |
2368 | We pre-build the map of blocks with incoming abnormal edges here. */ | |
8b1c6fd7 | 2369 | has_abnormal_preds = sbitmap_alloc (last_basic_block_for_fn (cfun)); |
f61e445a | 2370 | bitmap_clear (has_abnormal_preds); |
83737db2 | 2371 | |
04a90bec | 2372 | FOR_ALL_BB_FN (block, cfun) |
7e6eb623 | 2373 | { |
a28fee03 SB |
2374 | edge_iterator ei; |
2375 | edge e; | |
2376 | ||
2377 | FOR_EACH_EDGE (e, ei, block->preds) | |
83737db2 DB |
2378 | { |
2379 | e->flags &= ~EDGE_DFS_BACK; | |
2380 | if (e->flags & EDGE_ABNORMAL) | |
2381 | { | |
d7c028c0 | 2382 | bitmap_set_bit (has_abnormal_preds, block->index); |
83737db2 DB |
2383 | break; |
2384 | } | |
2385 | } | |
a28fee03 | 2386 | |
83737db2 | 2387 | BB_VISITED (block) = 0; |
a19eb9d2 | 2388 | |
a28fee03 | 2389 | /* While we are here, give empty ANTIC_IN sets to each block. */ |
83737db2 | 2390 | ANTIC_IN (block) = bitmap_set_new (); |
d75dbccd | 2391 | PA_IN (block) = bitmap_set_new (); |
7e6eb623 | 2392 | } |
83737db2 | 2393 | |
a28fee03 | 2394 | /* At the exit block we anticipate nothing. */ |
fefa31b5 | 2395 | BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1; |
a28fee03 | 2396 | |
8b1c6fd7 | 2397 | changed_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun) + 1); |
f61e445a | 2398 | bitmap_ones (changed_blocks); |
7e6eb623 DB |
2399 | while (changed) |
2400 | { | |
83737db2 DB |
2401 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2402 | fprintf (dump_file, "Starting iteration %d\n", num_iterations); | |
3bc27de7 RG |
2403 | /* ??? We need to clear our PHI translation cache here as the |
2404 | ANTIC sets shrink and we restrict valid translations to | |
2405 | those having operands with leaders in ANTIC. Same below | |
2406 | for PA ANTIC computation. */ | |
a28fee03 | 2407 | num_iterations++; |
c33bae88 | 2408 | changed = false; |
585d0dc4 | 2409 | for (i = postorder_num - 1; i >= 0; i--) |
83737db2 | 2410 | { |
d7c028c0 | 2411 | if (bitmap_bit_p (changed_blocks, postorder[i])) |
83737db2 | 2412 | { |
06e28de2 | 2413 | basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]); |
83737db2 | 2414 | changed |= compute_antic_aux (block, |
d7c028c0 | 2415 | bitmap_bit_p (has_abnormal_preds, |
83737db2 DB |
2416 | block->index)); |
2417 | } | |
2418 | } | |
d75dbccd | 2419 | /* Theoretically possible, but *highly* unlikely. */ |
77a74ed7 | 2420 | gcc_checking_assert (num_iterations < 500); |
2e24fa83 | 2421 | } |
a28fee03 | 2422 | |
9fe0cb7d RG |
2423 | statistics_histogram_event (cfun, "compute_antic iterations", |
2424 | num_iterations); | |
83737db2 | 2425 | |
d75dbccd DB |
2426 | if (do_partial_partial) |
2427 | { | |
f61e445a | 2428 | bitmap_ones (changed_blocks); |
d75dbccd DB |
2429 | mark_dfs_back_edges (); |
2430 | num_iterations = 0; | |
2431 | changed = true; | |
2432 | while (changed) | |
2433 | { | |
2434 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2435 | fprintf (dump_file, "Starting iteration %d\n", num_iterations); | |
2436 | num_iterations++; | |
2437 | changed = false; | |
585d0dc4 | 2438 | for (i = postorder_num - 1 ; i >= 0; i--) |
d75dbccd | 2439 | { |
d7c028c0 | 2440 | if (bitmap_bit_p (changed_blocks, postorder[i])) |
d75dbccd | 2441 | { |
06e28de2 | 2442 | basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]); |
d75dbccd DB |
2443 | changed |
2444 | |= compute_partial_antic_aux (block, | |
d7c028c0 | 2445 | bitmap_bit_p (has_abnormal_preds, |
d75dbccd DB |
2446 | block->index)); |
2447 | } | |
2448 | } | |
2449 | /* Theoretically possible, but *highly* unlikely. */ | |
77a74ed7 | 2450 | gcc_checking_assert (num_iterations < 500); |
d75dbccd | 2451 | } |
9fe0cb7d RG |
2452 | statistics_histogram_event (cfun, "compute_partial_antic iterations", |
2453 | num_iterations); | |
d75dbccd | 2454 | } |
83737db2 DB |
2455 | sbitmap_free (has_abnormal_preds); |
2456 | sbitmap_free (changed_blocks); | |
6de9cd9a DN |
2457 | } |
2458 | ||
c90186eb DB |
2459 | |
2460 | /* Inserted expressions are placed onto this worklist, which is used | |
2461 | for performing quick dead code elimination of insertions we made | |
2462 | that didn't turn out to be necessary. */ | |
0ab555de | 2463 | static bitmap inserted_exprs; |
c90186eb | 2464 | |
ce94d354 | 2465 | /* The actual worker for create_component_ref_by_pieces. */ |
b9c5e484 | 2466 | |
85300b46 | 2467 | static tree |
ce94d354 | 2468 | create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref, |
e076271b | 2469 | unsigned int *operand, gimple_seq *stmts) |
85300b46 | 2470 | { |
9771b263 | 2471 | vn_reference_op_t currop = &ref->operands[*operand]; |
c9145754 | 2472 | tree genop; |
ce94d354 | 2473 | ++*operand; |
c9145754 | 2474 | switch (currop->opcode) |
85300b46 | 2475 | { |
c9145754 DB |
2476 | case CALL_EXPR: |
2477 | { | |
b3be2694 | 2478 | tree folded, sc = NULL_TREE; |
ce94d354 | 2479 | unsigned int nargs = 0; |
b3be2694 RG |
2480 | tree fn, *args; |
2481 | if (TREE_CODE (currop->op0) == FUNCTION_DECL) | |
2482 | fn = currop->op0; | |
2483 | else | |
e076271b | 2484 | fn = find_or_generate_expression (block, currop->op0, stmts); |
c3dd8dd7 RB |
2485 | if (!fn) |
2486 | return NULL_TREE; | |
b3be2694 | 2487 | if (currop->op1) |
c3dd8dd7 RB |
2488 | { |
2489 | sc = find_or_generate_expression (block, currop->op1, stmts); | |
2490 | if (!sc) | |
2491 | return NULL_TREE; | |
2492 | } | |
9771b263 DN |
2493 | args = XNEWVEC (tree, ref->operands.length () - 1); |
2494 | while (*operand < ref->operands.length ()) | |
c9145754 | 2495 | { |
ce94d354 | 2496 | args[nargs] = create_component_ref_by_pieces_1 (block, ref, |
e076271b | 2497 | operand, stmts); |
c3dd8dd7 RB |
2498 | if (!args[nargs]) |
2499 | return NULL_TREE; | |
ce94d354 | 2500 | nargs++; |
c9145754 | 2501 | } |
726a989a | 2502 | folded = build_call_array (currop->type, |
b3be2694 RG |
2503 | (TREE_CODE (fn) == FUNCTION_DECL |
2504 | ? build_fold_addr_expr (fn) : fn), | |
726a989a | 2505 | nargs, args); |
d5e254e1 IE |
2506 | if (currop->with_bounds) |
2507 | CALL_WITH_BOUNDS_P (folded) = true; | |
c9145754 | 2508 | free (args); |
7aec7a38 | 2509 | if (sc) |
b3be2694 | 2510 | CALL_EXPR_STATIC_CHAIN (folded) = sc; |
c9145754 DB |
2511 | return folded; |
2512 | } | |
ea814c66 | 2513 | |
70f34814 RG |
2514 | case MEM_REF: |
2515 | { | |
2516 | tree baseop = create_component_ref_by_pieces_1 (block, ref, operand, | |
e076271b | 2517 | stmts); |
c3dd8dd7 RB |
2518 | if (!baseop) |
2519 | return NULL_TREE; | |
70f34814 | 2520 | tree offset = currop->op0; |
70f34814 RG |
2521 | if (TREE_CODE (baseop) == ADDR_EXPR |
2522 | && handled_component_p (TREE_OPERAND (baseop, 0))) | |
2523 | { | |
2524 | HOST_WIDE_INT off; | |
2525 | tree base; | |
2526 | base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0), | |
2527 | &off); | |
2528 | gcc_assert (base); | |
2529 | offset = int_const_binop (PLUS_EXPR, offset, | |
2530 | build_int_cst (TREE_TYPE (offset), | |
d35936ab | 2531 | off)); |
70f34814 RG |
2532 | baseop = build_fold_addr_expr (base); |
2533 | } | |
2534 | return fold_build2 (MEM_REF, currop->type, baseop, offset); | |
2535 | } | |
ea814c66 | 2536 | |
150e3929 RG |
2537 | case TARGET_MEM_REF: |
2538 | { | |
4d948885 | 2539 | tree genop0 = NULL_TREE, genop1 = NULL_TREE; |
9771b263 | 2540 | vn_reference_op_t nextop = &ref->operands[++*operand]; |
150e3929 | 2541 | tree baseop = create_component_ref_by_pieces_1 (block, ref, operand, |
e076271b | 2542 | stmts); |
c3dd8dd7 RB |
2543 | if (!baseop) |
2544 | return NULL_TREE; | |
150e3929 | 2545 | if (currop->op0) |
c3dd8dd7 RB |
2546 | { |
2547 | genop0 = find_or_generate_expression (block, currop->op0, stmts); | |
2548 | if (!genop0) | |
2549 | return NULL_TREE; | |
2550 | } | |
4d948885 | 2551 | if (nextop->op0) |
c3dd8dd7 RB |
2552 | { |
2553 | genop1 = find_or_generate_expression (block, nextop->op0, stmts); | |
2554 | if (!genop1) | |
2555 | return NULL_TREE; | |
2556 | } | |
4d948885 RG |
2557 | return build5 (TARGET_MEM_REF, currop->type, |
2558 | baseop, currop->op2, genop0, currop->op1, genop1); | |
150e3929 | 2559 | } |
ea814c66 | 2560 | |
ce94d354 RG |
2561 | case ADDR_EXPR: |
2562 | if (currop->op0) | |
2563 | { | |
2564 | gcc_assert (is_gimple_min_invariant (currop->op0)); | |
2565 | return currop->op0; | |
2566 | } | |
2567 | /* Fallthrough. */ | |
c9145754 DB |
2568 | case REALPART_EXPR: |
2569 | case IMAGPART_EXPR: | |
2570 | case VIEW_CONVERT_EXPR: | |
2571 | { | |
c3dd8dd7 RB |
2572 | tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand, |
2573 | stmts); | |
2574 | if (!genop0) | |
2575 | return NULL_TREE; | |
ea814c66 | 2576 | return fold_build1 (currop->opcode, currop->type, genop0); |
c9145754 | 2577 | } |
ea814c66 | 2578 | |
842679dc TV |
2579 | case WITH_SIZE_EXPR: |
2580 | { | |
2581 | tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand, | |
e076271b | 2582 | stmts); |
c3dd8dd7 RB |
2583 | if (!genop0) |
2584 | return NULL_TREE; | |
e076271b | 2585 | tree genop1 = find_or_generate_expression (block, currop->op0, stmts); |
c3dd8dd7 RB |
2586 | if (!genop1) |
2587 | return NULL_TREE; | |
842679dc TV |
2588 | return fold_build2 (currop->opcode, currop->type, genop0, genop1); |
2589 | } | |
ea814c66 | 2590 | |
c9145754 | 2591 | case BIT_FIELD_REF: |
e13f1c14 | 2592 | { |
ce94d354 | 2593 | tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand, |
e076271b | 2594 | stmts); |
c3dd8dd7 RB |
2595 | if (!genop0) |
2596 | return NULL_TREE; | |
ea814c66 EB |
2597 | tree op1 = currop->op0; |
2598 | tree op2 = currop->op1; | |
ea814c66 | 2599 | return fold_build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2); |
e13f1c14 | 2600 | } |
c9145754 DB |
2601 | |
2602 | /* For array ref vn_reference_op's, operand 1 of the array ref | |
2603 | is op0 of the reference op and operand 3 of the array ref is | |
2604 | op1. */ | |
2605 | case ARRAY_RANGE_REF: | |
2606 | case ARRAY_REF: | |
2607 | { | |
c9145754 DB |
2608 | tree genop0; |
2609 | tree genop1 = currop->op0; | |
c9145754 | 2610 | tree genop2 = currop->op1; |
e52201b6 | 2611 | tree genop3 = currop->op2; |
c3dd8dd7 RB |
2612 | genop0 = create_component_ref_by_pieces_1 (block, ref, operand, |
2613 | stmts); | |
2614 | if (!genop0) | |
2615 | return NULL_TREE; | |
e076271b | 2616 | genop1 = find_or_generate_expression (block, genop1, stmts); |
c3dd8dd7 RB |
2617 | if (!genop1) |
2618 | return NULL_TREE; | |
c9145754 DB |
2619 | if (genop2) |
2620 | { | |
d4d73ce2 EB |
2621 | tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0)); |
2622 | /* Drop zero minimum index if redundant. */ | |
2623 | if (integer_zerop (genop2) | |
2624 | && (!domain_type | |
2625 | || integer_zerop (TYPE_MIN_VALUE (domain_type)))) | |
d53bed0b RG |
2626 | genop2 = NULL_TREE; |
2627 | else | |
c3dd8dd7 RB |
2628 | { |
2629 | genop2 = find_or_generate_expression (block, genop2, stmts); | |
2630 | if (!genop2) | |
2631 | return NULL_TREE; | |
2632 | } | |
c9145754 | 2633 | } |
e52201b6 RG |
2634 | if (genop3) |
2635 | { | |
2636 | tree elmt_type = TREE_TYPE (TREE_TYPE (genop0)); | |
d53bed0b RG |
2637 | /* We can't always put a size in units of the element alignment |
2638 | here as the element alignment may be not visible. See | |
2639 | PR43783. Simply drop the element size for constant | |
2640 | sizes. */ | |
2641 | if (tree_int_cst_equal (genop3, TYPE_SIZE_UNIT (elmt_type))) | |
2642 | genop3 = NULL_TREE; | |
2643 | else | |
2644 | { | |
2645 | genop3 = size_binop (EXACT_DIV_EXPR, genop3, | |
2646 | size_int (TYPE_ALIGN_UNIT (elmt_type))); | |
e076271b | 2647 | genop3 = find_or_generate_expression (block, genop3, stmts); |
c3dd8dd7 RB |
2648 | if (!genop3) |
2649 | return NULL_TREE; | |
d53bed0b | 2650 | } |
e52201b6 | 2651 | } |
c9145754 DB |
2652 | return build4 (currop->opcode, currop->type, genop0, genop1, |
2653 | genop2, genop3); | |
2654 | } | |
85300b46 DB |
2655 | case COMPONENT_REF: |
2656 | { | |
2657 | tree op0; | |
2658 | tree op1; | |
c9145754 | 2659 | tree genop2 = currop->op1; |
e076271b | 2660 | op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts); |
c3dd8dd7 RB |
2661 | if (!op0) |
2662 | return NULL_TREE; | |
e076271b | 2663 | /* op1 should be a FIELD_DECL, which are represented by themselves. */ |
c9145754 DB |
2664 | op1 = currop->op0; |
2665 | if (genop2) | |
c3dd8dd7 RB |
2666 | { |
2667 | genop2 = find_or_generate_expression (block, genop2, stmts); | |
2668 | if (!genop2) | |
2669 | return NULL_TREE; | |
2670 | } | |
ea814c66 | 2671 | return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2); |
85300b46 | 2672 | } |
ea814c66 | 2673 | |
c9145754 | 2674 | case SSA_NAME: |
85300b46 | 2675 | { |
e076271b | 2676 | genop = find_or_generate_expression (block, currop->op0, stmts); |
c9145754 | 2677 | return genop; |
85300b46 | 2678 | } |
c9145754 DB |
2679 | case STRING_CST: |
2680 | case INTEGER_CST: | |
2681 | case COMPLEX_CST: | |
2682 | case VECTOR_CST: | |
2683 | case REAL_CST: | |
2684 | case CONSTRUCTOR: | |
85300b46 DB |
2685 | case VAR_DECL: |
2686 | case PARM_DECL: | |
c9145754 | 2687 | case CONST_DECL: |
5230d884 | 2688 | case RESULT_DECL: |
c9145754 | 2689 | case FUNCTION_DECL: |
c9145754 DB |
2690 | return currop->op0; |
2691 | ||
85300b46 | 2692 | default: |
b9c5e484 | 2693 | gcc_unreachable (); |
85300b46 | 2694 | } |
85300b46 | 2695 | } |
c90186eb | 2696 | |
ce94d354 | 2697 | /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the |
70f34814 | 2698 | COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with |
ce94d354 RG |
2699 | trying to rename aggregates into ssa form directly, which is a no no. |
2700 | ||
2701 | Thus, this routine doesn't create temporaries, it just builds a | |
2702 | single access expression for the array, calling | |
2703 | find_or_generate_expression to build the innermost pieces. | |
2704 | ||
2705 | This function is a subroutine of create_expression_by_pieces, and | |
2706 | should not be called on it's own unless you really know what you | |
2707 | are doing. */ | |
2708 | ||
2709 | static tree | |
2710 | create_component_ref_by_pieces (basic_block block, vn_reference_t ref, | |
e076271b | 2711 | gimple_seq *stmts) |
ce94d354 RG |
2712 | { |
2713 | unsigned int op = 0; | |
e076271b | 2714 | return create_component_ref_by_pieces_1 (block, ref, &op, stmts); |
ce94d354 RG |
2715 | } |
2716 | ||
c3dd8dd7 RB |
2717 | /* Find a simple leader for an expression, or generate one using |
2718 | create_expression_by_pieces from a NARY expression for the value. | |
56db793a | 2719 | BLOCK is the basic_block we are looking for leaders in. |
e076271b | 2720 | OP is the tree expression to find a leader for or generate. |
c3dd8dd7 | 2721 | Returns the leader or NULL_TREE on failure. */ |
56db793a DB |
2722 | |
2723 | static tree | |
e076271b | 2724 | find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts) |
56db793a | 2725 | { |
e076271b RG |
2726 | pre_expr expr = get_or_alloc_expr_for (op); |
2727 | unsigned int lookfor = get_expr_value_id (expr); | |
2728 | pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor); | |
c9145754 DB |
2729 | if (leader) |
2730 | { | |
2731 | if (leader->kind == NAME) | |
e076271b | 2732 | return PRE_EXPR_NAME (leader); |
c9145754 | 2733 | else if (leader->kind == CONSTANT) |
e076271b | 2734 | return PRE_EXPR_CONSTANT (leader); |
c3dd8dd7 RB |
2735 | |
2736 | /* Defer. */ | |
2737 | return NULL_TREE; | |
c9145754 | 2738 | } |
e9284566 | 2739 | |
c3dd8dd7 RB |
2740 | /* It must be a complex expression, so generate it recursively. Note |
2741 | that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c | |
2742 | where the insert algorithm fails to insert a required expression. */ | |
9771b263 | 2743 | bitmap exprset = value_expressions[lookfor]; |
e076271b RG |
2744 | bitmap_iterator bi; |
2745 | unsigned int i; | |
2746 | EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi) | |
56db793a | 2747 | { |
e076271b | 2748 | pre_expr temp = expression_for_id (i); |
c3dd8dd7 RB |
2749 | /* We cannot insert random REFERENCE expressions at arbitrary |
2750 | places. We can insert NARYs which eventually re-materializes | |
2751 | its operand values. */ | |
2752 | if (temp->kind == NARY) | |
e076271b RG |
2753 | return create_expression_by_pieces (block, temp, stmts, |
2754 | get_expr_type (expr)); | |
56db793a | 2755 | } |
e076271b | 2756 | |
c3dd8dd7 RB |
2757 | /* Defer. */ |
2758 | return NULL_TREE; | |
56db793a DB |
2759 | } |
2760 | ||
726a989a | 2761 | #define NECESSARY GF_PLF_1 |
c9145754 | 2762 | |
56db793a | 2763 | /* Create an expression in pieces, so that we can handle very complex |
b9c5e484 | 2764 | expressions that may be ANTIC, but not necessary GIMPLE. |
56db793a DB |
2765 | BLOCK is the basic block the expression will be inserted into, |
2766 | EXPR is the expression to insert (in value form) | |
2767 | STMTS is a statement list to append the necessary insertions into. | |
2768 | ||
0e61db61 | 2769 | This function will die if we hit some value that shouldn't be |
56db793a | 2770 | ANTIC but is (IE there is no leader for it, or its components). |
c3dd8dd7 RB |
2771 | The function returns NULL_TREE in case a different antic expression |
2772 | has to be inserted first. | |
56db793a DB |
2773 | This function may also generate expressions that are themselves |
2774 | partially or fully redundant. Those that are will be either made | |
2775 | fully redundant during the next iteration of insert (for partially | |
2776 | redundant ones), or eliminated by eliminate (for fully redundant | |
c3dd8dd7 | 2777 | ones). */ |
56db793a DB |
2778 | |
2779 | static tree | |
726a989a | 2780 | create_expression_by_pieces (basic_block block, pre_expr expr, |
e076271b | 2781 | gimple_seq *stmts, tree type) |
56db793a | 2782 | { |
83d5977e | 2783 | tree name; |
150e3929 RG |
2784 | tree folded; |
2785 | gimple_seq forced_stmts = NULL; | |
c9145754 | 2786 | unsigned int value_id; |
726a989a | 2787 | gimple_stmt_iterator gsi; |
c9145754 DB |
2788 | tree exprtype = type ? type : get_expr_type (expr); |
2789 | pre_expr nameexpr; | |
538dd0b7 | 2790 | gassign *newstmt; |
81c4f554 | 2791 | |
c9145754 | 2792 | switch (expr->kind) |
56db793a | 2793 | { |
c9145754 DB |
2794 | /* We may hit the NAME/CONSTANT case if we have to convert types |
2795 | that value numbering saw through. */ | |
2796 | case NAME: | |
2797 | folded = PRE_EXPR_NAME (expr); | |
2798 | break; | |
2799 | case CONSTANT: | |
2800 | folded = PRE_EXPR_CONSTANT (expr); | |
2801 | break; | |
2802 | case REFERENCE: | |
43da81be | 2803 | { |
c9145754 | 2804 | vn_reference_t ref = PRE_EXPR_REFERENCE (expr); |
e076271b | 2805 | folded = create_component_ref_by_pieces (block, ref, stmts); |
c3dd8dd7 RB |
2806 | if (!folded) |
2807 | return NULL_TREE; | |
43da81be DB |
2808 | } |
2809 | break; | |
c9145754 | 2810 | case NARY: |
c90186eb | 2811 | { |
c9145754 | 2812 | vn_nary_op_t nary = PRE_EXPR_NARY (expr); |
f843144b | 2813 | tree *genop = XALLOCAVEC (tree, nary->length); |
5a7d7f9c RG |
2814 | unsigned i; |
2815 | for (i = 0; i < nary->length; ++i) | |
85300b46 | 2816 | { |
e076271b | 2817 | genop[i] = find_or_generate_expression (block, nary->op[i], stmts); |
c3dd8dd7 RB |
2818 | if (!genop[i]) |
2819 | return NULL_TREE; | |
48acf1b7 RG |
2820 | /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It |
2821 | may have conversions stripped. */ | |
2822 | if (nary->opcode == POINTER_PLUS_EXPR) | |
2823 | { | |
2824 | if (i == 0) | |
d4f5cd5e RB |
2825 | genop[i] = gimple_convert (&forced_stmts, |
2826 | nary->type, genop[i]); | |
48acf1b7 | 2827 | else if (i == 1) |
d4f5cd5e RB |
2828 | genop[i] = gimple_convert (&forced_stmts, |
2829 | sizetype, genop[i]); | |
48acf1b7 | 2830 | } |
5a7d7f9c | 2831 | else |
d4f5cd5e RB |
2832 | genop[i] = gimple_convert (&forced_stmts, |
2833 | TREE_TYPE (nary->op[i]), genop[i]); | |
5a7d7f9c RG |
2834 | } |
2835 | if (nary->opcode == CONSTRUCTOR) | |
2836 | { | |
9771b263 | 2837 | vec<constructor_elt, va_gc> *elts = NULL; |
5a7d7f9c RG |
2838 | for (i = 0; i < nary->length; ++i) |
2839 | CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]); | |
2840 | folded = build_constructor (nary->type, elts); | |
2841 | } | |
2842 | else | |
2843 | { | |
2844 | switch (nary->length) | |
2845 | { | |
2846 | case 1: | |
2847 | folded = fold_build1 (nary->opcode, nary->type, | |
2848 | genop[0]); | |
2849 | break; | |
2850 | case 2: | |
2851 | folded = fold_build2 (nary->opcode, nary->type, | |
2852 | genop[0], genop[1]); | |
2853 | break; | |
2854 | case 3: | |
2855 | folded = fold_build3 (nary->opcode, nary->type, | |
a475fd3d | 2856 | genop[0], genop[1], genop[2]); |
5a7d7f9c RG |
2857 | break; |
2858 | default: | |
2859 | gcc_unreachable (); | |
2860 | } | |
85300b46 | 2861 | } |
56db793a | 2862 | } |
c9145754 | 2863 | break; |
56db793a | 2864 | default: |
e076271b | 2865 | gcc_unreachable (); |
56db793a | 2866 | } |
150e3929 RG |
2867 | |
2868 | if (!useless_type_conversion_p (exprtype, TREE_TYPE (folded))) | |
2869 | folded = fold_convert (exprtype, folded); | |
2870 | ||
81c4f554 SB |
2871 | /* Force the generated expression to be a sequence of GIMPLE |
2872 | statements. | |
2873 | We have to call unshare_expr because force_gimple_operand may | |
2874 | modify the tree we pass to it. */ | |
d4f5cd5e RB |
2875 | gimple_seq tem = NULL; |
2876 | folded = force_gimple_operand (unshare_expr (folded), &tem, | |
150e3929 | 2877 | false, NULL); |
d4f5cd5e | 2878 | gimple_seq_add_seq_without_update (&forced_stmts, tem); |
81c4f554 SB |
2879 | |
2880 | /* If we have any intermediate expressions to the value sets, add them | |
a7849637 | 2881 | to the value sets and chain them in the instruction stream. */ |
81c4f554 SB |
2882 | if (forced_stmts) |
2883 | { | |
726a989a RB |
2884 | gsi = gsi_start (forced_stmts); |
2885 | for (; !gsi_end_p (gsi); gsi_next (&gsi)) | |
81c4f554 | 2886 | { |
355fe088 | 2887 | gimple *stmt = gsi_stmt (gsi); |
726a989a | 2888 | tree forcedname = gimple_get_lhs (stmt); |
c9145754 | 2889 | pre_expr nameexpr; |
b9c5e484 | 2890 | |
c9145754 DB |
2891 | if (TREE_CODE (forcedname) == SSA_NAME) |
2892 | { | |
0ab555de | 2893 | bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname)); |
c9145754 DB |
2894 | VN_INFO_GET (forcedname)->valnum = forcedname; |
2895 | VN_INFO (forcedname)->value_id = get_next_value_id (); | |
2896 | nameexpr = get_or_alloc_expr_for_name (forcedname); | |
2897 | add_to_value (VN_INFO (forcedname)->value_id, nameexpr); | |
40b178f4 | 2898 | bitmap_value_replace_in_set (NEW_SETS (block), nameexpr); |
c9145754 DB |
2899 | bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr); |
2900 | } | |
81231d13 RB |
2901 | |
2902 | gimple_set_vuse (stmt, BB_LIVE_VOP_ON_EXIT (block)); | |
b6fed550 | 2903 | gimple_set_modified (stmt, true); |
81c4f554 | 2904 | } |
726a989a | 2905 | gimple_seq_add_seq (stmts, forced_stmts); |
81c4f554 SB |
2906 | } |
2907 | ||
83d5977e RG |
2908 | name = make_temp_ssa_name (exprtype, NULL, "pretmp"); |
2909 | newstmt = gimple_build_assign (name, folded); | |
81231d13 | 2910 | gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block)); |
b6fed550 | 2911 | gimple_set_modified (newstmt, true); |
726a989a | 2912 | gimple_set_plf (newstmt, NECESSARY, false); |
c90186eb | 2913 | |
726a989a | 2914 | gimple_seq_add_stmt (stmts, newstmt); |
0ab555de | 2915 | bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name)); |
cfaab3a9 | 2916 | |
0aa16586 RG |
2917 | /* Fold the last statement. */ |
2918 | gsi = gsi_last (*stmts); | |
748c5114 RG |
2919 | if (fold_stmt_inplace (&gsi)) |
2920 | update_stmt (gsi_stmt (gsi)); | |
0aa16586 | 2921 | |
c9145754 | 2922 | /* Add a value number to the temporary. |
81c4f554 | 2923 | The value may already exist in either NEW_SETS, or AVAIL_OUT, because |
e9284566 DB |
2924 | we are creating the expression by pieces, and this particular piece of |
2925 | the expression may have been represented. There is no harm in replacing | |
2926 | here. */ | |
c9145754 | 2927 | value_id = get_expr_value_id (expr); |
40b178f4 RG |
2928 | VN_INFO_GET (name)->value_id = value_id; |
2929 | VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id); | |
2930 | if (VN_INFO (name)->valnum == NULL_TREE) | |
2931 | VN_INFO (name)->valnum = name; | |
2932 | gcc_assert (VN_INFO (name)->valnum != NULL_TREE); | |
c9145754 DB |
2933 | nameexpr = get_or_alloc_expr_for_name (name); |
2934 | add_to_value (value_id, nameexpr); | |
70f34814 | 2935 | if (NEW_SETS (block)) |
c9145754 DB |
2936 | bitmap_value_replace_in_set (NEW_SETS (block), nameexpr); |
2937 | bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr); | |
81c4f554 SB |
2938 | |
2939 | pre_stats.insertions++; | |
56db793a | 2940 | if (dump_file && (dump_flags & TDF_DETAILS)) |
b9c5e484 | 2941 | { |
56db793a | 2942 | fprintf (dump_file, "Inserted "); |
726a989a | 2943 | print_gimple_stmt (dump_file, newstmt, 0, 0); |
984af6ac RB |
2944 | fprintf (dump_file, " in predecessor %d (%04d)\n", |
2945 | block->index, value_id); | |
56db793a | 2946 | } |
81c4f554 | 2947 | |
56db793a DB |
2948 | return name; |
2949 | } | |
e9284566 | 2950 | |
c9145754 | 2951 | |
83737db2 | 2952 | /* Insert the to-be-made-available values of expression EXPRNUM for each |
c90186eb | 2953 | predecessor, stored in AVAIL, into the predecessors of BLOCK, and |
c9145754 | 2954 | merge the result with a phi node, given the same value number as |
c90186eb | 2955 | NODE. Return true if we have inserted new stuff. */ |
e9284566 DB |
2956 | |
2957 | static bool | |
83737db2 | 2958 | insert_into_preds_of_block (basic_block block, unsigned int exprnum, |
9771b263 | 2959 | vec<pre_expr> avail) |
e9284566 | 2960 | { |
c9145754 DB |
2961 | pre_expr expr = expression_for_id (exprnum); |
2962 | pre_expr newphi; | |
2963 | unsigned int val = get_expr_value_id (expr); | |
e9284566 | 2964 | edge pred; |
0fc6c492 DB |
2965 | bool insertions = false; |
2966 | bool nophi = false; | |
e9284566 | 2967 | basic_block bprime; |
c9145754 | 2968 | pre_expr eprime; |
e9284566 | 2969 | edge_iterator ei; |
c9145754 | 2970 | tree type = get_expr_type (expr); |
de081cfd | 2971 | tree temp; |
538dd0b7 | 2972 | gphi *phi; |
b9c5e484 | 2973 | |
0fc6c492 | 2974 | /* Make sure we aren't creating an induction variable. */ |
391886c8 | 2975 | if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2) |
0fc6c492 DB |
2976 | { |
2977 | bool firstinsideloop = false; | |
2978 | bool secondinsideloop = false; | |
b9c5e484 | 2979 | firstinsideloop = flow_bb_inside_loop_p (block->loop_father, |
0fc6c492 DB |
2980 | EDGE_PRED (block, 0)->src); |
2981 | secondinsideloop = flow_bb_inside_loop_p (block->loop_father, | |
2982 | EDGE_PRED (block, 1)->src); | |
2983 | /* Induction variables only have one edge inside the loop. */ | |
a8338640 | 2984 | if ((firstinsideloop ^ secondinsideloop) |
cddaefa3 | 2985 | && expr->kind != REFERENCE) |
0fc6c492 DB |
2986 | { |
2987 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2988 | fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n"); | |
2989 | nophi = true; | |
2990 | } | |
2991 | } | |
b9c5e484 | 2992 | |
c9145754 DB |
2993 | /* Make the necessary insertions. */ |
2994 | FOR_EACH_EDGE (pred, ei, block->preds) | |
2995 | { | |
726a989a | 2996 | gimple_seq stmts = NULL; |
c9145754 DB |
2997 | tree builtexpr; |
2998 | bprime = pred->src; | |
9771b263 | 2999 | eprime = avail[pred->dest_idx]; |
c9145754 DB |
3000 | |
3001 | if (eprime->kind != NAME && eprime->kind != CONSTANT) | |
3002 | { | |
e076271b RG |
3003 | builtexpr = create_expression_by_pieces (bprime, eprime, |
3004 | &stmts, type); | |
c9145754 | 3005 | gcc_assert (!(pred->flags & EDGE_ABNORMAL)); |
726a989a | 3006 | gsi_insert_seq_on_edge (pred, stmts); |
c3dd8dd7 RB |
3007 | if (!builtexpr) |
3008 | { | |
3009 | /* We cannot insert a PHI node if we failed to insert | |
3010 | on one edge. */ | |
3011 | nophi = true; | |
3012 | continue; | |
3013 | } | |
9771b263 | 3014 | avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr); |
c9145754 DB |
3015 | insertions = true; |
3016 | } | |
3017 | else if (eprime->kind == CONSTANT) | |
3018 | { | |
3019 | /* Constants may not have the right type, fold_convert | |
1c8f7377 | 3020 | should give us back a constant with the right type. */ |
c9145754 | 3021 | tree constant = PRE_EXPR_CONSTANT (eprime); |
15d5fe33 | 3022 | if (!useless_type_conversion_p (type, TREE_TYPE (constant))) |
c9145754 DB |
3023 | { |
3024 | tree builtexpr = fold_convert (type, constant); | |
b8698a0f | 3025 | if (!is_gimple_min_invariant (builtexpr)) |
c9145754 DB |
3026 | { |
3027 | tree forcedexpr = force_gimple_operand (builtexpr, | |
3028 | &stmts, true, | |
3029 | NULL); | |
15d5fe33 | 3030 | if (!is_gimple_min_invariant (forcedexpr)) |
c9145754 DB |
3031 | { |
3032 | if (forcedexpr != builtexpr) | |
3033 | { | |
3034 | VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime); | |
3035 | VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime); | |
3036 | } | |
3037 | if (stmts) | |
3038 | { | |
726a989a RB |
3039 | gimple_stmt_iterator gsi; |
3040 | gsi = gsi_start (stmts); | |
3041 | for (; !gsi_end_p (gsi); gsi_next (&gsi)) | |
c9145754 | 3042 | { |
355fe088 | 3043 | gimple *stmt = gsi_stmt (gsi); |
0ab555de MM |
3044 | tree lhs = gimple_get_lhs (stmt); |
3045 | if (TREE_CODE (lhs) == SSA_NAME) | |
3046 | bitmap_set_bit (inserted_exprs, | |
3047 | SSA_NAME_VERSION (lhs)); | |
726a989a | 3048 | gimple_set_plf (stmt, NECESSARY, false); |
c9145754 | 3049 | } |
726a989a | 3050 | gsi_insert_seq_on_edge (pred, stmts); |
c9145754 | 3051 | } |
9771b263 DN |
3052 | avail[pred->dest_idx] |
3053 | = get_or_alloc_expr_for_name (forcedexpr); | |
c9145754 DB |
3054 | } |
3055 | } | |
70f34814 | 3056 | else |
9771b263 DN |
3057 | avail[pred->dest_idx] |
3058 | = get_or_alloc_expr_for_constant (builtexpr); | |
c9145754 DB |
3059 | } |
3060 | } | |
3061 | else if (eprime->kind == NAME) | |
3062 | { | |
3063 | /* We may have to do a conversion because our value | |
3064 | numbering can look through types in certain cases, but | |
3065 | our IL requires all operands of a phi node have the same | |
3066 | type. */ | |
3067 | tree name = PRE_EXPR_NAME (eprime); | |
f709638a | 3068 | if (!useless_type_conversion_p (type, TREE_TYPE (name))) |
c9145754 DB |
3069 | { |
3070 | tree builtexpr; | |
3071 | tree forcedexpr; | |
f709638a | 3072 | builtexpr = fold_convert (type, name); |
c9145754 DB |
3073 | forcedexpr = force_gimple_operand (builtexpr, |
3074 | &stmts, true, | |
3075 | NULL); | |
3076 | ||
3077 | if (forcedexpr != name) | |
3078 | { | |
3079 | VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum; | |
3080 | VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id; | |
3081 | } | |
c90186eb | 3082 | |
c9145754 DB |
3083 | if (stmts) |
3084 | { | |
726a989a RB |
3085 | gimple_stmt_iterator gsi; |
3086 | gsi = gsi_start (stmts); | |
3087 | for (; !gsi_end_p (gsi); gsi_next (&gsi)) | |
c9145754 | 3088 | { |
355fe088 | 3089 | gimple *stmt = gsi_stmt (gsi); |
0ab555de MM |
3090 | tree lhs = gimple_get_lhs (stmt); |
3091 | if (TREE_CODE (lhs) == SSA_NAME) | |
3092 | bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (lhs)); | |
726a989a | 3093 | gimple_set_plf (stmt, NECESSARY, false); |
c9145754 | 3094 | } |
726a989a | 3095 | gsi_insert_seq_on_edge (pred, stmts); |
c9145754 | 3096 | } |
9771b263 | 3097 | avail[pred->dest_idx] = get_or_alloc_expr_for_name (forcedexpr); |
c9145754 | 3098 | } |
b9c5e484 | 3099 | } |
e9284566 | 3100 | } |
0fc6c492 DB |
3101 | /* If we didn't want a phi node, and we made insertions, we still have |
3102 | inserted new stuff, and thus return true. If we didn't want a phi node, | |
3103 | and didn't make insertions, we haven't added anything new, so return | |
3104 | false. */ | |
3105 | if (nophi && insertions) | |
3106 | return true; | |
3107 | else if (nophi && !insertions) | |
3108 | return false; | |
3109 | ||
e9284566 | 3110 | /* Now build a phi for the new variable. */ |
83d5977e | 3111 | temp = make_temp_ssa_name (type, NULL, "prephitmp"); |
1e52075c | 3112 | phi = create_phi_node (temp, block); |
de081cfd RG |
3113 | |
3114 | gimple_set_plf (phi, NECESSARY, false); | |
40b178f4 RG |
3115 | VN_INFO_GET (temp)->value_id = val; |
3116 | VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val); | |
3117 | if (VN_INFO (temp)->valnum == NULL_TREE) | |
3118 | VN_INFO (temp)->valnum = temp; | |
3119 | bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp)); | |
e9284566 | 3120 | FOR_EACH_EDGE (pred, ei, block->preds) |
c9145754 | 3121 | { |
9771b263 | 3122 | pre_expr ae = avail[pred->dest_idx]; |
c9145754 DB |
3123 | gcc_assert (get_expr_type (ae) == type |
3124 | || useless_type_conversion_p (type, get_expr_type (ae))); | |
3125 | if (ae->kind == CONSTANT) | |
2724573f RB |
3126 | add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)), |
3127 | pred, UNKNOWN_LOCATION); | |
c9145754 | 3128 | else |
1c8f7377 | 3129 | add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION); |
c9145754 | 3130 | } |
b9c5e484 | 3131 | |
40b178f4 | 3132 | newphi = get_or_alloc_expr_for_name (temp); |
c9145754 | 3133 | add_to_value (val, newphi); |
b9c5e484 | 3134 | |
e9284566 DB |
3135 | /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing |
3136 | this insertion, since we test for the existence of this value in PHI_GEN | |
3137 | before proceeding with the partial redundancy checks in insert_aux. | |
b9c5e484 | 3138 | |
e9284566 DB |
3139 | The value may exist in AVAIL_OUT, in particular, it could be represented |
3140 | by the expression we are trying to eliminate, in which case we want the | |
3141 | replacement to occur. If it's not existing in AVAIL_OUT, we want it | |
3142 | inserted there. | |
b9c5e484 | 3143 | |
e9284566 DB |
3144 | Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of |
3145 | this block, because if it did, it would have existed in our dominator's | |
3146 | AVAIL_OUT, and would have been skipped due to the full redundancy check. | |
3147 | */ | |
3148 | ||
c9145754 | 3149 | bitmap_insert_into_set (PHI_GEN (block), newphi); |
b9c5e484 | 3150 | bitmap_value_replace_in_set (AVAIL_OUT (block), |
c9145754 | 3151 | newphi); |
e9284566 | 3152 | bitmap_insert_into_set (NEW_SETS (block), |
c9145754 | 3153 | newphi); |
b9c5e484 | 3154 | |
42c6b3ca RB |
3155 | /* If we insert a PHI node for a conversion of another PHI node |
3156 | in the same basic-block try to preserve range information. | |
3157 | This is important so that followup loop passes receive optimal | |
3158 | number of iteration analysis results. See PR61743. */ | |
3159 | if (expr->kind == NARY | |
3160 | && CONVERT_EXPR_CODE_P (expr->u.nary->opcode) | |
3161 | && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME | |
3162 | && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block | |
3163 | && INTEGRAL_TYPE_P (type) | |
3164 | && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0])) | |
3165 | && (TYPE_PRECISION (type) | |
3166 | >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0]))) | |
3167 | && SSA_NAME_RANGE_INFO (expr->u.nary->op[0])) | |
3168 | { | |
3169 | wide_int min, max; | |
3170 | if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE | |
3171 | && !wi::neg_p (min, SIGNED) | |
3172 | && !wi::neg_p (max, SIGNED)) | |
3173 | /* Just handle extension and sign-changes of all-positive ranges. */ | |
3174 | set_range_info (temp, | |
3175 | SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]), | |
3176 | wide_int_storage::from (min, TYPE_PRECISION (type), | |
3177 | TYPE_SIGN (type)), | |
3178 | wide_int_storage::from (max, TYPE_PRECISION (type), | |
3179 | TYPE_SIGN (type))); | |
3180 | } | |
3181 | ||
e9284566 DB |
3182 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3183 | { | |
3184 | fprintf (dump_file, "Created phi "); | |
726a989a | 3185 | print_gimple_stmt (dump_file, phi, 0, 0); |
984af6ac | 3186 | fprintf (dump_file, " in block %d (%04d)\n", block->index, val); |
e9284566 DB |
3187 | } |
3188 | pre_stats.phis++; | |
3189 | return true; | |
3190 | } | |
3191 | ||
3192 | ||
b9c5e484 | 3193 | |
7e6eb623 DB |
3194 | /* Perform insertion of partially redundant values. |
3195 | For BLOCK, do the following: | |
3196 | 1. Propagate the NEW_SETS of the dominator into the current block. | |
b9c5e484 | 3197 | If the block has multiple predecessors, |
7e6eb623 | 3198 | 2a. Iterate over the ANTIC expressions for the block to see if |
83737db2 | 3199 | any of them are partially redundant. |
7e6eb623 | 3200 | 2b. If so, insert them into the necessary predecessors to make |
83737db2 | 3201 | the expression fully redundant. |
7e6eb623 DB |
3202 | 2c. Insert a new PHI merging the values of the predecessors. |
3203 | 2d. Insert the new PHI, and the new expressions, into the | |
83737db2 | 3204 | NEW_SETS set. |
7e6eb623 | 3205 | 3. Recursively call ourselves on the dominator children of BLOCK. |
6de9cd9a | 3206 | |
83737db2 | 3207 | Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by |
d75dbccd | 3208 | do_regular_insertion and do_partial_insertion. |
83737db2 | 3209 | |
7e6eb623 | 3210 | */ |
e9284566 | 3211 | |
83737db2 DB |
3212 | static bool |
3213 | do_regular_insertion (basic_block block, basic_block dom) | |
3214 | { | |
3215 | bool new_stuff = false; | |
9771b263 | 3216 | vec<pre_expr> exprs; |
c9145754 | 3217 | pre_expr expr; |
3b56f890 | 3218 | auto_vec<pre_expr> avail; |
83737db2 DB |
3219 | int i; |
3220 | ||
1c8f7377 | 3221 | exprs = sorted_array_from_bitmap_set (ANTIC_IN (block)); |
9771b263 | 3222 | avail.safe_grow (EDGE_COUNT (block->preds)); |
1c8f7377 | 3223 | |
9771b263 | 3224 | FOR_EACH_VEC_ELT (exprs, i, expr) |
83737db2 | 3225 | { |
4bcc5786 RB |
3226 | if (expr->kind == NARY |
3227 | || expr->kind == REFERENCE) | |
83737db2 | 3228 | { |
c9145754 | 3229 | unsigned int val; |
83737db2 DB |
3230 | bool by_some = false; |
3231 | bool cant_insert = false; | |
3232 | bool all_same = true; | |
c9145754 | 3233 | pre_expr first_s = NULL; |
83737db2 DB |
3234 | edge pred; |
3235 | basic_block bprime; | |
c9145754 | 3236 | pre_expr eprime = NULL; |
83737db2 | 3237 | edge_iterator ei; |
f11d2f1e | 3238 | pre_expr edoubleprime = NULL; |
5813994e | 3239 | bool do_insertion = false; |
83737db2 | 3240 | |
c9145754 | 3241 | val = get_expr_value_id (expr); |
83737db2 DB |
3242 | if (bitmap_set_contains_value (PHI_GEN (block), val)) |
3243 | continue; | |
3244 | if (bitmap_set_contains_value (AVAIL_OUT (dom), val)) | |
3245 | { | |
3246 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
ead69dac JL |
3247 | { |
3248 | fprintf (dump_file, "Found fully redundant value: "); | |
3249 | print_pre_expr (dump_file, expr); | |
3250 | fprintf (dump_file, "\n"); | |
3251 | } | |
83737db2 DB |
3252 | continue; |
3253 | } | |
3254 | ||
83737db2 DB |
3255 | FOR_EACH_EDGE (pred, ei, block->preds) |
3256 | { | |
c9145754 | 3257 | unsigned int vprime; |
83737db2 | 3258 | |
c4ab2baa RG |
3259 | /* We should never run insertion for the exit block |
3260 | and so not come across fake pred edges. */ | |
3261 | gcc_assert (!(pred->flags & EDGE_FAKE)); | |
83737db2 DB |
3262 | bprime = pred->src; |
3263 | eprime = phi_translate (expr, ANTIC_IN (block), NULL, | |
3264 | bprime, block); | |
3265 | ||
3266 | /* eprime will generally only be NULL if the | |
3267 | value of the expression, translated | |
3268 | through the PHI for this predecessor, is | |
3269 | undefined. If that is the case, we can't | |
3270 | make the expression fully redundant, | |
3271 | because its value is undefined along a | |
3272 | predecessor path. We can thus break out | |
3273 | early because it doesn't matter what the | |
3274 | rest of the results are. */ | |
3275 | if (eprime == NULL) | |
3276 | { | |
9771b263 | 3277 | avail[pred->dest_idx] = NULL; |
83737db2 DB |
3278 | cant_insert = true; |
3279 | break; | |
3280 | } | |
3281 | ||
3282 | eprime = fully_constant_expression (eprime); | |
726a989a RB |
3283 | vprime = get_expr_value_id (eprime); |
3284 | edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), | |
e076271b | 3285 | vprime); |
83737db2 DB |
3286 | if (edoubleprime == NULL) |
3287 | { | |
9771b263 | 3288 | avail[pred->dest_idx] = eprime; |
83737db2 DB |
3289 | all_same = false; |
3290 | } | |
3291 | else | |
3292 | { | |
9771b263 | 3293 | avail[pred->dest_idx] = edoubleprime; |
83737db2 | 3294 | by_some = true; |
5813994e RG |
3295 | /* We want to perform insertions to remove a redundancy on |
3296 | a path in the CFG we want to optimize for speed. */ | |
3297 | if (optimize_edge_for_speed_p (pred)) | |
3298 | do_insertion = true; | |
83737db2 DB |
3299 | if (first_s == NULL) |
3300 | first_s = edoubleprime; | |
5deac340 | 3301 | else if (!pre_expr_d::equal (first_s, edoubleprime)) |
83737db2 DB |
3302 | all_same = false; |
3303 | } | |
3304 | } | |
3305 | /* If we can insert it, it's not the same value | |
3306 | already existing along every predecessor, and | |
3307 | it's defined by some predecessor, it is | |
3308 | partially redundant. */ | |
3bc27de7 | 3309 | if (!cant_insert && !all_same && by_some) |
83737db2 | 3310 | { |
3bc27de7 RG |
3311 | if (!do_insertion) |
3312 | { | |
3313 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3314 | { | |
3315 | fprintf (dump_file, "Skipping partial redundancy for " | |
3316 | "expression "); | |
3317 | print_pre_expr (dump_file, expr); | |
3318 | fprintf (dump_file, " (%04d), no redundancy on to be " | |
3319 | "optimized for speed edge\n", val); | |
3320 | } | |
3321 | } | |
19372838 RG |
3322 | else if (dbg_cnt (treepre_insert)) |
3323 | { | |
3324 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3325 | { | |
3326 | fprintf (dump_file, "Found partial redundancy for " | |
3327 | "expression "); | |
3328 | print_pre_expr (dump_file, expr); | |
3329 | fprintf (dump_file, " (%04d)\n", | |
3330 | get_expr_value_id (expr)); | |
3331 | } | |
3332 | if (insert_into_preds_of_block (block, | |
3333 | get_expression_id (expr), | |
3334 | avail)) | |
3335 | new_stuff = true; | |
3336 | } | |
83737db2 DB |
3337 | } |
3338 | /* If all edges produce the same value and that value is | |
3339 | an invariant, then the PHI has the same value on all | |
3340 | edges. Note this. */ | |
4bcc5786 | 3341 | else if (!cant_insert && all_same) |
83737db2 | 3342 | { |
4bcc5786 RB |
3343 | gcc_assert (edoubleprime->kind == CONSTANT |
3344 | || edoubleprime->kind == NAME); | |
3345 | ||
3346 | tree temp = make_temp_ssa_name (get_expr_type (expr), | |
3347 | NULL, "pretmp"); | |
538dd0b7 DM |
3348 | gassign *assign |
3349 | = gimple_build_assign (temp, | |
3350 | edoubleprime->kind == CONSTANT ? | |
3351 | PRE_EXPR_CONSTANT (edoubleprime) : | |
3352 | PRE_EXPR_NAME (edoubleprime)); | |
4bcc5786 RB |
3353 | gimple_stmt_iterator gsi = gsi_after_labels (block); |
3354 | gsi_insert_before (&gsi, assign, GSI_NEW_STMT); | |
3355 | ||
3356 | gimple_set_plf (assign, NECESSARY, false); | |
3357 | VN_INFO_GET (temp)->value_id = val; | |
3358 | VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val); | |
3359 | if (VN_INFO (temp)->valnum == NULL_TREE) | |
3360 | VN_INFO (temp)->valnum = temp; | |
3361 | bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp)); | |
3362 | pre_expr newe = get_or_alloc_expr_for_name (temp); | |
3363 | add_to_value (val, newe); | |
3364 | bitmap_value_replace_in_set (AVAIL_OUT (block), newe); | |
3365 | bitmap_insert_into_set (NEW_SETS (block), newe); | |
83737db2 | 3366 | } |
83737db2 DB |
3367 | } |
3368 | } | |
3369 | ||
9771b263 | 3370 | exprs.release (); |
83737db2 DB |
3371 | return new_stuff; |
3372 | } | |
3373 | ||
3374 | ||
d75dbccd DB |
3375 | /* Perform insertion for partially anticipatable expressions. There |
3376 | is only one case we will perform insertion for these. This case is | |
3377 | if the expression is partially anticipatable, and fully available. | |
3378 | In this case, we know that putting it earlier will enable us to | |
3379 | remove the later computation. */ | |
3380 | ||
3381 | ||
3382 | static bool | |
3383 | do_partial_partial_insertion (basic_block block, basic_block dom) | |
3384 | { | |
3385 | bool new_stuff = false; | |
9771b263 | 3386 | vec<pre_expr> exprs; |
c9145754 | 3387 | pre_expr expr; |
ef062b13 | 3388 | auto_vec<pre_expr> avail; |
d75dbccd DB |
3389 | int i; |
3390 | ||
1c8f7377 | 3391 | exprs = sorted_array_from_bitmap_set (PA_IN (block)); |
9771b263 | 3392 | avail.safe_grow (EDGE_COUNT (block->preds)); |
1c8f7377 | 3393 | |
9771b263 | 3394 | FOR_EACH_VEC_ELT (exprs, i, expr) |
d75dbccd | 3395 | { |
4bcc5786 RB |
3396 | if (expr->kind == NARY |
3397 | || expr->kind == REFERENCE) | |
d75dbccd | 3398 | { |
c9145754 | 3399 | unsigned int val; |
d75dbccd DB |
3400 | bool by_all = true; |
3401 | bool cant_insert = false; | |
3402 | edge pred; | |
3403 | basic_block bprime; | |
c9145754 | 3404 | pre_expr eprime = NULL; |
d75dbccd DB |
3405 | edge_iterator ei; |
3406 | ||
c9145754 | 3407 | val = get_expr_value_id (expr); |
d75dbccd DB |
3408 | if (bitmap_set_contains_value (PHI_GEN (block), val)) |
3409 | continue; | |
3410 | if (bitmap_set_contains_value (AVAIL_OUT (dom), val)) | |
3411 | continue; | |
3412 | ||
d75dbccd DB |
3413 | FOR_EACH_EDGE (pred, ei, block->preds) |
3414 | { | |
c9145754 DB |
3415 | unsigned int vprime; |
3416 | pre_expr edoubleprime; | |
d75dbccd | 3417 | |
c4ab2baa RG |
3418 | /* We should never run insertion for the exit block |
3419 | and so not come across fake pred edges. */ | |
3420 | gcc_assert (!(pred->flags & EDGE_FAKE)); | |
d75dbccd DB |
3421 | bprime = pred->src; |
3422 | eprime = phi_translate (expr, ANTIC_IN (block), | |
3423 | PA_IN (block), | |
3424 | bprime, block); | |
3425 | ||
3426 | /* eprime will generally only be NULL if the | |
3427 | value of the expression, translated | |
3428 | through the PHI for this predecessor, is | |
3429 | undefined. If that is the case, we can't | |
3430 | make the expression fully redundant, | |
3431 | because its value is undefined along a | |
3432 | predecessor path. We can thus break out | |
3433 | early because it doesn't matter what the | |
3434 | rest of the results are. */ | |
3435 | if (eprime == NULL) | |
3436 | { | |
9771b263 | 3437 | avail[pred->dest_idx] = NULL; |
d75dbccd DB |
3438 | cant_insert = true; |
3439 | break; | |
3440 | } | |
3441 | ||
3442 | eprime = fully_constant_expression (eprime); | |
726a989a | 3443 | vprime = get_expr_value_id (eprime); |
e076271b | 3444 | edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime); |
9771b263 | 3445 | avail[pred->dest_idx] = edoubleprime; |
d75dbccd DB |
3446 | if (edoubleprime == NULL) |
3447 | { | |
3448 | by_all = false; | |
3449 | break; | |
3450 | } | |
d75dbccd DB |
3451 | } |
3452 | ||
3453 | /* If we can insert it, it's not the same value | |
3454 | already existing along every predecessor, and | |
3455 | it's defined by some predecessor, it is | |
3456 | partially redundant. */ | |
fa06ad0d | 3457 | if (!cant_insert && by_all) |
d75dbccd | 3458 | { |
fa06ad0d JR |
3459 | edge succ; |
3460 | bool do_insertion = false; | |
3461 | ||
3462 | /* Insert only if we can remove a later expression on a path | |
3463 | that we want to optimize for speed. | |
3464 | The phi node that we will be inserting in BLOCK is not free, | |
3465 | and inserting it for the sake of !optimize_for_speed successor | |
3466 | may cause regressions on the speed path. */ | |
3467 | FOR_EACH_EDGE (succ, ei, block->succs) | |
3468 | { | |
0c46ead2 JH |
3469 | if (bitmap_set_contains_value (PA_IN (succ->dest), val) |
3470 | || bitmap_set_contains_value (ANTIC_IN (succ->dest), val)) | |
fa06ad0d JR |
3471 | { |
3472 | if (optimize_edge_for_speed_p (succ)) | |
3473 | do_insertion = true; | |
3474 | } | |
3475 | } | |
3476 | ||
3477 | if (!do_insertion) | |
3478 | { | |
3479 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3480 | { | |
3481 | fprintf (dump_file, "Skipping partial partial redundancy " | |
3482 | "for expression "); | |
3483 | print_pre_expr (dump_file, expr); | |
0c46ead2 | 3484 | fprintf (dump_file, " (%04d), not (partially) anticipated " |
fa06ad0d JR |
3485 | "on any to be optimized for speed edges\n", val); |
3486 | } | |
3487 | } | |
3488 | else if (dbg_cnt (treepre_insert)) | |
3489 | { | |
3490 | pre_stats.pa_insert++; | |
19372838 RG |
3491 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3492 | { | |
3493 | fprintf (dump_file, "Found partial partial redundancy " | |
3494 | "for expression "); | |
3495 | print_pre_expr (dump_file, expr); | |
3496 | fprintf (dump_file, " (%04d)\n", | |
3497 | get_expr_value_id (expr)); | |
3498 | } | |
fa06ad0d JR |
3499 | if (insert_into_preds_of_block (block, |
3500 | get_expression_id (expr), | |
3501 | avail)) | |
3502 | new_stuff = true; | |
3503 | } | |
3504 | } | |
d75dbccd DB |
3505 | } |
3506 | } | |
3507 | ||
9771b263 | 3508 | exprs.release (); |
d75dbccd DB |
3509 | return new_stuff; |
3510 | } | |
83737db2 | 3511 | |
7e6eb623 DB |
3512 | static bool |
3513 | insert_aux (basic_block block) | |
6de9cd9a | 3514 | { |
7e6eb623 DB |
3515 | basic_block son; |
3516 | bool new_stuff = false; | |
6de9cd9a | 3517 | |
7e6eb623 | 3518 | if (block) |
6de9cd9a | 3519 | { |
7e6eb623 DB |
3520 | basic_block dom; |
3521 | dom = get_immediate_dominator (CDI_DOMINATORS, block); | |
3522 | if (dom) | |
a32b97a2 | 3523 | { |
3cd8c58a | 3524 | unsigned i; |
87c476a2 | 3525 | bitmap_iterator bi; |
6b416da1 | 3526 | bitmap_set_t newset = NEW_SETS (dom); |
e9284566 | 3527 | if (newset) |
87c476a2 | 3528 | { |
e9284566 DB |
3529 | /* Note that we need to value_replace both NEW_SETS, and |
3530 | AVAIL_OUT. For both the case of NEW_SETS, the value may be | |
3531 | represented by some non-simple expression here that we want | |
3532 | to replace it with. */ | |
83737db2 | 3533 | FOR_EACH_EXPR_ID_IN_SET (newset, i, bi) |
e9284566 | 3534 | { |
c9145754 | 3535 | pre_expr expr = expression_for_id (i); |
83737db2 DB |
3536 | bitmap_value_replace_in_set (NEW_SETS (block), expr); |
3537 | bitmap_value_replace_in_set (AVAIL_OUT (block), expr); | |
e9284566 | 3538 | } |
87c476a2 | 3539 | } |
c5cbcccf | 3540 | if (!single_pred_p (block)) |
6de9cd9a | 3541 | { |
83737db2 | 3542 | new_stuff |= do_regular_insertion (block, dom); |
d75dbccd DB |
3543 | if (do_partial_partial) |
3544 | new_stuff |= do_partial_partial_insertion (block, dom); | |
6de9cd9a DN |
3545 | } |
3546 | } | |
3547 | } | |
7e6eb623 DB |
3548 | for (son = first_dom_son (CDI_DOMINATORS, block); |
3549 | son; | |
3550 | son = next_dom_son (CDI_DOMINATORS, son)) | |
3551 | { | |
3552 | new_stuff |= insert_aux (son); | |
3553 | } | |
3554 | ||
3555 | return new_stuff; | |
6de9cd9a DN |
3556 | } |
3557 | ||
7e6eb623 | 3558 | /* Perform insertion of partially redundant values. */ |
6de9cd9a | 3559 | |
7e6eb623 DB |
3560 | static void |
3561 | insert (void) | |
6de9cd9a | 3562 | { |
7e6eb623 | 3563 | bool new_stuff = true; |
6de9cd9a | 3564 | basic_block bb; |
7e6eb623 | 3565 | int num_iterations = 0; |
b9c5e484 | 3566 | |
04a90bec | 3567 | FOR_ALL_BB_FN (bb, cfun) |
6b416da1 | 3568 | NEW_SETS (bb) = bitmap_set_new (); |
b9c5e484 | 3569 | |
7e6eb623 | 3570 | while (new_stuff) |
6de9cd9a | 3571 | { |
7e6eb623 | 3572 | num_iterations++; |
19372838 RG |
3573 | if (dump_file && dump_flags & TDF_DETAILS) |
3574 | fprintf (dump_file, "Starting insert iteration %d\n", num_iterations); | |
fefa31b5 | 3575 | new_stuff = insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
3dbc97a9 RB |
3576 | |
3577 | /* Clear the NEW sets before the next iteration. We have already | |
3578 | fully propagated its contents. */ | |
3579 | if (new_stuff) | |
04a90bec | 3580 | FOR_ALL_BB_FN (bb, cfun) |
3dbc97a9 | 3581 | bitmap_set_free (NEW_SETS (bb)); |
6de9cd9a | 3582 | } |
9fe0cb7d | 3583 | statistics_histogram_event (cfun, "insert iterations", num_iterations); |
7e6eb623 | 3584 | } |
6de9cd9a | 3585 | |
33c94679 | 3586 | |
665fcad8 SB |
3587 | /* Compute the AVAIL set for all basic blocks. |
3588 | ||
3589 | This function performs value numbering of the statements in each basic | |
3590 | block. The AVAIL sets are built from information we glean while doing | |
3591 | this value numbering, since the AVAIL sets contain only one entry per | |
7e6eb623 | 3592 | value. |
b9c5e484 | 3593 | |
7e6eb623 | 3594 | AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)]. |
ff2ad0f7 | 3595 | AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */ |
6de9cd9a | 3596 | |
7e6eb623 | 3597 | static void |
665fcad8 | 3598 | compute_avail (void) |
6de9cd9a | 3599 | { |
c9145754 | 3600 | |
665fcad8 SB |
3601 | basic_block block, son; |
3602 | basic_block *worklist; | |
3603 | size_t sp = 0; | |
b005da11 RG |
3604 | unsigned i; |
3605 | ||
3606 | /* We pretend that default definitions are defined in the entry block. | |
3607 | This includes function arguments and the static chain decl. */ | |
3608 | for (i = 1; i < num_ssa_names; ++i) | |
3609 | { | |
3610 | tree name = ssa_name (i); | |
3611 | pre_expr e; | |
3612 | if (!name | |
3613 | || !SSA_NAME_IS_DEFAULT_DEF (name) | |
3614 | || has_zero_uses (name) | |
ea057359 | 3615 | || virtual_operand_p (name)) |
b005da11 | 3616 | continue; |
665fcad8 | 3617 | |
b005da11 RG |
3618 | e = get_or_alloc_expr_for_name (name); |
3619 | add_to_value (get_expr_value_id (e), e); | |
fefa31b5 DM |
3620 | bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e); |
3621 | bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)), | |
3622 | e); | |
2984956b AP |
3623 | } |
3624 | ||
e3815735 RB |
3625 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3626 | { | |
fefa31b5 | 3627 | print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), |
e3815735 | 3628 | "tmp_gen", ENTRY_BLOCK); |
fefa31b5 | 3629 | print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)), |
e3815735 RB |
3630 | "avail_out", ENTRY_BLOCK); |
3631 | } | |
3632 | ||
665fcad8 | 3633 | /* Allocate the worklist. */ |
0cae8d31 | 3634 | worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun)); |
665fcad8 SB |
3635 | |
3636 | /* Seed the algorithm by putting the dominator children of the entry | |
3637 | block on the worklist. */ | |
fefa31b5 | 3638 | for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
665fcad8 SB |
3639 | son; |
3640 | son = next_dom_son (CDI_DOMINATORS, son)) | |
3641 | worklist[sp++] = son; | |
3642 | ||
81231d13 RB |
3643 | BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
3644 | = ssa_default_def (cfun, gimple_vop (cfun)); | |
3645 | ||
665fcad8 SB |
3646 | /* Loop until the worklist is empty. */ |
3647 | while (sp) | |
6de9cd9a | 3648 | { |
355fe088 | 3649 | gimple *stmt; |
7e6eb623 DB |
3650 | basic_block dom; |
3651 | ||
665fcad8 SB |
3652 | /* Pick a block from the worklist. */ |
3653 | block = worklist[--sp]; | |
3654 | ||
ff2ad0f7 DN |
3655 | /* Initially, the set of available values in BLOCK is that of |
3656 | its immediate dominator. */ | |
7e6eb623 DB |
3657 | dom = get_immediate_dominator (CDI_DOMINATORS, block); |
3658 | if (dom) | |
81231d13 RB |
3659 | { |
3660 | bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom)); | |
3661 | BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom); | |
3662 | } | |
33c94679 | 3663 | |
ff2ad0f7 | 3664 | /* Generate values for PHI nodes. */ |
538dd0b7 DM |
3665 | for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi); |
3666 | gsi_next (&gsi)) | |
e3815735 | 3667 | { |
538dd0b7 | 3668 | tree result = gimple_phi_result (gsi.phi ()); |
e3815735 RB |
3669 | |
3670 | /* We have no need for virtual phis, as they don't represent | |
3671 | actual computations. */ | |
3672 | if (virtual_operand_p (result)) | |
81231d13 RB |
3673 | { |
3674 | BB_LIVE_VOP_ON_EXIT (block) = result; | |
3675 | continue; | |
3676 | } | |
e3815735 RB |
3677 | |
3678 | pre_expr e = get_or_alloc_expr_for_name (result); | |
3679 | add_to_value (get_expr_value_id (e), e); | |
3680 | bitmap_value_insert_into_set (AVAIL_OUT (block), e); | |
3681 | bitmap_insert_into_set (PHI_GEN (block), e); | |
3682 | } | |
7e6eb623 | 3683 | |
a19eb9d2 RG |
3684 | BB_MAY_NOTRETURN (block) = 0; |
3685 | ||
ff2ad0f7 DN |
3686 | /* Now compute value numbers and populate value sets with all |
3687 | the expressions computed in BLOCK. */ | |
538dd0b7 DM |
3688 | for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi); |
3689 | gsi_next (&gsi)) | |
6de9cd9a | 3690 | { |
f47c96aa AM |
3691 | ssa_op_iter iter; |
3692 | tree op; | |
ff2ad0f7 | 3693 | |
726a989a | 3694 | stmt = gsi_stmt (gsi); |
ff2ad0f7 | 3695 | |
a19eb9d2 RG |
3696 | /* Cache whether the basic-block has any non-visible side-effect |
3697 | or control flow. | |
3698 | If this isn't a call or it is the last stmt in the | |
3699 | basic-block then the CFG represents things correctly. */ | |
9cbbba28 | 3700 | if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt)) |
a19eb9d2 RG |
3701 | { |
3702 | /* Non-looping const functions always return normally. | |
3703 | Otherwise the call might not return or have side-effects | |
3704 | that forbids hoisting possibly trapping expressions | |
3705 | before it. */ | |
3706 | int flags = gimple_call_flags (stmt); | |
3707 | if (!(flags & ECF_CONST) | |
3708 | || (flags & ECF_LOOPING_CONST_OR_PURE)) | |
3709 | BB_MAY_NOTRETURN (block) = 1; | |
3710 | } | |
3711 | ||
c9145754 | 3712 | FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF) |
83737db2 | 3713 | { |
c9145754 | 3714 | pre_expr e = get_or_alloc_expr_for_name (op); |
83737db2 | 3715 | |
c9145754 | 3716 | add_to_value (get_expr_value_id (e), e); |
40b178f4 | 3717 | bitmap_insert_into_set (TMP_GEN (block), e); |
c9145754 | 3718 | bitmap_value_insert_into_set (AVAIL_OUT (block), e); |
83737db2 DB |
3719 | } |
3720 | ||
81231d13 RB |
3721 | if (gimple_vdef (stmt)) |
3722 | BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt); | |
3723 | ||
17742d62 RG |
3724 | if (gimple_has_side_effects (stmt) |
3725 | || stmt_could_throw_p (stmt) | |
3726 | || is_gimple_debug (stmt)) | |
726a989a RB |
3727 | continue; |
3728 | ||
17742d62 | 3729 | FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE) |
e3815735 RB |
3730 | { |
3731 | if (ssa_undefined_value_p (op)) | |
3732 | continue; | |
3733 | pre_expr e = get_or_alloc_expr_for_name (op); | |
3734 | bitmap_value_insert_into_set (EXP_GEN (block), e); | |
3735 | } | |
17742d62 | 3736 | |
726a989a | 3737 | switch (gimple_code (stmt)) |
7e6eb623 | 3738 | { |
726a989a | 3739 | case GIMPLE_RETURN: |
c9145754 | 3740 | continue; |
726a989a RB |
3741 | |
3742 | case GIMPLE_CALL: | |
c9145754 | 3743 | { |
726a989a | 3744 | vn_reference_t ref; |
26f3a4e1 | 3745 | vn_reference_s ref1; |
726a989a | 3746 | pre_expr result = NULL; |
c9145754 | 3747 | |
9cbbba28 EB |
3748 | /* We can value number only calls to real functions. */ |
3749 | if (gimple_call_internal_p (stmt)) | |
726a989a | 3750 | continue; |
c9145754 | 3751 | |
538dd0b7 | 3752 | vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1); |
726a989a RB |
3753 | if (!ref) |
3754 | continue; | |
c9145754 | 3755 | |
cd32bb90 RG |
3756 | /* If the value of the call is not invalidated in |
3757 | this block until it is computed, add the expression | |
3758 | to EXP_GEN. */ | |
3759 | if (!gimple_vuse (stmt) | |
3760 | || gimple_code | |
3761 | (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI | |
3762 | || gimple_bb (SSA_NAME_DEF_STMT | |
3763 | (gimple_vuse (stmt))) != block) | |
3764 | { | |
971540bd | 3765 | result = pre_expr_pool.allocate (); |
cd32bb90 RG |
3766 | result->kind = REFERENCE; |
3767 | result->id = 0; | |
3768 | PRE_EXPR_REFERENCE (result) = ref; | |
3769 | ||
3770 | get_or_alloc_expression_id (result); | |
3771 | add_to_value (get_expr_value_id (result), result); | |
3c1d57d0 | 3772 | bitmap_value_insert_into_set (EXP_GEN (block), result); |
cd32bb90 | 3773 | } |
726a989a RB |
3774 | continue; |
3775 | } | |
c9145754 | 3776 | |
726a989a RB |
3777 | case GIMPLE_ASSIGN: |
3778 | { | |
3779 | pre_expr result = NULL; | |
17742d62 | 3780 | switch (vn_get_stmt_kind (stmt)) |
726a989a | 3781 | { |
17742d62 | 3782 | case VN_NARY: |
726a989a | 3783 | { |
61514fe4 | 3784 | enum tree_code code = gimple_assign_rhs_code (stmt); |
726a989a | 3785 | vn_nary_op_t nary; |
61514fe4 RG |
3786 | |
3787 | /* COND_EXPR and VEC_COND_EXPR are awkward in | |
3788 | that they contain an embedded complex expression. | |
3789 | Don't even try to shove those through PRE. */ | |
3790 | if (code == COND_EXPR | |
3791 | || code == VEC_COND_EXPR) | |
3792 | continue; | |
3793 | ||
91af9dc9 | 3794 | vn_nary_op_lookup_stmt (stmt, &nary); |
726a989a RB |
3795 | if (!nary) |
3796 | continue; | |
3797 | ||
073a8998 | 3798 | /* If the NARY traps and there was a preceding |
bea966c2 RG |
3799 | point in the block that might not return avoid |
3800 | adding the nary to EXP_GEN. */ | |
3801 | if (BB_MAY_NOTRETURN (block) | |
3802 | && vn_nary_may_trap (nary)) | |
3803 | continue; | |
3804 | ||
971540bd | 3805 | result = pre_expr_pool.allocate (); |
726a989a RB |
3806 | result->kind = NARY; |
3807 | result->id = 0; | |
3808 | PRE_EXPR_NARY (result) = nary; | |
3809 | break; | |
3810 | } | |
c9145754 | 3811 | |
17742d62 | 3812 | case VN_REFERENCE: |
726a989a RB |
3813 | { |
3814 | vn_reference_t ref; | |
726a989a | 3815 | vn_reference_lookup (gimple_assign_rhs1 (stmt), |
5006671f | 3816 | gimple_vuse (stmt), |
3bc27de7 | 3817 | VN_WALK, &ref); |
726a989a RB |
3818 | if (!ref) |
3819 | continue; | |
3820 | ||
cd32bb90 RG |
3821 | /* If the value of the reference is not invalidated in |
3822 | this block until it is computed, add the expression | |
3823 | to EXP_GEN. */ | |
3824 | if (gimple_vuse (stmt)) | |
3825 | { | |
355fe088 | 3826 | gimple *def_stmt; |
cd32bb90 RG |
3827 | bool ok = true; |
3828 | def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt)); | |
3829 | while (!gimple_nop_p (def_stmt) | |
3830 | && gimple_code (def_stmt) != GIMPLE_PHI | |
3831 | && gimple_bb (def_stmt) == block) | |
3832 | { | |
3833 | if (stmt_may_clobber_ref_p | |
3834 | (def_stmt, gimple_assign_rhs1 (stmt))) | |
3835 | { | |
3836 | ok = false; | |
3837 | break; | |
3838 | } | |
3839 | def_stmt | |
3840 | = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt)); | |
3841 | } | |
3842 | if (!ok) | |
3843 | continue; | |
3844 | } | |
3845 | ||
971540bd | 3846 | result = pre_expr_pool.allocate (); |
726a989a RB |
3847 | result->kind = REFERENCE; |
3848 | result->id = 0; | |
3849 | PRE_EXPR_REFERENCE (result) = ref; | |
3850 | break; | |
3851 | } | |
c9145754 | 3852 | |
726a989a | 3853 | default: |
726a989a | 3854 | continue; |
c9145754 | 3855 | } |
726a989a RB |
3856 | |
3857 | get_or_alloc_expression_id (result); | |
3858 | add_to_value (get_expr_value_id (result), result); | |
3c1d57d0 | 3859 | bitmap_value_insert_into_set (EXP_GEN (block), result); |
dda243de | 3860 | continue; |
c9145754 DB |
3861 | } |
3862 | default: | |
3863 | break; | |
7e6eb623 | 3864 | } |
6de9cd9a | 3865 | } |
726a989a | 3866 | |
e3815735 RB |
3867 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3868 | { | |
3869 | print_bitmap_set (dump_file, EXP_GEN (block), | |
3870 | "exp_gen", block->index); | |
3871 | print_bitmap_set (dump_file, PHI_GEN (block), | |
3872 | "phi_gen", block->index); | |
3873 | print_bitmap_set (dump_file, TMP_GEN (block), | |
3874 | "tmp_gen", block->index); | |
3875 | print_bitmap_set (dump_file, AVAIL_OUT (block), | |
3876 | "avail_out", block->index); | |
3877 | } | |
3878 | ||
665fcad8 SB |
3879 | /* Put the dominator children of BLOCK on the worklist of blocks |
3880 | to compute available sets for. */ | |
3881 | for (son = first_dom_son (CDI_DOMINATORS, block); | |
3882 | son; | |
3883 | son = next_dom_son (CDI_DOMINATORS, son)) | |
3884 | worklist[sp++] = son; | |
6de9cd9a | 3885 | } |
33c94679 | 3886 | |
665fcad8 | 3887 | free (worklist); |
7e6eb623 DB |
3888 | } |
3889 | ||
40b178f4 RG |
3890 | |
3891 | /* Local state for the eliminate domwalk. */ | |
355fe088 TS |
3892 | static vec<gimple *> el_to_remove; |
3893 | static vec<gimple *> el_to_fixup; | |
40b178f4 | 3894 | static unsigned int el_todo; |
9771b263 DN |
3895 | static vec<tree> el_avail; |
3896 | static vec<tree> el_avail_stack; | |
40b178f4 RG |
3897 | |
3898 | /* Return a leader for OP that is available at the current point of the | |
3899 | eliminate domwalk. */ | |
3d45dd59 RG |
3900 | |
3901 | static tree | |
40b178f4 | 3902 | eliminate_avail (tree op) |
3d45dd59 | 3903 | { |
40b178f4 RG |
3904 | tree valnum = VN_INFO (op)->valnum; |
3905 | if (TREE_CODE (valnum) == SSA_NAME) | |
3906 | { | |
3907 | if (SSA_NAME_IS_DEFAULT_DEF (valnum)) | |
3908 | return valnum; | |
9771b263 DN |
3909 | if (el_avail.length () > SSA_NAME_VERSION (valnum)) |
3910 | return el_avail[SSA_NAME_VERSION (valnum)]; | |
40b178f4 RG |
3911 | } |
3912 | else if (is_gimple_min_invariant (valnum)) | |
3913 | return valnum; | |
3914 | return NULL_TREE; | |
3915 | } | |
3916 | ||
3917 | /* At the current point of the eliminate domwalk make OP available. */ | |
3d45dd59 | 3918 | |
40b178f4 RG |
3919 | static void |
3920 | eliminate_push_avail (tree op) | |
3921 | { | |
3922 | tree valnum = VN_INFO (op)->valnum; | |
3923 | if (TREE_CODE (valnum) == SSA_NAME) | |
3924 | { | |
9771b263 DN |
3925 | if (el_avail.length () <= SSA_NAME_VERSION (valnum)) |
3926 | el_avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1); | |
6be4c4ec RB |
3927 | tree pushop = op; |
3928 | if (el_avail[SSA_NAME_VERSION (valnum)]) | |
3929 | pushop = el_avail[SSA_NAME_VERSION (valnum)]; | |
3930 | el_avail_stack.safe_push (pushop); | |
9771b263 | 3931 | el_avail[SSA_NAME_VERSION (valnum)] = op; |
40b178f4 RG |
3932 | } |
3933 | } | |
3934 | ||
3935 | /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns | |
3936 | the leader for the expression if insertion was successful. */ | |
3937 | ||
3938 | static tree | |
3939 | eliminate_insert (gimple_stmt_iterator *gsi, tree val) | |
3940 | { | |
3941 | tree expr = vn_get_expr_for (val); | |
3942 | if (!CONVERT_EXPR_P (expr) | |
3943 | && TREE_CODE (expr) != VIEW_CONVERT_EXPR) | |
3d45dd59 | 3944 | return NULL_TREE; |
3d45dd59 | 3945 | |
40b178f4 RG |
3946 | tree op = TREE_OPERAND (expr, 0); |
3947 | tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (op) : op; | |
3948 | if (!leader) | |
3d45dd59 | 3949 | return NULL_TREE; |
3d45dd59 | 3950 | |
40b178f4 | 3951 | tree res = make_temp_ssa_name (TREE_TYPE (val), NULL, "pretmp"); |
538dd0b7 DM |
3952 | gassign *tem = gimple_build_assign (res, |
3953 | fold_build1 (TREE_CODE (expr), | |
3954 | TREE_TYPE (expr), leader)); | |
40b178f4 RG |
3955 | gsi_insert_before (gsi, tem, GSI_SAME_STMT); |
3956 | VN_INFO_GET (res)->valnum = val; | |
3957 | ||
3958 | if (TREE_CODE (leader) == SSA_NAME) | |
3959 | gimple_set_plf (SSA_NAME_DEF_STMT (leader), NECESSARY, true); | |
3960 | ||
3961 | pre_stats.insertions++; | |
3962 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3963 | { | |
3964 | fprintf (dump_file, "Inserted "); | |
3965 | print_gimple_stmt (dump_file, tem, 0, 0); | |
3966 | } | |
3967 | ||
3968 | return res; | |
3d45dd59 | 3969 | } |
33c94679 | 3970 | |
4d9192b5 TS |
3971 | class eliminate_dom_walker : public dom_walker |
3972 | { | |
3973 | public: | |
b82ef848 RB |
3974 | eliminate_dom_walker (cdi_direction direction, bool do_pre_) |
3975 | : dom_walker (direction), do_pre (do_pre_) {} | |
4d9192b5 TS |
3976 | |
3977 | virtual void before_dom_children (basic_block); | |
3978 | virtual void after_dom_children (basic_block); | |
b82ef848 RB |
3979 | |
3980 | bool do_pre; | |
4d9192b5 TS |
3981 | }; |
3982 | ||
40b178f4 | 3983 | /* Perform elimination for the basic-block B during the domwalk. */ |
7e6eb623 | 3984 | |
4d9192b5 TS |
3985 | void |
3986 | eliminate_dom_walker::before_dom_children (basic_block b) | |
7e6eb623 | 3987 | { |
40b178f4 | 3988 | /* Mark new bb. */ |
9771b263 | 3989 | el_avail_stack.safe_push (NULL_TREE); |
40b178f4 | 3990 | |
6aa4c5b6 RB |
3991 | /* ??? If we do nothing for unreachable blocks then this will confuse |
3992 | tailmerging. Eventually we can reduce its reliance on SCCVN now | |
3993 | that we fully copy/constant-propagate (most) things. */ | |
1d44def2 | 3994 | |
538dd0b7 | 3995 | for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);) |
7e6eb623 | 3996 | { |
538dd0b7 | 3997 | gphi *phi = gsi.phi (); |
6aa4c5b6 | 3998 | tree res = PHI_RESULT (phi); |
c9145754 | 3999 | |
6aa4c5b6 | 4000 | if (virtual_operand_p (res)) |
40b178f4 | 4001 | { |
40b178f4 RG |
4002 | gsi_next (&gsi); |
4003 | continue; | |
4004 | } | |
c9145754 | 4005 | |
6aa4c5b6 RB |
4006 | tree sprime = eliminate_avail (res); |
4007 | if (sprime | |
4008 | && sprime != res) | |
40b178f4 | 4009 | { |
6aa4c5b6 RB |
4010 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4011 | { | |
4012 | fprintf (dump_file, "Replaced redundant PHI node defining "); | |
4013 | print_generic_expr (dump_file, res, 0); | |
4014 | fprintf (dump_file, " with "); | |
4015 | print_generic_expr (dump_file, sprime, 0); | |
4016 | fprintf (dump_file, "\n"); | |
4017 | } | |
40b178f4 | 4018 | |
6aa4c5b6 RB |
4019 | /* If we inserted this PHI node ourself, it's not an elimination. */ |
4020 | if (inserted_exprs | |
4021 | && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res))) | |
4022 | pre_stats.phis--; | |
4023 | else | |
4024 | pre_stats.eliminations++; | |
ff2ad0f7 | 4025 | |
6aa4c5b6 RB |
4026 | /* If we will propagate into all uses don't bother to do |
4027 | anything. */ | |
4028 | if (may_propagate_copy (res, sprime)) | |
4029 | { | |
4030 | /* Mark the PHI for removal. */ | |
4031 | el_to_remove.safe_push (phi); | |
4032 | gsi_next (&gsi); | |
4033 | continue; | |
4034 | } | |
40b178f4 | 4035 | |
6aa4c5b6 | 4036 | remove_phi_node (&gsi, false); |
40b178f4 | 4037 | |
6aa4c5b6 RB |
4038 | if (inserted_exprs |
4039 | && !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)) | |
4040 | && TREE_CODE (sprime) == SSA_NAME) | |
4041 | gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true); | |
40b178f4 | 4042 | |
6aa4c5b6 RB |
4043 | if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime))) |
4044 | sprime = fold_convert (TREE_TYPE (res), sprime); | |
355fe088 | 4045 | gimple *stmt = gimple_build_assign (res, sprime); |
6aa4c5b6 RB |
4046 | /* ??? It cannot yet be necessary (DOM walk). */ |
4047 | gimple_set_plf (stmt, NECESSARY, gimple_plf (phi, NECESSARY)); | |
40b178f4 | 4048 | |
6aa4c5b6 RB |
4049 | gimple_stmt_iterator gsi2 = gsi_after_labels (b); |
4050 | gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT); | |
4051 | continue; | |
4052 | } | |
3d45dd59 | 4053 | |
6aa4c5b6 RB |
4054 | eliminate_push_avail (res); |
4055 | gsi_next (&gsi); | |
4056 | } | |
4bcc5786 | 4057 | |
538dd0b7 DM |
4058 | for (gimple_stmt_iterator gsi = gsi_start_bb (b); |
4059 | !gsi_end_p (gsi); | |
4060 | gsi_next (&gsi)) | |
6aa4c5b6 RB |
4061 | { |
4062 | tree sprime = NULL_TREE; | |
355fe088 | 4063 | gimple *stmt = gsi_stmt (gsi); |
6aa4c5b6 RB |
4064 | tree lhs = gimple_get_lhs (stmt); |
4065 | if (lhs && TREE_CODE (lhs) == SSA_NAME | |
4066 | && !gimple_has_volatile_ops (stmt) | |
4bcc5786 RB |
4067 | /* See PR43491. Do not replace a global register variable when |
4068 | it is a the RHS of an assignment. Do replace local register | |
4069 | variables since gcc does not guarantee a local variable will | |
4070 | be allocated in register. | |
6aa4c5b6 RB |
4071 | ??? The fix isn't effective here. This should instead |
4072 | be ensured by not value-numbering them the same but treating | |
4073 | them like volatiles? */ | |
4074 | && !(gimple_assign_single_p (stmt) | |
4075 | && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL | |
4076 | && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)) | |
4077 | && is_global_var (gimple_assign_rhs1 (stmt))))) | |
4078 | { | |
4079 | sprime = eliminate_avail (lhs); | |
40b178f4 RG |
4080 | if (!sprime) |
4081 | { | |
3d45dd59 RG |
4082 | /* If there is no existing usable leader but SCCVN thinks |
4083 | it has an expression it wants to use as replacement, | |
4084 | insert that. */ | |
40b178f4 RG |
4085 | tree val = VN_INFO (lhs)->valnum; |
4086 | if (val != VN_TOP | |
4087 | && TREE_CODE (val) == SSA_NAME | |
4088 | && VN_INFO (val)->needs_insertion | |
a044f0e7 | 4089 | && VN_INFO (val)->expr != NULL_TREE |
40b178f4 RG |
4090 | && (sprime = eliminate_insert (&gsi, val)) != NULL_TREE) |
4091 | eliminate_push_avail (sprime); | |
4092 | } | |
40b178f4 | 4093 | |
6aa4c5b6 RB |
4094 | /* If this now constitutes a copy duplicate points-to |
4095 | and range info appropriately. This is especially | |
4096 | important for inserted code. See tree-ssa-copy.c | |
4097 | for similar code. */ | |
4098 | if (sprime | |
4099 | && TREE_CODE (sprime) == SSA_NAME) | |
4100 | { | |
4101 | basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime)); | |
4102 | if (POINTER_TYPE_P (TREE_TYPE (lhs)) | |
4103 | && SSA_NAME_PTR_INFO (lhs) | |
4104 | && !SSA_NAME_PTR_INFO (sprime)) | |
ff2ad0f7 | 4105 | { |
6aa4c5b6 RB |
4106 | duplicate_ssa_name_ptr_info (sprime, |
4107 | SSA_NAME_PTR_INFO (lhs)); | |
4108 | if (b != sprime_b) | |
4109 | mark_ptr_info_alignment_unknown | |
4110 | (SSA_NAME_PTR_INFO (sprime)); | |
40b178f4 | 4111 | } |
6aa4c5b6 RB |
4112 | else if (!POINTER_TYPE_P (TREE_TYPE (lhs)) |
4113 | && SSA_NAME_RANGE_INFO (lhs) | |
4114 | && !SSA_NAME_RANGE_INFO (sprime) | |
4115 | && b == sprime_b) | |
4116 | duplicate_ssa_name_range_info (sprime, | |
4117 | SSA_NAME_RANGE_TYPE (lhs), | |
4118 | SSA_NAME_RANGE_INFO (lhs)); | |
40b178f4 | 4119 | } |
30fd5881 | 4120 | |
6aa4c5b6 RB |
4121 | /* Inhibit the use of an inserted PHI on a loop header when |
4122 | the address of the memory reference is a simple induction | |
4123 | variable. In other cases the vectorizer won't do anything | |
4124 | anyway (either it's loop invariant or a complicated | |
4125 | expression). */ | |
40b178f4 | 4126 | if (sprime |
6aa4c5b6 RB |
4127 | && TREE_CODE (sprime) == SSA_NAME |
4128 | && do_pre | |
4129 | && flag_tree_loop_vectorize | |
4130 | && loop_outer (b->loop_father) | |
4131 | && has_zero_uses (sprime) | |
4132 | && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime)) | |
4133 | && gimple_assign_load_p (stmt)) | |
40b178f4 | 4134 | { |
355fe088 | 4135 | gimple *def_stmt = SSA_NAME_DEF_STMT (sprime); |
6aa4c5b6 RB |
4136 | basic_block def_bb = gimple_bb (def_stmt); |
4137 | if (gimple_code (def_stmt) == GIMPLE_PHI | |
4138 | && b->loop_father->header == def_bb) | |
cddaefa3 | 4139 | { |
6aa4c5b6 RB |
4140 | ssa_op_iter iter; |
4141 | tree op; | |
4142 | bool found = false; | |
4143 | FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE) | |
cddaefa3 | 4144 | { |
6aa4c5b6 RB |
4145 | affine_iv iv; |
4146 | def_bb = gimple_bb (SSA_NAME_DEF_STMT (op)); | |
4147 | if (def_bb | |
4148 | && flow_bb_inside_loop_p (b->loop_father, def_bb) | |
4149 | && simple_iv (b->loop_father, | |
4150 | b->loop_father, op, &iv, true)) | |
cddaefa3 | 4151 | { |
6aa4c5b6 RB |
4152 | found = true; |
4153 | break; | |
cddaefa3 | 4154 | } |
6aa4c5b6 RB |
4155 | } |
4156 | if (found) | |
4157 | { | |
4158 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
cddaefa3 | 4159 | { |
6aa4c5b6 RB |
4160 | fprintf (dump_file, "Not replacing "); |
4161 | print_gimple_expr (dump_file, stmt, 0, 0); | |
4162 | fprintf (dump_file, " with "); | |
4163 | print_generic_expr (dump_file, sprime, 0); | |
4164 | fprintf (dump_file, " which would add a loop" | |
4165 | " carried dependence to loop %d\n", | |
4166 | b->loop_father->num); | |
cddaefa3 | 4167 | } |
6aa4c5b6 | 4168 | /* Don't keep sprime available. */ |
6aa4c5b6 | 4169 | sprime = NULL_TREE; |
cddaefa3 RB |
4170 | } |
4171 | } | |
6aa4c5b6 RB |
4172 | } |
4173 | ||
4174 | if (sprime) | |
4175 | { | |
4176 | /* If we can propagate the value computed for LHS into | |
4177 | all uses don't bother doing anything with this stmt. */ | |
4178 | if (may_propagate_copy (lhs, sprime)) | |
4179 | { | |
4180 | /* Mark it for removal. */ | |
4181 | el_to_remove.safe_push (stmt); | |
4182 | ||
4183 | /* ??? Don't count copy/constant propagations. */ | |
4184 | if (gimple_assign_single_p (stmt) | |
4185 | && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME | |
4186 | || gimple_assign_rhs1 (stmt) == sprime)) | |
4187 | continue; | |
4188 | ||
4189 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4190 | { | |
4191 | fprintf (dump_file, "Replaced "); | |
4192 | print_gimple_expr (dump_file, stmt, 0, 0); | |
4193 | fprintf (dump_file, " with "); | |
4194 | print_generic_expr (dump_file, sprime, 0); | |
4195 | fprintf (dump_file, " in all uses of "); | |
4196 | print_gimple_stmt (dump_file, stmt, 0, 0); | |
4197 | } | |
4198 | ||
4199 | pre_stats.eliminations++; | |
4200 | continue; | |
4201 | } | |
4202 | ||
4203 | /* If this is an assignment from our leader (which | |
4204 | happens in the case the value-number is a constant) | |
4205 | then there is nothing to do. */ | |
4206 | if (gimple_assign_single_p (stmt) | |
4207 | && sprime == gimple_assign_rhs1 (stmt)) | |
4208 | continue; | |
4209 | ||
4210 | /* Else replace its RHS. */ | |
4211 | bool can_make_abnormal_goto | |
4212 | = is_gimple_call (stmt) | |
4213 | && stmt_can_make_abnormal_goto (stmt); | |
cddaefa3 | 4214 | |
40b178f4 RG |
4215 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4216 | { | |
4217 | fprintf (dump_file, "Replaced "); | |
4218 | print_gimple_expr (dump_file, stmt, 0, 0); | |
4219 | fprintf (dump_file, " with "); | |
4220 | print_generic_expr (dump_file, sprime, 0); | |
4221 | fprintf (dump_file, " in "); | |
4222 | print_gimple_stmt (dump_file, stmt, 0, 0); | |
ff2ad0f7 | 4223 | } |
40b178f4 RG |
4224 | |
4225 | if (TREE_CODE (sprime) == SSA_NAME) | |
4226 | gimple_set_plf (SSA_NAME_DEF_STMT (sprime), | |
4227 | NECESSARY, true); | |
40b178f4 RG |
4228 | |
4229 | pre_stats.eliminations++; | |
355fe088 | 4230 | gimple *orig_stmt = stmt; |
6aa4c5b6 RB |
4231 | if (!useless_type_conversion_p (TREE_TYPE (lhs), |
4232 | TREE_TYPE (sprime))) | |
4233 | sprime = fold_convert (TREE_TYPE (lhs), sprime); | |
d1c0308e RB |
4234 | tree vdef = gimple_vdef (stmt); |
4235 | tree vuse = gimple_vuse (stmt); | |
40b178f4 RG |
4236 | propagate_tree_value_into_stmt (&gsi, sprime); |
4237 | stmt = gsi_stmt (gsi); | |
4238 | update_stmt (stmt); | |
d1c0308e RB |
4239 | if (vdef != gimple_vdef (stmt)) |
4240 | VN_INFO (vdef)->valnum = vuse; | |
40b178f4 RG |
4241 | |
4242 | /* If we removed EH side-effects from the statement, clean | |
4243 | its EH information. */ | |
4244 | if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt)) | |
6cdb0ee3 | 4245 | { |
40b178f4 RG |
4246 | bitmap_set_bit (need_eh_cleanup, |
4247 | gimple_bb (stmt)->index); | |
6cdb0ee3 | 4248 | if (dump_file && (dump_flags & TDF_DETAILS)) |
40b178f4 RG |
4249 | fprintf (dump_file, " Removed EH side-effects.\n"); |
4250 | } | |
6cdb0ee3 | 4251 | |
40b178f4 RG |
4252 | /* Likewise for AB side-effects. */ |
4253 | if (can_make_abnormal_goto | |
4254 | && !stmt_can_make_abnormal_goto (stmt)) | |
4255 | { | |
4256 | bitmap_set_bit (need_ab_cleanup, | |
4257 | gimple_bb (stmt)->index); | |
4258 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4259 | fprintf (dump_file, " Removed AB side-effects.\n"); | |
6cdb0ee3 | 4260 | } |
6aa4c5b6 RB |
4261 | |
4262 | continue; | |
6cdb0ee3 | 4263 | } |
40b178f4 | 4264 | } |
6aa4c5b6 | 4265 | |
40b178f4 | 4266 | /* If the statement is a scalar store, see if the expression |
6aa4c5b6 RB |
4267 | has the same value number as its rhs. If so, the store is |
4268 | dead. */ | |
4269 | if (gimple_assign_single_p (stmt) | |
4270 | && !gimple_has_volatile_ops (stmt) | |
4271 | && !is_gimple_reg (gimple_assign_lhs (stmt)) | |
4272 | && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME | |
4273 | || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))) | |
4274 | { | |
4275 | tree val; | |
4276 | tree rhs = gimple_assign_rhs1 (stmt); | |
4277 | val = vn_reference_lookup (gimple_assign_lhs (stmt), | |
4278 | gimple_vuse (stmt), VN_WALK, NULL); | |
4279 | if (TREE_CODE (rhs) == SSA_NAME) | |
4280 | rhs = VN_INFO (rhs)->valnum; | |
4281 | if (val | |
4282 | && operand_equal_p (val, rhs, 0)) | |
4283 | { | |
4284 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4285 | { | |
4286 | fprintf (dump_file, "Deleted redundant store "); | |
4287 | print_gimple_stmt (dump_file, stmt, 0, 0); | |
4288 | } | |
4289 | ||
4290 | /* Queue stmt for removal. */ | |
4291 | el_to_remove.safe_push (stmt); | |
4292 | continue; | |
4293 | } | |
d2713985 RB |
4294 | } |
4295 | ||
4296 | /* If this is a control statement value numbering left edges | |
4297 | unexecuted on force the condition in a way consistent with | |
4298 | that. */ | |
4299 | if (gcond *cond = dyn_cast <gcond *> (stmt)) | |
4300 | { | |
4301 | if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) | |
4302 | ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE)) | |
4303 | { | |
4304 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4305 | { | |
4306 | fprintf (dump_file, "Removing unexecutable edge from "); | |
4307 | print_gimple_stmt (dump_file, stmt, 0, 0); | |
4308 | } | |
4309 | if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0) | |
4310 | == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0)) | |
4311 | gimple_cond_make_true (cond); | |
4312 | else | |
4313 | gimple_cond_make_false (cond); | |
4314 | update_stmt (cond); | |
4315 | el_todo |= TODO_cleanup_cfg; | |
4316 | continue; | |
4317 | } | |
4318 | } | |
40b178f4 | 4319 | |
6aa4c5b6 RB |
4320 | bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt); |
4321 | bool was_noreturn = (is_gimple_call (stmt) | |
4322 | && gimple_call_noreturn_p (stmt)); | |
4323 | tree vdef = gimple_vdef (stmt); | |
4324 | tree vuse = gimple_vuse (stmt); | |
4325 | ||
4326 | /* If we didn't replace the whole stmt (or propagate the result | |
4327 | into all uses), replace all uses on this stmt with their | |
4328 | leaders. */ | |
4329 | use_operand_p use_p; | |
4330 | ssa_op_iter iter; | |
4331 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE) | |
40b178f4 | 4332 | { |
6aa4c5b6 RB |
4333 | tree use = USE_FROM_PTR (use_p); |
4334 | /* ??? The call code above leaves stmt operands un-updated. */ | |
4335 | if (TREE_CODE (use) != SSA_NAME) | |
4336 | continue; | |
4337 | tree sprime = eliminate_avail (use); | |
4338 | if (sprime && sprime != use | |
4339 | && may_propagate_copy (use, sprime) | |
4340 | /* We substitute into debug stmts to avoid excessive | |
4341 | debug temporaries created by removed stmts, but we need | |
4342 | to avoid doing so for inserted sprimes as we never want | |
4343 | to create debug temporaries for them. */ | |
4344 | && (!inserted_exprs | |
4345 | || TREE_CODE (sprime) != SSA_NAME | |
4346 | || !is_gimple_debug (stmt) | |
4347 | || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime)))) | |
aa7069aa | 4348 | { |
6aa4c5b6 RB |
4349 | propagate_value (use_p, sprime); |
4350 | gimple_set_modified (stmt, true); | |
4351 | if (TREE_CODE (sprime) == SSA_NAME | |
4352 | && !is_gimple_debug (stmt)) | |
4353 | gimple_set_plf (SSA_NAME_DEF_STMT (sprime), | |
4354 | NECESSARY, true); | |
40b178f4 RG |
4355 | } |
4356 | } | |
6aa4c5b6 | 4357 | |
40b178f4 | 4358 | /* Visit indirect calls and turn them into direct calls if |
6aa4c5b6 | 4359 | possible using the devirtualization machinery. */ |
538dd0b7 | 4360 | if (gcall *call_stmt = dyn_cast <gcall *> (stmt)) |
40b178f4 | 4361 | { |
538dd0b7 | 4362 | tree fn = gimple_call_fn (call_stmt); |
6aa4c5b6 | 4363 | if (fn |
7d0aa05b JH |
4364 | && flag_devirtualize |
4365 | && virtual_method_call_p (fn)) | |
e248d83f | 4366 | { |
6f8091fc | 4367 | tree otr_type = obj_type_ref_class (fn); |
7d0aa05b | 4368 | tree instance; |
6f8091fc | 4369 | ipa_polymorphic_call_context context (current_function_decl, fn, stmt, &instance); |
7d0aa05b JH |
4370 | bool final; |
4371 | ||
4d7cf10d | 4372 | context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn), otr_type, stmt); |
7d0aa05b JH |
4373 | |
4374 | vec <cgraph_node *>targets | |
4375 | = possible_polymorphic_call_targets (obj_type_ref_class (fn), | |
4376 | tree_to_uhwi | |
4377 | (OBJ_TYPE_REF_TOKEN (fn)), | |
4378 | context, | |
4379 | &final); | |
1218f76f | 4380 | if (dump_file) |
7d0aa05b JH |
4381 | dump_possible_polymorphic_call_targets (dump_file, |
4382 | obj_type_ref_class (fn), | |
4383 | tree_to_uhwi | |
4384 | (OBJ_TYPE_REF_TOKEN (fn)), | |
4385 | context); | |
4386 | if (final && targets.length () <= 1 && dbg_cnt (devirt)) | |
e248d83f | 4387 | { |
7d0aa05b JH |
4388 | tree fn; |
4389 | if (targets.length () == 1) | |
4390 | fn = targets[0]->decl; | |
4391 | else | |
4392 | fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
6aa4c5b6 RB |
4393 | if (dump_enabled_p ()) |
4394 | { | |
807b7d62 | 4395 | location_t loc = gimple_location_safe (stmt); |
6aa4c5b6 RB |
4396 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, |
4397 | "converting indirect call to " | |
4398 | "function %s\n", | |
d52f5295 | 4399 | cgraph_node::get (fn)->name ()); |
6aa4c5b6 | 4400 | } |
538dd0b7 | 4401 | gimple_call_set_fndecl (call_stmt, fn); |
0b986c6a | 4402 | maybe_remove_unused_call_args (cfun, call_stmt); |
6aa4c5b6 | 4403 | gimple_set_modified (stmt, true); |
e248d83f MJ |
4404 | } |
4405 | } | |
6aa4c5b6 | 4406 | } |
87c20fe7 | 4407 | |
6aa4c5b6 RB |
4408 | if (gimple_modified_p (stmt)) |
4409 | { | |
4410 | /* If a formerly non-invariant ADDR_EXPR is turned into an | |
4411 | invariant one it was on a separate stmt. */ | |
4412 | if (gimple_assign_single_p (stmt) | |
4413 | && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR) | |
4414 | recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt)); | |
355fe088 | 4415 | gimple *old_stmt = stmt; |
6aa4c5b6 RB |
4416 | if (is_gimple_call (stmt)) |
4417 | { | |
4418 | /* ??? Only fold calls inplace for now, this may create new | |
4419 | SSA names which in turn will confuse free_scc_vn SSA name | |
4420 | release code. */ | |
4421 | fold_stmt_inplace (&gsi); | |
40b178f4 RG |
4422 | /* When changing a call into a noreturn call, cfg cleanup |
4423 | is needed to fix up the noreturn call. */ | |
4424 | if (!was_noreturn && gimple_call_noreturn_p (stmt)) | |
2a5671ee | 4425 | el_to_fixup.safe_push (stmt); |
6aa4c5b6 RB |
4426 | } |
4427 | else | |
4428 | { | |
4429 | fold_stmt (&gsi); | |
4430 | stmt = gsi_stmt (gsi); | |
4431 | if ((gimple_code (stmt) == GIMPLE_COND | |
538dd0b7 DM |
4432 | && (gimple_cond_true_p (as_a <gcond *> (stmt)) |
4433 | || gimple_cond_false_p (as_a <gcond *> (stmt)))) | |
6aa4c5b6 | 4434 | || (gimple_code (stmt) == GIMPLE_SWITCH |
538dd0b7 DM |
4435 | && TREE_CODE (gimple_switch_index ( |
4436 | as_a <gswitch *> (stmt))) | |
4437 | == INTEGER_CST)) | |
6aa4c5b6 RB |
4438 | el_todo |= TODO_cleanup_cfg; |
4439 | } | |
4440 | /* If we removed EH side-effects from the statement, clean | |
4441 | its EH information. */ | |
4442 | if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)) | |
4443 | { | |
4444 | bitmap_set_bit (need_eh_cleanup, | |
4445 | gimple_bb (stmt)->index); | |
4446 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4447 | fprintf (dump_file, " Removed EH side-effects.\n"); | |
4448 | } | |
4449 | /* Likewise for AB side-effects. */ | |
4450 | if (can_make_abnormal_goto | |
4451 | && !stmt_can_make_abnormal_goto (stmt)) | |
4452 | { | |
4453 | bitmap_set_bit (need_ab_cleanup, | |
4454 | gimple_bb (stmt)->index); | |
4455 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4456 | fprintf (dump_file, " Removed AB side-effects.\n"); | |
4457 | } | |
4458 | update_stmt (stmt); | |
4459 | if (vdef != gimple_vdef (stmt)) | |
4460 | VN_INFO (vdef)->valnum = vuse; | |
4461 | } | |
30fd5881 | 4462 | |
5d5cb4d4 RB |
4463 | /* Make new values available - for fully redundant LHS we |
4464 | continue with the next stmt above and skip this. */ | |
4465 | def_operand_p defp; | |
4466 | FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF) | |
4467 | eliminate_push_avail (DEF_FROM_PTR (defp)); | |
6aa4c5b6 | 4468 | } |
40b178f4 | 4469 | |
6aa4c5b6 RB |
4470 | /* Replace destination PHI arguments. */ |
4471 | edge_iterator ei; | |
4472 | edge e; | |
4473 | FOR_EACH_EDGE (e, ei, b->succs) | |
4474 | { | |
538dd0b7 DM |
4475 | for (gphi_iterator gsi = gsi_start_phis (e->dest); |
4476 | !gsi_end_p (gsi); | |
4477 | gsi_next (&gsi)) | |
6aa4c5b6 | 4478 | { |
538dd0b7 | 4479 | gphi *phi = gsi.phi (); |
6aa4c5b6 RB |
4480 | use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e); |
4481 | tree arg = USE_FROM_PTR (use_p); | |
4482 | if (TREE_CODE (arg) != SSA_NAME | |
4483 | || virtual_operand_p (arg)) | |
4484 | continue; | |
4485 | tree sprime = eliminate_avail (arg); | |
4486 | if (sprime && may_propagate_copy (arg, sprime)) | |
4487 | { | |
4488 | propagate_value (use_p, sprime); | |
4489 | if (TREE_CODE (sprime) == SSA_NAME) | |
4490 | gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true); | |
aa7069aa | 4491 | } |
83737db2 | 4492 | } |
40b178f4 RG |
4493 | } |
4494 | } | |
439ef907 | 4495 | |
40b178f4 | 4496 | /* Make no longer available leaders no longer available. */ |
439ef907 | 4497 | |
4d9192b5 TS |
4498 | void |
4499 | eliminate_dom_walker::after_dom_children (basic_block) | |
40b178f4 RG |
4500 | { |
4501 | tree entry; | |
9771b263 | 4502 | while ((entry = el_avail_stack.pop ()) != NULL_TREE) |
6be4c4ec RB |
4503 | { |
4504 | tree valnum = VN_INFO (entry)->valnum; | |
4505 | tree old = el_avail[SSA_NAME_VERSION (valnum)]; | |
4506 | if (old == entry) | |
4507 | el_avail[SSA_NAME_VERSION (valnum)] = NULL_TREE; | |
4508 | else | |
4509 | el_avail[SSA_NAME_VERSION (valnum)] = entry; | |
4510 | } | |
40b178f4 | 4511 | } |
439ef907 | 4512 | |
40b178f4 | 4513 | /* Eliminate fully redundant computations. */ |
439ef907 | 4514 | |
40b178f4 | 4515 | static unsigned int |
b82ef848 | 4516 | eliminate (bool do_pre) |
40b178f4 | 4517 | { |
40b178f4 | 4518 | gimple_stmt_iterator gsi; |
355fe088 | 4519 | gimple *stmt; |
439ef907 | 4520 | |
40b178f4 RG |
4521 | need_eh_cleanup = BITMAP_ALLOC (NULL); |
4522 | need_ab_cleanup = BITMAP_ALLOC (NULL); | |
4d2ab9e3 | 4523 | |
9771b263 | 4524 | el_to_remove.create (0); |
2a5671ee | 4525 | el_to_fixup.create (0); |
40b178f4 | 4526 | el_todo = 0; |
52e4630c | 4527 | el_avail.create (num_ssa_names); |
9771b263 | 4528 | el_avail_stack.create (0); |
40b178f4 | 4529 | |
b82ef848 RB |
4530 | eliminate_dom_walker (CDI_DOMINATORS, |
4531 | do_pre).walk (cfun->cfg->x_entry_block_ptr); | |
40b178f4 | 4532 | |
9771b263 DN |
4533 | el_avail.release (); |
4534 | el_avail_stack.release (); | |
b80280f2 | 4535 | |
5006671f | 4536 | /* We cannot remove stmts during BB walk, especially not release SSA |
439ef907 | 4537 | names there as this confuses the VN machinery. The stmts ending |
6aa4c5b6 RB |
4538 | up in el_to_remove are either stores or simple copies. |
4539 | Remove stmts in reverse order to make debug stmt creation possible. */ | |
4540 | while (!el_to_remove.is_empty ()) | |
5006671f | 4541 | { |
6aa4c5b6 RB |
4542 | stmt = el_to_remove.pop (); |
4543 | ||
4544 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
439ef907 | 4545 | { |
6aa4c5b6 RB |
4546 | fprintf (dump_file, "Removing dead stmt "); |
4547 | print_gimple_stmt (dump_file, stmt, 0, 0); | |
439ef907 RG |
4548 | } |
4549 | ||
6aa4c5b6 RB |
4550 | tree lhs; |
4551 | if (gimple_code (stmt) == GIMPLE_PHI) | |
4552 | lhs = gimple_phi_result (stmt); | |
4553 | else | |
4554 | lhs = gimple_get_lhs (stmt); | |
4555 | ||
4556 | if (inserted_exprs | |
4557 | && TREE_CODE (lhs) == SSA_NAME) | |
4558 | bitmap_clear_bit (inserted_exprs, SSA_NAME_VERSION (lhs)); | |
4559 | ||
4560 | gsi = gsi_for_stmt (stmt); | |
4561 | if (gimple_code (stmt) == GIMPLE_PHI) | |
25b7069a | 4562 | remove_phi_node (&gsi, true); |
6aa4c5b6 | 4563 | else |
439ef907 | 4564 | { |
a2c0ed2e | 4565 | basic_block bb = gimple_bb (stmt); |
439ef907 | 4566 | unlink_stmt_vdef (stmt); |
b5b3ec3e RG |
4567 | if (gsi_remove (&gsi, true)) |
4568 | bitmap_set_bit (need_eh_cleanup, bb->index); | |
439ef907 RG |
4569 | release_defs (stmt); |
4570 | } | |
25b7069a RB |
4571 | |
4572 | /* Removing a stmt may expose a forwarder block. */ | |
4573 | el_todo |= TODO_cleanup_cfg; | |
5006671f | 4574 | } |
9771b263 | 4575 | el_to_remove.release (); |
5006671f | 4576 | |
2a5671ee RB |
4577 | /* Fixup stmts that became noreturn calls. This may require splitting |
4578 | blocks and thus isn't possible during the dominator walk. Do this | |
4579 | in reverse order so we don't inadvertedly remove a stmt we want to | |
4580 | fixup by visiting a dominating now noreturn call first. */ | |
4581 | while (!el_to_fixup.is_empty ()) | |
4582 | { | |
4583 | stmt = el_to_fixup.pop (); | |
4584 | ||
4585 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4586 | { | |
4587 | fprintf (dump_file, "Fixing up noreturn call "); | |
4588 | print_gimple_stmt (dump_file, stmt, 0, 0); | |
4589 | } | |
4590 | ||
4591 | if (fixup_noreturn_call (stmt)) | |
4592 | el_todo |= TODO_cleanup_cfg; | |
4593 | } | |
4594 | el_to_fixup.release (); | |
4595 | ||
40b178f4 RG |
4596 | return el_todo; |
4597 | } | |
4598 | ||
4599 | /* Perform CFG cleanups made necessary by elimination. */ | |
4600 | ||
9c370032 | 4601 | static unsigned |
40b178f4 RG |
4602 | fini_eliminate (void) |
4603 | { | |
4604 | bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup); | |
4605 | bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup); | |
4606 | ||
4607 | if (do_eh_cleanup) | |
4608 | gimple_purge_all_dead_eh_edges (need_eh_cleanup); | |
4609 | ||
4610 | if (do_ab_cleanup) | |
4611 | gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup); | |
4612 | ||
4613 | BITMAP_FREE (need_eh_cleanup); | |
4614 | BITMAP_FREE (need_ab_cleanup); | |
4615 | ||
4616 | if (do_eh_cleanup || do_ab_cleanup) | |
9c370032 RB |
4617 | return TODO_cleanup_cfg; |
4618 | return 0; | |
6de9cd9a DN |
4619 | } |
4620 | ||
0fc6c492 DB |
4621 | /* Borrow a bit of tree-ssa-dce.c for the moment. |
4622 | XXX: In 4.1, we should be able to just run a DCE pass after PRE, though | |
4623 | this may be a bit faster, and we may want critical edges kept split. */ | |
4624 | ||
4625 | /* If OP's defining statement has not already been determined to be necessary, | |
d4e6fecb | 4626 | mark that statement necessary. Return the stmt, if it is newly |
b9c5e484 | 4627 | necessary. */ |
0fc6c492 | 4628 | |
355fe088 | 4629 | static inline gimple * |
d4e6fecb | 4630 | mark_operand_necessary (tree op) |
0fc6c492 | 4631 | { |
355fe088 | 4632 | gimple *stmt; |
0fc6c492 DB |
4633 | |
4634 | gcc_assert (op); | |
4635 | ||
c90186eb DB |
4636 | if (TREE_CODE (op) != SSA_NAME) |
4637 | return NULL; | |
4638 | ||
0fc6c492 DB |
4639 | stmt = SSA_NAME_DEF_STMT (op); |
4640 | gcc_assert (stmt); | |
4641 | ||
726a989a RB |
4642 | if (gimple_plf (stmt, NECESSARY) |
4643 | || gimple_nop_p (stmt)) | |
d4e6fecb | 4644 | return NULL; |
0fc6c492 | 4645 | |
726a989a | 4646 | gimple_set_plf (stmt, NECESSARY, true); |
d4e6fecb | 4647 | return stmt; |
0fc6c492 DB |
4648 | } |
4649 | ||
4650 | /* Because we don't follow exactly the standard PRE algorithm, and decide not | |
4651 | to insert PHI nodes sometimes, and because value numbering of casts isn't | |
4652 | perfect, we sometimes end up inserting dead code. This simple DCE-like | |
4653 | pass removes any insertions we made that weren't actually used. */ | |
4654 | ||
4655 | static void | |
4656 | remove_dead_inserted_code (void) | |
4657 | { | |
0ab555de MM |
4658 | bitmap worklist; |
4659 | unsigned i; | |
4660 | bitmap_iterator bi; | |
355fe088 | 4661 | gimple *t; |
0fc6c492 | 4662 | |
0ab555de MM |
4663 | worklist = BITMAP_ALLOC (NULL); |
4664 | EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi) | |
0fc6c492 | 4665 | { |
0ab555de | 4666 | t = SSA_NAME_DEF_STMT (ssa_name (i)); |
726a989a | 4667 | if (gimple_plf (t, NECESSARY)) |
0ab555de | 4668 | bitmap_set_bit (worklist, i); |
0fc6c492 | 4669 | } |
0ab555de | 4670 | while (!bitmap_empty_p (worklist)) |
0fc6c492 | 4671 | { |
0ab555de MM |
4672 | i = bitmap_first_set_bit (worklist); |
4673 | bitmap_clear_bit (worklist, i); | |
4674 | t = SSA_NAME_DEF_STMT (ssa_name (i)); | |
c90186eb DB |
4675 | |
4676 | /* PHI nodes are somewhat special in that each PHI alternative has | |
4677 | data and control dependencies. All the statements feeding the | |
4678 | PHI node's arguments are always necessary. */ | |
726a989a | 4679 | if (gimple_code (t) == GIMPLE_PHI) |
0fc6c492 | 4680 | { |
726a989a | 4681 | unsigned k; |
d4e6fecb | 4682 | |
726a989a | 4683 | for (k = 0; k < gimple_phi_num_args (t); k++) |
83737db2 | 4684 | { |
0fc6c492 DB |
4685 | tree arg = PHI_ARG_DEF (t, k); |
4686 | if (TREE_CODE (arg) == SSA_NAME) | |
d4e6fecb | 4687 | { |
355fe088 | 4688 | gimple *n = mark_operand_necessary (arg); |
726a989a | 4689 | if (n) |
0ab555de | 4690 | bitmap_set_bit (worklist, SSA_NAME_VERSION (arg)); |
d4e6fecb | 4691 | } |
0fc6c492 DB |
4692 | } |
4693 | } | |
4694 | else | |
4695 | { | |
4696 | /* Propagate through the operands. Examine all the USE, VUSE and | |
89fb70a3 | 4697 | VDEF operands in this statement. Mark all the statements |
0fc6c492 DB |
4698 | which feed this statement's uses as necessary. */ |
4699 | ssa_op_iter iter; | |
4700 | tree use; | |
4701 | ||
38635499 | 4702 | /* The operands of VDEF expressions are also needed as they |
0fc6c492 | 4703 | represent potential definitions that may reach this |
89fb70a3 | 4704 | statement (VDEF operands allow us to follow def-def |
0fc6c492 | 4705 | links). */ |
33c94679 | 4706 | |
0fc6c492 | 4707 | FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES) |
d4e6fecb | 4708 | { |
355fe088 | 4709 | gimple *n = mark_operand_necessary (use); |
d4e6fecb | 4710 | if (n) |
0ab555de | 4711 | bitmap_set_bit (worklist, SSA_NAME_VERSION (use)); |
d4e6fecb | 4712 | } |
0fc6c492 DB |
4713 | } |
4714 | } | |
c90186eb | 4715 | |
0ab555de | 4716 | EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi) |
0fc6c492 | 4717 | { |
0ab555de | 4718 | t = SSA_NAME_DEF_STMT (ssa_name (i)); |
726a989a | 4719 | if (!gimple_plf (t, NECESSARY)) |
0fc6c492 | 4720 | { |
726a989a | 4721 | gimple_stmt_iterator gsi; |
c90186eb | 4722 | |
0fc6c492 DB |
4723 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4724 | { | |
4725 | fprintf (dump_file, "Removing unnecessary insertion:"); | |
726a989a | 4726 | print_gimple_stmt (dump_file, t, 0, 0); |
0fc6c492 | 4727 | } |
c90186eb | 4728 | |
726a989a RB |
4729 | gsi = gsi_for_stmt (t); |
4730 | if (gimple_code (t) == GIMPLE_PHI) | |
4731 | remove_phi_node (&gsi, true); | |
0fc6c492 | 4732 | else |
b70fdfe4 AO |
4733 | { |
4734 | gsi_remove (&gsi, true); | |
4735 | release_defs (t); | |
4736 | } | |
0fc6c492 DB |
4737 | } |
4738 | } | |
0ab555de | 4739 | BITMAP_FREE (worklist); |
0fc6c492 | 4740 | } |
c90186eb | 4741 | |
9ca87236 | 4742 | |
ff2ad0f7 | 4743 | /* Initialize data structures used by PRE. */ |
6de9cd9a DN |
4744 | |
4745 | static void | |
40b178f4 | 4746 | init_pre (void) |
6de9cd9a | 4747 | { |
c9145754 DB |
4748 | basic_block bb; |
4749 | ||
4750 | next_expression_id = 1; | |
9771b263 DN |
4751 | expressions.create (0); |
4752 | expressions.safe_push (NULL); | |
4753 | value_expressions.create (get_max_value_id () + 1); | |
c3284718 | 4754 | value_expressions.safe_grow_cleared (get_max_value_id () + 1); |
9771b263 | 4755 | name_to_id.create (0); |
c9145754 | 4756 | |
0ab555de | 4757 | inserted_exprs = BITMAP_ALLOC (NULL); |
c90186eb | 4758 | |
b71b4522 DB |
4759 | connect_infinite_loops_to_exit (); |
4760 | memset (&pre_stats, 0, sizeof (pre_stats)); | |
4761 | ||
0cae8d31 | 4762 | postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun)); |
585d0dc4 | 4763 | postorder_num = inverted_post_order_compute (postorder); |
c9145754 | 4764 | |
a72ae88a | 4765 | alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets)); |
c9145754 | 4766 | |
b71b4522 | 4767 | calculate_dominance_info (CDI_POST_DOMINATORS); |
d75dbccd DB |
4768 | calculate_dominance_info (CDI_DOMINATORS); |
4769 | ||
c9145754 | 4770 | bitmap_obstack_initialize (&grand_bitmap_obstack); |
c203e8a7 TS |
4771 | phi_translate_table = new hash_table<expr_pred_trans_d> (5110); |
4772 | expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3); | |
04a90bec | 4773 | FOR_ALL_BB_FN (bb, cfun) |
c9145754 | 4774 | { |
40b178f4 RG |
4775 | EXP_GEN (bb) = bitmap_set_new (); |
4776 | PHI_GEN (bb) = bitmap_set_new (); | |
4777 | TMP_GEN (bb) = bitmap_set_new (); | |
c9145754 DB |
4778 | AVAIL_OUT (bb) = bitmap_set_new (); |
4779 | } | |
ff2ad0f7 DN |
4780 | } |
4781 | ||
4782 | ||
4783 | /* Deallocate data structures used by PRE. */ | |
6de9cd9a | 4784 | |
ff2ad0f7 | 4785 | static void |
40b178f4 | 4786 | fini_pre () |
ff2ad0f7 | 4787 | { |
c9145754 | 4788 | free (postorder); |
9771b263 | 4789 | value_expressions.release (); |
0ab555de | 4790 | BITMAP_FREE (inserted_exprs); |
c9145754 | 4791 | bitmap_obstack_release (&grand_bitmap_obstack); |
971540bd ML |
4792 | bitmap_set_pool.release (); |
4793 | pre_expr_pool.release (); | |
c203e8a7 TS |
4794 | delete phi_translate_table; |
4795 | phi_translate_table = NULL; | |
4796 | delete expression_to_id; | |
4797 | expression_to_id = NULL; | |
9771b263 | 4798 | name_to_id.release (); |
50265400 | 4799 | |
a72ae88a | 4800 | free_aux_for_blocks (); |
6809cbf9 | 4801 | |
b71b4522 | 4802 | free_dominance_info (CDI_POST_DOMINATORS); |
ff2ad0f7 DN |
4803 | } |
4804 | ||
be55bfe6 | 4805 | namespace { |
ff2ad0f7 | 4806 | |
be55bfe6 TS |
4807 | const pass_data pass_data_pre = |
4808 | { | |
4809 | GIMPLE_PASS, /* type */ | |
4810 | "pre", /* name */ | |
4811 | OPTGROUP_NONE, /* optinfo_flags */ | |
be55bfe6 TS |
4812 | TV_TREE_PRE, /* tv_id */ |
4813 | /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before | |
4814 | pass_pre. */ | |
4815 | ( PROP_no_crit_edges | PROP_cfg | PROP_ssa ), /* properties_required */ | |
4816 | 0, /* properties_provided */ | |
4817 | PROP_no_crit_edges, /* properties_destroyed */ | |
4818 | TODO_rebuild_alias, /* todo_flags_start */ | |
3bea341f | 4819 | 0, /* todo_flags_finish */ |
be55bfe6 TS |
4820 | }; |
4821 | ||
4822 | class pass_pre : public gimple_opt_pass | |
4823 | { | |
4824 | public: | |
4825 | pass_pre (gcc::context *ctxt) | |
4826 | : gimple_opt_pass (pass_data_pre, ctxt) | |
4827 | {} | |
4828 | ||
4829 | /* opt_pass methods: */ | |
4830 | virtual bool gate (function *) { return flag_tree_pre != 0; } | |
4831 | virtual unsigned int execute (function *); | |
4832 | ||
4833 | }; // class pass_pre | |
4834 | ||
4835 | unsigned int | |
4836 | pass_pre::execute (function *fun) | |
ff2ad0f7 | 4837 | { |
b80280f2 | 4838 | unsigned int todo = 0; |
83737db2 | 4839 | |
fa06ad0d | 4840 | do_partial_partial = |
be55bfe6 | 4841 | flag_tree_partial_pre && optimize_function_for_speed_p (fun); |
ff2ad0f7 | 4842 | |
c9145754 DB |
4843 | /* This has to happen before SCCVN runs because |
4844 | loop_optimizer_init may create new phis, etc. */ | |
40b178f4 | 4845 | loop_optimizer_init (LOOPS_NORMAL); |
c90186eb | 4846 | |
40b178f4 | 4847 | if (!run_scc_vn (VN_WALK)) |
863d2a57 | 4848 | { |
40b178f4 | 4849 | loop_optimizer_finalize (); |
b80280f2 | 4850 | return 0; |
863d2a57 | 4851 | } |
a7d4562a | 4852 | |
40b178f4 | 4853 | init_pre (); |
a8338640 | 4854 | scev_initialize (); |
c9145754 | 4855 | |
c9145754 | 4856 | /* Collect and value number expressions computed in each basic block. */ |
665fcad8 | 4857 | compute_avail (); |
6de9cd9a | 4858 | |
7e6eb623 DB |
4859 | /* Insert can get quite slow on an incredibly large number of basic |
4860 | blocks due to some quadratic behavior. Until this behavior is | |
4861 | fixed, don't run it when he have an incredibly large number of | |
4862 | bb's. If we aren't going to run insert, there is no point in | |
4863 | computing ANTIC, either, even though it's plenty fast. */ | |
be55bfe6 | 4864 | if (n_basic_blocks_for_fn (fun) < 4000) |
6de9cd9a | 4865 | { |
7e6eb623 | 4866 | compute_antic (); |
7e6eb623 DB |
4867 | insert (); |
4868 | } | |
ff2ad0f7 | 4869 | |
0ab555de MM |
4870 | /* Make sure to remove fake edges before committing our inserts. |
4871 | This makes sure we don't end up with extra critical edges that | |
4872 | we would need to split. */ | |
4873 | remove_fake_exit_edges (); | |
4874 | gsi_commit_edge_inserts (); | |
4875 | ||
81231d13 RB |
4876 | /* Eliminate folds statements which might (should not...) end up |
4877 | not keeping virtual operands up-to-date. */ | |
4878 | gcc_assert (!need_ssa_update_p (fun)); | |
4879 | ||
ff2ad0f7 | 4880 | /* Remove all the redundant expressions. */ |
b82ef848 | 4881 | todo |= eliminate (true); |
0fc6c492 | 4882 | |
be55bfe6 TS |
4883 | statistics_counter_event (fun, "Insertions", pre_stats.insertions); |
4884 | statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert); | |
4885 | statistics_counter_event (fun, "New PHIs", pre_stats.phis); | |
4886 | statistics_counter_event (fun, "Eliminated", pre_stats.eliminations); | |
c4ab2baa | 4887 | |
b71b4522 | 4888 | clear_expression_ids (); |
40b178f4 | 4889 | remove_dead_inserted_code (); |
c90186eb | 4890 | |
a8338640 | 4891 | scev_finalize (); |
40b178f4 | 4892 | fini_pre (); |
9c370032 | 4893 | todo |= fini_eliminate (); |
40b178f4 RG |
4894 | loop_optimizer_finalize (); |
4895 | ||
4896 | /* TODO: tail_merge_optimize may merge all predecessors of a block, in which | |
4897 | case we can merge the block with the remaining predecessor of the block. | |
4898 | It should either: | |
4899 | - call merge_blocks after each tail merge iteration | |
4900 | - call merge_blocks after all tail merge iterations | |
4901 | - mark TODO_cleanup_cfg when necessary | |
4902 | - share the cfg cleanup with fini_pre. */ | |
4903 | todo |= tail_merge_optimize (todo); | |
4904 | ||
c9e93168 TV |
4905 | free_scc_vn (); |
4906 | ||
678771ad RG |
4907 | /* Tail merging invalidates the virtual SSA web, together with |
4908 | cfg-cleanup opportunities exposed by PRE this will wreck the | |
4909 | SSA updating machinery. So make sure to run update-ssa | |
4910 | manually, before eventually scheduling cfg-cleanup as part of | |
4911 | the todo. */ | |
4912 | update_ssa (TODO_update_ssa_only_virtuals); | |
4913 | ||
b80280f2 | 4914 | return todo; |
ff2ad0f7 DN |
4915 | } |
4916 | ||
27a4cd48 DM |
4917 | } // anon namespace |
4918 | ||
4919 | gimple_opt_pass * | |
4920 | make_pass_pre (gcc::context *ctxt) | |
4921 | { | |
4922 | return new pass_pre (ctxt); | |
4923 | } | |
4924 | ||
27a4cd48 DM |
4925 | namespace { |
4926 | ||
4927 | const pass_data pass_data_fre = | |
ff2ad0f7 | 4928 | { |
27a4cd48 DM |
4929 | GIMPLE_PASS, /* type */ |
4930 | "fre", /* name */ | |
4931 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
4932 | TV_TREE_FRE, /* tv_id */ |
4933 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
4934 | 0, /* properties_provided */ | |
4935 | 0, /* properties_destroyed */ | |
4936 | 0, /* todo_flags_start */ | |
3bea341f | 4937 | 0, /* todo_flags_finish */ |
ff2ad0f7 | 4938 | }; |
27a4cd48 DM |
4939 | |
4940 | class pass_fre : public gimple_opt_pass | |
4941 | { | |
4942 | public: | |
c3284718 RS |
4943 | pass_fre (gcc::context *ctxt) |
4944 | : gimple_opt_pass (pass_data_fre, ctxt) | |
27a4cd48 DM |
4945 | {} |
4946 | ||
4947 | /* opt_pass methods: */ | |
65d3284b | 4948 | opt_pass * clone () { return new pass_fre (m_ctxt); } |
1a3d085c | 4949 | virtual bool gate (function *) { return flag_tree_fre != 0; } |
be55bfe6 | 4950 | virtual unsigned int execute (function *); |
27a4cd48 DM |
4951 | |
4952 | }; // class pass_fre | |
4953 | ||
be55bfe6 TS |
4954 | unsigned int |
4955 | pass_fre::execute (function *fun) | |
4956 | { | |
4957 | unsigned int todo = 0; | |
4958 | ||
4959 | if (!run_scc_vn (VN_WALKREWRITE)) | |
4960 | return 0; | |
4961 | ||
4962 | memset (&pre_stats, 0, sizeof (pre_stats)); | |
4963 | ||
4964 | /* Remove all the redundant expressions. */ | |
b82ef848 | 4965 | todo |= eliminate (false); |
be55bfe6 TS |
4966 | |
4967 | todo |= fini_eliminate (); | |
4968 | ||
4969 | free_scc_vn (); | |
4970 | ||
4971 | statistics_counter_event (fun, "Insertions", pre_stats.insertions); | |
4972 | statistics_counter_event (fun, "Eliminated", pre_stats.eliminations); | |
4973 | ||
4974 | return todo; | |
4975 | } | |
4976 | ||
27a4cd48 DM |
4977 | } // anon namespace |
4978 | ||
4979 | gimple_opt_pass * | |
4980 | make_pass_fre (gcc::context *ctxt) | |
4981 | { | |
4982 | return new pass_fre (ctxt); | |
4983 | } |