]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* SSA Dominator optimizations for trees |
c75c517d | 2 | Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
2090d6a0 | 3 | Free Software Foundation, Inc. |
6de9cd9a DN |
4 | Contributed by Diego Novillo <dnovillo@redhat.com> |
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/>. */ | |
6de9cd9a DN |
21 | |
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
25 | #include "tm.h" | |
26 | #include "tree.h" | |
27 | #include "flags.h" | |
6de9cd9a | 28 | #include "tm_p.h" |
6de9cd9a | 29 | #include "basic-block.h" |
d38ffc55 | 30 | #include "cfgloop.h" |
6de9cd9a | 31 | #include "output.h" |
6de9cd9a | 32 | #include "function.h" |
cf835838 JM |
33 | #include "tree-pretty-print.h" |
34 | #include "gimple-pretty-print.h" | |
6de9cd9a DN |
35 | #include "timevar.h" |
36 | #include "tree-dump.h" | |
37 | #include "tree-flow.h" | |
38 | #include "domwalk.h" | |
6de9cd9a | 39 | #include "tree-pass.h" |
c7f90219 | 40 | #include "tree-ssa-propagate.h" |
6de9cd9a | 41 | #include "langhooks.h" |
43f31be5 | 42 | #include "params.h" |
6de9cd9a DN |
43 | |
44 | /* This file implements optimizations on the dominator tree. */ | |
45 | ||
726a989a RB |
46 | /* Representation of a "naked" right-hand-side expression, to be used |
47 | in recording available expressions in the expression hash table. */ | |
48 | ||
49 | enum expr_kind | |
50 | { | |
51 | EXPR_SINGLE, | |
52 | EXPR_UNARY, | |
53 | EXPR_BINARY, | |
0354c0c7 | 54 | EXPR_TERNARY, |
726a989a RB |
55 | EXPR_CALL |
56 | }; | |
57 | ||
58 | struct hashable_expr | |
59 | { | |
60 | tree type; | |
61 | enum expr_kind kind; | |
62 | union { | |
63 | struct { tree rhs; } single; | |
64 | struct { enum tree_code op; tree opnd; } unary; | |
0354c0c7 BS |
65 | struct { enum tree_code op; tree opnd0, opnd1; } binary; |
66 | struct { enum tree_code op; tree opnd0, opnd1, opnd2; } ternary; | |
726a989a RB |
67 | struct { tree fn; bool pure; size_t nargs; tree *args; } call; |
68 | } ops; | |
69 | }; | |
70 | ||
71 | /* Structure for recording known values of a conditional expression | |
72 | at the exits from its block. */ | |
73 | ||
74 | struct cond_equivalence | |
75 | { | |
76 | struct hashable_expr cond; | |
77 | tree value; | |
78 | }; | |
efea75f9 JL |
79 | |
80 | /* Structure for recording edge equivalences as well as any pending | |
81 | edge redirections during the dominator optimizer. | |
82 | ||
83 | Computing and storing the edge equivalences instead of creating | |
84 | them on-demand can save significant amounts of time, particularly | |
b8698a0f | 85 | for pathological cases involving switch statements. |
efea75f9 JL |
86 | |
87 | These structures live for a single iteration of the dominator | |
88 | optimizer in the edge's AUX field. At the end of an iteration we | |
89 | free each of these structures and update the AUX field to point | |
90 | to any requested redirection target (the code for updating the | |
91 | CFG and SSA graph for edge redirection expects redirection edge | |
92 | targets to be in the AUX field for each edge. */ | |
93 | ||
94 | struct edge_info | |
95 | { | |
96 | /* If this edge creates a simple equivalence, the LHS and RHS of | |
97 | the equivalence will be stored here. */ | |
98 | tree lhs; | |
99 | tree rhs; | |
100 | ||
101 | /* Traversing an edge may also indicate one or more particular conditions | |
102 | are true or false. The number of recorded conditions can vary, but | |
103 | can be determined by the condition's code. So we have an array | |
104 | and its maximum index rather than use a varray. */ | |
726a989a | 105 | struct cond_equivalence *cond_equivalences; |
efea75f9 | 106 | unsigned int max_cond_equivalences; |
efea75f9 JL |
107 | }; |
108 | ||
6de9cd9a DN |
109 | /* Hash table with expressions made available during the renaming process. |
110 | When an assignment of the form X_i = EXPR is found, the statement is | |
111 | stored in this table. If the same expression EXPR is later found on the | |
112 | RHS of another statement, it is replaced with X_i (thus performing | |
113 | global redundancy elimination). Similarly as we pass through conditionals | |
114 | we record the conditional itself as having either a true or false value | |
115 | in this table. */ | |
116 | static htab_t avail_exprs; | |
117 | ||
48732f23 JL |
118 | /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any |
119 | expressions it enters into the hash table along with a marker entry | |
b3a27618 | 120 | (null). When we finish processing the block, we pop off entries and |
48732f23 JL |
121 | remove the expressions from the global hash table until we hit the |
122 | marker. */ | |
726a989a RB |
123 | typedef struct expr_hash_elt * expr_hash_elt_t; |
124 | DEF_VEC_P(expr_hash_elt_t); | |
125 | DEF_VEC_ALLOC_P(expr_hash_elt_t,heap); | |
126 | ||
127 | static VEC(expr_hash_elt_t,heap) *avail_exprs_stack; | |
48732f23 | 128 | |
726a989a | 129 | /* Structure for entries in the expression hash table. */ |
56b043c8 | 130 | |
6de9cd9a DN |
131 | struct expr_hash_elt |
132 | { | |
133 | /* The value (lhs) of this expression. */ | |
134 | tree lhs; | |
135 | ||
136 | /* The expression (rhs) we want to record. */ | |
726a989a | 137 | struct hashable_expr expr; |
6de9cd9a | 138 | |
f47c96aa | 139 | /* The stmt pointer if this element corresponds to a statement. */ |
726a989a | 140 | gimple stmt; |
6de9cd9a | 141 | |
726a989a | 142 | /* The hash value for RHS. */ |
6de9cd9a | 143 | hashval_t hash; |
726a989a RB |
144 | |
145 | /* A unique stamp, typically the address of the hash | |
146 | element itself, used in removing entries from the table. */ | |
147 | struct expr_hash_elt *stamp; | |
6de9cd9a DN |
148 | }; |
149 | ||
b5fefcf6 JL |
150 | /* Stack of dest,src pairs that need to be restored during finalization. |
151 | ||
152 | A NULL entry is used to mark the end of pairs which need to be | |
153 | restored during finalization of this block. */ | |
d4e6fecb | 154 | static VEC(tree,heap) *const_and_copies_stack; |
b5fefcf6 | 155 | |
6de9cd9a DN |
156 | /* Track whether or not we have changed the control flow graph. */ |
157 | static bool cfg_altered; | |
158 | ||
1eaba2f2 | 159 | /* Bitmap of blocks that have had EH statements cleaned. We should |
f6fe65dc | 160 | remove their dead edges eventually. */ |
1eaba2f2 RH |
161 | static bitmap need_eh_cleanup; |
162 | ||
6de9cd9a DN |
163 | /* Statistics for dominator optimizations. */ |
164 | struct opt_stats_d | |
165 | { | |
166 | long num_stmts; | |
167 | long num_exprs_considered; | |
168 | long num_re; | |
0bca51f0 DN |
169 | long num_const_prop; |
170 | long num_copy_prop; | |
6de9cd9a DN |
171 | }; |
172 | ||
23530866 JL |
173 | static struct opt_stats_d opt_stats; |
174 | ||
6de9cd9a | 175 | /* Local functions. */ |
ccf5c864 | 176 | static void optimize_stmt (basic_block, gimple_stmt_iterator); |
726a989a | 177 | static tree lookup_avail_expr (gimple, bool); |
6de9cd9a | 178 | static hashval_t avail_expr_hash (const void *); |
940db2c8 | 179 | static hashval_t real_avail_expr_hash (const void *); |
6de9cd9a DN |
180 | static int avail_expr_eq (const void *, const void *); |
181 | static void htab_statistics (FILE *, htab_t); | |
726a989a | 182 | static void record_cond (struct cond_equivalence *); |
b5fefcf6 JL |
183 | static void record_const_or_copy (tree, tree); |
184 | static void record_equality (tree, tree); | |
efea75f9 JL |
185 | static void record_equivalences_from_phis (basic_block); |
186 | static void record_equivalences_from_incoming_edge (basic_block); | |
87c93592 | 187 | static void eliminate_redundant_computations (gimple_stmt_iterator *); |
726a989a | 188 | static void record_equivalences_from_stmt (gimple, int); |
2090d6a0 | 189 | static void dom_thread_across_edge (struct dom_walk_data *, edge); |
ccf5c864 PB |
190 | static void dom_opt_leave_block (struct dom_walk_data *, basic_block); |
191 | static void dom_opt_enter_block (struct dom_walk_data *, basic_block); | |
48732f23 | 192 | static void remove_local_expressions_from_table (void); |
b5fefcf6 | 193 | static void restore_vars_to_original_value (void); |
28c008bb | 194 | static edge single_incoming_edge_ignoring_loop_edges (basic_block); |
6de9cd9a | 195 | |
0bca51f0 | 196 | |
726a989a RB |
197 | /* Given a statement STMT, initialize the hash table element pointed to |
198 | by ELEMENT. */ | |
199 | ||
200 | static void | |
201 | initialize_hash_element (gimple stmt, tree lhs, | |
202 | struct expr_hash_elt *element) | |
203 | { | |
204 | enum gimple_code code = gimple_code (stmt); | |
205 | struct hashable_expr *expr = &element->expr; | |
206 | ||
207 | if (code == GIMPLE_ASSIGN) | |
208 | { | |
209 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
210 | ||
211 | expr->type = NULL_TREE; | |
b8698a0f | 212 | |
726a989a RB |
213 | switch (get_gimple_rhs_class (subcode)) |
214 | { | |
215 | case GIMPLE_SINGLE_RHS: | |
0354c0c7 BS |
216 | expr->kind = EXPR_SINGLE; |
217 | expr->ops.single.rhs = gimple_assign_rhs1 (stmt); | |
218 | break; | |
726a989a | 219 | case GIMPLE_UNARY_RHS: |
0354c0c7 | 220 | expr->kind = EXPR_UNARY; |
726a989a | 221 | expr->type = TREE_TYPE (gimple_assign_lhs (stmt)); |
0354c0c7 BS |
222 | expr->ops.unary.op = subcode; |
223 | expr->ops.unary.opnd = gimple_assign_rhs1 (stmt); | |
224 | break; | |
726a989a | 225 | case GIMPLE_BINARY_RHS: |
0354c0c7 | 226 | expr->kind = EXPR_BINARY; |
726a989a | 227 | expr->type = TREE_TYPE (gimple_assign_lhs (stmt)); |
0354c0c7 BS |
228 | expr->ops.binary.op = subcode; |
229 | expr->ops.binary.opnd0 = gimple_assign_rhs1 (stmt); | |
230 | expr->ops.binary.opnd1 = gimple_assign_rhs2 (stmt); | |
231 | break; | |
232 | case GIMPLE_TERNARY_RHS: | |
233 | expr->kind = EXPR_TERNARY; | |
234 | expr->type = TREE_TYPE (gimple_assign_lhs (stmt)); | |
235 | expr->ops.ternary.op = subcode; | |
236 | expr->ops.ternary.opnd0 = gimple_assign_rhs1 (stmt); | |
237 | expr->ops.ternary.opnd1 = gimple_assign_rhs2 (stmt); | |
238 | expr->ops.ternary.opnd2 = gimple_assign_rhs3 (stmt); | |
239 | break; | |
726a989a RB |
240 | default: |
241 | gcc_unreachable (); | |
242 | } | |
243 | } | |
244 | else if (code == GIMPLE_COND) | |
245 | { | |
246 | expr->type = boolean_type_node; | |
247 | expr->kind = EXPR_BINARY; | |
248 | expr->ops.binary.op = gimple_cond_code (stmt); | |
249 | expr->ops.binary.opnd0 = gimple_cond_lhs (stmt); | |
250 | expr->ops.binary.opnd1 = gimple_cond_rhs (stmt); | |
251 | } | |
252 | else if (code == GIMPLE_CALL) | |
253 | { | |
254 | size_t nargs = gimple_call_num_args (stmt); | |
255 | size_t i; | |
256 | ||
257 | gcc_assert (gimple_call_lhs (stmt)); | |
258 | ||
259 | expr->type = TREE_TYPE (gimple_call_lhs (stmt)); | |
260 | expr->kind = EXPR_CALL; | |
261 | expr->ops.call.fn = gimple_call_fn (stmt); | |
262 | ||
263 | if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)) | |
264 | expr->ops.call.pure = true; | |
b8698a0f | 265 | else |
726a989a RB |
266 | expr->ops.call.pure = false; |
267 | ||
268 | expr->ops.call.nargs = nargs; | |
269 | expr->ops.call.args = (tree *) xcalloc (nargs, sizeof (tree)); | |
270 | for (i = 0; i < nargs; i++) | |
271 | expr->ops.call.args[i] = gimple_call_arg (stmt, i); | |
272 | } | |
273 | else if (code == GIMPLE_SWITCH) | |
274 | { | |
275 | expr->type = TREE_TYPE (gimple_switch_index (stmt)); | |
276 | expr->kind = EXPR_SINGLE; | |
277 | expr->ops.single.rhs = gimple_switch_index (stmt); | |
278 | } | |
279 | else if (code == GIMPLE_GOTO) | |
280 | { | |
281 | expr->type = TREE_TYPE (gimple_goto_dest (stmt)); | |
282 | expr->kind = EXPR_SINGLE; | |
283 | expr->ops.single.rhs = gimple_goto_dest (stmt); | |
284 | } | |
285 | else | |
286 | gcc_unreachable (); | |
287 | ||
288 | element->lhs = lhs; | |
289 | element->stmt = stmt; | |
290 | element->hash = avail_expr_hash (element); | |
291 | element->stamp = element; | |
292 | } | |
293 | ||
294 | /* Given a conditional expression COND as a tree, initialize | |
295 | a hashable_expr expression EXPR. The conditional must be a | |
296 | comparison or logical negation. A constant or a variable is | |
297 | not permitted. */ | |
298 | ||
299 | static void | |
300 | initialize_expr_from_cond (tree cond, struct hashable_expr *expr) | |
301 | { | |
302 | expr->type = boolean_type_node; | |
b8698a0f | 303 | |
726a989a RB |
304 | if (COMPARISON_CLASS_P (cond)) |
305 | { | |
306 | expr->kind = EXPR_BINARY; | |
307 | expr->ops.binary.op = TREE_CODE (cond); | |
308 | expr->ops.binary.opnd0 = TREE_OPERAND (cond, 0); | |
309 | expr->ops.binary.opnd1 = TREE_OPERAND (cond, 1); | |
310 | } | |
311 | else if (TREE_CODE (cond) == TRUTH_NOT_EXPR) | |
312 | { | |
313 | expr->kind = EXPR_UNARY; | |
314 | expr->ops.unary.op = TRUTH_NOT_EXPR; | |
315 | expr->ops.unary.opnd = TREE_OPERAND (cond, 0); | |
316 | } | |
317 | else | |
318 | gcc_unreachable (); | |
319 | } | |
320 | ||
321 | /* Given a hashable_expr expression EXPR and an LHS, | |
322 | initialize the hash table element pointed to by ELEMENT. */ | |
323 | ||
324 | static void | |
325 | initialize_hash_element_from_expr (struct hashable_expr *expr, | |
326 | tree lhs, | |
327 | struct expr_hash_elt *element) | |
328 | { | |
329 | element->expr = *expr; | |
330 | element->lhs = lhs; | |
331 | element->stmt = NULL; | |
332 | element->hash = avail_expr_hash (element); | |
333 | element->stamp = element; | |
334 | } | |
335 | ||
336 | /* Compare two hashable_expr structures for equivalence. | |
337 | They are considered equivalent when the the expressions | |
338 | they denote must necessarily be equal. The logic is intended | |
339 | to follow that of operand_equal_p in fold-const.c */ | |
340 | ||
341 | static bool | |
342 | hashable_expr_equal_p (const struct hashable_expr *expr0, | |
343 | const struct hashable_expr *expr1) | |
344 | { | |
345 | tree type0 = expr0->type; | |
346 | tree type1 = expr1->type; | |
347 | ||
348 | /* If either type is NULL, there is nothing to check. */ | |
349 | if ((type0 == NULL_TREE) ^ (type1 == NULL_TREE)) | |
350 | return false; | |
351 | ||
352 | /* If both types don't have the same signedness, precision, and mode, | |
353 | then we can't consider them equal. */ | |
354 | if (type0 != type1 | |
355 | && (TREE_CODE (type0) == ERROR_MARK | |
356 | || TREE_CODE (type1) == ERROR_MARK | |
357 | || TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1) | |
358 | || TYPE_PRECISION (type0) != TYPE_PRECISION (type1) | |
359 | || TYPE_MODE (type0) != TYPE_MODE (type1))) | |
360 | return false; | |
361 | ||
362 | if (expr0->kind != expr1->kind) | |
363 | return false; | |
364 | ||
365 | switch (expr0->kind) | |
366 | { | |
367 | case EXPR_SINGLE: | |
368 | return operand_equal_p (expr0->ops.single.rhs, | |
369 | expr1->ops.single.rhs, 0); | |
370 | ||
371 | case EXPR_UNARY: | |
372 | if (expr0->ops.unary.op != expr1->ops.unary.op) | |
373 | return false; | |
374 | ||
1a87cf0c | 375 | if ((CONVERT_EXPR_CODE_P (expr0->ops.unary.op) |
726a989a RB |
376 | || expr0->ops.unary.op == NON_LVALUE_EXPR) |
377 | && TYPE_UNSIGNED (expr0->type) != TYPE_UNSIGNED (expr1->type)) | |
378 | return false; | |
379 | ||
380 | return operand_equal_p (expr0->ops.unary.opnd, | |
381 | expr1->ops.unary.opnd, 0); | |
382 | ||
383 | case EXPR_BINARY: | |
0354c0c7 BS |
384 | if (expr0->ops.binary.op != expr1->ops.binary.op) |
385 | return false; | |
386 | ||
387 | if (operand_equal_p (expr0->ops.binary.opnd0, | |
388 | expr1->ops.binary.opnd0, 0) | |
389 | && operand_equal_p (expr0->ops.binary.opnd1, | |
390 | expr1->ops.binary.opnd1, 0)) | |
391 | return true; | |
392 | ||
393 | /* For commutative ops, allow the other order. */ | |
394 | return (commutative_tree_code (expr0->ops.binary.op) | |
395 | && operand_equal_p (expr0->ops.binary.opnd0, | |
396 | expr1->ops.binary.opnd1, 0) | |
397 | && operand_equal_p (expr0->ops.binary.opnd1, | |
398 | expr1->ops.binary.opnd0, 0)); | |
399 | ||
400 | case EXPR_TERNARY: | |
401 | if (expr0->ops.ternary.op != expr1->ops.ternary.op | |
402 | || !operand_equal_p (expr0->ops.ternary.opnd2, | |
403 | expr1->ops.ternary.opnd2, 0)) | |
404 | return false; | |
405 | ||
406 | if (operand_equal_p (expr0->ops.ternary.opnd0, | |
407 | expr1->ops.ternary.opnd0, 0) | |
408 | && operand_equal_p (expr0->ops.ternary.opnd1, | |
409 | expr1->ops.ternary.opnd1, 0)) | |
410 | return true; | |
411 | ||
412 | /* For commutative ops, allow the other order. */ | |
413 | return (commutative_ternary_tree_code (expr0->ops.ternary.op) | |
414 | && operand_equal_p (expr0->ops.ternary.opnd0, | |
415 | expr1->ops.ternary.opnd1, 0) | |
416 | && operand_equal_p (expr0->ops.ternary.opnd1, | |
417 | expr1->ops.ternary.opnd0, 0)); | |
726a989a RB |
418 | |
419 | case EXPR_CALL: | |
420 | { | |
421 | size_t i; | |
422 | ||
423 | /* If the calls are to different functions, then they | |
424 | clearly cannot be equal. */ | |
425 | if (! operand_equal_p (expr0->ops.call.fn, | |
426 | expr1->ops.call.fn, 0)) | |
427 | return false; | |
428 | ||
429 | if (! expr0->ops.call.pure) | |
430 | return false; | |
431 | ||
432 | if (expr0->ops.call.nargs != expr1->ops.call.nargs) | |
433 | return false; | |
434 | ||
435 | for (i = 0; i < expr0->ops.call.nargs; i++) | |
436 | if (! operand_equal_p (expr0->ops.call.args[i], | |
437 | expr1->ops.call.args[i], 0)) | |
438 | return false; | |
439 | ||
440 | return true; | |
441 | } | |
b8698a0f | 442 | |
726a989a RB |
443 | default: |
444 | gcc_unreachable (); | |
445 | } | |
446 | } | |
447 | ||
448 | /* Compute a hash value for a hashable_expr value EXPR and a | |
449 | previously accumulated hash value VAL. If two hashable_expr | |
450 | values compare equal with hashable_expr_equal_p, they must | |
451 | hash to the same value, given an identical value of VAL. | |
452 | The logic is intended to follow iterative_hash_expr in tree.c. */ | |
453 | ||
454 | static hashval_t | |
455 | iterative_hash_hashable_expr (const struct hashable_expr *expr, hashval_t val) | |
456 | { | |
457 | switch (expr->kind) | |
458 | { | |
459 | case EXPR_SINGLE: | |
460 | val = iterative_hash_expr (expr->ops.single.rhs, val); | |
461 | break; | |
462 | ||
463 | case EXPR_UNARY: | |
464 | val = iterative_hash_object (expr->ops.unary.op, val); | |
465 | ||
466 | /* Make sure to include signedness in the hash computation. | |
467 | Don't hash the type, that can lead to having nodes which | |
468 | compare equal according to operand_equal_p, but which | |
469 | have different hash codes. */ | |
1a87cf0c | 470 | if (CONVERT_EXPR_CODE_P (expr->ops.unary.op) |
726a989a RB |
471 | || expr->ops.unary.op == NON_LVALUE_EXPR) |
472 | val += TYPE_UNSIGNED (expr->type); | |
473 | ||
474 | val = iterative_hash_expr (expr->ops.unary.opnd, val); | |
475 | break; | |
476 | ||
477 | case EXPR_BINARY: | |
478 | val = iterative_hash_object (expr->ops.binary.op, val); | |
479 | if (commutative_tree_code (expr->ops.binary.op)) | |
0354c0c7 BS |
480 | val = iterative_hash_exprs_commutative (expr->ops.binary.opnd0, |
481 | expr->ops.binary.opnd1, val); | |
726a989a RB |
482 | else |
483 | { | |
484 | val = iterative_hash_expr (expr->ops.binary.opnd0, val); | |
485 | val = iterative_hash_expr (expr->ops.binary.opnd1, val); | |
486 | } | |
487 | break; | |
488 | ||
0354c0c7 BS |
489 | case EXPR_TERNARY: |
490 | val = iterative_hash_object (expr->ops.ternary.op, val); | |
491 | if (commutative_ternary_tree_code (expr->ops.ternary.op)) | |
492 | val = iterative_hash_exprs_commutative (expr->ops.ternary.opnd0, | |
493 | expr->ops.ternary.opnd1, val); | |
494 | else | |
495 | { | |
496 | val = iterative_hash_expr (expr->ops.ternary.opnd0, val); | |
497 | val = iterative_hash_expr (expr->ops.ternary.opnd1, val); | |
498 | } | |
499 | val = iterative_hash_expr (expr->ops.ternary.opnd2, val); | |
500 | break; | |
501 | ||
726a989a RB |
502 | case EXPR_CALL: |
503 | { | |
504 | size_t i; | |
505 | enum tree_code code = CALL_EXPR; | |
506 | ||
507 | val = iterative_hash_object (code, val); | |
508 | val = iterative_hash_expr (expr->ops.call.fn, val); | |
509 | for (i = 0; i < expr->ops.call.nargs; i++) | |
510 | val = iterative_hash_expr (expr->ops.call.args[i], val); | |
511 | } | |
512 | break; | |
b8698a0f | 513 | |
726a989a RB |
514 | default: |
515 | gcc_unreachable (); | |
516 | } | |
517 | ||
518 | return val; | |
519 | } | |
520 | ||
521 | /* Print a diagnostic dump of an expression hash table entry. */ | |
522 | ||
523 | static void | |
524 | print_expr_hash_elt (FILE * stream, const struct expr_hash_elt *element) | |
525 | { | |
526 | if (element->stmt) | |
527 | fprintf (stream, "STMT "); | |
528 | else | |
529 | fprintf (stream, "COND "); | |
530 | ||
531 | if (element->lhs) | |
532 | { | |
533 | print_generic_expr (stream, element->lhs, 0); | |
534 | fprintf (stream, " = "); | |
535 | } | |
b8698a0f | 536 | |
726a989a RB |
537 | switch (element->expr.kind) |
538 | { | |
539 | case EXPR_SINGLE: | |
540 | print_generic_expr (stream, element->expr.ops.single.rhs, 0); | |
541 | break; | |
542 | ||
543 | case EXPR_UNARY: | |
544 | fprintf (stream, "%s ", tree_code_name[element->expr.ops.unary.op]); | |
545 | print_generic_expr (stream, element->expr.ops.unary.opnd, 0); | |
546 | break; | |
547 | ||
548 | case EXPR_BINARY: | |
549 | print_generic_expr (stream, element->expr.ops.binary.opnd0, 0); | |
550 | fprintf (stream, " %s ", tree_code_name[element->expr.ops.binary.op]); | |
551 | print_generic_expr (stream, element->expr.ops.binary.opnd1, 0); | |
552 | break; | |
553 | ||
0354c0c7 BS |
554 | case EXPR_TERNARY: |
555 | fprintf (stream, " %s <", tree_code_name[element->expr.ops.ternary.op]); | |
556 | print_generic_expr (stream, element->expr.ops.ternary.opnd0, 0); | |
557 | fputs (", ", stream); | |
558 | print_generic_expr (stream, element->expr.ops.ternary.opnd1, 0); | |
559 | fputs (", ", stream); | |
560 | print_generic_expr (stream, element->expr.ops.ternary.opnd2, 0); | |
561 | fputs (">", stream); | |
562 | break; | |
563 | ||
726a989a RB |
564 | case EXPR_CALL: |
565 | { | |
566 | size_t i; | |
567 | size_t nargs = element->expr.ops.call.nargs; | |
568 | ||
569 | print_generic_expr (stream, element->expr.ops.call.fn, 0); | |
570 | fprintf (stream, " ("); | |
571 | for (i = 0; i < nargs; i++) | |
572 | { | |
573 | print_generic_expr (stream, element->expr.ops.call.args[i], 0); | |
574 | if (i + 1 < nargs) | |
575 | fprintf (stream, ", "); | |
576 | } | |
577 | fprintf (stream, ")"); | |
578 | } | |
579 | break; | |
580 | } | |
581 | fprintf (stream, "\n"); | |
582 | ||
583 | if (element->stmt) | |
584 | { | |
585 | fprintf (stream, " "); | |
586 | print_gimple_stmt (stream, element->stmt, 0, 0); | |
587 | } | |
588 | } | |
589 | ||
590 | /* Delete an expr_hash_elt and reclaim its storage. */ | |
591 | ||
592 | static void | |
593 | free_expr_hash_elt (void *elt) | |
594 | { | |
595 | struct expr_hash_elt *element = ((struct expr_hash_elt *)elt); | |
596 | ||
597 | if (element->expr.kind == EXPR_CALL) | |
598 | free (element->expr.ops.call.args); | |
599 | ||
600 | free (element); | |
601 | } | |
602 | ||
efea75f9 JL |
603 | /* Allocate an EDGE_INFO for edge E and attach it to E. |
604 | Return the new EDGE_INFO structure. */ | |
605 | ||
606 | static struct edge_info * | |
607 | allocate_edge_info (edge e) | |
608 | { | |
609 | struct edge_info *edge_info; | |
610 | ||
e1111e8e | 611 | edge_info = XCNEW (struct edge_info); |
efea75f9 JL |
612 | |
613 | e->aux = edge_info; | |
614 | return edge_info; | |
615 | } | |
616 | ||
617 | /* Free all EDGE_INFO structures associated with edges in the CFG. | |
cbb1cada | 618 | If a particular edge can be threaded, copy the redirection |
efea75f9 JL |
619 | target from the EDGE_INFO structure into the edge's AUX field |
620 | as required by code to update the CFG and SSA graph for | |
621 | jump threading. */ | |
622 | ||
623 | static void | |
624 | free_all_edge_infos (void) | |
625 | { | |
626 | basic_block bb; | |
627 | edge_iterator ei; | |
628 | edge e; | |
629 | ||
630 | FOR_EACH_BB (bb) | |
631 | { | |
632 | FOR_EACH_EDGE (e, ei, bb->preds) | |
633 | { | |
e1111e8e | 634 | struct edge_info *edge_info = (struct edge_info *) e->aux; |
efea75f9 JL |
635 | |
636 | if (edge_info) | |
637 | { | |
efea75f9 JL |
638 | if (edge_info->cond_equivalences) |
639 | free (edge_info->cond_equivalences); | |
640 | free (edge_info); | |
8702a557 | 641 | e->aux = NULL; |
efea75f9 JL |
642 | } |
643 | } | |
644 | } | |
645 | } | |
646 | ||
b8698a0f | 647 | /* Jump threading, redundancy elimination and const/copy propagation. |
6de9cd9a | 648 | |
6de9cd9a DN |
649 | This pass may expose new symbols that need to be renamed into SSA. For |
650 | every new symbol exposed, its corresponding bit will be set in | |
ff2ad0f7 | 651 | VARS_TO_RENAME. */ |
6de9cd9a | 652 | |
c2924966 | 653 | static unsigned int |
6de9cd9a DN |
654 | tree_ssa_dominator_optimize (void) |
655 | { | |
6de9cd9a | 656 | struct dom_walk_data walk_data; |
6de9cd9a | 657 | |
fded8de7 DN |
658 | memset (&opt_stats, 0, sizeof (opt_stats)); |
659 | ||
6de9cd9a | 660 | /* Create our hash tables. */ |
726a989a RB |
661 | avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free_expr_hash_elt); |
662 | avail_exprs_stack = VEC_alloc (expr_hash_elt_t, heap, 20); | |
d4e6fecb | 663 | const_and_copies_stack = VEC_alloc (tree, heap, 20); |
8bdbfff5 | 664 | need_eh_cleanup = BITMAP_ALLOC (NULL); |
6de9cd9a DN |
665 | |
666 | /* Setup callbacks for the generic dominator tree walker. */ | |
6de9cd9a | 667 | walk_data.dom_direction = CDI_DOMINATORS; |
fdabe5c2 | 668 | walk_data.initialize_block_local_data = NULL; |
ccf5c864 PB |
669 | walk_data.before_dom_children = dom_opt_enter_block; |
670 | walk_data.after_dom_children = dom_opt_leave_block; | |
6de9cd9a DN |
671 | /* Right now we only attach a dummy COND_EXPR to the global data pointer. |
672 | When we attach more stuff we'll need to fill this out with a real | |
673 | structure. */ | |
674 | walk_data.global_data = NULL; | |
fdabe5c2 | 675 | walk_data.block_local_data_size = 0; |
6de9cd9a DN |
676 | |
677 | /* Now initialize the dominator walker. */ | |
678 | init_walk_dominator_tree (&walk_data); | |
679 | ||
6de9cd9a | 680 | calculate_dominance_info (CDI_DOMINATORS); |
8d9d6561 | 681 | cfg_altered = false; |
6de9cd9a | 682 | |
b02b9b53 ZD |
683 | /* We need to know loop structures in order to avoid destroying them |
684 | in jump threading. Note that we still can e.g. thread through loop | |
685 | headers to an exit edge, or through loop header to the loop body, assuming | |
686 | that we update the loop info. */ | |
687 | loop_optimizer_init (LOOPS_HAVE_SIMPLE_LATCHES); | |
d38ffc55 | 688 | |
448ee662 RG |
689 | /* Initialize the value-handle array. */ |
690 | threadedge_initialize_values (); | |
691 | ||
2090d6a0 | 692 | /* We need accurate information regarding back edges in the CFG |
fa10beec | 693 | for jump threading; this may include back edges that are not part of |
b02b9b53 | 694 | a single loop. */ |
2090d6a0 | 695 | mark_dfs_back_edges (); |
b8698a0f | 696 | |
2090d6a0 JL |
697 | /* Recursively walk the dominator tree optimizing statements. */ |
698 | walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR); | |
6de9cd9a | 699 | |
2090d6a0 | 700 | { |
726a989a | 701 | gimple_stmt_iterator gsi; |
2090d6a0 JL |
702 | basic_block bb; |
703 | FOR_EACH_BB (bb) | |
726a989a RB |
704 | {for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
705 | update_stmt_if_modified (gsi_stmt (gsi)); | |
f430bae8 | 706 | } |
2090d6a0 | 707 | } |
a3b609df | 708 | |
2090d6a0 JL |
709 | /* If we exposed any new variables, go ahead and put them into |
710 | SSA form now, before we handle jump threading. This simplifies | |
711 | interactions between rewriting of _DECL nodes into SSA form | |
712 | and rewriting SSA_NAME nodes into SSA form after block | |
713 | duplication and CFG manipulation. */ | |
714 | update_ssa (TODO_update_ssa); | |
d38ffc55 | 715 | |
2090d6a0 | 716 | free_all_edge_infos (); |
d38ffc55 | 717 | |
2090d6a0 | 718 | /* Thread jumps, creating duplicate blocks as needed. */ |
b02b9b53 | 719 | cfg_altered |= thread_through_all_blocks (first_pass_instance); |
6de9cd9a | 720 | |
8d9d6561 EB |
721 | if (cfg_altered) |
722 | free_dominance_info (CDI_DOMINATORS); | |
723 | ||
2090d6a0 JL |
724 | /* Removal of statements may make some EH edges dead. Purge |
725 | such edges from the CFG as needed. */ | |
726 | if (!bitmap_empty_p (need_eh_cleanup)) | |
727 | { | |
45a7844f EB |
728 | unsigned i; |
729 | bitmap_iterator bi; | |
730 | ||
731 | /* Jump threading may have created forwarder blocks from blocks | |
732 | needing EH cleanup; the new successor of these blocks, which | |
733 | has inherited from the original block, needs the cleanup. */ | |
734 | EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi) | |
735 | { | |
736 | basic_block bb = BASIC_BLOCK (i); | |
737 | if (single_succ_p (bb) == 1 | |
738 | && (single_succ_edge (bb)->flags & EDGE_EH) == 0) | |
739 | { | |
740 | bitmap_clear_bit (need_eh_cleanup, i); | |
741 | bitmap_set_bit (need_eh_cleanup, single_succ (bb)->index); | |
742 | } | |
743 | } | |
744 | ||
726a989a | 745 | gimple_purge_all_dead_eh_edges (need_eh_cleanup); |
2090d6a0 JL |
746 | bitmap_zero (need_eh_cleanup); |
747 | } | |
6de9cd9a | 748 | |
01902653 RG |
749 | statistics_counter_event (cfun, "Redundant expressions eliminated", |
750 | opt_stats.num_re); | |
751 | statistics_counter_event (cfun, "Constants propagated", | |
752 | opt_stats.num_const_prop); | |
753 | statistics_counter_event (cfun, "Copies propagated", | |
754 | opt_stats.num_copy_prop); | |
755 | ||
6de9cd9a DN |
756 | /* Debugging dumps. */ |
757 | if (dump_file && (dump_flags & TDF_STATS)) | |
758 | dump_dominator_optimization_stats (dump_file); | |
759 | ||
b02b9b53 ZD |
760 | loop_optimizer_finalize (); |
761 | ||
2090d6a0 | 762 | /* Delete our main hashtable. */ |
6de9cd9a | 763 | htab_delete (avail_exprs); |
6de9cd9a DN |
764 | |
765 | /* And finalize the dominator walker. */ | |
766 | fini_walk_dominator_tree (&walk_data); | |
cfa4cb00 | 767 | |
b16caf72 | 768 | /* Free asserted bitmaps and stacks. */ |
8bdbfff5 | 769 | BITMAP_FREE (need_eh_cleanup); |
b8698a0f | 770 | |
726a989a | 771 | VEC_free (expr_hash_elt_t, heap, avail_exprs_stack); |
d4e6fecb | 772 | VEC_free (tree, heap, const_and_copies_stack); |
b8698a0f | 773 | |
448ee662 RG |
774 | /* Free the value-handle array. */ |
775 | threadedge_finalize_values (); | |
776 | ssa_name_values = NULL; | |
777 | ||
c2924966 | 778 | return 0; |
6de9cd9a DN |
779 | } |
780 | ||
781 | static bool | |
782 | gate_dominator (void) | |
783 | { | |
784 | return flag_tree_dom != 0; | |
785 | } | |
786 | ||
b8698a0f | 787 | struct gimple_opt_pass pass_dominator = |
6de9cd9a | 788 | { |
8ddbbcae JH |
789 | { |
790 | GIMPLE_PASS, | |
6de9cd9a DN |
791 | "dom", /* name */ |
792 | gate_dominator, /* gate */ | |
793 | tree_ssa_dominator_optimize, /* execute */ | |
794 | NULL, /* sub */ | |
795 | NULL, /* next */ | |
796 | 0, /* static_pass_number */ | |
797 | TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */ | |
4effdf02 | 798 | PROP_cfg | PROP_ssa, /* properties_required */ |
6de9cd9a | 799 | 0, /* properties_provided */ |
ae07b463 | 800 | 0, /* properties_destroyed */ |
6de9cd9a | 801 | 0, /* todo_flags_start */ |
0bca51f0 DN |
802 | TODO_dump_func |
803 | | TODO_update_ssa | |
2090d6a0 | 804 | | TODO_cleanup_cfg |
8ddbbcae JH |
805 | | TODO_verify_ssa /* todo_flags_finish */ |
806 | } | |
6de9cd9a DN |
807 | }; |
808 | ||
809 | ||
726a989a RB |
810 | /* Given a conditional statement CONDSTMT, convert the |
811 | condition to a canonical form. */ | |
0e0ed594 JL |
812 | |
813 | static void | |
726a989a | 814 | canonicalize_comparison (gimple condstmt) |
0e0ed594 | 815 | { |
0e0ed594 JL |
816 | tree op0; |
817 | tree op1; | |
726a989a | 818 | enum tree_code code; |
0e0ed594 | 819 | |
726a989a | 820 | gcc_assert (gimple_code (condstmt) == GIMPLE_COND); |
0e0ed594 | 821 | |
726a989a RB |
822 | op0 = gimple_cond_lhs (condstmt); |
823 | op1 = gimple_cond_rhs (condstmt); | |
824 | ||
825 | code = gimple_cond_code (condstmt); | |
0e0ed594 JL |
826 | |
827 | /* If it would be profitable to swap the operands, then do so to | |
828 | canonicalize the statement, enabling better optimization. | |
829 | ||
830 | By placing canonicalization of such expressions here we | |
831 | transparently keep statements in canonical form, even | |
832 | when the statement is modified. */ | |
833 | if (tree_swap_operands_p (op0, op1, false)) | |
834 | { | |
835 | /* For relationals we need to swap the operands | |
836 | and change the code. */ | |
837 | if (code == LT_EXPR | |
838 | || code == GT_EXPR | |
839 | || code == LE_EXPR | |
840 | || code == GE_EXPR) | |
841 | { | |
726a989a RB |
842 | code = swap_tree_comparison (code); |
843 | ||
844 | gimple_cond_set_code (condstmt, code); | |
845 | gimple_cond_set_lhs (condstmt, op1); | |
846 | gimple_cond_set_rhs (condstmt, op0); | |
847 | ||
848 | update_stmt (condstmt); | |
0e0ed594 JL |
849 | } |
850 | } | |
851 | } | |
6de9cd9a | 852 | |
6de9cd9a DN |
853 | /* Initialize local stacks for this optimizer and record equivalences |
854 | upon entry to BB. Equivalences can come from the edge traversed to | |
855 | reach BB or they may come from PHI nodes at the start of BB. */ | |
856 | ||
6de9cd9a DN |
857 | /* Remove all the expressions in LOCALS from TABLE, stopping when there are |
858 | LIMIT entries left in LOCALs. */ | |
859 | ||
860 | static void | |
48732f23 | 861 | remove_local_expressions_from_table (void) |
6de9cd9a | 862 | { |
6de9cd9a | 863 | /* Remove all the expressions made available in this block. */ |
726a989a | 864 | while (VEC_length (expr_hash_elt_t, avail_exprs_stack) > 0) |
6de9cd9a | 865 | { |
726a989a | 866 | expr_hash_elt_t victim = VEC_pop (expr_hash_elt_t, avail_exprs_stack); |
aabf6a03 | 867 | void **slot; |
48732f23 | 868 | |
726a989a | 869 | if (victim == NULL) |
48732f23 | 870 | break; |
6de9cd9a | 871 | |
726a989a RB |
872 | /* This must precede the actual removal from the hash table, |
873 | as ELEMENT and the table entry may share a call argument | |
874 | vector which will be freed during removal. */ | |
875 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
876 | { | |
877 | fprintf (dump_file, "<<<< "); | |
aabf6a03 | 878 | print_expr_hash_elt (dump_file, victim); |
726a989a RB |
879 | } |
880 | ||
aabf6a03 RG |
881 | slot = htab_find_slot_with_hash (avail_exprs, |
882 | victim, victim->hash, NO_INSERT); | |
883 | gcc_assert (slot && *slot == (void *) victim); | |
884 | htab_clear_slot (avail_exprs, slot); | |
6de9cd9a DN |
885 | } |
886 | } | |
887 | ||
b5fefcf6 JL |
888 | /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore |
889 | CONST_AND_COPIES to its original state, stopping when we hit a | |
890 | NULL marker. */ | |
6de9cd9a DN |
891 | |
892 | static void | |
b5fefcf6 | 893 | restore_vars_to_original_value (void) |
6de9cd9a | 894 | { |
d4e6fecb | 895 | while (VEC_length (tree, const_and_copies_stack) > 0) |
6de9cd9a DN |
896 | { |
897 | tree prev_value, dest; | |
898 | ||
d4e6fecb | 899 | dest = VEC_pop (tree, const_and_copies_stack); |
6de9cd9a | 900 | |
b5fefcf6 JL |
901 | if (dest == NULL) |
902 | break; | |
903 | ||
726a989a RB |
904 | if (dump_file && (dump_flags & TDF_DETAILS)) |
905 | { | |
906 | fprintf (dump_file, "<<<< COPY "); | |
907 | print_generic_expr (dump_file, dest, 0); | |
908 | fprintf (dump_file, " = "); | |
909 | print_generic_expr (dump_file, SSA_NAME_VALUE (dest), 0); | |
910 | fprintf (dump_file, "\n"); | |
911 | } | |
912 | ||
d4e6fecb | 913 | prev_value = VEC_pop (tree, const_and_copies_stack); |
448ee662 | 914 | set_ssa_name_value (dest, prev_value); |
6de9cd9a DN |
915 | } |
916 | } | |
917 | ||
2090d6a0 JL |
918 | /* A trivial wrapper so that we can present the generic jump |
919 | threading code with a simple API for simplifying statements. */ | |
920 | static tree | |
726a989a RB |
921 | simplify_stmt_for_jump_threading (gimple stmt, |
922 | gimple within_stmt ATTRIBUTE_UNUSED) | |
2090d6a0 JL |
923 | { |
924 | return lookup_avail_expr (stmt, false); | |
925 | } | |
926 | ||
927 | /* Wrapper for common code to attempt to thread an edge. For example, | |
928 | it handles lazily building the dummy condition and the bookkeeping | |
929 | when jump threading is successful. */ | |
930 | ||
931 | static void | |
932 | dom_thread_across_edge (struct dom_walk_data *walk_data, edge e) | |
933 | { | |
2090d6a0 | 934 | if (! walk_data->global_data) |
726a989a RB |
935 | { |
936 | gimple dummy_cond = | |
937 | gimple_build_cond (NE_EXPR, | |
938 | integer_zero_node, integer_zero_node, | |
939 | NULL, NULL); | |
940 | walk_data->global_data = dummy_cond; | |
941 | } | |
2090d6a0 | 942 | |
726a989a | 943 | thread_across_edge ((gimple) walk_data->global_data, e, false, |
2090d6a0 JL |
944 | &const_and_copies_stack, |
945 | simplify_stmt_for_jump_threading); | |
946 | } | |
947 | ||
6de9cd9a DN |
948 | /* PHI nodes can create equivalences too. |
949 | ||
950 | Ignoring any alternatives which are the same as the result, if | |
951 | all the alternatives are equal, then the PHI node creates an | |
b16caf72 | 952 | equivalence. */ |
dd747311 | 953 | |
6de9cd9a | 954 | static void |
efea75f9 | 955 | record_equivalences_from_phis (basic_block bb) |
6de9cd9a | 956 | { |
726a989a | 957 | gimple_stmt_iterator gsi; |
b8698a0f | 958 | |
726a989a | 959 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
6de9cd9a | 960 | { |
726a989a RB |
961 | gimple phi = gsi_stmt (gsi); |
962 | ||
963 | tree lhs = gimple_phi_result (phi); | |
6de9cd9a | 964 | tree rhs = NULL; |
726a989a | 965 | size_t i; |
6de9cd9a | 966 | |
726a989a | 967 | for (i = 0; i < gimple_phi_num_args (phi); i++) |
6de9cd9a | 968 | { |
726a989a | 969 | tree t = gimple_phi_arg_def (phi, i); |
6de9cd9a | 970 | |
6e38fea3 KH |
971 | /* Ignore alternatives which are the same as our LHS. Since |
972 | LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we | |
973 | can simply compare pointers. */ | |
073b8140 | 974 | if (lhs == t) |
a18428f3 KH |
975 | continue; |
976 | ||
977 | /* If we have not processed an alternative yet, then set | |
978 | RHS to this alternative. */ | |
979 | if (rhs == NULL) | |
980 | rhs = t; | |
981 | /* If we have processed an alternative (stored in RHS), then | |
982 | see if it is equal to this one. If it isn't, then stop | |
983 | the search. */ | |
984 | else if (! operand_equal_for_phi_arg_p (rhs, t)) | |
6de9cd9a DN |
985 | break; |
986 | } | |
987 | ||
988 | /* If we had no interesting alternatives, then all the RHS alternatives | |
989 | must have been the same as LHS. */ | |
990 | if (!rhs) | |
991 | rhs = lhs; | |
992 | ||
993 | /* If we managed to iterate through each PHI alternative without | |
994 | breaking out of the loop, then we have a PHI which may create | |
995 | a useful equivalence. We do not need to record unwind data for | |
996 | this, since this is a true assignment and not an equivalence | |
1ea7e6ad | 997 | inferred from a comparison. All uses of this ssa name are dominated |
6de9cd9a | 998 | by this assignment, so unwinding just costs time and space. */ |
726a989a | 999 | if (i == gimple_phi_num_args (phi) && may_propagate_copy (lhs, rhs)) |
448ee662 | 1000 | set_ssa_name_value (lhs, rhs); |
6de9cd9a DN |
1001 | } |
1002 | } | |
1003 | ||
28c008bb JL |
1004 | /* Ignoring loop backedges, if BB has precisely one incoming edge then |
1005 | return that edge. Otherwise return NULL. */ | |
1006 | static edge | |
1007 | single_incoming_edge_ignoring_loop_edges (basic_block bb) | |
1008 | { | |
1009 | edge retval = NULL; | |
1010 | edge e; | |
628f6a4e | 1011 | edge_iterator ei; |
28c008bb | 1012 | |
628f6a4e | 1013 | FOR_EACH_EDGE (e, ei, bb->preds) |
28c008bb JL |
1014 | { |
1015 | /* A loop back edge can be identified by the destination of | |
1016 | the edge dominating the source of the edge. */ | |
1017 | if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest)) | |
1018 | continue; | |
1019 | ||
1020 | /* If we have already seen a non-loop edge, then we must have | |
1021 | multiple incoming non-loop edges and thus we return NULL. */ | |
1022 | if (retval) | |
1023 | return NULL; | |
1024 | ||
1025 | /* This is the first non-loop incoming edge we have found. Record | |
1026 | it. */ | |
1027 | retval = e; | |
1028 | } | |
1029 | ||
1030 | return retval; | |
1031 | } | |
1032 | ||
6de9cd9a DN |
1033 | /* Record any equivalences created by the incoming edge to BB. If BB |
1034 | has more than one incoming edge, then no equivalence is created. */ | |
1035 | ||
1036 | static void | |
efea75f9 | 1037 | record_equivalences_from_incoming_edge (basic_block bb) |
6de9cd9a | 1038 | { |
efea75f9 | 1039 | edge e; |
6de9cd9a | 1040 | basic_block parent; |
efea75f9 | 1041 | struct edge_info *edge_info; |
6de9cd9a | 1042 | |
35fd3193 | 1043 | /* If our parent block ended with a control statement, then we may be |
6de9cd9a DN |
1044 | able to record some equivalences based on which outgoing edge from |
1045 | the parent was followed. */ | |
1046 | parent = get_immediate_dominator (CDI_DOMINATORS, bb); | |
6de9cd9a | 1047 | |
efea75f9 | 1048 | e = single_incoming_edge_ignoring_loop_edges (bb); |
6de9cd9a | 1049 | |
efea75f9 JL |
1050 | /* If we had a single incoming edge from our parent block, then enter |
1051 | any data associated with the edge into our tables. */ | |
1052 | if (e && e->src == parent) | |
6de9cd9a | 1053 | { |
efea75f9 | 1054 | unsigned int i; |
6de9cd9a | 1055 | |
e1111e8e | 1056 | edge_info = (struct edge_info *) e->aux; |
6de9cd9a | 1057 | |
efea75f9 | 1058 | if (edge_info) |
6de9cd9a | 1059 | { |
efea75f9 JL |
1060 | tree lhs = edge_info->lhs; |
1061 | tree rhs = edge_info->rhs; | |
726a989a | 1062 | struct cond_equivalence *cond_equivalences = edge_info->cond_equivalences; |
efea75f9 JL |
1063 | |
1064 | if (lhs) | |
1065 | record_equality (lhs, rhs); | |
1066 | ||
1067 | if (cond_equivalences) | |
726a989a RB |
1068 | for (i = 0; i < edge_info->max_cond_equivalences; i++) |
1069 | record_cond (&cond_equivalences[i]); | |
6de9cd9a DN |
1070 | } |
1071 | } | |
6de9cd9a DN |
1072 | } |
1073 | ||
1074 | /* Dump SSA statistics on FILE. */ | |
1075 | ||
1076 | void | |
1077 | dump_dominator_optimization_stats (FILE *file) | |
1078 | { | |
6de9cd9a DN |
1079 | fprintf (file, "Total number of statements: %6ld\n\n", |
1080 | opt_stats.num_stmts); | |
1081 | fprintf (file, "Exprs considered for dominator optimizations: %6ld\n", | |
1082 | opt_stats.num_exprs_considered); | |
1083 | ||
6de9cd9a DN |
1084 | fprintf (file, "\nHash table statistics:\n"); |
1085 | ||
1086 | fprintf (file, " avail_exprs: "); | |
1087 | htab_statistics (file, avail_exprs); | |
1088 | } | |
1089 | ||
1090 | ||
1091 | /* Dump SSA statistics on stderr. */ | |
1092 | ||
24e47c76 | 1093 | DEBUG_FUNCTION void |
6de9cd9a DN |
1094 | debug_dominator_optimization_stats (void) |
1095 | { | |
1096 | dump_dominator_optimization_stats (stderr); | |
1097 | } | |
1098 | ||
1099 | ||
1100 | /* Dump statistics for the hash table HTAB. */ | |
1101 | ||
1102 | static void | |
1103 | htab_statistics (FILE *file, htab_t htab) | |
1104 | { | |
1105 | fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n", | |
1106 | (long) htab_size (htab), | |
1107 | (long) htab_elements (htab), | |
1108 | htab_collisions (htab)); | |
1109 | } | |
1110 | ||
726a989a RB |
1111 | |
1112 | /* Enter condition equivalence into the expression hash table. | |
1113 | This indicates that a conditional expression has a known | |
1114 | boolean value. */ | |
6de9cd9a DN |
1115 | |
1116 | static void | |
726a989a | 1117 | record_cond (struct cond_equivalence *p) |
6de9cd9a | 1118 | { |
e1111e8e | 1119 | struct expr_hash_elt *element = XCNEW (struct expr_hash_elt); |
6de9cd9a DN |
1120 | void **slot; |
1121 | ||
726a989a | 1122 | initialize_hash_element_from_expr (&p->cond, p->value, element); |
6de9cd9a DN |
1123 | |
1124 | slot = htab_find_slot_with_hash (avail_exprs, (void *)element, | |
5746637c | 1125 | element->hash, INSERT); |
6de9cd9a DN |
1126 | if (*slot == NULL) |
1127 | { | |
1128 | *slot = (void *) element; | |
726a989a RB |
1129 | |
1130 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1131 | { | |
1132 | fprintf (dump_file, "1>>> "); | |
1133 | print_expr_hash_elt (dump_file, element); | |
1134 | } | |
1135 | ||
1136 | VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, element); | |
6de9cd9a DN |
1137 | } |
1138 | else | |
1139 | free (element); | |
1140 | } | |
1141 | ||
726a989a RB |
1142 | /* Build a cond_equivalence record indicating that the comparison |
1143 | CODE holds between operands OP0 and OP1. */ | |
b8698a0f | 1144 | |
efea75f9 | 1145 | static void |
726a989a RB |
1146 | build_and_record_new_cond (enum tree_code code, |
1147 | tree op0, tree op1, | |
1148 | struct cond_equivalence *p) | |
efea75f9 | 1149 | { |
726a989a RB |
1150 | struct hashable_expr *cond = &p->cond; |
1151 | ||
1152 | gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison); | |
1153 | ||
1154 | cond->type = boolean_type_node; | |
1155 | cond->kind = EXPR_BINARY; | |
1156 | cond->ops.binary.op = code; | |
1157 | cond->ops.binary.opnd0 = op0; | |
1158 | cond->ops.binary.opnd1 = op1; | |
1159 | ||
1160 | p->value = boolean_true_node; | |
efea75f9 JL |
1161 | } |
1162 | ||
1163 | /* Record that COND is true and INVERTED is false into the edge information | |
1164 | structure. Also record that any conditions dominated by COND are true | |
1165 | as well. | |
d2d8936f JL |
1166 | |
1167 | For example, if a < b is true, then a <= b must also be true. */ | |
1168 | ||
1169 | static void | |
efea75f9 | 1170 | record_conditions (struct edge_info *edge_info, tree cond, tree inverted) |
d2d8936f | 1171 | { |
efea75f9 JL |
1172 | tree op0, op1; |
1173 | ||
1174 | if (!COMPARISON_CLASS_P (cond)) | |
1175 | return; | |
1176 | ||
1177 | op0 = TREE_OPERAND (cond, 0); | |
1178 | op1 = TREE_OPERAND (cond, 1); | |
1179 | ||
d2d8936f JL |
1180 | switch (TREE_CODE (cond)) |
1181 | { | |
1182 | case LT_EXPR: | |
d2d8936f | 1183 | case GT_EXPR: |
14b41b5f RS |
1184 | if (FLOAT_TYPE_P (TREE_TYPE (op0))) |
1185 | { | |
726a989a RB |
1186 | edge_info->max_cond_equivalences = 6; |
1187 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 6); | |
14b41b5f | 1188 | build_and_record_new_cond (ORDERED_EXPR, op0, op1, |
726a989a | 1189 | &edge_info->cond_equivalences[4]); |
14b41b5f | 1190 | build_and_record_new_cond (LTGT_EXPR, op0, op1, |
726a989a | 1191 | &edge_info->cond_equivalences[5]); |
14b41b5f RS |
1192 | } |
1193 | else | |
726a989a RB |
1194 | { |
1195 | edge_info->max_cond_equivalences = 4; | |
1196 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4); | |
14b41b5f RS |
1197 | } |
1198 | ||
efea75f9 JL |
1199 | build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR |
1200 | ? LE_EXPR : GE_EXPR), | |
726a989a | 1201 | op0, op1, &edge_info->cond_equivalences[2]); |
efea75f9 | 1202 | build_and_record_new_cond (NE_EXPR, op0, op1, |
726a989a | 1203 | &edge_info->cond_equivalences[3]); |
d2d8936f JL |
1204 | break; |
1205 | ||
1206 | case GE_EXPR: | |
1207 | case LE_EXPR: | |
14b41b5f RS |
1208 | if (FLOAT_TYPE_P (TREE_TYPE (op0))) |
1209 | { | |
726a989a RB |
1210 | edge_info->max_cond_equivalences = 3; |
1211 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 3); | |
14b41b5f | 1212 | build_and_record_new_cond (ORDERED_EXPR, op0, op1, |
726a989a | 1213 | &edge_info->cond_equivalences[2]); |
14b41b5f RS |
1214 | } |
1215 | else | |
1216 | { | |
726a989a RB |
1217 | edge_info->max_cond_equivalences = 2; |
1218 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 2); | |
14b41b5f | 1219 | } |
d2d8936f JL |
1220 | break; |
1221 | ||
1222 | case EQ_EXPR: | |
14b41b5f RS |
1223 | if (FLOAT_TYPE_P (TREE_TYPE (op0))) |
1224 | { | |
726a989a RB |
1225 | edge_info->max_cond_equivalences = 5; |
1226 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 5); | |
14b41b5f | 1227 | build_and_record_new_cond (ORDERED_EXPR, op0, op1, |
726a989a | 1228 | &edge_info->cond_equivalences[4]); |
14b41b5f RS |
1229 | } |
1230 | else | |
1231 | { | |
726a989a RB |
1232 | edge_info->max_cond_equivalences = 4; |
1233 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4); | |
14b41b5f | 1234 | } |
efea75f9 | 1235 | build_and_record_new_cond (LE_EXPR, op0, op1, |
726a989a | 1236 | &edge_info->cond_equivalences[2]); |
efea75f9 | 1237 | build_and_record_new_cond (GE_EXPR, op0, op1, |
726a989a | 1238 | &edge_info->cond_equivalences[3]); |
d2d8936f JL |
1239 | break; |
1240 | ||
1241 | case UNORDERED_EXPR: | |
726a989a RB |
1242 | edge_info->max_cond_equivalences = 8; |
1243 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 8); | |
efea75f9 | 1244 | build_and_record_new_cond (NE_EXPR, op0, op1, |
726a989a | 1245 | &edge_info->cond_equivalences[2]); |
efea75f9 | 1246 | build_and_record_new_cond (UNLE_EXPR, op0, op1, |
726a989a | 1247 | &edge_info->cond_equivalences[3]); |
efea75f9 | 1248 | build_and_record_new_cond (UNGE_EXPR, op0, op1, |
726a989a | 1249 | &edge_info->cond_equivalences[4]); |
efea75f9 | 1250 | build_and_record_new_cond (UNEQ_EXPR, op0, op1, |
726a989a | 1251 | &edge_info->cond_equivalences[5]); |
efea75f9 | 1252 | build_and_record_new_cond (UNLT_EXPR, op0, op1, |
726a989a | 1253 | &edge_info->cond_equivalences[6]); |
efea75f9 | 1254 | build_and_record_new_cond (UNGT_EXPR, op0, op1, |
726a989a | 1255 | &edge_info->cond_equivalences[7]); |
d2d8936f JL |
1256 | break; |
1257 | ||
1258 | case UNLT_EXPR: | |
d2d8936f | 1259 | case UNGT_EXPR: |
726a989a RB |
1260 | edge_info->max_cond_equivalences = 4; |
1261 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4); | |
efea75f9 JL |
1262 | build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR |
1263 | ? UNLE_EXPR : UNGE_EXPR), | |
726a989a | 1264 | op0, op1, &edge_info->cond_equivalences[2]); |
efea75f9 | 1265 | build_and_record_new_cond (NE_EXPR, op0, op1, |
726a989a | 1266 | &edge_info->cond_equivalences[3]); |
d2d8936f JL |
1267 | break; |
1268 | ||
1269 | case UNEQ_EXPR: | |
726a989a RB |
1270 | edge_info->max_cond_equivalences = 4; |
1271 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4); | |
efea75f9 | 1272 | build_and_record_new_cond (UNLE_EXPR, op0, op1, |
726a989a | 1273 | &edge_info->cond_equivalences[2]); |
efea75f9 | 1274 | build_and_record_new_cond (UNGE_EXPR, op0, op1, |
726a989a | 1275 | &edge_info->cond_equivalences[3]); |
d2d8936f JL |
1276 | break; |
1277 | ||
1278 | case LTGT_EXPR: | |
726a989a RB |
1279 | edge_info->max_cond_equivalences = 4; |
1280 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4); | |
efea75f9 | 1281 | build_and_record_new_cond (NE_EXPR, op0, op1, |
726a989a | 1282 | &edge_info->cond_equivalences[2]); |
efea75f9 | 1283 | build_and_record_new_cond (ORDERED_EXPR, op0, op1, |
726a989a | 1284 | &edge_info->cond_equivalences[3]); |
efea75f9 | 1285 | break; |
d2d8936f JL |
1286 | |
1287 | default: | |
726a989a RB |
1288 | edge_info->max_cond_equivalences = 2; |
1289 | edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 2); | |
d2d8936f JL |
1290 | break; |
1291 | } | |
efea75f9 JL |
1292 | |
1293 | /* Now store the original true and false conditions into the first | |
1294 | two slots. */ | |
726a989a RB |
1295 | initialize_expr_from_cond (cond, &edge_info->cond_equivalences[0].cond); |
1296 | edge_info->cond_equivalences[0].value = boolean_true_node; | |
1297 | ||
1298 | /* It is possible for INVERTED to be the negation of a comparison, | |
1299 | and not a valid RHS or GIMPLE_COND condition. This happens because | |
1300 | invert_truthvalue may return such an expression when asked to invert | |
1301 | a floating-point comparison. These comparisons are not assumed to | |
1302 | obey the trichotomy law. */ | |
1303 | initialize_expr_from_cond (inverted, &edge_info->cond_equivalences[1].cond); | |
1304 | edge_info->cond_equivalences[1].value = boolean_false_node; | |
d2d8936f JL |
1305 | } |
1306 | ||
6de9cd9a DN |
1307 | /* A helper function for record_const_or_copy and record_equality. |
1308 | Do the work of recording the value and undo info. */ | |
1309 | ||
1310 | static void | |
b5fefcf6 | 1311 | record_const_or_copy_1 (tree x, tree y, tree prev_x) |
6de9cd9a | 1312 | { |
448ee662 | 1313 | set_ssa_name_value (x, y); |
6de9cd9a | 1314 | |
726a989a RB |
1315 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1316 | { | |
1317 | fprintf (dump_file, "0>>> COPY "); | |
1318 | print_generic_expr (dump_file, x, 0); | |
1319 | fprintf (dump_file, " = "); | |
1320 | print_generic_expr (dump_file, y, 0); | |
1321 | fprintf (dump_file, "\n"); | |
1322 | } | |
1323 | ||
d4e6fecb NS |
1324 | VEC_reserve (tree, heap, const_and_copies_stack, 2); |
1325 | VEC_quick_push (tree, const_and_copies_stack, prev_x); | |
1326 | VEC_quick_push (tree, const_and_copies_stack, x); | |
6de9cd9a DN |
1327 | } |
1328 | ||
84dd478f DB |
1329 | /* Return the loop depth of the basic block of the defining statement of X. |
1330 | This number should not be treated as absolutely correct because the loop | |
1331 | information may not be completely up-to-date when dom runs. However, it | |
1332 | will be relatively correct, and as more passes are taught to keep loop info | |
1333 | up to date, the result will become more and more accurate. */ | |
1334 | ||
0bca51f0 | 1335 | int |
84dd478f DB |
1336 | loop_depth_of_name (tree x) |
1337 | { | |
726a989a | 1338 | gimple defstmt; |
84dd478f DB |
1339 | basic_block defbb; |
1340 | ||
1341 | /* If it's not an SSA_NAME, we have no clue where the definition is. */ | |
1342 | if (TREE_CODE (x) != SSA_NAME) | |
1343 | return 0; | |
1344 | ||
1345 | /* Otherwise return the loop depth of the defining statement's bb. | |
1346 | Note that there may not actually be a bb for this statement, if the | |
1347 | ssa_name is live on entry. */ | |
1348 | defstmt = SSA_NAME_DEF_STMT (x); | |
726a989a | 1349 | defbb = gimple_bb (defstmt); |
84dd478f DB |
1350 | if (!defbb) |
1351 | return 0; | |
1352 | ||
1353 | return defbb->loop_depth; | |
1354 | } | |
1355 | ||
6de9cd9a | 1356 | /* Record that X is equal to Y in const_and_copies. Record undo |
ceb7eb8f | 1357 | information in the block-local vector. */ |
6de9cd9a DN |
1358 | |
1359 | static void | |
b5fefcf6 | 1360 | record_const_or_copy (tree x, tree y) |
6de9cd9a | 1361 | { |
3aecd08b | 1362 | tree prev_x = SSA_NAME_VALUE (x); |
6de9cd9a | 1363 | |
726a989a RB |
1364 | gcc_assert (TREE_CODE (x) == SSA_NAME); |
1365 | ||
6de9cd9a DN |
1366 | if (TREE_CODE (y) == SSA_NAME) |
1367 | { | |
3aecd08b | 1368 | tree tmp = SSA_NAME_VALUE (y); |
6de9cd9a DN |
1369 | if (tmp) |
1370 | y = tmp; | |
1371 | } | |
1372 | ||
b5fefcf6 | 1373 | record_const_or_copy_1 (x, y, prev_x); |
6de9cd9a DN |
1374 | } |
1375 | ||
1376 | /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR. | |
1377 | This constrains the cases in which we may treat this as assignment. */ | |
1378 | ||
1379 | static void | |
b5fefcf6 | 1380 | record_equality (tree x, tree y) |
6de9cd9a DN |
1381 | { |
1382 | tree prev_x = NULL, prev_y = NULL; | |
1383 | ||
1384 | if (TREE_CODE (x) == SSA_NAME) | |
3aecd08b | 1385 | prev_x = SSA_NAME_VALUE (x); |
6de9cd9a | 1386 | if (TREE_CODE (y) == SSA_NAME) |
3aecd08b | 1387 | prev_y = SSA_NAME_VALUE (y); |
6de9cd9a | 1388 | |
84dd478f DB |
1389 | /* If one of the previous values is invariant, or invariant in more loops |
1390 | (by depth), then use that. | |
6de9cd9a DN |
1391 | Otherwise it doesn't matter which value we choose, just so |
1392 | long as we canonicalize on one value. */ | |
ad6003f2 | 1393 | if (is_gimple_min_invariant (y)) |
6de9cd9a | 1394 | ; |
ad6003f2 RG |
1395 | else if (is_gimple_min_invariant (x) |
1396 | || (loop_depth_of_name (x) <= loop_depth_of_name (y))) | |
6de9cd9a | 1397 | prev_x = x, x = y, y = prev_x, prev_x = prev_y; |
ad6003f2 | 1398 | else if (prev_x && is_gimple_min_invariant (prev_x)) |
6de9cd9a | 1399 | x = y, y = prev_x, prev_x = prev_y; |
c9145754 | 1400 | else if (prev_y) |
6de9cd9a DN |
1401 | y = prev_y; |
1402 | ||
1403 | /* After the swapping, we must have one SSA_NAME. */ | |
1404 | if (TREE_CODE (x) != SSA_NAME) | |
1405 | return; | |
1406 | ||
1407 | /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a | |
1408 | variable compared against zero. If we're honoring signed zeros, | |
1409 | then we cannot record this value unless we know that the value is | |
1ea7e6ad | 1410 | nonzero. */ |
6de9cd9a DN |
1411 | if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x))) |
1412 | && (TREE_CODE (y) != REAL_CST | |
1413 | || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y)))) | |
1414 | return; | |
1415 | ||
b5fefcf6 | 1416 | record_const_or_copy_1 (x, y, prev_x); |
6de9cd9a DN |
1417 | } |
1418 | ||
f67e783f ZD |
1419 | /* Returns true when STMT is a simple iv increment. It detects the |
1420 | following situation: | |
b8698a0f | 1421 | |
f67e783f ZD |
1422 | i_1 = phi (..., i_2) |
1423 | i_2 = i_1 +/- ... */ | |
1424 | ||
1425 | static bool | |
726a989a | 1426 | simple_iv_increment_p (gimple stmt) |
f67e783f | 1427 | { |
726a989a RB |
1428 | tree lhs, preinc; |
1429 | gimple phi; | |
1430 | size_t i; | |
f67e783f | 1431 | |
726a989a | 1432 | if (gimple_code (stmt) != GIMPLE_ASSIGN) |
f67e783f ZD |
1433 | return false; |
1434 | ||
726a989a | 1435 | lhs = gimple_assign_lhs (stmt); |
f67e783f ZD |
1436 | if (TREE_CODE (lhs) != SSA_NAME) |
1437 | return false; | |
1438 | ||
726a989a RB |
1439 | if (gimple_assign_rhs_code (stmt) != PLUS_EXPR |
1440 | && gimple_assign_rhs_code (stmt) != MINUS_EXPR) | |
f67e783f ZD |
1441 | return false; |
1442 | ||
726a989a RB |
1443 | preinc = gimple_assign_rhs1 (stmt); |
1444 | ||
f67e783f ZD |
1445 | if (TREE_CODE (preinc) != SSA_NAME) |
1446 | return false; | |
1447 | ||
1448 | phi = SSA_NAME_DEF_STMT (preinc); | |
726a989a | 1449 | if (gimple_code (phi) != GIMPLE_PHI) |
f67e783f ZD |
1450 | return false; |
1451 | ||
726a989a RB |
1452 | for (i = 0; i < gimple_phi_num_args (phi); i++) |
1453 | if (gimple_phi_arg_def (phi, i) == lhs) | |
f67e783f ZD |
1454 | return true; |
1455 | ||
1456 | return false; | |
1457 | } | |
1458 | ||
ff2ad0f7 | 1459 | /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current |
b8698a0f | 1460 | known value for that SSA_NAME (or NULL if no value is known). |
ff2ad0f7 | 1461 | |
b16caf72 JL |
1462 | Propagate values from CONST_AND_COPIES into the PHI nodes of the |
1463 | successors of BB. */ | |
ff2ad0f7 DN |
1464 | |
1465 | static void | |
b16caf72 | 1466 | cprop_into_successor_phis (basic_block bb) |
ff2ad0f7 DN |
1467 | { |
1468 | edge e; | |
628f6a4e | 1469 | edge_iterator ei; |
ff2ad0f7 | 1470 | |
628f6a4e | 1471 | FOR_EACH_EDGE (e, ei, bb->succs) |
ff2ad0f7 | 1472 | { |
0492baf2 | 1473 | int indx; |
726a989a | 1474 | gimple_stmt_iterator gsi; |
ff2ad0f7 DN |
1475 | |
1476 | /* If this is an abnormal edge, then we do not want to copy propagate | |
1477 | into the PHI alternative associated with this edge. */ | |
1478 | if (e->flags & EDGE_ABNORMAL) | |
1479 | continue; | |
1480 | ||
726a989a RB |
1481 | gsi = gsi_start_phis (e->dest); |
1482 | if (gsi_end_p (gsi)) | |
ff2ad0f7 DN |
1483 | continue; |
1484 | ||
0492baf2 | 1485 | indx = e->dest_idx; |
726a989a | 1486 | for ( ; !gsi_end_p (gsi); gsi_next (&gsi)) |
ff2ad0f7 | 1487 | { |
c22940cd | 1488 | tree new_val; |
ff2ad0f7 | 1489 | use_operand_p orig_p; |
c22940cd | 1490 | tree orig_val; |
726a989a | 1491 | gimple phi = gsi_stmt (gsi); |
ff2ad0f7 | 1492 | |
ff2ad0f7 DN |
1493 | /* The alternative may be associated with a constant, so verify |
1494 | it is an SSA_NAME before doing anything with it. */ | |
726a989a RB |
1495 | orig_p = gimple_phi_arg_imm_use_ptr (phi, indx); |
1496 | orig_val = get_use_from_ptr (orig_p); | |
c22940cd | 1497 | if (TREE_CODE (orig_val) != SSA_NAME) |
ff2ad0f7 DN |
1498 | continue; |
1499 | ||
ff2ad0f7 DN |
1500 | /* If we have *ORIG_P in our constant/copy table, then replace |
1501 | ORIG_P with its value in our constant/copy table. */ | |
c22940cd TN |
1502 | new_val = SSA_NAME_VALUE (orig_val); |
1503 | if (new_val | |
1504 | && new_val != orig_val | |
1505 | && (TREE_CODE (new_val) == SSA_NAME | |
1506 | || is_gimple_min_invariant (new_val)) | |
1507 | && may_propagate_copy (orig_val, new_val)) | |
1508 | propagate_value (orig_p, new_val); | |
ff2ad0f7 DN |
1509 | } |
1510 | } | |
1511 | } | |
1512 | ||
efea75f9 JL |
1513 | /* We have finished optimizing BB, record any information implied by |
1514 | taking a specific outgoing edge from BB. */ | |
1515 | ||
1516 | static void | |
1517 | record_edge_info (basic_block bb) | |
1518 | { | |
726a989a | 1519 | gimple_stmt_iterator gsi = gsi_last_bb (bb); |
efea75f9 JL |
1520 | struct edge_info *edge_info; |
1521 | ||
726a989a | 1522 | if (! gsi_end_p (gsi)) |
efea75f9 | 1523 | { |
726a989a | 1524 | gimple stmt = gsi_stmt (gsi); |
db3927fb | 1525 | location_t loc = gimple_location (stmt); |
efea75f9 | 1526 | |
726a989a | 1527 | if (gimple_code (stmt) == GIMPLE_SWITCH) |
efea75f9 | 1528 | { |
726a989a | 1529 | tree index = gimple_switch_index (stmt); |
efea75f9 | 1530 | |
726a989a | 1531 | if (TREE_CODE (index) == SSA_NAME) |
efea75f9 | 1532 | { |
726a989a RB |
1533 | int i; |
1534 | int n_labels = gimple_switch_num_labels (stmt); | |
e1111e8e | 1535 | tree *info = XCNEWVEC (tree, last_basic_block); |
efea75f9 JL |
1536 | edge e; |
1537 | edge_iterator ei; | |
1538 | ||
1539 | for (i = 0; i < n_labels; i++) | |
1540 | { | |
726a989a | 1541 | tree label = gimple_switch_label (stmt, i); |
efea75f9 | 1542 | basic_block target_bb = label_to_block (CASE_LABEL (label)); |
efea75f9 JL |
1543 | if (CASE_HIGH (label) |
1544 | || !CASE_LOW (label) | |
1545 | || info[target_bb->index]) | |
1546 | info[target_bb->index] = error_mark_node; | |
1547 | else | |
1548 | info[target_bb->index] = label; | |
1549 | } | |
1550 | ||
1551 | FOR_EACH_EDGE (e, ei, bb->succs) | |
1552 | { | |
1553 | basic_block target_bb = e->dest; | |
726a989a | 1554 | tree label = info[target_bb->index]; |
ff2ad0f7 | 1555 | |
726a989a | 1556 | if (label != NULL && label != error_mark_node) |
efea75f9 | 1557 | { |
db3927fb AH |
1558 | tree x = fold_convert_loc (loc, TREE_TYPE (index), |
1559 | CASE_LOW (label)); | |
efea75f9 | 1560 | edge_info = allocate_edge_info (e); |
726a989a | 1561 | edge_info->lhs = index; |
efea75f9 JL |
1562 | edge_info->rhs = x; |
1563 | } | |
1564 | } | |
1565 | free (info); | |
1566 | } | |
1567 | } | |
1568 | ||
1569 | /* A COND_EXPR may create equivalences too. */ | |
726a989a | 1570 | if (gimple_code (stmt) == GIMPLE_COND) |
efea75f9 | 1571 | { |
efea75f9 JL |
1572 | edge true_edge; |
1573 | edge false_edge; | |
1574 | ||
726a989a RB |
1575 | tree op0 = gimple_cond_lhs (stmt); |
1576 | tree op1 = gimple_cond_rhs (stmt); | |
1577 | enum tree_code code = gimple_cond_code (stmt); | |
efea75f9 | 1578 | |
726a989a | 1579 | extract_true_false_edges_from_block (bb, &true_edge, &false_edge); |
efea75f9 | 1580 | |
726a989a RB |
1581 | /* Special case comparing booleans against a constant as we |
1582 | know the value of OP0 on both arms of the branch. i.e., we | |
1583 | can record an equivalence for OP0 rather than COND. */ | |
1584 | if ((code == EQ_EXPR || code == NE_EXPR) | |
1585 | && TREE_CODE (op0) == SSA_NAME | |
1586 | && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE | |
1587 | && is_gimple_min_invariant (op1)) | |
1588 | { | |
1589 | if (code == EQ_EXPR) | |
1590 | { | |
1591 | edge_info = allocate_edge_info (true_edge); | |
1592 | edge_info->lhs = op0; | |
1593 | edge_info->rhs = (integer_zerop (op1) | |
1594 | ? boolean_false_node | |
1595 | : boolean_true_node); | |
1596 | ||
1597 | edge_info = allocate_edge_info (false_edge); | |
1598 | edge_info->lhs = op0; | |
1599 | edge_info->rhs = (integer_zerop (op1) | |
1600 | ? boolean_true_node | |
1601 | : boolean_false_node); | |
1602 | } | |
1603 | else | |
1604 | { | |
1605 | edge_info = allocate_edge_info (true_edge); | |
1606 | edge_info->lhs = op0; | |
1607 | edge_info->rhs = (integer_zerop (op1) | |
1608 | ? boolean_true_node | |
1609 | : boolean_false_node); | |
1610 | ||
1611 | edge_info = allocate_edge_info (false_edge); | |
1612 | edge_info->lhs = op0; | |
1613 | edge_info->rhs = (integer_zerop (op1) | |
1614 | ? boolean_false_node | |
1615 | : boolean_true_node); | |
1616 | } | |
1617 | } | |
1618 | else if (is_gimple_min_invariant (op0) | |
1619 | && (TREE_CODE (op1) == SSA_NAME | |
1620 | || is_gimple_min_invariant (op1))) | |
1621 | { | |
1622 | tree cond = build2 (code, boolean_type_node, op0, op1); | |
db3927fb | 1623 | tree inverted = invert_truthvalue_loc (loc, cond); |
726a989a RB |
1624 | struct edge_info *edge_info; |
1625 | ||
1626 | edge_info = allocate_edge_info (true_edge); | |
1627 | record_conditions (edge_info, cond, inverted); | |
1628 | ||
1629 | if (code == EQ_EXPR) | |
1630 | { | |
1631 | edge_info->lhs = op1; | |
1632 | edge_info->rhs = op0; | |
1633 | } | |
1634 | ||
1635 | edge_info = allocate_edge_info (false_edge); | |
1636 | record_conditions (edge_info, inverted, cond); | |
1637 | ||
533e50f6 | 1638 | if (TREE_CODE (inverted) == EQ_EXPR) |
726a989a RB |
1639 | { |
1640 | edge_info->lhs = op1; | |
1641 | edge_info->rhs = op0; | |
1642 | } | |
1643 | } | |
1644 | ||
1645 | else if (TREE_CODE (op0) == SSA_NAME | |
1646 | && (is_gimple_min_invariant (op1) | |
1647 | || TREE_CODE (op1) == SSA_NAME)) | |
1648 | { | |
1649 | tree cond = build2 (code, boolean_type_node, op0, op1); | |
db3927fb | 1650 | tree inverted = invert_truthvalue_loc (loc, cond); |
726a989a RB |
1651 | struct edge_info *edge_info; |
1652 | ||
1653 | edge_info = allocate_edge_info (true_edge); | |
1654 | record_conditions (edge_info, cond, inverted); | |
1655 | ||
1656 | if (code == EQ_EXPR) | |
1657 | { | |
1658 | edge_info->lhs = op0; | |
1659 | edge_info->rhs = op1; | |
1660 | } | |
1661 | ||
1662 | edge_info = allocate_edge_info (false_edge); | |
1663 | record_conditions (edge_info, inverted, cond); | |
1664 | ||
533e50f6 | 1665 | if (TREE_CODE (inverted) == EQ_EXPR) |
726a989a RB |
1666 | { |
1667 | edge_info->lhs = op0; | |
1668 | edge_info->rhs = op1; | |
1669 | } | |
1670 | } | |
1671 | } | |
1672 | ||
1673 | /* ??? TRUTH_NOT_EXPR can create an equivalence too. */ | |
efea75f9 JL |
1674 | } |
1675 | } | |
1676 | ||
6de9cd9a | 1677 | static void |
ccf5c864 PB |
1678 | dom_opt_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED, |
1679 | basic_block bb) | |
6de9cd9a | 1680 | { |
ccf5c864 PB |
1681 | gimple_stmt_iterator gsi; |
1682 | ||
1683 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1684 | fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index); | |
1685 | ||
1686 | /* Push a marker on the stacks of local information so that we know how | |
1687 | far to unwind when we finalize this block. */ | |
1688 | VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, NULL); | |
1689 | VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE); | |
1690 | ||
1691 | record_equivalences_from_incoming_edge (bb); | |
1692 | ||
1693 | /* PHI nodes can create equivalences too. */ | |
1694 | record_equivalences_from_phis (bb); | |
1695 | ||
1696 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
1697 | optimize_stmt (bb, gsi); | |
1698 | ||
1699 | /* Now prepare to process dominated blocks. */ | |
efea75f9 | 1700 | record_edge_info (bb); |
b16caf72 | 1701 | cprop_into_successor_phis (bb); |
6de9cd9a DN |
1702 | } |
1703 | ||
ccf5c864 PB |
1704 | /* We have finished processing the dominator children of BB, perform |
1705 | any finalization actions in preparation for leaving this node in | |
1706 | the dominator tree. */ | |
1707 | ||
1708 | static void | |
1709 | dom_opt_leave_block (struct dom_walk_data *walk_data, basic_block bb) | |
1710 | { | |
1711 | gimple last; | |
1712 | ||
1713 | /* If we have an outgoing edge to a block with multiple incoming and | |
1714 | outgoing edges, then we may be able to thread the edge, i.e., we | |
1715 | may be able to statically determine which of the outgoing edges | |
1716 | will be traversed when the incoming edge from BB is traversed. */ | |
1717 | if (single_succ_p (bb) | |
1718 | && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0 | |
1719 | && potentially_threadable_block (single_succ (bb))) | |
1720 | { | |
1721 | dom_thread_across_edge (walk_data, single_succ_edge (bb)); | |
1722 | } | |
1723 | else if ((last = last_stmt (bb)) | |
1724 | && gimple_code (last) == GIMPLE_COND | |
1725 | && EDGE_COUNT (bb->succs) == 2 | |
1726 | && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0 | |
1727 | && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0) | |
1728 | { | |
1729 | edge true_edge, false_edge; | |
1730 | ||
1731 | extract_true_false_edges_from_block (bb, &true_edge, &false_edge); | |
1732 | ||
1733 | /* Only try to thread the edge if it reaches a target block with | |
1734 | more than one predecessor and more than one successor. */ | |
1735 | if (potentially_threadable_block (true_edge->dest)) | |
1736 | { | |
1737 | struct edge_info *edge_info; | |
1738 | unsigned int i; | |
1739 | ||
1740 | /* Push a marker onto the available expression stack so that we | |
1741 | unwind any expressions related to the TRUE arm before processing | |
1742 | the false arm below. */ | |
1743 | VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, NULL); | |
1744 | VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE); | |
1745 | ||
1746 | edge_info = (struct edge_info *) true_edge->aux; | |
1747 | ||
1748 | /* If we have info associated with this edge, record it into | |
1749 | our equivalence tables. */ | |
1750 | if (edge_info) | |
1751 | { | |
1752 | struct cond_equivalence *cond_equivalences = edge_info->cond_equivalences; | |
1753 | tree lhs = edge_info->lhs; | |
1754 | tree rhs = edge_info->rhs; | |
1755 | ||
1756 | /* If we have a simple NAME = VALUE equivalence, record it. */ | |
1757 | if (lhs && TREE_CODE (lhs) == SSA_NAME) | |
1758 | record_const_or_copy (lhs, rhs); | |
1759 | ||
1760 | /* If we have 0 = COND or 1 = COND equivalences, record them | |
1761 | into our expression hash tables. */ | |
1762 | if (cond_equivalences) | |
1763 | for (i = 0; i < edge_info->max_cond_equivalences; i++) | |
1764 | record_cond (&cond_equivalences[i]); | |
1765 | } | |
1766 | ||
1767 | dom_thread_across_edge (walk_data, true_edge); | |
1768 | ||
1769 | /* And restore the various tables to their state before | |
1770 | we threaded this edge. */ | |
1771 | remove_local_expressions_from_table (); | |
1772 | } | |
1773 | ||
1774 | /* Similarly for the ELSE arm. */ | |
1775 | if (potentially_threadable_block (false_edge->dest)) | |
1776 | { | |
1777 | struct edge_info *edge_info; | |
1778 | unsigned int i; | |
1779 | ||
1780 | VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE); | |
1781 | edge_info = (struct edge_info *) false_edge->aux; | |
1782 | ||
1783 | /* If we have info associated with this edge, record it into | |
1784 | our equivalence tables. */ | |
1785 | if (edge_info) | |
1786 | { | |
1787 | struct cond_equivalence *cond_equivalences = edge_info->cond_equivalences; | |
1788 | tree lhs = edge_info->lhs; | |
1789 | tree rhs = edge_info->rhs; | |
1790 | ||
1791 | /* If we have a simple NAME = VALUE equivalence, record it. */ | |
1792 | if (lhs && TREE_CODE (lhs) == SSA_NAME) | |
1793 | record_const_or_copy (lhs, rhs); | |
1794 | ||
1795 | /* If we have 0 = COND or 1 = COND equivalences, record them | |
1796 | into our expression hash tables. */ | |
1797 | if (cond_equivalences) | |
1798 | for (i = 0; i < edge_info->max_cond_equivalences; i++) | |
1799 | record_cond (&cond_equivalences[i]); | |
1800 | } | |
1801 | ||
1802 | /* Now thread the edge. */ | |
1803 | dom_thread_across_edge (walk_data, false_edge); | |
1804 | ||
1805 | /* No need to remove local expressions from our tables | |
1806 | or restore vars to their original value as that will | |
1807 | be done immediately below. */ | |
1808 | } | |
1809 | } | |
1810 | ||
1811 | remove_local_expressions_from_table (); | |
1812 | restore_vars_to_original_value (); | |
ccf5c864 PB |
1813 | } |
1814 | ||
6de9cd9a DN |
1815 | /* Search for redundant computations in STMT. If any are found, then |
1816 | replace them with the variable holding the result of the computation. | |
1817 | ||
1818 | If safe, record this expression into the available expression hash | |
1819 | table. */ | |
1820 | ||
87c93592 | 1821 | static void |
726a989a | 1822 | eliminate_redundant_computations (gimple_stmt_iterator* gsi) |
6de9cd9a | 1823 | { |
726a989a | 1824 | tree expr_type; |
6de9cd9a | 1825 | tree cached_lhs; |
726a989a | 1826 | bool insert = true; |
726a989a | 1827 | bool assigns_var_p = false; |
6de9cd9a | 1828 | |
726a989a RB |
1829 | gimple stmt = gsi_stmt (*gsi); |
1830 | ||
1831 | tree def = gimple_get_lhs (stmt); | |
6de9cd9a DN |
1832 | |
1833 | /* Certain expressions on the RHS can be optimized away, but can not | |
471854f8 | 1834 | themselves be entered into the hash tables. */ |
ff88c5aa | 1835 | if (! def |
6de9cd9a DN |
1836 | || TREE_CODE (def) != SSA_NAME |
1837 | || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) | |
5006671f | 1838 | || gimple_vdef (stmt) |
f67e783f ZD |
1839 | /* Do not record equivalences for increments of ivs. This would create |
1840 | overlapping live ranges for a very questionable gain. */ | |
1841 | || simple_iv_increment_p (stmt)) | |
6de9cd9a DN |
1842 | insert = false; |
1843 | ||
1844 | /* Check if the expression has been computed before. */ | |
48732f23 | 1845 | cached_lhs = lookup_avail_expr (stmt, insert); |
6de9cd9a | 1846 | |
6de9cd9a DN |
1847 | opt_stats.num_exprs_considered++; |
1848 | ||
726a989a RB |
1849 | /* Get the type of the expression we are trying to optimize. */ |
1850 | if (is_gimple_assign (stmt)) | |
019b02f1 | 1851 | { |
726a989a RB |
1852 | expr_type = TREE_TYPE (gimple_assign_lhs (stmt)); |
1853 | assigns_var_p = true; | |
019b02f1 | 1854 | } |
726a989a RB |
1855 | else if (gimple_code (stmt) == GIMPLE_COND) |
1856 | expr_type = boolean_type_node; | |
1857 | else if (is_gimple_call (stmt)) | |
019b02f1 | 1858 | { |
726a989a RB |
1859 | gcc_assert (gimple_call_lhs (stmt)); |
1860 | expr_type = TREE_TYPE (gimple_call_lhs (stmt)); | |
1861 | assigns_var_p = true; | |
019b02f1 | 1862 | } |
726a989a RB |
1863 | else if (gimple_code (stmt) == GIMPLE_SWITCH) |
1864 | expr_type = TREE_TYPE (gimple_switch_index (stmt)); | |
1865 | else | |
1866 | gcc_unreachable (); | |
1867 | ||
1868 | if (!cached_lhs) | |
87c93592 | 1869 | return; |
6de9cd9a DN |
1870 | |
1871 | /* It is safe to ignore types here since we have already done | |
1872 | type checking in the hashing and equality routines. In fact | |
1873 | type checking here merely gets in the way of constant | |
1874 | propagation. Also, make sure that it is safe to propagate | |
726a989a RB |
1875 | CACHED_LHS into the expression in STMT. */ |
1876 | if ((TREE_CODE (cached_lhs) != SSA_NAME | |
1877 | && (assigns_var_p | |
1878 | || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))) | |
1879 | || may_propagate_copy_into_stmt (stmt, cached_lhs)) | |
1880 | { | |
1881 | #if defined ENABLE_CHECKING | |
1882 | gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME | |
1883 | || is_gimple_min_invariant (cached_lhs)); | |
1884 | #endif | |
1885 | ||
6de9cd9a DN |
1886 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1887 | { | |
1888 | fprintf (dump_file, " Replaced redundant expr '"); | |
726a989a | 1889 | print_gimple_expr (dump_file, stmt, 0, dump_flags); |
6de9cd9a DN |
1890 | fprintf (dump_file, "' with '"); |
1891 | print_generic_expr (dump_file, cached_lhs, dump_flags); | |
726a989a | 1892 | fprintf (dump_file, "'\n"); |
6de9cd9a DN |
1893 | } |
1894 | ||
1895 | opt_stats.num_re++; | |
b8698a0f | 1896 | |
726a989a RB |
1897 | if (assigns_var_p |
1898 | && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))) | |
1899 | cached_lhs = fold_convert (expr_type, cached_lhs); | |
6de9cd9a | 1900 | |
726a989a RB |
1901 | propagate_tree_value_into_stmt (gsi, cached_lhs); |
1902 | ||
1903 | /* Since it is always necessary to mark the result as modified, | |
1904 | perhaps we should move this into propagate_tree_value_into_stmt | |
1905 | itself. */ | |
1906 | gimple_set_modified (gsi_stmt (*gsi), true); | |
1907 | } | |
6de9cd9a DN |
1908 | } |
1909 | ||
726a989a | 1910 | /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either |
6de9cd9a DN |
1911 | the available expressions table or the const_and_copies table. |
1912 | Detect and record those equivalences. */ | |
726a989a RB |
1913 | /* We handle only very simple copy equivalences here. The heavy |
1914 | lifing is done by eliminate_redundant_computations. */ | |
6de9cd9a DN |
1915 | |
1916 | static void | |
726a989a | 1917 | record_equivalences_from_stmt (gimple stmt, int may_optimize_p) |
6de9cd9a | 1918 | { |
726a989a RB |
1919 | tree lhs; |
1920 | enum tree_code lhs_code; | |
6de9cd9a | 1921 | |
726a989a RB |
1922 | gcc_assert (is_gimple_assign (stmt)); |
1923 | ||
1924 | lhs = gimple_assign_lhs (stmt); | |
1925 | lhs_code = TREE_CODE (lhs); | |
6de9cd9a | 1926 | |
726a989a | 1927 | if (lhs_code == SSA_NAME |
7e673273 | 1928 | && gimple_assign_single_p (stmt)) |
726a989a RB |
1929 | { |
1930 | tree rhs = gimple_assign_rhs1 (stmt); | |
b8698a0f | 1931 | |
6de9cd9a DN |
1932 | /* If the RHS of the assignment is a constant or another variable that |
1933 | may be propagated, register it in the CONST_AND_COPIES table. We | |
1934 | do not need to record unwind data for this, since this is a true | |
1ea7e6ad | 1935 | assignment and not an equivalence inferred from a comparison. All |
6de9cd9a DN |
1936 | uses of this ssa name are dominated by this assignment, so unwinding |
1937 | just costs time and space. */ | |
1938 | if (may_optimize_p | |
1939 | && (TREE_CODE (rhs) == SSA_NAME | |
1940 | || is_gimple_min_invariant (rhs))) | |
726a989a RB |
1941 | { |
1942 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1943 | { | |
1944 | fprintf (dump_file, "==== ASGN "); | |
1945 | print_generic_expr (dump_file, lhs, 0); | |
1946 | fprintf (dump_file, " = "); | |
1947 | print_generic_expr (dump_file, rhs, 0); | |
1948 | fprintf (dump_file, "\n"); | |
1949 | } | |
1950 | ||
448ee662 | 1951 | set_ssa_name_value (lhs, rhs); |
726a989a | 1952 | } |
6de9cd9a DN |
1953 | } |
1954 | ||
6de9cd9a DN |
1955 | /* A memory store, even an aliased store, creates a useful |
1956 | equivalence. By exchanging the LHS and RHS, creating suitable | |
1957 | vops and recording the result in the available expression table, | |
1958 | we may be able to expose more redundant loads. */ | |
726a989a RB |
1959 | if (!gimple_has_volatile_ops (stmt) |
1960 | && gimple_references_memory_p (stmt) | |
1961 | && gimple_assign_single_p (stmt) | |
1962 | && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME | |
1963 | || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))) | |
6de9cd9a DN |
1964 | && !is_gimple_reg (lhs)) |
1965 | { | |
726a989a RB |
1966 | tree rhs = gimple_assign_rhs1 (stmt); |
1967 | gimple new_stmt; | |
6de9cd9a | 1968 | |
cf3135aa | 1969 | /* Build a new statement with the RHS and LHS exchanged. */ |
726a989a RB |
1970 | if (TREE_CODE (rhs) == SSA_NAME) |
1971 | { | |
1972 | /* NOTE tuples. The call to gimple_build_assign below replaced | |
1973 | a call to build_gimple_modify_stmt, which did not set the | |
1974 | SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so | |
1975 | may cause an SSA validation failure, as the LHS may be a | |
1976 | default-initialized name and should have no definition. I'm | |
1977 | a bit dubious of this, as the artificial statement that we | |
1978 | generate here may in fact be ill-formed, but it is simply | |
1979 | used as an internal device in this pass, and never becomes | |
1980 | part of the CFG. */ | |
1981 | gimple defstmt = SSA_NAME_DEF_STMT (rhs); | |
1982 | new_stmt = gimple_build_assign (rhs, lhs); | |
1983 | SSA_NAME_DEF_STMT (rhs) = defstmt; | |
1984 | } | |
1985 | else | |
1986 | new_stmt = gimple_build_assign (rhs, lhs); | |
1987 | ||
5006671f | 1988 | gimple_set_vuse (new_stmt, gimple_vdef (stmt)); |
6de9cd9a | 1989 | |
cf3135aa RG |
1990 | /* Finally enter the statement into the available expression |
1991 | table. */ | |
1992 | lookup_avail_expr (new_stmt, true); | |
6de9cd9a DN |
1993 | } |
1994 | } | |
1995 | ||
ff2ad0f7 DN |
1996 | /* Replace *OP_P in STMT with any known equivalent value for *OP_P from |
1997 | CONST_AND_COPIES. */ | |
1998 | ||
87c93592 | 1999 | static void |
726a989a | 2000 | cprop_operand (gimple stmt, use_operand_p op_p) |
ff2ad0f7 | 2001 | { |
ff2ad0f7 DN |
2002 | tree val; |
2003 | tree op = USE_FROM_PTR (op_p); | |
2004 | ||
2005 | /* If the operand has a known constant value or it is known to be a | |
2006 | copy of some other variable, use the value or copy stored in | |
2007 | CONST_AND_COPIES. */ | |
3aecd08b | 2008 | val = SSA_NAME_VALUE (op); |
c9145754 | 2009 | if (val && val != op) |
ff2ad0f7 | 2010 | { |
ff2ad0f7 DN |
2011 | /* Do not change the base variable in the virtual operand |
2012 | tables. That would make it impossible to reconstruct | |
2013 | the renamed virtual operand if we later modify this | |
2014 | statement. Also only allow the new value to be an SSA_NAME | |
2015 | for propagation into virtual operands. */ | |
2016 | if (!is_gimple_reg (op) | |
0bca51f0 DN |
2017 | && (TREE_CODE (val) != SSA_NAME |
2018 | || is_gimple_reg (val) | |
2019 | || get_virtual_var (val) != get_virtual_var (op))) | |
87c93592 | 2020 | return; |
ff2ad0f7 | 2021 | |
aa24864c | 2022 | /* Do not replace hard register operands in asm statements. */ |
726a989a | 2023 | if (gimple_code (stmt) == GIMPLE_ASM |
aa24864c | 2024 | && !may_propagate_copy_into_asm (op)) |
87c93592 | 2025 | return; |
aa24864c | 2026 | |
ff2ad0f7 DN |
2027 | /* Certain operands are not allowed to be copy propagated due |
2028 | to their interaction with exception handling and some GCC | |
2029 | extensions. */ | |
66e8b99c | 2030 | if (!may_propagate_copy (op, val)) |
87c93592 | 2031 | return; |
66e8b99c RG |
2032 | |
2033 | /* Do not propagate addresses that point to volatiles into memory | |
2034 | stmts without volatile operands. */ | |
2035 | if (POINTER_TYPE_P (TREE_TYPE (val)) | |
2036 | && TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (val))) | |
2037 | && gimple_has_mem_ops (stmt) | |
2038 | && !gimple_has_volatile_ops (stmt)) | |
87c93592 | 2039 | return; |
66e8b99c | 2040 | |
111e0c9f DB |
2041 | /* Do not propagate copies if the propagated value is at a deeper loop |
2042 | depth than the propagatee. Otherwise, this may move loop variant | |
2043 | variables outside of their loops and prevent coalescing | |
2044 | opportunities. If the value was loop invariant, it will be hoisted | |
2045 | by LICM and exposed for copy propagation. */ | |
2046 | if (loop_depth_of_name (val) > loop_depth_of_name (op)) | |
87c93592 | 2047 | return; |
ff2ad0f7 | 2048 | |
e9d85fa6 RG |
2049 | /* Do not propagate copies into simple IV increment statements. |
2050 | See PR23821 for how this can disturb IV analysis. */ | |
2051 | if (TREE_CODE (val) != INTEGER_CST | |
2052 | && simple_iv_increment_p (stmt)) | |
2053 | return; | |
2054 | ||
ff2ad0f7 DN |
2055 | /* Dump details. */ |
2056 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2057 | { | |
2058 | fprintf (dump_file, " Replaced '"); | |
2059 | print_generic_expr (dump_file, op, dump_flags); | |
2060 | fprintf (dump_file, "' with %s '", | |
2061 | (TREE_CODE (val) != SSA_NAME ? "constant" : "variable")); | |
2062 | print_generic_expr (dump_file, val, dump_flags); | |
2063 | fprintf (dump_file, "'\n"); | |
2064 | } | |
2065 | ||
0bca51f0 DN |
2066 | if (TREE_CODE (val) != SSA_NAME) |
2067 | opt_stats.num_const_prop++; | |
2068 | else | |
2069 | opt_stats.num_copy_prop++; | |
2070 | ||
ff2ad0f7 DN |
2071 | propagate_value (op_p, val); |
2072 | ||
2073 | /* And note that we modified this statement. This is now | |
2074 | safe, even if we changed virtual operands since we will | |
2075 | rescan the statement and rewrite its operands again. */ | |
726a989a | 2076 | gimple_set_modified (stmt, true); |
ff2ad0f7 | 2077 | } |
ff2ad0f7 DN |
2078 | } |
2079 | ||
2080 | /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current | |
b8698a0f | 2081 | known value for that SSA_NAME (or NULL if no value is known). |
ff2ad0f7 DN |
2082 | |
2083 | Propagate values from CONST_AND_COPIES into the uses, vuses and | |
cfaab3a9 | 2084 | vdef_ops of STMT. */ |
ff2ad0f7 | 2085 | |
87c93592 | 2086 | static void |
726a989a | 2087 | cprop_into_stmt (gimple stmt) |
ff2ad0f7 | 2088 | { |
4c124b4c AM |
2089 | use_operand_p op_p; |
2090 | ssa_op_iter iter; | |
ff2ad0f7 | 2091 | |
4c124b4c | 2092 | FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES) |
ff2ad0f7 | 2093 | { |
ff2ad0f7 | 2094 | if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME) |
87c93592 | 2095 | cprop_operand (stmt, op_p); |
ff2ad0f7 | 2096 | } |
ff2ad0f7 DN |
2097 | } |
2098 | ||
206048bd | 2099 | /* Optimize the statement pointed to by iterator SI. |
b8698a0f | 2100 | |
6de9cd9a DN |
2101 | We try to perform some simplistic global redundancy elimination and |
2102 | constant propagation: | |
2103 | ||
2104 | 1- To detect global redundancy, we keep track of expressions that have | |
2105 | been computed in this block and its dominators. If we find that the | |
2106 | same expression is computed more than once, we eliminate repeated | |
2107 | computations by using the target of the first one. | |
2108 | ||
2109 | 2- Constant values and copy assignments. This is used to do very | |
2110 | simplistic constant and copy propagation. When a constant or copy | |
2111 | assignment is found, we map the value on the RHS of the assignment to | |
2112 | the variable in the LHS in the CONST_AND_COPIES table. */ | |
2113 | ||
2114 | static void | |
ccf5c864 | 2115 | optimize_stmt (basic_block bb, gimple_stmt_iterator si) |
6de9cd9a | 2116 | { |
726a989a | 2117 | gimple stmt, old_stmt; |
6de9cd9a | 2118 | bool may_optimize_p; |
c5cac099 | 2119 | bool modified_p = false; |
6de9cd9a | 2120 | |
726a989a | 2121 | old_stmt = stmt = gsi_stmt (si); |
b8698a0f | 2122 | |
726a989a | 2123 | if (gimple_code (stmt) == GIMPLE_COND) |
0e0ed594 | 2124 | canonicalize_comparison (stmt); |
b8698a0f | 2125 | |
f430bae8 | 2126 | update_stmt_if_modified (stmt); |
6de9cd9a | 2127 | opt_stats.num_stmts++; |
6de9cd9a DN |
2128 | |
2129 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2130 | { | |
2131 | fprintf (dump_file, "Optimizing statement "); | |
726a989a | 2132 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
6de9cd9a DN |
2133 | } |
2134 | ||
cfaab3a9 | 2135 | /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */ |
87c93592 | 2136 | cprop_into_stmt (stmt); |
6de9cd9a DN |
2137 | |
2138 | /* If the statement has been modified with constant replacements, | |
2139 | fold its RHS before checking for redundant computations. */ | |
726a989a | 2140 | if (gimple_modified_p (stmt)) |
6de9cd9a | 2141 | { |
726a989a | 2142 | tree rhs = NULL; |
6cedb4ac | 2143 | |
6de9cd9a DN |
2144 | /* Try to fold the statement making sure that STMT is kept |
2145 | up to date. */ | |
726a989a | 2146 | if (fold_stmt (&si)) |
6de9cd9a | 2147 | { |
726a989a | 2148 | stmt = gsi_stmt (si); |
076ba157 | 2149 | gimple_set_modified (stmt, true); |
6de9cd9a DN |
2150 | |
2151 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2152 | { | |
2153 | fprintf (dump_file, " Folded to: "); | |
726a989a | 2154 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
6de9cd9a DN |
2155 | } |
2156 | } | |
2157 | ||
726a989a RB |
2158 | /* We only need to consider cases that can yield a gimple operand. */ |
2159 | if (gimple_assign_single_p (stmt)) | |
2160 | rhs = gimple_assign_rhs1 (stmt); | |
2161 | else if (gimple_code (stmt) == GIMPLE_GOTO) | |
2162 | rhs = gimple_goto_dest (stmt); | |
2163 | else if (gimple_code (stmt) == GIMPLE_SWITCH) | |
2164 | /* This should never be an ADDR_EXPR. */ | |
2165 | rhs = gimple_switch_index (stmt); | |
2166 | ||
6cedb4ac | 2167 | if (rhs && TREE_CODE (rhs) == ADDR_EXPR) |
726a989a | 2168 | recompute_tree_invariant_for_addr_expr (rhs); |
6cedb4ac | 2169 | |
c5cac099 JJ |
2170 | /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called, |
2171 | even if fold_stmt updated the stmt already and thus cleared | |
2172 | gimple_modified_p flag on it. */ | |
2173 | modified_p = true; | |
6de9cd9a DN |
2174 | } |
2175 | ||
2176 | /* Check for redundant computations. Do this optimization only | |
2177 | for assignments that have no volatile ops and conditionals. */ | |
726a989a RB |
2178 | may_optimize_p = (!gimple_has_volatile_ops (stmt) |
2179 | && ((is_gimple_assign (stmt) | |
2180 | && !gimple_rhs_has_side_effects (stmt)) | |
2181 | || (is_gimple_call (stmt) | |
2182 | && gimple_call_lhs (stmt) != NULL_TREE | |
2183 | && !gimple_rhs_has_side_effects (stmt)) | |
2184 | || gimple_code (stmt) == GIMPLE_COND | |
2185 | || gimple_code (stmt) == GIMPLE_SWITCH)); | |
6de9cd9a DN |
2186 | |
2187 | if (may_optimize_p) | |
726a989a | 2188 | { |
44e10129 MM |
2189 | if (gimple_code (stmt) == GIMPLE_CALL) |
2190 | { | |
2191 | /* Resolve __builtin_constant_p. If it hasn't been | |
2192 | folded to integer_one_node by now, it's fairly | |
2193 | certain that the value simply isn't constant. */ | |
2194 | tree callee = gimple_call_fndecl (stmt); | |
2195 | if (callee | |
2196 | && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL | |
2197 | && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P) | |
2198 | { | |
2199 | propagate_tree_value_into_stmt (&si, integer_zero_node); | |
2200 | stmt = gsi_stmt (si); | |
2201 | } | |
2202 | } | |
aabf6a03 RG |
2203 | |
2204 | update_stmt_if_modified (stmt); | |
2205 | eliminate_redundant_computations (&si); | |
2206 | stmt = gsi_stmt (si); | |
726a989a | 2207 | } |
6de9cd9a DN |
2208 | |
2209 | /* Record any additional equivalences created by this statement. */ | |
726a989a RB |
2210 | if (is_gimple_assign (stmt)) |
2211 | record_equivalences_from_stmt (stmt, may_optimize_p); | |
6de9cd9a | 2212 | |
6de9cd9a DN |
2213 | /* If STMT is a COND_EXPR and it was modified, then we may know |
2214 | where it goes. If that is the case, then mark the CFG as altered. | |
2215 | ||
2216 | This will cause us to later call remove_unreachable_blocks and | |
b8698a0f | 2217 | cleanup_tree_cfg when it is safe to do so. It is not safe to |
6de9cd9a DN |
2218 | clean things up here since removal of edges and such can trigger |
2219 | the removal of PHI nodes, which in turn can release SSA_NAMEs to | |
2220 | the manager. | |
2221 | ||
2222 | That's all fine and good, except that once SSA_NAMEs are released | |
2223 | to the manager, we must not call create_ssa_name until all references | |
2224 | to released SSA_NAMEs have been eliminated. | |
2225 | ||
2226 | All references to the deleted SSA_NAMEs can not be eliminated until | |
2227 | we remove unreachable blocks. | |
2228 | ||
2229 | We can not remove unreachable blocks until after we have completed | |
2230 | any queued jump threading. | |
2231 | ||
2232 | We can not complete any queued jump threads until we have taken | |
2233 | appropriate variables out of SSA form. Taking variables out of | |
2234 | SSA form can call create_ssa_name and thus we lose. | |
2235 | ||
2236 | Ultimately I suspect we're going to need to change the interface | |
2237 | into the SSA_NAME manager. */ | |
c5cac099 | 2238 | if (gimple_modified_p (stmt) || modified_p) |
6de9cd9a DN |
2239 | { |
2240 | tree val = NULL; | |
b8698a0f | 2241 | |
aabf6a03 | 2242 | update_stmt_if_modified (stmt); |
6de9cd9a | 2243 | |
726a989a | 2244 | if (gimple_code (stmt) == GIMPLE_COND) |
db3927fb AH |
2245 | val = fold_binary_loc (gimple_location (stmt), |
2246 | gimple_cond_code (stmt), boolean_type_node, | |
726a989a RB |
2247 | gimple_cond_lhs (stmt), gimple_cond_rhs (stmt)); |
2248 | else if (gimple_code (stmt) == GIMPLE_SWITCH) | |
2249 | val = gimple_switch_index (stmt); | |
6de9cd9a | 2250 | |
1eaba2f2 | 2251 | if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val)) |
6de9cd9a | 2252 | cfg_altered = true; |
1eaba2f2 RH |
2253 | |
2254 | /* If we simplified a statement in such a way as to be shown that it | |
2255 | cannot trap, update the eh information and the cfg to match. */ | |
af47810a | 2256 | if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)) |
1eaba2f2 RH |
2257 | { |
2258 | bitmap_set_bit (need_eh_cleanup, bb->index); | |
2259 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2260 | fprintf (dump_file, " Flagged to clear EH edges.\n"); | |
2261 | } | |
6de9cd9a | 2262 | } |
6de9cd9a DN |
2263 | } |
2264 | ||
726a989a RB |
2265 | /* Search for an existing instance of STMT in the AVAIL_EXPRS table. |
2266 | If found, return its LHS. Otherwise insert STMT in the table and | |
2267 | return NULL_TREE. | |
6de9cd9a | 2268 | |
726a989a RB |
2269 | Also, when an expression is first inserted in the table, it is also |
2270 | is also added to AVAIL_EXPRS_STACK, so that it can be removed when | |
2271 | we finish processing this block and its children. */ | |
6de9cd9a DN |
2272 | |
2273 | static tree | |
726a989a | 2274 | lookup_avail_expr (gimple stmt, bool insert) |
6de9cd9a DN |
2275 | { |
2276 | void **slot; | |
2277 | tree lhs; | |
2278 | tree temp; | |
a7d04a53 | 2279 | struct expr_hash_elt element; |
6de9cd9a | 2280 | |
726a989a RB |
2281 | /* Get LHS of assignment or call, else NULL_TREE. */ |
2282 | lhs = gimple_get_lhs (stmt); | |
6de9cd9a | 2283 | |
a7d04a53 | 2284 | initialize_hash_element (stmt, lhs, &element); |
6de9cd9a | 2285 | |
726a989a RB |
2286 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2287 | { | |
2288 | fprintf (dump_file, "LKUP "); | |
a7d04a53 | 2289 | print_expr_hash_elt (dump_file, &element); |
726a989a RB |
2290 | } |
2291 | ||
6de9cd9a DN |
2292 | /* Don't bother remembering constant assignments and copy operations. |
2293 | Constants and copy operations are handled by the constant/copy propagator | |
2294 | in optimize_stmt. */ | |
a7d04a53 RG |
2295 | if (element.expr.kind == EXPR_SINGLE |
2296 | && (TREE_CODE (element.expr.ops.single.rhs) == SSA_NAME | |
2297 | || is_gimple_min_invariant (element.expr.ops.single.rhs))) | |
2298 | return NULL_TREE; | |
6de9cd9a | 2299 | |
6de9cd9a | 2300 | /* Finally try to find the expression in the main expression hash table. */ |
a7d04a53 | 2301 | slot = htab_find_slot_with_hash (avail_exprs, &element, element.hash, |
6de9cd9a DN |
2302 | (insert ? INSERT : NO_INSERT)); |
2303 | if (slot == NULL) | |
a7d04a53 | 2304 | return NULL_TREE; |
6de9cd9a DN |
2305 | |
2306 | if (*slot == NULL) | |
2307 | { | |
a7d04a53 RG |
2308 | struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt); |
2309 | *element2 = element; | |
2310 | element2->stamp = element2; | |
2311 | *slot = (void *) element2; | |
726a989a RB |
2312 | |
2313 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2314 | { | |
2315 | fprintf (dump_file, "2>>> "); | |
a7d04a53 | 2316 | print_expr_hash_elt (dump_file, element2); |
726a989a RB |
2317 | } |
2318 | ||
a7d04a53 | 2319 | VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, element2); |
6de9cd9a DN |
2320 | return NULL_TREE; |
2321 | } | |
2322 | ||
2323 | /* Extract the LHS of the assignment so that it can be used as the current | |
2324 | definition of another variable. */ | |
2325 | lhs = ((struct expr_hash_elt *)*slot)->lhs; | |
2326 | ||
2327 | /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then | |
2328 | use the value from the const_and_copies table. */ | |
2329 | if (TREE_CODE (lhs) == SSA_NAME) | |
2330 | { | |
3aecd08b | 2331 | temp = SSA_NAME_VALUE (lhs); |
c9145754 | 2332 | if (temp) |
6de9cd9a DN |
2333 | lhs = temp; |
2334 | } | |
2335 | ||
726a989a RB |
2336 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2337 | { | |
2338 | fprintf (dump_file, "FIND: "); | |
2339 | print_generic_expr (dump_file, lhs, 0); | |
2340 | fprintf (dump_file, "\n"); | |
2341 | } | |
2342 | ||
6de9cd9a DN |
2343 | return lhs; |
2344 | } | |
2345 | ||
726a989a RB |
2346 | /* Hashing and equality functions for AVAIL_EXPRS. We compute a value number |
2347 | for expressions using the code of the expression and the SSA numbers of | |
2348 | its operands. */ | |
6de9cd9a DN |
2349 | |
2350 | static hashval_t | |
2351 | avail_expr_hash (const void *p) | |
2352 | { | |
726a989a RB |
2353 | gimple stmt = ((const struct expr_hash_elt *)p)->stmt; |
2354 | const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr; | |
f47c96aa | 2355 | tree vuse; |
6de9cd9a | 2356 | hashval_t val = 0; |
6de9cd9a | 2357 | |
726a989a | 2358 | val = iterative_hash_hashable_expr (expr, val); |
6de9cd9a DN |
2359 | |
2360 | /* If the hash table entry is not associated with a statement, then we | |
2361 | can just hash the expression and not worry about virtual operands | |
2362 | and such. */ | |
726a989a | 2363 | if (!stmt) |
6de9cd9a DN |
2364 | return val; |
2365 | ||
5006671f | 2366 | /* Add the SSA version numbers of the vuse operand. This is important |
6de9cd9a DN |
2367 | because compound variables like arrays are not renamed in the |
2368 | operands. Rather, the rename is done on the virtual variable | |
2369 | representing all the elements of the array. */ | |
5006671f | 2370 | if ((vuse = gimple_vuse (stmt))) |
f47c96aa | 2371 | val = iterative_hash_expr (vuse, val); |
6de9cd9a DN |
2372 | |
2373 | return val; | |
2374 | } | |
2375 | ||
940db2c8 RH |
2376 | static hashval_t |
2377 | real_avail_expr_hash (const void *p) | |
2378 | { | |
2379 | return ((const struct expr_hash_elt *)p)->hash; | |
2380 | } | |
6de9cd9a DN |
2381 | |
2382 | static int | |
2383 | avail_expr_eq (const void *p1, const void *p2) | |
2384 | { | |
726a989a RB |
2385 | gimple stmt1 = ((const struct expr_hash_elt *)p1)->stmt; |
2386 | const struct hashable_expr *expr1 = &((const struct expr_hash_elt *)p1)->expr; | |
2387 | const struct expr_hash_elt *stamp1 = ((const struct expr_hash_elt *)p1)->stamp; | |
2388 | gimple stmt2 = ((const struct expr_hash_elt *)p2)->stmt; | |
2389 | const struct hashable_expr *expr2 = &((const struct expr_hash_elt *)p2)->expr; | |
2390 | const struct expr_hash_elt *stamp2 = ((const struct expr_hash_elt *)p2)->stamp; | |
2391 | ||
2392 | /* This case should apply only when removing entries from the table. */ | |
2393 | if (stamp1 == stamp2) | |
6de9cd9a DN |
2394 | return true; |
2395 | ||
726a989a RB |
2396 | /* FIXME tuples: |
2397 | We add stmts to a hash table and them modify them. To detect the case | |
2398 | that we modify a stmt and then search for it, we assume that the hash | |
2399 | is always modified by that change. | |
2400 | We have to fully check why this doesn't happen on trunk or rewrite | |
2401 | this in a more reliable (and easier to understand) way. */ | |
2402 | if (((const struct expr_hash_elt *)p1)->hash | |
2403 | != ((const struct expr_hash_elt *)p2)->hash) | |
6de9cd9a DN |
2404 | return false; |
2405 | ||
2406 | /* In case of a collision, both RHS have to be identical and have the | |
2407 | same VUSE operands. */ | |
726a989a RB |
2408 | if (hashable_expr_equal_p (expr1, expr2) |
2409 | && types_compatible_p (expr1->type, expr2->type)) | |
6de9cd9a | 2410 | { |
726a989a | 2411 | /* Note that STMT1 and/or STMT2 may be NULL. */ |
5006671f RG |
2412 | return ((stmt1 ? gimple_vuse (stmt1) : NULL_TREE) |
2413 | == (stmt2 ? gimple_vuse (stmt2) : NULL_TREE)); | |
6de9cd9a DN |
2414 | } |
2415 | ||
2416 | return false; | |
2417 | } | |
e67c25c7 JL |
2418 | |
2419 | /* PHI-ONLY copy and constant propagation. This pass is meant to clean | |
2420 | up degenerate PHIs created by or exposed by jump threading. */ | |
2421 | ||
2422 | /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return | |
2423 | NULL. */ | |
2424 | ||
bf1cbdc6 | 2425 | tree |
726a989a | 2426 | degenerate_phi_result (gimple phi) |
e67c25c7 | 2427 | { |
726a989a | 2428 | tree lhs = gimple_phi_result (phi); |
e67c25c7 | 2429 | tree val = NULL; |
726a989a | 2430 | size_t i; |
e67c25c7 JL |
2431 | |
2432 | /* Ignoring arguments which are the same as LHS, if all the remaining | |
2433 | arguments are the same, then the PHI is a degenerate and has the | |
2434 | value of that common argument. */ | |
726a989a | 2435 | for (i = 0; i < gimple_phi_num_args (phi); i++) |
e67c25c7 | 2436 | { |
726a989a | 2437 | tree arg = gimple_phi_arg_def (phi, i); |
e67c25c7 JL |
2438 | |
2439 | if (arg == lhs) | |
2440 | continue; | |
6edde545 RH |
2441 | else if (!arg) |
2442 | break; | |
e67c25c7 JL |
2443 | else if (!val) |
2444 | val = arg; | |
42a06e46 AO |
2445 | else if (arg == val) |
2446 | continue; | |
2447 | /* We bring in some of operand_equal_p not only to speed things | |
2448 | up, but also to avoid crashing when dereferencing the type of | |
2449 | a released SSA name. */ | |
6edde545 | 2450 | else if (TREE_CODE (val) != TREE_CODE (arg) |
42a06e46 AO |
2451 | || TREE_CODE (val) == SSA_NAME |
2452 | || !operand_equal_p (arg, val, 0)) | |
e67c25c7 JL |
2453 | break; |
2454 | } | |
726a989a | 2455 | return (i == gimple_phi_num_args (phi) ? val : NULL); |
e67c25c7 JL |
2456 | } |
2457 | ||
726a989a | 2458 | /* Given a statement STMT, which is either a PHI node or an assignment, |
e67c25c7 JL |
2459 | remove it from the IL. */ |
2460 | ||
2461 | static void | |
726a989a | 2462 | remove_stmt_or_phi (gimple stmt) |
e67c25c7 | 2463 | { |
726a989a RB |
2464 | gimple_stmt_iterator gsi = gsi_for_stmt (stmt); |
2465 | ||
2466 | if (gimple_code (stmt) == GIMPLE_PHI) | |
2467 | remove_phi_node (&gsi, true); | |
e67c25c7 JL |
2468 | else |
2469 | { | |
726a989a RB |
2470 | gsi_remove (&gsi, true); |
2471 | release_defs (stmt); | |
e67c25c7 JL |
2472 | } |
2473 | } | |
2474 | ||
726a989a | 2475 | /* Given a statement STMT, which is either a PHI node or an assignment, |
e67c25c7 | 2476 | return the "rhs" of the node, in the case of a non-degenerate |
726a989a | 2477 | phi, NULL is returned. */ |
e67c25c7 JL |
2478 | |
2479 | static tree | |
726a989a | 2480 | get_rhs_or_phi_arg (gimple stmt) |
e67c25c7 | 2481 | { |
726a989a RB |
2482 | if (gimple_code (stmt) == GIMPLE_PHI) |
2483 | return degenerate_phi_result (stmt); | |
2484 | else if (gimple_assign_single_p (stmt)) | |
2485 | return gimple_assign_rhs1 (stmt); | |
2486 | else | |
2487 | gcc_unreachable (); | |
e67c25c7 JL |
2488 | } |
2489 | ||
2490 | ||
726a989a | 2491 | /* Given a statement STMT, which is either a PHI node or an assignment, |
e67c25c7 JL |
2492 | return the "lhs" of the node. */ |
2493 | ||
2494 | static tree | |
726a989a | 2495 | get_lhs_or_phi_result (gimple stmt) |
e67c25c7 | 2496 | { |
726a989a RB |
2497 | if (gimple_code (stmt) == GIMPLE_PHI) |
2498 | return gimple_phi_result (stmt); | |
2499 | else if (is_gimple_assign (stmt)) | |
2500 | return gimple_assign_lhs (stmt); | |
2501 | else | |
2502 | gcc_unreachable (); | |
e67c25c7 JL |
2503 | } |
2504 | ||
2505 | /* Propagate RHS into all uses of LHS (when possible). | |
2506 | ||
2507 | RHS and LHS are derived from STMT, which is passed in solely so | |
2508 | that we can remove it if propagation is successful. | |
2509 | ||
2510 | When propagating into a PHI node or into a statement which turns | |
2511 | into a trivial copy or constant initialization, set the | |
2512 | appropriate bit in INTERESTING_NAMEs so that we will visit those | |
2513 | nodes as well in an effort to pick up secondary optimization | |
2514 | opportunities. */ | |
2515 | ||
b8698a0f | 2516 | static void |
726a989a | 2517 | propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names) |
e67c25c7 JL |
2518 | { |
2519 | /* First verify that propagation is valid and isn't going to move a | |
2520 | loop variant variable outside its loop. */ | |
2521 | if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) | |
2522 | && (TREE_CODE (rhs) != SSA_NAME | |
2523 | || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs)) | |
2524 | && may_propagate_copy (lhs, rhs) | |
2525 | && loop_depth_of_name (lhs) >= loop_depth_of_name (rhs)) | |
2526 | { | |
2527 | use_operand_p use_p; | |
2528 | imm_use_iterator iter; | |
726a989a | 2529 | gimple use_stmt; |
e67c25c7 JL |
2530 | bool all = true; |
2531 | ||
2532 | /* Dump details. */ | |
2533 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2534 | { | |
2535 | fprintf (dump_file, " Replacing '"); | |
2536 | print_generic_expr (dump_file, lhs, dump_flags); | |
2537 | fprintf (dump_file, "' with %s '", | |
2538 | (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable")); | |
2539 | print_generic_expr (dump_file, rhs, dump_flags); | |
2540 | fprintf (dump_file, "'\n"); | |
2541 | } | |
2542 | ||
b8698a0f | 2543 | /* Walk over every use of LHS and try to replace the use with RHS. |
e67c25c7 JL |
2544 | At this point the only reason why such a propagation would not |
2545 | be successful would be if the use occurs in an ASM_EXPR. */ | |
6c00f606 | 2546 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs) |
e67c25c7 | 2547 | { |
b5b8b0ac AO |
2548 | /* Leave debug stmts alone. If we succeed in propagating |
2549 | all non-debug uses, we'll drop the DEF, and propagation | |
2550 | into debug stmts will occur then. */ | |
2551 | if (gimple_debug_bind_p (use_stmt)) | |
2552 | continue; | |
b8698a0f | 2553 | |
e67c25c7 | 2554 | /* It's not always safe to propagate into an ASM_EXPR. */ |
726a989a RB |
2555 | if (gimple_code (use_stmt) == GIMPLE_ASM |
2556 | && ! may_propagate_copy_into_asm (lhs)) | |
e67c25c7 JL |
2557 | { |
2558 | all = false; | |
2559 | continue; | |
2560 | } | |
2561 | ||
720151ca RG |
2562 | /* It's not ok to propagate into the definition stmt of RHS. |
2563 | <bb 9>: | |
2564 | # prephitmp.12_36 = PHI <g_67.1_6(9)> | |
2565 | g_67.1_6 = prephitmp.12_36; | |
2566 | goto <bb 9>; | |
2567 | While this is strictly all dead code we do not want to | |
2568 | deal with this here. */ | |
2569 | if (TREE_CODE (rhs) == SSA_NAME | |
2570 | && SSA_NAME_DEF_STMT (rhs) == use_stmt) | |
2571 | { | |
2572 | all = false; | |
2573 | continue; | |
2574 | } | |
2575 | ||
e67c25c7 JL |
2576 | /* Dump details. */ |
2577 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2578 | { | |
2579 | fprintf (dump_file, " Original statement:"); | |
726a989a | 2580 | print_gimple_stmt (dump_file, use_stmt, 0, dump_flags); |
e67c25c7 JL |
2581 | } |
2582 | ||
cbc75e62 | 2583 | /* Propagate the RHS into this use of the LHS. */ |
6c00f606 AM |
2584 | FOR_EACH_IMM_USE_ON_STMT (use_p, iter) |
2585 | propagate_value (use_p, rhs); | |
cbc75e62 JL |
2586 | |
2587 | /* Special cases to avoid useless calls into the folding | |
2588 | routines, operand scanning, etc. | |
2589 | ||
2590 | First, propagation into a PHI may cause the PHI to become | |
2591 | a degenerate, so mark the PHI as interesting. No other | |
2592 | actions are necessary. | |
2593 | ||
2594 | Second, if we're propagating a virtual operand and the | |
2595 | propagation does not change the underlying _DECL node for | |
2596 | the virtual operand, then no further actions are necessary. */ | |
726a989a | 2597 | if (gimple_code (use_stmt) == GIMPLE_PHI |
cbc75e62 JL |
2598 | || (! is_gimple_reg (lhs) |
2599 | && TREE_CODE (rhs) == SSA_NAME | |
2600 | && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs))) | |
2601 | { | |
2602 | /* Dump details. */ | |
2603 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2604 | { | |
2605 | fprintf (dump_file, " Updated statement:"); | |
726a989a | 2606 | print_gimple_stmt (dump_file, use_stmt, 0, dump_flags); |
cbc75e62 JL |
2607 | } |
2608 | ||
2609 | /* Propagation into a PHI may expose new degenerate PHIs, | |
2610 | so mark the result of the PHI as interesting. */ | |
726a989a | 2611 | if (gimple_code (use_stmt) == GIMPLE_PHI) |
cbc75e62 JL |
2612 | { |
2613 | tree result = get_lhs_or_phi_result (use_stmt); | |
2614 | bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result)); | |
2615 | } | |
cfaab3a9 | 2616 | |
cbc75e62 JL |
2617 | continue; |
2618 | } | |
2619 | ||
b8698a0f | 2620 | /* From this point onward we are propagating into a |
cbc75e62 JL |
2621 | real statement. Folding may (or may not) be possible, |
2622 | we may expose new operands, expose dead EH edges, | |
2623 | etc. */ | |
726a989a RB |
2624 | /* NOTE tuples. In the tuples world, fold_stmt_inplace |
2625 | cannot fold a call that simplifies to a constant, | |
2626 | because the GIMPLE_CALL must be replaced by a | |
2627 | GIMPLE_ASSIGN, and there is no way to effect such a | |
2628 | transformation in-place. We might want to consider | |
2629 | using the more general fold_stmt here. */ | |
e67c25c7 | 2630 | fold_stmt_inplace (use_stmt); |
3ae194cd JL |
2631 | |
2632 | /* Sometimes propagation can expose new operands to the | |
cff4e50d PB |
2633 | renamer. */ |
2634 | update_stmt (use_stmt); | |
e67c25c7 JL |
2635 | |
2636 | /* Dump details. */ | |
2637 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2638 | { | |
2639 | fprintf (dump_file, " Updated statement:"); | |
726a989a | 2640 | print_gimple_stmt (dump_file, use_stmt, 0, dump_flags); |
e67c25c7 JL |
2641 | } |
2642 | ||
e67c25c7 JL |
2643 | /* If we replaced a variable index with a constant, then |
2644 | we would need to update the invariant flag for ADDR_EXPRs. */ | |
726a989a RB |
2645 | if (gimple_assign_single_p (use_stmt) |
2646 | && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR) | |
07beea0d | 2647 | recompute_tree_invariant_for_addr_expr |
726a989a | 2648 | (gimple_assign_rhs1 (use_stmt)); |
e67c25c7 JL |
2649 | |
2650 | /* If we cleaned up EH information from the statement, | |
72922229 | 2651 | mark its containing block as needing EH cleanups. */ |
e67c25c7 | 2652 | if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt)) |
72922229 | 2653 | { |
726a989a | 2654 | bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index); |
72922229 JL |
2655 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2656 | fprintf (dump_file, " Flagged to clear EH edges.\n"); | |
2657 | } | |
e67c25c7 | 2658 | |
cbc75e62 JL |
2659 | /* Propagation may expose new trivial copy/constant propagation |
2660 | opportunities. */ | |
726a989a RB |
2661 | if (gimple_assign_single_p (use_stmt) |
2662 | && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME | |
2663 | && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME | |
2664 | || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt)))) | |
2665 | { | |
e67c25c7 JL |
2666 | tree result = get_lhs_or_phi_result (use_stmt); |
2667 | bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result)); | |
2668 | } | |
2669 | ||
2670 | /* Propagation into these nodes may make certain edges in | |
2671 | the CFG unexecutable. We want to identify them as PHI nodes | |
2672 | at the destination of those unexecutable edges may become | |
2673 | degenerates. */ | |
726a989a RB |
2674 | else if (gimple_code (use_stmt) == GIMPLE_COND |
2675 | || gimple_code (use_stmt) == GIMPLE_SWITCH | |
2676 | || gimple_code (use_stmt) == GIMPLE_GOTO) | |
2677 | { | |
e67c25c7 JL |
2678 | tree val; |
2679 | ||
726a989a | 2680 | if (gimple_code (use_stmt) == GIMPLE_COND) |
db3927fb AH |
2681 | val = fold_binary_loc (gimple_location (use_stmt), |
2682 | gimple_cond_code (use_stmt), | |
726a989a RB |
2683 | boolean_type_node, |
2684 | gimple_cond_lhs (use_stmt), | |
2685 | gimple_cond_rhs (use_stmt)); | |
2686 | else if (gimple_code (use_stmt) == GIMPLE_SWITCH) | |
2687 | val = gimple_switch_index (use_stmt); | |
e67c25c7 | 2688 | else |
726a989a | 2689 | val = gimple_goto_dest (use_stmt); |
e67c25c7 | 2690 | |
726a989a | 2691 | if (val && is_gimple_min_invariant (val)) |
e67c25c7 | 2692 | { |
726a989a | 2693 | basic_block bb = gimple_bb (use_stmt); |
e67c25c7 JL |
2694 | edge te = find_taken_edge (bb, val); |
2695 | edge_iterator ei; | |
2696 | edge e; | |
726a989a | 2697 | gimple_stmt_iterator gsi, psi; |
e67c25c7 JL |
2698 | |
2699 | /* Remove all outgoing edges except TE. */ | |
2700 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));) | |
2701 | { | |
2702 | if (e != te) | |
2703 | { | |
e67c25c7 JL |
2704 | /* Mark all the PHI nodes at the destination of |
2705 | the unexecutable edge as interesting. */ | |
726a989a RB |
2706 | for (psi = gsi_start_phis (e->dest); |
2707 | !gsi_end_p (psi); | |
2708 | gsi_next (&psi)) | |
2709 | { | |
2710 | gimple phi = gsi_stmt (psi); | |
2711 | ||
2712 | tree result = gimple_phi_result (phi); | |
e67c25c7 JL |
2713 | int version = SSA_NAME_VERSION (result); |
2714 | ||
2715 | bitmap_set_bit (interesting_names, version); | |
2716 | } | |
2717 | ||
2718 | te->probability += e->probability; | |
2719 | ||
2720 | te->count += e->count; | |
2721 | remove_edge (e); | |
8d9d6561 | 2722 | cfg_altered = true; |
e67c25c7 JL |
2723 | } |
2724 | else | |
2725 | ei_next (&ei); | |
2726 | } | |
2727 | ||
726a989a RB |
2728 | gsi = gsi_last_bb (gimple_bb (use_stmt)); |
2729 | gsi_remove (&gsi, true); | |
e67c25c7 JL |
2730 | |
2731 | /* And fixup the flags on the single remaining edge. */ | |
2732 | te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE); | |
2733 | te->flags &= ~EDGE_ABNORMAL; | |
2734 | te->flags |= EDGE_FALLTHRU; | |
2735 | if (te->probability > REG_BR_PROB_BASE) | |
2736 | te->probability = REG_BR_PROB_BASE; | |
2737 | } | |
2738 | } | |
2739 | } | |
2740 | ||
b8698a0f | 2741 | /* Ensure there is nothing else to do. */ |
a5f84464 | 2742 | gcc_assert (!all || has_zero_uses (lhs)); |
243cc836 | 2743 | |
e67c25c7 JL |
2744 | /* If we were able to propagate away all uses of LHS, then |
2745 | we can remove STMT. */ | |
2746 | if (all) | |
2747 | remove_stmt_or_phi (stmt); | |
2748 | } | |
2749 | } | |
2750 | ||
726a989a | 2751 | /* STMT is either a PHI node (potentially a degenerate PHI node) or |
e67c25c7 JL |
2752 | a statement that is a trivial copy or constant initialization. |
2753 | ||
2754 | Attempt to eliminate T by propagating its RHS into all uses of | |
2755 | its LHS. This may in turn set new bits in INTERESTING_NAMES | |
2756 | for nodes we want to revisit later. | |
2757 | ||
2758 | All exit paths should clear INTERESTING_NAMES for the result | |
726a989a | 2759 | of STMT. */ |
e67c25c7 JL |
2760 | |
2761 | static void | |
726a989a | 2762 | eliminate_const_or_copy (gimple stmt, bitmap interesting_names) |
e67c25c7 | 2763 | { |
726a989a | 2764 | tree lhs = get_lhs_or_phi_result (stmt); |
e67c25c7 JL |
2765 | tree rhs; |
2766 | int version = SSA_NAME_VERSION (lhs); | |
2767 | ||
2768 | /* If the LHS of this statement or PHI has no uses, then we can | |
2769 | just eliminate it. This can occur if, for example, the PHI | |
2770 | was created by block duplication due to threading and its only | |
2771 | use was in the conditional at the end of the block which was | |
2772 | deleted. */ | |
2773 | if (has_zero_uses (lhs)) | |
2774 | { | |
2775 | bitmap_clear_bit (interesting_names, version); | |
726a989a | 2776 | remove_stmt_or_phi (stmt); |
e67c25c7 JL |
2777 | return; |
2778 | } | |
2779 | ||
2780 | /* Get the RHS of the assignment or PHI node if the PHI is a | |
2781 | degenerate. */ | |
726a989a | 2782 | rhs = get_rhs_or_phi_arg (stmt); |
e67c25c7 JL |
2783 | if (!rhs) |
2784 | { | |
2785 | bitmap_clear_bit (interesting_names, version); | |
2786 | return; | |
2787 | } | |
2788 | ||
726a989a | 2789 | propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names); |
e67c25c7 | 2790 | |
726a989a | 2791 | /* Note that STMT may well have been deleted by now, so do |
e67c25c7 JL |
2792 | not access it, instead use the saved version # to clear |
2793 | T's entry in the worklist. */ | |
2794 | bitmap_clear_bit (interesting_names, version); | |
2795 | } | |
2796 | ||
2797 | /* The first phase in degenerate PHI elimination. | |
2798 | ||
2799 | Eliminate the degenerate PHIs in BB, then recurse on the | |
2800 | dominator children of BB. */ | |
2801 | ||
2802 | static void | |
2803 | eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names) | |
2804 | { | |
726a989a | 2805 | gimple_stmt_iterator gsi; |
e67c25c7 JL |
2806 | basic_block son; |
2807 | ||
726a989a | 2808 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
e67c25c7 | 2809 | { |
726a989a RB |
2810 | gimple phi = gsi_stmt (gsi); |
2811 | ||
e67c25c7 JL |
2812 | eliminate_const_or_copy (phi, interesting_names); |
2813 | } | |
2814 | ||
2815 | /* Recurse into the dominator children of BB. */ | |
2816 | for (son = first_dom_son (CDI_DOMINATORS, bb); | |
2817 | son; | |
2818 | son = next_dom_son (CDI_DOMINATORS, son)) | |
2819 | eliminate_degenerate_phis_1 (son, interesting_names); | |
2820 | } | |
2821 | ||
2822 | ||
2823 | /* A very simple pass to eliminate degenerate PHI nodes from the | |
2824 | IL. This is meant to be fast enough to be able to be run several | |
2825 | times in the optimization pipeline. | |
2826 | ||
2827 | Certain optimizations, particularly those which duplicate blocks | |
2828 | or remove edges from the CFG can create or expose PHIs which are | |
2829 | trivial copies or constant initializations. | |
2830 | ||
2831 | While we could pick up these optimizations in DOM or with the | |
2832 | combination of copy-prop and CCP, those solutions are far too | |
2833 | heavy-weight for our needs. | |
2834 | ||
2835 | This implementation has two phases so that we can efficiently | |
2836 | eliminate the first order degenerate PHIs and second order | |
2837 | degenerate PHIs. | |
2838 | ||
2839 | The first phase performs a dominator walk to identify and eliminate | |
2840 | the vast majority of the degenerate PHIs. When a degenerate PHI | |
2841 | is identified and eliminated any affected statements or PHIs | |
2842 | are put on a worklist. | |
2843 | ||
2844 | The second phase eliminates degenerate PHIs and trivial copies | |
2845 | or constant initializations using the worklist. This is how we | |
2846 | pick up the secondary optimization opportunities with minimal | |
2847 | cost. */ | |
2848 | ||
2849 | static unsigned int | |
2850 | eliminate_degenerate_phis (void) | |
2851 | { | |
2852 | bitmap interesting_names; | |
1f70491b | 2853 | bitmap interesting_names1; |
e67c25c7 | 2854 | |
72922229 JL |
2855 | /* Bitmap of blocks which need EH information updated. We can not |
2856 | update it on-the-fly as doing so invalidates the dominator tree. */ | |
2857 | need_eh_cleanup = BITMAP_ALLOC (NULL); | |
2858 | ||
e67c25c7 JL |
2859 | /* INTERESTING_NAMES is effectively our worklist, indexed by |
2860 | SSA_NAME_VERSION. | |
2861 | ||
2862 | A set bit indicates that the statement or PHI node which | |
2863 | defines the SSA_NAME should be (re)examined to determine if | |
66a4ad37 | 2864 | it has become a degenerate PHI or trivial const/copy propagation |
b8698a0f | 2865 | opportunity. |
e67c25c7 JL |
2866 | |
2867 | Experiments have show we generally get better compilation | |
2868 | time behavior with bitmaps rather than sbitmaps. */ | |
2869 | interesting_names = BITMAP_ALLOC (NULL); | |
1f70491b | 2870 | interesting_names1 = BITMAP_ALLOC (NULL); |
e67c25c7 | 2871 | |
8d9d6561 EB |
2872 | calculate_dominance_info (CDI_DOMINATORS); |
2873 | cfg_altered = false; | |
2874 | ||
917f1b7e | 2875 | /* First phase. Eliminate degenerate PHIs via a dominator |
e67c25c7 JL |
2876 | walk of the CFG. |
2877 | ||
2878 | Experiments have indicated that we generally get better | |
2879 | compile-time behavior by visiting blocks in the first | |
2880 | phase in dominator order. Presumably this is because walking | |
2881 | in dominator order leaves fewer PHIs for later examination | |
2882 | by the worklist phase. */ | |
e67c25c7 JL |
2883 | eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR, interesting_names); |
2884 | ||
917f1b7e | 2885 | /* Second phase. Eliminate second order degenerate PHIs as well |
e67c25c7 JL |
2886 | as trivial copies or constant initializations identified by |
2887 | the first phase or this phase. Basically we keep iterating | |
2888 | until our set of INTERESTING_NAMEs is empty. */ | |
2889 | while (!bitmap_empty_p (interesting_names)) | |
2890 | { | |
2891 | unsigned int i; | |
2892 | bitmap_iterator bi; | |
2893 | ||
1f70491b AP |
2894 | /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap |
2895 | changed during the loop. Copy it to another bitmap and | |
2896 | use that. */ | |
2897 | bitmap_copy (interesting_names1, interesting_names); | |
2898 | ||
2899 | EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi) | |
e67c25c7 JL |
2900 | { |
2901 | tree name = ssa_name (i); | |
2902 | ||
2903 | /* Ignore SSA_NAMEs that have been released because | |
2904 | their defining statement was deleted (unreachable). */ | |
2905 | if (name) | |
2906 | eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)), | |
2907 | interesting_names); | |
2908 | } | |
2909 | } | |
72922229 | 2910 | |
8d9d6561 EB |
2911 | if (cfg_altered) |
2912 | free_dominance_info (CDI_DOMINATORS); | |
2913 | ||
72922229 JL |
2914 | /* Propagation of const and copies may make some EH edges dead. Purge |
2915 | such edges from the CFG as needed. */ | |
2916 | if (!bitmap_empty_p (need_eh_cleanup)) | |
2917 | { | |
726a989a | 2918 | gimple_purge_all_dead_eh_edges (need_eh_cleanup); |
72922229 JL |
2919 | BITMAP_FREE (need_eh_cleanup); |
2920 | } | |
e67c25c7 JL |
2921 | |
2922 | BITMAP_FREE (interesting_names); | |
1f70491b | 2923 | BITMAP_FREE (interesting_names1); |
e67c25c7 JL |
2924 | return 0; |
2925 | } | |
2926 | ||
8ddbbcae | 2927 | struct gimple_opt_pass pass_phi_only_cprop = |
e67c25c7 | 2928 | { |
8ddbbcae JH |
2929 | { |
2930 | GIMPLE_PASS, | |
e67c25c7 JL |
2931 | "phicprop", /* name */ |
2932 | gate_dominator, /* gate */ | |
2933 | eliminate_degenerate_phis, /* execute */ | |
2934 | NULL, /* sub */ | |
2935 | NULL, /* next */ | |
2936 | 0, /* static_pass_number */ | |
b6313dcf | 2937 | TV_TREE_PHI_CPROP, /* tv_id */ |
4effdf02 | 2938 | PROP_cfg | PROP_ssa, /* properties_required */ |
e67c25c7 | 2939 | 0, /* properties_provided */ |
ae07b463 | 2940 | 0, /* properties_destroyed */ |
e67c25c7 | 2941 | 0, /* todo_flags_start */ |
706ca88e | 2942 | TODO_cleanup_cfg |
b8698a0f | 2943 | | TODO_dump_func |
706ca88e DN |
2944 | | TODO_ggc_collect |
2945 | | TODO_verify_ssa | |
2946 | | TODO_verify_stmts | |
8ddbbcae JH |
2947 | | TODO_update_ssa /* todo_flags_finish */ |
2948 | } | |
e67c25c7 | 2949 | }; |