GCC Middle and Back End API Reference
dominance.c File Reference

Data Structures

struct  dom_info


typedef unsigned int TBB


static void init_dom_info (struct dom_info *, enum cdi_direction)
static void free_dom_info (struct dom_info *)
static void calc_dfs_tree_nonrec (struct dom_info *, basic_block, bool)
static void calc_dfs_tree (struct dom_info *, bool)
static void compress (struct dom_info *, TBB)
static TBB eval (struct dom_info *, TBB)
static void link_roots (struct dom_info *, TBB, TBB)
static void calc_idoms (struct dom_info *, bool)
void debug_dominance_info (enum cdi_direction)
void debug_dominance_tree (enum cdi_direction, basic_block)
static void init_dom_info ()
static unsigned int dom_convert_dir_to_idx ()
static void free_dom_info ()
static void calc_dfs_tree_nonrec ()
static void calc_dfs_tree ()
static void compress ()
static TBB eval ()
static void link_roots ()
static void calc_idoms ()
static void assign_dfs_numbers ()
static void compute_dom_fast_query ()
void calculate_dominance_info ()
void free_dominance_info ()
basic_block get_immediate_dominator ()
void set_immediate_dominator (enum cdi_direction dir, basic_block bb, basic_block dominated_by)
vec< basic_blockget_dominated_by ()
vec< basic_blockget_dominated_by_region (enum cdi_direction dir, basic_block *region, unsigned n_region)
vec< basic_blockget_dominated_to_depth ()
vec< basic_blockget_all_dominated_blocks ()
void redirect_immediate_dominators (enum cdi_direction dir, basic_block bb, basic_block to)
basic_block nearest_common_dominator ()
basic_block nearest_common_dominator_for_set ()
bool dominated_by_p ()
unsigned bb_dom_dfs_in ()
unsigned bb_dom_dfs_out ()
DEBUG_FUNCTION void verify_dominators ()
basic_block recompute_dominator ()
static void prune_bbs_to_update_dominators (vec< basic_block > bbs, bool conservative)
static basic_block root_of_dom_tree ()
static void determine_dominators_for_sons (struct graph *g, vec< basic_block > bbs, int y, int *son, int *brother)
void iterate_fix_dominators (enum cdi_direction dir, vec< basic_block > bbs, bool conservative)
void add_to_dominance_info ()
void delete_from_dominance_info ()
basic_block first_dom_son ()
basic_block next_dom_son ()
enum dom_state dom_info_state ()
void set_dom_info_availability ()
bool dom_info_available_p ()
DEBUG_FUNCTION void debug_dominance_info ()
static void debug_dominance_tree_1 (enum cdi_direction dir, basic_block root, unsigned indent, bool indent_first)
DEBUG_FUNCTION void debug_dominance_tree ()

Typedef Documentation

typedef unsigned int TBB
   Calculate (post)dominators in slightly super-linear time.
   Copyright (C) 2000-2013 Free Software Foundation, Inc.
   Contributed by Michael Matz (matz@ifh.de).

   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
   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
   This file implements the well known algorithm from Lengauer and Tarjan
   to compute the dominators in a control flow graph.  A basic block D is said
   to dominate another block X, when all paths from the entry node of the CFG
   to X go also over D.  The dominance relation is a transitive reflexive
   relation and its minimal transitive reduction is a tree, called the
   dominator tree.  So for each block X besides the entry block exists a
   block I(X), called the immediate dominator of X, which is the parent of X
   in the dominator tree.

   The algorithm computes this dominator tree implicitly by computing for
   each block its immediate dominator.  We use tree balancing and path
   compression, so it's the O(e*a(e,v)) variant, where a(e,v) is the very
   slowly growing functional inverse of the Ackerman function.  
   We name our nodes with integers, beginning with 1.  Zero is reserved for
   'undefined' or 'end of list'.  The name of each node is given by the dfs
   number of the corresponding basic block.  Please note, that we include the
   artificial ENTRY_BLOCK (or EXIT_BLOCK in the post-dom case) in our lists to
   support multiple entry points.  Its dfs number is of course 1.  
   Type of Basic Block aka. TBB 

Function Documentation

void add_to_dominance_info ( )

References indent, and et_node::son.

static void assign_dfs_numbers ( )
   Assign dfs numbers starting from NUM to NODE and its sons.  

References et_new_tree().

