1 /* RTL dead code elimination.
2 Copyright (C) 2005-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
30 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
33 #include "cfgcleanup.h"
36 #include "tree-pass.h"
41 /* -------------------------------------------------------------------------
42 Core mark/delete routines
43 ------------------------------------------------------------------------- */
45 /* True if we are invoked while the df engine is running; in this case,
46 we don't want to reenter it. */
47 static bool df_in_progress
= false;
49 /* True if we are allowed to alter the CFG in this pass. */
50 static bool can_alter_cfg
= false;
52 /* Instructions that have been marked but whose dependencies have not
53 yet been processed. */
54 static vec
<rtx_insn
*> worklist
;
56 /* Bitmap of instructions marked as needed indexed by INSN_UID. */
57 static sbitmap marked
;
59 /* Bitmap obstacks used for block processing by the fast algorithm. */
60 static bitmap_obstack dce_blocks_bitmap_obstack
;
61 static bitmap_obstack dce_tmp_bitmap_obstack
;
63 static bool find_call_stack_args (rtx_call_insn
*, bool, bool, bitmap
);
65 /* A subroutine for which BODY is part of the instruction being tested;
66 either the top-level pattern, or an element of a PARALLEL. The
67 instruction is known not to be a bare USE or CLOBBER. */
70 deletable_insn_p_1 (rtx body
)
72 switch (GET_CODE (body
))
76 /* The UNSPEC case was added here because the ia-64 claims that
77 USEs do not work after reload and generates UNSPECS rather
78 than USEs. Since dce is run after reload we need to avoid
79 deleting these even if they are dead. If it turns out that
80 USEs really do work after reload, the ia-64 should be
81 changed, and the UNSPEC case can be removed. */
86 return !volatile_refs_p (body
);
90 /* Don't delete calls that may throw if we cannot do so. */
93 can_delete_call (rtx_insn
*insn
)
95 if (cfun
->can_delete_dead_exceptions
&& can_alter_cfg
)
97 if (!insn_nothrow_p (insn
))
101 /* If we can't alter cfg, even when the call can't throw exceptions, it
102 might have EDGE_ABNORMAL_CALL edges and so we shouldn't delete such
104 gcc_assert (CALL_P (insn
));
105 if (BLOCK_FOR_INSN (insn
) && BB_END (BLOCK_FOR_INSN (insn
)) == insn
)
110 FOR_EACH_EDGE (e
, ei
, BLOCK_FOR_INSN (insn
)->succs
)
111 if ((e
->flags
& EDGE_ABNORMAL_CALL
) != 0)
117 /* Return true if INSN is a normal instruction that can be deleted by
121 deletable_insn_p (rtx_insn
*insn
, bool fast
, bitmap arg_stores
)
128 /* We cannot delete calls inside of the recursive dce because
129 this may cause basic blocks to be deleted and this messes up
130 the rest of the stack of optimization passes. */
132 /* We cannot delete pure or const sibling calls because it is
133 hard to see the result. */
134 && (!SIBLING_CALL_P (insn
))
135 /* We can delete dead const or pure calls as long as they do not
137 && (RTL_CONST_OR_PURE_CALL_P (insn
)
138 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
))
139 /* Don't delete calls that may throw if we cannot do so. */
140 && can_delete_call (insn
))
141 return find_call_stack_args (as_a
<rtx_call_insn
*> (insn
), false,
144 /* Don't delete jumps, notes and the like. */
145 if (!NONJUMP_INSN_P (insn
))
148 /* Don't delete insns that may throw if we cannot do so. */
149 if (!(cfun
->can_delete_dead_exceptions
&& can_alter_cfg
)
150 && !insn_nothrow_p (insn
))
153 /* If INSN sets a global_reg, leave it untouched. */
154 FOR_EACH_INSN_DEF (def
, insn
)
155 if (HARD_REGISTER_NUM_P (DF_REF_REGNO (def
))
156 && global_regs
[DF_REF_REGNO (def
)])
158 /* Initialization of pseudo PIC register should never be removed. */
159 else if (DF_REF_REG (def
) == pic_offset_table_rtx
160 && REGNO (pic_offset_table_rtx
) >= FIRST_PSEUDO_REGISTER
)
163 /* Callee-save restores are needed. */
164 if (RTX_FRAME_RELATED_P (insn
)
165 && crtl
->shrink_wrapped_separate
166 && find_reg_note (insn
, REG_CFA_RESTORE
, NULL
))
169 body
= PATTERN (insn
);
170 switch (GET_CODE (body
))
180 /* A CLOBBER of a dead pseudo register serves no purpose.
181 That is not necessarily true for hard registers until
184 return REG_P (x
) && (!HARD_REGISTER_P (x
) || reload_completed
);
187 /* Because of the way that use-def chains are built, it is not
188 possible to tell if the clobber is dead because it can
189 never be the target of a use-def chain. */
193 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; i
--)
194 if (!deletable_insn_p_1 (XVECEXP (body
, 0, i
)))
199 return deletable_insn_p_1 (body
);
204 /* Return true if INSN has been marked as needed. */
207 marked_insn_p (rtx_insn
*insn
)
209 /* Artificial defs are always needed and they do not have an insn.
210 We should never see them here. */
212 return bitmap_bit_p (marked
, INSN_UID (insn
));
216 /* If INSN has not yet been marked as needed, mark it now, and add it to
220 mark_insn (rtx_insn
*insn
, bool fast
)
222 if (!marked_insn_p (insn
))
225 worklist
.safe_push (insn
);
226 bitmap_set_bit (marked
, INSN_UID (insn
));
228 fprintf (dump_file
, " Adding insn %d to worklist\n", INSN_UID (insn
));
231 && !SIBLING_CALL_P (insn
)
232 && (RTL_CONST_OR_PURE_CALL_P (insn
)
233 && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
))
234 && can_delete_call (insn
))
235 find_call_stack_args (as_a
<rtx_call_insn
*> (insn
), true, fast
, NULL
);
240 /* A note_stores callback used by mark_nonreg_stores. DATA is the
241 instruction containing DEST. */
244 mark_nonreg_stores_1 (rtx dest
, const_rtx pattern
, void *data
)
246 if (GET_CODE (pattern
) != CLOBBER
&& !REG_P (dest
))
248 gcc_checking_assert (GET_CODE (pattern
) != CLOBBER_HIGH
);
249 mark_insn ((rtx_insn
*) data
, true);
254 /* A note_stores callback used by mark_nonreg_stores. DATA is the
255 instruction containing DEST. */
258 mark_nonreg_stores_2 (rtx dest
, const_rtx pattern
, void *data
)
260 if (GET_CODE (pattern
) != CLOBBER
&& !REG_P (dest
))
262 gcc_checking_assert (GET_CODE (pattern
) != CLOBBER_HIGH
);
263 mark_insn ((rtx_insn
*) data
, false);
268 /* Mark INSN if BODY stores to a non-register destination. */
271 mark_nonreg_stores (rtx body
, rtx_insn
*insn
, bool fast
)
274 note_stores (body
, mark_nonreg_stores_1
, insn
);
276 note_stores (body
, mark_nonreg_stores_2
, insn
);
280 /* Return true if a store to SIZE bytes, starting OFF bytes from stack pointer,
281 is a call argument store, and clear corresponding bits from SP_BYTES
285 check_argument_store (HOST_WIDE_INT size
, HOST_WIDE_INT off
,
286 HOST_WIDE_INT min_sp_off
, HOST_WIDE_INT max_sp_off
,
290 for (byte
= off
; byte
< off
+ size
; byte
++)
292 if (byte
< min_sp_off
293 || byte
>= max_sp_off
294 || !bitmap_clear_bit (sp_bytes
, byte
- min_sp_off
))
300 /* If MEM has sp address, return 0, if it has sp + const address,
301 return that const, if it has reg address where reg is set to sp + const
302 and FAST is false, return const, otherwise return
303 INTTYPE_MINUMUM (HOST_WIDE_INT). */
306 sp_based_mem_offset (rtx_call_insn
*call_insn
, const_rtx mem
, bool fast
)
308 HOST_WIDE_INT off
= 0;
309 rtx addr
= XEXP (mem
, 0);
310 if (GET_CODE (addr
) == PLUS
311 && REG_P (XEXP (addr
, 0))
312 && CONST_INT_P (XEXP (addr
, 1)))
314 off
= INTVAL (XEXP (addr
, 1));
315 addr
= XEXP (addr
, 0);
317 if (addr
== stack_pointer_rtx
)
320 if (!REG_P (addr
) || fast
)
321 return INTTYPE_MINIMUM (HOST_WIDE_INT
);
323 /* If not fast, use chains to see if addr wasn't set to sp + offset. */
325 FOR_EACH_INSN_USE (use
, call_insn
)
326 if (rtx_equal_p (addr
, DF_REF_REG (use
)))
330 return INTTYPE_MINIMUM (HOST_WIDE_INT
);
332 struct df_link
*defs
;
333 for (defs
= DF_REF_CHAIN (use
); defs
; defs
= defs
->next
)
334 if (! DF_REF_IS_ARTIFICIAL (defs
->ref
))
338 return INTTYPE_MINIMUM (HOST_WIDE_INT
);
340 rtx set
= single_set (DF_REF_INSN (defs
->ref
));
342 return INTTYPE_MINIMUM (HOST_WIDE_INT
);
344 if (GET_CODE (SET_SRC (set
)) != PLUS
345 || XEXP (SET_SRC (set
), 0) != stack_pointer_rtx
346 || !CONST_INT_P (XEXP (SET_SRC (set
), 1)))
347 return INTTYPE_MINIMUM (HOST_WIDE_INT
);
349 off
+= INTVAL (XEXP (SET_SRC (set
), 1));
353 /* Data for check_argument_load called via note_uses. */
354 struct check_argument_load_data
{
356 HOST_WIDE_INT min_sp_off
, max_sp_off
;
357 rtx_call_insn
*call_insn
;
362 /* Helper function for find_call_stack_args. Check if there are
363 any loads from the argument slots in between the const/pure call
364 and store to the argument slot, set LOAD_FOUND if any is found. */
367 check_argument_load (rtx
*loc
, void *data
)
369 struct check_argument_load_data
*d
370 = (struct check_argument_load_data
*) data
;
371 subrtx_iterator::array_type array
;
372 FOR_EACH_SUBRTX (iter
, array
, *loc
, NONCONST
)
374 const_rtx mem
= *iter
;
377 && MEM_SIZE_KNOWN_P (mem
)
378 && MEM_SIZE (mem
).is_constant (&size
))
380 HOST_WIDE_INT off
= sp_based_mem_offset (d
->call_insn
, mem
, d
->fast
);
381 if (off
!= INTTYPE_MINIMUM (HOST_WIDE_INT
)
382 && off
< d
->max_sp_off
383 && off
+ size
> d
->min_sp_off
)
384 for (HOST_WIDE_INT byte
= MAX (off
, d
->min_sp_off
);
385 byte
< MIN (off
+ size
, d
->max_sp_off
); byte
++)
386 if (bitmap_bit_p (d
->sp_bytes
, byte
- d
->min_sp_off
))
388 d
->load_found
= true;
395 /* Try to find all stack stores of CALL_INSN arguments if
396 ACCUMULATE_OUTGOING_ARGS. If all stack stores have been found
397 and it is therefore safe to eliminate the call, return true,
398 otherwise return false. This function should be first called
399 with DO_MARK false, and only when the CALL_INSN is actually
400 going to be marked called again with DO_MARK true. */
403 find_call_stack_args (rtx_call_insn
*call_insn
, bool do_mark
, bool fast
,
407 rtx_insn
*insn
, *prev_insn
;
409 HOST_WIDE_INT min_sp_off
, max_sp_off
;
412 gcc_assert (CALL_P (call_insn
));
413 if (!ACCUMULATE_OUTGOING_ARGS
)
418 gcc_assert (arg_stores
);
419 bitmap_clear (arg_stores
);
422 min_sp_off
= INTTYPE_MAXIMUM (HOST_WIDE_INT
);
425 /* First determine the minimum and maximum offset from sp for
427 for (p
= CALL_INSN_FUNCTION_USAGE (call_insn
); p
; p
= XEXP (p
, 1))
428 if (GET_CODE (XEXP (p
, 0)) == USE
429 && MEM_P (XEXP (XEXP (p
, 0), 0)))
431 rtx mem
= XEXP (XEXP (p
, 0), 0);
433 if (!MEM_SIZE_KNOWN_P (mem
) || !MEM_SIZE (mem
).is_constant (&size
))
435 HOST_WIDE_INT off
= sp_based_mem_offset (call_insn
, mem
, fast
);
436 if (off
== INTTYPE_MINIMUM (HOST_WIDE_INT
))
438 min_sp_off
= MIN (min_sp_off
, off
);
439 max_sp_off
= MAX (max_sp_off
, off
+ size
);
442 if (min_sp_off
>= max_sp_off
)
444 sp_bytes
= BITMAP_ALLOC (NULL
);
446 /* Set bits in SP_BYTES bitmap for bytes relative to sp + min_sp_off
447 which contain arguments. Checking has been done in the previous
449 for (p
= CALL_INSN_FUNCTION_USAGE (call_insn
); p
; p
= XEXP (p
, 1))
450 if (GET_CODE (XEXP (p
, 0)) == USE
451 && MEM_P (XEXP (XEXP (p
, 0), 0)))
453 rtx mem
= XEXP (XEXP (p
, 0), 0);
454 /* Checked in the previous iteration. */
455 HOST_WIDE_INT size
= MEM_SIZE (mem
).to_constant ();
456 HOST_WIDE_INT off
= sp_based_mem_offset (call_insn
, mem
, fast
);
457 gcc_checking_assert (off
!= INTTYPE_MINIMUM (HOST_WIDE_INT
));
458 for (HOST_WIDE_INT byte
= off
; byte
< off
+ size
; byte
++)
459 if (!bitmap_set_bit (sp_bytes
, byte
- min_sp_off
))
463 /* Walk backwards, looking for argument stores. The search stops
464 when seeing another call, sp adjustment, memory store other than
465 argument store or a read from an argument stack slot. */
466 struct check_argument_load_data data
467 = { sp_bytes
, min_sp_off
, max_sp_off
, call_insn
, fast
, false };
469 for (insn
= PREV_INSN (call_insn
); insn
; insn
= prev_insn
)
471 if (insn
== BB_HEAD (BLOCK_FOR_INSN (call_insn
)))
474 prev_insn
= PREV_INSN (insn
);
479 if (!NONDEBUG_INSN_P (insn
))
482 rtx set
= single_set (insn
);
483 if (!set
|| SET_DEST (set
) == stack_pointer_rtx
)
486 note_uses (&PATTERN (insn
), check_argument_load
, &data
);
490 if (!MEM_P (SET_DEST (set
)))
493 rtx mem
= SET_DEST (set
);
494 HOST_WIDE_INT off
= sp_based_mem_offset (call_insn
, mem
, fast
);
495 if (off
== INTTYPE_MINIMUM (HOST_WIDE_INT
))
499 if (!MEM_SIZE_KNOWN_P (mem
)
500 || !MEM_SIZE (mem
).is_constant (&size
)
501 || !check_argument_store (size
, off
, min_sp_off
,
502 max_sp_off
, sp_bytes
))
505 if (!deletable_insn_p (insn
, fast
, NULL
))
509 mark_insn (insn
, fast
);
511 bitmap_set_bit (arg_stores
, INSN_UID (insn
));
513 if (bitmap_empty_p (sp_bytes
))
520 BITMAP_FREE (sp_bytes
);
521 if (!ret
&& arg_stores
)
522 bitmap_clear (arg_stores
);
528 /* Remove all REG_EQUAL and REG_EQUIV notes referring to the registers INSN
532 remove_reg_equal_equiv_notes_for_defs (rtx_insn
*insn
)
536 FOR_EACH_INSN_DEF (def
, insn
)
537 remove_reg_equal_equiv_notes_for_regno (DF_REF_REGNO (def
));
540 /* Scan all BBs for debug insns and reset those that reference values
541 defined in unmarked insns. */
544 reset_unmarked_insns_debug_uses (void)
547 rtx_insn
*insn
, *next
;
549 FOR_EACH_BB_REVERSE_FN (bb
, cfun
)
550 FOR_BB_INSNS_REVERSE_SAFE (bb
, insn
, next
)
551 if (DEBUG_INSN_P (insn
))
555 FOR_EACH_INSN_USE (use
, insn
)
557 struct df_link
*defs
;
558 for (defs
= DF_REF_CHAIN (use
); defs
; defs
= defs
->next
)
561 if (DF_REF_IS_ARTIFICIAL (defs
->ref
))
563 ref_insn
= DF_REF_INSN (defs
->ref
);
564 if (!marked_insn_p (ref_insn
))
569 /* ??? FIXME could we propagate the values assigned to
571 INSN_VAR_LOCATION_LOC (insn
) = gen_rtx_UNKNOWN_VAR_LOC ();
572 df_insn_rescan_debug_internal (insn
);
578 /* Delete every instruction that hasn't been marked. */
581 delete_unmarked_insns (void)
584 rtx_insn
*insn
, *next
;
585 bool must_clean
= false;
587 FOR_EACH_BB_REVERSE_FN (bb
, cfun
)
588 FOR_BB_INSNS_REVERSE_SAFE (bb
, insn
, next
)
589 if (NONDEBUG_INSN_P (insn
))
591 rtx turn_into_use
= NULL_RTX
;
593 /* Always delete no-op moves. */
594 if (noop_move_p (insn
)
595 /* Unless the no-op move can throw and we are not allowed
597 && (!cfun
->can_throw_non_call_exceptions
598 || (cfun
->can_delete_dead_exceptions
&& can_alter_cfg
)
599 || insn_nothrow_p (insn
)))
601 if (RTX_FRAME_RELATED_P (insn
))
603 = find_reg_note (insn
, REG_CFA_RESTORE
, NULL
);
604 if (turn_into_use
&& REG_P (XEXP (turn_into_use
, 0)))
605 turn_into_use
= XEXP (turn_into_use
, 0);
607 turn_into_use
= NULL_RTX
;
610 /* Otherwise rely only on the DCE algorithm. */
611 else if (marked_insn_p (insn
))
614 /* Beware that reaching a dbg counter limit here can result
615 in miscompiled file. This occurs when a group of insns
616 must be deleted together, typically because the kept insn
617 depends on the output from the deleted insn. Deleting
618 this insns in reverse order (both at the bb level and
619 when looking at the blocks) minimizes this, but does not
620 eliminate it, since it is possible for the using insn to
621 be top of a block and the producer to be at the bottom of
622 the block. However, in most cases this will only result
623 in an uninitialized use of an insn that is dead anyway.
625 However, there is one rare case that will cause a
626 miscompile: deletion of non-looping pure and constant
627 calls on a machine where ACCUMULATE_OUTGOING_ARGS is true.
628 In this case it is possible to remove the call, but leave
629 the argument pushes to the stack. Because of the changes
630 to the stack pointer, this will almost always lead to a
636 fprintf (dump_file
, "DCE: Deleting insn %d\n", INSN_UID (insn
));
638 /* Before we delete the insn we have to remove the REG_EQUAL notes
639 for the destination regs in order to avoid dangling notes. */
640 remove_reg_equal_equiv_notes_for_defs (insn
);
644 /* Don't remove frame related noop moves if they cary
645 REG_CFA_RESTORE note, while we don't need to emit any code,
646 we need it to emit the CFI restore note. */
648 = gen_rtx_USE (GET_MODE (turn_into_use
), turn_into_use
);
649 INSN_CODE (insn
) = -1;
650 df_insn_rescan (insn
);
653 /* Now delete the insn. */
654 must_clean
|= delete_insn_and_edges (insn
);
657 /* Deleted a pure or const call. */
660 gcc_assert (can_alter_cfg
);
661 delete_unreachable_blocks ();
662 free_dominance_info (CDI_DOMINATORS
);
667 /* Go through the instructions and mark those whose necessity is not
668 dependent on inter-instruction information. Make sure all other
669 instructions are not marked. */
672 prescan_insns_for_dce (bool fast
)
675 rtx_insn
*insn
, *prev
;
676 bitmap arg_stores
= NULL
;
679 fprintf (dump_file
, "Finding needed instructions:\n");
681 if (!df_in_progress
&& ACCUMULATE_OUTGOING_ARGS
)
682 arg_stores
= BITMAP_ALLOC (NULL
);
684 FOR_EACH_BB_FN (bb
, cfun
)
686 FOR_BB_INSNS_REVERSE_SAFE (bb
, insn
, prev
)
687 if (NONDEBUG_INSN_P (insn
))
689 /* Don't mark argument stores now. They will be marked
690 if needed when the associated CALL is marked. */
691 if (arg_stores
&& bitmap_bit_p (arg_stores
, INSN_UID (insn
)))
693 if (deletable_insn_p (insn
, fast
, arg_stores
))
694 mark_nonreg_stores (PATTERN (insn
), insn
, fast
);
696 mark_insn (insn
, fast
);
698 /* find_call_stack_args only looks at argument stores in the
701 bitmap_clear (arg_stores
);
705 BITMAP_FREE (arg_stores
);
708 fprintf (dump_file
, "Finished finding needed instructions:\n");
712 /* UD-based DSE routines. */
714 /* Mark instructions that define artificially-used registers, such as
715 the frame pointer and the stack pointer. */
718 mark_artificial_uses (void)
721 struct df_link
*defs
;
724 FOR_ALL_BB_FN (bb
, cfun
)
725 FOR_EACH_ARTIFICIAL_USE (use
, bb
->index
)
726 for (defs
= DF_REF_CHAIN (use
); defs
; defs
= defs
->next
)
727 if (!DF_REF_IS_ARTIFICIAL (defs
->ref
))
728 mark_insn (DF_REF_INSN (defs
->ref
), false);
732 /* Mark every instruction that defines a register value that INSN uses. */
735 mark_reg_dependencies (rtx_insn
*insn
)
737 struct df_link
*defs
;
740 if (DEBUG_INSN_P (insn
))
743 FOR_EACH_INSN_USE (use
, insn
)
747 fprintf (dump_file
, "Processing use of ");
748 print_simple_rtl (dump_file
, DF_REF_REG (use
));
749 fprintf (dump_file
, " in insn %d:\n", INSN_UID (insn
));
751 for (defs
= DF_REF_CHAIN (use
); defs
; defs
= defs
->next
)
752 if (! DF_REF_IS_ARTIFICIAL (defs
->ref
))
753 mark_insn (DF_REF_INSN (defs
->ref
), false);
758 /* Initialize global variables for a new DCE pass. */
767 df_set_flags (DF_RD_PRUNE_DEAD_DEFS
);
768 df_chain_add_problem (DF_UD_CHAIN
);
778 bitmap_obstack_initialize (&dce_blocks_bitmap_obstack
);
779 bitmap_obstack_initialize (&dce_tmp_bitmap_obstack
);
780 can_alter_cfg
= false;
783 can_alter_cfg
= true;
785 marked
= sbitmap_alloc (get_max_uid () + 1);
786 bitmap_clear (marked
);
790 /* Free the data allocated by init_dce. */
795 sbitmap_free (marked
);
799 bitmap_obstack_release (&dce_blocks_bitmap_obstack
);
800 bitmap_obstack_release (&dce_tmp_bitmap_obstack
);
805 /* UD-chain based DCE. */
808 rest_of_handle_ud_dce (void)
814 prescan_insns_for_dce (false);
815 mark_artificial_uses ();
816 while (worklist
.length () > 0)
818 insn
= worklist
.pop ();
819 mark_reg_dependencies (insn
);
823 if (MAY_HAVE_DEBUG_BIND_INSNS
)
824 reset_unmarked_insns_debug_uses ();
826 /* Before any insns are deleted, we must remove the chains since
827 they are not bidirectional. */
828 df_remove_problem (df_chain
);
829 delete_unmarked_insns ();
838 const pass_data pass_data_ud_rtl_dce
=
842 OPTGROUP_NONE
, /* optinfo_flags */
844 0, /* properties_required */
845 0, /* properties_provided */
846 0, /* properties_destroyed */
847 0, /* todo_flags_start */
848 TODO_df_finish
, /* todo_flags_finish */
851 class pass_ud_rtl_dce
: public rtl_opt_pass
854 pass_ud_rtl_dce (gcc::context
*ctxt
)
855 : rtl_opt_pass (pass_data_ud_rtl_dce
, ctxt
)
858 /* opt_pass methods: */
859 virtual bool gate (function
*)
861 return optimize
> 1 && flag_dce
&& dbg_cnt (dce_ud
);
864 virtual unsigned int execute (function
*)
866 return rest_of_handle_ud_dce ();
869 }; // class pass_ud_rtl_dce
874 make_pass_ud_rtl_dce (gcc::context
*ctxt
)
876 return new pass_ud_rtl_dce (ctxt
);
880 /* -------------------------------------------------------------------------
882 ------------------------------------------------------------------------- */
884 /* Process basic block BB. Return true if the live_in set has
885 changed. REDO_OUT is true if the info at the bottom of the block
886 needs to be recalculated before starting. AU is the proper set of
887 artificial uses. Track global substitution of uses of dead pseudos
888 in debug insns using GLOBAL_DEBUG. */
891 word_dce_process_block (basic_block bb
, bool redo_out
,
892 struct dead_debug_global
*global_debug
)
894 bitmap local_live
= BITMAP_ALLOC (&dce_tmp_bitmap_obstack
);
897 struct dead_debug_local debug
;
901 /* Need to redo the live_out set of this block if when one of
902 the succs of this block has had a change in it live in
906 df_confluence_function_n con_fun_n
= df_word_lr
->problem
->con_fun_n
;
907 bitmap_clear (DF_WORD_LR_OUT (bb
));
908 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
914 fprintf (dump_file
, "processing block %d live out = ", bb
->index
);
915 df_print_word_regset (dump_file
, DF_WORD_LR_OUT (bb
));
918 bitmap_copy (local_live
, DF_WORD_LR_OUT (bb
));
919 dead_debug_local_init (&debug
, NULL
, global_debug
);
921 FOR_BB_INSNS_REVERSE (bb
, insn
)
922 if (DEBUG_INSN_P (insn
))
925 FOR_EACH_INSN_USE (use
, insn
)
926 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
927 && known_eq (GET_MODE_SIZE (GET_MODE (DF_REF_REAL_REG (use
))),
929 && !bitmap_bit_p (local_live
, 2 * DF_REF_REGNO (use
))
930 && !bitmap_bit_p (local_live
, 2 * DF_REF_REGNO (use
) + 1))
931 dead_debug_add (&debug
, use
, DF_REF_REGNO (use
));
933 else if (INSN_P (insn
))
937 /* No matter if the instruction is needed or not, we remove
938 any regno in the defs from the live set. */
939 any_changed
= df_word_lr_simulate_defs (insn
, local_live
);
941 mark_insn (insn
, true);
943 /* On the other hand, we do not allow the dead uses to set
944 anything in local_live. */
945 if (marked_insn_p (insn
))
946 df_word_lr_simulate_uses (insn
, local_live
);
948 /* Insert debug temps for dead REGs used in subsequent debug
949 insns. We may have to emit a debug temp even if the insn
950 was marked, in case the debug use was after the point of
952 if (debug
.used
&& !bitmap_empty_p (debug
.used
))
956 FOR_EACH_INSN_DEF (def
, insn
)
957 dead_debug_insert_temp (&debug
, DF_REF_REGNO (def
), insn
,
959 && !control_flow_insn_p (insn
)
960 ? DEBUG_TEMP_AFTER_WITH_REG_FORCE
961 : DEBUG_TEMP_BEFORE_WITH_VALUE
);
966 fprintf (dump_file
, "finished processing insn %d live out = ",
968 df_print_word_regset (dump_file
, local_live
);
972 block_changed
= !bitmap_equal_p (local_live
, DF_WORD_LR_IN (bb
));
974 bitmap_copy (DF_WORD_LR_IN (bb
), local_live
);
976 dead_debug_local_finish (&debug
, NULL
);
977 BITMAP_FREE (local_live
);
978 return block_changed
;
982 /* Process basic block BB. Return true if the live_in set has
983 changed. REDO_OUT is true if the info at the bottom of the block
984 needs to be recalculated before starting. AU is the proper set of
985 artificial uses. Track global substitution of uses of dead pseudos
986 in debug insns using GLOBAL_DEBUG. */
989 dce_process_block (basic_block bb
, bool redo_out
, bitmap au
,
990 struct dead_debug_global
*global_debug
)
992 bitmap local_live
= BITMAP_ALLOC (&dce_tmp_bitmap_obstack
);
996 struct dead_debug_local debug
;
1000 /* Need to redo the live_out set of this block if when one of
1001 the succs of this block has had a change in it live in
1005 df_confluence_function_n con_fun_n
= df_lr
->problem
->con_fun_n
;
1006 bitmap_clear (DF_LR_OUT (bb
));
1007 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1013 fprintf (dump_file
, "processing block %d lr out = ", bb
->index
);
1014 df_print_regset (dump_file
, DF_LR_OUT (bb
));
1017 bitmap_copy (local_live
, DF_LR_OUT (bb
));
1019 df_simulate_initialize_backwards (bb
, local_live
);
1020 dead_debug_local_init (&debug
, NULL
, global_debug
);
1022 FOR_BB_INSNS_REVERSE (bb
, insn
)
1023 if (DEBUG_INSN_P (insn
))
1026 FOR_EACH_INSN_USE (use
, insn
)
1027 if (!bitmap_bit_p (local_live
, DF_REF_REGNO (use
))
1028 && !bitmap_bit_p (au
, DF_REF_REGNO (use
)))
1029 dead_debug_add (&debug
, use
, DF_REF_REGNO (use
));
1031 else if (INSN_P (insn
))
1033 bool needed
= marked_insn_p (insn
);
1035 /* The insn is needed if there is someone who uses the output. */
1037 FOR_EACH_INSN_DEF (def
, insn
)
1038 if (bitmap_bit_p (local_live
, DF_REF_REGNO (def
))
1039 || bitmap_bit_p (au
, DF_REF_REGNO (def
)))
1042 mark_insn (insn
, true);
1046 /* No matter if the instruction is needed or not, we remove
1047 any regno in the defs from the live set. */
1048 df_simulate_defs (insn
, local_live
);
1050 /* On the other hand, we do not allow the dead uses to set
1051 anything in local_live. */
1053 df_simulate_uses (insn
, local_live
);
1055 /* Insert debug temps for dead REGs used in subsequent debug
1056 insns. We may have to emit a debug temp even if the insn
1057 was marked, in case the debug use was after the point of
1059 if (debug
.used
&& !bitmap_empty_p (debug
.used
))
1060 FOR_EACH_INSN_DEF (def
, insn
)
1061 dead_debug_insert_temp (&debug
, DF_REF_REGNO (def
), insn
,
1062 needed
&& !control_flow_insn_p (insn
)
1063 ? DEBUG_TEMP_AFTER_WITH_REG_FORCE
1064 : DEBUG_TEMP_BEFORE_WITH_VALUE
);
1067 dead_debug_local_finish (&debug
, NULL
);
1068 df_simulate_finalize_backwards (bb
, local_live
);
1070 block_changed
= !bitmap_equal_p (local_live
, DF_LR_IN (bb
));
1072 bitmap_copy (DF_LR_IN (bb
), local_live
);
1074 BITMAP_FREE (local_live
);
1075 return block_changed
;
1079 /* Perform fast DCE once initialization is done. If WORD_LEVEL is
1080 true, use the word level dce, otherwise do it at the pseudo
1084 fast_dce (bool word_level
)
1086 int *postorder
= df_get_postorder (DF_BACKWARD
);
1087 int n_blocks
= df_get_n_blocks (DF_BACKWARD
);
1088 /* The set of blocks that have been seen on this iteration. */
1089 bitmap processed
= BITMAP_ALLOC (&dce_blocks_bitmap_obstack
);
1090 /* The set of blocks that need to have the out vectors reset because
1091 the in of one of their successors has changed. */
1092 bitmap redo_out
= BITMAP_ALLOC (&dce_blocks_bitmap_obstack
);
1093 bitmap all_blocks
= BITMAP_ALLOC (&dce_blocks_bitmap_obstack
);
1094 bool global_changed
= true;
1096 /* These regs are considered always live so if they end up dying
1097 because of some def, we need to bring the back again. Calling
1098 df_simulate_fixup_sets has the disadvantage of calling
1099 bb_has_eh_pred once per insn, so we cache the information
1101 bitmap au
= &df
->regular_block_artificial_uses
;
1102 bitmap au_eh
= &df
->eh_block_artificial_uses
;
1104 struct dead_debug_global global_debug
;
1106 prescan_insns_for_dce (true);
1108 for (i
= 0; i
< n_blocks
; i
++)
1109 bitmap_set_bit (all_blocks
, postorder
[i
]);
1111 dead_debug_global_init (&global_debug
, NULL
);
1113 while (global_changed
)
1115 global_changed
= false;
1117 for (i
= 0; i
< n_blocks
; i
++)
1119 int index
= postorder
[i
];
1120 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, index
);
1123 if (index
< NUM_FIXED_BLOCKS
)
1125 bitmap_set_bit (processed
, index
);
1131 = word_dce_process_block (bb
, bitmap_bit_p (redo_out
, index
),
1135 = dce_process_block (bb
, bitmap_bit_p (redo_out
, index
),
1136 bb_has_eh_pred (bb
) ? au_eh
: au
,
1138 bitmap_set_bit (processed
, index
);
1144 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1145 if (bitmap_bit_p (processed
, e
->src
->index
))
1146 /* Be tricky about when we need to iterate the
1147 analysis. We only have redo the analysis if the
1148 bitmaps change at the top of a block that is the
1150 global_changed
= true;
1152 bitmap_set_bit (redo_out
, e
->src
->index
);
1158 /* Turn off the RUN_DCE flag to prevent recursive calls to
1160 int old_flag
= df_clear_flags (DF_LR_RUN_DCE
);
1162 /* So something was deleted that requires a redo. Do it on
1164 delete_unmarked_insns ();
1165 bitmap_clear (marked
);
1166 bitmap_clear (processed
);
1167 bitmap_clear (redo_out
);
1169 /* We do not need to rescan any instructions. We only need
1170 to redo the dataflow equations for the blocks that had a
1171 change at the top of the block. Then we need to redo the
1174 df_analyze_problem (df_word_lr
, all_blocks
, postorder
, n_blocks
);
1176 df_analyze_problem (df_lr
, all_blocks
, postorder
, n_blocks
);
1178 if (old_flag
& DF_LR_RUN_DCE
)
1179 df_set_flags (DF_LR_RUN_DCE
);
1181 prescan_insns_for_dce (true);
1185 dead_debug_global_finish (&global_debug
, NULL
);
1187 delete_unmarked_insns ();
1189 BITMAP_FREE (processed
);
1190 BITMAP_FREE (redo_out
);
1191 BITMAP_FREE (all_blocks
);
1195 /* Fast register level DCE. */
1198 rest_of_handle_fast_dce (void)
1207 /* Fast byte level DCE. */
1217 timevar_push (TV_DCE
);
1218 old_flags
= df_clear_flags (DF_DEFER_INSN_RESCAN
+ DF_NO_INSN_RESCAN
);
1219 df_word_lr_add_problem ();
1223 df_set_flags (old_flags
);
1224 timevar_pop (TV_DCE
);
1228 /* This is an internal call that is used by the df live register
1229 problem to run fast dce as a side effect of creating the live
1230 information. The stack is organized so that the lr problem is run,
1231 this pass is run, which updates the live info and the df scanning
1232 info, and then returns to allow the rest of the problems to be run.
1234 This can be called by elsewhere but it will not update the bit
1235 vectors for any other problems than LR. */
1238 run_fast_df_dce (void)
1242 /* If dce is able to delete something, it has to happen
1243 immediately. Otherwise there will be problems handling the
1246 df_clear_flags (DF_DEFER_INSN_RESCAN
+ DF_NO_INSN_RESCAN
);
1248 df_in_progress
= true;
1249 rest_of_handle_fast_dce ();
1250 df_in_progress
= false;
1252 df_set_flags (old_flags
);
1257 /* Run a fast DCE pass. */
1263 rest_of_handle_fast_dce ();
1269 const pass_data pass_data_fast_rtl_dce
=
1271 RTL_PASS
, /* type */
1272 "rtl_dce", /* name */
1273 OPTGROUP_NONE
, /* optinfo_flags */
1275 0, /* properties_required */
1276 0, /* properties_provided */
1277 0, /* properties_destroyed */
1278 0, /* todo_flags_start */
1279 TODO_df_finish
, /* todo_flags_finish */
1282 class pass_fast_rtl_dce
: public rtl_opt_pass
1285 pass_fast_rtl_dce (gcc::context
*ctxt
)
1286 : rtl_opt_pass (pass_data_fast_rtl_dce
, ctxt
)
1289 /* opt_pass methods: */
1290 virtual bool gate (function
*)
1292 return optimize
> 0 && flag_dce
&& dbg_cnt (dce_fast
);
1295 virtual unsigned int execute (function
*)
1297 return rest_of_handle_fast_dce ();
1300 }; // class pass_fast_rtl_dce
1305 make_pass_fast_rtl_dce (gcc::context
*ctxt
)
1307 return new pass_fast_rtl_dce (ctxt
);