Import stripped gcc-4.0.1 sources.
[dragonfly.git] / contrib / gcc-4.0 / gcc / tree-ssa-dce.c
bloba236206ff3927b4d7a73853fd71c8e78a63e957e
1 /* Dead code elimination pass for the GNU compiler.
2 Copyright (C) 2002, 2003, 2004, 2005 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 "obstack.h"
58 #include "basic-block.h"
60 #include "tree.h"
61 #include "diagnostic.h"
62 #include "tree-flow.h"
63 #include "tree-gimple.h"
64 #include "tree-dump.h"
65 #include "tree-pass.h"
66 #include "timevar.h"
67 #include "flags.h"
69 static struct stmt_stats
71 int total;
72 int total_phis;
73 int removed;
74 int removed_phis;
75 } stats;
77 static varray_type worklist;
79 /* Vector indicating an SSA name has already been processed and marked
80 as necessary. */
81 static sbitmap processed;
83 /* Vector indicating that last_stmt if a basic block has already been
84 marked as necessary. */
85 static sbitmap last_stmt_necessary;
87 /* Before we can determine whether a control branch is dead, we need to
88 compute which blocks are control dependent on which edges.
90 We expect each block to be control dependent on very few edges so we
91 use a bitmap for each block recording its edges. An array holds the
92 bitmap. The Ith bit in the bitmap is set if that block is dependent
93 on the Ith edge. */
94 bitmap *control_dependence_map;
96 /* Vector indicating that a basic block has already had all the edges
97 processed that it is control dependent on. */
98 sbitmap visited_control_parents;
100 /* Execute CODE for each edge (given number EDGE_NUMBER within the CODE)
101 for which the block with index N is control dependent. */
102 #define EXECUTE_IF_CONTROL_DEPENDENT(N, EDGE_NUMBER, CODE) \
104 bitmap_iterator bi; \
106 EXECUTE_IF_SET_IN_BITMAP (control_dependence_map[N], 0, EDGE_NUMBER, bi) \
108 CODE; \
112 /* Local function prototypes. */
113 static inline void set_control_dependence_map_bit (basic_block, int);
114 static inline void clear_control_dependence_bitmap (basic_block);
115 static void find_all_control_dependences (struct edge_list *);
116 static void find_control_dependence (struct edge_list *, int);
117 static inline basic_block find_pdom (basic_block);
119 static inline void mark_stmt_necessary (tree, bool);
120 static inline void mark_operand_necessary (tree, bool);
122 static void mark_stmt_if_obviously_necessary (tree, bool);
123 static void find_obviously_necessary_stmts (struct edge_list *);
125 static void mark_control_dependent_edges_necessary (basic_block, struct edge_list *);
126 static void propagate_necessity (struct edge_list *);
128 static void eliminate_unnecessary_stmts (void);
129 static void remove_dead_phis (basic_block);
130 static void remove_dead_stmt (block_stmt_iterator *, basic_block);
132 static void print_stats (void);
133 static void tree_dce_init (bool);
134 static void tree_dce_done (bool);
136 /* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */
137 static inline void
138 set_control_dependence_map_bit (basic_block bb, int edge_index)
140 if (bb == ENTRY_BLOCK_PTR)
141 return;
142 gcc_assert (bb != EXIT_BLOCK_PTR);
143 bitmap_set_bit (control_dependence_map[bb->index], edge_index);
146 /* Clear all control dependences for block BB. */
147 static inline
148 void clear_control_dependence_bitmap (basic_block bb)
150 bitmap_clear (control_dependence_map[bb->index]);
153 /* Record all blocks' control dependences on all edges in the edge
154 list EL, ala Morgan, Section 3.6. */
156 static void
157 find_all_control_dependences (struct edge_list *el)
159 int i;
161 for (i = 0; i < NUM_EDGES (el); ++i)
162 find_control_dependence (el, i);
165 /* Determine all blocks' control dependences on the given edge with edge_list
166 EL index EDGE_INDEX, ala Morgan, Section 3.6. */
168 static void
169 find_control_dependence (struct edge_list *el, int edge_index)
171 basic_block current_block;
172 basic_block ending_block;
174 gcc_assert (INDEX_EDGE_PRED_BB (el, edge_index) != EXIT_BLOCK_PTR);
176 if (INDEX_EDGE_PRED_BB (el, edge_index) == ENTRY_BLOCK_PTR)
177 ending_block = ENTRY_BLOCK_PTR->next_bb;
178 else
179 ending_block = find_pdom (INDEX_EDGE_PRED_BB (el, edge_index));
181 for (current_block = INDEX_EDGE_SUCC_BB (el, edge_index);
182 current_block != ending_block && current_block != EXIT_BLOCK_PTR;
183 current_block = find_pdom (current_block))
185 edge e = INDEX_EDGE (el, edge_index);
187 /* For abnormal edges, we don't make current_block control
188 dependent because instructions that throw are always necessary
189 anyway. */
190 if (e->flags & EDGE_ABNORMAL)
191 continue;
193 set_control_dependence_map_bit (current_block, edge_index);
197 /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
198 This function is necessary because some blocks have negative numbers. */
200 static inline basic_block
201 find_pdom (basic_block block)
203 gcc_assert (block != ENTRY_BLOCK_PTR);
205 if (block == EXIT_BLOCK_PTR)
206 return EXIT_BLOCK_PTR;
207 else
209 basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
210 if (! bb)
211 return EXIT_BLOCK_PTR;
212 return bb;
216 #define NECESSARY(stmt) stmt->common.asm_written_flag
218 /* If STMT is not already marked necessary, mark it, and add it to the
219 worklist if ADD_TO_WORKLIST is true. */
220 static inline void
221 mark_stmt_necessary (tree stmt, bool add_to_worklist)
223 gcc_assert (stmt);
224 gcc_assert (stmt != error_mark_node);
225 gcc_assert (!DECL_P (stmt));
227 if (NECESSARY (stmt))
228 return;
230 if (dump_file && (dump_flags & TDF_DETAILS))
232 fprintf (dump_file, "Marking useful stmt: ");
233 print_generic_stmt (dump_file, stmt, TDF_SLIM);
234 fprintf (dump_file, "\n");
237 NECESSARY (stmt) = 1;
238 if (add_to_worklist)
239 VARRAY_PUSH_TREE (worklist, stmt);
242 /* Mark the statement defining operand OP as necessary. PHIONLY is true
243 if we should only mark it necessary if it is a phi node. */
245 static inline void
246 mark_operand_necessary (tree op, bool phionly)
248 tree stmt;
249 int ver;
251 gcc_assert (op);
253 ver = SSA_NAME_VERSION (op);
254 if (TEST_BIT (processed, ver))
255 return;
256 SET_BIT (processed, ver);
258 stmt = SSA_NAME_DEF_STMT (op);
259 gcc_assert (stmt);
261 if (NECESSARY (stmt)
262 || IS_EMPTY_STMT (stmt)
263 || (phionly && TREE_CODE (stmt) != PHI_NODE))
264 return;
266 NECESSARY (stmt) = 1;
267 VARRAY_PUSH_TREE (worklist, stmt);
271 /* Mark STMT as necessary if it obviously is. Add it to the worklist if
272 it can make other statements necessary.
274 If AGGRESSIVE is false, control statements are conservatively marked as
275 necessary. */
277 static void
278 mark_stmt_if_obviously_necessary (tree stmt, bool aggressive)
280 v_may_def_optype v_may_defs;
281 v_must_def_optype v_must_defs;
282 stmt_ann_t ann;
283 tree op, def;
284 ssa_op_iter iter;
286 /* With non-call exceptions, we have to assume that all statements could
287 throw. If a statement may throw, it is inherently necessary. */
288 if (flag_non_call_exceptions
289 && tree_could_throw_p (stmt))
291 mark_stmt_necessary (stmt, true);
292 return;
295 /* Statements that are implicitly live. Most function calls, asm and return
296 statements are required. Labels and BIND_EXPR nodes are kept because
297 they are control flow, and we have no way of knowing whether they can be
298 removed. DCE can eliminate all the other statements in a block, and CFG
299 can then remove the block and labels. */
300 switch (TREE_CODE (stmt))
302 case BIND_EXPR:
303 case LABEL_EXPR:
304 case CASE_LABEL_EXPR:
305 mark_stmt_necessary (stmt, false);
306 return;
308 case ASM_EXPR:
309 case RESX_EXPR:
310 case RETURN_EXPR:
311 mark_stmt_necessary (stmt, true);
312 return;
314 case CALL_EXPR:
315 /* Most, but not all function calls are required. Function calls that
316 produce no result and have no side effects (i.e. const pure
317 functions) are unnecessary. */
318 if (TREE_SIDE_EFFECTS (stmt))
319 mark_stmt_necessary (stmt, true);
320 return;
322 case MODIFY_EXPR:
323 op = get_call_expr_in (stmt);
324 if (op && TREE_SIDE_EFFECTS (op))
326 mark_stmt_necessary (stmt, true);
327 return;
330 /* These values are mildly magic bits of the EH runtime. We can't
331 see the entire lifetime of these values until landing pads are
332 generated. */
333 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == EXC_PTR_EXPR
334 || TREE_CODE (TREE_OPERAND (stmt, 0)) == FILTER_EXPR)
336 mark_stmt_necessary (stmt, true);
337 return;
339 break;
341 case GOTO_EXPR:
342 gcc_assert (!simple_goto_p (stmt));
343 mark_stmt_necessary (stmt, true);
344 return;
346 case COND_EXPR:
347 gcc_assert (EDGE_COUNT (bb_for_stmt (stmt)->succs) == 2);
348 /* Fall through. */
350 case SWITCH_EXPR:
351 if (! aggressive)
352 mark_stmt_necessary (stmt, true);
353 break;
355 default:
356 break;
359 ann = stmt_ann (stmt);
361 /* If the statement has volatile operands, it needs to be preserved.
362 Same for statements that can alter control flow in unpredictable
363 ways. */
364 if (ann->has_volatile_ops || is_ctrl_altering_stmt (stmt))
366 mark_stmt_necessary (stmt, true);
367 return;
370 get_stmt_operands (stmt);
372 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
374 if (is_global_var (SSA_NAME_VAR (def)))
376 mark_stmt_necessary (stmt, true);
377 return;
381 /* Check virtual definitions. If we get here, the only virtual
382 definitions we should see are those generated by assignment
383 statements. */
384 v_may_defs = V_MAY_DEF_OPS (ann);
385 v_must_defs = V_MUST_DEF_OPS (ann);
386 if (NUM_V_MAY_DEFS (v_may_defs) > 0 || NUM_V_MUST_DEFS (v_must_defs) > 0)
388 tree lhs;
390 gcc_assert (TREE_CODE (stmt) == MODIFY_EXPR);
392 /* Note that we must not check the individual virtual operands
393 here. In particular, if this is an aliased store, we could
394 end up with something like the following (SSA notation
395 redacted for brevity):
397 foo (int *p, int i)
399 int x;
400 p_1 = (i_2 > 3) ? &x : p_1;
402 # x_4 = V_MAY_DEF <x_3>
403 *p_1 = 5;
405 return 2;
408 Notice that the store to '*p_1' should be preserved, if we
409 were to check the virtual definitions in that store, we would
410 not mark it needed. This is because 'x' is not a global
411 variable.
413 Therefore, we check the base address of the LHS. If the
414 address is a pointer, we check if its name tag or type tag is
415 a global variable. Otherwise, we check if the base variable
416 is a global. */
417 lhs = TREE_OPERAND (stmt, 0);
418 if (REFERENCE_CLASS_P (lhs))
419 lhs = get_base_address (lhs);
421 if (lhs == NULL_TREE)
423 /* If LHS is NULL, it means that we couldn't get the base
424 address of the reference. In which case, we should not
425 remove this store. */
426 mark_stmt_necessary (stmt, true);
428 else if (DECL_P (lhs))
430 /* If the store is to a global symbol, we need to keep it. */
431 if (is_global_var (lhs))
432 mark_stmt_necessary (stmt, true);
434 else if (INDIRECT_REF_P (lhs))
436 tree ptr = TREE_OPERAND (lhs, 0);
437 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
438 tree nmt = (pi) ? pi->name_mem_tag : NULL_TREE;
439 tree tmt = var_ann (SSA_NAME_VAR (ptr))->type_mem_tag;
441 /* If either the name tag or the type tag for PTR is a
442 global variable, then the store is necessary. */
443 if ((nmt && is_global_var (nmt))
444 || (tmt && is_global_var (tmt)))
446 mark_stmt_necessary (stmt, true);
447 return;
450 else
451 gcc_unreachable ();
454 return;
457 /* Find obviously necessary statements. These are things like most function
458 calls, and stores to file level variables.
460 If EL is NULL, control statements are conservatively marked as
461 necessary. Otherwise it contains the list of edges used by control
462 dependence analysis. */
464 static void
465 find_obviously_necessary_stmts (struct edge_list *el)
467 basic_block bb;
468 block_stmt_iterator i;
469 edge e;
471 FOR_EACH_BB (bb)
473 tree phi;
475 /* Check any PHI nodes in the block. */
476 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
478 NECESSARY (phi) = 0;
480 /* PHIs for virtual variables do not directly affect code
481 generation and need not be considered inherently necessary
482 regardless of the bits set in their decl.
484 Thus, we only need to mark PHIs for real variables which
485 need their result preserved as being inherently necessary. */
486 if (is_gimple_reg (PHI_RESULT (phi))
487 && is_global_var (SSA_NAME_VAR (PHI_RESULT (phi))))
488 mark_stmt_necessary (phi, true);
491 /* Check all statements in the block. */
492 for (i = bsi_start (bb); ! bsi_end_p (i); bsi_next (&i))
494 tree stmt = bsi_stmt (i);
495 NECESSARY (stmt) = 0;
496 mark_stmt_if_obviously_necessary (stmt, el != NULL);
500 if (el)
502 /* Prevent the loops from being removed. We must keep the infinite loops,
503 and we currently do not have a means to recognize the finite ones. */
504 FOR_EACH_BB (bb)
506 edge_iterator ei;
507 FOR_EACH_EDGE (e, ei, bb->succs)
508 if (e->flags & EDGE_DFS_BACK)
509 mark_control_dependent_edges_necessary (e->dest, el);
514 /* Make corresponding control dependent edges necessary. We only
515 have to do this once for each basic block, so we clear the bitmap
516 after we're done. */
517 static void
518 mark_control_dependent_edges_necessary (basic_block bb, struct edge_list *el)
520 unsigned edge_number;
522 gcc_assert (bb != EXIT_BLOCK_PTR);
524 if (bb == ENTRY_BLOCK_PTR)
525 return;
527 EXECUTE_IF_CONTROL_DEPENDENT (bb->index, edge_number,
529 tree t;
530 basic_block cd_bb = INDEX_EDGE_PRED_BB (el, edge_number);
532 if (TEST_BIT (last_stmt_necessary, cd_bb->index))
533 continue;
534 SET_BIT (last_stmt_necessary, cd_bb->index);
536 t = last_stmt (cd_bb);
537 if (t && is_ctrl_stmt (t))
538 mark_stmt_necessary (t, true);
542 /* Propagate necessity using the operands of necessary statements. Process
543 the uses on each statement in the worklist, and add all feeding statements
544 which contribute to the calculation of this value to the worklist.
546 In conservative mode, EL is NULL. */
548 static void
549 propagate_necessity (struct edge_list *el)
551 tree i;
552 bool aggressive = (el ? true : false);
554 if (dump_file && (dump_flags & TDF_DETAILS))
555 fprintf (dump_file, "\nProcessing worklist:\n");
557 while (VARRAY_ACTIVE_SIZE (worklist) > 0)
559 /* Take `i' from worklist. */
560 i = VARRAY_TOP_TREE (worklist);
561 VARRAY_POP (worklist);
563 if (dump_file && (dump_flags & TDF_DETAILS))
565 fprintf (dump_file, "processing: ");
566 print_generic_stmt (dump_file, i, TDF_SLIM);
567 fprintf (dump_file, "\n");
570 if (aggressive)
572 /* Mark the last statements of the basic blocks that the block
573 containing `i' is control dependent on, but only if we haven't
574 already done so. */
575 basic_block bb = bb_for_stmt (i);
576 if (bb != ENTRY_BLOCK_PTR
577 && ! TEST_BIT (visited_control_parents, bb->index))
579 SET_BIT (visited_control_parents, bb->index);
580 mark_control_dependent_edges_necessary (bb, el);
584 if (TREE_CODE (i) == PHI_NODE)
586 /* PHI nodes are somewhat special in that each PHI alternative has
587 data and control dependencies. All the statements feeding the
588 PHI node's arguments are always necessary. In aggressive mode,
589 we also consider the control dependent edges leading to the
590 predecessor block associated with each PHI alternative as
591 necessary. */
592 int k;
593 for (k = 0; k < PHI_NUM_ARGS (i); k++)
595 tree arg = PHI_ARG_DEF (i, k);
596 if (TREE_CODE (arg) == SSA_NAME)
597 mark_operand_necessary (arg, false);
600 if (aggressive)
602 for (k = 0; k < PHI_NUM_ARGS (i); k++)
604 basic_block arg_bb = PHI_ARG_EDGE (i, k)->src;
605 if (arg_bb != ENTRY_BLOCK_PTR
606 && ! TEST_BIT (visited_control_parents, arg_bb->index))
608 SET_BIT (visited_control_parents, arg_bb->index);
609 mark_control_dependent_edges_necessary (arg_bb, el);
614 else
616 /* Propagate through the operands. Examine all the USE, VUSE and
617 V_MAY_DEF operands in this statement. Mark all the statements
618 which feed this statement's uses as necessary. */
619 ssa_op_iter iter;
620 tree use;
622 get_stmt_operands (i);
624 /* The operands of V_MAY_DEF expressions are also needed as they
625 represent potential definitions that may reach this
626 statement (V_MAY_DEF operands allow us to follow def-def
627 links). */
629 FOR_EACH_SSA_TREE_OPERAND (use, i, iter, SSA_OP_ALL_USES)
630 mark_operand_necessary (use, false);
636 /* Propagate necessity around virtual phi nodes used in kill operands.
637 The reason this isn't done during propagate_necessity is because we don't
638 want to keep phis around that are just there for must-defs, unless we
639 absolutely have to. After we've rewritten the reaching definitions to be
640 correct in the previous part of the fixup routine, we can simply propagate
641 around the information about which of these virtual phi nodes are really
642 used, and set the NECESSARY flag accordingly.
643 Note that we do the minimum here to ensure that we keep alive the phis that
644 are actually used in the corrected SSA form. In particular, some of these
645 phis may now have all of the same operand, and will be deleted by some
646 other pass. */
648 static void
649 mark_really_necessary_kill_operand_phis (void)
651 basic_block bb;
652 int i;
654 /* Seed the worklist with the new virtual phi arguments and virtual
655 uses */
656 FOR_EACH_BB (bb)
658 block_stmt_iterator bsi;
659 tree phi;
661 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
663 if (!is_gimple_reg (PHI_RESULT (phi)) && NECESSARY (phi))
665 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
666 mark_operand_necessary (PHI_ARG_DEF (phi, i), true);
670 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
672 tree stmt = bsi_stmt (bsi);
674 if (NECESSARY (stmt))
676 use_operand_p use_p;
677 ssa_op_iter iter;
678 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
679 SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_KILLS)
681 tree use = USE_FROM_PTR (use_p);
682 mark_operand_necessary (use, true);
688 /* Mark all virtual phis still in use as necessary, and all of their
689 arguments that are phis as necessary. */
690 while (VARRAY_ACTIVE_SIZE (worklist) > 0)
692 tree use = VARRAY_TOP_TREE (worklist);
693 VARRAY_POP (worklist);
695 for (i = 0; i < PHI_NUM_ARGS (use); i++)
696 mark_operand_necessary (PHI_ARG_DEF (use, i), true);
703 /* Eliminate unnecessary statements. Any instruction not marked as necessary
704 contributes nothing to the program, and can be deleted. */
706 static void
707 eliminate_unnecessary_stmts (void)
709 basic_block bb;
710 block_stmt_iterator i;
712 if (dump_file && (dump_flags & TDF_DETAILS))
713 fprintf (dump_file, "\nEliminating unnecessary statements:\n");
715 clear_special_calls ();
716 FOR_EACH_BB (bb)
718 /* Remove dead PHI nodes. */
719 remove_dead_phis (bb);
722 FOR_EACH_BB (bb)
724 /* Remove dead statements. */
725 for (i = bsi_start (bb); ! bsi_end_p (i) ; )
727 tree t = bsi_stmt (i);
729 stats.total++;
731 /* If `i' is not necessary then remove it. */
732 if (! NECESSARY (t))
733 remove_dead_stmt (&i, bb);
734 else
736 tree call = get_call_expr_in (t);
737 if (call)
738 notice_special_calls (call);
739 bsi_next (&i);
745 /* Remove dead PHI nodes from block BB. */
747 static void
748 remove_dead_phis (basic_block bb)
750 tree prev, phi;
752 prev = NULL_TREE;
753 phi = phi_nodes (bb);
754 while (phi)
756 stats.total_phis++;
758 if (! NECESSARY (phi))
760 tree next = PHI_CHAIN (phi);
762 if (dump_file && (dump_flags & TDF_DETAILS))
764 fprintf (dump_file, "Deleting : ");
765 print_generic_stmt (dump_file, phi, TDF_SLIM);
766 fprintf (dump_file, "\n");
769 remove_phi_node (phi, prev, bb);
770 stats.removed_phis++;
771 phi = next;
773 else
775 prev = phi;
776 phi = PHI_CHAIN (phi);
781 /* Remove dead statement pointed by iterator I. Receives the basic block BB
782 containing I so that we don't have to look it up. */
784 static void
785 remove_dead_stmt (block_stmt_iterator *i, basic_block bb)
787 tree t = bsi_stmt (*i);
788 def_operand_p def_p;
790 ssa_op_iter iter;
792 if (dump_file && (dump_flags & TDF_DETAILS))
794 fprintf (dump_file, "Deleting : ");
795 print_generic_stmt (dump_file, t, TDF_SLIM);
796 fprintf (dump_file, "\n");
799 stats.removed++;
801 /* If we have determined that a conditional branch statement contributes
802 nothing to the program, then we not only remove it, but we also change
803 the flow graph so that the current block will simply fall-thru to its
804 immediate post-dominator. The blocks we are circumventing will be
805 removed by cleaup_cfg if this change in the flow graph makes them
806 unreachable. */
807 if (is_ctrl_stmt (t))
809 basic_block post_dom_bb;
811 /* The post dominance info has to be up-to-date. */
812 gcc_assert (dom_computed[CDI_POST_DOMINATORS] == DOM_OK);
813 /* Get the immediate post dominator of bb. */
814 post_dom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
815 /* Some blocks don't have an immediate post dominator. This can happen
816 for example with infinite loops. Removing an infinite loop is an
817 inappropriate transformation anyway... */
818 if (! post_dom_bb)
820 bsi_next (i);
821 return;
824 /* If the post dominator block has PHI nodes, we might be unable
825 to compute the right PHI args for them. Since the control
826 statement is unnecessary, all edges can be regarded as
827 equivalent, but we have to get rid of the condition, since it
828 might reference a variable that was determined to be
829 unnecessary and thus removed. */
830 if (phi_nodes (post_dom_bb))
831 post_dom_bb = EDGE_SUCC (bb, 0)->dest;
832 else
834 /* Redirect the first edge out of BB to reach POST_DOM_BB. */
835 redirect_edge_and_branch (EDGE_SUCC (bb, 0), post_dom_bb);
836 PENDING_STMT (EDGE_SUCC (bb, 0)) = NULL;
838 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
839 EDGE_SUCC (bb, 0)->count = bb->count;
841 /* The edge is no longer associated with a conditional, so it does
842 not have TRUE/FALSE flags. */
843 EDGE_SUCC (bb, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
845 /* If the edge reaches any block other than the exit, then it is a
846 fallthru edge; if it reaches the exit, then it is not a fallthru
847 edge. */
848 if (post_dom_bb != EXIT_BLOCK_PTR)
849 EDGE_SUCC (bb, 0)->flags |= EDGE_FALLTHRU;
850 else
851 EDGE_SUCC (bb, 0)->flags &= ~EDGE_FALLTHRU;
853 /* Remove the remaining the outgoing edges. */
854 while (EDGE_COUNT (bb->succs) != 1)
855 remove_edge (EDGE_SUCC (bb, 1));
858 FOR_EACH_SSA_DEF_OPERAND (def_p, t, iter,
859 SSA_OP_VIRTUAL_DEFS | SSA_OP_VIRTUAL_KILLS)
861 tree def = DEF_FROM_PTR (def_p);
862 bitmap_set_bit (vars_to_rename,
863 var_ann (SSA_NAME_VAR (def))->uid);
865 bsi_remove (i);
866 release_defs (t);
869 /* Print out removed statement statistics. */
871 static void
872 print_stats (void)
874 if (dump_file && (dump_flags & (TDF_STATS|TDF_DETAILS)))
876 float percg;
878 percg = ((float) stats.removed / (float) stats.total) * 100;
879 fprintf (dump_file, "Removed %d of %d statements (%d%%)\n",
880 stats.removed, stats.total, (int) percg);
882 if (stats.total_phis == 0)
883 percg = 0;
884 else
885 percg = ((float) stats.removed_phis / (float) stats.total_phis) * 100;
887 fprintf (dump_file, "Removed %d of %d PHI nodes (%d%%)\n",
888 stats.removed_phis, stats.total_phis, (int) percg);
892 /* Initialization for this pass. Set up the used data structures. */
894 static void
895 tree_dce_init (bool aggressive)
897 memset ((void *) &stats, 0, sizeof (stats));
899 if (aggressive)
901 int i;
903 control_dependence_map
904 = xmalloc (last_basic_block * sizeof (bitmap));
905 for (i = 0; i < last_basic_block; ++i)
906 control_dependence_map[i] = BITMAP_ALLOC (NULL);
908 last_stmt_necessary = sbitmap_alloc (last_basic_block);
909 sbitmap_zero (last_stmt_necessary);
912 processed = sbitmap_alloc (num_ssa_names + 1);
913 sbitmap_zero (processed);
915 VARRAY_TREE_INIT (worklist, 64, "work list");
918 /* Cleanup after this pass. */
920 static void
921 tree_dce_done (bool aggressive)
923 if (aggressive)
925 int i;
927 for (i = 0; i < last_basic_block; ++i)
928 BITMAP_FREE (control_dependence_map[i]);
929 free (control_dependence_map);
931 sbitmap_free (visited_control_parents);
932 sbitmap_free (last_stmt_necessary);
935 sbitmap_free (processed);
938 /* Main routine to eliminate dead code.
940 AGGRESSIVE controls the aggressiveness of the algorithm.
941 In conservative mode, we ignore control dependence and simply declare
942 all but the most trivially dead branches necessary. This mode is fast.
943 In aggressive mode, control dependences are taken into account, which
944 results in more dead code elimination, but at the cost of some time.
946 FIXME: Aggressive mode before PRE doesn't work currently because
947 the dominance info is not invalidated after DCE1. This is
948 not an issue right now because we only run aggressive DCE
949 as the last tree SSA pass, but keep this in mind when you
950 start experimenting with pass ordering. */
952 static void
953 perform_tree_ssa_dce (bool aggressive)
955 struct edge_list *el = NULL;
957 tree_dce_init (aggressive);
959 if (aggressive)
961 /* Compute control dependence. */
962 timevar_push (TV_CONTROL_DEPENDENCES);
963 calculate_dominance_info (CDI_POST_DOMINATORS);
964 el = create_edge_list ();
965 find_all_control_dependences (el);
966 timevar_pop (TV_CONTROL_DEPENDENCES);
968 visited_control_parents = sbitmap_alloc (last_basic_block);
969 sbitmap_zero (visited_control_parents);
971 mark_dfs_back_edges ();
974 find_obviously_necessary_stmts (el);
976 propagate_necessity (el);
978 mark_really_necessary_kill_operand_phis ();
979 eliminate_unnecessary_stmts ();
981 if (aggressive)
982 free_dominance_info (CDI_POST_DOMINATORS);
984 /* Debugging dumps. */
985 if (dump_file)
986 print_stats ();
988 tree_dce_done (aggressive);
990 free_edge_list (el);
993 /* Pass entry points. */
994 static void
995 tree_ssa_dce (void)
997 perform_tree_ssa_dce (/*aggressive=*/false);
1000 static void
1001 tree_ssa_cd_dce (void)
1003 perform_tree_ssa_dce (/*aggressive=*/optimize >= 2);
1006 static bool
1007 gate_dce (void)
1009 return flag_tree_dce != 0;
1012 struct tree_opt_pass pass_dce =
1014 "dce", /* name */
1015 gate_dce, /* gate */
1016 tree_ssa_dce, /* execute */
1017 NULL, /* sub */
1018 NULL, /* next */
1019 0, /* static_pass_number */
1020 TV_TREE_DCE, /* tv_id */
1021 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
1022 0, /* properties_provided */
1023 0, /* properties_destroyed */
1024 0, /* todo_flags_start */
1025 TODO_dump_func | TODO_fix_def_def_chains | TODO_cleanup_cfg | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
1026 0 /* letter */
1029 struct tree_opt_pass pass_cd_dce =
1031 "cddce", /* name */
1032 gate_dce, /* gate */
1033 tree_ssa_cd_dce, /* execute */
1034 NULL, /* sub */
1035 NULL, /* next */
1036 0, /* static_pass_number */
1037 TV_TREE_CD_DCE, /* tv_id */
1038 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
1039 0, /* properties_provided */
1040 0, /* properties_destroyed */
1041 0, /* todo_flags_start */
1042 TODO_dump_func | TODO_fix_def_def_chains | TODO_cleanup_cfg | TODO_ggc_collect | TODO_verify_ssa | TODO_verify_flow,
1043 /* todo_flags_finish */
1044 0 /* letter */