2014-03-14 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-dce.c
blob13a71ceea0b025d8c2235f64d3c730cc093970fd
1 /* Dead code elimination pass for the GNU compiler.
2 Copyright (C) 2002-2014 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 3, 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 COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Dead code elimination.
25 References:
27 Building an Optimizing Compiler,
28 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
30 Advanced Compiler Design and Implementation,
31 Steven Muchnick, Morgan Kaufmann, 1997, Section 18.10.
33 Dead-code elimination is the removal of statements which have no
34 impact on the program's output. "Dead statements" have no impact
35 on the program's output, while "necessary statements" may have
36 impact on the output.
38 The algorithm consists of three phases:
39 1. Marking as necessary all statements known to be necessary,
40 e.g. most function calls, writing a value to memory, etc;
41 2. Propagating necessary statements, e.g., the statements
42 giving values to operands in necessary statements; and
43 3. Removing dead statements. */
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
50 #include "tree.h"
51 #include "calls.h"
52 #include "gimple-pretty-print.h"
53 #include "basic-block.h"
54 #include "tree-ssa-alias.h"
55 #include "internal-fn.h"
56 #include "tree-eh.h"
57 #include "gimple-expr.h"
58 #include "is-a.h"
59 #include "gimple.h"
60 #include "gimplify.h"
61 #include "gimple-iterator.h"
62 #include "gimple-ssa.h"
63 #include "tree-cfg.h"
64 #include "tree-phinodes.h"
65 #include "ssa-iterators.h"
66 #include "stringpool.h"
67 #include "tree-ssanames.h"
68 #include "tree-ssa-loop-niter.h"
69 #include "tree-into-ssa.h"
70 #include "expr.h"
71 #include "tree-dfa.h"
72 #include "tree-pass.h"
73 #include "flags.h"
74 #include "cfgloop.h"
75 #include "tree-scalar-evolution.h"
77 static struct stmt_stats
79 int total;
80 int total_phis;
81 int removed;
82 int removed_phis;
83 } stats;
85 #define STMT_NECESSARY GF_PLF_1
87 static vec<gimple> worklist;
89 /* Vector indicating an SSA name has already been processed and marked
90 as necessary. */
91 static sbitmap processed;
93 /* Vector indicating that the last statement of a basic block has already
94 been marked as necessary. */
95 static sbitmap last_stmt_necessary;
97 /* Vector indicating that BB contains statements that are live. */
98 static sbitmap bb_contains_live_stmts;
100 /* Before we can determine whether a control branch is dead, we need to
101 compute which blocks are control dependent on which edges.
103 We expect each block to be control dependent on very few edges so we
104 use a bitmap for each block recording its edges. An array holds the
105 bitmap. The Ith bit in the bitmap is set if that block is dependent
106 on the Ith edge. */
107 static control_dependences *cd;
109 /* Vector indicating that a basic block has already had all the edges
110 processed that it is control dependent on. */
111 static sbitmap visited_control_parents;
113 /* TRUE if this pass alters the CFG (by removing control statements).
114 FALSE otherwise.
116 If this pass alters the CFG, then it will arrange for the dominators
117 to be recomputed. */
118 static bool cfg_altered;
121 /* If STMT is not already marked necessary, mark it, and add it to the
122 worklist if ADD_TO_WORKLIST is true. */
124 static inline void
125 mark_stmt_necessary (gimple stmt, bool add_to_worklist)
127 gcc_assert (stmt);
129 if (gimple_plf (stmt, STMT_NECESSARY))
130 return;
132 if (dump_file && (dump_flags & TDF_DETAILS))
134 fprintf (dump_file, "Marking useful stmt: ");
135 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
136 fprintf (dump_file, "\n");
139 gimple_set_plf (stmt, STMT_NECESSARY, true);
140 if (add_to_worklist)
141 worklist.safe_push (stmt);
142 if (bb_contains_live_stmts && !is_gimple_debug (stmt))
143 bitmap_set_bit (bb_contains_live_stmts, gimple_bb (stmt)->index);
147 /* Mark the statement defining operand OP as necessary. */
149 static inline void
150 mark_operand_necessary (tree op)
152 gimple stmt;
153 int ver;
155 gcc_assert (op);
157 ver = SSA_NAME_VERSION (op);
158 if (bitmap_bit_p (processed, ver))
160 stmt = SSA_NAME_DEF_STMT (op);
161 gcc_assert (gimple_nop_p (stmt)
162 || gimple_plf (stmt, STMT_NECESSARY));
163 return;
165 bitmap_set_bit (processed, ver);
167 stmt = SSA_NAME_DEF_STMT (op);
168 gcc_assert (stmt);
170 if (gimple_plf (stmt, STMT_NECESSARY) || gimple_nop_p (stmt))
171 return;
173 if (dump_file && (dump_flags & TDF_DETAILS))
175 fprintf (dump_file, "marking necessary through ");
176 print_generic_expr (dump_file, op, 0);
177 fprintf (dump_file, " stmt ");
178 print_gimple_stmt (dump_file, stmt, 0, 0);
181 gimple_set_plf (stmt, STMT_NECESSARY, true);
182 if (bb_contains_live_stmts)
183 bitmap_set_bit (bb_contains_live_stmts, gimple_bb (stmt)->index);
184 worklist.safe_push (stmt);
188 /* Mark STMT as necessary if it obviously is. Add it to the worklist if
189 it can make other statements necessary.
191 If AGGRESSIVE is false, control statements are conservatively marked as
192 necessary. */
194 static void
195 mark_stmt_if_obviously_necessary (gimple stmt, bool aggressive)
197 /* With non-call exceptions, we have to assume that all statements could
198 throw. If a statement could throw, it can be deemed necessary. */
199 if (cfun->can_throw_non_call_exceptions
200 && !cfun->can_delete_dead_exceptions
201 && stmt_could_throw_p (stmt))
203 mark_stmt_necessary (stmt, true);
204 return;
207 /* Statements that are implicitly live. Most function calls, asm
208 and return statements are required. Labels and GIMPLE_BIND nodes
209 are kept because they are control flow, and we have no way of
210 knowing whether they can be removed. DCE can eliminate all the
211 other statements in a block, and CFG can then remove the block
212 and labels. */
213 switch (gimple_code (stmt))
215 case GIMPLE_PREDICT:
216 case GIMPLE_LABEL:
217 mark_stmt_necessary (stmt, false);
218 return;
220 case GIMPLE_ASM:
221 case GIMPLE_RESX:
222 case GIMPLE_RETURN:
223 mark_stmt_necessary (stmt, true);
224 return;
226 case GIMPLE_CALL:
228 tree callee = gimple_call_fndecl (stmt);
229 if (callee != NULL_TREE
230 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
231 switch (DECL_FUNCTION_CODE (callee))
233 case BUILT_IN_MALLOC:
234 case BUILT_IN_CALLOC:
235 case BUILT_IN_ALLOCA:
236 case BUILT_IN_ALLOCA_WITH_ALIGN:
237 return;
239 default:;
241 /* Most, but not all function calls are required. Function calls that
242 produce no result and have no side effects (i.e. const pure
243 functions) are unnecessary. */
244 if (gimple_has_side_effects (stmt))
246 mark_stmt_necessary (stmt, true);
247 return;
249 if (!gimple_call_lhs (stmt))
250 return;
251 break;
254 case GIMPLE_DEBUG:
255 /* Debug temps without a value are not useful. ??? If we could
256 easily locate the debug temp bind stmt for a use thereof,
257 would could refrain from marking all debug temps here, and
258 mark them only if they're used. */
259 if (!gimple_debug_bind_p (stmt)
260 || gimple_debug_bind_has_value_p (stmt)
261 || TREE_CODE (gimple_debug_bind_get_var (stmt)) != DEBUG_EXPR_DECL)
262 mark_stmt_necessary (stmt, false);
263 return;
265 case GIMPLE_GOTO:
266 gcc_assert (!simple_goto_p (stmt));
267 mark_stmt_necessary (stmt, true);
268 return;
270 case GIMPLE_COND:
271 gcc_assert (EDGE_COUNT (gimple_bb (stmt)->succs) == 2);
272 /* Fall through. */
274 case GIMPLE_SWITCH:
275 if (! aggressive)
276 mark_stmt_necessary (stmt, true);
277 break;
279 case GIMPLE_ASSIGN:
280 if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
281 && TREE_CLOBBER_P (gimple_assign_rhs1 (stmt)))
282 return;
283 break;
285 default:
286 break;
289 /* If the statement has volatile operands, it needs to be preserved.
290 Same for statements that can alter control flow in unpredictable
291 ways. */
292 if (gimple_has_volatile_ops (stmt) || is_ctrl_altering_stmt (stmt))
294 mark_stmt_necessary (stmt, true);
295 return;
298 if (stmt_may_clobber_global_p (stmt))
300 mark_stmt_necessary (stmt, true);
301 return;
304 return;
308 /* Mark the last statement of BB as necessary. */
310 static void
311 mark_last_stmt_necessary (basic_block bb)
313 gimple stmt = last_stmt (bb);
315 bitmap_set_bit (last_stmt_necessary, bb->index);
316 bitmap_set_bit (bb_contains_live_stmts, bb->index);
318 /* We actually mark the statement only if it is a control statement. */
319 if (stmt && is_ctrl_stmt (stmt))
320 mark_stmt_necessary (stmt, true);
324 /* Mark control dependent edges of BB as necessary. We have to do this only
325 once for each basic block so we set the appropriate bit after we're done.
327 When IGNORE_SELF is true, ignore BB in the list of control dependences. */
329 static void
330 mark_control_dependent_edges_necessary (basic_block bb, bool ignore_self)
332 bitmap_iterator bi;
333 unsigned edge_number;
334 bool skipped = false;
336 gcc_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
338 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
339 return;
341 EXECUTE_IF_SET_IN_BITMAP (cd->get_edges_dependent_on (bb->index),
342 0, edge_number, bi)
344 basic_block cd_bb = cd->get_edge (edge_number)->src;
346 if (ignore_self && cd_bb == bb)
348 skipped = true;
349 continue;
352 if (!bitmap_bit_p (last_stmt_necessary, cd_bb->index))
353 mark_last_stmt_necessary (cd_bb);
356 if (!skipped)
357 bitmap_set_bit (visited_control_parents, bb->index);
361 /* Find obviously necessary statements. These are things like most function
362 calls, and stores to file level variables.
364 If EL is NULL, control statements are conservatively marked as
365 necessary. Otherwise it contains the list of edges used by control
366 dependence analysis. */
368 static void
369 find_obviously_necessary_stmts (bool aggressive)
371 basic_block bb;
372 gimple_stmt_iterator gsi;
373 edge e;
374 gimple phi, stmt;
375 int flags;
377 FOR_EACH_BB_FN (bb, cfun)
379 /* PHI nodes are never inherently necessary. */
380 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
382 phi = gsi_stmt (gsi);
383 gimple_set_plf (phi, STMT_NECESSARY, false);
386 /* Check all statements in the block. */
387 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
389 stmt = gsi_stmt (gsi);
390 gimple_set_plf (stmt, STMT_NECESSARY, false);
391 mark_stmt_if_obviously_necessary (stmt, aggressive);
395 /* Pure and const functions are finite and thus have no infinite loops in
396 them. */
397 flags = flags_from_decl_or_type (current_function_decl);
398 if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
399 return;
401 /* Prevent the empty possibly infinite loops from being removed. */
402 if (aggressive)
404 struct loop *loop;
405 scev_initialize ();
406 if (mark_irreducible_loops ())
407 FOR_EACH_BB_FN (bb, cfun)
409 edge_iterator ei;
410 FOR_EACH_EDGE (e, ei, bb->succs)
411 if ((e->flags & EDGE_DFS_BACK)
412 && (e->flags & EDGE_IRREDUCIBLE_LOOP))
414 if (dump_file)
415 fprintf (dump_file, "Marking back edge of irreducible loop %i->%i\n",
416 e->src->index, e->dest->index);
417 mark_control_dependent_edges_necessary (e->dest, false);
421 FOR_EACH_LOOP (loop, 0)
422 if (!finite_loop_p (loop))
424 if (dump_file)
425 fprintf (dump_file, "can not prove finiteness of loop %i\n", loop->num);
426 mark_control_dependent_edges_necessary (loop->latch, false);
428 scev_finalize ();
433 /* Return true if REF is based on an aliased base, otherwise false. */
435 static bool
436 ref_may_be_aliased (tree ref)
438 gcc_assert (TREE_CODE (ref) != WITH_SIZE_EXPR);
439 while (handled_component_p (ref))
440 ref = TREE_OPERAND (ref, 0);
441 if (TREE_CODE (ref) == MEM_REF
442 && TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
443 ref = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
444 return !(DECL_P (ref)
445 && !may_be_aliased (ref));
448 static bitmap visited = NULL;
449 static unsigned int longest_chain = 0;
450 static unsigned int total_chain = 0;
451 static unsigned int nr_walks = 0;
452 static bool chain_ovfl = false;
454 /* Worker for the walker that marks reaching definitions of REF,
455 which is based on a non-aliased decl, necessary. It returns
456 true whenever the defining statement of the current VDEF is
457 a kill for REF, as no dominating may-defs are necessary for REF
458 anymore. DATA points to the basic-block that contains the
459 stmt that refers to REF. */
461 static bool
462 mark_aliased_reaching_defs_necessary_1 (ao_ref *ref, tree vdef, void *data)
464 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
466 /* All stmts we visit are necessary. */
467 mark_operand_necessary (vdef);
469 /* If the stmt lhs kills ref, then we can stop walking. */
470 if (gimple_has_lhs (def_stmt)
471 && TREE_CODE (gimple_get_lhs (def_stmt)) != SSA_NAME
472 /* The assignment is not necessarily carried out if it can throw
473 and we can catch it in the current function where we could inspect
474 the previous value.
475 ??? We only need to care about the RHS throwing. For aggregate
476 assignments or similar calls and non-call exceptions the LHS
477 might throw as well. */
478 && !stmt_can_throw_internal (def_stmt))
480 tree base, lhs = gimple_get_lhs (def_stmt);
481 HOST_WIDE_INT size, offset, max_size;
482 ao_ref_base (ref);
483 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
484 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
485 so base == refd->base does not always hold. */
486 if (base == ref->base)
488 /* For a must-alias check we need to be able to constrain
489 the accesses properly. */
490 if (size != -1 && size == max_size
491 && ref->max_size != -1)
493 if (offset <= ref->offset
494 && offset + size >= ref->offset + ref->max_size)
495 return true;
497 /* Or they need to be exactly the same. */
498 else if (ref->ref
499 /* Make sure there is no induction variable involved
500 in the references (gcc.c-torture/execute/pr42142.c).
501 The simplest way is to check if the kill dominates
502 the use. */
503 /* But when both are in the same block we cannot
504 easily tell whether we came from a backedge
505 unless we decide to compute stmt UIDs
506 (see PR58246). */
507 && (basic_block) data != gimple_bb (def_stmt)
508 && dominated_by_p (CDI_DOMINATORS, (basic_block) data,
509 gimple_bb (def_stmt))
510 && operand_equal_p (ref->ref, lhs, 0))
511 return true;
515 /* Otherwise keep walking. */
516 return false;
519 static void
520 mark_aliased_reaching_defs_necessary (gimple stmt, tree ref)
522 unsigned int chain;
523 ao_ref refd;
524 gcc_assert (!chain_ovfl);
525 ao_ref_init (&refd, ref);
526 chain = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
527 mark_aliased_reaching_defs_necessary_1,
528 gimple_bb (stmt), NULL);
529 if (chain > longest_chain)
530 longest_chain = chain;
531 total_chain += chain;
532 nr_walks++;
535 /* Worker for the walker that marks reaching definitions of REF, which
536 is not based on a non-aliased decl. For simplicity we need to end
537 up marking all may-defs necessary that are not based on a non-aliased
538 decl. The only job of this walker is to skip may-defs based on
539 a non-aliased decl. */
541 static bool
542 mark_all_reaching_defs_necessary_1 (ao_ref *ref ATTRIBUTE_UNUSED,
543 tree vdef, void *data ATTRIBUTE_UNUSED)
545 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
547 /* We have to skip already visited (and thus necessary) statements
548 to make the chaining work after we dropped back to simple mode. */
549 if (chain_ovfl
550 && bitmap_bit_p (processed, SSA_NAME_VERSION (vdef)))
552 gcc_assert (gimple_nop_p (def_stmt)
553 || gimple_plf (def_stmt, STMT_NECESSARY));
554 return false;
557 /* We want to skip stores to non-aliased variables. */
558 if (!chain_ovfl
559 && gimple_assign_single_p (def_stmt))
561 tree lhs = gimple_assign_lhs (def_stmt);
562 if (!ref_may_be_aliased (lhs))
563 return false;
566 /* We want to skip statments that do not constitute stores but have
567 a virtual definition. */
568 if (is_gimple_call (def_stmt))
570 tree callee = gimple_call_fndecl (def_stmt);
571 if (callee != NULL_TREE
572 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
573 switch (DECL_FUNCTION_CODE (callee))
575 case BUILT_IN_MALLOC:
576 case BUILT_IN_CALLOC:
577 case BUILT_IN_ALLOCA:
578 case BUILT_IN_ALLOCA_WITH_ALIGN:
579 case BUILT_IN_FREE:
580 return false;
582 default:;
586 mark_operand_necessary (vdef);
588 return false;
591 static void
592 mark_all_reaching_defs_necessary (gimple stmt)
594 walk_aliased_vdefs (NULL, gimple_vuse (stmt),
595 mark_all_reaching_defs_necessary_1, NULL, &visited);
598 /* Return true for PHI nodes with one or identical arguments
599 can be removed. */
600 static bool
601 degenerate_phi_p (gimple phi)
603 unsigned int i;
604 tree op = gimple_phi_arg_def (phi, 0);
605 for (i = 1; i < gimple_phi_num_args (phi); i++)
606 if (gimple_phi_arg_def (phi, i) != op)
607 return false;
608 return true;
611 /* Propagate necessity using the operands of necessary statements.
612 Process the uses on each statement in the worklist, and add all
613 feeding statements which contribute to the calculation of this
614 value to the worklist.
616 In conservative mode, EL is NULL. */
618 static void
619 propagate_necessity (bool aggressive)
621 gimple stmt;
623 if (dump_file && (dump_flags & TDF_DETAILS))
624 fprintf (dump_file, "\nProcessing worklist:\n");
626 while (worklist.length () > 0)
628 /* Take STMT from worklist. */
629 stmt = worklist.pop ();
631 if (dump_file && (dump_flags & TDF_DETAILS))
633 fprintf (dump_file, "processing: ");
634 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
635 fprintf (dump_file, "\n");
638 if (aggressive)
640 /* Mark the last statement of the basic blocks on which the block
641 containing STMT is control dependent, but only if we haven't
642 already done so. */
643 basic_block bb = gimple_bb (stmt);
644 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
645 && !bitmap_bit_p (visited_control_parents, bb->index))
646 mark_control_dependent_edges_necessary (bb, false);
649 if (gimple_code (stmt) == GIMPLE_PHI
650 /* We do not process virtual PHI nodes nor do we track their
651 necessity. */
652 && !virtual_operand_p (gimple_phi_result (stmt)))
654 /* PHI nodes are somewhat special in that each PHI alternative has
655 data and control dependencies. All the statements feeding the
656 PHI node's arguments are always necessary. In aggressive mode,
657 we also consider the control dependent edges leading to the
658 predecessor block associated with each PHI alternative as
659 necessary. */
660 size_t k;
662 for (k = 0; k < gimple_phi_num_args (stmt); k++)
664 tree arg = PHI_ARG_DEF (stmt, k);
665 if (TREE_CODE (arg) == SSA_NAME)
666 mark_operand_necessary (arg);
669 /* For PHI operands it matters from where the control flow arrives
670 to the BB. Consider the following example:
672 a=exp1;
673 b=exp2;
674 if (test)
676 else
678 c=PHI(a,b)
680 We need to mark control dependence of the empty basic blocks, since they
681 contains computation of PHI operands.
683 Doing so is too restrictive in the case the predecestor block is in
684 the loop. Consider:
686 if (b)
688 int i;
689 for (i = 0; i<1000; ++i)
691 j = 0;
693 return j;
695 There is PHI for J in the BB containing return statement.
696 In this case the control dependence of predecestor block (that is
697 within the empty loop) also contains the block determining number
698 of iterations of the block that would prevent removing of empty
699 loop in this case.
701 This scenario can be avoided by splitting critical edges.
702 To save the critical edge splitting pass we identify how the control
703 dependence would look like if the edge was split.
705 Consider the modified CFG created from current CFG by splitting
706 edge B->C. In the postdominance tree of modified CFG, C' is
707 always child of C. There are two cases how chlids of C' can look
708 like:
710 1) C' is leaf
712 In this case the only basic block C' is control dependent on is B.
714 2) C' has single child that is B
716 In this case control dependence of C' is same as control
717 dependence of B in original CFG except for block B itself.
718 (since C' postdominate B in modified CFG)
720 Now how to decide what case happens? There are two basic options:
722 a) C postdominate B. Then C immediately postdominate B and
723 case 2 happens iff there is no other way from B to C except
724 the edge B->C.
726 There is other way from B to C iff there is succesor of B that
727 is not postdominated by B. Testing this condition is somewhat
728 expensive, because we need to iterate all succesors of B.
729 We are safe to assume that this does not happen: we will mark B
730 as needed when processing the other path from B to C that is
731 conrol dependent on B and marking control dependencies of B
732 itself is harmless because they will be processed anyway after
733 processing control statement in B.
735 b) C does not postdominate B. Always case 1 happens since there is
736 path from C to exit that does not go through B and thus also C'. */
738 if (aggressive && !degenerate_phi_p (stmt))
740 for (k = 0; k < gimple_phi_num_args (stmt); k++)
742 basic_block arg_bb = gimple_phi_arg_edge (stmt, k)->src;
744 if (gimple_bb (stmt)
745 != get_immediate_dominator (CDI_POST_DOMINATORS, arg_bb))
747 if (!bitmap_bit_p (last_stmt_necessary, arg_bb->index))
748 mark_last_stmt_necessary (arg_bb);
750 else if (arg_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
751 && !bitmap_bit_p (visited_control_parents,
752 arg_bb->index))
753 mark_control_dependent_edges_necessary (arg_bb, true);
757 else
759 /* Propagate through the operands. Examine all the USE, VUSE and
760 VDEF operands in this statement. Mark all the statements
761 which feed this statement's uses as necessary. */
762 ssa_op_iter iter;
763 tree use;
765 /* If this is a call to free which is directly fed by an
766 allocation function do not mark that necessary through
767 processing the argument. */
768 if (gimple_call_builtin_p (stmt, BUILT_IN_FREE))
770 tree ptr = gimple_call_arg (stmt, 0);
771 gimple def_stmt;
772 tree def_callee;
773 /* If the pointer we free is defined by an allocation
774 function do not add the call to the worklist. */
775 if (TREE_CODE (ptr) == SSA_NAME
776 && is_gimple_call (def_stmt = SSA_NAME_DEF_STMT (ptr))
777 && (def_callee = gimple_call_fndecl (def_stmt))
778 && DECL_BUILT_IN_CLASS (def_callee) == BUILT_IN_NORMAL
779 && (DECL_FUNCTION_CODE (def_callee) == BUILT_IN_MALLOC
780 || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_CALLOC))
781 continue;
784 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
785 mark_operand_necessary (use);
787 use = gimple_vuse (stmt);
788 if (!use)
789 continue;
791 /* If we dropped to simple mode make all immediately
792 reachable definitions necessary. */
793 if (chain_ovfl)
795 mark_all_reaching_defs_necessary (stmt);
796 continue;
799 /* For statements that may load from memory (have a VUSE) we
800 have to mark all reaching (may-)definitions as necessary.
801 We partition this task into two cases:
802 1) explicit loads based on decls that are not aliased
803 2) implicit loads (like calls) and explicit loads not
804 based on decls that are not aliased (like indirect
805 references or loads from globals)
806 For 1) we mark all reaching may-defs as necessary, stopping
807 at dominating kills. For 2) we want to mark all dominating
808 references necessary, but non-aliased ones which we handle
809 in 1). By keeping a global visited bitmap for references
810 we walk for 2) we avoid quadratic behavior for those. */
812 if (is_gimple_call (stmt))
814 tree callee = gimple_call_fndecl (stmt);
815 unsigned i;
817 /* Calls to functions that are merely acting as barriers
818 or that only store to memory do not make any previous
819 stores necessary. */
820 if (callee != NULL_TREE
821 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
822 && (DECL_FUNCTION_CODE (callee) == BUILT_IN_MEMSET
823 || DECL_FUNCTION_CODE (callee) == BUILT_IN_MEMSET_CHK
824 || DECL_FUNCTION_CODE (callee) == BUILT_IN_MALLOC
825 || DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC
826 || DECL_FUNCTION_CODE (callee) == BUILT_IN_FREE
827 || DECL_FUNCTION_CODE (callee) == BUILT_IN_VA_END
828 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
829 || (DECL_FUNCTION_CODE (callee)
830 == BUILT_IN_ALLOCA_WITH_ALIGN)
831 || DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE
832 || DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE
833 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ASSUME_ALIGNED))
834 continue;
836 /* Calls implicitly load from memory, their arguments
837 in addition may explicitly perform memory loads. */
838 mark_all_reaching_defs_necessary (stmt);
839 for (i = 0; i < gimple_call_num_args (stmt); ++i)
841 tree arg = gimple_call_arg (stmt, i);
842 if (TREE_CODE (arg) == SSA_NAME
843 || is_gimple_min_invariant (arg))
844 continue;
845 if (TREE_CODE (arg) == WITH_SIZE_EXPR)
846 arg = TREE_OPERAND (arg, 0);
847 if (!ref_may_be_aliased (arg))
848 mark_aliased_reaching_defs_necessary (stmt, arg);
851 else if (gimple_assign_single_p (stmt))
853 tree rhs;
854 /* If this is a load mark things necessary. */
855 rhs = gimple_assign_rhs1 (stmt);
856 if (TREE_CODE (rhs) != SSA_NAME
857 && !is_gimple_min_invariant (rhs)
858 && TREE_CODE (rhs) != CONSTRUCTOR)
860 if (!ref_may_be_aliased (rhs))
861 mark_aliased_reaching_defs_necessary (stmt, rhs);
862 else
863 mark_all_reaching_defs_necessary (stmt);
866 else if (gimple_code (stmt) == GIMPLE_RETURN)
868 tree rhs = gimple_return_retval (stmt);
869 /* A return statement may perform a load. */
870 if (rhs
871 && TREE_CODE (rhs) != SSA_NAME
872 && !is_gimple_min_invariant (rhs)
873 && TREE_CODE (rhs) != CONSTRUCTOR)
875 if (!ref_may_be_aliased (rhs))
876 mark_aliased_reaching_defs_necessary (stmt, rhs);
877 else
878 mark_all_reaching_defs_necessary (stmt);
881 else if (gimple_code (stmt) == GIMPLE_ASM)
883 unsigned i;
884 mark_all_reaching_defs_necessary (stmt);
885 /* Inputs may perform loads. */
886 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
888 tree op = TREE_VALUE (gimple_asm_input_op (stmt, i));
889 if (TREE_CODE (op) != SSA_NAME
890 && !is_gimple_min_invariant (op)
891 && TREE_CODE (op) != CONSTRUCTOR
892 && !ref_may_be_aliased (op))
893 mark_aliased_reaching_defs_necessary (stmt, op);
896 else if (gimple_code (stmt) == GIMPLE_TRANSACTION)
898 /* The beginning of a transaction is a memory barrier. */
899 /* ??? If we were really cool, we'd only be a barrier
900 for the memories touched within the transaction. */
901 mark_all_reaching_defs_necessary (stmt);
903 else
904 gcc_unreachable ();
906 /* If we over-used our alias oracle budget drop to simple
907 mode. The cost metric allows quadratic behavior
908 (number of uses times number of may-defs queries) up to
909 a constant maximal number of queries and after that falls back to
910 super-linear complexity. */
911 if (/* Constant but quadratic for small functions. */
912 total_chain > 128 * 128
913 /* Linear in the number of may-defs. */
914 && total_chain > 32 * longest_chain
915 /* Linear in the number of uses. */
916 && total_chain > nr_walks * 32)
918 chain_ovfl = true;
919 if (visited)
920 bitmap_clear (visited);
926 /* Remove dead PHI nodes from block BB. */
928 static bool
929 remove_dead_phis (basic_block bb)
931 bool something_changed = false;
932 gimple phi;
933 gimple_stmt_iterator gsi;
935 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);)
937 stats.total_phis++;
938 phi = gsi_stmt (gsi);
940 /* We do not track necessity of virtual PHI nodes. Instead do
941 very simple dead PHI removal here. */
942 if (virtual_operand_p (gimple_phi_result (phi)))
944 /* Virtual PHI nodes with one or identical arguments
945 can be removed. */
946 if (degenerate_phi_p (phi))
948 tree vdef = gimple_phi_result (phi);
949 tree vuse = gimple_phi_arg_def (phi, 0);
951 use_operand_p use_p;
952 imm_use_iterator iter;
953 gimple use_stmt;
954 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
955 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
956 SET_USE (use_p, vuse);
957 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef)
958 && TREE_CODE (vuse) == SSA_NAME)
959 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
961 else
962 gimple_set_plf (phi, STMT_NECESSARY, true);
965 if (!gimple_plf (phi, STMT_NECESSARY))
967 something_changed = true;
968 if (dump_file && (dump_flags & TDF_DETAILS))
970 fprintf (dump_file, "Deleting : ");
971 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
972 fprintf (dump_file, "\n");
975 remove_phi_node (&gsi, true);
976 stats.removed_phis++;
977 continue;
980 gsi_next (&gsi);
982 return something_changed;
985 /* Forward edge E to respective POST_DOM_BB and update PHIs. */
987 static edge
988 forward_edge_to_pdom (edge e, basic_block post_dom_bb)
990 gimple_stmt_iterator gsi;
991 edge e2 = NULL;
992 edge_iterator ei;
994 if (dump_file && (dump_flags & TDF_DETAILS))
995 fprintf (dump_file, "Redirecting edge %i->%i to %i\n", e->src->index,
996 e->dest->index, post_dom_bb->index);
998 e2 = redirect_edge_and_branch (e, post_dom_bb);
999 cfg_altered = true;
1001 /* If edge was already around, no updating is necessary. */
1002 if (e2 != e)
1003 return e2;
1005 if (!gimple_seq_empty_p (phi_nodes (post_dom_bb)))
1007 /* We are sure that for every live PHI we are seeing control dependent BB.
1008 This means that we can pick any edge to duplicate PHI args from. */
1009 FOR_EACH_EDGE (e2, ei, post_dom_bb->preds)
1010 if (e2 != e)
1011 break;
1012 for (gsi = gsi_start_phis (post_dom_bb); !gsi_end_p (gsi);)
1014 gimple phi = gsi_stmt (gsi);
1015 tree op;
1016 source_location locus;
1018 /* PHIs for virtuals have no control dependency relation on them.
1019 We are lost here and must force renaming of the symbol. */
1020 if (virtual_operand_p (gimple_phi_result (phi)))
1022 mark_virtual_phi_result_for_renaming (phi);
1023 remove_phi_node (&gsi, true);
1024 continue;
1027 /* Dead PHI do not imply control dependency. */
1028 if (!gimple_plf (phi, STMT_NECESSARY))
1030 gsi_next (&gsi);
1031 continue;
1034 op = gimple_phi_arg_def (phi, e2->dest_idx);
1035 locus = gimple_phi_arg_location (phi, e2->dest_idx);
1036 add_phi_arg (phi, op, e, locus);
1037 /* The resulting PHI if not dead can only be degenerate. */
1038 gcc_assert (degenerate_phi_p (phi));
1039 gsi_next (&gsi);
1042 return e;
1045 /* Remove dead statement pointed to by iterator I. Receives the basic block BB
1046 containing I so that we don't have to look it up. */
1048 static void
1049 remove_dead_stmt (gimple_stmt_iterator *i, basic_block bb)
1051 gimple stmt = gsi_stmt (*i);
1053 if (dump_file && (dump_flags & TDF_DETAILS))
1055 fprintf (dump_file, "Deleting : ");
1056 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1057 fprintf (dump_file, "\n");
1060 stats.removed++;
1062 /* If we have determined that a conditional branch statement contributes
1063 nothing to the program, then we not only remove it, but we also change
1064 the flow graph so that the current block will simply fall-thru to its
1065 immediate post-dominator. The blocks we are circumventing will be
1066 removed by cleanup_tree_cfg if this change in the flow graph makes them
1067 unreachable. */
1068 if (is_ctrl_stmt (stmt))
1070 basic_block post_dom_bb;
1071 edge e, e2;
1072 edge_iterator ei;
1074 post_dom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
1076 e = find_edge (bb, post_dom_bb);
1078 /* If edge is already there, try to use it. This avoids need to update
1079 PHI nodes. Also watch for cases where post dominator does not exists
1080 or is exit block. These can happen for infinite loops as we create
1081 fake edges in the dominator tree. */
1082 if (e)
1084 else if (! post_dom_bb || post_dom_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1085 e = EDGE_SUCC (bb, 0);
1086 else
1087 e = forward_edge_to_pdom (EDGE_SUCC (bb, 0), post_dom_bb);
1088 gcc_assert (e);
1089 e->probability = REG_BR_PROB_BASE;
1090 e->count = bb->count;
1092 /* The edge is no longer associated with a conditional, so it does
1093 not have TRUE/FALSE flags. */
1094 e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
1096 /* The lone outgoing edge from BB will be a fallthru edge. */
1097 e->flags |= EDGE_FALLTHRU;
1099 /* Remove the remaining outgoing edges. */
1100 for (ei = ei_start (bb->succs); (e2 = ei_safe_edge (ei)); )
1101 if (e != e2)
1103 cfg_altered = true;
1104 remove_edge (e2);
1106 else
1107 ei_next (&ei);
1110 /* If this is a store into a variable that is being optimized away,
1111 add a debug bind stmt if possible. */
1112 if (MAY_HAVE_DEBUG_STMTS
1113 && gimple_assign_single_p (stmt)
1114 && is_gimple_val (gimple_assign_rhs1 (stmt)))
1116 tree lhs = gimple_assign_lhs (stmt);
1117 if ((TREE_CODE (lhs) == VAR_DECL || TREE_CODE (lhs) == PARM_DECL)
1118 && !DECL_IGNORED_P (lhs)
1119 && is_gimple_reg_type (TREE_TYPE (lhs))
1120 && !is_global_var (lhs)
1121 && !DECL_HAS_VALUE_EXPR_P (lhs))
1123 tree rhs = gimple_assign_rhs1 (stmt);
1124 gimple note
1125 = gimple_build_debug_bind (lhs, unshare_expr (rhs), stmt);
1126 gsi_insert_after (i, note, GSI_SAME_STMT);
1130 unlink_stmt_vdef (stmt);
1131 gsi_remove (i, true);
1132 release_defs (stmt);
1135 /* Eliminate unnecessary statements. Any instruction not marked as necessary
1136 contributes nothing to the program, and can be deleted. */
1138 static bool
1139 eliminate_unnecessary_stmts (void)
1141 bool something_changed = false;
1142 basic_block bb;
1143 gimple_stmt_iterator gsi, psi;
1144 gimple stmt;
1145 tree call;
1146 vec<basic_block> h;
1148 if (dump_file && (dump_flags & TDF_DETAILS))
1149 fprintf (dump_file, "\nEliminating unnecessary statements:\n");
1151 clear_special_calls ();
1153 /* Walking basic blocks and statements in reverse order avoids
1154 releasing SSA names before any other DEFs that refer to them are
1155 released. This helps avoid loss of debug information, as we get
1156 a chance to propagate all RHSs of removed SSAs into debug uses,
1157 rather than only the latest ones. E.g., consider:
1159 x_3 = y_1 + z_2;
1160 a_5 = x_3 - b_4;
1161 # DEBUG a => a_5
1163 If we were to release x_3 before a_5, when we reached a_5 and
1164 tried to substitute it into the debug stmt, we'd see x_3 there,
1165 but x_3's DEF, type, etc would have already been disconnected.
1166 By going backwards, the debug stmt first changes to:
1168 # DEBUG a => x_3 - b_4
1170 and then to:
1172 # DEBUG a => y_1 + z_2 - b_4
1174 as desired. */
1175 gcc_assert (dom_info_available_p (CDI_DOMINATORS));
1176 h = get_all_dominated_blocks (CDI_DOMINATORS,
1177 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1179 while (h.length ())
1181 bb = h.pop ();
1183 /* Remove dead statements. */
1184 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi = psi)
1186 stmt = gsi_stmt (gsi);
1188 psi = gsi;
1189 gsi_prev (&psi);
1191 stats.total++;
1193 /* We can mark a call to free as not necessary if the
1194 defining statement of its argument is not necessary
1195 (and thus is getting removed). */
1196 if (gimple_plf (stmt, STMT_NECESSARY)
1197 && gimple_call_builtin_p (stmt, BUILT_IN_FREE))
1199 tree ptr = gimple_call_arg (stmt, 0);
1200 if (TREE_CODE (ptr) == SSA_NAME)
1202 gimple def_stmt = SSA_NAME_DEF_STMT (ptr);
1203 if (!gimple_nop_p (def_stmt)
1204 && !gimple_plf (def_stmt, STMT_NECESSARY))
1205 gimple_set_plf (stmt, STMT_NECESSARY, false);
1209 /* If GSI is not necessary then remove it. */
1210 if (!gimple_plf (stmt, STMT_NECESSARY))
1212 if (!is_gimple_debug (stmt))
1213 something_changed = true;
1214 remove_dead_stmt (&gsi, bb);
1216 else if (is_gimple_call (stmt))
1218 tree name = gimple_call_lhs (stmt);
1220 notice_special_calls (stmt);
1222 /* When LHS of var = call (); is dead, simplify it into
1223 call (); saving one operand. */
1224 if (name
1225 && TREE_CODE (name) == SSA_NAME
1226 && !bitmap_bit_p (processed, SSA_NAME_VERSION (name))
1227 /* Avoid doing so for allocation calls which we
1228 did not mark as necessary, it will confuse the
1229 special logic we apply to malloc/free pair removal. */
1230 && (!(call = gimple_call_fndecl (stmt))
1231 || DECL_BUILT_IN_CLASS (call) != BUILT_IN_NORMAL
1232 || (DECL_FUNCTION_CODE (call) != BUILT_IN_MALLOC
1233 && DECL_FUNCTION_CODE (call) != BUILT_IN_CALLOC
1234 && DECL_FUNCTION_CODE (call) != BUILT_IN_ALLOCA
1235 && (DECL_FUNCTION_CODE (call)
1236 != BUILT_IN_ALLOCA_WITH_ALIGN))))
1238 something_changed = true;
1239 if (dump_file && (dump_flags & TDF_DETAILS))
1241 fprintf (dump_file, "Deleting LHS of call: ");
1242 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1243 fprintf (dump_file, "\n");
1246 gimple_call_set_lhs (stmt, NULL_TREE);
1247 maybe_clean_or_replace_eh_stmt (stmt, stmt);
1248 update_stmt (stmt);
1249 release_ssa_name (name);
1255 h.release ();
1257 /* Since we don't track liveness of virtual PHI nodes, it is possible that we
1258 rendered some PHI nodes unreachable while they are still in use.
1259 Mark them for renaming. */
1260 if (cfg_altered)
1262 basic_block prev_bb;
1264 find_unreachable_blocks ();
1266 /* Delete all unreachable basic blocks in reverse dominator order. */
1267 for (bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
1268 bb != ENTRY_BLOCK_PTR_FOR_FN (cfun); bb = prev_bb)
1270 prev_bb = bb->prev_bb;
1272 if (!bitmap_bit_p (bb_contains_live_stmts, bb->index)
1273 || !(bb->flags & BB_REACHABLE))
1275 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1276 if (virtual_operand_p (gimple_phi_result (gsi_stmt (gsi))))
1278 bool found = false;
1279 imm_use_iterator iter;
1281 FOR_EACH_IMM_USE_STMT (stmt, iter, gimple_phi_result (gsi_stmt (gsi)))
1283 if (!(gimple_bb (stmt)->flags & BB_REACHABLE))
1284 continue;
1285 if (gimple_code (stmt) == GIMPLE_PHI
1286 || gimple_plf (stmt, STMT_NECESSARY))
1288 found = true;
1289 BREAK_FROM_IMM_USE_STMT (iter);
1292 if (found)
1293 mark_virtual_phi_result_for_renaming (gsi_stmt (gsi));
1296 if (!(bb->flags & BB_REACHABLE))
1298 /* Speed up the removal of blocks that don't
1299 dominate others. Walking backwards, this should
1300 be the common case. ??? Do we need to recompute
1301 dominators because of cfg_altered? */
1302 if (!MAY_HAVE_DEBUG_STMTS
1303 || !first_dom_son (CDI_DOMINATORS, bb))
1304 delete_basic_block (bb);
1305 else
1307 h = get_all_dominated_blocks (CDI_DOMINATORS, bb);
1309 while (h.length ())
1311 bb = h.pop ();
1312 prev_bb = bb->prev_bb;
1313 /* Rearrangements to the CFG may have failed
1314 to update the dominators tree, so that
1315 formerly-dominated blocks are now
1316 otherwise reachable. */
1317 if (!!(bb->flags & BB_REACHABLE))
1318 continue;
1319 delete_basic_block (bb);
1322 h.release ();
1328 FOR_EACH_BB_FN (bb, cfun)
1330 /* Remove dead PHI nodes. */
1331 something_changed |= remove_dead_phis (bb);
1334 return something_changed;
1338 /* Print out removed statement statistics. */
1340 static void
1341 print_stats (void)
1343 float percg;
1345 percg = ((float) stats.removed / (float) stats.total) * 100;
1346 fprintf (dump_file, "Removed %d of %d statements (%d%%)\n",
1347 stats.removed, stats.total, (int) percg);
1349 if (stats.total_phis == 0)
1350 percg = 0;
1351 else
1352 percg = ((float) stats.removed_phis / (float) stats.total_phis) * 100;
1354 fprintf (dump_file, "Removed %d of %d PHI nodes (%d%%)\n",
1355 stats.removed_phis, stats.total_phis, (int) percg);
1358 /* Initialization for this pass. Set up the used data structures. */
1360 static void
1361 tree_dce_init (bool aggressive)
1363 memset ((void *) &stats, 0, sizeof (stats));
1365 if (aggressive)
1367 last_stmt_necessary = sbitmap_alloc (last_basic_block_for_fn (cfun));
1368 bitmap_clear (last_stmt_necessary);
1369 bb_contains_live_stmts = sbitmap_alloc (last_basic_block_for_fn (cfun));
1370 bitmap_clear (bb_contains_live_stmts);
1373 processed = sbitmap_alloc (num_ssa_names + 1);
1374 bitmap_clear (processed);
1376 worklist.create (64);
1377 cfg_altered = false;
1380 /* Cleanup after this pass. */
1382 static void
1383 tree_dce_done (bool aggressive)
1385 if (aggressive)
1387 delete cd;
1388 sbitmap_free (visited_control_parents);
1389 sbitmap_free (last_stmt_necessary);
1390 sbitmap_free (bb_contains_live_stmts);
1391 bb_contains_live_stmts = NULL;
1394 sbitmap_free (processed);
1396 worklist.release ();
1399 /* Main routine to eliminate dead code.
1401 AGGRESSIVE controls the aggressiveness of the algorithm.
1402 In conservative mode, we ignore control dependence and simply declare
1403 all but the most trivially dead branches necessary. This mode is fast.
1404 In aggressive mode, control dependences are taken into account, which
1405 results in more dead code elimination, but at the cost of some time.
1407 FIXME: Aggressive mode before PRE doesn't work currently because
1408 the dominance info is not invalidated after DCE1. This is
1409 not an issue right now because we only run aggressive DCE
1410 as the last tree SSA pass, but keep this in mind when you
1411 start experimenting with pass ordering. */
1413 static unsigned int
1414 perform_tree_ssa_dce (bool aggressive)
1416 bool something_changed = 0;
1418 calculate_dominance_info (CDI_DOMINATORS);
1420 /* Preheaders are needed for SCEV to work.
1421 Simple lateches and recorded exits improve chances that loop will
1422 proved to be finite in testcases such as in loop-15.c and loop-24.c */
1423 if (aggressive)
1424 loop_optimizer_init (LOOPS_NORMAL
1425 | LOOPS_HAVE_RECORDED_EXITS);
1427 tree_dce_init (aggressive);
1429 if (aggressive)
1431 /* Compute control dependence. */
1432 calculate_dominance_info (CDI_POST_DOMINATORS);
1433 cd = new control_dependences (create_edge_list ());
1435 visited_control_parents =
1436 sbitmap_alloc (last_basic_block_for_fn (cfun));
1437 bitmap_clear (visited_control_parents);
1439 mark_dfs_back_edges ();
1442 find_obviously_necessary_stmts (aggressive);
1444 if (aggressive)
1445 loop_optimizer_finalize ();
1447 longest_chain = 0;
1448 total_chain = 0;
1449 nr_walks = 0;
1450 chain_ovfl = false;
1451 visited = BITMAP_ALLOC (NULL);
1452 propagate_necessity (aggressive);
1453 BITMAP_FREE (visited);
1455 something_changed |= eliminate_unnecessary_stmts ();
1456 something_changed |= cfg_altered;
1458 /* We do not update postdominators, so free them unconditionally. */
1459 free_dominance_info (CDI_POST_DOMINATORS);
1461 /* If we removed paths in the CFG, then we need to update
1462 dominators as well. I haven't investigated the possibility
1463 of incrementally updating dominators. */
1464 if (cfg_altered)
1465 free_dominance_info (CDI_DOMINATORS);
1467 statistics_counter_event (cfun, "Statements deleted", stats.removed);
1468 statistics_counter_event (cfun, "PHI nodes deleted", stats.removed_phis);
1470 /* Debugging dumps. */
1471 if (dump_file && (dump_flags & (TDF_STATS|TDF_DETAILS)))
1472 print_stats ();
1474 tree_dce_done (aggressive);
1476 if (something_changed)
1477 return TODO_update_ssa | TODO_cleanup_cfg;
1478 return 0;
1481 /* Pass entry points. */
1482 static unsigned int
1483 tree_ssa_dce (void)
1485 return perform_tree_ssa_dce (/*aggressive=*/false);
1488 static unsigned int
1489 tree_ssa_dce_loop (void)
1491 unsigned int todo;
1492 todo = perform_tree_ssa_dce (/*aggressive=*/false);
1493 if (todo)
1495 free_numbers_of_iterations_estimates ();
1496 scev_reset ();
1498 return todo;
1501 static unsigned int
1502 tree_ssa_cd_dce (void)
1504 return perform_tree_ssa_dce (/*aggressive=*/optimize >= 2);
1507 static bool
1508 gate_dce (void)
1510 return flag_tree_dce != 0;
1513 namespace {
1515 const pass_data pass_data_dce =
1517 GIMPLE_PASS, /* type */
1518 "dce", /* name */
1519 OPTGROUP_NONE, /* optinfo_flags */
1520 true, /* has_gate */
1521 true, /* has_execute */
1522 TV_TREE_DCE, /* tv_id */
1523 ( PROP_cfg | PROP_ssa ), /* properties_required */
1524 0, /* properties_provided */
1525 0, /* properties_destroyed */
1526 0, /* todo_flags_start */
1527 TODO_verify_ssa, /* todo_flags_finish */
1530 class pass_dce : public gimple_opt_pass
1532 public:
1533 pass_dce (gcc::context *ctxt)
1534 : gimple_opt_pass (pass_data_dce, ctxt)
1537 /* opt_pass methods: */
1538 opt_pass * clone () { return new pass_dce (m_ctxt); }
1539 bool gate () { return gate_dce (); }
1540 unsigned int execute () { return tree_ssa_dce (); }
1542 }; // class pass_dce
1544 } // anon namespace
1546 gimple_opt_pass *
1547 make_pass_dce (gcc::context *ctxt)
1549 return new pass_dce (ctxt);
1552 namespace {
1554 const pass_data pass_data_dce_loop =
1556 GIMPLE_PASS, /* type */
1557 "dceloop", /* name */
1558 OPTGROUP_NONE, /* optinfo_flags */
1559 true, /* has_gate */
1560 true, /* has_execute */
1561 TV_TREE_DCE, /* tv_id */
1562 ( PROP_cfg | PROP_ssa ), /* properties_required */
1563 0, /* properties_provided */
1564 0, /* properties_destroyed */
1565 0, /* todo_flags_start */
1566 TODO_verify_ssa, /* todo_flags_finish */
1569 class pass_dce_loop : public gimple_opt_pass
1571 public:
1572 pass_dce_loop (gcc::context *ctxt)
1573 : gimple_opt_pass (pass_data_dce_loop, ctxt)
1576 /* opt_pass methods: */
1577 opt_pass * clone () { return new pass_dce_loop (m_ctxt); }
1578 bool gate () { return gate_dce (); }
1579 unsigned int execute () { return tree_ssa_dce_loop (); }
1581 }; // class pass_dce_loop
1583 } // anon namespace
1585 gimple_opt_pass *
1586 make_pass_dce_loop (gcc::context *ctxt)
1588 return new pass_dce_loop (ctxt);
1591 namespace {
1593 const pass_data pass_data_cd_dce =
1595 GIMPLE_PASS, /* type */
1596 "cddce", /* name */
1597 OPTGROUP_NONE, /* optinfo_flags */
1598 true, /* has_gate */
1599 true, /* has_execute */
1600 TV_TREE_CD_DCE, /* tv_id */
1601 ( PROP_cfg | PROP_ssa ), /* properties_required */
1602 0, /* properties_provided */
1603 0, /* properties_destroyed */
1604 0, /* todo_flags_start */
1605 ( TODO_verify_ssa | TODO_verify_flow ), /* todo_flags_finish */
1608 class pass_cd_dce : public gimple_opt_pass
1610 public:
1611 pass_cd_dce (gcc::context *ctxt)
1612 : gimple_opt_pass (pass_data_cd_dce, ctxt)
1615 /* opt_pass methods: */
1616 opt_pass * clone () { return new pass_cd_dce (m_ctxt); }
1617 bool gate () { return gate_dce (); }
1618 unsigned int execute () { return tree_ssa_cd_dce (); }
1620 }; // class pass_cd_dce
1622 } // anon namespace
1624 gimple_opt_pass *
1625 make_pass_cd_dce (gcc::context *ctxt)
1627 return new pass_cd_dce (ctxt);