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