* basic-block.h (ei_safe_edge): New function.
[official-gcc.git] / gcc / tree-ssa-dce.c
blob0631d3b545fe2906e8bc5c668df728990754d5f6
1 /* Dead code elimination pass for the GNU compiler.
2 Copyright (C) 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, 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 "errors.h"
51 #include "ggc.h"
53 /* These RTL headers are needed for basic-block.h. */
54 #include "rtl.h"
55 #include "tm_p.h"
56 #include "hard-reg-set.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"
68 static struct stmt_stats
70 int total;
71 int total_phis;
72 int removed;
73 int removed_phis;
74 } stats;
76 static varray_type worklist;
78 /* Vector indicating an SSA name has already been processed and marked
79 as necessary. */
80 static sbitmap processed;
82 /* Vector indicating that last_stmt if a basic block has already been
83 marked as necessary. */
84 static sbitmap last_stmt_necessary;
86 /* Before we can determine whether a control branch is dead, we need to
87 compute which blocks are control dependent on which edges.
89 We expect each block to be control dependent on very few edges so we
90 use a bitmap for each block recording its edges. An array holds the
91 bitmap. The Ith bit in the bitmap is set if that block is dependent
92 on the Ith edge. */
93 bitmap *control_dependence_map;
95 /* Execute CODE for each edge (given number EDGE_NUMBER within the CODE)
96 for which the block with index N is control dependent. */
97 #define EXECUTE_IF_CONTROL_DEPENDENT(N, EDGE_NUMBER, CODE) \
98 EXECUTE_IF_SET_IN_BITMAP (control_dependence_map[N], 0, EDGE_NUMBER, CODE)
100 /* Local function prototypes. */
101 static inline void set_control_dependence_map_bit (basic_block, int);
102 static inline void clear_control_dependence_bitmap (basic_block);
103 static void find_all_control_dependences (struct edge_list *);
104 static void find_control_dependence (struct edge_list *, int);
105 static inline basic_block find_pdom (basic_block);
107 static inline void mark_stmt_necessary (tree, bool);
108 static inline void mark_operand_necessary (tree);
110 static void mark_stmt_if_obviously_necessary (tree, bool);
111 static void find_obviously_necessary_stmts (struct edge_list *);
113 static void mark_control_dependent_edges_necessary (basic_block, struct edge_list *);
114 static void propagate_necessity (struct edge_list *);
116 static void eliminate_unnecessary_stmts (void);
117 static void remove_dead_phis (basic_block);
118 static void remove_dead_stmt (block_stmt_iterator *, basic_block);
120 static void print_stats (void);
121 static void tree_dce_init (bool);
122 static void tree_dce_done (bool);
124 /* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */
125 static inline void
126 set_control_dependence_map_bit (basic_block bb, int edge_index)
128 if (bb == ENTRY_BLOCK_PTR)
129 return;
130 gcc_assert (bb != EXIT_BLOCK_PTR);
131 bitmap_set_bit (control_dependence_map[bb->index], edge_index);
134 /* Clear all control dependences for block BB. */
135 static inline
136 void clear_control_dependence_bitmap (basic_block bb)
138 bitmap_clear (control_dependence_map[bb->index]);
141 /* Record all blocks' control dependences on all edges in the edge
142 list EL, ala Morgan, Section 3.6. */
144 static void
145 find_all_control_dependences (struct edge_list *el)
147 int i;
149 for (i = 0; i < NUM_EDGES (el); ++i)
150 find_control_dependence (el, i);
153 /* Determine all blocks' control dependences on the given edge with edge_list
154 EL index EDGE_INDEX, ala Morgan, Section 3.6. */
156 static void
157 find_control_dependence (struct edge_list *el, int edge_index)
159 basic_block current_block;
160 basic_block ending_block;
162 gcc_assert (INDEX_EDGE_PRED_BB (el, edge_index) != EXIT_BLOCK_PTR);
164 if (INDEX_EDGE_PRED_BB (el, edge_index) == ENTRY_BLOCK_PTR)
165 ending_block = ENTRY_BLOCK_PTR->next_bb;
166 else
167 ending_block = find_pdom (INDEX_EDGE_PRED_BB (el, edge_index));
169 for (current_block = INDEX_EDGE_SUCC_BB (el, edge_index);
170 current_block != ending_block && current_block != EXIT_BLOCK_PTR;
171 current_block = find_pdom (current_block))
173 edge e = INDEX_EDGE (el, edge_index);
175 /* For abnormal edges, we don't make current_block control
176 dependent because instructions that throw are always necessary
177 anyway. */
178 if (e->flags & EDGE_ABNORMAL)
179 continue;
181 set_control_dependence_map_bit (current_block, edge_index);
185 /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
186 This function is necessary because some blocks have negative numbers. */
188 static inline basic_block
189 find_pdom (basic_block block)
191 gcc_assert (block != ENTRY_BLOCK_PTR);
193 if (block == EXIT_BLOCK_PTR)
194 return EXIT_BLOCK_PTR;
195 else
197 basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
198 if (! bb)
199 return EXIT_BLOCK_PTR;
200 return bb;
204 #define NECESSARY(stmt) stmt->common.asm_written_flag
206 /* If STMT is not already marked necessary, mark it, and add it to the
207 worklist if ADD_TO_WORKLIST is true. */
208 static inline void
209 mark_stmt_necessary (tree stmt, bool add_to_worklist)
211 gcc_assert (stmt);
212 gcc_assert (stmt != error_mark_node);
213 gcc_assert (!DECL_P (stmt));
215 if (NECESSARY (stmt))
216 return;
218 if (dump_file && (dump_flags & TDF_DETAILS))
220 fprintf (dump_file, "Marking useful stmt: ");
221 print_generic_stmt (dump_file, stmt, TDF_SLIM);
222 fprintf (dump_file, "\n");
225 NECESSARY (stmt) = 1;
226 if (add_to_worklist)
227 VARRAY_PUSH_TREE (worklist, stmt);
230 /* Mark the statement defining operand OP as necessary. */
232 static inline void
233 mark_operand_necessary (tree op)
235 tree stmt;
236 int ver;
238 gcc_assert (op);
240 ver = SSA_NAME_VERSION (op);
241 if (TEST_BIT (processed, ver))
242 return;
243 SET_BIT (processed, ver);
245 stmt = SSA_NAME_DEF_STMT (op);
246 gcc_assert (stmt);
248 if (NECESSARY (stmt)
249 || IS_EMPTY_STMT (stmt))
250 return;
252 NECESSARY (stmt) = 1;
253 VARRAY_PUSH_TREE (worklist, stmt);
257 /* Mark STMT as necessary if it is obviously is. Add it to the worklist if
258 it can make other statements necessary.
260 If AGGRESSIVE is false, control statements are conservatively marked as
261 necessary. */
263 static void
264 mark_stmt_if_obviously_necessary (tree stmt, bool aggressive)
266 v_may_def_optype v_may_defs;
267 v_must_def_optype v_must_defs;
268 stmt_ann_t ann;
269 tree op, def;
270 ssa_op_iter iter;
272 /* Statements that are implicitly live. Most function calls, asm and return
273 statements are required. Labels and BIND_EXPR nodes are kept because
274 they are control flow, and we have no way of knowing whether they can be
275 removed. DCE can eliminate all the other statements in a block, and CFG
276 can then remove the block and labels. */
277 switch (TREE_CODE (stmt))
279 case BIND_EXPR:
280 case LABEL_EXPR:
281 case CASE_LABEL_EXPR:
282 mark_stmt_necessary (stmt, false);
283 return;
285 case ASM_EXPR:
286 case RESX_EXPR:
287 case RETURN_EXPR:
288 mark_stmt_necessary (stmt, true);
289 return;
291 case CALL_EXPR:
292 /* Most, but not all function calls are required. Function calls that
293 produce no result and have no side effects (i.e. const pure
294 functions) are unnecessary. */
295 if (TREE_SIDE_EFFECTS (stmt))
296 mark_stmt_necessary (stmt, true);
297 return;
299 case MODIFY_EXPR:
300 op = get_call_expr_in (stmt);
301 if (op && TREE_SIDE_EFFECTS (op))
303 mark_stmt_necessary (stmt, true);
304 return;
307 /* These values are mildly magic bits of the EH runtime. We can't
308 see the entire lifetime of these values until landing pads are
309 generated. */
310 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == EXC_PTR_EXPR
311 || TREE_CODE (TREE_OPERAND (stmt, 0)) == FILTER_EXPR)
313 mark_stmt_necessary (stmt, true);
314 return;
316 break;
318 case GOTO_EXPR:
319 if (! simple_goto_p (stmt))
320 mark_stmt_necessary (stmt, true);
321 return;
323 case COND_EXPR:
324 if (GOTO_DESTINATION (COND_EXPR_THEN (stmt))
325 == GOTO_DESTINATION (COND_EXPR_ELSE (stmt)))
327 /* A COND_EXPR is obviously dead if the target labels are the same.
328 We cannot kill the statement at this point, so to prevent the
329 statement from being marked necessary, we replace the condition
330 with a constant. The stmt is killed later on in cfg_cleanup. */
331 COND_EXPR_COND (stmt) = integer_zero_node;
332 modify_stmt (stmt);
333 return;
335 /* Fall through. */
337 case SWITCH_EXPR:
338 if (! aggressive)
339 mark_stmt_necessary (stmt, true);
340 break;
342 default:
343 break;
346 ann = stmt_ann (stmt);
348 /* If the statement has volatile operands, it needs to be preserved.
349 Same for statements that can alter control flow in unpredictable
350 ways. */
351 if (ann->has_volatile_ops || is_ctrl_altering_stmt (stmt))
353 mark_stmt_necessary (stmt, true);
354 return;
357 get_stmt_operands (stmt);
359 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
361 if (is_global_var (SSA_NAME_VAR (def)))
363 mark_stmt_necessary (stmt, true);
364 return;
368 /* Check virtual definitions. If we get here, the only virtual
369 definitions we should see are those generated by assignment
370 statements. */
371 v_may_defs = V_MAY_DEF_OPS (ann);
372 v_must_defs = V_MUST_DEF_OPS (ann);
373 if (NUM_V_MAY_DEFS (v_may_defs) > 0 || NUM_V_MUST_DEFS (v_must_defs) > 0)
375 tree lhs;
377 gcc_assert (TREE_CODE (stmt) == MODIFY_EXPR);
379 /* Note that we must not check the individual virtual operands
380 here. In particular, if this is an aliased store, we could
381 end up with something like the following (SSA notation
382 redacted for brevity):
384 foo (int *p, int i)
386 int x;
387 p_1 = (i_2 > 3) ? &x : p_1;
389 # x_4 = V_MAY_DEF <x_3>
390 *p_1 = 5;
392 return 2;
395 Notice that the store to '*p_1' should be preserved, if we
396 were to check the virtual definitions in that store, we would
397 not mark it needed. This is because 'x' is not a global
398 variable.
400 Therefore, we check the base address of the LHS. If the
401 address is a pointer, we check if its name tag or type tag is
402 a global variable. Otherwise, we check if the base variable
403 is a global. */
404 lhs = TREE_OPERAND (stmt, 0);
405 if (TREE_CODE_CLASS (TREE_CODE (lhs)) == 'r')
406 lhs = get_base_address (lhs);
408 if (lhs == NULL_TREE)
410 /* If LHS is NULL, it means that we couldn't get the base
411 address of the reference. In which case, we should not
412 remove this store. */
413 mark_stmt_necessary (stmt, true);
415 else if (DECL_P (lhs))
417 /* If the store is to a global symbol, we need to keep it. */
418 if (is_global_var (lhs))
419 mark_stmt_necessary (stmt, true);
421 else if (TREE_CODE (lhs) == INDIRECT_REF)
423 tree ptr = TREE_OPERAND (lhs, 0);
424 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
425 tree nmt = (pi) ? pi->name_mem_tag : NULL_TREE;
426 tree tmt = var_ann (SSA_NAME_VAR (ptr))->type_mem_tag;
428 /* If either the name tag or the type tag for PTR is a
429 global variable, then the store is necessary. */
430 if ((nmt && is_global_var (nmt))
431 || (tmt && is_global_var (tmt)))
433 mark_stmt_necessary (stmt, true);
434 return;
437 else
438 gcc_unreachable ();
441 return;
444 /* Find obviously necessary statements. These are things like most function
445 calls, and stores to file level variables.
447 If EL is NULL, control statements are conservatively marked as
448 necessary. Otherwise it contains the list of edges used by control
449 dependence analysis. */
451 static void
452 find_obviously_necessary_stmts (struct edge_list *el)
454 basic_block bb;
455 block_stmt_iterator i;
456 edge e;
458 FOR_EACH_BB (bb)
460 tree phi;
462 /* Check any PHI nodes in the block. */
463 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
465 NECESSARY (phi) = 0;
467 /* PHIs for virtual variables do not directly affect code
468 generation and need not be considered inherently necessary
469 regardless of the bits set in their decl.
471 Thus, we only need to mark PHIs for real variables which
472 need their result preserved as being inherently necessary. */
473 if (is_gimple_reg (PHI_RESULT (phi))
474 && is_global_var (SSA_NAME_VAR (PHI_RESULT (phi))))
475 mark_stmt_necessary (phi, true);
478 /* Check all statements in the block. */
479 for (i = bsi_start (bb); ! bsi_end_p (i); bsi_next (&i))
481 tree stmt = bsi_stmt (i);
482 NECESSARY (stmt) = 0;
483 mark_stmt_if_obviously_necessary (stmt, el != NULL);
486 /* Mark this basic block as `not visited'. A block will be marked
487 visited when the edges that it is control dependent on have been
488 marked. */
489 bb->flags &= ~BB_VISITED;
492 if (el)
494 /* Prevent the loops from being removed. We must keep the infinite loops,
495 and we currently do not have a means to recognize the finite ones. */
496 FOR_EACH_BB (bb)
498 edge_iterator ei;
499 FOR_EACH_EDGE (e, ei, bb->succs)
501 if (e->flags & EDGE_DFS_BACK)
502 mark_control_dependent_edges_necessary (e->dest, el);
508 /* Make corresponding control dependent edges necessary. We only
509 have to do this once for each basic block, so we clear the bitmap
510 after we're done. */
511 static void
512 mark_control_dependent_edges_necessary (basic_block bb, struct edge_list *el)
514 int edge_number;
516 gcc_assert (bb != EXIT_BLOCK_PTR);
518 if (bb == ENTRY_BLOCK_PTR)
519 return;
521 EXECUTE_IF_CONTROL_DEPENDENT (bb->index, edge_number,
523 tree t;
524 basic_block cd_bb = INDEX_EDGE_PRED_BB (el, edge_number);
526 if (TEST_BIT (last_stmt_necessary, cd_bb->index))
527 continue;
528 SET_BIT (last_stmt_necessary, cd_bb->index);
530 t = last_stmt (cd_bb);
531 if (t && is_ctrl_stmt (t))
532 mark_stmt_necessary (t, true);
536 /* Propagate necessity using the operands of necessary statements. Process
537 the uses on each statement in the worklist, and add all feeding statements
538 which contribute to the calculation of this value to the worklist.
540 In conservative mode, EL is NULL. */
542 static void
543 propagate_necessity (struct edge_list *el)
545 tree i;
546 bool aggressive = (el ? true : false);
548 if (dump_file && (dump_flags & TDF_DETAILS))
549 fprintf (dump_file, "\nProcessing worklist:\n");
551 while (VARRAY_ACTIVE_SIZE (worklist) > 0)
553 /* Take `i' from worklist. */
554 i = VARRAY_TOP_TREE (worklist);
555 VARRAY_POP (worklist);
557 if (dump_file && (dump_flags & TDF_DETAILS))
559 fprintf (dump_file, "processing: ");
560 print_generic_stmt (dump_file, i, TDF_SLIM);
561 fprintf (dump_file, "\n");
564 if (aggressive)
566 /* Mark the last statements of the basic blocks that the block
567 containing `i' is control dependent on, but only if we haven't
568 already done so. */
569 basic_block bb = bb_for_stmt (i);
570 if (! (bb->flags & BB_VISITED))
572 bb->flags |= BB_VISITED;
573 mark_control_dependent_edges_necessary (bb, el);
577 if (TREE_CODE (i) == PHI_NODE)
579 /* PHI nodes are somewhat special in that each PHI alternative has
580 data and control dependencies. All the statements feeding the
581 PHI node's arguments are always necessary. In aggressive mode,
582 we also consider the control dependent edges leading to the
583 predecessor block associated with each PHI alternative as
584 necessary. */
585 int k;
586 for (k = 0; k < PHI_NUM_ARGS (i); k++)
588 tree arg = PHI_ARG_DEF (i, k);
589 if (TREE_CODE (arg) == SSA_NAME)
590 mark_operand_necessary (arg);
593 if (aggressive)
595 for (k = 0; k < PHI_NUM_ARGS (i); k++)
597 basic_block arg_bb = PHI_ARG_EDGE (i, k)->src;
598 if (! (arg_bb->flags & BB_VISITED))
600 arg_bb->flags |= BB_VISITED;
601 mark_control_dependent_edges_necessary (arg_bb, el);
606 else
608 /* Propagate through the operands. Examine all the USE, VUSE and
609 V_MAY_DEF operands in this statement. Mark all the statements
610 which feed this statement's uses as necessary. */
611 ssa_op_iter iter;
612 tree use;
614 get_stmt_operands (i);
616 /* The operands of V_MAY_DEF expressions are also needed as they
617 represent potential definitions that may reach this
618 statement (V_MAY_DEF operands allow us to follow def-def
619 links). */
621 FOR_EACH_SSA_TREE_OPERAND (use, i, iter, SSA_OP_ALL_USES)
622 mark_operand_necessary (use);
627 /* Eliminate unnecessary statements. Any instruction not marked as necessary
628 contributes nothing to the program, and can be deleted. */
630 static void
631 eliminate_unnecessary_stmts (void)
633 basic_block bb;
634 block_stmt_iterator i;
636 if (dump_file && (dump_flags & TDF_DETAILS))
637 fprintf (dump_file, "\nEliminating unnecessary statements:\n");
639 clear_special_calls ();
640 FOR_EACH_BB (bb)
642 /* Remove dead PHI nodes. */
643 remove_dead_phis (bb);
645 /* Remove dead statements. */
646 for (i = bsi_start (bb); ! bsi_end_p (i) ; )
648 tree t = bsi_stmt (i);
650 stats.total++;
652 /* If `i' is not necessary then remove it. */
653 if (! NECESSARY (t))
654 remove_dead_stmt (&i, bb);
655 else
657 tree call = get_call_expr_in (t);
658 if (call)
659 notice_special_calls (call);
660 bsi_next (&i);
666 /* Remove dead PHI nodes from block BB. */
668 static void
669 remove_dead_phis (basic_block bb)
671 tree prev, phi;
673 prev = NULL_TREE;
674 phi = phi_nodes (bb);
675 while (phi)
677 stats.total_phis++;
679 if (! NECESSARY (phi))
681 tree next = PHI_CHAIN (phi);
683 if (dump_file && (dump_flags & TDF_DETAILS))
685 fprintf (dump_file, "Deleting : ");
686 print_generic_stmt (dump_file, phi, TDF_SLIM);
687 fprintf (dump_file, "\n");
690 remove_phi_node (phi, prev, bb);
691 stats.removed_phis++;
692 phi = next;
694 else
696 prev = phi;
697 phi = PHI_CHAIN (phi);
702 /* Remove dead statement pointed by iterator I. Receives the basic block BB
703 containing I so that we don't have to look it up. */
705 static void
706 remove_dead_stmt (block_stmt_iterator *i, basic_block bb)
708 tree t = bsi_stmt (*i);
710 if (dump_file && (dump_flags & TDF_DETAILS))
712 fprintf (dump_file, "Deleting : ");
713 print_generic_stmt (dump_file, t, TDF_SLIM);
714 fprintf (dump_file, "\n");
717 stats.removed++;
719 /* If we have determined that a conditional branch statement contributes
720 nothing to the program, then we not only remove it, but we also change
721 the flow graph so that the current block will simply fall-thru to its
722 immediate post-dominator. The blocks we are circumventing will be
723 removed by cleaup_cfg if this change in the flow graph makes them
724 unreachable. */
725 if (is_ctrl_stmt (t))
727 basic_block post_dom_bb;
728 /* The post dominance info has to be up-to-date. */
729 gcc_assert (dom_computed[CDI_POST_DOMINATORS] == DOM_OK);
730 /* Get the immediate post dominator of bb. */
731 post_dom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
732 /* Some blocks don't have an immediate post dominator. This can happen
733 for example with infinite loops. Removing an infinite loop is an
734 inappropriate transformation anyway... */
735 if (! post_dom_bb)
737 bsi_next (i);
738 return;
741 /* Redirect the first edge out of BB to reach POST_DOM_BB. */
742 redirect_edge_and_branch (EDGE_SUCC (bb, 0), post_dom_bb);
743 PENDING_STMT (EDGE_SUCC (bb, 0)) = NULL;
745 /* The edge is no longer associated with a conditional, so it does
746 not have TRUE/FALSE flags. */
747 EDGE_SUCC (bb, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
749 /* If the edge reaches any block other than the exit, then it is a
750 fallthru edge; if it reaches the exit, then it is not a fallthru
751 edge. */
752 if (post_dom_bb != EXIT_BLOCK_PTR)
753 EDGE_SUCC (bb, 0)->flags |= EDGE_FALLTHRU;
754 else
755 EDGE_SUCC (bb, 0)->flags &= ~EDGE_FALLTHRU;
757 /* Remove the remaining the outgoing edges. */
758 while (EDGE_COUNT (bb->succs) != 1)
759 remove_edge (EDGE_SUCC (bb, 1));
762 bsi_remove (i);
763 release_defs (t);
766 /* Print out removed statement statistics. */
768 static void
769 print_stats (void)
771 if (dump_file && (dump_flags & (TDF_STATS|TDF_DETAILS)))
773 float percg;
775 percg = ((float) stats.removed / (float) stats.total) * 100;
776 fprintf (dump_file, "Removed %d of %d statements (%d%%)\n",
777 stats.removed, stats.total, (int) percg);
779 if (stats.total_phis == 0)
780 percg = 0;
781 else
782 percg = ((float) stats.removed_phis / (float) stats.total_phis) * 100;
784 fprintf (dump_file, "Removed %d of %d PHI nodes (%d%%)\n",
785 stats.removed_phis, stats.total_phis, (int) percg);
789 /* Initialization for this pass. Set up the used data structures. */
791 static void
792 tree_dce_init (bool aggressive)
794 memset ((void *) &stats, 0, sizeof (stats));
796 if (aggressive)
798 int i;
800 control_dependence_map
801 = xmalloc (last_basic_block * sizeof (bitmap));
802 for (i = 0; i < last_basic_block; ++i)
803 control_dependence_map[i] = BITMAP_XMALLOC ();
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 VARRAY_TREE_INIT (worklist, 64, "work list");
815 /* Cleanup after this pass. */
817 static void
818 tree_dce_done (bool aggressive)
820 if (aggressive)
822 int i;
824 for (i = 0; i < last_basic_block; ++i)
825 BITMAP_XFREE (control_dependence_map[i]);
826 free (control_dependence_map);
828 sbitmap_free (last_stmt_necessary);
831 sbitmap_free (processed);
834 /* Main routine to eliminate dead code.
836 AGGRESSIVE controls the aggressiveness of the algorithm.
837 In conservative mode, we ignore control dependence and simply declare
838 all but the most trivially dead branches necessary. This mode is fast.
839 In aggressive mode, control dependences are taken into account, which
840 results in more dead code elimination, but at the cost of some time.
842 FIXME: Aggressive mode before PRE doesn't work currently because
843 the dominance info is not invalidated after DCE1. This is
844 not an issue right now because we only run aggressive DCE
845 as the last tree SSA pass, but keep this in mind when you
846 start experimenting with pass ordering. */
848 static void
849 perform_tree_ssa_dce (bool aggressive)
851 struct edge_list *el = NULL;
853 tree_dce_init (aggressive);
855 if (aggressive)
857 /* Compute control dependence. */
858 timevar_push (TV_CONTROL_DEPENDENCES);
859 calculate_dominance_info (CDI_POST_DOMINATORS);
860 el = create_edge_list ();
861 find_all_control_dependences (el);
862 timevar_pop (TV_CONTROL_DEPENDENCES);
864 mark_dfs_back_edges ();
867 find_obviously_necessary_stmts (el);
869 propagate_necessity (el);
871 eliminate_unnecessary_stmts ();
873 if (aggressive)
874 free_dominance_info (CDI_POST_DOMINATORS);
876 cleanup_tree_cfg ();
878 /* Debugging dumps. */
879 if (dump_file)
881 dump_function_to_file (current_function_decl, dump_file, dump_flags);
882 print_stats ();
885 tree_dce_done (aggressive);
887 free_edge_list (el);
890 /* Pass entry points. */
891 static void
892 tree_ssa_dce (void)
894 perform_tree_ssa_dce (/*aggressive=*/false);
897 static void
898 tree_ssa_cd_dce (void)
900 perform_tree_ssa_dce (/*aggressive=*/optimize >= 2);
903 static bool
904 gate_dce (void)
906 return flag_tree_dce != 0;
909 struct tree_opt_pass pass_dce =
911 "dce", /* name */
912 gate_dce, /* gate */
913 tree_ssa_dce, /* execute */
914 NULL, /* sub */
915 NULL, /* next */
916 0, /* static_pass_number */
917 TV_TREE_DCE, /* tv_id */
918 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
919 0, /* properties_provided */
920 0, /* properties_destroyed */
921 0, /* todo_flags_start */
922 TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
923 0 /* letter */
926 struct tree_opt_pass pass_cd_dce =
928 "cddce", /* name */
929 gate_dce, /* gate */
930 tree_ssa_cd_dce, /* execute */
931 NULL, /* sub */
932 NULL, /* next */
933 0, /* static_pass_number */
934 TV_TREE_CD_DCE, /* tv_id */
935 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
936 0, /* properties_provided */
937 0, /* properties_destroyed */
938 0, /* todo_flags_start */
939 TODO_ggc_collect | TODO_verify_ssa | TODO_verify_flow,
940 /* todo_flags_finish */
941 0 /* letter */