* basic-block.h (last_basic_block): Eliminate macro. * asan.c (transform_statements): Eliminate use of last_basic_block in favor of last_basic_block_for_fn, in order to make use of cfun explicit. * bb-reorder.c (copy_bb, reorder_basic_blocks): Likewise. * bt-load.c (compute_defs_uses_and_gen, compute_kill, compute_out, link_btr_uses, build_btr_def_use_webs, migrate_btr_defs): Likewise. * cfg.c (compact_blocks): Likewise. * cfganal.c (mark_dfs_back_edges, control_dependences::control_dependences, post_order_compute, pre_and_rev_post_order_compute_fn, dfs_enumerate_from, compute_idf, single_pred_before_succ_order): Likewise. * cfgbuild.c (make_edges): Likewise. * cfgexpand.c (add_scope_conflicts, gimple_expand_cfg): Likewise. * cfghooks.c (verify_flow_info): Likewise. * cfgloop.c (verify_loop_structure): Likewise. * cfgloopanal.c (just_once_each_iteration_p, mark_irreducible_loops): Likewise. * cfgloopmanip.c (fix_bb_placements, remove_path, update_dominators_in_loop): Likewise. * cfgrtl.c (create_basic_block_structure, rtl_create_basic_block, break_superblocks, rtl_flow_call_edges_add): Likewise. * config/epiphany/resolve-sw-modes.c (resolve_sw_modes): Likewise. * config/frv/frv.c (frv_optimize_membar): Likewise. * config/mips/mips.c (r10k_insert_cache_barriers): Likewise. * config/spu/spu.c (spu_machine_dependent_reorg): Likewise. * cprop.c (compute_local_properties, find_implicit_sets, bypass_conditional_jumps, one_cprop_pass): Likewise. * cse.c (cse_main): Likewise. * df-core.c (rest_of_handle_df_initialize, df_worklist_dataflow, df_analyze, df_grow_bb_info, df_compact_blocks): Likewise. * df-problems.c (df_lr_verify_solution_start, df_live_verify_solution_start, df_md_local_compute): Likewise. * dominance.c (init_dom_info, calc_dfs_tree_nonrec, calc_dfs_tree, calc_idoms): Likewise. * domwalk.c (dom_walker::walk): Likewise. * dse.c (dse_step0, dse_step3): Likewise. * function.c (epilogue_done): Likewise. * gcse.c (alloc_gcse_mem, compute_local_properties, prune_insertions_deletions, compute_pre_data, pre_expr_reaches_here_p, one_pre_gcse_pass, compute_code_hoist_vbeinout, should_hoist_expr_to_dom, hoist_code, one_code_hoisting_pass): Likewise. * graph.c (draw_cfg_nodes_no_loops): Likewise. * graphite-sese-to-poly.c (build_scop_bbs): Likewise. * haifa-sched.c (unlink_bb_notes): Likewise. * ipa-split.c (execute_split_functions): Likewise. * ira-build.c (create_loop_tree_nodes, remove_unnecessary_regions): Likewise. * ira-emit.c (ira_emit): Likewise. * ira.c (find_moveable_pseudos, ira): Likewise. * lcm.c (compute_antinout_edge, compute_laterin, compute_insert_delete, pre_edge_lcm, compute_available, compute_nearerout, compute_rev_insert_delete, pre_edge_rev_lcm): Likewise. * loop-unroll.c (opt_info_start_duplication, apply_opt_in_copies): Likewise. * lower-subreg.c (decompose_multiword_subregs): Likewise. * lra-lives.c (lra_create_live_ranges): Likewise. * lra.c (lra): Likewise. * mode-switching.c (optimize_mode_switching): Likewise. * recog.c (split_all_insns): Likewise. * regcprop.c (copyprop_hardreg_forward): Likewise. * regrename.c (regrename_analyze): Likewise. * reload1.c (reload): Likewise. * resource.c (init_resource_info): Likewise. * sched-rgn.c (haifa_find_rgns, extend_rgns, compute_trg_info, realloc_bb_state_array, schedule_region, extend_regions): Likewise. * sel-sched-ir.c (sel_extend_global_bb_info, extend_region_bb_info, recompute_rev_top_order, sel_init_pipelining, make_regions_from_the_rest): Likewise. * store-motion.c (remove_reachable_equiv_notes,build_store_vectors) Likewise. * tracer.c (tail_duplicate): Likewise. * trans-mem.c (tm_region_init, get_bb_regions_instrumented): Likewise. * tree-cfg.c (create_bb, cleanup_dead_labels, gimple_dump_cfg, gimple_flow_call_edges_add): Likewise. * tree-cfgcleanup.c (split_bbs_on_noreturn_calls, cleanup_tree_cfg_1): Likewise. * tree-complex.c (tree_lower_complex): Likewise. * tree-inline.c (copy_cfg_body): Likewise. * tree-into-ssa.c (mark_phi_for_rewrite, rewrite_into_ssa, prepare_def_site_for, update_ssa): Likewise. * tree-ssa-dce.c (tree_dce_init, perform_tree_ssa_dce): Likewise. * tree-ssa-dom.c (record_edge_info): Likewise. * tree-ssa-live.c (new_tree_live_info, live_worklist): Likewise. * tree-ssa-loop-im.c (fill_always_executed_in_1): Likewise. * tree-ssa-loop-manip.c (copy_phi_node_args gimple_duplicate_loop_to_header_edge): Likewise. * tree-ssa-pre.c (compute_antic): Likewise. * tree-ssa-propagate.c (ssa_prop_init): Likewise. * tree-ssa-reassoc.c (init_reassoc): Likewise. * tree-ssa-sccvn.c (init_scc_vn): Likewise. * tree-ssa-tail-merge.c (init_worklist): Likewise. * tree-ssa-uncprop.c (associate_equivalences_with_edges): Likewise. * tree-stdarg.c (reachable_at_most_once): Likewise. * tree-vrp.c (find_assert_locations): Likewise. * var-tracking.c (vt_find_locations): Likewise. From-SVN: r205826
221 lines
6.9 KiB
C
221 lines
6.9 KiB
C
/* Generic dominator tree walker
|
|
Copyright (C) 2003-2013 Free Software Foundation, Inc.
|
|
Contributed by Diego Novillo <dnovillo@redhat.com>
|
|
|
|
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/>. */
|
|
|
|
#include "config.h"
|
|
#include "system.h"
|
|
#include "coretypes.h"
|
|
#include "tm.h"
|
|
#include "basic-block.h"
|
|
#include "domwalk.h"
|
|
#include "sbitmap.h"
|
|
|
|
/* This file implements a generic walker for dominator trees.
|
|
|
|
To understand the dominator walker one must first have a grasp of dominators,
|
|
immediate dominators and the dominator tree.
|
|
|
|
Dominators
|
|
A block B1 is said to dominate B2 if every path from the entry to B2 must
|
|
pass through B1. Given the dominance relationship, we can proceed to
|
|
compute immediate dominators. Note it is not important whether or not
|
|
our definition allows a block to dominate itself.
|
|
|
|
Immediate Dominators:
|
|
Every block in the CFG has no more than one immediate dominator. The
|
|
immediate dominator of block BB must dominate BB and must not dominate
|
|
any other dominator of BB and must not be BB itself.
|
|
|
|
Dominator tree:
|
|
If we then construct a tree where each node is a basic block and there
|
|
is an edge from each block's immediate dominator to the block itself, then
|
|
we have a dominator tree.
|
|
|
|
|
|
[ Note this walker can also walk the post-dominator tree, which is
|
|
defined in a similar manner. i.e., block B1 is said to post-dominate
|
|
block B2 if all paths from B2 to the exit block must pass through
|
|
B1. ]
|
|
|
|
For example, given the CFG
|
|
|
|
1
|
|
|
|
|
2
|
|
/ \
|
|
3 4
|
|
/ \
|
|
+---------->5 6
|
|
| / \ /
|
|
| +--->8 7
|
|
| | / |
|
|
| +--9 11
|
|
| / |
|
|
+--- 10 ---> 12
|
|
|
|
|
|
We have a dominator tree which looks like
|
|
|
|
1
|
|
|
|
|
2
|
|
/ \
|
|
/ \
|
|
3 4
|
|
/ / \ \
|
|
| | | |
|
|
5 6 7 12
|
|
| |
|
|
8 11
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
|
|
|
|
|
The dominator tree is the basis for a number of analysis, transformation
|
|
and optimization algorithms that operate on a semi-global basis.
|
|
|
|
The dominator walker is a generic routine which visits blocks in the CFG
|
|
via a depth first search of the dominator tree. In the example above
|
|
the dominator walker might visit blocks in the following order
|
|
1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12.
|
|
|
|
The dominator walker has a number of callbacks to perform actions
|
|
during the walk of the dominator tree. There are two callbacks
|
|
which walk statements, one before visiting the dominator children,
|
|
one after visiting the dominator children. There is a callback
|
|
before and after each statement walk callback. In addition, the
|
|
dominator walker manages allocation/deallocation of data structures
|
|
which are local to each block visited.
|
|
|
|
The dominator walker is meant to provide a generic means to build a pass
|
|
which can analyze or transform/optimize a function based on walking
|
|
the dominator tree. One simply fills in the dominator walker data
|
|
structure with the appropriate callbacks and calls the walker.
|
|
|
|
We currently use the dominator walker to prune the set of variables
|
|
which might need PHI nodes (which can greatly improve compile-time
|
|
performance in some cases).
|
|
|
|
We also use the dominator walker to rewrite the function into SSA form
|
|
which reduces code duplication since the rewriting phase is inherently
|
|
a walk of the dominator tree.
|
|
|
|
And (of course), we use the dominator walker to drive our dominator
|
|
optimizer, which is a semi-global optimizer.
|
|
|
|
TODO:
|
|
|
|
Walking statements is based on the block statement iterator abstraction,
|
|
which is currently an abstraction over walking tree statements. Thus
|
|
the dominator walker is currently only useful for trees. */
|
|
|
|
static int *bb_postorder;
|
|
|
|
static int
|
|
cmp_bb_postorder (const void *a, const void *b)
|
|
{
|
|
basic_block bb1 = *(basic_block *)const_cast<void *>(a);
|
|
basic_block bb2 = *(basic_block *)const_cast<void *>(b);
|
|
if (bb1->index == bb2->index)
|
|
return 0;
|
|
/* Place higher completion number first (pop off lower number first). */
|
|
if (bb_postorder[bb1->index] > bb_postorder[bb2->index])
|
|
return -1;
|
|
return 1;
|
|
}
|
|
|
|
/* Recursively walk the dominator tree.
|
|
BB is the basic block we are currently visiting. */
|
|
|
|
void
|
|
dom_walker::walk (basic_block bb)
|
|
{
|
|
basic_block dest;
|
|
basic_block *worklist = XNEWVEC (basic_block,
|
|
n_basic_blocks_for_fn (cfun) * 2);
|
|
int sp = 0;
|
|
int *postorder, postorder_num;
|
|
|
|
if (m_dom_direction == CDI_DOMINATORS)
|
|
{
|
|
postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
|
|
postorder_num = inverted_post_order_compute (postorder);
|
|
bb_postorder = XNEWVEC (int, last_basic_block_for_fn (cfun));
|
|
for (int i = 0; i < postorder_num; ++i)
|
|
bb_postorder[postorder[i]] = i;
|
|
free (postorder);
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
/* Don't worry about unreachable blocks. */
|
|
if (EDGE_COUNT (bb->preds) > 0
|
|
|| bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
|
|
|| bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
|
|
{
|
|
/* Callback for subclasses to do custom things before we have walked
|
|
the dominator children, but before we walk statements. */
|
|
before_dom_children (bb);
|
|
|
|
/* Mark the current BB to be popped out of the recursion stack
|
|
once children are processed. */
|
|
worklist[sp++] = bb;
|
|
worklist[sp++] = NULL;
|
|
|
|
int saved_sp = sp;
|
|
for (dest = first_dom_son (m_dom_direction, bb);
|
|
dest; dest = next_dom_son (m_dom_direction, dest))
|
|
worklist[sp++] = dest;
|
|
if (m_dom_direction == CDI_DOMINATORS)
|
|
switch (sp - saved_sp)
|
|
{
|
|
case 0:
|
|
case 1:
|
|
break;
|
|
default:
|
|
qsort (&worklist[saved_sp], sp - saved_sp,
|
|
sizeof (basic_block), cmp_bb_postorder);
|
|
}
|
|
}
|
|
/* NULL is used to mark pop operations in the recursion stack. */
|
|
while (sp > 0 && !worklist[sp - 1])
|
|
{
|
|
--sp;
|
|
bb = worklist[--sp];
|
|
|
|
/* Callback allowing subclasses to do custom things after we have
|
|
walked dominator children, but before we walk statements. */
|
|
after_dom_children (bb);
|
|
}
|
|
if (sp)
|
|
bb = worklist[--sp];
|
|
else
|
|
break;
|
|
}
|
|
if (m_dom_direction == CDI_DOMINATORS)
|
|
{
|
|
free (bb_postorder);
|
|
bb_postorder = NULL;
|
|
}
|
|
free (worklist);
|
|
}
|