gcc/ChangeLog: * doc/invoke.texi (-fvar-tracking-assignments): New. (-fvar-tracking-assignments-toggle): New. (-fdump-final-insns=file): Mark filename as optional. (--param min-nondebug-insn-uid): New. (-gdwarf-@{version}): Mention version 4. * opts.c (common_handle_option): Accept it. * tree-vrp.c (find_assert_locations_1): Skip debug stmts. * regrename.c (regrename_optimize): Drop last. Don't count debug insns as uses. Don't reject change because of debug insn. (do_replace): Reject DEBUG_INSN as chain starter. Take base_regno from the chain starter, and check for inexact matches in DEBUG_INSNS. (scan_rtx_reg): Accept inexact matches in DEBUG_INSNs. (build_def_use): Simplify and fix the marking of DEBUG_INSNs. * sched-ebb.c (schedule_ebbs): Skip boundary debug insns. * fwprop.c (forward_propagate_and_simplify): ...into debug insns. * doc/gimple.texi (is_gimple_debug): New. (gimple_debug_bind_p): New. (is_gimple_call, gimple_assign_cast_p): End sentence with period. * doc/install.texi (bootstrap-debug): More details. (bootstrap-debug-big, bootstrap-debug-lean): Document. (bootstrap-debug-lib): More details. (bootstrap-debug-ckovw): Update. (bootstrap-time): New. * tree-into-ssa.c (mark_def_sites): Skip debug stmts. (insert_phi_nodes_for): Insert debug stmts. (rewrite_stmt): Take iterator. Insert debug stmts. (rewrite_enter_block): Adjust. (maybe_replace_use_in_debug_stmt): New. (rewrite_update_stmt): Use it. (mark_use_interesting): Return early for debug stmts. * tree-ssa-loop-im.c (rewrite_bittest): Propagate DEFs into debug stmts before replacing stmt. (move_computations_stmt): Likewise. * ira-conflicts.c (add_copies): Skip debug insns. * regstat.c (regstat_init_n_sets_and_refs): Discount debug insns. (regstat_bb_compute_ri): Skip debug insns. * tree-ssa-threadupdate.c (redirection_block_p): Skip debug stmts. * tree-ssa-loop-manip.c (find_uses_to_rename_stmt, check_loop_closed_ssa_stmt): Skip debug stmts. * tree-tailcall.c (find_tail_calls): Likewise. * tree-ssa-loop-ch.c (should_duplicate_loop_header_p): Likewise. * tree.h (MAY_HAVE_DEBUG_STMTS): New. (build_var_debug_value_stat): Declare. (build_var_debug_value): Define. (target_for_debug_bind): Declare. * reload.c (find_equiv_reg): Skip debug insns. * rtlanal.c (reg_used_between_p): Skip debug insns. (side_effects_p): Likewise. (canonicalize_condition): Likewise. * ddg.c (create_ddg_dep_from_intra_loop_link): Check that non-debug insns never depend on debug insns. (create_ddg_dep_no_link): Likewise. (add_cross_iteration_register_deps): Use ANTI_DEP for debug insns. Don't add inter-loop dependencies for debug insns. (build_intra_loop_deps): Likewise. (create_ddg): Count debug insns. * ddg.h (struct ddg::num_debug): New. (num_backargs): Pair up with previous int field. * diagnostic.c (diagnostic_report_diagnostic): Skip notes on -fcompare-debug-second. * final.c (get_attr_length_1): Skip debug insns. (rest_of_clean-state): Don't dump CFA_RESTORE_STATE. * gcc.c (invoke_as): Call compare-debug-dump-opt. (driver_self_specs): Map -fdump-final-insns to -fdump-final-insns=.. (get_local_tick): New. (compare_debug_dump_opt_spec_function): Test for . argument and compute output name. Compute temp output spec without flag name. Compute -frandom-seed. (OPT): Undef after use. * cfgloopanal.c (num_loop_insns): Skip debug insns. (average_num_loop_insns): Likewise. * params.h (MIN_NONDEBUG_INSN_UID): New. * gimple.def (GIMPLE_DEBUG): New. * ipa-reference.c (scan_stmt_for_static_refs): Skip debug stmts. * auto-inc-dec.c (merge_in_block): Skip debug insns. (merge_in_block): Fix whitespace. * toplev.c (flag_var_tracking): Update comment. (flag_var_tracking_assignments): New. (flag_var_tracking_assignments_toggle): New. (process_options): Don't open final insns dump file if we're not going to write to it. Compute defaults for var_tracking. * df-scan.c (df_insn_rescan_debug_internal): New. (df_uses_record): Handle debug insns. * haifa-sched.c (ready): Initialize n_debug. (contributes_to_priority): Skip debug insns. (dep_list_size): New. (priority): Use it. (rank_for_schedule): Likewise. Schedule debug insns as soon as they're ready. Disregard previous debug insns to make decisions. (queue_insn): Never queue debug insns. (ready_add, ready_remove_first, ready_remove): Count debug insns. (schedule_insn): Don't reject debug insns because of issue rate. (get_ebb_head_tail, no_real_insns_p): Skip boundary debug insns. (queue_to_ready): Skip and discount debug insns. (choose_ready): Let debug insns through. (schedule_block): Check boundary debug insns. Discount debug insns, schedule them early. Adjust whitespace. (set_priorities): Check for boundary debug insns. (add_jump_dependencies): Use dep_list_size. (prev_non_location_insn): New. (check_cfg): Use it. * tree-ssa-loop-ivopts.c (find-interesting_users): Skip debug stmts. (remove_unused_ivs): Reset debug stmts. * modulo-sched.c (const_iteration_count): Skip debug insns. (res_MII): Discount debug insns. (loop_single_full_bb_p): Skip debug insns. (sms_schedule): Likewise. (sms_schedule_by_order): Likewise. (ps_has_conflicts): Likewise. * caller-save.c (refmarker_fn): New. (save_call_clobbered_regs): Replace regs with saved mem in debug insns. (mark_referenced_regs): Take pointer, mark and arg. Adjust. Call refmarker_fn mark for hardregnos. (mark_reg_as_referenced): New. (replace_reg_with_saved_mem): New. * ipa-pure-const.c (check_stmt): Skip debug stmts. * cse.c (cse_insn): Canonicalize debug insns. Skip them when searching back. (cse_extended_basic_block): Skip debug insns. (count_reg_usage): Likewise. (is_dead_reg): New, split out of... (set_live_p): ... here. (insn_live_p): Use it for debug insns. * tree-stdarg.c (check_all_va_list_escapes): Skip debug stmts. (execute_optimize_stdarg): Likewise. * tree-ssa-dom.c (propagate_rhs_into_lhs): Likewise. * tree-ssa-propagate.c (substitute_and_fold): Don't regard changes in debug stmts as changes. * sel-sched.c (moving_insn_creates_bookkeeping_block_p): New. (moveup_expr): Don't move across debug insns. Don't move debug insn if it would create a bookkeeping block. (moveup_expr_cached): Don't use cache for debug insns that are heads of blocks. (compute_av_set_inside_bb): Skip debug insns. (sel_rank_for_schedule): Schedule debug insns first. Remove dead code. (block_valid_for_bookkeeping_p); Support lax searches. (create_block_for_bookkeeping): Adjust block numbers when encountering debug-only blocks. (find_place_for_bookkeeping): Deal with debug-only blocks. (generate_bookkeeping_insn): Accept no place to insert. (remove_temp_moveop_nops): New argument full_tidying. (prepare_place_to_insert): Deal with debug insns. (advance_state_on_fence): Debug insns don't start cycles. (update_boundaries): Take fence as argument. Deal with debug insns. (schedule_expr_on_boundary): No full_tidying on debug insns. (fill_insns): Deal with debug insns. (track_scheduled_insns_and_blocks): Don't count debug insns. (need_nop_to_preserve_insn_bb): New, split out of... (remove_insn_from_stream): ... this. (fur_orig_expr_not_found): Skip debug insns. * rtl.def (VALUE): Move up. (DEBUG_INSN): New. * tree-ssa-sink.c (all_immediate_uses_same_place): Skip debug stmts. (nearest_common_dominator_of_uses): Take debug_stmts argument. Set it if debug stmts are found. (statement_sink_location): Skip debug stmts. Propagate moving defs into debug stmts. * ifcvt.c (first_active_insn): Skip debug insns. (last_active_insns): Likewise. (cond_exec_process_insns): Likewise. (noce_process_if_block): Likewise. (check_cond_move_block): Likewise. (cond_move_convert_if_block): Likewise. (block_jumps_and_fallthru_p): Likewise. (dead_or_predicable): Likewise. * dwarf2out.c (debug_str_hash_forced): New. (find_AT_string): Add comment. (gen_label_for_indirect_string): New. (get_debug_string_label): New. (AT_string_form): Use it. (mem_loc_descriptor): Handle non-TLS symbols. Handle MINUS , DIV, MOD, AND, IOR, XOR, NOT, ABS, NEG, and CONST_STRING. Accept but discard COMPARE, IF_THEN_ELSE, ROTATE, ROTATERT, TRUNCATE and several operations that cannot be represented with DWARF opcodes. (loc_descriptor): Ignore SIGN_EXTEND and ZERO_EXTEND. Require dwarf_version 4 for DW_OP_implicit_value and DW_OP_stack_value. (dwarf2out_var_location): Take during-call mark into account. (output_indirect_string): Update comment. Output if there are label and references. (prune_indirect_string): New. (prune_unused_types): Call it if debug_str_hash_forced. More in dwarf2out.c, from Jakub Jelinek <jakub@redhat.com>: (dw_long_long_const): Remove. (struct dw_val_struct): Change val_long_long type to rtx. (print_die, attr_checksum, same_dw_val_p, loc_descriptor): Adjust for val_long_long change to CONST_DOUBLE rtx from a long hi/lo pair. (output_die): Likewise. Use HOST_BITS_PER_WIDE_INT size of each component instead of HOST_BITS_PER_LONG. (output_loc_operands): Likewise. For const8* assert HOST_BITS_PER_WIDE_INT rather than HOST_BITS_PER_LONG is >= 64. (output_loc_operands_raw): For const8* assert HOST_BITS_PER_WIDE_INT rather than HOST_BITS_PER_LONG is >= 64. (add_AT_long_long): Remove val_hi and val_lo arguments, add val_const_double. (size_of_die): Use HOST_BITS_PER_WIDE_INT size multiplier instead of HOST_BITS_PER_LONG for dw_val_class_long_long. (add_const_value_attribute): Adjust add_AT_long_long caller. Don't handle TLS SYMBOL_REFs. If CONST wraps a constant, tail recurse. (dwarf_stack_op_name): Handle DW_OP_implicit_value and DW_OP_stack_value. (size_of_loc_descr, output_loc_operands, output_loc_operands_raw): Handle DW_OP_implicit_value. (extract_int): Move prototype earlier. (mem_loc_descriptor): For SUBREG punt if inner mode size is wider than DWARF2_ADDR_SIZE. Handle SIGN_EXTEND and ZERO_EXTEND by DW_OP_shl and DW_OP_shr{a,}. Handle EQ, NE, GT, GE, LT, LE, GTU, GEU, LTU, LEU, SMIN, SMAX, UMIN, UMAX, SIGN_EXTRACT, ZERO_EXTRACT. (loc_descriptor): Compare mode size with DWARF2_ADDR_SIZE instead of Pmode size. (loc_descriptor): Add MODE argument. Handle CONST_INT, CONST_DOUBLE, CONST_VECTOR, CONST, LABEL_REF and SYMBOL_REF if mode != VOIDmode, attempt to handle other expressions. Don't handle TLS SYMBOL_REFs. (concat_loc_descriptor, concatn_loc_descriptor, loc_descriptor_from_tree_1): Adjust loc_descriptor callers. (add_location_or_const_value_attribute): Likewise. For single location loc_lists attempt to use add_const_value_attribute for constant decls. Add DW_AT_const_value even if NOTE_VAR_LOCATION is VAR_LOCATION with CONSTANT_P or CONST_STRING in its expression. * cfgbuild.c (inside_basic_block_p): Handle debug insns. (control_flow_insn_p): Likewise. * tree-parloops.c (eliminate_local_variables_stmt): Handle debug stmt. (separate_decls_in_region_debug_bind): New. (separate_decls_in_region): Process debug bind stmts afterwards. * recog.c (verify_changes): Handle debug insns. (extract_insn): Likewise. (peephole2_optimize): Skip debug insns. * dse.c (scan_insn): Skip debug insns. * sel-sched-ir.c (return_nop_to_pool): Take full_tidying argument. Pass it on. (setup_id_for_insn): Handle debug insns. (maybe_tidy_empty_bb): Adjust whitespace. (tidy_control_flow): Skip debug insns. (sel_remove_insn): Adjust for debug insns. (sel_estimate_number_of_insns): Skip debug insns. (create_insn_rtx_from_pattern): Handle debug insns. (create_copy_of_insn_rtx): Likewise. * sel-sched-.h (sel_bb_end): Declare. (sel_bb_empty_or_nop_p): New. (get_all_loop_exits): Use it. (_eligible_successor_edge_p): Likewise. (return_nop_to_pool): Adjust. * tree-eh.c (tre_empty_eh_handler_p): Skip debug stmts. * ira-lives.c (process_bb_node_lives): Skip debug insns. * gimple-pretty-print.c (dump_gimple_debug): New. (dump_gimple_stmt): Use it. (dump_bb_header): Skip gimple debug stmts. * regmove.c (optimize_reg_copy_1): Discount debug insns. (fixup_match_2): Likewise. (regmove_backward_pass): Likewise. Simplify combined replacement. Handle debug insns. * function.c (instantiate_virtual_regs): Handle debug insns. * function.h (struct emit_status): Add x_cur_debug_insn_uid. * print-rtl.h: Include cselib.h. (print_rtx): Print VALUEs. Split out and recurse for VAR_LOCATIONs. * df.h (df_inns_rescan_debug_internal): Declare. * gcse.c (alloc_hash_table): Estimate n_insns. (cprop_insn): Don't regard debug insns as changes. (bypass_conditional_jumps): Skip debug insns. (one_pre_gcse_pass): Adjust. (one_code_hoisting_pass): Likewise. (compute_ld_motion_mems): Skip debug insns. (one_cprop_pass): Adjust. * tree-if-conv.c (tree_if_convert_stmt): Reset debug stmts. (if_convertible_stmt_p): Handle debug stmts. * init-regs.c (initialize_uninitialized_regs): Skip debug insns. * tree-vect-loop.c (vect_is_simple_reduction): Skip debug stmts. * ira-build.c (create_bb_allocnos): Skip debug insns. * tree-flow-inline.h (has_zero_uses): Discount debug stmts. (has_single_use): Likewise. (single_imm_use): Likewise. (num_imm_uses): Likewise. * tree-ssa-phiopt.c (empty_block_p): Skip debug stmts. * tree-ssa-coalesce.c (build_ssa_conflict_graph): Skip debug stmts. (create_outofssa_var_map): Likewise. * lower-subreg.c (adjust_decomposed_uses): New. (resolve_debug): New. (decompose_multiword_subregs): Use it. * tree-dfa.c (find_referenced_vars): Skip debug stmts. * emit-rtl.c: Include params.h. (cur_debug_insn_uid): Define. (set_new_first_and_last_insn): Set cur_debug_insn_uid too. (copy_rtx_if_shared_1): Handle debug insns. (reset_used_flags): Likewise. (set_used_flags): LIkewise. (get_max_insn_count): New. (next_nondebug_insn): New. (prev_nondebug_insn): New. (make_debug_insn_raw): New. (emit_insn_before_noloc): Handle debug insns. (emit_jump_insn_before_noloc): Likewise. (emit_call_insn_before_noloc): Likewise. (emit_debug_insn_before_noloc): New. (emit_insn_after_noloc): Handle debug insns. (emit_jump_insn_after_noloc): Likewise. (emit_call_insn_after_noloc): Likewise. (emit_debug_insn_after_noloc): Likewise. (emit_insn_after): Take loc from earlier non-debug insn. (emit_jump_insn_after): Likewise. (emit_call_insn_after): Likewise. (emit_debug_insn_after_setloc): New. (emit_debug_insn_after): New. (emit_insn_before): Take loc from later non-debug insn. (emit_jump_insn_before): Likewise. (emit_call_insn_before): Likewise. (emit_debug_insn_before_setloc): New. (emit_debug_insn_before): New. (emit_insn): Handle debug insns. (emit_debug_insn): New. (emit_jump_insn): Handle debug insns. (emit_call_insn): Likewise. (emit): Likewise. (init_emit): Take min-nondebug-insn-uid into account. Initialize cur_debug_insn_uid. (emit_copy_of_insn_after): Handle debug insns. * cfgexpand.c (gimple_assign_rhs_to_tree): Do not overwrite location of single rhs in place. (maybe_dump_rtl_for_gimple_stmt): Dump lineno. (floor_sdiv_adjust): New. (cell_sdiv_adjust): New. (cell_udiv_adjust): New. (round_sdiv_adjust): New. (round_udiv_adjust): New. (wrap_constant): Moved from cselib. (unwrap_constant): New. (expand_debug_expr): New. (expand_debug_locations): New. (expand_gimple_basic_block): Drop hiding redeclaration. Expand debug bind stmts. (gimple_expand_cfg): Expand debug locations. * cselib.c: Include tree-pass.h. (struct expand_value_data): New. (cselib_record_sets_hook): New. (PRESERVED_VALUE_P, LONG_TERM_PRESERVED_VALUE_P): New. (cselib_clear_table): Move, and implemnet in terms of... (cselib_reset_table_with_next_value): ... this. (cselib_get_next_unknown_value): New. (discard_useless_locs): Don't discard preserved values. (cselib_preserve_value): New. (cselib_preserved_value_p): New. (cselib_preserve_definitely): New. (cselib_clear_preserve): New. (cselib_preserve_only_values): New. (new_cselib_val): Take rtx argument. Dump it in details. (cselib_lookup_mem): Adjust. (expand_loc): Take regs_active in struct. Adjust. Silence dumps unless details are requested. (cselib_expand_value_rtx_cb): New. (cselib_expand_value_rtx): Rename and reimplment in terms of... (cselib_expand_value_rtx_1): ... this. Adjust. Silence dumps without details. Copy more subregs. Try to resolve values using a callback. Wrap constants. (cselib_subst_to_values): Adjust. (cselib_log_lookup): New. (cselib_lookup): Call it. (cselib_invalidate_regno): Don't count preserved values as useless. (cselib_invalidate_mem): Likewise. (cselib_record_set): Likewise. (struct set): Renamed to cselib_set, moved to cselib.h. (cselib_record_sets): Adjust. Call hook. (cselib_process_insn): Reset table when it would be cleared. (dump_cselib_val): New. (dump_cselib_table): New. * tree-cfgcleanup.c (tree_forwarded_block_p): Skip debug stmts. (remove_forwarder_block): Support moving debug stmts. * cselib.h (cselib_record_sets_hook): Declare. (cselib_expand_callback): New type. (cselib_expand_value_rtx_cb): Declare. (cselib_reset_table_with_next_value): Declare. (cselib_get_next_unknown_value): Declare. (cselib_preserve_value): Declare. (cselib_preserved_value_p): Declare. (cselib_preserve_only_values): Declare. (dump_cselib_table): Declare. * cfgcleanup.c (flow_find_cross_jump): Skip debug insns. (try_crossjump_to_edge): Likewise. (delete_unreachable_blocks): Remove dominant GIMPLE blocks after dominated blocks when debug stmts are present. * simplify-rtx.c (delegitimize_mem_from_attrs): New. * tree-ssa-live.c (remove_unused_locals): Skip debug stmts. (set_var_live_on_entry): Likewise. * loop-invariant.c (find_invariants_bb): Skip debug insns. * cfglayout.c (curr_location, last_location): Make static. (set_curr_insn_source_location): Don't avoid bouncing. (get_curr_insn_source_location): New. (get_curr_insn_block): New. (duplicate_insn_chain): Handle debug insns. * tree-ssa-forwprop.c (forward_propagate_addr_expr): Propagate into debug stmts. * common.opt (fcompare-debug): Move to sort order. (fdump-unnumbered-links): Likewise. (fvar-tracking-assignments): New. (fvar-tracking-assignments-toggle): New. * tree-ssa-dce.c (mark_stmt_necessary): Don't mark blocks because of debug stmts. (mark_stmt_if_obviously_necessary): Mark debug stmts. (eliminate_unnecessary_stmts): Walk dominated blocks before dominators. * tree-ssa-ter.c (find_replaceable_in_bb): Skip debug stmts. * ira.c (memref_used_between_p): Skip debug insns. (update_equiv_regs): Likewise. * sched-deps.c (sd_lists_size): Accept empty list. (sd_init_insn): Mark debug insns. (sd_finish_insn): Unmark them. (sd_add_dep): Reject non-debug deps on debug insns. (fixup_sched_groups): Give debug insns group treatment. Skip debug insns. (sched_analyze_reg): Don't mark debug insns for sched before call. (sched_analyze_2): Handle debug insns. (sched_analyze_insn): Compute next non-debug insn. Handle debug insns. (deps_analyze_insn): Handle debug insns. (deps_start_bb): Skip debug insns. (init_deps): Initialize last_debug_insn. * tree-ssa.c (target_for_debug_bind): New. (find_released_ssa_name): New. (propagate_var_def_into_debug_stmts): New. (propagate_defs_into_debug_stmts): New. (verify_ssa): Skip debug bind stmts without values. (warn_uninialized_vars): Skip debug stmts. * target-def.h (TARGET_DELEGITIMIZE_ADDRESS): Set default. * rtl.c (rtx_equal_p_cb): Handle VALUEs. (rtx_equal_p): Likewise. * ira-costs.c (scan_one_insn): Skip debug insns. (process_bb_node_for_hard_reg_moves): Likewise. * rtl.h (DEBUG_INSN_P): New. (NONDEBUG_INSN_P): New. (MAY_HAVE_DEBUG_INSNS): New. (INSN_P): Accept debug insns. (RTX_FRAME_RELATED_P): Likewise. (INSN_DELETED_P): Likewise (PAT_VAR_LOCATION_DECL): New. (PAT_VAR_LOCATION_LOC): New. (PAT_VAR_OCATION_STATUS): New. (NOTE_VAR_LOCATION_DECL): Reimplement. (NOTE_VAR_LOCATION_LOC): Likewise. (NOTE_VAR_LOCATION_STATUS): Likewise. (INSN_VAR_LOCATION): New. (INSN_VAR_LOCATION_DECL): New. (INSN_VAR_LOCATION_LOC): New. (INSN_VAR_LOCATION_STATUS): New. (gen_rtx_UNKNOWN_VAR_LOC): New. (VAR_LOC_UNKNOWN_P): New. (NOTE_DURING_CALL_P): New. (SCHED_GROUP_P): Accept debug insns. (emit_debug_insn_before): Declare. (emit_debug_insn_before_noloc): Declare. (emit_debug_insn_beore_setloc): Declare. (emit_debug_insn_after): Declare. (emit_debug_insn_after_noloc): Declare. (emit_debug_insn_after_setloc): Declare. (emit_debug_insn): Declare. (make_debug_insn_raw): Declare. (prev_nondebug_insn): Declare. (next_nondebug_insn): Declare. (delegitimize_mem_from_attrs): Declare. (get_max_insn_count): Declare. (wrap_constant): Declare. (unwrap_constant): Declare. (get_curr_insn_source_location): Declare. (get_curr_insn_block): Declare. * tree-inline.c (insert_debug_decl_map): New. (processing_debug_stmt): New. (remap_decl): Don't create new mappings in debug stmts. (remap_gimple_op_r): Don't add references in debug stmts. (copy_tree_body_r): Likewise. (remap_gimple_stmt): Handle debug bind stmts. (copy_bb): Skip debug stmts. (copy_edges_for_bb): Likewise. (copy_debug_stmt): New. (copy_debug_stmts): New. (copy_body): Copy debug stmts at the end. (insert_init_debug_bind): New. (insert_init_stmt): Take id. Skip and emit debug stmts. (setup_one_parameter): Remap variable earlier, register debug mapping. (estimate_num_insns): Skip debug stmts. (expand_call_inline): Preserve debug_map. (optimize_inline_calls): Check for no debug_stmts left-overs. (unsave_expr_now): Preserve debug_map. (copy_gimple_seq_and_replace_locals): Likewise. (tree_function_versioning): Check for no debug_stmts left-overs. Init and destroy debug_map as needed. Split edges unconditionally. (build_duplicate_type): Init and destroy debug_map as needed. * tree-inline.h: Include gimple.h instead of pointer-set.h. (struct copy_body_data): Add debug_stmts and debug_map. * sched-int.h (struct ready_list): Add n_debug. (struct deps): Add last_debug_insn. (DEBUG_INSN_SCHED_P): New. (BOUNDARY_DEBUG_INSN_P): New. (SCHEDULE_DEBUG_INSN_P): New. (sd_iterator_cond): Accept empty list. * combine.c (create_log_links): Skip debug insns. (combine_instructions): Likewise. (cleanup_auto_inc_dec): New. From Jakub Jelinek: Make sure the return value is always unshared. (struct rtx_subst_pair): New. (auto_adjust_pair): New. (propagate_for_debug_subst): New. (propagate_for_debug): New. (try_combine): Skip debug insns. Propagate removed defs into debug insns. (next_nonnote_nondebug_insn): New. (distribute_notes): Use it. Skip debug insns. (distribute_links): Skip debug insns. * tree-outof-ssa.c (set_location_for_edge): Likewise. * resource.c (mark_target_live_regs): Likewise. * var-tracking.c: Include cselib.h and target.h. (enum micro_operation_type): Add MO_VAL_USE, MO_VAL_LOC, and MO_VAL_SET. (micro_operation_type_name): New. (enum emit_note_where): Add EMIT_NOTE_AFTER_CALL_INSN. (struct micro_operation_def): Update comments. (decl_or_value): New type. Use instead of decls. (struct emit_note_data_def): Add vars. (struct attrs_def): Use decl_or_value. (struct variable_tracking_info_def): Add permp, flooded. (struct location_chain_def): Update comment. (struct variable_part_def): Use decl_or_value. (struct variable_def): Make var_part a variable length array. (valvar_pool): New. (scratch_regs): New. (cselib_hook_called): New. (dv_is_decl_p): New. (dv_is_value_p): New. (dv_as_decl): New. (dv_as_value): New. (dv_as_opaque): New. (dv_onepart_p): New. (dv_pool): New. (IS_DECL_CODE): New. (check_value_is_not_decl): New. (dv_from_decl): New. (dv_from_value): New. (dv_htab_hash): New. (variable_htab_hash): Use it. (variable_htab_eq): Support values. (variable_htab_free): Free from the right pool. (attrs_list_member, attrs_list_insert): Use decl_or_value. (attrs_list_union): Adjust. (attrs_list_mpdv_union): New. (tie_break_pointers): New. (canon_value_cmp): New. (unshare_variable): Return possibly-modified slot. (vars_copy_1): Adjust. (var_reg_decl_set): Adjust. Split out of... (var_reg_set): ... this. (get_init_value): Adjust. (var_reg_delete_and_set): Adjust. (var_reg_delete): Adjust. (var_regno_delete): Adjust. (var_mem_decl_set): Split out of... (var_mem_set): ... this. (var_mem_delete_and_set): Adjust. (var_mem_delete): Adjust. (val_store): New. (val_reset): New. (val_resolve): New. (variable_union): Adjust. Speed up merge of 1-part vars. (variable_canonicalize): Use unshared slot. (VALUED_RECURSED_INTO): New. (find_loc_in_1pdv): New. (struct dfset_merge): New. (insert_into_intersection): New. (intersect_loc_chains): New. (loc_cmp): New. (canonicalize_loc_order_check): New. (canonicalize_values_mark): New. (canonicalize_values_star): New. (variable_merge_over_cur): New. (variable_merge_over_src): New. (dataflow_set_merge): New. (dataflow_set_equiv_regs): New. (remove_duplicate_values): New. (struct dfset_post_merge): New. (variable_post_merge_new_vals): New. (variable_post_merge_perm_vals): New. (dataflow_post_merge_adjust): New. (find_mem_expr_in_1pdv): New. (dataflow_set_preserve_mem_locs): New. (dataflow_set_remove_mem_locs): New. (dataflow_set_clear_at_call): New. (onepart_variable_different_p): New. (variable_different_p): Use it. (dataflow_set_different_1): Adjust. Make detailed dump more verbose. (track_expr_p): Add need_rtl parameter. Don't generate rtl if not needed. (track_loc_p): Pass it true. (struct count_use_info): New. (find_use_val): New. (replace_expr_with_values): New. (log_op_type): New. (use_type): New, partially split out of... (count_uses): ... this. Count new micro-ops. (count_uses_1): Adjust. (count_stores): Adjust. (count_with_sets): New. (VAL_NEEDS_RESOLUTION): New. (VAL_HOLDS_TRACK_EXPR): New. (VAL_EXPR_IS_COPIED): New. (VAL_EXPR_IS_CLOBBERED): New. (add_uses): Adjust. Generate new micro-ops. (add_uses_1): Adjust. (add_stores): Generate new micro-ops. (add_with_sets): New. (find_src_status): Adjust. (find_src_set_src): Adjust. (compute_bb_dataflow): Use dataflow_set_clear_at_call. Handle new micro-ops. Canonicalize value equivalances. (vt_find_locations): Compute total size of hash tables for dumping. Perform merge for var-tracking-assignments. Don't disregard single-block loops. (dump_attrs_list): Handle decl_or_value. (dump_variable): Take variable. Deal with decl_or_value. (dump_variable_slot): New. (dump_vars): Use it. (dump_dataflow_sets): Adjust. (set_slot_part): New, extended to support one-part variables after splitting out of... (set_variable_part): ... this. (clobber_slot_part): New, split out of... (clobber_variable_part): ... this. (delete_slot_part): New, split out of... (delete_variable_part): .... this. (check_wrap_constant): New. (vt_expand_loc_callback): New. (vt_expand_loc): New. (emit_note_insn_var_location): Adjust. Handle values. Handle EMIT_NOTE_AFTER_CALL_INSN. (emit_notes_for_differences_1): Adjust. Handle values. (emit_notes_for_differences_2): Likewise. (emit_notes_for_differences): Adjust. (emit_notes_in_bb): Take pointer to set. Emit AFTER_CALL_INSN notes. Adjust. Handle new micro-ops. (vt_add_function_parameters): Adjust. Create and bind values. (vt_initialize): Adjust. Initialize scratch_regs and valvar_pool, flooded and perm.. Initialize and use cselib. Log operations. Move some code to count_with_sets and add_with_sets. (delete_debug_insns): New. (vt_debug_insns_local): New. (vt_finalize): Release permp, valvar_pool, scratch_regs. Finish cselib. (var_tracking_main): If var-tracking-assignments is enabled but var-tracking isn't, delete debug insns and leave. Likewise if we exceed limits or fail the stack adjustments tests, and after all var-tracking processing. More in var-tracking, from Jakub Jelinek <jakub@redhat.com>: (dataflow_set): Add traversed_vars. (value_chain, const_value_chain): New typedefs. (value_chain_pool, value_chains): New variables. (value_chain_htab_hash, value_chain_htab_eq, add_value_chain, add_value_chains, add_cselib_value_chains, remove_value_chain, remove_value_chains, remove_cselib_value_chains): New functions. (shared_hash_find_slot_unshare_1, shared_hash_find_slot_1, shared_hash_find_slot_noinsert_1, shared_hash_find_1): New static inlines. (shared_hash_find_slot_unshare, shared_hash_find_slot, shared_hash_find_slot_noinsert, shared_hash_find): Update. (dst_can_be_shared): New variable. (unshare_variable): Unshare set->vars if shared, use shared_hash_*. Clear dst_can_be_shared. If set->traversed_vars is non-NULL and different from set->vars, look up slot again instead of using the passed in slot. (dataflow_set_init): Initialize traversed_vars. (variable_union): Use shared_hash_*. Use initially NO_INSERT lookup if set->vars is shared. Don't keep slot cleared before calling unshare_variable. Unshare set->vars if needed. Adjust unshare_variable callers. Clear dst_can_be_shared if needed. Even ->refcount == 1 vars must be unshared if set->vars is shared and var needs to be modified. (dataflow_set_union): Set traversed_vars during canonicalization. (VALUE_CHANGED, DECL_CHANGED): Define. (set_dv_changed, dv_changed_p): New static inlines. (track_expr_p): Clear DECL_CHANGED. (dump_dataflow_sets): Set it. (variable_was_changed): Call set_dv_changed. (emit_note_insn_var_location): Likewise. (changed_variables_stack): New variable. (check_changed_vars_1, check_changed_vars_2): New functions. (emit_notes_for_changes): Do nothing if changed_variables is empty. Traverse changed_variables with check_changed_vars_1, call check_changed_vars_2 on each changed_variables_stack entry. (emit_notes_in_bb): Add SET argument. Just clear it at the beginning, use it instead of local &set, don't destroy it at the end. (vt_emit_notes): Call dataflow_set_clear early on all VTI(bb)->out sets, never use them, instead use emit_notes_in_bb computed set, dataflow_set_clear also VTI(bb)->in when we are done with the basic block. Initialize changed_variables_stack, free it afterwards. If ENABLE_CHECKING verify that after noting differences to an empty set value_chains hash table is empty. (vt_initialize): Initialize value_chains and value_chain_pool. (vt_finalize): Delete value_chains htab, free value_chain_pool. (variable_tracking_main): Call dump_dataflow_sets before calling vt_emit_notes, not after it. * tree-flow.h (propagate_defs_into_debug_stmts): Declare. (propagate_var_def_into_debug_stmts): Declare. * df-problems.c (df_lr_bb_local_compute): Skip debug insns. (df_set_note): Reject debug insns. (df_whole_mw_reg_dead_p): Take added_notes_p argument. Don't add notes to debug insns. (df_note_bb_compute): Adjust. Likewise. (df_simulate_uses): Skip debug insns. (df_simulate_initialize_backwards): Likewise. * reg-stack.c (subst_stack_regs_in_debug_insn): New. (subst_stack_regs_pat): Reject debug insns. (convert_regs_1): Handle debug insns. * Makefile.in (TREE_INLINE_H): Take pointer-set.h from GIMPLE_H. (print-rtl.o): Depend on cselib.h. (cselib.o): Depend on TREE_PASS_H. (var-tracking.o): Depend on cselib.h and TARGET_H. * sched-rgn.c (rgn_estimate_number_of_insns): Discount debug insns. (init_ready_list): Skip boundary debug insns. (add_branch_dependences): Skip debug insns. (free_block_dependencies): Check for blocks with only debug insns. (compute_priorities): Likewise. * gimple.c (gss_for_code): Handle GIMPLE_DEBUG. (gimple_build_with_ops_stat): Take subcode as unsigned. Adjust all callers. (gimple_build_debug_bind_stat): New. (empty_body_p): Skip debug stmts. (gimple_has_side_effects): Likewise. (gimple_rhs_has_side_effects): Likewise. * gimple.h (enum gimple_debug_subcode, GIMPLE_DEBUG_BIND): New. (gimple_build_debug_bind_stat): Declare. (gimple_build_debug_bind): Define. (is_gimple_debug): New. (gimple_debug_bind_p): New. (gimple_debug_bind_get_var): New. (gimple_debug_bind_get_value): New. (gimple_debug_bind_get_value_ptr): New. (gimple_debug_bind_set_var): New. (gimple_debug_bind_set_value): New. (GIMPLE_DEBUG_BIND_NOVALUE): New internal temporary macro. (gimple_debug_bind_reset_value): New. (gimple_debug_bind_has_value_p): New. (gsi_next_nondebug): New. (gsi_prev_nondebug): New. (gsi_start_nondebug_bb): New. (gsi_last_nondebug_bb): New. * sched-vis.c (print_pattern): Handle VAR_LOCATION. (print_insn): Handle DEBUG_INSN. * tree-cfg.c (remove_bb): Walk stmts backwards. Let loc of first insn prevail. (first_stmt): Skip debug stmts. (first_non_label_stmt): Likewise. (last_stmt): Likewise. (has_zero_uses_1): New. (single_imm_use_1): New. (verify_gimple_debug): New. (verify_types_in_gimple_stmt): Handle debug stmts. (verify_stmt): Likewise. (debug_loop_num): Skip debug stmts. (remove_edge_and_dominated_blocks): Remove dominators last. * tree-ssa-reasssoc.c (rewrite_expr_tree): Propagate into debug stmts. (linearize_expr): Likewise. * config/i386/i386.c (ix86_delegitimize_address): Call default implementation. * config/ia64/ia64.c (ia64_safe_itanium_class): Handle debug insns. (group_barrier_needed): Skip debug insns. (emit_insn_group_barriers): Likewise. (emit_all_insn_group_barriers): Likewise. (ia64_variable_issue): Handle debug insns. (ia64_dfa_new_cycle): Likewise. (final_emit_insn_group_barriers): Skip debug insns. (ia64_dwarf2out_def_steady_cfa): Take frame argument. Don't def cfa without frame. (process_set): Likewise. (process_for_unwind_directive): Pass frame on. * config/rs6000/rs6000.c (TARGET_DELEGITIMIZE_ADDRESS): Define. (rs6000_delegitimize_address): New. (rs6000_debug_adjust_cost): Handle debug insns. (is_microcoded_insn): Likewise. (is_cracked_insn): Likewise. (is_nonpipeline_insn): Likewise. (insn_must_be_first_in_group): Likewise. (insn_must_be_last_in_group): Likewise. (force_new_group): Likewise. * cfgrtl.c (rtl_split_block): Emit INSN_DELETED note if block contains only debug insns. (rtl_merge_blocks): Skip debug insns. (purge_dead_edges): Likewise. (rtl_block_ends_with_call_p): Skip debug insns. * dce.c (deletable_insn_p): Handle VAR_LOCATION. (mark_reg_dependencies): Skip debug insns. * params.def (PARAM_MIN_NONDEBUG_INSN_UID): New. * tree-ssanames.c (release_ssa_name): Propagate def into debug stmts. * tree-ssa-threadedge.c (record_temporary_equivalences_from_stmts): Skip debug stmts. * regcprop.c (replace_oldest_value_addr): Skip debug insns. (replace_oldest_value_mem): Use ALL_REGS for debug insns. (copyprop_hardreg_forward_1): Handle debug insns. * reload1.c (reload): Skip debug insns. Replace unassigned pseudos in debug insns with their equivalences. (eliminate_regs_in_insn): Skip debug insns. (emit_input_reload_insns): Skip debug insns at first, adjust them later. * tree-ssa-operands.c (add_virtual_operand): Reject debug stmts. (get_indirect_ref_operands): Pass opf_no_vops on. (get_expr_operands): Likewise. Skip debug stmts. (parse_ssa_operands): Scan debug insns with opf_no_vops. gcc/testsuite/ChangeLog: * gcc.dg/guality/guality.c: New. * gcc.dg/guality/guality.h: New. * gcc.dg/guality/guality.exp: New. * gcc.dg/guality/example.c: New. * lib/gcc-dg.exp (cleanup-dump): Remove .gk files. (cleanup-saved-temps): Likewise, .gkd files too. gcc/cp/ChangeLog: * cp-tree.h (TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS): New. * cp-lang.c (cxx_dwarf_name): Pass it. * error.c (count_non_default_template_args): Take flags as argument. Adjust all callers. Skip counting of default arguments if the new flag is given. ChangeLog: * Makefile.tpl (BUILD_CONFIG): Default to bootstrap-debug. * Makefile.in: Rebuilt. contrib/ChangeLog: * compare-debug: Look for .gkd files and compare them. config/ChangeLog: * bootstrap-debug.mk: Add comments. * bootstrap-debug-big.mk: New. * bootstrap-debug-lean.mk: New. * bootstrap-debug-ckovw.mk: Add comments. * bootstrap-debug-lib.mk: Drop CFLAGS for stages. Use -g0 for TFLAGS in stage1. Drop -fvar-tracking-assignments-toggle. From-SVN: r151312
1501 lines
42 KiB
C
1501 lines
42 KiB
C
/* Dead code elimination pass for the GNU compiler.
|
|
Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
|
|
Free Software Foundation, Inc.
|
|
Contributed by Ben Elliston <bje@redhat.com>
|
|
and Andrew MacLeod <amacleod@redhat.com>
|
|
Adapted to use control dependence by Steven Bosscher, SUSE Labs.
|
|
|
|
This file is part of GCC.
|
|
|
|
GCC is free software; you can redistribute it and/or modify it
|
|
under the terms of the GNU General Public License as published by the
|
|
Free Software Foundation; either version 3, or (at your option) any
|
|
later version.
|
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with GCC; see the file COPYING3. If not see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
/* Dead code elimination.
|
|
|
|
References:
|
|
|
|
Building an Optimizing Compiler,
|
|
Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
|
|
|
|
Advanced Compiler Design and Implementation,
|
|
Steven Muchnick, Morgan Kaufmann, 1997, Section 18.10.
|
|
|
|
Dead-code elimination is the removal of statements which have no
|
|
impact on the program's output. "Dead statements" have no impact
|
|
on the program's output, while "necessary statements" may have
|
|
impact on the output.
|
|
|
|
The algorithm consists of three phases:
|
|
1. Marking as necessary all statements known to be necessary,
|
|
e.g. most function calls, writing a value to memory, etc;
|
|
2. Propagating necessary statements, e.g., the statements
|
|
giving values to operands in necessary statements; and
|
|
3. Removing dead statements. */
|
|
|
|
#include "config.h"
|
|
#include "system.h"
|
|
#include "coretypes.h"
|
|
#include "tm.h"
|
|
#include "ggc.h"
|
|
|
|
/* These RTL headers are needed for basic-block.h. */
|
|
#include "rtl.h"
|
|
#include "tm_p.h"
|
|
#include "hard-reg-set.h"
|
|
#include "obstack.h"
|
|
#include "basic-block.h"
|
|
|
|
#include "tree.h"
|
|
#include "diagnostic.h"
|
|
#include "tree-flow.h"
|
|
#include "gimple.h"
|
|
#include "tree-dump.h"
|
|
#include "tree-pass.h"
|
|
#include "timevar.h"
|
|
#include "flags.h"
|
|
#include "cfgloop.h"
|
|
#include "tree-scalar-evolution.h"
|
|
|
|
static struct stmt_stats
|
|
{
|
|
int total;
|
|
int total_phis;
|
|
int removed;
|
|
int removed_phis;
|
|
} stats;
|
|
|
|
#define STMT_NECESSARY GF_PLF_1
|
|
|
|
static VEC(gimple,heap) *worklist;
|
|
|
|
/* Vector indicating an SSA name has already been processed and marked
|
|
as necessary. */
|
|
static sbitmap processed;
|
|
|
|
/* Vector indicating that last_stmt if a basic block has already been
|
|
marked as necessary. */
|
|
static sbitmap last_stmt_necessary;
|
|
|
|
/* Vector indicating that BB contains statements that are live. */
|
|
static sbitmap bb_contains_live_stmts;
|
|
|
|
/* Before we can determine whether a control branch is dead, we need to
|
|
compute which blocks are control dependent on which edges.
|
|
|
|
We expect each block to be control dependent on very few edges so we
|
|
use a bitmap for each block recording its edges. An array holds the
|
|
bitmap. The Ith bit in the bitmap is set if that block is dependent
|
|
on the Ith edge. */
|
|
static bitmap *control_dependence_map;
|
|
|
|
/* Vector indicating that a basic block has already had all the edges
|
|
processed that it is control dependent on. */
|
|
static sbitmap visited_control_parents;
|
|
|
|
/* TRUE if this pass alters the CFG (by removing control statements).
|
|
FALSE otherwise.
|
|
|
|
If this pass alters the CFG, then it will arrange for the dominators
|
|
to be recomputed. */
|
|
static bool cfg_altered;
|
|
|
|
/* Execute code that follows the macro for each edge (given number
|
|
EDGE_NUMBER within the CODE) for which the block with index N is
|
|
control dependent. */
|
|
#define EXECUTE_IF_CONTROL_DEPENDENT(BI, N, EDGE_NUMBER) \
|
|
EXECUTE_IF_SET_IN_BITMAP (control_dependence_map[(N)], 0, \
|
|
(EDGE_NUMBER), (BI))
|
|
|
|
|
|
/* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */
|
|
static inline void
|
|
set_control_dependence_map_bit (basic_block bb, int edge_index)
|
|
{
|
|
if (bb == ENTRY_BLOCK_PTR)
|
|
return;
|
|
gcc_assert (bb != EXIT_BLOCK_PTR);
|
|
bitmap_set_bit (control_dependence_map[bb->index], edge_index);
|
|
}
|
|
|
|
/* Clear all control dependences for block BB. */
|
|
static inline void
|
|
clear_control_dependence_bitmap (basic_block bb)
|
|
{
|
|
bitmap_clear (control_dependence_map[bb->index]);
|
|
}
|
|
|
|
|
|
/* Find the immediate postdominator PDOM of the specified basic block BLOCK.
|
|
This function is necessary because some blocks have negative numbers. */
|
|
|
|
static inline basic_block
|
|
find_pdom (basic_block block)
|
|
{
|
|
gcc_assert (block != ENTRY_BLOCK_PTR);
|
|
|
|
if (block == EXIT_BLOCK_PTR)
|
|
return EXIT_BLOCK_PTR;
|
|
else
|
|
{
|
|
basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
|
|
if (! bb)
|
|
return EXIT_BLOCK_PTR;
|
|
return bb;
|
|
}
|
|
}
|
|
|
|
|
|
/* Determine all blocks' control dependences on the given edge with edge_list
|
|
EL index EDGE_INDEX, ala Morgan, Section 3.6. */
|
|
|
|
static void
|
|
find_control_dependence (struct edge_list *el, int edge_index)
|
|
{
|
|
basic_block current_block;
|
|
basic_block ending_block;
|
|
|
|
gcc_assert (INDEX_EDGE_PRED_BB (el, edge_index) != EXIT_BLOCK_PTR);
|
|
|
|
if (INDEX_EDGE_PRED_BB (el, edge_index) == ENTRY_BLOCK_PTR)
|
|
ending_block = single_succ (ENTRY_BLOCK_PTR);
|
|
else
|
|
ending_block = find_pdom (INDEX_EDGE_PRED_BB (el, edge_index));
|
|
|
|
for (current_block = INDEX_EDGE_SUCC_BB (el, edge_index);
|
|
current_block != ending_block && current_block != EXIT_BLOCK_PTR;
|
|
current_block = find_pdom (current_block))
|
|
{
|
|
edge e = INDEX_EDGE (el, edge_index);
|
|
|
|
/* For abnormal edges, we don't make current_block control
|
|
dependent because instructions that throw are always necessary
|
|
anyway. */
|
|
if (e->flags & EDGE_ABNORMAL)
|
|
continue;
|
|
|
|
set_control_dependence_map_bit (current_block, edge_index);
|
|
}
|
|
}
|
|
|
|
|
|
/* Record all blocks' control dependences on all edges in the edge
|
|
list EL, ala Morgan, Section 3.6. */
|
|
|
|
static void
|
|
find_all_control_dependences (struct edge_list *el)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < NUM_EDGES (el); ++i)
|
|
find_control_dependence (el, i);
|
|
}
|
|
|
|
/* If STMT is not already marked necessary, mark it, and add it to the
|
|
worklist if ADD_TO_WORKLIST is true. */
|
|
static inline void
|
|
mark_stmt_necessary (gimple stmt, bool add_to_worklist)
|
|
{
|
|
gcc_assert (stmt);
|
|
|
|
if (gimple_plf (stmt, STMT_NECESSARY))
|
|
return;
|
|
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
{
|
|
fprintf (dump_file, "Marking useful stmt: ");
|
|
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
|
|
fprintf (dump_file, "\n");
|
|
}
|
|
|
|
gimple_set_plf (stmt, STMT_NECESSARY, true);
|
|
if (add_to_worklist)
|
|
VEC_safe_push (gimple, heap, worklist, stmt);
|
|
if (bb_contains_live_stmts && !is_gimple_debug (stmt))
|
|
SET_BIT (bb_contains_live_stmts, gimple_bb (stmt)->index);
|
|
}
|
|
|
|
|
|
/* Mark the statement defining operand OP as necessary. */
|
|
|
|
static inline void
|
|
mark_operand_necessary (tree op)
|
|
{
|
|
gimple stmt;
|
|
int ver;
|
|
|
|
gcc_assert (op);
|
|
|
|
ver = SSA_NAME_VERSION (op);
|
|
if (TEST_BIT (processed, ver))
|
|
{
|
|
stmt = SSA_NAME_DEF_STMT (op);
|
|
gcc_assert (gimple_nop_p (stmt)
|
|
|| gimple_plf (stmt, STMT_NECESSARY));
|
|
return;
|
|
}
|
|
SET_BIT (processed, ver);
|
|
|
|
stmt = SSA_NAME_DEF_STMT (op);
|
|
gcc_assert (stmt);
|
|
|
|
if (gimple_plf (stmt, STMT_NECESSARY) || gimple_nop_p (stmt))
|
|
return;
|
|
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
{
|
|
fprintf (dump_file, "marking necessary through ");
|
|
print_generic_expr (dump_file, op, 0);
|
|
fprintf (dump_file, " stmt ");
|
|
print_gimple_stmt (dump_file, stmt, 0, 0);
|
|
}
|
|
|
|
gimple_set_plf (stmt, STMT_NECESSARY, true);
|
|
if (bb_contains_live_stmts)
|
|
SET_BIT (bb_contains_live_stmts, gimple_bb (stmt)->index);
|
|
VEC_safe_push (gimple, heap, worklist, stmt);
|
|
}
|
|
|
|
|
|
/* Mark STMT as necessary if it obviously is. Add it to the worklist if
|
|
it can make other statements necessary.
|
|
|
|
If AGGRESSIVE is false, control statements are conservatively marked as
|
|
necessary. */
|
|
|
|
static void
|
|
mark_stmt_if_obviously_necessary (gimple stmt, bool aggressive)
|
|
{
|
|
tree lhs = NULL_TREE;
|
|
/* With non-call exceptions, we have to assume that all statements could
|
|
throw. If a statement may throw, it is inherently necessary. */
|
|
if (flag_non_call_exceptions
|
|
&& stmt_could_throw_p (stmt))
|
|
{
|
|
mark_stmt_necessary (stmt, true);
|
|
return;
|
|
}
|
|
|
|
/* Statements that are implicitly live. Most function calls, asm
|
|
and return statements are required. Labels and GIMPLE_BIND nodes
|
|
are kept because they are control flow, and we have no way of
|
|
knowing whether they can be removed. DCE can eliminate all the
|
|
other statements in a block, and CFG can then remove the block
|
|
and labels. */
|
|
switch (gimple_code (stmt))
|
|
{
|
|
case GIMPLE_PREDICT:
|
|
case GIMPLE_LABEL:
|
|
mark_stmt_necessary (stmt, false);
|
|
return;
|
|
|
|
case GIMPLE_ASM:
|
|
case GIMPLE_RESX:
|
|
case GIMPLE_RETURN:
|
|
mark_stmt_necessary (stmt, true);
|
|
return;
|
|
|
|
case GIMPLE_CALL:
|
|
/* Most, but not all function calls are required. Function calls that
|
|
produce no result and have no side effects (i.e. const pure
|
|
functions) are unnecessary. */
|
|
if (gimple_has_side_effects (stmt))
|
|
{
|
|
mark_stmt_necessary (stmt, true);
|
|
return;
|
|
}
|
|
if (!gimple_call_lhs (stmt))
|
|
return;
|
|
lhs = gimple_call_lhs (stmt);
|
|
/* Fall through */
|
|
|
|
case GIMPLE_ASSIGN:
|
|
if (!lhs)
|
|
lhs = gimple_assign_lhs (stmt);
|
|
/* These values are mildly magic bits of the EH runtime. We can't
|
|
see the entire lifetime of these values until landing pads are
|
|
generated. */
|
|
if (TREE_CODE (lhs) == EXC_PTR_EXPR
|
|
|| TREE_CODE (lhs) == FILTER_EXPR)
|
|
{
|
|
mark_stmt_necessary (stmt, true);
|
|
return;
|
|
}
|
|
break;
|
|
|
|
case GIMPLE_DEBUG:
|
|
mark_stmt_necessary (stmt, false);
|
|
return;
|
|
|
|
case GIMPLE_GOTO:
|
|
gcc_assert (!simple_goto_p (stmt));
|
|
mark_stmt_necessary (stmt, true);
|
|
return;
|
|
|
|
case GIMPLE_COND:
|
|
gcc_assert (EDGE_COUNT (gimple_bb (stmt)->succs) == 2);
|
|
/* Fall through. */
|
|
|
|
case GIMPLE_SWITCH:
|
|
if (! aggressive)
|
|
mark_stmt_necessary (stmt, true);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
/* If the statement has volatile operands, it needs to be preserved.
|
|
Same for statements that can alter control flow in unpredictable
|
|
ways. */
|
|
if (gimple_has_volatile_ops (stmt) || is_ctrl_altering_stmt (stmt))
|
|
{
|
|
mark_stmt_necessary (stmt, true);
|
|
return;
|
|
}
|
|
|
|
if (is_hidden_global_store (stmt))
|
|
{
|
|
mark_stmt_necessary (stmt, true);
|
|
return;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
/* Make corresponding control dependent edges necessary. We only
|
|
have to do this once for each basic block, so we clear the bitmap
|
|
after we're done. */
|
|
static void
|
|
mark_control_dependent_edges_necessary (basic_block bb, struct edge_list *el)
|
|
{
|
|
bitmap_iterator bi;
|
|
unsigned edge_number;
|
|
|
|
gcc_assert (bb != EXIT_BLOCK_PTR);
|
|
|
|
if (bb == ENTRY_BLOCK_PTR)
|
|
return;
|
|
|
|
EXECUTE_IF_CONTROL_DEPENDENT (bi, bb->index, edge_number)
|
|
{
|
|
gimple stmt;
|
|
basic_block cd_bb = INDEX_EDGE_PRED_BB (el, edge_number);
|
|
|
|
if (TEST_BIT (last_stmt_necessary, cd_bb->index))
|
|
continue;
|
|
SET_BIT (last_stmt_necessary, cd_bb->index);
|
|
SET_BIT (bb_contains_live_stmts, cd_bb->index);
|
|
|
|
stmt = last_stmt (cd_bb);
|
|
if (stmt && is_ctrl_stmt (stmt))
|
|
mark_stmt_necessary (stmt, true);
|
|
}
|
|
}
|
|
|
|
|
|
/* Find obviously necessary statements. These are things like most function
|
|
calls, and stores to file level variables.
|
|
|
|
If EL is NULL, control statements are conservatively marked as
|
|
necessary. Otherwise it contains the list of edges used by control
|
|
dependence analysis. */
|
|
|
|
static void
|
|
find_obviously_necessary_stmts (struct edge_list *el)
|
|
{
|
|
basic_block bb;
|
|
gimple_stmt_iterator gsi;
|
|
edge e;
|
|
gimple phi, stmt;
|
|
|
|
FOR_EACH_BB (bb)
|
|
{
|
|
/* PHI nodes are never inherently necessary. */
|
|
for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
|
|
{
|
|
phi = gsi_stmt (gsi);
|
|
gimple_set_plf (phi, STMT_NECESSARY, false);
|
|
}
|
|
|
|
/* Check all statements in the block. */
|
|
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
|
|
{
|
|
stmt = gsi_stmt (gsi);
|
|
gimple_set_plf (stmt, STMT_NECESSARY, false);
|
|
mark_stmt_if_obviously_necessary (stmt, el != NULL);
|
|
}
|
|
}
|
|
|
|
/* Pure and const functions are finite and thus have no infinite loops in
|
|
them. */
|
|
if ((TREE_READONLY (current_function_decl)
|
|
|| DECL_PURE_P (current_function_decl))
|
|
&& !DECL_LOOPING_CONST_OR_PURE_P (current_function_decl))
|
|
return;
|
|
|
|
/* Prevent the empty possibly infinite loops from being removed. */
|
|
if (el)
|
|
{
|
|
loop_iterator li;
|
|
struct loop *loop;
|
|
scev_initialize ();
|
|
if (mark_irreducible_loops ())
|
|
FOR_EACH_BB (bb)
|
|
{
|
|
edge_iterator ei;
|
|
FOR_EACH_EDGE (e, ei, bb->succs)
|
|
if ((e->flags & EDGE_DFS_BACK)
|
|
&& (e->flags & EDGE_IRREDUCIBLE_LOOP))
|
|
{
|
|
if (dump_file)
|
|
fprintf (dump_file, "Marking back edge of irreducible loop %i->%i\n",
|
|
e->src->index, e->dest->index);
|
|
mark_control_dependent_edges_necessary (e->dest, el);
|
|
}
|
|
}
|
|
|
|
FOR_EACH_LOOP (li, loop, 0)
|
|
if (!finite_loop_p (loop))
|
|
{
|
|
if (dump_file)
|
|
fprintf (dump_file, "can not prove finiteness of loop %i\n", loop->num);
|
|
mark_control_dependent_edges_necessary (loop->latch, el);
|
|
}
|
|
scev_finalize ();
|
|
}
|
|
}
|
|
|
|
|
|
/* Return true if REF is based on an aliased base, otherwise false. */
|
|
|
|
static bool
|
|
ref_may_be_aliased (tree ref)
|
|
{
|
|
while (handled_component_p (ref))
|
|
ref = TREE_OPERAND (ref, 0);
|
|
return !(DECL_P (ref)
|
|
&& !may_be_aliased (ref));
|
|
}
|
|
|
|
static bitmap visited = NULL;
|
|
static unsigned int longest_chain = 0;
|
|
static unsigned int total_chain = 0;
|
|
static bool chain_ovfl = false;
|
|
|
|
/* Worker for the walker that marks reaching definitions of REF,
|
|
which is based on a non-aliased decl, necessary. It returns
|
|
true whenever the defining statement of the current VDEF is
|
|
a kill for REF, as no dominating may-defs are necessary for REF
|
|
anymore. DATA points to cached get_ref_base_and_extent data for REF. */
|
|
|
|
static bool
|
|
mark_aliased_reaching_defs_necessary_1 (ao_ref *ref, tree vdef,
|
|
void *data ATTRIBUTE_UNUSED)
|
|
{
|
|
gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
|
|
|
|
/* All stmts we visit are necessary. */
|
|
mark_operand_necessary (vdef);
|
|
|
|
/* If the stmt lhs kills ref, then we can stop walking. */
|
|
if (gimple_has_lhs (def_stmt)
|
|
&& TREE_CODE (gimple_get_lhs (def_stmt)) != SSA_NAME)
|
|
{
|
|
tree base, lhs = gimple_get_lhs (def_stmt);
|
|
HOST_WIDE_INT size, offset, max_size;
|
|
ao_ref_base (ref);
|
|
base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
|
|
/* We can get MEM[symbol: sZ, index: D.8862_1] here,
|
|
so base == refd->base does not always hold. */
|
|
if (base == ref->base)
|
|
{
|
|
/* For a must-alias check we need to be able to constrain
|
|
the accesses properly. */
|
|
if (size != -1 && size == max_size
|
|
&& ref->max_size != -1)
|
|
{
|
|
if (offset <= ref->offset
|
|
&& offset + size >= ref->offset + ref->max_size)
|
|
return true;
|
|
}
|
|
/* Or they need to be exactly the same. */
|
|
else if (ref->ref
|
|
&& operand_equal_p (ref->ref, lhs, 0))
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/* Otherwise keep walking. */
|
|
return false;
|
|
}
|
|
|
|
static void
|
|
mark_aliased_reaching_defs_necessary (gimple stmt, tree ref)
|
|
{
|
|
unsigned int chain;
|
|
ao_ref refd;
|
|
gcc_assert (!chain_ovfl);
|
|
ao_ref_init (&refd, ref);
|
|
chain = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
|
|
mark_aliased_reaching_defs_necessary_1,
|
|
NULL, NULL);
|
|
if (chain > longest_chain)
|
|
longest_chain = chain;
|
|
total_chain += chain;
|
|
}
|
|
|
|
/* Worker for the walker that marks reaching definitions of REF, which
|
|
is not based on a non-aliased decl. For simplicity we need to end
|
|
up marking all may-defs necessary that are not based on a non-aliased
|
|
decl. The only job of this walker is to skip may-defs based on
|
|
a non-aliased decl. */
|
|
|
|
static bool
|
|
mark_all_reaching_defs_necessary_1 (ao_ref *ref ATTRIBUTE_UNUSED,
|
|
tree vdef, void *data ATTRIBUTE_UNUSED)
|
|
{
|
|
gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
|
|
|
|
/* We have to skip already visited (and thus necessary) statements
|
|
to make the chaining work after we dropped back to simple mode. */
|
|
if (chain_ovfl
|
|
&& TEST_BIT (processed, SSA_NAME_VERSION (vdef)))
|
|
{
|
|
gcc_assert (gimple_nop_p (def_stmt)
|
|
|| gimple_plf (def_stmt, STMT_NECESSARY));
|
|
return false;
|
|
}
|
|
|
|
/* We want to skip stores to non-aliased variables. */
|
|
if (!chain_ovfl
|
|
&& gimple_assign_single_p (def_stmt))
|
|
{
|
|
tree lhs = gimple_assign_lhs (def_stmt);
|
|
if (!ref_may_be_aliased (lhs))
|
|
return false;
|
|
}
|
|
|
|
mark_operand_necessary (vdef);
|
|
|
|
return false;
|
|
}
|
|
|
|
static void
|
|
mark_all_reaching_defs_necessary (gimple stmt)
|
|
{
|
|
walk_aliased_vdefs (NULL, gimple_vuse (stmt),
|
|
mark_all_reaching_defs_necessary_1, NULL, &visited);
|
|
}
|
|
|
|
/* Return true for PHI nodes with one or identical arguments
|
|
can be removed. */
|
|
static bool
|
|
degenerate_phi_p (gimple phi)
|
|
{
|
|
unsigned int i;
|
|
tree op = gimple_phi_arg_def (phi, 0);
|
|
for (i = 1; i < gimple_phi_num_args (phi); i++)
|
|
if (gimple_phi_arg_def (phi, i) != op)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/* Propagate necessity using the operands of necessary statements.
|
|
Process the uses on each statement in the worklist, and add all
|
|
feeding statements which contribute to the calculation of this
|
|
value to the worklist.
|
|
|
|
In conservative mode, EL is NULL. */
|
|
|
|
static void
|
|
propagate_necessity (struct edge_list *el)
|
|
{
|
|
gimple stmt;
|
|
bool aggressive = (el ? true : false);
|
|
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
fprintf (dump_file, "\nProcessing worklist:\n");
|
|
|
|
while (VEC_length (gimple, worklist) > 0)
|
|
{
|
|
/* Take STMT from worklist. */
|
|
stmt = VEC_pop (gimple, worklist);
|
|
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
{
|
|
fprintf (dump_file, "processing: ");
|
|
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
|
|
fprintf (dump_file, "\n");
|
|
}
|
|
|
|
if (aggressive)
|
|
{
|
|
/* Mark the last statements of the basic blocks that the block
|
|
containing STMT is control dependent on, but only if we haven't
|
|
already done so. */
|
|
basic_block bb = gimple_bb (stmt);
|
|
if (bb != ENTRY_BLOCK_PTR
|
|
&& ! TEST_BIT (visited_control_parents, bb->index))
|
|
{
|
|
SET_BIT (visited_control_parents, bb->index);
|
|
mark_control_dependent_edges_necessary (bb, el);
|
|
}
|
|
}
|
|
|
|
if (gimple_code (stmt) == GIMPLE_PHI
|
|
/* We do not process virtual PHI nodes nor do we track their
|
|
necessity. */
|
|
&& is_gimple_reg (gimple_phi_result (stmt)))
|
|
{
|
|
/* PHI nodes are somewhat special in that each PHI alternative has
|
|
data and control dependencies. All the statements feeding the
|
|
PHI node's arguments are always necessary. In aggressive mode,
|
|
we also consider the control dependent edges leading to the
|
|
predecessor block associated with each PHI alternative as
|
|
necessary. */
|
|
size_t k;
|
|
|
|
for (k = 0; k < gimple_phi_num_args (stmt); k++)
|
|
{
|
|
tree arg = PHI_ARG_DEF (stmt, k);
|
|
if (TREE_CODE (arg) == SSA_NAME)
|
|
mark_operand_necessary (arg);
|
|
}
|
|
|
|
if (aggressive && !degenerate_phi_p (stmt))
|
|
{
|
|
for (k = 0; k < gimple_phi_num_args (stmt); k++)
|
|
{
|
|
basic_block arg_bb = gimple_phi_arg_edge (stmt, k)->src;
|
|
if (arg_bb != ENTRY_BLOCK_PTR
|
|
&& ! TEST_BIT (visited_control_parents, arg_bb->index))
|
|
{
|
|
SET_BIT (visited_control_parents, arg_bb->index);
|
|
mark_control_dependent_edges_necessary (arg_bb, el);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* Propagate through the operands. Examine all the USE, VUSE and
|
|
VDEF operands in this statement. Mark all the statements
|
|
which feed this statement's uses as necessary. */
|
|
ssa_op_iter iter;
|
|
tree use;
|
|
|
|
FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
|
|
mark_operand_necessary (use);
|
|
|
|
use = gimple_vuse (stmt);
|
|
if (!use)
|
|
continue;
|
|
|
|
/* If we dropped to simple mode make all immediately
|
|
reachable definitions necessary. */
|
|
if (chain_ovfl)
|
|
{
|
|
mark_all_reaching_defs_necessary (stmt);
|
|
continue;
|
|
}
|
|
|
|
/* For statements that may load from memory (have a VUSE) we
|
|
have to mark all reaching (may-)definitions as necessary.
|
|
We partition this task into two cases:
|
|
1) explicit loads based on decls that are not aliased
|
|
2) implicit loads (like calls) and explicit loads not
|
|
based on decls that are not aliased (like indirect
|
|
references or loads from globals)
|
|
For 1) we mark all reaching may-defs as necessary, stopping
|
|
at dominating kills. For 2) we want to mark all dominating
|
|
references necessary, but non-aliased ones which we handle
|
|
in 1). By keeping a global visited bitmap for references
|
|
we walk for 2) we avoid quadratic behavior for those. */
|
|
|
|
if (is_gimple_call (stmt))
|
|
{
|
|
tree callee = gimple_call_fndecl (stmt);
|
|
unsigned i;
|
|
|
|
/* Calls to functions that are merely acting as barriers
|
|
or that only store to memory do not make any previous
|
|
stores necessary. */
|
|
if (callee != NULL_TREE
|
|
&& DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
|
|
&& (DECL_FUNCTION_CODE (callee) == BUILT_IN_MEMSET
|
|
|| DECL_FUNCTION_CODE (callee) == BUILT_IN_MALLOC
|
|
|| DECL_FUNCTION_CODE (callee) == BUILT_IN_FREE))
|
|
continue;
|
|
|
|
/* Calls implicitly load from memory, their arguments
|
|
in addition may explicitly perform memory loads. */
|
|
mark_all_reaching_defs_necessary (stmt);
|
|
for (i = 0; i < gimple_call_num_args (stmt); ++i)
|
|
{
|
|
tree arg = gimple_call_arg (stmt, i);
|
|
if (TREE_CODE (arg) == SSA_NAME
|
|
|| is_gimple_min_invariant (arg))
|
|
continue;
|
|
if (!ref_may_be_aliased (arg))
|
|
mark_aliased_reaching_defs_necessary (stmt, arg);
|
|
}
|
|
}
|
|
else if (gimple_assign_single_p (stmt))
|
|
{
|
|
tree rhs;
|
|
bool rhs_aliased = false;
|
|
/* If this is a load mark things necessary. */
|
|
rhs = gimple_assign_rhs1 (stmt);
|
|
if (TREE_CODE (rhs) != SSA_NAME
|
|
&& !is_gimple_min_invariant (rhs))
|
|
{
|
|
if (!ref_may_be_aliased (rhs))
|
|
mark_aliased_reaching_defs_necessary (stmt, rhs);
|
|
else
|
|
rhs_aliased = true;
|
|
}
|
|
if (rhs_aliased)
|
|
mark_all_reaching_defs_necessary (stmt);
|
|
}
|
|
else if (gimple_code (stmt) == GIMPLE_RETURN)
|
|
{
|
|
tree rhs = gimple_return_retval (stmt);
|
|
/* A return statement may perform a load. */
|
|
if (TREE_CODE (rhs) != SSA_NAME
|
|
&& !is_gimple_min_invariant (rhs))
|
|
{
|
|
if (!ref_may_be_aliased (rhs))
|
|
mark_aliased_reaching_defs_necessary (stmt, rhs);
|
|
else
|
|
mark_all_reaching_defs_necessary (stmt);
|
|
}
|
|
}
|
|
else if (gimple_code (stmt) == GIMPLE_ASM)
|
|
{
|
|
unsigned i;
|
|
mark_all_reaching_defs_necessary (stmt);
|
|
/* Inputs may perform loads. */
|
|
for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
|
|
{
|
|
tree op = TREE_VALUE (gimple_asm_input_op (stmt, i));
|
|
if (TREE_CODE (op) != SSA_NAME
|
|
&& !is_gimple_min_invariant (op)
|
|
&& !ref_may_be_aliased (op))
|
|
mark_aliased_reaching_defs_necessary (stmt, op);
|
|
}
|
|
}
|
|
else
|
|
gcc_unreachable ();
|
|
|
|
/* If we over-used our alias oracle budget drop to simple
|
|
mode. The cost metric allows quadratic behavior up to
|
|
a constant maximal chain and after that falls back to
|
|
super-linear complexity. */
|
|
if (longest_chain > 256
|
|
&& total_chain > 256 * longest_chain)
|
|
{
|
|
chain_ovfl = true;
|
|
if (visited)
|
|
bitmap_clear (visited);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Replace all uses of result of PHI by underlying variable and mark it
|
|
for renaming. */
|
|
|
|
static void
|
|
mark_virtual_phi_result_for_renaming (gimple phi)
|
|
{
|
|
bool used = false;
|
|
imm_use_iterator iter;
|
|
use_operand_p use_p;
|
|
gimple stmt;
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
{
|
|
fprintf (dump_file, "Marking result for renaming : ");
|
|
print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
|
|
fprintf (dump_file, "\n");
|
|
}
|
|
FOR_EACH_IMM_USE_STMT (stmt, iter, gimple_phi_result (phi))
|
|
{
|
|
FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
|
|
SET_USE (use_p, SSA_NAME_VAR (gimple_phi_result (phi)));
|
|
update_stmt (stmt);
|
|
used = true;
|
|
}
|
|
if (used)
|
|
mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
|
|
}
|
|
|
|
/* Remove dead PHI nodes from block BB. */
|
|
|
|
static bool
|
|
remove_dead_phis (basic_block bb)
|
|
{
|
|
bool something_changed = false;
|
|
gimple_seq phis;
|
|
gimple phi;
|
|
gimple_stmt_iterator gsi;
|
|
phis = phi_nodes (bb);
|
|
|
|
for (gsi = gsi_start (phis); !gsi_end_p (gsi);)
|
|
{
|
|
stats.total_phis++;
|
|
phi = gsi_stmt (gsi);
|
|
|
|
/* We do not track necessity of virtual PHI nodes. Instead do
|
|
very simple dead PHI removal here. */
|
|
if (!is_gimple_reg (gimple_phi_result (phi)))
|
|
{
|
|
/* Virtual PHI nodes with one or identical arguments
|
|
can be removed. */
|
|
if (degenerate_phi_p (phi))
|
|
{
|
|
tree vdef = gimple_phi_result (phi);
|
|
tree vuse = gimple_phi_arg_def (phi, 0);
|
|
|
|
use_operand_p use_p;
|
|
imm_use_iterator iter;
|
|
gimple use_stmt;
|
|
FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
|
|
FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
|
|
SET_USE (use_p, vuse);
|
|
if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef)
|
|
&& TREE_CODE (vuse) == SSA_NAME)
|
|
SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
|
|
}
|
|
else
|
|
gimple_set_plf (phi, STMT_NECESSARY, true);
|
|
}
|
|
|
|
if (!gimple_plf (phi, STMT_NECESSARY))
|
|
{
|
|
something_changed = true;
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
{
|
|
fprintf (dump_file, "Deleting : ");
|
|
print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
|
|
fprintf (dump_file, "\n");
|
|
}
|
|
|
|
remove_phi_node (&gsi, true);
|
|
stats.removed_phis++;
|
|
continue;
|
|
}
|
|
|
|
gsi_next (&gsi);
|
|
}
|
|
return something_changed;
|
|
}
|
|
|
|
/* Find first live post dominator of BB. */
|
|
|
|
static basic_block
|
|
get_live_post_dom (basic_block bb)
|
|
{
|
|
basic_block post_dom_bb;
|
|
|
|
|
|
/* The post dominance info has to be up-to-date. */
|
|
gcc_assert (dom_info_state (CDI_POST_DOMINATORS) == DOM_OK);
|
|
|
|
/* Get the immediate post dominator of bb. */
|
|
post_dom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
|
|
/* And look for first live one. */
|
|
while (post_dom_bb != EXIT_BLOCK_PTR
|
|
&& !TEST_BIT (bb_contains_live_stmts, post_dom_bb->index))
|
|
post_dom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, post_dom_bb);
|
|
|
|
return post_dom_bb;
|
|
}
|
|
|
|
/* Forward edge E to respective POST_DOM_BB and update PHIs. */
|
|
|
|
static edge
|
|
forward_edge_to_pdom (edge e, basic_block post_dom_bb)
|
|
{
|
|
gimple_stmt_iterator gsi;
|
|
edge e2 = NULL;
|
|
edge_iterator ei;
|
|
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
fprintf (dump_file, "Redirecting edge %i->%i to %i\n", e->src->index,
|
|
e->dest->index, post_dom_bb->index);
|
|
|
|
e2 = redirect_edge_and_branch (e, post_dom_bb);
|
|
cfg_altered = true;
|
|
|
|
/* If edge was already around, no updating is neccesary. */
|
|
if (e2 != e)
|
|
return e2;
|
|
|
|
if (phi_nodes (post_dom_bb))
|
|
{
|
|
/* We are sure that for every live PHI we are seeing control dependent BB.
|
|
This means that we can look up the end of control dependent path leading
|
|
to the PHI itself. */
|
|
FOR_EACH_EDGE (e2, ei, post_dom_bb->preds)
|
|
if (e2 != e && dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->src))
|
|
break;
|
|
for (gsi = gsi_start_phis (post_dom_bb); !gsi_end_p (gsi);)
|
|
{
|
|
gimple phi = gsi_stmt (gsi);
|
|
tree op;
|
|
source_location locus;
|
|
|
|
/* Dead PHI do not imply control dependency. */
|
|
if (!gimple_plf (phi, STMT_NECESSARY)
|
|
&& is_gimple_reg (gimple_phi_result (phi)))
|
|
{
|
|
gsi_next (&gsi);
|
|
continue;
|
|
}
|
|
if (gimple_phi_arg_def (phi, e->dest_idx))
|
|
{
|
|
gsi_next (&gsi);
|
|
continue;
|
|
}
|
|
|
|
/* We didn't find edge to update. This can happen for PHIs on virtuals
|
|
since there is no control dependency relation on them. We are lost
|
|
here and must force renaming of the symbol. */
|
|
if (!is_gimple_reg (gimple_phi_result (phi)))
|
|
{
|
|
mark_virtual_phi_result_for_renaming (phi);
|
|
remove_phi_node (&gsi, true);
|
|
continue;
|
|
}
|
|
if (!e2)
|
|
{
|
|
op = gimple_phi_arg_def (phi, e->dest_idx == 0 ? 1 : 0);
|
|
locus = gimple_phi_arg_location (phi, e->dest_idx == 0 ? 1 : 0);
|
|
}
|
|
else
|
|
{
|
|
op = gimple_phi_arg_def (phi, e2->dest_idx);
|
|
locus = gimple_phi_arg_location (phi, e2->dest_idx);
|
|
}
|
|
add_phi_arg (phi, op, e, locus);
|
|
gcc_assert (e2 || degenerate_phi_p (phi));
|
|
gsi_next (&gsi);
|
|
}
|
|
}
|
|
return e;
|
|
}
|
|
|
|
/* Remove dead statement pointed to by iterator I. Receives the basic block BB
|
|
containing I so that we don't have to look it up. */
|
|
|
|
static void
|
|
remove_dead_stmt (gimple_stmt_iterator *i, basic_block bb)
|
|
{
|
|
gimple stmt = gsi_stmt (*i);
|
|
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
{
|
|
fprintf (dump_file, "Deleting : ");
|
|
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
|
|
fprintf (dump_file, "\n");
|
|
}
|
|
|
|
stats.removed++;
|
|
|
|
/* If we have determined that a conditional branch statement contributes
|
|
nothing to the program, then we not only remove it, but we also change
|
|
the flow graph so that the current block will simply fall-thru to its
|
|
immediate post-dominator. The blocks we are circumventing will be
|
|
removed by cleanup_tree_cfg if this change in the flow graph makes them
|
|
unreachable. */
|
|
if (is_ctrl_stmt (stmt))
|
|
{
|
|
basic_block post_dom_bb;
|
|
edge e, e2;
|
|
edge_iterator ei;
|
|
|
|
post_dom_bb = get_live_post_dom (bb);
|
|
|
|
e = find_edge (bb, post_dom_bb);
|
|
|
|
/* If edge is already there, try to use it. This avoids need to update
|
|
PHI nodes. Also watch for cases where post dominator does not exists
|
|
or is exit block. These can happen for infinite loops as we create
|
|
fake edges in the dominator tree. */
|
|
if (e)
|
|
;
|
|
else if (! post_dom_bb || post_dom_bb == EXIT_BLOCK_PTR)
|
|
e = EDGE_SUCC (bb, 0);
|
|
else
|
|
e = forward_edge_to_pdom (EDGE_SUCC (bb, 0), post_dom_bb);
|
|
gcc_assert (e);
|
|
e->probability = REG_BR_PROB_BASE;
|
|
e->count = bb->count;
|
|
|
|
/* The edge is no longer associated with a conditional, so it does
|
|
not have TRUE/FALSE flags. */
|
|
e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
|
|
|
|
/* The lone outgoing edge from BB will be a fallthru edge. */
|
|
e->flags |= EDGE_FALLTHRU;
|
|
|
|
/* Remove the remaining outgoing edges. */
|
|
for (ei = ei_start (bb->succs); (e2 = ei_safe_edge (ei)); )
|
|
if (e != e2)
|
|
{
|
|
cfg_altered = true;
|
|
remove_edge (e2);
|
|
}
|
|
else
|
|
ei_next (&ei);
|
|
}
|
|
|
|
unlink_stmt_vdef (stmt);
|
|
gsi_remove (i, true);
|
|
release_defs (stmt);
|
|
}
|
|
|
|
/* Eliminate unnecessary statements. Any instruction not marked as necessary
|
|
contributes nothing to the program, and can be deleted. */
|
|
|
|
static bool
|
|
eliminate_unnecessary_stmts (void)
|
|
{
|
|
bool something_changed = false;
|
|
basic_block bb;
|
|
gimple_stmt_iterator gsi;
|
|
gimple stmt;
|
|
tree call;
|
|
VEC (basic_block, heap) *h;
|
|
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
fprintf (dump_file, "\nEliminating unnecessary statements:\n");
|
|
|
|
clear_special_calls ();
|
|
|
|
/* Walking basic blocks and statements in reverse order avoids
|
|
releasing SSA names before any other DEFs that refer to them are
|
|
released. This helps avoid loss of debug information, as we get
|
|
a chance to propagate all RHSs of removed SSAs into debug uses,
|
|
rather than only the latest ones. E.g., consider:
|
|
|
|
x_3 = y_1 + z_2;
|
|
a_5 = x_3 - b_4;
|
|
# DEBUG a => a_5
|
|
|
|
If we were to release x_3 before a_5, when we reached a_5 and
|
|
tried to substitute it into the debug stmt, we'd see x_3 there,
|
|
but x_3's DEF, type, etc would have already been disconnected.
|
|
By going backwards, the debug stmt first changes to:
|
|
|
|
# DEBUG a => x_3 - b_4
|
|
|
|
and then to:
|
|
|
|
# DEBUG a => y_1 + z_2 - b_4
|
|
|
|
as desired. */
|
|
gcc_assert (dom_info_available_p (CDI_DOMINATORS));
|
|
h = get_all_dominated_blocks (CDI_DOMINATORS, single_succ (ENTRY_BLOCK_PTR));
|
|
|
|
while (VEC_length (basic_block, h))
|
|
{
|
|
bb = VEC_pop (basic_block, h);
|
|
|
|
/* Remove dead statements. */
|
|
for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
|
|
{
|
|
stmt = gsi_stmt (gsi);
|
|
|
|
stats.total++;
|
|
|
|
/* If GSI is not necessary then remove it. */
|
|
if (!gimple_plf (stmt, STMT_NECESSARY))
|
|
{
|
|
remove_dead_stmt (&gsi, bb);
|
|
something_changed = true;
|
|
|
|
/* If stmt was the last stmt in the block, we want to
|
|
move gsi to the stmt that became the last stmt, but
|
|
gsi_prev would crash. */
|
|
if (gsi_end_p (gsi))
|
|
gsi = gsi_last_bb (bb);
|
|
else
|
|
gsi_prev (&gsi);
|
|
}
|
|
else if (is_gimple_call (stmt))
|
|
{
|
|
call = gimple_call_fndecl (stmt);
|
|
if (call)
|
|
{
|
|
tree name;
|
|
|
|
/* When LHS of var = call (); is dead, simplify it into
|
|
call (); saving one operand. */
|
|
name = gimple_call_lhs (stmt);
|
|
if (name && TREE_CODE (name) == SSA_NAME
|
|
&& !TEST_BIT (processed, SSA_NAME_VERSION (name)))
|
|
{
|
|
something_changed = true;
|
|
if (dump_file && (dump_flags & TDF_DETAILS))
|
|
{
|
|
fprintf (dump_file, "Deleting LHS of call: ");
|
|
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
|
|
fprintf (dump_file, "\n");
|
|
}
|
|
|
|
gimple_call_set_lhs (stmt, NULL_TREE);
|
|
maybe_clean_or_replace_eh_stmt (stmt, stmt);
|
|
update_stmt (stmt);
|
|
release_ssa_name (name);
|
|
}
|
|
notice_special_calls (stmt);
|
|
}
|
|
gsi_prev (&gsi);
|
|
}
|
|
else
|
|
gsi_prev (&gsi);
|
|
}
|
|
}
|
|
|
|
VEC_free (basic_block, heap, h);
|
|
|
|
/* Since we don't track liveness of virtual PHI nodes, it is possible that we
|
|
rendered some PHI nodes unreachable while they are still in use.
|
|
Mark them for renaming. */
|
|
if (cfg_altered)
|
|
{
|
|
basic_block prev_bb;
|
|
|
|
find_unreachable_blocks ();
|
|
|
|
/* Delete all unreachable basic blocks in reverse dominator order. */
|
|
for (bb = EXIT_BLOCK_PTR->prev_bb; bb != ENTRY_BLOCK_PTR; bb = prev_bb)
|
|
{
|
|
prev_bb = bb->prev_bb;
|
|
|
|
if (!TEST_BIT (bb_contains_live_stmts, bb->index)
|
|
|| !(bb->flags & BB_REACHABLE))
|
|
{
|
|
for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
|
|
if (!is_gimple_reg (gimple_phi_result (gsi_stmt (gsi))))
|
|
{
|
|
bool found = false;
|
|
imm_use_iterator iter;
|
|
|
|
FOR_EACH_IMM_USE_STMT (stmt, iter, gimple_phi_result (gsi_stmt (gsi)))
|
|
{
|
|
if (!(gimple_bb (stmt)->flags & BB_REACHABLE))
|
|
continue;
|
|
if (gimple_code (stmt) == GIMPLE_PHI
|
|
|| gimple_plf (stmt, STMT_NECESSARY))
|
|
{
|
|
found = true;
|
|
BREAK_FROM_IMM_USE_STMT (iter);
|
|
}
|
|
}
|
|
if (found)
|
|
mark_virtual_phi_result_for_renaming (gsi_stmt (gsi));
|
|
}
|
|
|
|
if (!(bb->flags & BB_REACHABLE))
|
|
{
|
|
/* Speed up the removal of blocks that don't
|
|
dominate others. Walking backwards, this should
|
|
be the common case. ??? Do we need to recompute
|
|
dominators because of cfg_altered? */
|
|
if (!MAY_HAVE_DEBUG_STMTS
|
|
|| !first_dom_son (CDI_DOMINATORS, bb))
|
|
delete_basic_block (bb);
|
|
else
|
|
{
|
|
h = get_all_dominated_blocks (CDI_DOMINATORS, bb);
|
|
|
|
while (VEC_length (basic_block, h))
|
|
{
|
|
bb = VEC_pop (basic_block, h);
|
|
prev_bb = bb->prev_bb;
|
|
/* Rearrangements to the CFG may have failed
|
|
to update the dominators tree, so that
|
|
formerly-dominated blocks are now
|
|
otherwise reachable. */
|
|
if (!!(bb->flags & BB_REACHABLE))
|
|
continue;
|
|
delete_basic_block (bb);
|
|
}
|
|
|
|
VEC_free (basic_block, heap, h);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
FOR_EACH_BB (bb)
|
|
{
|
|
/* Remove dead PHI nodes. */
|
|
something_changed |= remove_dead_phis (bb);
|
|
}
|
|
|
|
return something_changed;
|
|
}
|
|
|
|
|
|
/* Print out removed statement statistics. */
|
|
|
|
static void
|
|
print_stats (void)
|
|
{
|
|
float percg;
|
|
|
|
percg = ((float) stats.removed / (float) stats.total) * 100;
|
|
fprintf (dump_file, "Removed %d of %d statements (%d%%)\n",
|
|
stats.removed, stats.total, (int) percg);
|
|
|
|
if (stats.total_phis == 0)
|
|
percg = 0;
|
|
else
|
|
percg = ((float) stats.removed_phis / (float) stats.total_phis) * 100;
|
|
|
|
fprintf (dump_file, "Removed %d of %d PHI nodes (%d%%)\n",
|
|
stats.removed_phis, stats.total_phis, (int) percg);
|
|
}
|
|
|
|
/* Initialization for this pass. Set up the used data structures. */
|
|
|
|
static void
|
|
tree_dce_init (bool aggressive)
|
|
{
|
|
memset ((void *) &stats, 0, sizeof (stats));
|
|
|
|
if (aggressive)
|
|
{
|
|
int i;
|
|
|
|
control_dependence_map = XNEWVEC (bitmap, last_basic_block);
|
|
for (i = 0; i < last_basic_block; ++i)
|
|
control_dependence_map[i] = BITMAP_ALLOC (NULL);
|
|
|
|
last_stmt_necessary = sbitmap_alloc (last_basic_block);
|
|
sbitmap_zero (last_stmt_necessary);
|
|
bb_contains_live_stmts = sbitmap_alloc (last_basic_block);
|
|
sbitmap_zero (bb_contains_live_stmts);
|
|
}
|
|
|
|
processed = sbitmap_alloc (num_ssa_names + 1);
|
|
sbitmap_zero (processed);
|
|
|
|
worklist = VEC_alloc (gimple, heap, 64);
|
|
cfg_altered = false;
|
|
}
|
|
|
|
/* Cleanup after this pass. */
|
|
|
|
static void
|
|
tree_dce_done (bool aggressive)
|
|
{
|
|
if (aggressive)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < last_basic_block; ++i)
|
|
BITMAP_FREE (control_dependence_map[i]);
|
|
free (control_dependence_map);
|
|
|
|
sbitmap_free (visited_control_parents);
|
|
sbitmap_free (last_stmt_necessary);
|
|
sbitmap_free (bb_contains_live_stmts);
|
|
bb_contains_live_stmts = NULL;
|
|
}
|
|
|
|
sbitmap_free (processed);
|
|
|
|
VEC_free (gimple, heap, worklist);
|
|
}
|
|
|
|
/* Main routine to eliminate dead code.
|
|
|
|
AGGRESSIVE controls the aggressiveness of the algorithm.
|
|
In conservative mode, we ignore control dependence and simply declare
|
|
all but the most trivially dead branches necessary. This mode is fast.
|
|
In aggressive mode, control dependences are taken into account, which
|
|
results in more dead code elimination, but at the cost of some time.
|
|
|
|
FIXME: Aggressive mode before PRE doesn't work currently because
|
|
the dominance info is not invalidated after DCE1. This is
|
|
not an issue right now because we only run aggressive DCE
|
|
as the last tree SSA pass, but keep this in mind when you
|
|
start experimenting with pass ordering. */
|
|
|
|
static unsigned int
|
|
perform_tree_ssa_dce (bool aggressive)
|
|
{
|
|
struct edge_list *el = NULL;
|
|
bool something_changed = 0;
|
|
|
|
/* Preheaders are needed for SCEV to work.
|
|
Simple lateches and recorded exits improve chances that loop will
|
|
proved to be finite in testcases such as in loop-15.c and loop-24.c */
|
|
if (aggressive)
|
|
loop_optimizer_init (LOOPS_NORMAL
|
|
| LOOPS_HAVE_RECORDED_EXITS);
|
|
|
|
tree_dce_init (aggressive);
|
|
|
|
if (aggressive)
|
|
{
|
|
/* Compute control dependence. */
|
|
timevar_push (TV_CONTROL_DEPENDENCES);
|
|
calculate_dominance_info (CDI_POST_DOMINATORS);
|
|
el = create_edge_list ();
|
|
find_all_control_dependences (el);
|
|
timevar_pop (TV_CONTROL_DEPENDENCES);
|
|
|
|
visited_control_parents = sbitmap_alloc (last_basic_block);
|
|
sbitmap_zero (visited_control_parents);
|
|
|
|
mark_dfs_back_edges ();
|
|
}
|
|
|
|
find_obviously_necessary_stmts (el);
|
|
|
|
if (aggressive)
|
|
loop_optimizer_finalize ();
|
|
|
|
longest_chain = 0;
|
|
total_chain = 0;
|
|
chain_ovfl = false;
|
|
propagate_necessity (el);
|
|
BITMAP_FREE (visited);
|
|
|
|
something_changed |= eliminate_unnecessary_stmts ();
|
|
something_changed |= cfg_altered;
|
|
|
|
/* We do not update postdominators, so free them unconditionally. */
|
|
free_dominance_info (CDI_POST_DOMINATORS);
|
|
|
|
/* If we removed paths in the CFG, then we need to update
|
|
dominators as well. I haven't investigated the possibility
|
|
of incrementally updating dominators. */
|
|
if (cfg_altered)
|
|
free_dominance_info (CDI_DOMINATORS);
|
|
|
|
statistics_counter_event (cfun, "Statements deleted", stats.removed);
|
|
statistics_counter_event (cfun, "PHI nodes deleted", stats.removed_phis);
|
|
|
|
/* Debugging dumps. */
|
|
if (dump_file && (dump_flags & (TDF_STATS|TDF_DETAILS)))
|
|
print_stats ();
|
|
|
|
tree_dce_done (aggressive);
|
|
|
|
free_edge_list (el);
|
|
|
|
if (something_changed)
|
|
return (TODO_update_ssa | TODO_cleanup_cfg | TODO_ggc_collect
|
|
| TODO_remove_unused_locals);
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
/* Pass entry points. */
|
|
static unsigned int
|
|
tree_ssa_dce (void)
|
|
{
|
|
return perform_tree_ssa_dce (/*aggressive=*/false);
|
|
}
|
|
|
|
static unsigned int
|
|
tree_ssa_dce_loop (void)
|
|
{
|
|
unsigned int todo;
|
|
todo = perform_tree_ssa_dce (/*aggressive=*/false);
|
|
if (todo)
|
|
{
|
|
free_numbers_of_iterations_estimates ();
|
|
scev_reset ();
|
|
}
|
|
return todo;
|
|
}
|
|
|
|
static unsigned int
|
|
tree_ssa_cd_dce (void)
|
|
{
|
|
return perform_tree_ssa_dce (/*aggressive=*/optimize >= 2);
|
|
}
|
|
|
|
static bool
|
|
gate_dce (void)
|
|
{
|
|
return flag_tree_dce != 0;
|
|
}
|
|
|
|
struct gimple_opt_pass pass_dce =
|
|
{
|
|
{
|
|
GIMPLE_PASS,
|
|
"dce", /* name */
|
|
gate_dce, /* gate */
|
|
tree_ssa_dce, /* execute */
|
|
NULL, /* sub */
|
|
NULL, /* next */
|
|
0, /* static_pass_number */
|
|
TV_TREE_DCE, /* tv_id */
|
|
PROP_cfg | PROP_ssa, /* properties_required */
|
|
0, /* properties_provided */
|
|
0, /* properties_destroyed */
|
|
0, /* todo_flags_start */
|
|
TODO_dump_func | TODO_verify_ssa /* todo_flags_finish */
|
|
}
|
|
};
|
|
|
|
struct gimple_opt_pass pass_dce_loop =
|
|
{
|
|
{
|
|
GIMPLE_PASS,
|
|
"dceloop", /* name */
|
|
gate_dce, /* gate */
|
|
tree_ssa_dce_loop, /* execute */
|
|
NULL, /* sub */
|
|
NULL, /* next */
|
|
0, /* static_pass_number */
|
|
TV_TREE_DCE, /* tv_id */
|
|
PROP_cfg | PROP_ssa, /* properties_required */
|
|
0, /* properties_provided */
|
|
0, /* properties_destroyed */
|
|
0, /* todo_flags_start */
|
|
TODO_dump_func | TODO_verify_ssa /* todo_flags_finish */
|
|
}
|
|
};
|
|
|
|
struct gimple_opt_pass pass_cd_dce =
|
|
{
|
|
{
|
|
GIMPLE_PASS,
|
|
"cddce", /* name */
|
|
gate_dce, /* gate */
|
|
tree_ssa_cd_dce, /* execute */
|
|
NULL, /* sub */
|
|
NULL, /* next */
|
|
0, /* static_pass_number */
|
|
TV_TREE_CD_DCE, /* tv_id */
|
|
PROP_cfg | PROP_ssa, /* properties_required */
|
|
0, /* properties_provided */
|
|
0, /* properties_destroyed */
|
|
0, /* todo_flags_start */
|
|
TODO_dump_func | TODO_verify_ssa
|
|
| TODO_verify_flow /* todo_flags_finish */
|
|
}
|
|
};
|