GCC Middle and Back End API Reference
tree-ssa-sink.c File Reference

Functions

static basic_block find_bb_for_arg ()
static bool all_immediate_uses_same_place ()
static basic_block nearest_common_dominator_of_uses ()
static basic_block select_best_block (basic_block early_bb, basic_block late_bb, gimple stmt)
static bool statement_sink_location (gimple stmt, basic_block frombb, gimple_stmt_iterator *togsi)
static void sink_code_in_bb ()
static void execute_sink_code ()
static unsigned int do_sink ()
static bool gate_sink ()
gimple_opt_passmake_pass_sink_code ()

Variables

struct {
   int   sunk
sink_stats

Function Documentation

static bool all_immediate_uses_same_place ( )
static
   When the first immediate use is in a statement, then return true if all
   immediate uses in IMM are in the same statement.
   We could also do the case where  the first immediate use is in a phi node,
   and all the other uses are in phis in the same basic block, but this
   requires some expensive checking later (you have to make sure no def/vdef
   in the statement occurs for multiple edges in the various phi nodes it's
   used in, so that you only have one place you can sink it to.  

References is_gimple_debug().

static unsigned int do_sink ( )
static
   Gate and execute functions for PRE.  
static void execute_sink_code ( )
static
@verbatim 

Perform code sinking. This moves code down the flowgraph when we know it would be profitable to do so, or it wouldn't increase the number of executions of the statement.

IE given

a_1 = b + c; if (<something>) { } else { foo (&b, &c); a_5 = b + c; } a_6 = PHI (a_5, a_1); USE a_6.

we'll transform this into:

if (<something>) { a_1 = b + c; } else { foo (&b, &c); a_5 = b + c; } a_6 = PHI (a_5, a_1); USE a_6.

Note that this reduces the number of computations of a = b + c to 1 when we take the else edge, instead of 2.

References GIMPLE_PASS.

static basic_block find_bb_for_arg ( )
static
   Given a PHI, and one of its arguments (DEF), find the edge for
   that argument and return it.  If the argument occurs twice in the PHI node,
   we return NULL.  
static bool gate_sink ( )
static
gimple_opt_pass* make_pass_sink_code ( )
static basic_block nearest_common_dominator_of_uses ( )
static
   Find the nearest common dominator of all of the immediate uses in IMM.  
             Short circuit. Nothing dominates the entry block.  
static basic_block select_best_block ( basic_block  early_bb,
basic_block  late_bb,
gimple  stmt 
)
static
   Given EARLY_BB and LATE_BB, two blocks in a path through the dominator
   tree, return the best basic block between them (inclusive) to place
   statements.

   We want the most control dependent block in the shallowest loop nest.

   If the resulting block is in a shallower loop nest, then use it.  Else
   only use the resulting block if it has significantly lower execution
   frequency than EARLY_BB to avoid gratutious statement movement.  We
   consider statements with VOPS more desirable to move.

   This pass would obviously benefit from PDO as it utilizes block
   frequencies.  It would also benefit from recomputing frequencies
   if profile data is not available since frequencies often get out
   of sync with reality.  
         If we've moved into a lower loop nest, then that becomes
         our best block.  
         Walk up the dominator tree, hopefully we'll find a shallower
         loop nest.  
     If we found a shallower loop nest, then we always consider that
     a win.  This will always give us the most control dependent block
     within that loop nest.  
     Get the sinking threshold.  If the statement to be moved has memory
     operands, then increase the threshold by 7% as those are even more
     profitable to avoid, clamping at 100%.  
     If BEST_BB is at the same nesting level, then require it to have
     significantly lower execution frequency to avoid gratutious movement.  
     No better block found, so return EARLY_BB, which happens to be the
     statement's original block.  

References bb_loop_depth(), and get_immediate_dominator().

static void sink_code_in_bb ( )
static
   Perform code sinking on BB 
     If this block doesn't dominate anything, there can't be any place to sink
     the statements to.  
     We can't move things across abnormal edges, so don't try.  
         Update virtual operands of statements in the path we
         do not sink to.  
         If this is the end of the basic block, we need to insert at the end
         of the basic block.  
         If we've just removed the last statement of the BB, the
         gsi_end_p() test below would fail, but gsi_prev() would have
         succeeded, and we want it to succeed.  So we keep track of
         whether we're at the last statement and pick up the new last
         statement.  
static bool statement_sink_location ( gimple  stmt,
basic_block  frombb,
gimple_stmt_iterator togsi 
)
static
   Given a statement (STMT) and the basic block it is currently in (FROMBB),
   determine the location to sink the statement to, if any.
   Returns true if there is such location; in that case, TOGSI points to the
   statement before that STMT should be moved.  
     We only can sink assignments.  
     We only can sink stmts with a single definition.  
     Return if there are no immediate uses of this stmt.  
     There are a few classes of things we can't or don't move, some because we
     don't have code to handle it, some because it's not profitable and some
     because it's not legal.

     We can't sink things that may be global stores, at least not without
     calculating a lot more information, because we may cause it to no longer
     be seen by an external routine that needs it depending on where it gets
     moved to.

     We don't want to sink loads from memory.

     We can't sink statements that end basic blocks without splitting the
     incoming edge for the sink location to place it there.

     We can't sink statements that have volatile operands.

     We don't want to sink dead code, so anything with 0 immediate uses is not
     sunk.

     Don't sink BLKmode assignments if current function has any local explicit
     register variables, as BLKmode assignments may involve memcpy or memset
     calls or, on some targets, inline expansion thereof that sometimes need
     to use specific hard registers.
     If stmt is a store the one and only use needs to be the VOP
     merging PHI node.  
             A killing definition is not a use.  
                 If use_stmt is or might be a nop assignment then USE_STMT
                 acts as a use as well as definition.  
     If all the immediate uses are not in the same place, find the nearest
     common dominator of all the immediate uses.  For PHI nodes, we have to
     find the nearest common dominator of all of the predecessor blocks, since
     that is where insertion would have to take place.  
         Our common dominator has to be dominated by frombb in order to be a
         trivially safe place to put this statement, since it has multiple
         uses.  
     This can happen if there are multiple uses in a PHI.  
     If the latch block is empty, don't make it non-empty by sinking
     something into it.  

Variable Documentation

struct { ... } sink_stats
@verbatim 

Code sinking for trees Copyright (C) 2001-2013 Free Software Foundation, Inc. Contributed by Daniel Berlin dan@d.nosp@m.berl.nosp@m.in.or.nosp@m.g

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/.

   TODO:
   1. Sinking store only using scalar promotion (IE without moving the RHS):

   *q = p;
   p = p + 1;
   if (something)
     *q = <not p>;
   else
     y = *q;


   should become
   sinktemp = p;
   p = p + 1;
   if (something)
     *q = <not p>;
   else
   {
     *q = sinktemp;
     y = *q
   }
   Store copy propagation will take care of the store elimination above.


   2. Sinking using Partial Dead Code Elimination.  
int sunk
     The number of statements sunk down the flowgraph by code sinking.