]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Dead code elimination pass for the GNU compiler. |
ad616de1 | 2 | Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Ben Elliston <bje@redhat.com> |
4 | and Andrew MacLeod <amacleod@redhat.com> | |
5 | Adapted to use control dependence by Steven Bosscher, SUSE Labs. | |
6 | ||
7 | This file is part of GCC. | |
8 | ||
9 | GCC is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by the | |
11 | Free Software Foundation; either version 2, or (at your option) any | |
12 | later version. | |
13 | ||
14 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 | for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with GCC; see the file COPYING. If not, write to the Free | |
366ccddb KC |
21 | Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA |
22 | 02110-1301, USA. */ | |
6de9cd9a DN |
23 | |
24 | /* Dead code elimination. | |
25 | ||
26 | References: | |
27 | ||
28 | Building an Optimizing Compiler, | |
29 | Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9. | |
30 | ||
31 | Advanced Compiler Design and Implementation, | |
32 | Steven Muchnick, Morgan Kaufmann, 1997, Section 18.10. | |
33 | ||
34 | Dead-code elimination is the removal of statements which have no | |
35 | impact on the program's output. "Dead statements" have no impact | |
36 | on the program's output, while "necessary statements" may have | |
37 | impact on the output. | |
38 | ||
39 | The algorithm consists of three phases: | |
40 | 1. Marking as necessary all statements known to be necessary, | |
41 | e.g. most function calls, writing a value to memory, etc; | |
42 | 2. Propagating necessary statements, e.g., the statements | |
43 | giving values to operands in necessary statements; and | |
44 | 3. Removing dead statements. */ | |
45 | ||
46 | #include "config.h" | |
47 | #include "system.h" | |
48 | #include "coretypes.h" | |
49 | #include "tm.h" | |
6de9cd9a DN |
50 | #include "ggc.h" |
51 | ||
52 | /* These RTL headers are needed for basic-block.h. */ | |
53 | #include "rtl.h" | |
54 | #include "tm_p.h" | |
55 | #include "hard-reg-set.h" | |
7932a3db | 56 | #include "obstack.h" |
6de9cd9a DN |
57 | #include "basic-block.h" |
58 | ||
59 | #include "tree.h" | |
60 | #include "diagnostic.h" | |
61 | #include "tree-flow.h" | |
eadf906f | 62 | #include "tree-gimple.h" |
6de9cd9a DN |
63 | #include "tree-dump.h" |
64 | #include "tree-pass.h" | |
65 | #include "timevar.h" | |
66 | #include "flags.h" | |
49896738 | 67 | #include "cfgloop.h" |
a4176272 | 68 | #include "tree-scalar-evolution.h" |
6de9cd9a DN |
69 | \f |
70 | static struct stmt_stats | |
71 | { | |
72 | int total; | |
73 | int total_phis; | |
74 | int removed; | |
75 | int removed_phis; | |
76 | } stats; | |
77 | ||
906532aa | 78 | static VEC(tree,heap) *worklist; |
6de9cd9a DN |
79 | |
80 | /* Vector indicating an SSA name has already been processed and marked | |
81 | as necessary. */ | |
82 | static sbitmap processed; | |
83 | ||
84 | /* Vector indicating that last_stmt if a basic block has already been | |
85 | marked as necessary. */ | |
86 | static sbitmap last_stmt_necessary; | |
87 | ||
88 | /* Before we can determine whether a control branch is dead, we need to | |
89 | compute which blocks are control dependent on which edges. | |
90 | ||
91 | We expect each block to be control dependent on very few edges so we | |
92 | use a bitmap for each block recording its edges. An array holds the | |
93 | bitmap. The Ith bit in the bitmap is set if that block is dependent | |
94 | on the Ith edge. */ | |
8c80c4aa | 95 | static bitmap *control_dependence_map; |
6de9cd9a | 96 | |
a28fee03 SB |
97 | /* Vector indicating that a basic block has already had all the edges |
98 | processed that it is control dependent on. */ | |
8c80c4aa | 99 | static sbitmap visited_control_parents; |
a28fee03 | 100 | |
9da4058c JL |
101 | /* TRUE if this pass alters the CFG (by removing control statements). |
102 | FALSE otherwise. | |
103 | ||
104 | If this pass alters the CFG, then it will arrange for the dominators | |
105 | to be recomputed. */ | |
106 | static bool cfg_altered; | |
107 | ||
6de9cd9a DN |
108 | /* Execute CODE for each edge (given number EDGE_NUMBER within the CODE) |
109 | for which the block with index N is control dependent. */ | |
110 | #define EXECUTE_IF_CONTROL_DEPENDENT(N, EDGE_NUMBER, CODE) \ | |
87c476a2 ZD |
111 | { \ |
112 | bitmap_iterator bi; \ | |
113 | \ | |
114 | EXECUTE_IF_SET_IN_BITMAP (control_dependence_map[N], 0, EDGE_NUMBER, bi) \ | |
115 | { \ | |
116 | CODE; \ | |
117 | } \ | |
118 | } | |
6de9cd9a DN |
119 | |
120 | /* Local function prototypes. */ | |
121 | static inline void set_control_dependence_map_bit (basic_block, int); | |
122 | static inline void clear_control_dependence_bitmap (basic_block); | |
123 | static void find_all_control_dependences (struct edge_list *); | |
124 | static void find_control_dependence (struct edge_list *, int); | |
125 | static inline basic_block find_pdom (basic_block); | |
126 | ||
127 | static inline void mark_stmt_necessary (tree, bool); | |
52328bf6 | 128 | static inline void mark_operand_necessary (tree, bool); |
6de9cd9a | 129 | |
6de9cd9a DN |
130 | static void mark_stmt_if_obviously_necessary (tree, bool); |
131 | static void find_obviously_necessary_stmts (struct edge_list *); | |
132 | ||
133 | static void mark_control_dependent_edges_necessary (basic_block, struct edge_list *); | |
134 | static void propagate_necessity (struct edge_list *); | |
135 | ||
136 | static void eliminate_unnecessary_stmts (void); | |
137 | static void remove_dead_phis (basic_block); | |
138 | static void remove_dead_stmt (block_stmt_iterator *, basic_block); | |
139 | ||
140 | static void print_stats (void); | |
141 | static void tree_dce_init (bool); | |
142 | static void tree_dce_done (bool); | |
143 | \f | |
144 | /* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */ | |
145 | static inline void | |
146 | set_control_dependence_map_bit (basic_block bb, int edge_index) | |
147 | { | |
148 | if (bb == ENTRY_BLOCK_PTR) | |
149 | return; | |
1e128c5f | 150 | gcc_assert (bb != EXIT_BLOCK_PTR); |
6de9cd9a DN |
151 | bitmap_set_bit (control_dependence_map[bb->index], edge_index); |
152 | } | |
153 | ||
154 | /* Clear all control dependences for block BB. */ | |
155 | static inline | |
156 | void clear_control_dependence_bitmap (basic_block bb) | |
157 | { | |
158 | bitmap_clear (control_dependence_map[bb->index]); | |
159 | } | |
160 | ||
161 | /* Record all blocks' control dependences on all edges in the edge | |
162 | list EL, ala Morgan, Section 3.6. */ | |
163 | ||
164 | static void | |
165 | find_all_control_dependences (struct edge_list *el) | |
166 | { | |
167 | int i; | |
168 | ||
169 | for (i = 0; i < NUM_EDGES (el); ++i) | |
170 | find_control_dependence (el, i); | |
171 | } | |
172 | ||
173 | /* Determine all blocks' control dependences on the given edge with edge_list | |
174 | EL index EDGE_INDEX, ala Morgan, Section 3.6. */ | |
175 | ||
176 | static void | |
177 | find_control_dependence (struct edge_list *el, int edge_index) | |
178 | { | |
179 | basic_block current_block; | |
180 | basic_block ending_block; | |
181 | ||
1e128c5f | 182 | gcc_assert (INDEX_EDGE_PRED_BB (el, edge_index) != EXIT_BLOCK_PTR); |
6de9cd9a DN |
183 | |
184 | if (INDEX_EDGE_PRED_BB (el, edge_index) == ENTRY_BLOCK_PTR) | |
953ff289 | 185 | ending_block = single_succ (ENTRY_BLOCK_PTR); |
6de9cd9a DN |
186 | else |
187 | ending_block = find_pdom (INDEX_EDGE_PRED_BB (el, edge_index)); | |
188 | ||
189 | for (current_block = INDEX_EDGE_SUCC_BB (el, edge_index); | |
190 | current_block != ending_block && current_block != EXIT_BLOCK_PTR; | |
191 | current_block = find_pdom (current_block)) | |
192 | { | |
193 | edge e = INDEX_EDGE (el, edge_index); | |
194 | ||
195 | /* For abnormal edges, we don't make current_block control | |
196 | dependent because instructions that throw are always necessary | |
197 | anyway. */ | |
198 | if (e->flags & EDGE_ABNORMAL) | |
199 | continue; | |
200 | ||
201 | set_control_dependence_map_bit (current_block, edge_index); | |
202 | } | |
203 | } | |
204 | ||
205 | /* Find the immediate postdominator PDOM of the specified basic block BLOCK. | |
206 | This function is necessary because some blocks have negative numbers. */ | |
207 | ||
208 | static inline basic_block | |
209 | find_pdom (basic_block block) | |
210 | { | |
1e128c5f GB |
211 | gcc_assert (block != ENTRY_BLOCK_PTR); |
212 | ||
213 | if (block == EXIT_BLOCK_PTR) | |
6de9cd9a DN |
214 | return EXIT_BLOCK_PTR; |
215 | else | |
216 | { | |
217 | basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block); | |
218 | if (! bb) | |
219 | return EXIT_BLOCK_PTR; | |
220 | return bb; | |
221 | } | |
222 | } | |
223 | \f | |
224 | #define NECESSARY(stmt) stmt->common.asm_written_flag | |
225 | ||
226 | /* If STMT is not already marked necessary, mark it, and add it to the | |
227 | worklist if ADD_TO_WORKLIST is true. */ | |
228 | static inline void | |
229 | mark_stmt_necessary (tree stmt, bool add_to_worklist) | |
230 | { | |
1e128c5f | 231 | gcc_assert (stmt); |
1e128c5f | 232 | gcc_assert (!DECL_P (stmt)); |
6de9cd9a DN |
233 | |
234 | if (NECESSARY (stmt)) | |
235 | return; | |
236 | ||
237 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
238 | { | |
239 | fprintf (dump_file, "Marking useful stmt: "); | |
240 | print_generic_stmt (dump_file, stmt, TDF_SLIM); | |
241 | fprintf (dump_file, "\n"); | |
242 | } | |
243 | ||
244 | NECESSARY (stmt) = 1; | |
245 | if (add_to_worklist) | |
906532aa | 246 | VEC_safe_push (tree, heap, worklist, stmt); |
6de9cd9a DN |
247 | } |
248 | ||
52328bf6 DB |
249 | /* Mark the statement defining operand OP as necessary. PHIONLY is true |
250 | if we should only mark it necessary if it is a phi node. */ | |
6de9cd9a DN |
251 | |
252 | static inline void | |
52328bf6 | 253 | mark_operand_necessary (tree op, bool phionly) |
6de9cd9a DN |
254 | { |
255 | tree stmt; | |
256 | int ver; | |
257 | ||
1e128c5f | 258 | gcc_assert (op); |
6de9cd9a DN |
259 | |
260 | ver = SSA_NAME_VERSION (op); | |
261 | if (TEST_BIT (processed, ver)) | |
262 | return; | |
263 | SET_BIT (processed, ver); | |
264 | ||
265 | stmt = SSA_NAME_DEF_STMT (op); | |
1e128c5f | 266 | gcc_assert (stmt); |
6de9cd9a DN |
267 | |
268 | if (NECESSARY (stmt) | |
52328bf6 DB |
269 | || IS_EMPTY_STMT (stmt) |
270 | || (phionly && TREE_CODE (stmt) != PHI_NODE)) | |
6de9cd9a DN |
271 | return; |
272 | ||
273 | NECESSARY (stmt) = 1; | |
906532aa | 274 | VEC_safe_push (tree, heap, worklist, stmt); |
6de9cd9a DN |
275 | } |
276 | \f | |
6de9cd9a | 277 | |
adb35797 | 278 | /* Mark STMT as necessary if it obviously is. Add it to the worklist if |
6de9cd9a DN |
279 | it can make other statements necessary. |
280 | ||
281 | If AGGRESSIVE is false, control statements are conservatively marked as | |
282 | necessary. */ | |
283 | ||
284 | static void | |
285 | mark_stmt_if_obviously_necessary (tree stmt, bool aggressive) | |
286 | { | |
6de9cd9a | 287 | stmt_ann_t ann; |
4c124b4c AM |
288 | tree op, def; |
289 | ssa_op_iter iter; | |
6de9cd9a | 290 | |
75b80166 SB |
291 | /* With non-call exceptions, we have to assume that all statements could |
292 | throw. If a statement may throw, it is inherently necessary. */ | |
293 | if (flag_non_call_exceptions | |
294 | && tree_could_throw_p (stmt)) | |
295 | { | |
296 | mark_stmt_necessary (stmt, true); | |
297 | return; | |
298 | } | |
299 | ||
6de9cd9a DN |
300 | /* Statements that are implicitly live. Most function calls, asm and return |
301 | statements are required. Labels and BIND_EXPR nodes are kept because | |
302 | they are control flow, and we have no way of knowing whether they can be | |
303 | removed. DCE can eliminate all the other statements in a block, and CFG | |
304 | can then remove the block and labels. */ | |
305 | switch (TREE_CODE (stmt)) | |
306 | { | |
307 | case BIND_EXPR: | |
308 | case LABEL_EXPR: | |
309 | case CASE_LABEL_EXPR: | |
310 | mark_stmt_necessary (stmt, false); | |
311 | return; | |
312 | ||
313 | case ASM_EXPR: | |
314 | case RESX_EXPR: | |
315 | case RETURN_EXPR: | |
316 | mark_stmt_necessary (stmt, true); | |
317 | return; | |
318 | ||
319 | case CALL_EXPR: | |
320 | /* Most, but not all function calls are required. Function calls that | |
321 | produce no result and have no side effects (i.e. const pure | |
322 | functions) are unnecessary. */ | |
323 | if (TREE_SIDE_EFFECTS (stmt)) | |
324 | mark_stmt_necessary (stmt, true); | |
325 | return; | |
326 | ||
327 | case MODIFY_EXPR: | |
cd709752 RH |
328 | op = get_call_expr_in (stmt); |
329 | if (op && TREE_SIDE_EFFECTS (op)) | |
6de9cd9a DN |
330 | { |
331 | mark_stmt_necessary (stmt, true); | |
332 | return; | |
333 | } | |
334 | ||
335 | /* These values are mildly magic bits of the EH runtime. We can't | |
336 | see the entire lifetime of these values until landing pads are | |
337 | generated. */ | |
338 | if (TREE_CODE (TREE_OPERAND (stmt, 0)) == EXC_PTR_EXPR | |
339 | || TREE_CODE (TREE_OPERAND (stmt, 0)) == FILTER_EXPR) | |
340 | { | |
341 | mark_stmt_necessary (stmt, true); | |
342 | return; | |
343 | } | |
344 | break; | |
345 | ||
346 | case GOTO_EXPR: | |
7f604986 KH |
347 | gcc_assert (!simple_goto_p (stmt)); |
348 | mark_stmt_necessary (stmt, true); | |
6de9cd9a DN |
349 | return; |
350 | ||
351 | case COND_EXPR: | |
269da1e9 | 352 | gcc_assert (EDGE_COUNT (bb_for_stmt (stmt)->succs) == 2); |
6de9cd9a DN |
353 | /* Fall through. */ |
354 | ||
355 | case SWITCH_EXPR: | |
356 | if (! aggressive) | |
357 | mark_stmt_necessary (stmt, true); | |
358 | break; | |
359 | ||
360 | default: | |
361 | break; | |
362 | } | |
363 | ||
364 | ann = stmt_ann (stmt); | |
c597ef4e DN |
365 | |
366 | /* If the statement has volatile operands, it needs to be preserved. | |
367 | Same for statements that can alter control flow in unpredictable | |
368 | ways. */ | |
369 | if (ann->has_volatile_ops || is_ctrl_altering_stmt (stmt)) | |
6de9cd9a DN |
370 | { |
371 | mark_stmt_necessary (stmt, true); | |
372 | return; | |
373 | } | |
374 | ||
4c124b4c | 375 | FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF) |
6de9cd9a | 376 | { |
c597ef4e | 377 | if (is_global_var (SSA_NAME_VAR (def))) |
6de9cd9a DN |
378 | { |
379 | mark_stmt_necessary (stmt, true); | |
380 | return; | |
381 | } | |
382 | } | |
fa555252 | 383 | if (is_hidden_global_store (stmt)) |
a32b97a2 | 384 | { |
fa555252 DB |
385 | mark_stmt_necessary (stmt, true); |
386 | return; | |
6de9cd9a DN |
387 | } |
388 | ||
389 | return; | |
390 | } | |
391 | \f | |
392 | /* Find obviously necessary statements. These are things like most function | |
393 | calls, and stores to file level variables. | |
394 | ||
395 | If EL is NULL, control statements are conservatively marked as | |
396 | necessary. Otherwise it contains the list of edges used by control | |
397 | dependence analysis. */ | |
398 | ||
399 | static void | |
400 | find_obviously_necessary_stmts (struct edge_list *el) | |
401 | { | |
402 | basic_block bb; | |
403 | block_stmt_iterator i; | |
404 | edge e; | |
405 | ||
406 | FOR_EACH_BB (bb) | |
407 | { | |
408 | tree phi; | |
409 | ||
410 | /* Check any PHI nodes in the block. */ | |
17192884 | 411 | for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) |
6de9cd9a DN |
412 | { |
413 | NECESSARY (phi) = 0; | |
414 | ||
415 | /* PHIs for virtual variables do not directly affect code | |
416 | generation and need not be considered inherently necessary | |
417 | regardless of the bits set in their decl. | |
418 | ||
419 | Thus, we only need to mark PHIs for real variables which | |
420 | need their result preserved as being inherently necessary. */ | |
421 | if (is_gimple_reg (PHI_RESULT (phi)) | |
c597ef4e | 422 | && is_global_var (SSA_NAME_VAR (PHI_RESULT (phi)))) |
6de9cd9a DN |
423 | mark_stmt_necessary (phi, true); |
424 | } | |
425 | ||
426 | /* Check all statements in the block. */ | |
427 | for (i = bsi_start (bb); ! bsi_end_p (i); bsi_next (&i)) | |
428 | { | |
429 | tree stmt = bsi_stmt (i); | |
430 | NECESSARY (stmt) = 0; | |
431 | mark_stmt_if_obviously_necessary (stmt, el != NULL); | |
432 | } | |
6de9cd9a DN |
433 | } |
434 | ||
435 | if (el) | |
436 | { | |
437 | /* Prevent the loops from being removed. We must keep the infinite loops, | |
438 | and we currently do not have a means to recognize the finite ones. */ | |
439 | FOR_EACH_BB (bb) | |
440 | { | |
628f6a4e BE |
441 | edge_iterator ei; |
442 | FOR_EACH_EDGE (e, ei, bb->succs) | |
6de9cd9a DN |
443 | if (e->flags & EDGE_DFS_BACK) |
444 | mark_control_dependent_edges_necessary (e->dest, el); | |
445 | } | |
446 | } | |
447 | } | |
448 | \f | |
449 | /* Make corresponding control dependent edges necessary. We only | |
450 | have to do this once for each basic block, so we clear the bitmap | |
451 | after we're done. */ | |
452 | static void | |
453 | mark_control_dependent_edges_necessary (basic_block bb, struct edge_list *el) | |
454 | { | |
3cd8c58a | 455 | unsigned edge_number; |
6de9cd9a | 456 | |
1e128c5f | 457 | gcc_assert (bb != EXIT_BLOCK_PTR); |
7e6eb623 DB |
458 | |
459 | if (bb == ENTRY_BLOCK_PTR) | |
460 | return; | |
461 | ||
6de9cd9a DN |
462 | EXECUTE_IF_CONTROL_DEPENDENT (bb->index, edge_number, |
463 | { | |
464 | tree t; | |
465 | basic_block cd_bb = INDEX_EDGE_PRED_BB (el, edge_number); | |
466 | ||
467 | if (TEST_BIT (last_stmt_necessary, cd_bb->index)) | |
468 | continue; | |
469 | SET_BIT (last_stmt_necessary, cd_bb->index); | |
470 | ||
471 | t = last_stmt (cd_bb); | |
1eaba2f2 | 472 | if (t && is_ctrl_stmt (t)) |
6de9cd9a DN |
473 | mark_stmt_necessary (t, true); |
474 | }); | |
475 | } | |
476 | \f | |
477 | /* Propagate necessity using the operands of necessary statements. Process | |
478 | the uses on each statement in the worklist, and add all feeding statements | |
479 | which contribute to the calculation of this value to the worklist. | |
480 | ||
481 | In conservative mode, EL is NULL. */ | |
482 | ||
483 | static void | |
484 | propagate_necessity (struct edge_list *el) | |
485 | { | |
486 | tree i; | |
487 | bool aggressive = (el ? true : false); | |
488 | ||
489 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
490 | fprintf (dump_file, "\nProcessing worklist:\n"); | |
491 | ||
906532aa | 492 | while (VEC_length (tree, worklist) > 0) |
6de9cd9a DN |
493 | { |
494 | /* Take `i' from worklist. */ | |
906532aa | 495 | i = VEC_pop (tree, worklist); |
6de9cd9a DN |
496 | |
497 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
498 | { | |
499 | fprintf (dump_file, "processing: "); | |
500 | print_generic_stmt (dump_file, i, TDF_SLIM); | |
501 | fprintf (dump_file, "\n"); | |
502 | } | |
503 | ||
504 | if (aggressive) | |
505 | { | |
506 | /* Mark the last statements of the basic blocks that the block | |
507 | containing `i' is control dependent on, but only if we haven't | |
508 | already done so. */ | |
509 | basic_block bb = bb_for_stmt (i); | |
a28fee03 SB |
510 | if (bb != ENTRY_BLOCK_PTR |
511 | && ! TEST_BIT (visited_control_parents, bb->index)) | |
6de9cd9a | 512 | { |
a28fee03 | 513 | SET_BIT (visited_control_parents, bb->index); |
6de9cd9a DN |
514 | mark_control_dependent_edges_necessary (bb, el); |
515 | } | |
516 | } | |
517 | ||
518 | if (TREE_CODE (i) == PHI_NODE) | |
519 | { | |
520 | /* PHI nodes are somewhat special in that each PHI alternative has | |
521 | data and control dependencies. All the statements feeding the | |
522 | PHI node's arguments are always necessary. In aggressive mode, | |
523 | we also consider the control dependent edges leading to the | |
524 | predecessor block associated with each PHI alternative as | |
525 | necessary. */ | |
526 | int k; | |
527 | for (k = 0; k < PHI_NUM_ARGS (i); k++) | |
528 | { | |
529 | tree arg = PHI_ARG_DEF (i, k); | |
530 | if (TREE_CODE (arg) == SSA_NAME) | |
52328bf6 | 531 | mark_operand_necessary (arg, false); |
6de9cd9a DN |
532 | } |
533 | ||
534 | if (aggressive) | |
535 | { | |
536 | for (k = 0; k < PHI_NUM_ARGS (i); k++) | |
537 | { | |
538 | basic_block arg_bb = PHI_ARG_EDGE (i, k)->src; | |
a28fee03 SB |
539 | if (arg_bb != ENTRY_BLOCK_PTR |
540 | && ! TEST_BIT (visited_control_parents, arg_bb->index)) | |
6de9cd9a | 541 | { |
a28fee03 | 542 | SET_BIT (visited_control_parents, arg_bb->index); |
6de9cd9a DN |
543 | mark_control_dependent_edges_necessary (arg_bb, el); |
544 | } | |
545 | } | |
546 | } | |
547 | } | |
548 | else | |
549 | { | |
550 | /* Propagate through the operands. Examine all the USE, VUSE and | |
a32b97a2 BB |
551 | V_MAY_DEF operands in this statement. Mark all the statements |
552 | which feed this statement's uses as necessary. */ | |
4c124b4c AM |
553 | ssa_op_iter iter; |
554 | tree use; | |
6de9cd9a | 555 | |
a32b97a2 | 556 | /* The operands of V_MAY_DEF expressions are also needed as they |
6de9cd9a | 557 | represent potential definitions that may reach this |
a32b97a2 BB |
558 | statement (V_MAY_DEF operands allow us to follow def-def |
559 | links). */ | |
4c124b4c AM |
560 | |
561 | FOR_EACH_SSA_TREE_OPERAND (use, i, iter, SSA_OP_ALL_USES) | |
52328bf6 | 562 | mark_operand_necessary (use, false); |
6de9cd9a DN |
563 | } |
564 | } | |
565 | } | |
52328bf6 DB |
566 | |
567 | ||
568 | /* Propagate necessity around virtual phi nodes used in kill operands. | |
569 | The reason this isn't done during propagate_necessity is because we don't | |
570 | want to keep phis around that are just there for must-defs, unless we | |
571 | absolutely have to. After we've rewritten the reaching definitions to be | |
572 | correct in the previous part of the fixup routine, we can simply propagate | |
573 | around the information about which of these virtual phi nodes are really | |
574 | used, and set the NECESSARY flag accordingly. | |
575 | Note that we do the minimum here to ensure that we keep alive the phis that | |
576 | are actually used in the corrected SSA form. In particular, some of these | |
577 | phis may now have all of the same operand, and will be deleted by some | |
578 | other pass. */ | |
579 | ||
580 | static void | |
581 | mark_really_necessary_kill_operand_phis (void) | |
582 | { | |
583 | basic_block bb; | |
584 | int i; | |
585 | ||
586 | /* Seed the worklist with the new virtual phi arguments and virtual | |
587 | uses */ | |
588 | FOR_EACH_BB (bb) | |
589 | { | |
590 | block_stmt_iterator bsi; | |
591 | tree phi; | |
592 | ||
593 | for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) | |
594 | { | |
595 | if (!is_gimple_reg (PHI_RESULT (phi)) && NECESSARY (phi)) | |
596 | { | |
597 | for (i = 0; i < PHI_NUM_ARGS (phi); i++) | |
598 | mark_operand_necessary (PHI_ARG_DEF (phi, i), true); | |
599 | } | |
600 | } | |
601 | ||
602 | for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi)) | |
603 | { | |
604 | tree stmt = bsi_stmt (bsi); | |
605 | ||
606 | if (NECESSARY (stmt)) | |
607 | { | |
608 | use_operand_p use_p; | |
609 | ssa_op_iter iter; | |
610 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, | |
611 | SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_KILLS) | |
612 | { | |
613 | tree use = USE_FROM_PTR (use_p); | |
614 | mark_operand_necessary (use, true); | |
615 | } | |
616 | } | |
617 | } | |
618 | } | |
619 | ||
620 | /* Mark all virtual phis still in use as necessary, and all of their | |
621 | arguments that are phis as necessary. */ | |
906532aa | 622 | while (VEC_length (tree, worklist) > 0) |
52328bf6 | 623 | { |
906532aa | 624 | tree use = VEC_pop (tree, worklist); |
52328bf6 DB |
625 | |
626 | for (i = 0; i < PHI_NUM_ARGS (use); i++) | |
627 | mark_operand_necessary (PHI_ARG_DEF (use, i), true); | |
628 | } | |
629 | } | |
630 | ||
631 | ||
6de9cd9a | 632 | \f |
52328bf6 | 633 | |
6de9cd9a DN |
634 | /* Eliminate unnecessary statements. Any instruction not marked as necessary |
635 | contributes nothing to the program, and can be deleted. */ | |
636 | ||
637 | static void | |
638 | eliminate_unnecessary_stmts (void) | |
639 | { | |
640 | basic_block bb; | |
641 | block_stmt_iterator i; | |
642 | ||
643 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
644 | fprintf (dump_file, "\nEliminating unnecessary statements:\n"); | |
52328bf6 | 645 | |
6de9cd9a DN |
646 | clear_special_calls (); |
647 | FOR_EACH_BB (bb) | |
648 | { | |
649 | /* Remove dead PHI nodes. */ | |
650 | remove_dead_phis (bb); | |
e18d4a19 | 651 | } |
6de9cd9a | 652 | |
e18d4a19 AO |
653 | FOR_EACH_BB (bb) |
654 | { | |
6de9cd9a DN |
655 | /* Remove dead statements. */ |
656 | for (i = bsi_start (bb); ! bsi_end_p (i) ; ) | |
657 | { | |
52328bf6 DB |
658 | tree t = bsi_stmt (i); |
659 | ||
660 | stats.total++; | |
661 | ||
662 | /* If `i' is not necessary then remove it. */ | |
663 | if (! NECESSARY (t)) | |
664 | remove_dead_stmt (&i, bb); | |
665 | else | |
666 | { | |
667 | tree call = get_call_expr_in (t); | |
668 | if (call) | |
669 | notice_special_calls (call); | |
670 | bsi_next (&i); | |
671 | } | |
6de9cd9a DN |
672 | } |
673 | } | |
52328bf6 | 674 | } |
6de9cd9a DN |
675 | \f |
676 | /* Remove dead PHI nodes from block BB. */ | |
677 | ||
678 | static void | |
679 | remove_dead_phis (basic_block bb) | |
680 | { | |
681 | tree prev, phi; | |
682 | ||
683 | prev = NULL_TREE; | |
684 | phi = phi_nodes (bb); | |
685 | while (phi) | |
686 | { | |
687 | stats.total_phis++; | |
688 | ||
689 | if (! NECESSARY (phi)) | |
690 | { | |
17192884 | 691 | tree next = PHI_CHAIN (phi); |
6de9cd9a DN |
692 | |
693 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
694 | { | |
695 | fprintf (dump_file, "Deleting : "); | |
696 | print_generic_stmt (dump_file, phi, TDF_SLIM); | |
697 | fprintf (dump_file, "\n"); | |
698 | } | |
699 | ||
d19e3ef6 | 700 | remove_phi_node (phi, prev); |
6de9cd9a DN |
701 | stats.removed_phis++; |
702 | phi = next; | |
703 | } | |
704 | else | |
705 | { | |
706 | prev = phi; | |
17192884 | 707 | phi = PHI_CHAIN (phi); |
6de9cd9a DN |
708 | } |
709 | } | |
710 | } | |
711 | \f | |
206048bd | 712 | /* Remove dead statement pointed to by iterator I. Receives the basic block BB |
6de9cd9a DN |
713 | containing I so that we don't have to look it up. */ |
714 | ||
715 | static void | |
716 | remove_dead_stmt (block_stmt_iterator *i, basic_block bb) | |
717 | { | |
718 | tree t = bsi_stmt (*i); | |
52328bf6 DB |
719 | def_operand_p def_p; |
720 | ||
721 | ssa_op_iter iter; | |
6de9cd9a DN |
722 | |
723 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
724 | { | |
725 | fprintf (dump_file, "Deleting : "); | |
726 | print_generic_stmt (dump_file, t, TDF_SLIM); | |
727 | fprintf (dump_file, "\n"); | |
728 | } | |
729 | ||
730 | stats.removed++; | |
731 | ||
732 | /* If we have determined that a conditional branch statement contributes | |
733 | nothing to the program, then we not only remove it, but we also change | |
734 | the flow graph so that the current block will simply fall-thru to its | |
735 | immediate post-dominator. The blocks we are circumventing will be | |
32cd8777 | 736 | removed by cleaup_tree_cfg if this change in the flow graph makes them |
6de9cd9a DN |
737 | unreachable. */ |
738 | if (is_ctrl_stmt (t)) | |
739 | { | |
740 | basic_block post_dom_bb; | |
e18d4a19 | 741 | |
6de9cd9a | 742 | /* The post dominance info has to be up-to-date. */ |
1e128c5f | 743 | gcc_assert (dom_computed[CDI_POST_DOMINATORS] == DOM_OK); |
6de9cd9a DN |
744 | /* Get the immediate post dominator of bb. */ |
745 | post_dom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb); | |
746 | /* Some blocks don't have an immediate post dominator. This can happen | |
747 | for example with infinite loops. Removing an infinite loop is an | |
748 | inappropriate transformation anyway... */ | |
749 | if (! post_dom_bb) | |
750 | { | |
751 | bsi_next (i); | |
752 | return; | |
753 | } | |
754 | ||
e18d4a19 AO |
755 | /* If the post dominator block has PHI nodes, we might be unable |
756 | to compute the right PHI args for them. Since the control | |
757 | statement is unnecessary, all edges can be regarded as | |
758 | equivalent, but we have to get rid of the condition, since it | |
759 | might reference a variable that was determined to be | |
760 | unnecessary and thus removed. */ | |
761 | if (phi_nodes (post_dom_bb)) | |
762 | post_dom_bb = EDGE_SUCC (bb, 0)->dest; | |
763 | else | |
764 | { | |
765 | /* Redirect the first edge out of BB to reach POST_DOM_BB. */ | |
766 | redirect_edge_and_branch (EDGE_SUCC (bb, 0), post_dom_bb); | |
767 | PENDING_STMT (EDGE_SUCC (bb, 0)) = NULL; | |
768 | } | |
628f6a4e BE |
769 | EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE; |
770 | EDGE_SUCC (bb, 0)->count = bb->count; | |
6de9cd9a DN |
771 | |
772 | /* The edge is no longer associated with a conditional, so it does | |
773 | not have TRUE/FALSE flags. */ | |
628f6a4e | 774 | EDGE_SUCC (bb, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE); |
6de9cd9a DN |
775 | |
776 | /* If the edge reaches any block other than the exit, then it is a | |
777 | fallthru edge; if it reaches the exit, then it is not a fallthru | |
778 | edge. */ | |
779 | if (post_dom_bb != EXIT_BLOCK_PTR) | |
628f6a4e | 780 | EDGE_SUCC (bb, 0)->flags |= EDGE_FALLTHRU; |
6de9cd9a | 781 | else |
628f6a4e | 782 | EDGE_SUCC (bb, 0)->flags &= ~EDGE_FALLTHRU; |
6de9cd9a DN |
783 | |
784 | /* Remove the remaining the outgoing edges. */ | |
c5cbcccf | 785 | while (!single_succ_p (bb)) |
9da4058c JL |
786 | { |
787 | /* FIXME. When we remove the edge, we modify the CFG, which | |
788 | in turn modifies the dominator and post-dominator tree. | |
789 | Is it safe to postpone recomputing the dominator and | |
790 | post-dominator tree until the end of this pass given that | |
791 | the post-dominators are used above? */ | |
792 | cfg_altered = true; | |
793 | remove_edge (EDGE_SUCC (bb, 1)); | |
794 | } | |
6de9cd9a | 795 | } |
52328bf6 | 796 | |
66d3fe47 | 797 | FOR_EACH_SSA_DEF_OPERAND (def_p, t, iter, SSA_OP_VIRTUAL_DEFS) |
52328bf6 DB |
798 | { |
799 | tree def = DEF_FROM_PTR (def_p); | |
0bca51f0 | 800 | mark_sym_for_renaming (SSA_NAME_VAR (def)); |
52328bf6 | 801 | } |
736432ee | 802 | bsi_remove (i, true); |
52328bf6 | 803 | release_defs (t); |
6de9cd9a DN |
804 | } |
805 | \f | |
806 | /* Print out removed statement statistics. */ | |
807 | ||
808 | static void | |
809 | print_stats (void) | |
810 | { | |
811 | if (dump_file && (dump_flags & (TDF_STATS|TDF_DETAILS))) | |
812 | { | |
813 | float percg; | |
814 | ||
815 | percg = ((float) stats.removed / (float) stats.total) * 100; | |
816 | fprintf (dump_file, "Removed %d of %d statements (%d%%)\n", | |
817 | stats.removed, stats.total, (int) percg); | |
818 | ||
819 | if (stats.total_phis == 0) | |
820 | percg = 0; | |
821 | else | |
822 | percg = ((float) stats.removed_phis / (float) stats.total_phis) * 100; | |
823 | ||
824 | fprintf (dump_file, "Removed %d of %d PHI nodes (%d%%)\n", | |
825 | stats.removed_phis, stats.total_phis, (int) percg); | |
826 | } | |
827 | } | |
828 | \f | |
829 | /* Initialization for this pass. Set up the used data structures. */ | |
830 | ||
831 | static void | |
832 | tree_dce_init (bool aggressive) | |
833 | { | |
834 | memset ((void *) &stats, 0, sizeof (stats)); | |
835 | ||
836 | if (aggressive) | |
837 | { | |
838 | int i; | |
839 | ||
e1111e8e | 840 | control_dependence_map = XNEWVEC (bitmap, last_basic_block); |
6de9cd9a | 841 | for (i = 0; i < last_basic_block; ++i) |
8bdbfff5 | 842 | control_dependence_map[i] = BITMAP_ALLOC (NULL); |
6de9cd9a DN |
843 | |
844 | last_stmt_necessary = sbitmap_alloc (last_basic_block); | |
845 | sbitmap_zero (last_stmt_necessary); | |
846 | } | |
847 | ||
95a3742c | 848 | processed = sbitmap_alloc (num_ssa_names + 1); |
6de9cd9a DN |
849 | sbitmap_zero (processed); |
850 | ||
906532aa | 851 | worklist = VEC_alloc (tree, heap, 64); |
9da4058c | 852 | cfg_altered = false; |
6de9cd9a DN |
853 | } |
854 | ||
855 | /* Cleanup after this pass. */ | |
856 | ||
857 | static void | |
858 | tree_dce_done (bool aggressive) | |
859 | { | |
860 | if (aggressive) | |
861 | { | |
862 | int i; | |
863 | ||
864 | for (i = 0; i < last_basic_block; ++i) | |
8bdbfff5 | 865 | BITMAP_FREE (control_dependence_map[i]); |
6de9cd9a DN |
866 | free (control_dependence_map); |
867 | ||
a28fee03 | 868 | sbitmap_free (visited_control_parents); |
6de9cd9a DN |
869 | sbitmap_free (last_stmt_necessary); |
870 | } | |
871 | ||
872 | sbitmap_free (processed); | |
906532aa KH |
873 | |
874 | VEC_free (tree, heap, worklist); | |
6de9cd9a DN |
875 | } |
876 | \f | |
877 | /* Main routine to eliminate dead code. | |
878 | ||
879 | AGGRESSIVE controls the aggressiveness of the algorithm. | |
880 | In conservative mode, we ignore control dependence and simply declare | |
881 | all but the most trivially dead branches necessary. This mode is fast. | |
882 | In aggressive mode, control dependences are taken into account, which | |
883 | results in more dead code elimination, but at the cost of some time. | |
884 | ||
885 | FIXME: Aggressive mode before PRE doesn't work currently because | |
886 | the dominance info is not invalidated after DCE1. This is | |
887 | not an issue right now because we only run aggressive DCE | |
888 | as the last tree SSA pass, but keep this in mind when you | |
889 | start experimenting with pass ordering. */ | |
890 | ||
891 | static void | |
892 | perform_tree_ssa_dce (bool aggressive) | |
893 | { | |
894 | struct edge_list *el = NULL; | |
895 | ||
896 | tree_dce_init (aggressive); | |
897 | ||
898 | if (aggressive) | |
899 | { | |
900 | /* Compute control dependence. */ | |
901 | timevar_push (TV_CONTROL_DEPENDENCES); | |
902 | calculate_dominance_info (CDI_POST_DOMINATORS); | |
903 | el = create_edge_list (); | |
904 | find_all_control_dependences (el); | |
905 | timevar_pop (TV_CONTROL_DEPENDENCES); | |
906 | ||
a28fee03 SB |
907 | visited_control_parents = sbitmap_alloc (last_basic_block); |
908 | sbitmap_zero (visited_control_parents); | |
909 | ||
6de9cd9a DN |
910 | mark_dfs_back_edges (); |
911 | } | |
912 | ||
913 | find_obviously_necessary_stmts (el); | |
914 | ||
915 | propagate_necessity (el); | |
916 | ||
52328bf6 | 917 | mark_really_necessary_kill_operand_phis (); |
6de9cd9a DN |
918 | eliminate_unnecessary_stmts (); |
919 | ||
920 | if (aggressive) | |
921 | free_dominance_info (CDI_POST_DOMINATORS); | |
922 | ||
9da4058c JL |
923 | /* If we removed paths in the CFG, then we need to update |
924 | dominators as well. I haven't investigated the possibility | |
925 | of incrementally updating dominators. */ | |
926 | if (cfg_altered) | |
927 | free_dominance_info (CDI_DOMINATORS); | |
928 | ||
6de9cd9a DN |
929 | /* Debugging dumps. */ |
930 | if (dump_file) | |
88a40e67 | 931 | print_stats (); |
6de9cd9a DN |
932 | |
933 | tree_dce_done (aggressive); | |
960076d9 AP |
934 | |
935 | free_edge_list (el); | |
6de9cd9a DN |
936 | } |
937 | ||
938 | /* Pass entry points. */ | |
b1e8be10 | 939 | static void |
6de9cd9a DN |
940 | tree_ssa_dce (void) |
941 | { | |
942 | perform_tree_ssa_dce (/*aggressive=*/false); | |
943 | } | |
944 | ||
49896738 RH |
945 | static void |
946 | tree_ssa_dce_loop (void) | |
947 | { | |
948 | perform_tree_ssa_dce (/*aggressive=*/false); | |
949 | free_numbers_of_iterations_estimates (current_loops); | |
a4176272 | 950 | scev_reset (); |
49896738 RH |
951 | } |
952 | ||
6de9cd9a DN |
953 | static void |
954 | tree_ssa_cd_dce (void) | |
955 | { | |
956 | perform_tree_ssa_dce (/*aggressive=*/optimize >= 2); | |
957 | } | |
958 | ||
959 | static bool | |
960 | gate_dce (void) | |
961 | { | |
962 | return flag_tree_dce != 0; | |
963 | } | |
964 | ||
965 | struct tree_opt_pass pass_dce = | |
966 | { | |
967 | "dce", /* name */ | |
968 | gate_dce, /* gate */ | |
969 | tree_ssa_dce, /* execute */ | |
970 | NULL, /* sub */ | |
971 | NULL, /* next */ | |
972 | 0, /* static_pass_number */ | |
973 | TV_TREE_DCE, /* tv_id */ | |
c1b763fa | 974 | PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */ |
6de9cd9a DN |
975 | 0, /* properties_provided */ |
976 | 0, /* properties_destroyed */ | |
977 | 0, /* todo_flags_start */ | |
0bca51f0 | 978 | TODO_dump_func |
8b1062b2 | 979 | | TODO_update_ssa |
0bca51f0 DN |
980 | | TODO_cleanup_cfg |
981 | | TODO_ggc_collect | |
3f519b35 RG |
982 | | TODO_verify_ssa |
983 | | TODO_remove_unused_locals, /* todo_flags_finish */ | |
9f8628ba | 984 | 0 /* letter */ |
6de9cd9a DN |
985 | }; |
986 | ||
49896738 RH |
987 | struct tree_opt_pass pass_dce_loop = |
988 | { | |
989 | "dceloop", /* name */ | |
990 | gate_dce, /* gate */ | |
991 | tree_ssa_dce_loop, /* execute */ | |
992 | NULL, /* sub */ | |
993 | NULL, /* next */ | |
994 | 0, /* static_pass_number */ | |
995 | TV_TREE_DCE, /* tv_id */ | |
996 | PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */ | |
997 | 0, /* properties_provided */ | |
998 | 0, /* properties_destroyed */ | |
999 | 0, /* todo_flags_start */ | |
1000 | TODO_dump_func | |
8b1062b2 | 1001 | | TODO_update_ssa |
49896738 RH |
1002 | | TODO_cleanup_cfg |
1003 | | TODO_verify_ssa, /* todo_flags_finish */ | |
1004 | 0 /* letter */ | |
1005 | }; | |
1006 | ||
6de9cd9a DN |
1007 | struct tree_opt_pass pass_cd_dce = |
1008 | { | |
1009 | "cddce", /* name */ | |
1010 | gate_dce, /* gate */ | |
1011 | tree_ssa_cd_dce, /* execute */ | |
1012 | NULL, /* sub */ | |
1013 | NULL, /* next */ | |
1014 | 0, /* static_pass_number */ | |
1015 | TV_TREE_CD_DCE, /* tv_id */ | |
c1b763fa | 1016 | PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */ |
6de9cd9a DN |
1017 | 0, /* properties_provided */ |
1018 | 0, /* properties_destroyed */ | |
1019 | 0, /* todo_flags_start */ | |
0bca51f0 | 1020 | TODO_dump_func |
8b1062b2 | 1021 | | TODO_update_ssa |
0bca51f0 DN |
1022 | | TODO_cleanup_cfg |
1023 | | TODO_ggc_collect | |
1024 | | TODO_verify_ssa | |
1025 | | TODO_verify_flow, /* todo_flags_finish */ | |
9f8628ba | 1026 | 0 /* letter */ |
6de9cd9a | 1027 | }; |