MAINTAINERS: Update my entry.
[official-gcc.git] / gcc / tree-ssa-dce.c
blob16ff4a30d1f24b6b47677a81464ca835a5aa86c5
1 /* Dead code elimination pass for the GNU compiler.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
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.
7 This file is part of GCC.
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.
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.
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
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA. */
24 /* Dead code elimination.
26 References:
28 Building an Optimizing Compiler,
29 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
31 Advanced Compiler Design and Implementation,
32 Steven Muchnick, Morgan Kaufmann, 1997, Section 18.10.
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.
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. */
46 #include "config.h"
47 #include "system.h"
48 #include "coretypes.h"
49 #include "tm.h"
50 #include "ggc.h"
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"
56 #include "obstack.h"
57 #include "basic-block.h"
59 #include "tree.h"
60 #include "diagnostic.h"
61 #include "tree-flow.h"
62 #include "tree-gimple.h"
63 #include "tree-dump.h"
64 #include "tree-pass.h"
65 #include "timevar.h"
66 #include "flags.h"
67 #include "cfgloop.h"
68 #include "tree-scalar-evolution.h"
70 static struct stmt_stats
72 int total;
73 int total_phis;
74 int removed;
75 int removed_phis;
76 } stats;
78 static VEC(tree,heap) *worklist;
80 /* Vector indicating an SSA name has already been processed and marked
81 as necessary. */
82 static sbitmap processed;
84 /* Vector indicating that last_stmt if a basic block has already been
85 marked as necessary. */
86 static sbitmap last_stmt_necessary;
88 /* Before we can determine whether a control branch is dead, we need to
89 compute which blocks are control dependent on which edges.
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. */
95 static bitmap *control_dependence_map;
97 /* Vector indicating that a basic block has already had all the edges
98 processed that it is control dependent on. */
99 static sbitmap visited_control_parents;
101 /* TRUE if this pass alters the CFG (by removing control statements).
102 FALSE otherwise.
104 If this pass alters the CFG, then it will arrange for the dominators
105 to be recomputed. */
106 static bool cfg_altered;
108 /* Execute code that follows the macro for each edge (given number
109 EDGE_NUMBER within the CODE) for which the block with index N is
110 control dependent. */
111 #define EXECUTE_IF_CONTROL_DEPENDENT(BI, N, EDGE_NUMBER) \
112 EXECUTE_IF_SET_IN_BITMAP (control_dependence_map[(N)], 0, \
113 (EDGE_NUMBER), (BI))
116 /* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */
117 static inline void
118 set_control_dependence_map_bit (basic_block bb, int edge_index)
120 if (bb == ENTRY_BLOCK_PTR)
121 return;
122 gcc_assert (bb != EXIT_BLOCK_PTR);
123 bitmap_set_bit (control_dependence_map[bb->index], edge_index);
126 /* Clear all control dependences for block BB. */
127 static inline void
128 clear_control_dependence_bitmap (basic_block bb)
130 bitmap_clear (control_dependence_map[bb->index]);
134 /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
135 This function is necessary because some blocks have negative numbers. */
137 static inline basic_block
138 find_pdom (basic_block block)
140 gcc_assert (block != ENTRY_BLOCK_PTR);
142 if (block == EXIT_BLOCK_PTR)
143 return EXIT_BLOCK_PTR;
144 else
146 basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
147 if (! bb)
148 return EXIT_BLOCK_PTR;
149 return bb;
154 /* Determine all blocks' control dependences on the given edge with edge_list
155 EL index EDGE_INDEX, ala Morgan, Section 3.6. */
157 static void
158 find_control_dependence (struct edge_list *el, int edge_index)
160 basic_block current_block;
161 basic_block ending_block;
163 gcc_assert (INDEX_EDGE_PRED_BB (el, edge_index) != EXIT_BLOCK_PTR);
165 if (INDEX_EDGE_PRED_BB (el, edge_index) == ENTRY_BLOCK_PTR)
166 ending_block = single_succ (ENTRY_BLOCK_PTR);
167 else
168 ending_block = find_pdom (INDEX_EDGE_PRED_BB (el, edge_index));
170 for (current_block = INDEX_EDGE_SUCC_BB (el, edge_index);
171 current_block != ending_block && current_block != EXIT_BLOCK_PTR;
172 current_block = find_pdom (current_block))
174 edge e = INDEX_EDGE (el, edge_index);
176 /* For abnormal edges, we don't make current_block control
177 dependent because instructions that throw are always necessary
178 anyway. */
179 if (e->flags & EDGE_ABNORMAL)
180 continue;
182 set_control_dependence_map_bit (current_block, edge_index);
187 /* Record all blocks' control dependences on all edges in the edge
188 list EL, ala Morgan, Section 3.6. */
190 static void
191 find_all_control_dependences (struct edge_list *el)
193 int i;
195 for (i = 0; i < NUM_EDGES (el); ++i)
196 find_control_dependence (el, i);
200 #define NECESSARY(stmt) stmt->base.asm_written_flag
202 /* If STMT is not already marked necessary, mark it, and add it to the
203 worklist if ADD_TO_WORKLIST is true. */
204 static inline void
205 mark_stmt_necessary (tree stmt, bool add_to_worklist)
207 gcc_assert (stmt);
208 gcc_assert (!DECL_P (stmt));
210 if (NECESSARY (stmt))
211 return;
213 if (dump_file && (dump_flags & TDF_DETAILS))
215 fprintf (dump_file, "Marking useful stmt: ");
216 print_generic_stmt (dump_file, stmt, TDF_SLIM);
217 fprintf (dump_file, "\n");
220 NECESSARY (stmt) = 1;
221 if (add_to_worklist)
222 VEC_safe_push (tree, heap, worklist, stmt);
225 /* Mark the statement defining operand OP as necessary. PHIONLY is true
226 if we should only mark it necessary if it is a phi node. */
228 static inline void
229 mark_operand_necessary (tree op, bool phionly)
231 tree stmt;
232 int ver;
234 gcc_assert (op);
236 ver = SSA_NAME_VERSION (op);
237 if (TEST_BIT (processed, ver))
238 return;
239 SET_BIT (processed, ver);
241 stmt = SSA_NAME_DEF_STMT (op);
242 gcc_assert (stmt);
244 if (NECESSARY (stmt)
245 || IS_EMPTY_STMT (stmt)
246 || (phionly && TREE_CODE (stmt) != PHI_NODE))
247 return;
249 NECESSARY (stmt) = 1;
250 VEC_safe_push (tree, heap, worklist, stmt);
254 /* Mark STMT as necessary if it obviously is. Add it to the worklist if
255 it can make other statements necessary.
257 If AGGRESSIVE is false, control statements are conservatively marked as
258 necessary. */
260 static void
261 mark_stmt_if_obviously_necessary (tree stmt, bool aggressive)
263 stmt_ann_t ann;
264 tree op;
266 /* With non-call exceptions, we have to assume that all statements could
267 throw. If a statement may throw, it is inherently necessary. */
268 if (flag_non_call_exceptions
269 && tree_could_throw_p (stmt))
271 mark_stmt_necessary (stmt, true);
272 return;
275 /* Statements that are implicitly live. Most function calls, asm and return
276 statements are required. Labels and BIND_EXPR nodes are kept because
277 they are control flow, and we have no way of knowing whether they can be
278 removed. DCE can eliminate all the other statements in a block, and CFG
279 can then remove the block and labels. */
280 switch (TREE_CODE (stmt))
282 case BIND_EXPR:
283 case LABEL_EXPR:
284 case CASE_LABEL_EXPR:
285 mark_stmt_necessary (stmt, false);
286 return;
288 case ASM_EXPR:
289 case RESX_EXPR:
290 case RETURN_EXPR:
291 mark_stmt_necessary (stmt, true);
292 return;
294 case CALL_EXPR:
295 /* Most, but not all function calls are required. Function calls that
296 produce no result and have no side effects (i.e. const pure
297 functions) are unnecessary. */
298 if (TREE_SIDE_EFFECTS (stmt))
299 mark_stmt_necessary (stmt, true);
300 return;
302 case GIMPLE_MODIFY_STMT:
303 op = get_call_expr_in (stmt);
304 if (op && TREE_SIDE_EFFECTS (op))
306 mark_stmt_necessary (stmt, true);
307 return;
310 /* These values are mildly magic bits of the EH runtime. We can't
311 see the entire lifetime of these values until landing pads are
312 generated. */
313 if (TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == EXC_PTR_EXPR
314 || TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == FILTER_EXPR)
316 mark_stmt_necessary (stmt, true);
317 return;
319 break;
321 case GOTO_EXPR:
322 gcc_assert (!simple_goto_p (stmt));
323 mark_stmt_necessary (stmt, true);
324 return;
326 case COND_EXPR:
327 gcc_assert (EDGE_COUNT (bb_for_stmt (stmt)->succs) == 2);
328 /* Fall through. */
330 case SWITCH_EXPR:
331 if (! aggressive)
332 mark_stmt_necessary (stmt, true);
333 break;
335 default:
336 break;
339 ann = stmt_ann (stmt);
341 /* If the statement has volatile operands, it needs to be preserved.
342 Same for statements that can alter control flow in unpredictable
343 ways. */
344 if (ann->has_volatile_ops || is_ctrl_altering_stmt (stmt))
346 mark_stmt_necessary (stmt, true);
347 return;
350 if (is_hidden_global_store (stmt))
352 mark_stmt_necessary (stmt, true);
353 return;
356 return;
360 /* Make corresponding control dependent edges necessary. We only
361 have to do this once for each basic block, so we clear the bitmap
362 after we're done. */
363 static void
364 mark_control_dependent_edges_necessary (basic_block bb, struct edge_list *el)
366 bitmap_iterator bi;
367 unsigned edge_number;
369 gcc_assert (bb != EXIT_BLOCK_PTR);
371 if (bb == ENTRY_BLOCK_PTR)
372 return;
374 EXECUTE_IF_CONTROL_DEPENDENT (bi, bb->index, edge_number)
376 tree t;
377 basic_block cd_bb = INDEX_EDGE_PRED_BB (el, edge_number);
379 if (TEST_BIT (last_stmt_necessary, cd_bb->index))
380 continue;
381 SET_BIT (last_stmt_necessary, cd_bb->index);
383 t = last_stmt (cd_bb);
384 if (t && is_ctrl_stmt (t))
385 mark_stmt_necessary (t, true);
390 /* Find obviously necessary statements. These are things like most function
391 calls, and stores to file level variables.
393 If EL is NULL, control statements are conservatively marked as
394 necessary. Otherwise it contains the list of edges used by control
395 dependence analysis. */
397 static void
398 find_obviously_necessary_stmts (struct edge_list *el)
400 basic_block bb;
401 block_stmt_iterator i;
402 edge e;
404 FOR_EACH_BB (bb)
406 tree phi;
408 /* PHI nodes are never inherently necessary. */
409 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
410 NECESSARY (phi) = 0;
412 /* Check all statements in the block. */
413 for (i = bsi_start (bb); ! bsi_end_p (i); bsi_next (&i))
415 tree stmt = bsi_stmt (i);
416 NECESSARY (stmt) = 0;
417 mark_stmt_if_obviously_necessary (stmt, el != NULL);
421 if (el)
423 /* Prevent the loops from being removed. We must keep the infinite loops,
424 and we currently do not have a means to recognize the finite ones. */
425 FOR_EACH_BB (bb)
427 edge_iterator ei;
428 FOR_EACH_EDGE (e, ei, bb->succs)
429 if (e->flags & EDGE_DFS_BACK)
430 mark_control_dependent_edges_necessary (e->dest, el);
436 /* Propagate necessity using the operands of necessary statements.
437 Process the uses on each statement in the worklist, and add all
438 feeding statements which contribute to the calculation of this
439 value to the worklist.
441 In conservative mode, EL is NULL. */
443 static void
444 propagate_necessity (struct edge_list *el)
446 tree stmt;
447 bool aggressive = (el ? true : false);
449 if (dump_file && (dump_flags & TDF_DETAILS))
450 fprintf (dump_file, "\nProcessing worklist:\n");
452 while (VEC_length (tree, worklist) > 0)
454 /* Take STMT from worklist. */
455 stmt = VEC_pop (tree, worklist);
457 if (dump_file && (dump_flags & TDF_DETAILS))
459 fprintf (dump_file, "processing: ");
460 print_generic_stmt (dump_file, stmt, TDF_SLIM);
461 fprintf (dump_file, "\n");
464 if (aggressive)
466 /* Mark the last statements of the basic blocks that the block
467 containing STMT is control dependent on, but only if we haven't
468 already done so. */
469 basic_block bb = bb_for_stmt (stmt);
470 if (bb != ENTRY_BLOCK_PTR
471 && ! TEST_BIT (visited_control_parents, bb->index))
473 SET_BIT (visited_control_parents, bb->index);
474 mark_control_dependent_edges_necessary (bb, el);
478 if (TREE_CODE (stmt) == PHI_NODE)
480 /* PHI nodes are somewhat special in that each PHI alternative has
481 data and control dependencies. All the statements feeding the
482 PHI node's arguments are always necessary. In aggressive mode,
483 we also consider the control dependent edges leading to the
484 predecessor block associated with each PHI alternative as
485 necessary. */
486 int k;
488 for (k = 0; k < PHI_NUM_ARGS (stmt); k++)
490 tree arg = PHI_ARG_DEF (stmt, k);
491 if (TREE_CODE (arg) == SSA_NAME)
492 mark_operand_necessary (arg, false);
495 if (aggressive)
497 for (k = 0; k < PHI_NUM_ARGS (stmt); k++)
499 basic_block arg_bb = PHI_ARG_EDGE (stmt, k)->src;
500 if (arg_bb != ENTRY_BLOCK_PTR
501 && ! TEST_BIT (visited_control_parents, arg_bb->index))
503 SET_BIT (visited_control_parents, arg_bb->index);
504 mark_control_dependent_edges_necessary (arg_bb, el);
509 else
511 /* Propagate through the operands. Examine all the USE, VUSE and
512 V_MAY_DEF operands in this statement. Mark all the statements
513 which feed this statement's uses as necessary. */
514 ssa_op_iter iter;
515 tree use;
517 /* The operands of V_MAY_DEF expressions are also needed as they
518 represent potential definitions that may reach this
519 statement (V_MAY_DEF operands allow us to follow def-def
520 links). */
522 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_ALL_USES)
523 mark_operand_necessary (use, false);
529 /* Propagate necessity around virtual phi nodes used in kill operands.
530 The reason this isn't done during propagate_necessity is because we don't
531 want to keep phis around that are just there for must-defs, unless we
532 absolutely have to. After we've rewritten the reaching definitions to be
533 correct in the previous part of the fixup routine, we can simply propagate
534 around the information about which of these virtual phi nodes are really
535 used, and set the NECESSARY flag accordingly.
536 Note that we do the minimum here to ensure that we keep alive the phis that
537 are actually used in the corrected SSA form. In particular, some of these
538 phis may now have all of the same operand, and will be deleted by some
539 other pass. */
541 static void
542 mark_really_necessary_kill_operand_phis (void)
544 basic_block bb;
545 int i;
547 /* Seed the worklist with the new virtual phi arguments and virtual
548 uses */
549 FOR_EACH_BB (bb)
551 block_stmt_iterator bsi;
552 tree phi;
554 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
556 if (!is_gimple_reg (PHI_RESULT (phi)) && NECESSARY (phi))
558 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
559 mark_operand_necessary (PHI_ARG_DEF (phi, i), true);
563 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
565 tree stmt = bsi_stmt (bsi);
567 if (NECESSARY (stmt))
569 use_operand_p use_p;
570 ssa_op_iter iter;
571 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
572 SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_KILLS)
574 tree use = USE_FROM_PTR (use_p);
575 mark_operand_necessary (use, true);
581 /* Mark all virtual phis still in use as necessary, and all of their
582 arguments that are phis as necessary. */
583 while (VEC_length (tree, worklist) > 0)
585 tree use = VEC_pop (tree, worklist);
587 for (i = 0; i < PHI_NUM_ARGS (use); i++)
588 mark_operand_necessary (PHI_ARG_DEF (use, i), true);
593 /* Remove dead PHI nodes from block BB. */
595 static void
596 remove_dead_phis (basic_block bb)
598 tree prev, phi;
600 prev = NULL_TREE;
601 phi = phi_nodes (bb);
602 while (phi)
604 stats.total_phis++;
606 if (! NECESSARY (phi))
608 tree next = PHI_CHAIN (phi);
610 if (dump_file && (dump_flags & TDF_DETAILS))
612 fprintf (dump_file, "Deleting : ");
613 print_generic_stmt (dump_file, phi, TDF_SLIM);
614 fprintf (dump_file, "\n");
617 remove_phi_node (phi, prev, true);
618 stats.removed_phis++;
619 phi = next;
621 else
623 prev = phi;
624 phi = PHI_CHAIN (phi);
630 /* Remove dead statement pointed to by iterator I. Receives the basic block BB
631 containing I so that we don't have to look it up. */
633 static void
634 remove_dead_stmt (block_stmt_iterator *i, basic_block bb)
636 tree t = bsi_stmt (*i);
637 def_operand_p def_p;
639 ssa_op_iter iter;
641 if (dump_file && (dump_flags & TDF_DETAILS))
643 fprintf (dump_file, "Deleting : ");
644 print_generic_stmt (dump_file, t, TDF_SLIM);
645 fprintf (dump_file, "\n");
648 stats.removed++;
650 /* If we have determined that a conditional branch statement contributes
651 nothing to the program, then we not only remove it, but we also change
652 the flow graph so that the current block will simply fall-thru to its
653 immediate post-dominator. The blocks we are circumventing will be
654 removed by cleanup_tree_cfg if this change in the flow graph makes them
655 unreachable. */
656 if (is_ctrl_stmt (t))
658 basic_block post_dom_bb;
660 /* The post dominance info has to be up-to-date. */
661 gcc_assert (dom_computed[CDI_POST_DOMINATORS] == DOM_OK);
662 /* Get the immediate post dominator of bb. */
663 post_dom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
665 /* There are three particularly problematical cases.
667 1. Blocks that do not have an immediate post dominator. This
668 can happen with infinite loops.
670 2. Blocks that are only post dominated by the exit block. These
671 can also happen for infinite loops as we create fake edges
672 in the dominator tree.
674 3. If the post dominator has PHI nodes we may be able to compute
675 the right PHI args for them.
678 In each of these cases we must remove the control statement
679 as it may reference SSA_NAMEs which are going to be removed and
680 we remove all but one outgoing edge from the block. */
681 if (! post_dom_bb
682 || post_dom_bb == EXIT_BLOCK_PTR
683 || phi_nodes (post_dom_bb))
685 else
687 /* Redirect the first edge out of BB to reach POST_DOM_BB. */
688 redirect_edge_and_branch (EDGE_SUCC (bb, 0), post_dom_bb);
689 PENDING_STMT (EDGE_SUCC (bb, 0)) = NULL;
691 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
692 EDGE_SUCC (bb, 0)->count = bb->count;
694 /* The edge is no longer associated with a conditional, so it does
695 not have TRUE/FALSE flags. */
696 EDGE_SUCC (bb, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
698 /* The lone outgoing edge from BB will be a fallthru edge. */
699 EDGE_SUCC (bb, 0)->flags |= EDGE_FALLTHRU;
701 /* Remove the remaining the outgoing edges. */
702 while (!single_succ_p (bb))
704 /* FIXME. When we remove the edge, we modify the CFG, which
705 in turn modifies the dominator and post-dominator tree.
706 Is it safe to postpone recomputing the dominator and
707 post-dominator tree until the end of this pass given that
708 the post-dominators are used above? */
709 cfg_altered = true;
710 remove_edge (EDGE_SUCC (bb, 1));
714 FOR_EACH_SSA_DEF_OPERAND (def_p, t, iter, SSA_OP_VIRTUAL_DEFS)
716 tree def = DEF_FROM_PTR (def_p);
717 mark_sym_for_renaming (SSA_NAME_VAR (def));
719 bsi_remove (i, true);
720 release_defs (t);
724 /* Eliminate unnecessary statements. Any instruction not marked as necessary
725 contributes nothing to the program, and can be deleted. */
727 static void
728 eliminate_unnecessary_stmts (void)
730 basic_block bb;
731 block_stmt_iterator i;
733 if (dump_file && (dump_flags & TDF_DETAILS))
734 fprintf (dump_file, "\nEliminating unnecessary statements:\n");
736 clear_special_calls ();
737 FOR_EACH_BB (bb)
739 /* Remove dead PHI nodes. */
740 remove_dead_phis (bb);
743 FOR_EACH_BB (bb)
745 /* Remove dead statements. */
746 for (i = bsi_start (bb); ! bsi_end_p (i) ; )
748 tree t = bsi_stmt (i);
750 stats.total++;
752 /* If `i' is not necessary then remove it. */
753 if (! NECESSARY (t))
754 remove_dead_stmt (&i, bb);
755 else
757 tree call = get_call_expr_in (t);
758 if (call)
759 notice_special_calls (call);
760 bsi_next (&i);
767 /* Print out removed statement statistics. */
769 static void
770 print_stats (void)
772 if (dump_file && (dump_flags & (TDF_STATS|TDF_DETAILS)))
774 float percg;
776 percg = ((float) stats.removed / (float) stats.total) * 100;
777 fprintf (dump_file, "Removed %d of %d statements (%d%%)\n",
778 stats.removed, stats.total, (int) percg);
780 if (stats.total_phis == 0)
781 percg = 0;
782 else
783 percg = ((float) stats.removed_phis / (float) stats.total_phis) * 100;
785 fprintf (dump_file, "Removed %d of %d PHI nodes (%d%%)\n",
786 stats.removed_phis, stats.total_phis, (int) percg);
790 /* Initialization for this pass. Set up the used data structures. */
792 static void
793 tree_dce_init (bool aggressive)
795 memset ((void *) &stats, 0, sizeof (stats));
797 if (aggressive)
799 int i;
801 control_dependence_map = XNEWVEC (bitmap, last_basic_block);
802 for (i = 0; i < last_basic_block; ++i)
803 control_dependence_map[i] = BITMAP_ALLOC (NULL);
805 last_stmt_necessary = sbitmap_alloc (last_basic_block);
806 sbitmap_zero (last_stmt_necessary);
809 processed = sbitmap_alloc (num_ssa_names + 1);
810 sbitmap_zero (processed);
812 worklist = VEC_alloc (tree, heap, 64);
813 cfg_altered = false;
816 /* Cleanup after this pass. */
818 static void
819 tree_dce_done (bool aggressive)
821 if (aggressive)
823 int i;
825 for (i = 0; i < last_basic_block; ++i)
826 BITMAP_FREE (control_dependence_map[i]);
827 free (control_dependence_map);
829 sbitmap_free (visited_control_parents);
830 sbitmap_free (last_stmt_necessary);
833 sbitmap_free (processed);
835 VEC_free (tree, heap, worklist);
838 /* Main routine to eliminate dead code.
840 AGGRESSIVE controls the aggressiveness of the algorithm.
841 In conservative mode, we ignore control dependence and simply declare
842 all but the most trivially dead branches necessary. This mode is fast.
843 In aggressive mode, control dependences are taken into account, which
844 results in more dead code elimination, but at the cost of some time.
846 FIXME: Aggressive mode before PRE doesn't work currently because
847 the dominance info is not invalidated after DCE1. This is
848 not an issue right now because we only run aggressive DCE
849 as the last tree SSA pass, but keep this in mind when you
850 start experimenting with pass ordering. */
852 static void
853 perform_tree_ssa_dce (bool aggressive)
855 struct edge_list *el = NULL;
857 tree_dce_init (aggressive);
859 if (aggressive)
861 /* Compute control dependence. */
862 timevar_push (TV_CONTROL_DEPENDENCES);
863 calculate_dominance_info (CDI_POST_DOMINATORS);
864 el = create_edge_list ();
865 find_all_control_dependences (el);
866 timevar_pop (TV_CONTROL_DEPENDENCES);
868 visited_control_parents = sbitmap_alloc (last_basic_block);
869 sbitmap_zero (visited_control_parents);
871 mark_dfs_back_edges ();
874 find_obviously_necessary_stmts (el);
876 propagate_necessity (el);
878 mark_really_necessary_kill_operand_phis ();
879 eliminate_unnecessary_stmts ();
881 if (aggressive)
882 free_dominance_info (CDI_POST_DOMINATORS);
884 /* If we removed paths in the CFG, then we need to update
885 dominators as well. I haven't investigated the possibility
886 of incrementally updating dominators. */
887 if (cfg_altered)
888 free_dominance_info (CDI_DOMINATORS);
890 /* Debugging dumps. */
891 if (dump_file)
892 print_stats ();
894 tree_dce_done (aggressive);
896 free_edge_list (el);
899 /* Pass entry points. */
900 static unsigned int
901 tree_ssa_dce (void)
903 perform_tree_ssa_dce (/*aggressive=*/false);
904 return 0;
907 static unsigned int
908 tree_ssa_dce_loop (void)
910 perform_tree_ssa_dce (/*aggressive=*/false);
911 free_numbers_of_iterations_estimates ();
912 scev_reset ();
913 return 0;
916 static unsigned int
917 tree_ssa_cd_dce (void)
919 perform_tree_ssa_dce (/*aggressive=*/optimize >= 2);
920 return 0;
923 static bool
924 gate_dce (void)
926 return flag_tree_dce != 0;
929 struct tree_opt_pass pass_dce =
931 "dce", /* name */
932 gate_dce, /* gate */
933 tree_ssa_dce, /* execute */
934 NULL, /* sub */
935 NULL, /* next */
936 0, /* static_pass_number */
937 TV_TREE_DCE, /* tv_id */
938 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
939 0, /* properties_provided */
940 0, /* properties_destroyed */
941 0, /* todo_flags_start */
942 TODO_dump_func
943 | TODO_update_ssa
944 | TODO_cleanup_cfg
945 | TODO_ggc_collect
946 | TODO_verify_ssa
947 | TODO_remove_unused_locals, /* todo_flags_finish */
948 0 /* letter */
951 struct tree_opt_pass pass_dce_loop =
953 "dceloop", /* name */
954 gate_dce, /* gate */
955 tree_ssa_dce_loop, /* execute */
956 NULL, /* sub */
957 NULL, /* next */
958 0, /* static_pass_number */
959 TV_TREE_DCE, /* tv_id */
960 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
961 0, /* properties_provided */
962 0, /* properties_destroyed */
963 0, /* todo_flags_start */
964 TODO_dump_func
965 | TODO_update_ssa
966 | TODO_cleanup_cfg
967 | TODO_verify_ssa, /* todo_flags_finish */
968 0 /* letter */
971 struct tree_opt_pass pass_cd_dce =
973 "cddce", /* name */
974 gate_dce, /* gate */
975 tree_ssa_cd_dce, /* execute */
976 NULL, /* sub */
977 NULL, /* next */
978 0, /* static_pass_number */
979 TV_TREE_CD_DCE, /* tv_id */
980 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
981 0, /* properties_provided */
982 0, /* properties_destroyed */
983 0, /* todo_flags_start */
984 TODO_dump_func
985 | TODO_update_ssa
986 | TODO_cleanup_cfg
987 | TODO_ggc_collect
988 | TODO_verify_ssa
989 | TODO_verify_flow, /* todo_flags_finish */
990 0 /* letter */