unsigned bb_dom_dfs_in ( )
   Returns the entry dfs number for basic block BB, in the direction DIR.  

References dominated_by_p(), nearest_common_dominator(), basic_block_def::preds, and edge_def::src.

unsigned bb_dom_dfs_out ( )
   Returns the exit dfs number for basic block BB, in the direction DIR.  
static void calc_dfs_tree ( struct dom_info ,
static void calc_dfs_tree ( )
   The main entry for calculating the DFS tree or forest.  DI is our working
   structure and REVERSE is true, if we are interested in the reverse flow
   graph.  In that case the result is not necessarily a tree but a forest,
   because there may be nodes from which the EXIT_BLOCK is unreachable.  
     The first block is the ENTRY_BLOCK (or EXIT_BLOCK if REVERSE).  
         In the post-dom case we may have nodes without a path to EXIT_BLOCK.
         They are reverse-unreachable.  In the dom-case we disallow such
         nodes, but in post-dom we have to deal with them.

         There are two situations in which this occurs.  First, noreturn
         functions.  Second, infinite loops.  In the first case we need to
         pretend that there is an edge to the exit block.  In the second
         case, we wind up with a forest.  We need to process all noreturn
         blocks before we know if we've got any infinite loops.  
     This aborts e.g. when there is _no_ path from ENTRY to EXIT at all.  
static void calc_dfs_tree_nonrec ( struct dom_info ,
basic_block  ,
static void calc_dfs_tree_nonrec ( )
   The nonrecursive variant of creating a DFS tree.  DI is our working
   structure, BB the starting basic block for this tree and REVERSE
   is true, if predecessors should be visited instead of successors of a
   node.  After this is done all nodes reachable from BB were visited, have
   assigned their dfs number and are linked together to form a tree.  
     We call this _only_ if bb is not already visited.  
     Start block (ENTRY_BLOCK_PTR for forward problem, EXIT_BLOCK for backward
     Ending block.  
     Initialize our border blocks, and the first edge.  
     When the stack is empty we break out of this loop.  
         This loop traverses edges e in depth first manner, and fills the
             Deduce from E the current and the next block (BB and BN), and the
             next edge.  
                 If the next node BN is either already visited or a border
                 block the current edge is useless, and simply overwritten
                 with the next edge out of the current node.  
             Fill the DFS tree info calculatable _before_ recursing.  
             Save the current point in the CFG on the stack, and recurse.  
         OK.  The edge-list was exhausted, meaning normally we would
         end the recursion.  After returning from the recursive call,
         there were (may be) other statements which were run after a
         child node was completely considered by DFS.  Here is the
         point to do it in the non-recursive variant.
         E.g. The block just completed is in e->dest for forward DFS,
         the block not yet completed (the parent of the one above)
         in e->src.  This could be used e.g. for computing the number of
         descendants or the tree depth.  
static void calc_idoms ( struct dom_info ,
static void calc_idoms ( )
   This calculates the immediate dominators (or post-dominators if REVERSE is
   true).  DI is our working structure and should hold the DFS forest.
   On return the immediate dominator to node V is in di->dom[V].  
     Go backwards in DFS order, to first look at the leafs.  
             If this block has a fake edge to exit, process that first.  
         Search all direct predecessors for the smallest node with a path
         to them.  That way we have the smallest node with also a path to
         us only over nodes behind us.  In effect we search for our
             Call eval() only if really needed.  If k1 is above V in DFS tree,
             then we know, that eval(k1) == k1 and key[k1] == k1.  
         Transform semidominators into dominators.  
         We don't need to cleanup next_bucket[].  
     Explicitly define the dominators.  
void calculate_dominance_info ( )
   The main entry point into this module.  DIR is set depending on whether
   we want to compute dominators or postdominators.  
static void compress ( struct dom_info ,
static void compress ( )
   Compress the path from V to the root of its set and update path_min at the
   same time.  After compress(di, V) set_chain[V] is the root of the set V is
   in and path_min[V] is the node with the smallest key[] value on the path
   from V to that root.  
     Btw. It's not worth to unrecurse compress() as the depth is usually not
     greater than 5 even for huge graphs (I've not seen call depth > 4).
     Also performance wise compress() ranges _far_ behind eval().  

References dom_info::key, dom_info::path_min, dom_info::set_chain, dom_info::set_child, and dom_info::set_size.

static void compute_dom_fast_query ( )
   Compute the data necessary for fast resolving of dominator queries in a
   static dominator tree.  
void debug_dominance_info ( enum  cdi_direction)
DEBUG_FUNCTION void debug_dominance_info ( )
void debug_dominance_tree ( enum  cdi_direction,
DEBUG_FUNCTION void debug_dominance_tree ( )
   Prints to stderr representation of the dominance tree (for direction DIR)
   rooted in ROOT.  
static void debug_dominance_tree_1 ( enum cdi_direction  dir,
basic_block  root,
unsigned  indent,
bool  indent_first 
   Prints to stderr representation of the dominance tree (for direction DIR)
   rooted in ROOT, indented by INDENT tabulators.  If INDENT_FIRST is false,
   the first line of the output is not indented.  

Referenced by first_dom_son().

void delete_from_dominance_info ( )
static void determine_dominators_for_sons ( struct graph g,
vec< basic_block bbs,
int  y,
int *  son,
int *  brother 
   See the comment in iterate_fix_dominators.  Finds the immediate dominators
   for the sons of Y, found using the SON and BROTHER arrays representing
   the dominance tree of graph G.  BBS maps the vertices of G to the basic
         Handle the common case Y has just one son specially.  
     ???  Needed to work around the pre-processor confusion with
     using a multi-argument template type as macro argument.  

References CDI_DOMINATORS, dom, dom_convert_dir_to_idx(), g, map, and set_immediate_dominator().

static unsigned int dom_convert_dir_to_idx ( )
   Map dominance calculation type to array index used for various
   dominance information arrays.  This version is simple -- it will need
   to be modified, obviously, if additional values are added to

Referenced by determine_dominators_for_sons(), free_dominance_info(), get_dominated_by_region(), get_dominated_to_depth(), and nearest_common_dominator_for_set().

bool dom_info_available_p ( )
   Returns true if dominance information for direction DIR is available.  
enum dom_state dom_info_state ( )
   Return dominance availability for dominance info DIR.  
bool dominated_by_p ( )
    Given a dominator tree, we can determine whether one thing
    dominates another in constant time by using two DFS numbers:

    1. The number for when we visit a node on the way down the tree
    2. The number for when we visit a node on the way back up the tree

    You can view these as bounds for the range of dfs numbers the
    nodes in the subtree of the dominator tree rooted at that node
    will contain.

    The dominator tree is always a simple acyclic tree, so there are
    only three possible relations two nodes in the dominator tree have
    to each other:

    1. Node A is above Node B (and thus, Node A dominates node B)

    / \
   B   D

   In the above case, DFS_Number_In of A will be <= DFS_Number_In of
   B, and DFS_Number_Out of A will be >= DFS_Number_Out of B.  This is
   because we must hit A in the dominator tree *before* B on the walk
   down, and we will hit A *after* B on the walk back up

   2. Node A is below node B (and thus, node B dominates node A)

    / \
   C   D

   In the above case, DFS_Number_In of A will be >= DFS_Number_In of
   B, and DFS_Number_Out of A will be <= DFS_Number_Out of B.

   This is because we must hit A in the dominator tree *after* B on
   the walk down, and we will hit A *before* B on the walk back up

   3. Node A and B are siblings (and thus, neither dominates the other)

    / \
   A   B

   In the above case, DFS_Number_In of A will *always* be <=
   DFS_Number_In of B, and DFS_Number_Out of A will *always* be <=
   DFS_Number_Out of B.  This is because we will always finish the dfs
   walk of one of the subtrees before the other, and thus, the dfs
   numbers for one subtree can't intersect with the range of dfs
   numbers for the other subtree.  If you swap A and B's position in
   the dominator tree, the comparison changes direction, but the point
   is that both comparisons will always go the same way if there is no
   dominance relationship.

   Thus, it is sufficient to write

   A_Dominates_B (node A, node B)
     return DFS_Number_In(A) <= DFS_Number_In(B)
            && DFS_Number_Out (A) >= DFS_Number_Out(B);

   A_Dominated_by_B (node A, node B)
     return DFS_Number_In(A) >= DFS_Number_In(A)
            && DFS_Number_Out (A) <= DFS_Number_Out(B);
   Return TRUE in case BB1 is dominated by BB2.  
static TBB eval ( struct dom_info ,

Referenced by gimplify_expr().

static TBB eval ( )
   Compress the path from V to the set root of V if needed (when the root has
   changed since the last call).  Returns the node with the smallest key[]
   value on the path from V to the root.  
     The representative of the set V is in, also called root (as the set
     representation is a tree).  
     V itself is the root.  
     Compress only if necessary.  
basic_block first_dom_son ( )
   Returns the first son of BB in the dominator or postdominator tree
   as determined by DIR.  

References debug_dominance_tree_1().

static void free_dom_info ( struct dom_info )
static void free_dom_info ( )
   Free all allocated memory in DI, but not DI itself.  
void free_dominance_info ( )
   Free dominance information for direction DIR.  

References et_node::data, basic_block_def::dom, dom_convert_dir_to_idx(), et_node::right, et_node::son, and vNULL.

vec<basic_block> get_all_dominated_blocks ( )
   Returns the list of basic blocks including BB dominated by BB, in the
   direction DIR.  The vector will be sorted in preorder.  
vec<basic_block> get_dominated_by ( )
   Returns the list of basic blocks immediately dominated by BB, in the
   direction DIR.  
vec<basic_block> get_dominated_by_region ( enum cdi_direction  dir,
basic_block region,
unsigned  n_region 
   Returns the list of basic blocks that are immediately dominated (in
   direction DIR) by some block between N_REGION ones stored in REGION,
   except for blocks in the REGION itself.  

References basic_block_def::dom, dom_convert_dir_to_idx(), DOM_NO_FAST_QUERY, DOM_OK, et_set_father(), et_split(), and et_node::son.

Referenced by move_stmt_r().

vec<basic_block> get_dominated_to_depth ( )
   Returns the list of basic blocks including BB dominated by BB, in the
   direction DIR up to DEPTH in the dominator tree.  The DEPTH of zero will
   produce a vector containing all dominated blocks.  The vector will be sorted
   in preorder.  

References et_node::data, basic_block_def::dom, dom_convert_dir_to_idx(), and et_nca().

basic_block get_immediate_dominator ( )
   Return the immediate dominator of basic block BB.  

References dom, first_dom_son(), basic_block_def::flags, next_dom_son(), and vNULL.

static void init_dom_info ( struct dom_info ,
enum  cdi_direction 
static void init_dom_info ( )
   Allocate all needed memory in a pessimistic fashion (so we round up).
   This initializes the contents of DI, which already must be allocated.  
     We need memory for n_basic_blocks nodes.  

References CDI_DOMINATORS, CDI_POST_DOMINATORS, and dom_info::fake_exit_edge.

void iterate_fix_dominators ( enum cdi_direction  dir,
vec< basic_block bbs,
bool  conservative 
   Recompute dominance information for basic blocks in the set BBS.  The
   function assumes that the immediate dominators of all the other blocks
   in CFG are correct, and that there are no unreachable blocks.

   If CONSERVATIVE is true, we additionally assume that all the ancestors of
   a block of BBS in the current dominance tree dominate it.  
     We only support updating dominators.  There are some problems with
     updating postdominators (need to add fake edges from infinite loops
     and noreturn functions), and since we do not currently use
     iterate_fix_dominators for postdominators, any attempt to handle these
     problems would be unused, untested, and almost surely buggy.  We keep
     the DIR argument for consistency with the rest of the dominator analysis
     The algorithm we use takes inspiration from the following papers, although
     the details are quite different from any of them:

     [1] G. Ramalingam, T. Reps, An Incremental Algorithm for Maintaining the
         Dominator Tree of a Reducible Flowgraph
     [2]  V. C. Sreedhar, G. R. Gao, Y.-F. Lee: Incremental computation of
          dominator trees
     [3]  K. D. Cooper, T. J. Harvey and K. Kennedy: A Simple, Fast Dominance

     First, we use the following heuristics to decrease the size of the BBS
       a) if BB has a single predecessor, then its immediate dominator is this
       additionally, if CONSERVATIVE is true:
       b) if all the predecessors of BB except for one (X) are dominated by BB,
          then X is the immediate dominator of BB
       c) if the nearest common ancestor of the predecessors of BB is X and
          X -> BB is an edge in CFG, then X is the immediate dominator of BB

     Then, we need to establish the dominance relation among the basic blocks
     in BBS.  We split the dominance tree by removing the immediate dominator
     edges from BBS, creating a forest F.  We form a graph G whose vertices
     are BBS and ENTRY and X -> Y is an edge of G if there exists an edge
     X' -> Y in CFG such that X' belongs to the tree of the dominance forest
     whose root is X.  We then determine dominance tree of G.  Note that
     for X, Y in BBS, X dominates Y in CFG if and only if X dominates Y in G.
     In this step, we can use arbitrary algorithm to determine dominators.
     We decided to prefer the algorithm [3] to the algorithm of
     Lengauer and Tarjan, since the set BBS is usually small (rarely exceeding
     10 during gcc bootstrap), and [3] should perform better in this case.

     Finally, we need to determine the immediate dominators for the basic
     blocks of BBS.  If the immediate dominator of X in G is Y, then
     the immediate dominator of X in CFG belongs to the tree of F rooted in
     Y.  We process the dominator tree T of G recursively, starting from leaves.
     Suppose that X_1, X_2, ..., X_k are the sons of Y in T, and that the
     subtrees of the dominance tree of CFG rooted in X_i are already correct.
     Let G' be the subgraph of G induced by {X_1, X_2, ..., X_k}.  We make
     the following observations:
       (i) the immediate dominator of all blocks in a strongly connected
           component of G' is the same
       (ii) if X has no predecessors in G', then the immediate dominator of X
            is the nearest common ancestor of the predecessors of X in the
            subtree of F rooted in Y
     Therefore, it suffices to find the topological ordering of G', and
     process the nodes X_i in this order using the rules (i) and (ii).
     Then, we contract all the nodes X_i with Y in G, so that the further
     steps work correctly.  
         Split the tree now.  If the idoms of blocks in BBS are not
         conservatively correct, setting the dominators using the
         heuristics in prune_bbs_to_update_dominators could
         create cycles in the dominance "tree", and cause ICE.  
     Construct the graph G.  
         If the dominance tree is conservatively correct, split it now.  
             Do not include parallel edges to G.  
     Find the dominator tree of G.  
     Finally, traverse the tree and find the immediate dominators.  
static void link_roots ( struct dom_info ,
TBB  ,
static void link_roots ( )
   This essentially merges the two sets of V and W, giving a single set with
   the new root V.  The internal representation of these disjoint sets is a
   balanced tree.  Currently link(V,W) is only used with V being the parent
   of W.  
     Rebalance the tree.  
     Merge all subtrees.  

References bitmap_bit_p(), edge_def::dest, dom_info::dfs_parent, dom_info::dfs_to_bb, ei_edge(), ei_end_p(), ei_next(), dom_info::fake_exit_edge, basic_block_def::index, edge_iterator::index, basic_block_def::preds, edge_def::src, and basic_block_def::succs.

basic_block nearest_common_dominator ( )
   Find first basic block in the tree dominating both BB1 and BB2.  
basic_block nearest_common_dominator_for_set ( )
   Find the nearest common dominator for the basic blocks in BLOCKS,
   using dominance direction DIR.  

References et_node::dfs_num_in, et_node::dfs_num_out, dom_convert_dir_to_idx(), DOM_OK, and et_below().

basic_block next_dom_son ( )
   Returns the next dominance son after BB in the dominator or postdominator
   tree as determined by DIR, or NULL if it was the last one.  
static void prune_bbs_to_update_dominators ( vec< basic_block bbs,
bool  conservative 
   Use simple heuristics (see iterate_fix_dominators) to determine dominators
   of BBS.  We assume that all the immediate dominators except for those of the
   blocks in BBS are correct.  If CONSERVATIVE is true, we also assume that the
   currently recorded immediate dominators of blocks in BBS really dominate the
   blocks.  The basic blocks for that we determine the dominator are removed
   from BBS.  

References CDI_DOMINATORS, identify_vertices(), recompute_dominator(), and set_immediate_dominator().

basic_block recompute_dominator ( )
   Determine immediate dominator (or postdominator, according to DIR) of BB,
   assuming that dominators of other blocks are correct.  We also use it to
   recompute the dominators in a restricted area, by iterating it until it
   reaches a fixed point.  

References CDI_DOMINATORS, and set_immediate_dominator().

void redirect_immediate_dominators ( enum cdi_direction  dir,
basic_block  bb,
basic_block  to 
   Redirect all edges pointing to BB to TO.  
static basic_block root_of_dom_tree ( )
   Returns root of the dominance tree in the direction DIR that contains
void set_dom_info_availability ( )
   Set the dominance availability for dominance info DIR to NEW_STATE.  
DEBUG_FUNCTION void verify_dominators ( )