[Ada] Missing range check on assignment to bit-packed array
[official-gcc.git] / gcc / tree-ssa-dce.c
blobc73fbabfe29107703f8cb1fcd8b49f242db9c474
1 /* Dead code elimination pass for the GNU compiler.
2 Copyright (C) 2002-2019 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 "backend.h"
49 #include "rtl.h"
50 #include "tree.h"
51 #include "gimple.h"
52 #include "cfghooks.h"
53 #include "tree-pass.h"
54 #include "ssa.h"
55 #include "gimple-pretty-print.h"
56 #include "fold-const.h"
57 #include "calls.h"
58 #include "cfganal.h"
59 #include "tree-eh.h"
60 #include "gimplify.h"
61 #include "gimple-iterator.h"
62 #include "tree-cfg.h"
63 #include "tree-ssa-loop-niter.h"
64 #include "tree-into-ssa.h"
65 #include "tree-dfa.h"
66 #include "cfgloop.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-ssa-propagate.h"
69 #include "gimple-fold.h"
71 static struct stmt_stats
73 int total;
74 int total_phis;
75 int removed;
76 int removed_phis;
77 } stats;
79 #define STMT_NECESSARY GF_PLF_1
81 static vec<gimple *> worklist;
83 /* Vector indicating an SSA name has already been processed and marked
84 as necessary. */
85 static sbitmap processed;
87 /* Vector indicating that the last statement of a basic block has already
88 been marked as necessary. */
89 static sbitmap last_stmt_necessary;
91 /* Vector indicating that BB contains statements that are live. */
92 static sbitmap bb_contains_live_stmts;
94 /* Before we can determine whether a control branch is dead, we need to
95 compute which blocks are control dependent on which edges.
97 We expect each block to be control dependent on very few edges so we
98 use a bitmap for each block recording its edges. An array holds the
99 bitmap. The Ith bit in the bitmap is set if that block is dependent
100 on the Ith edge. */
101 static control_dependences *cd;
103 /* Vector indicating that a basic block has already had all the edges
104 processed that it is control dependent on. */
105 static sbitmap visited_control_parents;
107 /* TRUE if this pass alters the CFG (by removing control statements).
108 FALSE otherwise.
110 If this pass alters the CFG, then it will arrange for the dominators
111 to be recomputed. */
112 static bool cfg_altered;
114 /* When non-NULL holds map from basic block index into the postorder. */
115 static int *bb_postorder;
118 /* If STMT is not already marked necessary, mark it, and add it to the
119 worklist if ADD_TO_WORKLIST is true. */
121 static inline void
122 mark_stmt_necessary (gimple *stmt, bool add_to_worklist)
124 gcc_assert (stmt);
126 if (gimple_plf (stmt, STMT_NECESSARY))
127 return;
129 if (dump_file && (dump_flags & TDF_DETAILS))
131 fprintf (dump_file, "Marking useful stmt: ");
132 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
133 fprintf (dump_file, "\n");
136 gimple_set_plf (stmt, STMT_NECESSARY, true);
137 if (add_to_worklist)
138 worklist.safe_push (stmt);
139 if (add_to_worklist && bb_contains_live_stmts && !is_gimple_debug (stmt))
140 bitmap_set_bit (bb_contains_live_stmts, gimple_bb (stmt)->index);
144 /* Mark the statement defining operand OP as necessary. */
146 static inline void
147 mark_operand_necessary (tree op)
149 gimple *stmt;
150 int ver;
152 gcc_assert (op);
154 ver = SSA_NAME_VERSION (op);
155 if (bitmap_bit_p (processed, ver))
157 stmt = SSA_NAME_DEF_STMT (op);
158 gcc_assert (gimple_nop_p (stmt)
159 || gimple_plf (stmt, STMT_NECESSARY));
160 return;
162 bitmap_set_bit (processed, ver);
164 stmt = SSA_NAME_DEF_STMT (op);
165 gcc_assert (stmt);
167 if (gimple_plf (stmt, STMT_NECESSARY) || gimple_nop_p (stmt))
168 return;
170 if (dump_file && (dump_flags & TDF_DETAILS))
172 fprintf (dump_file, "marking necessary through ");
173 print_generic_expr (dump_file, op);
174 fprintf (dump_file, " stmt ");
175 print_gimple_stmt (dump_file, stmt, 0);
178 gimple_set_plf (stmt, STMT_NECESSARY, true);
179 if (bb_contains_live_stmts)
180 bitmap_set_bit (bb_contains_live_stmts, gimple_bb (stmt)->index);
181 worklist.safe_push (stmt);
185 /* Mark STMT as necessary if it obviously is. Add it to the worklist if
186 it can make other statements necessary.
188 If AGGRESSIVE is false, control statements are conservatively marked as
189 necessary. */
191 static void
192 mark_stmt_if_obviously_necessary (gimple *stmt, bool aggressive)
194 /* With non-call exceptions, we have to assume that all statements could
195 throw. If a statement could throw, it can be deemed necessary. */
196 if (cfun->can_throw_non_call_exceptions
197 && !cfun->can_delete_dead_exceptions
198 && stmt_could_throw_p (cfun, stmt))
200 mark_stmt_necessary (stmt, true);
201 return;
204 /* Statements that are implicitly live. Most function calls, asm
205 and return statements are required. Labels and GIMPLE_BIND nodes
206 are kept because they are control flow, and we have no way of
207 knowing whether they can be removed. DCE can eliminate all the
208 other statements in a block, and CFG can then remove the block
209 and labels. */
210 switch (gimple_code (stmt))
212 case GIMPLE_PREDICT:
213 case GIMPLE_LABEL:
214 mark_stmt_necessary (stmt, false);
215 return;
217 case GIMPLE_ASM:
218 case GIMPLE_RESX:
219 case GIMPLE_RETURN:
220 mark_stmt_necessary (stmt, true);
221 return;
223 case GIMPLE_CALL:
225 tree callee = gimple_call_fndecl (stmt);
226 if (callee != NULL_TREE
227 && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
228 switch (DECL_FUNCTION_CODE (callee))
230 case BUILT_IN_MALLOC:
231 case BUILT_IN_ALIGNED_ALLOC:
232 case BUILT_IN_CALLOC:
233 CASE_BUILT_IN_ALLOCA:
234 case BUILT_IN_STRDUP:
235 case BUILT_IN_STRNDUP:
236 return;
238 default:;
240 /* Most, but not all function calls are required. Function calls that
241 produce no result and have no side effects (i.e. const pure
242 functions) are unnecessary. */
243 if (gimple_has_side_effects (stmt))
245 mark_stmt_necessary (stmt, true);
246 return;
248 /* IFN_GOACC_LOOP calls are necessary in that they are used to
249 represent parameter (i.e. step, bound) of a lowered OpenACC
250 partitioned loop. But this kind of partitioned loop might not
251 survive from aggressive loop removal for it has loop exit and
252 is assumed to be finite. Therefore, we need to explicitly mark
253 these calls. (An example is libgomp.oacc-c-c++-common/pr84955.c) */
254 if (gimple_call_internal_p (stmt, IFN_GOACC_LOOP))
256 mark_stmt_necessary (stmt, true);
257 return;
259 if (!gimple_call_lhs (stmt))
260 return;
261 break;
264 case GIMPLE_DEBUG:
265 /* Debug temps without a value are not useful. ??? If we could
266 easily locate the debug temp bind stmt for a use thereof,
267 would could refrain from marking all debug temps here, and
268 mark them only if they're used. */
269 if (gimple_debug_nonbind_marker_p (stmt)
270 || !gimple_debug_bind_p (stmt)
271 || gimple_debug_bind_has_value_p (stmt)
272 || TREE_CODE (gimple_debug_bind_get_var (stmt)) != DEBUG_EXPR_DECL)
273 mark_stmt_necessary (stmt, false);
274 return;
276 case GIMPLE_GOTO:
277 gcc_assert (!simple_goto_p (stmt));
278 mark_stmt_necessary (stmt, true);
279 return;
281 case GIMPLE_COND:
282 gcc_assert (EDGE_COUNT (gimple_bb (stmt)->succs) == 2);
283 /* Fall through. */
285 case GIMPLE_SWITCH:
286 if (! aggressive)
287 mark_stmt_necessary (stmt, true);
288 break;
290 case GIMPLE_ASSIGN:
291 if (gimple_clobber_p (stmt))
292 return;
293 break;
295 default:
296 break;
299 /* If the statement has volatile operands, it needs to be preserved.
300 Same for statements that can alter control flow in unpredictable
301 ways. */
302 if (gimple_has_volatile_ops (stmt) || is_ctrl_altering_stmt (stmt))
304 mark_stmt_necessary (stmt, true);
305 return;
308 if (stmt_may_clobber_global_p (stmt))
310 mark_stmt_necessary (stmt, true);
311 return;
314 return;
318 /* Mark the last statement of BB as necessary. */
320 static void
321 mark_last_stmt_necessary (basic_block bb)
323 gimple *stmt = last_stmt (bb);
325 bitmap_set_bit (last_stmt_necessary, bb->index);
326 bitmap_set_bit (bb_contains_live_stmts, bb->index);
328 /* We actually mark the statement only if it is a control statement. */
329 if (stmt && is_ctrl_stmt (stmt))
330 mark_stmt_necessary (stmt, true);
334 /* Mark control dependent edges of BB as necessary. We have to do this only
335 once for each basic block so we set the appropriate bit after we're done.
337 When IGNORE_SELF is true, ignore BB in the list of control dependences. */
339 static void
340 mark_control_dependent_edges_necessary (basic_block bb, bool ignore_self)
342 bitmap_iterator bi;
343 unsigned edge_number;
344 bool skipped = false;
346 gcc_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
348 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
349 return;
351 EXECUTE_IF_SET_IN_BITMAP (cd->get_edges_dependent_on (bb->index),
352 0, edge_number, bi)
354 basic_block cd_bb = cd->get_edge_src (edge_number);
356 if (ignore_self && cd_bb == bb)
358 skipped = true;
359 continue;
362 if (!bitmap_bit_p (last_stmt_necessary, cd_bb->index))
363 mark_last_stmt_necessary (cd_bb);
366 if (!skipped)
367 bitmap_set_bit (visited_control_parents, bb->index);
371 /* Find obviously necessary statements. These are things like most function
372 calls, and stores to file level variables.
374 If EL is NULL, control statements are conservatively marked as
375 necessary. Otherwise it contains the list of edges used by control
376 dependence analysis. */
378 static void
379 find_obviously_necessary_stmts (bool aggressive)
381 basic_block bb;
382 gimple_stmt_iterator gsi;
383 edge e;
384 gimple *phi, *stmt;
385 int flags;
387 FOR_EACH_BB_FN (bb, cfun)
389 /* PHI nodes are never inherently necessary. */
390 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
392 phi = gsi_stmt (gsi);
393 gimple_set_plf (phi, STMT_NECESSARY, false);
396 /* Check all statements in the block. */
397 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
399 stmt = gsi_stmt (gsi);
400 gimple_set_plf (stmt, STMT_NECESSARY, false);
401 mark_stmt_if_obviously_necessary (stmt, aggressive);
405 /* Pure and const functions are finite and thus have no infinite loops in
406 them. */
407 flags = flags_from_decl_or_type (current_function_decl);
408 if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
409 return;
411 /* Prevent the empty possibly infinite loops from being removed. */
412 if (aggressive)
414 struct loop *loop;
415 if (mark_irreducible_loops ())
416 FOR_EACH_BB_FN (bb, cfun)
418 edge_iterator ei;
419 FOR_EACH_EDGE (e, ei, bb->succs)
420 if ((e->flags & EDGE_DFS_BACK)
421 && (e->flags & EDGE_IRREDUCIBLE_LOOP))
423 if (dump_file)
424 fprintf (dump_file, "Marking back edge of irreducible loop %i->%i\n",
425 e->src->index, e->dest->index);
426 mark_control_dependent_edges_necessary (e->dest, false);
430 FOR_EACH_LOOP (loop, 0)
431 if (!finite_loop_p (loop))
433 if (dump_file)
434 fprintf (dump_file, "cannot prove finiteness of loop %i\n", loop->num);
435 mark_control_dependent_edges_necessary (loop->latch, false);
441 /* Return true if REF is based on an aliased base, otherwise false. */
443 static bool
444 ref_may_be_aliased (tree ref)
446 gcc_assert (TREE_CODE (ref) != WITH_SIZE_EXPR);
447 while (handled_component_p (ref))
448 ref = TREE_OPERAND (ref, 0);
449 if (TREE_CODE (ref) == MEM_REF
450 && TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
451 ref = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
452 return !(DECL_P (ref)
453 && !may_be_aliased (ref));
456 static bitmap visited = NULL;
457 static unsigned int longest_chain = 0;
458 static unsigned int total_chain = 0;
459 static unsigned int nr_walks = 0;
460 static bool chain_ovfl = false;
462 /* Worker for the walker that marks reaching definitions of REF,
463 which is based on a non-aliased decl, necessary. It returns
464 true whenever the defining statement of the current VDEF is
465 a kill for REF, as no dominating may-defs are necessary for REF
466 anymore. DATA points to the basic-block that contains the
467 stmt that refers to REF. */
469 static bool
470 mark_aliased_reaching_defs_necessary_1 (ao_ref *ref, tree vdef, void *data)
472 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
474 /* All stmts we visit are necessary. */
475 if (! gimple_clobber_p (def_stmt))
476 mark_operand_necessary (vdef);
478 /* If the stmt lhs kills ref, then we can stop walking. */
479 if (gimple_has_lhs (def_stmt)
480 && TREE_CODE (gimple_get_lhs (def_stmt)) != SSA_NAME
481 /* The assignment is not necessarily carried out if it can throw
482 and we can catch it in the current function where we could inspect
483 the previous value.
484 ??? We only need to care about the RHS throwing. For aggregate
485 assignments or similar calls and non-call exceptions the LHS
486 might throw as well. */
487 && !stmt_can_throw_internal (cfun, def_stmt))
489 tree base, lhs = gimple_get_lhs (def_stmt);
490 poly_int64 size, offset, max_size;
491 bool reverse;
492 ao_ref_base (ref);
493 base
494 = get_ref_base_and_extent (lhs, &offset, &size, &max_size, &reverse);
495 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
496 so base == refd->base does not always hold. */
497 if (base == ref->base)
499 /* For a must-alias check we need to be able to constrain
500 the accesses properly. */
501 if (known_eq (size, max_size)
502 && known_subrange_p (ref->offset, ref->max_size, offset, size))
503 return true;
504 /* Or they need to be exactly the same. */
505 else if (ref->ref
506 /* Make sure there is no induction variable involved
507 in the references (gcc.c-torture/execute/pr42142.c).
508 The simplest way is to check if the kill dominates
509 the use. */
510 /* But when both are in the same block we cannot
511 easily tell whether we came from a backedge
512 unless we decide to compute stmt UIDs
513 (see PR58246). */
514 && (basic_block) data != gimple_bb (def_stmt)
515 && dominated_by_p (CDI_DOMINATORS, (basic_block) data,
516 gimple_bb (def_stmt))
517 && operand_equal_p (ref->ref, lhs, 0))
518 return true;
522 /* Otherwise keep walking. */
523 return false;
526 static void
527 mark_aliased_reaching_defs_necessary (gimple *stmt, tree ref)
529 unsigned int chain;
530 ao_ref refd;
531 gcc_assert (!chain_ovfl);
532 ao_ref_init (&refd, ref);
533 chain = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
534 mark_aliased_reaching_defs_necessary_1,
535 gimple_bb (stmt), NULL);
536 if (chain > longest_chain)
537 longest_chain = chain;
538 total_chain += chain;
539 nr_walks++;
542 /* Worker for the walker that marks reaching definitions of REF, which
543 is not based on a non-aliased decl. For simplicity we need to end
544 up marking all may-defs necessary that are not based on a non-aliased
545 decl. The only job of this walker is to skip may-defs based on
546 a non-aliased decl. */
548 static bool
549 mark_all_reaching_defs_necessary_1 (ao_ref *ref ATTRIBUTE_UNUSED,
550 tree vdef, void *data ATTRIBUTE_UNUSED)
552 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
554 /* We have to skip already visited (and thus necessary) statements
555 to make the chaining work after we dropped back to simple mode. */
556 if (chain_ovfl
557 && bitmap_bit_p (processed, SSA_NAME_VERSION (vdef)))
559 gcc_assert (gimple_nop_p (def_stmt)
560 || gimple_plf (def_stmt, STMT_NECESSARY));
561 return false;
564 /* We want to skip stores to non-aliased variables. */
565 if (!chain_ovfl
566 && gimple_assign_single_p (def_stmt))
568 tree lhs = gimple_assign_lhs (def_stmt);
569 if (!ref_may_be_aliased (lhs))
570 return false;
573 /* We want to skip statments that do not constitute stores but have
574 a virtual definition. */
575 if (is_gimple_call (def_stmt))
577 tree callee = gimple_call_fndecl (def_stmt);
578 if (callee != NULL_TREE
579 && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
580 switch (DECL_FUNCTION_CODE (callee))
582 case BUILT_IN_MALLOC:
583 case BUILT_IN_ALIGNED_ALLOC:
584 case BUILT_IN_CALLOC:
585 CASE_BUILT_IN_ALLOCA:
586 case BUILT_IN_FREE:
587 return false;
589 default:;
593 if (! gimple_clobber_p (def_stmt))
594 mark_operand_necessary (vdef);
596 return false;
599 static void
600 mark_all_reaching_defs_necessary (gimple *stmt)
602 walk_aliased_vdefs (NULL, gimple_vuse (stmt),
603 mark_all_reaching_defs_necessary_1, NULL, &visited);
606 /* Return true for PHI nodes with one or identical arguments
607 can be removed. */
608 static bool
609 degenerate_phi_p (gimple *phi)
611 unsigned int i;
612 tree op = gimple_phi_arg_def (phi, 0);
613 for (i = 1; i < gimple_phi_num_args (phi); i++)
614 if (gimple_phi_arg_def (phi, i) != op)
615 return false;
616 return true;
619 /* Propagate necessity using the operands of necessary statements.
620 Process the uses on each statement in the worklist, and add all
621 feeding statements which contribute to the calculation of this
622 value to the worklist.
624 In conservative mode, EL is NULL. */
626 static void
627 propagate_necessity (bool aggressive)
629 gimple *stmt;
631 if (dump_file && (dump_flags & TDF_DETAILS))
632 fprintf (dump_file, "\nProcessing worklist:\n");
634 while (worklist.length () > 0)
636 /* Take STMT from worklist. */
637 stmt = worklist.pop ();
639 if (dump_file && (dump_flags & TDF_DETAILS))
641 fprintf (dump_file, "processing: ");
642 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
643 fprintf (dump_file, "\n");
646 if (aggressive)
648 /* Mark the last statement of the basic blocks on which the block
649 containing STMT is control dependent, but only if we haven't
650 already done so. */
651 basic_block bb = gimple_bb (stmt);
652 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
653 && !bitmap_bit_p (visited_control_parents, bb->index))
654 mark_control_dependent_edges_necessary (bb, false);
657 if (gimple_code (stmt) == GIMPLE_PHI
658 /* We do not process virtual PHI nodes nor do we track their
659 necessity. */
660 && !virtual_operand_p (gimple_phi_result (stmt)))
662 /* PHI nodes are somewhat special in that each PHI alternative has
663 data and control dependencies. All the statements feeding the
664 PHI node's arguments are always necessary. In aggressive mode,
665 we also consider the control dependent edges leading to the
666 predecessor block associated with each PHI alternative as
667 necessary. */
668 gphi *phi = as_a <gphi *> (stmt);
669 size_t k;
671 for (k = 0; k < gimple_phi_num_args (stmt); k++)
673 tree arg = PHI_ARG_DEF (stmt, k);
674 if (TREE_CODE (arg) == SSA_NAME)
675 mark_operand_necessary (arg);
678 /* For PHI operands it matters from where the control flow arrives
679 to the BB. Consider the following example:
681 a=exp1;
682 b=exp2;
683 if (test)
685 else
687 c=PHI(a,b)
689 We need to mark control dependence of the empty basic blocks, since they
690 contains computation of PHI operands.
692 Doing so is too restrictive in the case the predecestor block is in
693 the loop. Consider:
695 if (b)
697 int i;
698 for (i = 0; i<1000; ++i)
700 j = 0;
702 return j;
704 There is PHI for J in the BB containing return statement.
705 In this case the control dependence of predecestor block (that is
706 within the empty loop) also contains the block determining number
707 of iterations of the block that would prevent removing of empty
708 loop in this case.
710 This scenario can be avoided by splitting critical edges.
711 To save the critical edge splitting pass we identify how the control
712 dependence would look like if the edge was split.
714 Consider the modified CFG created from current CFG by splitting
715 edge B->C. In the postdominance tree of modified CFG, C' is
716 always child of C. There are two cases how chlids of C' can look
717 like:
719 1) C' is leaf
721 In this case the only basic block C' is control dependent on is B.
723 2) C' has single child that is B
725 In this case control dependence of C' is same as control
726 dependence of B in original CFG except for block B itself.
727 (since C' postdominate B in modified CFG)
729 Now how to decide what case happens? There are two basic options:
731 a) C postdominate B. Then C immediately postdominate B and
732 case 2 happens iff there is no other way from B to C except
733 the edge B->C.
735 There is other way from B to C iff there is succesor of B that
736 is not postdominated by B. Testing this condition is somewhat
737 expensive, because we need to iterate all succesors of B.
738 We are safe to assume that this does not happen: we will mark B
739 as needed when processing the other path from B to C that is
740 conrol dependent on B and marking control dependencies of B
741 itself is harmless because they will be processed anyway after
742 processing control statement in B.
744 b) C does not postdominate B. Always case 1 happens since there is
745 path from C to exit that does not go through B and thus also C'. */
747 if (aggressive && !degenerate_phi_p (stmt))
749 for (k = 0; k < gimple_phi_num_args (stmt); k++)
751 basic_block arg_bb = gimple_phi_arg_edge (phi, k)->src;
753 if (gimple_bb (stmt)
754 != get_immediate_dominator (CDI_POST_DOMINATORS, arg_bb))
756 if (!bitmap_bit_p (last_stmt_necessary, arg_bb->index))
757 mark_last_stmt_necessary (arg_bb);
759 else if (arg_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
760 && !bitmap_bit_p (visited_control_parents,
761 arg_bb->index))
762 mark_control_dependent_edges_necessary (arg_bb, true);
766 else
768 /* Propagate through the operands. Examine all the USE, VUSE and
769 VDEF operands in this statement. Mark all the statements
770 which feed this statement's uses as necessary. */
771 ssa_op_iter iter;
772 tree use;
774 /* If this is a call to free which is directly fed by an
775 allocation function do not mark that necessary through
776 processing the argument. */
777 if (gimple_call_builtin_p (stmt, BUILT_IN_FREE))
779 tree ptr = gimple_call_arg (stmt, 0);
780 gimple *def_stmt;
781 tree def_callee;
782 /* If the pointer we free is defined by an allocation
783 function do not add the call to the worklist. */
784 if (TREE_CODE (ptr) == SSA_NAME
785 && is_gimple_call (def_stmt = SSA_NAME_DEF_STMT (ptr))
786 && (def_callee = gimple_call_fndecl (def_stmt))
787 && DECL_BUILT_IN_CLASS (def_callee) == BUILT_IN_NORMAL
788 && (DECL_FUNCTION_CODE (def_callee) == BUILT_IN_ALIGNED_ALLOC
789 || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_MALLOC
790 || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_CALLOC))
791 continue;
794 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
795 mark_operand_necessary (use);
797 use = gimple_vuse (stmt);
798 if (!use)
799 continue;
801 /* If we dropped to simple mode make all immediately
802 reachable definitions necessary. */
803 if (chain_ovfl)
805 mark_all_reaching_defs_necessary (stmt);
806 continue;
809 /* For statements that may load from memory (have a VUSE) we
810 have to mark all reaching (may-)definitions as necessary.
811 We partition this task into two cases:
812 1) explicit loads based on decls that are not aliased
813 2) implicit loads (like calls) and explicit loads not
814 based on decls that are not aliased (like indirect
815 references or loads from globals)
816 For 1) we mark all reaching may-defs as necessary, stopping
817 at dominating kills. For 2) we want to mark all dominating
818 references necessary, but non-aliased ones which we handle
819 in 1). By keeping a global visited bitmap for references
820 we walk for 2) we avoid quadratic behavior for those. */
822 if (is_gimple_call (stmt))
824 tree callee = gimple_call_fndecl (stmt);
825 unsigned i;
827 /* Calls to functions that are merely acting as barriers
828 or that only store to memory do not make any previous
829 stores necessary. */
830 if (callee != NULL_TREE
831 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
832 && (DECL_FUNCTION_CODE (callee) == BUILT_IN_MEMSET
833 || DECL_FUNCTION_CODE (callee) == BUILT_IN_MEMSET_CHK
834 || DECL_FUNCTION_CODE (callee) == BUILT_IN_MALLOC
835 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALIGNED_ALLOC
836 || DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC
837 || DECL_FUNCTION_CODE (callee) == BUILT_IN_FREE
838 || DECL_FUNCTION_CODE (callee) == BUILT_IN_VA_END
839 || ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callee))
840 || DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE
841 || DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE
842 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ASSUME_ALIGNED))
843 continue;
845 /* Calls implicitly load from memory, their arguments
846 in addition may explicitly perform memory loads. */
847 mark_all_reaching_defs_necessary (stmt);
848 for (i = 0; i < gimple_call_num_args (stmt); ++i)
850 tree arg = gimple_call_arg (stmt, i);
851 if (TREE_CODE (arg) == SSA_NAME
852 || is_gimple_min_invariant (arg))
853 continue;
854 if (TREE_CODE (arg) == WITH_SIZE_EXPR)
855 arg = TREE_OPERAND (arg, 0);
856 if (!ref_may_be_aliased (arg))
857 mark_aliased_reaching_defs_necessary (stmt, arg);
860 else if (gimple_assign_single_p (stmt))
862 tree rhs;
863 /* If this is a load mark things necessary. */
864 rhs = gimple_assign_rhs1 (stmt);
865 if (TREE_CODE (rhs) != SSA_NAME
866 && !is_gimple_min_invariant (rhs)
867 && TREE_CODE (rhs) != CONSTRUCTOR)
869 if (!ref_may_be_aliased (rhs))
870 mark_aliased_reaching_defs_necessary (stmt, rhs);
871 else
872 mark_all_reaching_defs_necessary (stmt);
875 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
877 tree rhs = gimple_return_retval (return_stmt);
878 /* A return statement may perform a load. */
879 if (rhs
880 && TREE_CODE (rhs) != SSA_NAME
881 && !is_gimple_min_invariant (rhs)
882 && TREE_CODE (rhs) != CONSTRUCTOR)
884 if (!ref_may_be_aliased (rhs))
885 mark_aliased_reaching_defs_necessary (stmt, rhs);
886 else
887 mark_all_reaching_defs_necessary (stmt);
890 else if (gasm *asm_stmt = dyn_cast <gasm *> (stmt))
892 unsigned i;
893 mark_all_reaching_defs_necessary (stmt);
894 /* Inputs may perform loads. */
895 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
897 tree op = TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
898 if (TREE_CODE (op) != SSA_NAME
899 && !is_gimple_min_invariant (op)
900 && TREE_CODE (op) != CONSTRUCTOR
901 && !ref_may_be_aliased (op))
902 mark_aliased_reaching_defs_necessary (stmt, op);
905 else if (gimple_code (stmt) == GIMPLE_TRANSACTION)
907 /* The beginning of a transaction is a memory barrier. */
908 /* ??? If we were really cool, we'd only be a barrier
909 for the memories touched within the transaction. */
910 mark_all_reaching_defs_necessary (stmt);
912 else
913 gcc_unreachable ();
915 /* If we over-used our alias oracle budget drop to simple
916 mode. The cost metric allows quadratic behavior
917 (number of uses times number of may-defs queries) up to
918 a constant maximal number of queries and after that falls back to
919 super-linear complexity. */
920 if (/* Constant but quadratic for small functions. */
921 total_chain > 128 * 128
922 /* Linear in the number of may-defs. */
923 && total_chain > 32 * longest_chain
924 /* Linear in the number of uses. */
925 && total_chain > nr_walks * 32)
927 chain_ovfl = true;
928 if (visited)
929 bitmap_clear (visited);
935 /* Remove dead PHI nodes from block BB. */
937 static bool
938 remove_dead_phis (basic_block bb)
940 bool something_changed = false;
941 gphi *phi;
942 gphi_iterator gsi;
944 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);)
946 stats.total_phis++;
947 phi = gsi.phi ();
949 /* We do not track necessity of virtual PHI nodes. Instead do
950 very simple dead PHI removal here. */
951 if (virtual_operand_p (gimple_phi_result (phi)))
953 /* Virtual PHI nodes with one or identical arguments
954 can be removed. */
955 if (degenerate_phi_p (phi))
957 tree vdef = gimple_phi_result (phi);
958 tree vuse = gimple_phi_arg_def (phi, 0);
960 use_operand_p use_p;
961 imm_use_iterator iter;
962 gimple *use_stmt;
963 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
964 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
965 SET_USE (use_p, vuse);
966 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef)
967 && TREE_CODE (vuse) == SSA_NAME)
968 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
970 else
971 gimple_set_plf (phi, STMT_NECESSARY, true);
974 if (!gimple_plf (phi, STMT_NECESSARY))
976 something_changed = true;
977 if (dump_file && (dump_flags & TDF_DETAILS))
979 fprintf (dump_file, "Deleting : ");
980 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
981 fprintf (dump_file, "\n");
984 remove_phi_node (&gsi, true);
985 stats.removed_phis++;
986 continue;
989 gsi_next (&gsi);
991 return something_changed;
995 /* Remove dead statement pointed to by iterator I. Receives the basic block BB
996 containing I so that we don't have to look it up. */
998 static void
999 remove_dead_stmt (gimple_stmt_iterator *i, basic_block bb,
1000 vec<edge> &to_remove_edges)
1002 gimple *stmt = gsi_stmt (*i);
1004 if (dump_file && (dump_flags & TDF_DETAILS))
1006 fprintf (dump_file, "Deleting : ");
1007 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1008 fprintf (dump_file, "\n");
1011 stats.removed++;
1013 /* If we have determined that a conditional branch statement contributes
1014 nothing to the program, then we not only remove it, but we need to update
1015 the CFG. We can chose any of edges out of BB as long as we are sure to not
1016 close infinite loops. This is done by always choosing the edge closer to
1017 exit in inverted_post_order_compute order. */
1018 if (is_ctrl_stmt (stmt))
1020 edge_iterator ei;
1021 edge e = NULL, e2;
1023 /* See if there is only one non-abnormal edge. */
1024 if (single_succ_p (bb))
1025 e = single_succ_edge (bb);
1026 /* Otherwise chose one that is closer to bb with live statement in it.
1027 To be able to chose one, we compute inverted post order starting from
1028 all BBs with live statements. */
1029 if (!e)
1031 if (!bb_postorder)
1033 auto_vec<int, 20> postorder;
1034 inverted_post_order_compute (&postorder,
1035 &bb_contains_live_stmts);
1036 bb_postorder = XNEWVEC (int, last_basic_block_for_fn (cfun));
1037 for (unsigned int i = 0; i < postorder.length (); ++i)
1038 bb_postorder[postorder[i]] = i;
1040 FOR_EACH_EDGE (e2, ei, bb->succs)
1041 if (!e || e2->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
1042 || bb_postorder [e->dest->index]
1043 < bb_postorder [e2->dest->index])
1044 e = e2;
1046 gcc_assert (e);
1047 e->probability = profile_probability::always ();
1049 /* The edge is no longer associated with a conditional, so it does
1050 not have TRUE/FALSE flags.
1051 We are also safe to drop EH/ABNORMAL flags and turn them into
1052 normal control flow, because we know that all the destinations (including
1053 those odd edges) are equivalent for program execution. */
1054 e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_EH | EDGE_ABNORMAL);
1056 /* The lone outgoing edge from BB will be a fallthru edge. */
1057 e->flags |= EDGE_FALLTHRU;
1059 /* Remove the remaining outgoing edges. */
1060 FOR_EACH_EDGE (e2, ei, bb->succs)
1061 if (e != e2)
1063 /* If we made a BB unconditionally exit a loop or removed
1064 an entry into an irreducible region, then this transform
1065 alters the set of BBs in the loop. Schedule a fixup. */
1066 if (loop_exit_edge_p (bb->loop_father, e)
1067 || (e2->dest->flags & BB_IRREDUCIBLE_LOOP))
1068 loops_state_set (LOOPS_NEED_FIXUP);
1069 to_remove_edges.safe_push (e2);
1073 /* If this is a store into a variable that is being optimized away,
1074 add a debug bind stmt if possible. */
1075 if (MAY_HAVE_DEBUG_BIND_STMTS
1076 && gimple_assign_single_p (stmt)
1077 && is_gimple_val (gimple_assign_rhs1 (stmt)))
1079 tree lhs = gimple_assign_lhs (stmt);
1080 if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
1081 && !DECL_IGNORED_P (lhs)
1082 && is_gimple_reg_type (TREE_TYPE (lhs))
1083 && !is_global_var (lhs)
1084 && !DECL_HAS_VALUE_EXPR_P (lhs))
1086 tree rhs = gimple_assign_rhs1 (stmt);
1087 gdebug *note
1088 = gimple_build_debug_bind (lhs, unshare_expr (rhs), stmt);
1089 gsi_insert_after (i, note, GSI_SAME_STMT);
1093 unlink_stmt_vdef (stmt);
1094 gsi_remove (i, true);
1095 release_defs (stmt);
1098 /* Helper for maybe_optimize_arith_overflow. Find in *TP if there are any
1099 uses of data (SSA_NAME) other than REALPART_EXPR referencing it. */
1101 static tree
1102 find_non_realpart_uses (tree *tp, int *walk_subtrees, void *data)
1104 if (TYPE_P (*tp) || TREE_CODE (*tp) == REALPART_EXPR)
1105 *walk_subtrees = 0;
1106 if (*tp == (tree) data)
1107 return *tp;
1108 return NULL_TREE;
1111 /* If the IMAGPART_EXPR of the {ADD,SUB,MUL}_OVERFLOW result is never used,
1112 but REALPART_EXPR is, optimize the {ADD,SUB,MUL}_OVERFLOW internal calls
1113 into plain unsigned {PLUS,MINUS,MULT}_EXPR, and if needed reset debug
1114 uses. */
1116 static void
1117 maybe_optimize_arith_overflow (gimple_stmt_iterator *gsi,
1118 enum tree_code subcode)
1120 gimple *stmt = gsi_stmt (*gsi);
1121 tree lhs = gimple_call_lhs (stmt);
1123 if (lhs == NULL || TREE_CODE (lhs) != SSA_NAME)
1124 return;
1126 imm_use_iterator imm_iter;
1127 use_operand_p use_p;
1128 bool has_debug_uses = false;
1129 bool has_realpart_uses = false;
1130 bool has_other_uses = false;
1131 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
1133 gimple *use_stmt = USE_STMT (use_p);
1134 if (is_gimple_debug (use_stmt))
1135 has_debug_uses = true;
1136 else if (is_gimple_assign (use_stmt)
1137 && gimple_assign_rhs_code (use_stmt) == REALPART_EXPR
1138 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == lhs)
1139 has_realpart_uses = true;
1140 else
1142 has_other_uses = true;
1143 break;
1147 if (!has_realpart_uses || has_other_uses)
1148 return;
1150 tree arg0 = gimple_call_arg (stmt, 0);
1151 tree arg1 = gimple_call_arg (stmt, 1);
1152 location_t loc = gimple_location (stmt);
1153 tree type = TREE_TYPE (TREE_TYPE (lhs));
1154 tree utype = type;
1155 if (!TYPE_UNSIGNED (type))
1156 utype = build_nonstandard_integer_type (TYPE_PRECISION (type), 1);
1157 tree result = fold_build2_loc (loc, subcode, utype,
1158 fold_convert_loc (loc, utype, arg0),
1159 fold_convert_loc (loc, utype, arg1));
1160 result = fold_convert_loc (loc, type, result);
1162 if (has_debug_uses)
1164 gimple *use_stmt;
1165 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, lhs)
1167 if (!gimple_debug_bind_p (use_stmt))
1168 continue;
1169 tree v = gimple_debug_bind_get_value (use_stmt);
1170 if (walk_tree (&v, find_non_realpart_uses, lhs, NULL))
1172 gimple_debug_bind_reset_value (use_stmt);
1173 update_stmt (use_stmt);
1178 if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
1179 result = drop_tree_overflow (result);
1180 tree overflow = build_zero_cst (type);
1181 tree ctype = build_complex_type (type);
1182 if (TREE_CODE (result) == INTEGER_CST)
1183 result = build_complex (ctype, result, overflow);
1184 else
1185 result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
1186 ctype, result, overflow);
1188 if (dump_file && (dump_flags & TDF_DETAILS))
1190 fprintf (dump_file, "Transforming call: ");
1191 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1192 fprintf (dump_file, "because the overflow result is never used into: ");
1193 print_generic_stmt (dump_file, result, TDF_SLIM);
1194 fprintf (dump_file, "\n");
1197 if (!update_call_from_tree (gsi, result))
1198 gimplify_and_update_call_from_tree (gsi, result);
1201 /* Eliminate unnecessary statements. Any instruction not marked as necessary
1202 contributes nothing to the program, and can be deleted. */
1204 static bool
1205 eliminate_unnecessary_stmts (void)
1207 bool something_changed = false;
1208 basic_block bb;
1209 gimple_stmt_iterator gsi, psi;
1210 gimple *stmt;
1211 tree call;
1212 vec<basic_block> h;
1213 auto_vec<edge> to_remove_edges;
1215 if (dump_file && (dump_flags & TDF_DETAILS))
1216 fprintf (dump_file, "\nEliminating unnecessary statements:\n");
1218 clear_special_calls ();
1220 /* Walking basic blocks and statements in reverse order avoids
1221 releasing SSA names before any other DEFs that refer to them are
1222 released. This helps avoid loss of debug information, as we get
1223 a chance to propagate all RHSs of removed SSAs into debug uses,
1224 rather than only the latest ones. E.g., consider:
1226 x_3 = y_1 + z_2;
1227 a_5 = x_3 - b_4;
1228 # DEBUG a => a_5
1230 If we were to release x_3 before a_5, when we reached a_5 and
1231 tried to substitute it into the debug stmt, we'd see x_3 there,
1232 but x_3's DEF, type, etc would have already been disconnected.
1233 By going backwards, the debug stmt first changes to:
1235 # DEBUG a => x_3 - b_4
1237 and then to:
1239 # DEBUG a => y_1 + z_2 - b_4
1241 as desired. */
1242 gcc_assert (dom_info_available_p (CDI_DOMINATORS));
1243 h = get_all_dominated_blocks (CDI_DOMINATORS,
1244 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1246 while (h.length ())
1248 bb = h.pop ();
1250 /* Remove dead statements. */
1251 auto_bitmap debug_seen;
1252 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi = psi)
1254 stmt = gsi_stmt (gsi);
1256 psi = gsi;
1257 gsi_prev (&psi);
1259 stats.total++;
1261 /* We can mark a call to free as not necessary if the
1262 defining statement of its argument is not necessary
1263 (and thus is getting removed). */
1264 if (gimple_plf (stmt, STMT_NECESSARY)
1265 && gimple_call_builtin_p (stmt, BUILT_IN_FREE))
1267 tree ptr = gimple_call_arg (stmt, 0);
1268 if (TREE_CODE (ptr) == SSA_NAME)
1270 gimple *def_stmt = SSA_NAME_DEF_STMT (ptr);
1271 if (!gimple_nop_p (def_stmt)
1272 && !gimple_plf (def_stmt, STMT_NECESSARY))
1273 gimple_set_plf (stmt, STMT_NECESSARY, false);
1277 /* If GSI is not necessary then remove it. */
1278 if (!gimple_plf (stmt, STMT_NECESSARY))
1280 /* Keep clobbers that we can keep live live. */
1281 if (gimple_clobber_p (stmt))
1283 ssa_op_iter iter;
1284 use_operand_p use_p;
1285 bool dead = false;
1286 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1288 tree name = USE_FROM_PTR (use_p);
1289 if (!SSA_NAME_IS_DEFAULT_DEF (name)
1290 && !bitmap_bit_p (processed, SSA_NAME_VERSION (name)))
1292 dead = true;
1293 break;
1296 if (!dead)
1298 bitmap_clear (debug_seen);
1299 continue;
1302 if (!is_gimple_debug (stmt))
1303 something_changed = true;
1304 remove_dead_stmt (&gsi, bb, to_remove_edges);
1305 continue;
1307 else if (is_gimple_call (stmt))
1309 tree name = gimple_call_lhs (stmt);
1311 notice_special_calls (as_a <gcall *> (stmt));
1313 /* When LHS of var = call (); is dead, simplify it into
1314 call (); saving one operand. */
1315 if (name
1316 && TREE_CODE (name) == SSA_NAME
1317 && !bitmap_bit_p (processed, SSA_NAME_VERSION (name))
1318 /* Avoid doing so for allocation calls which we
1319 did not mark as necessary, it will confuse the
1320 special logic we apply to malloc/free pair removal. */
1321 && (!(call = gimple_call_fndecl (stmt))
1322 || DECL_BUILT_IN_CLASS (call) != BUILT_IN_NORMAL
1323 || (DECL_FUNCTION_CODE (call) != BUILT_IN_ALIGNED_ALLOC
1324 && DECL_FUNCTION_CODE (call) != BUILT_IN_MALLOC
1325 && DECL_FUNCTION_CODE (call) != BUILT_IN_CALLOC
1326 && !ALLOCA_FUNCTION_CODE_P
1327 (DECL_FUNCTION_CODE (call)))))
1329 something_changed = true;
1330 if (dump_file && (dump_flags & TDF_DETAILS))
1332 fprintf (dump_file, "Deleting LHS of call: ");
1333 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1334 fprintf (dump_file, "\n");
1337 gimple_call_set_lhs (stmt, NULL_TREE);
1338 maybe_clean_or_replace_eh_stmt (stmt, stmt);
1339 update_stmt (stmt);
1340 release_ssa_name (name);
1342 /* GOMP_SIMD_LANE (unless three argument) or ASAN_POISON
1343 without lhs is not needed. */
1344 if (gimple_call_internal_p (stmt))
1345 switch (gimple_call_internal_fn (stmt))
1347 case IFN_GOMP_SIMD_LANE:
1348 if (gimple_call_num_args (stmt) >= 3
1349 && !integer_nonzerop (gimple_call_arg (stmt, 2)))
1350 break;
1351 /* FALLTHRU */
1352 case IFN_ASAN_POISON:
1353 remove_dead_stmt (&gsi, bb, to_remove_edges);
1354 break;
1355 default:
1356 break;
1359 else if (gimple_call_internal_p (stmt))
1360 switch (gimple_call_internal_fn (stmt))
1362 case IFN_ADD_OVERFLOW:
1363 maybe_optimize_arith_overflow (&gsi, PLUS_EXPR);
1364 break;
1365 case IFN_SUB_OVERFLOW:
1366 maybe_optimize_arith_overflow (&gsi, MINUS_EXPR);
1367 break;
1368 case IFN_MUL_OVERFLOW:
1369 maybe_optimize_arith_overflow (&gsi, MULT_EXPR);
1370 break;
1371 default:
1372 break;
1375 else if (gimple_debug_bind_p (stmt))
1377 /* We are only keeping the last debug-bind of a
1378 non-DEBUG_EXPR_DECL variable in a series of
1379 debug-bind stmts. */
1380 tree var = gimple_debug_bind_get_var (stmt);
1381 if (TREE_CODE (var) != DEBUG_EXPR_DECL
1382 && !bitmap_set_bit (debug_seen, DECL_UID (var)))
1383 remove_dead_stmt (&gsi, bb, to_remove_edges);
1384 continue;
1386 bitmap_clear (debug_seen);
1389 /* Remove dead PHI nodes. */
1390 something_changed |= remove_dead_phis (bb);
1393 h.release ();
1395 /* Since we don't track liveness of virtual PHI nodes, it is possible that we
1396 rendered some PHI nodes unreachable while they are still in use.
1397 Mark them for renaming. */
1398 if (!to_remove_edges.is_empty ())
1400 basic_block prev_bb;
1402 /* Remove edges. We've delayed this to not get bogus debug stmts
1403 during PHI node removal. */
1404 for (unsigned i = 0; i < to_remove_edges.length (); ++i)
1405 remove_edge (to_remove_edges[i]);
1406 cfg_altered = true;
1408 find_unreachable_blocks ();
1410 /* Delete all unreachable basic blocks in reverse dominator order. */
1411 for (bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
1412 bb != ENTRY_BLOCK_PTR_FOR_FN (cfun); bb = prev_bb)
1414 prev_bb = bb->prev_bb;
1416 if (!bitmap_bit_p (bb_contains_live_stmts, bb->index)
1417 || !(bb->flags & BB_REACHABLE))
1419 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1420 gsi_next (&gsi))
1421 if (virtual_operand_p (gimple_phi_result (gsi.phi ())))
1423 bool found = false;
1424 imm_use_iterator iter;
1426 FOR_EACH_IMM_USE_STMT (stmt, iter,
1427 gimple_phi_result (gsi.phi ()))
1429 if (!(gimple_bb (stmt)->flags & BB_REACHABLE))
1430 continue;
1431 if (gimple_code (stmt) == GIMPLE_PHI
1432 || gimple_plf (stmt, STMT_NECESSARY))
1434 found = true;
1435 BREAK_FROM_IMM_USE_STMT (iter);
1438 if (found)
1439 mark_virtual_phi_result_for_renaming (gsi.phi ());
1442 if (!(bb->flags & BB_REACHABLE))
1444 /* Speed up the removal of blocks that don't
1445 dominate others. Walking backwards, this should
1446 be the common case. ??? Do we need to recompute
1447 dominators because of cfg_altered? */
1448 if (!first_dom_son (CDI_DOMINATORS, bb))
1449 delete_basic_block (bb);
1450 else
1452 h = get_all_dominated_blocks (CDI_DOMINATORS, bb);
1454 while (h.length ())
1456 bb = h.pop ();
1457 prev_bb = bb->prev_bb;
1458 /* Rearrangements to the CFG may have failed
1459 to update the dominators tree, so that
1460 formerly-dominated blocks are now
1461 otherwise reachable. */
1462 if (!!(bb->flags & BB_REACHABLE))
1463 continue;
1464 delete_basic_block (bb);
1467 h.release ();
1474 if (bb_postorder)
1475 free (bb_postorder);
1476 bb_postorder = NULL;
1478 return something_changed;
1482 /* Print out removed statement statistics. */
1484 static void
1485 print_stats (void)
1487 float percg;
1489 percg = ((float) stats.removed / (float) stats.total) * 100;
1490 fprintf (dump_file, "Removed %d of %d statements (%d%%)\n",
1491 stats.removed, stats.total, (int) percg);
1493 if (stats.total_phis == 0)
1494 percg = 0;
1495 else
1496 percg = ((float) stats.removed_phis / (float) stats.total_phis) * 100;
1498 fprintf (dump_file, "Removed %d of %d PHI nodes (%d%%)\n",
1499 stats.removed_phis, stats.total_phis, (int) percg);
1502 /* Initialization for this pass. Set up the used data structures. */
1504 static void
1505 tree_dce_init (bool aggressive)
1507 memset ((void *) &stats, 0, sizeof (stats));
1509 if (aggressive)
1511 last_stmt_necessary = sbitmap_alloc (last_basic_block_for_fn (cfun));
1512 bitmap_clear (last_stmt_necessary);
1513 bb_contains_live_stmts = sbitmap_alloc (last_basic_block_for_fn (cfun));
1514 bitmap_clear (bb_contains_live_stmts);
1517 processed = sbitmap_alloc (num_ssa_names + 1);
1518 bitmap_clear (processed);
1520 worklist.create (64);
1521 cfg_altered = false;
1524 /* Cleanup after this pass. */
1526 static void
1527 tree_dce_done (bool aggressive)
1529 if (aggressive)
1531 delete cd;
1532 sbitmap_free (visited_control_parents);
1533 sbitmap_free (last_stmt_necessary);
1534 sbitmap_free (bb_contains_live_stmts);
1535 bb_contains_live_stmts = NULL;
1538 sbitmap_free (processed);
1540 worklist.release ();
1543 /* Main routine to eliminate dead code.
1545 AGGRESSIVE controls the aggressiveness of the algorithm.
1546 In conservative mode, we ignore control dependence and simply declare
1547 all but the most trivially dead branches necessary. This mode is fast.
1548 In aggressive mode, control dependences are taken into account, which
1549 results in more dead code elimination, but at the cost of some time.
1551 FIXME: Aggressive mode before PRE doesn't work currently because
1552 the dominance info is not invalidated after DCE1. This is
1553 not an issue right now because we only run aggressive DCE
1554 as the last tree SSA pass, but keep this in mind when you
1555 start experimenting with pass ordering. */
1557 static unsigned int
1558 perform_tree_ssa_dce (bool aggressive)
1560 bool something_changed = 0;
1562 calculate_dominance_info (CDI_DOMINATORS);
1564 /* Preheaders are needed for SCEV to work.
1565 Simple lateches and recorded exits improve chances that loop will
1566 proved to be finite in testcases such as in loop-15.c and loop-24.c */
1567 bool in_loop_pipeline = scev_initialized_p ();
1568 if (aggressive && ! in_loop_pipeline)
1570 scev_initialize ();
1571 loop_optimizer_init (LOOPS_NORMAL
1572 | LOOPS_HAVE_RECORDED_EXITS);
1575 tree_dce_init (aggressive);
1577 if (aggressive)
1579 /* Compute control dependence. */
1580 calculate_dominance_info (CDI_POST_DOMINATORS);
1581 cd = new control_dependences ();
1583 visited_control_parents =
1584 sbitmap_alloc (last_basic_block_for_fn (cfun));
1585 bitmap_clear (visited_control_parents);
1587 mark_dfs_back_edges ();
1590 find_obviously_necessary_stmts (aggressive);
1592 if (aggressive && ! in_loop_pipeline)
1594 loop_optimizer_finalize ();
1595 scev_finalize ();
1598 longest_chain = 0;
1599 total_chain = 0;
1600 nr_walks = 0;
1601 chain_ovfl = false;
1602 visited = BITMAP_ALLOC (NULL);
1603 propagate_necessity (aggressive);
1604 BITMAP_FREE (visited);
1606 something_changed |= eliminate_unnecessary_stmts ();
1607 something_changed |= cfg_altered;
1609 /* We do not update postdominators, so free them unconditionally. */
1610 free_dominance_info (CDI_POST_DOMINATORS);
1612 /* If we removed paths in the CFG, then we need to update
1613 dominators as well. I haven't investigated the possibility
1614 of incrementally updating dominators. */
1615 if (cfg_altered)
1616 free_dominance_info (CDI_DOMINATORS);
1618 statistics_counter_event (cfun, "Statements deleted", stats.removed);
1619 statistics_counter_event (cfun, "PHI nodes deleted", stats.removed_phis);
1621 /* Debugging dumps. */
1622 if (dump_file && (dump_flags & (TDF_STATS|TDF_DETAILS)))
1623 print_stats ();
1625 tree_dce_done (aggressive);
1627 if (something_changed)
1629 free_numbers_of_iterations_estimates (cfun);
1630 if (in_loop_pipeline)
1631 scev_reset ();
1632 return TODO_update_ssa | TODO_cleanup_cfg;
1634 return 0;
1637 /* Pass entry points. */
1638 static unsigned int
1639 tree_ssa_dce (void)
1641 return perform_tree_ssa_dce (/*aggressive=*/false);
1644 static unsigned int
1645 tree_ssa_cd_dce (void)
1647 return perform_tree_ssa_dce (/*aggressive=*/optimize >= 2);
1650 namespace {
1652 const pass_data pass_data_dce =
1654 GIMPLE_PASS, /* type */
1655 "dce", /* name */
1656 OPTGROUP_NONE, /* optinfo_flags */
1657 TV_TREE_DCE, /* tv_id */
1658 ( PROP_cfg | PROP_ssa ), /* properties_required */
1659 0, /* properties_provided */
1660 0, /* properties_destroyed */
1661 0, /* todo_flags_start */
1662 0, /* todo_flags_finish */
1665 class pass_dce : public gimple_opt_pass
1667 public:
1668 pass_dce (gcc::context *ctxt)
1669 : gimple_opt_pass (pass_data_dce, ctxt)
1672 /* opt_pass methods: */
1673 opt_pass * clone () { return new pass_dce (m_ctxt); }
1674 virtual bool gate (function *) { return flag_tree_dce != 0; }
1675 virtual unsigned int execute (function *) { return tree_ssa_dce (); }
1677 }; // class pass_dce
1679 } // anon namespace
1681 gimple_opt_pass *
1682 make_pass_dce (gcc::context *ctxt)
1684 return new pass_dce (ctxt);
1687 namespace {
1689 const pass_data pass_data_cd_dce =
1691 GIMPLE_PASS, /* type */
1692 "cddce", /* name */
1693 OPTGROUP_NONE, /* optinfo_flags */
1694 TV_TREE_CD_DCE, /* tv_id */
1695 ( PROP_cfg | PROP_ssa ), /* properties_required */
1696 0, /* properties_provided */
1697 0, /* properties_destroyed */
1698 0, /* todo_flags_start */
1699 0, /* todo_flags_finish */
1702 class pass_cd_dce : public gimple_opt_pass
1704 public:
1705 pass_cd_dce (gcc::context *ctxt)
1706 : gimple_opt_pass (pass_data_cd_dce, ctxt)
1709 /* opt_pass methods: */
1710 opt_pass * clone () { return new pass_cd_dce (m_ctxt); }
1711 virtual bool gate (function *) { return flag_tree_dce != 0; }
1712 virtual unsigned int execute (function *) { return tree_ssa_cd_dce (); }
1714 }; // class pass_cd_dce
1716 } // anon namespace
1718 gimple_opt_pass *
1719 make_pass_cd_dce (gcc::context *ctxt)
1721 return new pass_cd_dce (ctxt);
1725 /* A cheap DCE interface. WORKLIST is a list of possibly dead stmts and
1726 is consumed by this function. The function has linear complexity in
1727 the number of dead stmts with a constant factor like the average SSA
1728 use operands number. */
1730 void
1731 simple_dce_from_worklist (bitmap worklist)
1733 while (! bitmap_empty_p (worklist))
1735 /* Pop item. */
1736 unsigned i = bitmap_first_set_bit (worklist);
1737 bitmap_clear_bit (worklist, i);
1739 tree def = ssa_name (i);
1740 /* Removed by somebody else or still in use. */
1741 if (! def || ! has_zero_uses (def))
1742 continue;
1744 gimple *t = SSA_NAME_DEF_STMT (def);
1745 if (gimple_has_side_effects (t))
1746 continue;
1748 /* Add uses to the worklist. */
1749 ssa_op_iter iter;
1750 use_operand_p use_p;
1751 FOR_EACH_PHI_OR_STMT_USE (use_p, t, iter, SSA_OP_USE)
1753 tree use = USE_FROM_PTR (use_p);
1754 if (TREE_CODE (use) == SSA_NAME
1755 && ! SSA_NAME_IS_DEFAULT_DEF (use))
1756 bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
1759 /* Remove stmt. */
1760 if (dump_file && (dump_flags & TDF_DETAILS))
1762 fprintf (dump_file, "Removing dead stmt:");
1763 print_gimple_stmt (dump_file, t, 0);
1765 gimple_stmt_iterator gsi = gsi_for_stmt (t);
1766 if (gimple_code (t) == GIMPLE_PHI)
1767 remove_phi_node (&gsi, true);
1768 else
1770 gsi_remove (&gsi, true);
1771 release_defs (t);