Home | History | Annotate | Line # | Download | only in gcc
      1  1.1  mrg /* Thread edges through blocks and update the control flow and SSA graphs.
      2  1.1  mrg    Copyright (C) 2004-2022 Free Software Foundation, Inc.
      3  1.1  mrg 
      4  1.1  mrg This file is part of GCC.
      5  1.1  mrg 
      6  1.1  mrg GCC is free software; you can redistribute it and/or modify
      7  1.1  mrg it under the terms of the GNU General Public License as published by
      8  1.1  mrg the Free Software Foundation; either version 3, or (at your option)
      9  1.1  mrg any later version.
     10  1.1  mrg 
     11  1.1  mrg GCC is distributed in the hope that it will be useful,
     12  1.1  mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  1.1  mrg GNU General Public License for more details.
     15  1.1  mrg 
     16  1.1  mrg You should have received a copy of the GNU General Public License
     17  1.1  mrg along with GCC; see the file COPYING3.  If not see
     18  1.1  mrg <http://www.gnu.org/licenses/>.  */
     19  1.1  mrg 
     20  1.1  mrg #include "config.h"
     21  1.1  mrg #include "system.h"
     22  1.1  mrg #include "coretypes.h"
     23  1.1  mrg #include "backend.h"
     24  1.1  mrg #include "tree.h"
     25  1.1  mrg #include "gimple.h"
     26  1.1  mrg #include "cfghooks.h"
     27  1.1  mrg #include "tree-pass.h"
     28  1.1  mrg #include "ssa.h"
     29  1.1  mrg #include "fold-const.h"
     30  1.1  mrg #include "cfganal.h"
     31  1.1  mrg #include "gimple-iterator.h"
     32  1.1  mrg #include "tree-ssa.h"
     33  1.1  mrg #include "tree-ssa-threadupdate.h"
     34  1.1  mrg #include "cfgloop.h"
     35  1.1  mrg #include "dbgcnt.h"
     36  1.1  mrg #include "tree-cfg.h"
     37  1.1  mrg #include "tree-vectorizer.h"
     38  1.1  mrg #include "tree-pass.h"
     39  1.1  mrg 
     40  1.1  mrg /* Given a block B, update the CFG and SSA graph to reflect redirecting
     41  1.1  mrg    one or more in-edges to B to instead reach the destination of an
     42  1.1  mrg    out-edge from B while preserving any side effects in B.
     43  1.1  mrg 
     44  1.1  mrg    i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
     45  1.1  mrg    side effects of executing B.
     46  1.1  mrg 
     47  1.1  mrg      1. Make a copy of B (including its outgoing edges and statements).  Call
     48  1.1  mrg 	the copy B'.  Note B' has no incoming edges or PHIs at this time.
     49  1.1  mrg 
     50  1.1  mrg      2. Remove the control statement at the end of B' and all outgoing edges
     51  1.1  mrg 	except B'->C.
     52  1.1  mrg 
     53  1.1  mrg      3. Add a new argument to each PHI in C with the same value as the existing
     54  1.1  mrg 	argument associated with edge B->C.  Associate the new PHI arguments
     55  1.1  mrg 	with the edge B'->C.
     56  1.1  mrg 
     57  1.1  mrg      4. For each PHI in B, find or create a PHI in B' with an identical
     58  1.1  mrg 	PHI_RESULT.  Add an argument to the PHI in B' which has the same
     59  1.1  mrg 	value as the PHI in B associated with the edge A->B.  Associate
     60  1.1  mrg 	the new argument in the PHI in B' with the edge A->B.
     61  1.1  mrg 
     62  1.1  mrg      5. Change the edge A->B to A->B'.
     63  1.1  mrg 
     64  1.1  mrg 	5a. This automatically deletes any PHI arguments associated with the
     65  1.1  mrg 	    edge A->B in B.
     66  1.1  mrg 
     67  1.1  mrg 	5b. This automatically associates each new argument added in step 4
     68  1.1  mrg 	    with the edge A->B'.
     69  1.1  mrg 
     70  1.1  mrg      6. Repeat for other incoming edges into B.
     71  1.1  mrg 
     72  1.1  mrg      7. Put the duplicated resources in B and all the B' blocks into SSA form.
     73  1.1  mrg 
     74  1.1  mrg    Note that block duplication can be minimized by first collecting the
     75  1.1  mrg    set of unique destination blocks that the incoming edges should
     76  1.1  mrg    be threaded to.
     77  1.1  mrg 
     78  1.1  mrg    We reduce the number of edges and statements we create by not copying all
     79  1.1  mrg    the outgoing edges and the control statement in step #1.  We instead create
     80  1.1  mrg    a template block without the outgoing edges and duplicate the template.
     81  1.1  mrg 
     82  1.1  mrg    Another case this code handles is threading through a "joiner" block.  In
     83  1.1  mrg    this case, we do not know the destination of the joiner block, but one
     84  1.1  mrg    of the outgoing edges from the joiner block leads to a threadable path.  This
     85  1.1  mrg    case largely works as outlined above, except the duplicate of the joiner
     86  1.1  mrg    block still contains a full set of outgoing edges and its control statement.
     87  1.1  mrg    We just redirect one of its outgoing edges to our jump threading path.  */
     88  1.1  mrg 
     89  1.1  mrg 
     90  1.1  mrg /* Steps #5 and #6 of the above algorithm are best implemented by walking
     91  1.1  mrg    all the incoming edges which thread to the same destination edge at
     92  1.1  mrg    the same time.  That avoids lots of table lookups to get information
     93  1.1  mrg    for the destination edge.
     94  1.1  mrg 
     95  1.1  mrg    To realize that implementation we create a list of incoming edges
     96  1.1  mrg    which thread to the same outgoing edge.  Thus to implement steps
     97  1.1  mrg    #5 and #6 we traverse our hash table of outgoing edge information.
     98  1.1  mrg    For each entry we walk the list of incoming edges which thread to
     99  1.1  mrg    the current outgoing edge.  */
    100  1.1  mrg 
    101  1.1  mrg struct el
    102  1.1  mrg {
    103  1.1  mrg   edge e;
    104  1.1  mrg   struct el *next;
    105  1.1  mrg };
    106  1.1  mrg 
    107  1.1  mrg /* Main data structure recording information regarding B's duplicate
    108  1.1  mrg    blocks.  */
    109  1.1  mrg 
    110  1.1  mrg /* We need to efficiently record the unique thread destinations of this
    111  1.1  mrg    block and specific information associated with those destinations.  We
    112  1.1  mrg    may have many incoming edges threaded to the same outgoing edge.  This
    113  1.1  mrg    can be naturally implemented with a hash table.  */
    114  1.1  mrg 
    115  1.1  mrg struct redirection_data : free_ptr_hash<redirection_data>
    116  1.1  mrg {
    117  1.1  mrg   /* We support wiring up two block duplicates in a jump threading path.
    118  1.1  mrg 
    119  1.1  mrg      One is a normal block copy where we remove the control statement
    120  1.1  mrg      and wire up its single remaining outgoing edge to the thread path.
    121  1.1  mrg 
    122  1.1  mrg      The other is a joiner block where we leave the control statement
    123  1.1  mrg      in place, but wire one of the outgoing edges to a thread path.
    124  1.1  mrg 
    125  1.1  mrg      In theory we could have multiple block duplicates in a jump
    126  1.1  mrg      threading path, but I haven't tried that.
    127  1.1  mrg 
    128  1.1  mrg      The duplicate blocks appear in this array in the same order in
    129  1.1  mrg      which they appear in the jump thread path.  */
    130  1.1  mrg   basic_block dup_blocks[2];
    131  1.1  mrg 
    132  1.1  mrg   vec<jump_thread_edge *> *path;
    133  1.1  mrg 
    134  1.1  mrg   /* A list of incoming edges which we want to thread to the
    135  1.1  mrg      same path.  */
    136  1.1  mrg   struct el *incoming_edges;
    137  1.1  mrg 
    138  1.1  mrg   /* hash_table support.  */
    139  1.1  mrg   static inline hashval_t hash (const redirection_data *);
    140  1.1  mrg   static inline int equal (const redirection_data *, const redirection_data *);
    141  1.1  mrg };
    142  1.1  mrg 
    143  1.1  mrg jump_thread_path_allocator::jump_thread_path_allocator ()
    144  1.1  mrg {
    145  1.1  mrg   obstack_init (&m_obstack);
    146  1.1  mrg }
    147  1.1  mrg 
    148  1.1  mrg jump_thread_path_allocator::~jump_thread_path_allocator ()
    149  1.1  mrg {
    150  1.1  mrg   obstack_free (&m_obstack, NULL);
    151  1.1  mrg }
    152  1.1  mrg 
    153  1.1  mrg jump_thread_edge *
    154  1.1  mrg jump_thread_path_allocator::allocate_thread_edge (edge e,
    155  1.1  mrg 						  jump_thread_edge_type type)
    156  1.1  mrg {
    157  1.1  mrg   void *r = obstack_alloc (&m_obstack, sizeof (jump_thread_edge));
    158  1.1  mrg   return new (r) jump_thread_edge (e, type);
    159  1.1  mrg }
    160  1.1  mrg 
    161  1.1  mrg vec<jump_thread_edge *> *
    162  1.1  mrg jump_thread_path_allocator::allocate_thread_path ()
    163  1.1  mrg {
    164  1.1  mrg   // ?? Since the paths live in an obstack, we should be able to remove all
    165  1.1  mrg   // references to path->release() throughout the code.
    166  1.1  mrg   void *r = obstack_alloc (&m_obstack, sizeof (vec <jump_thread_edge *>));
    167  1.1  mrg   return new (r) vec<jump_thread_edge *> ();
    168  1.1  mrg }
    169  1.1  mrg 
    170  1.1  mrg jt_path_registry::jt_path_registry (bool backedge_threads)
    171  1.1  mrg {
    172  1.1  mrg   m_paths.create (5);
    173  1.1  mrg   m_num_threaded_edges = 0;
    174  1.1  mrg   m_backedge_threads = backedge_threads;
    175  1.1  mrg }
    176  1.1  mrg 
    177  1.1  mrg jt_path_registry::~jt_path_registry ()
    178  1.1  mrg {
    179  1.1  mrg   m_paths.release ();
    180  1.1  mrg }
    181  1.1  mrg 
    182  1.1  mrg fwd_jt_path_registry::fwd_jt_path_registry ()
    183  1.1  mrg   : jt_path_registry (/*backedge_threads=*/false)
    184  1.1  mrg {
    185  1.1  mrg   m_removed_edges = new hash_table<struct removed_edges> (17);
    186  1.1  mrg   m_redirection_data = NULL;
    187  1.1  mrg }
    188  1.1  mrg 
    189  1.1  mrg fwd_jt_path_registry::~fwd_jt_path_registry ()
    190  1.1  mrg {
    191  1.1  mrg   delete m_removed_edges;
    192  1.1  mrg }
    193  1.1  mrg 
    194  1.1  mrg back_jt_path_registry::back_jt_path_registry ()
    195  1.1  mrg   : jt_path_registry (/*backedge_threads=*/true)
    196  1.1  mrg {
    197  1.1  mrg }
    198  1.1  mrg 
    199  1.1  mrg void
    200  1.1  mrg jt_path_registry::push_edge (vec<jump_thread_edge *> *path,
    201  1.1  mrg 			     edge e, jump_thread_edge_type type)
    202  1.1  mrg {
    203  1.1  mrg   jump_thread_edge *x =  m_allocator.allocate_thread_edge (e, type);
    204  1.1  mrg   path->safe_push (x);
    205  1.1  mrg }
    206  1.1  mrg 
    207  1.1  mrg vec<jump_thread_edge *> *
    208  1.1  mrg jt_path_registry::allocate_thread_path ()
    209  1.1  mrg {
    210  1.1  mrg   return m_allocator.allocate_thread_path ();
    211  1.1  mrg }
    212  1.1  mrg 
    213  1.1  mrg /* Dump a jump threading path, including annotations about each
    214  1.1  mrg    edge in the path.  */
    215  1.1  mrg 
    216  1.1  mrg static void
    217  1.1  mrg dump_jump_thread_path (FILE *dump_file,
    218  1.1  mrg 		       const vec<jump_thread_edge *> &path,
    219  1.1  mrg 		       bool registering)
    220  1.1  mrg {
    221  1.1  mrg   if (registering)
    222  1.1  mrg     fprintf (dump_file,
    223  1.1  mrg 	     "  [%u] Registering jump thread: (%d, %d) incoming edge; ",
    224  1.1  mrg 	     dbg_cnt_counter (registered_jump_thread),
    225  1.1  mrg 	     path[0]->e->src->index, path[0]->e->dest->index);
    226  1.1  mrg   else
    227  1.1  mrg     fprintf (dump_file,
    228  1.1  mrg 	     "  Cancelling jump thread: (%d, %d) incoming edge; ",
    229  1.1  mrg 	     path[0]->e->src->index, path[0]->e->dest->index);
    230  1.1  mrg 
    231  1.1  mrg   for (unsigned int i = 1; i < path.length (); i++)
    232  1.1  mrg     {
    233  1.1  mrg       /* We can get paths with a NULL edge when the final destination
    234  1.1  mrg 	 of a jump thread turns out to be a constant address.  We dump
    235  1.1  mrg 	 those paths when debugging, so we have to be prepared for that
    236  1.1  mrg 	 possibility here.  */
    237  1.1  mrg       if (path[i]->e == NULL)
    238  1.1  mrg 	continue;
    239  1.1  mrg 
    240  1.1  mrg       fprintf (dump_file, " (%d, %d) ",
    241  1.1  mrg 	       path[i]->e->src->index, path[i]->e->dest->index);
    242  1.1  mrg       switch (path[i]->type)
    243  1.1  mrg 	{
    244  1.1  mrg 	case EDGE_COPY_SRC_JOINER_BLOCK:
    245  1.1  mrg 	  fprintf (dump_file, "joiner");
    246  1.1  mrg 	  break;
    247  1.1  mrg 	case EDGE_COPY_SRC_BLOCK:
    248  1.1  mrg 	  fprintf (dump_file, "normal");
    249  1.1  mrg 	  break;
    250  1.1  mrg 	case EDGE_NO_COPY_SRC_BLOCK:
    251  1.1  mrg 	  fprintf (dump_file, "nocopy");
    252  1.1  mrg 	  break;
    253  1.1  mrg 	default:
    254  1.1  mrg 	  gcc_unreachable ();
    255  1.1  mrg 	}
    256  1.1  mrg 
    257  1.1  mrg       if ((path[i]->e->flags & EDGE_DFS_BACK) != 0)
    258  1.1  mrg 	fprintf (dump_file, " (back)");
    259  1.1  mrg     }
    260  1.1  mrg   fprintf (dump_file, "; \n");
    261  1.1  mrg }
    262  1.1  mrg 
    263  1.1  mrg DEBUG_FUNCTION void
    264  1.1  mrg debug (const vec<jump_thread_edge *> &path)
    265  1.1  mrg {
    266  1.1  mrg   dump_jump_thread_path (stderr, path, true);
    267  1.1  mrg }
    268  1.1  mrg 
    269  1.1  mrg DEBUG_FUNCTION void
    270  1.1  mrg debug (const vec<jump_thread_edge *> *path)
    271  1.1  mrg {
    272  1.1  mrg   debug (*path);
    273  1.1  mrg }
    274  1.1  mrg 
    275  1.1  mrg /* Release the memory associated with PATH, and if dumping is enabled,
    276  1.1  mrg    dump out the reason why the thread was canceled.  */
    277  1.1  mrg 
    278  1.1  mrg static void
    279  1.1  mrg cancel_thread (vec<jump_thread_edge *> *path, const char *reason = NULL)
    280  1.1  mrg {
    281  1.1  mrg   if (dump_file && (dump_flags & TDF_DETAILS))
    282  1.1  mrg     {
    283  1.1  mrg       if (reason)
    284  1.1  mrg 	fprintf (dump_file, "%s: ", reason);
    285  1.1  mrg 
    286  1.1  mrg       dump_jump_thread_path (dump_file, *path, false);
    287  1.1  mrg       fprintf (dump_file, "\n");
    288  1.1  mrg     }
    289  1.1  mrg   path->release ();
    290  1.1  mrg }
    291  1.1  mrg 
    292  1.1  mrg /* Simple hashing function.  For any given incoming edge E, we're going
    293  1.1  mrg    to be most concerned with the final destination of its jump thread
    294  1.1  mrg    path.  So hash on the block index of the final edge in the path.  */
    295  1.1  mrg 
    296  1.1  mrg inline hashval_t
    297  1.1  mrg redirection_data::hash (const redirection_data *p)
    298  1.1  mrg {
    299  1.1  mrg   vec<jump_thread_edge *> *path = p->path;
    300  1.1  mrg   return path->last ()->e->dest->index;
    301  1.1  mrg }
    302  1.1  mrg 
    303  1.1  mrg /* Given two hash table entries, return true if they have the same
    304  1.1  mrg    jump threading path.  */
    305  1.1  mrg inline int
    306  1.1  mrg redirection_data::equal (const redirection_data *p1, const redirection_data *p2)
    307  1.1  mrg {
    308  1.1  mrg   vec<jump_thread_edge *> *path1 = p1->path;
    309  1.1  mrg   vec<jump_thread_edge *> *path2 = p2->path;
    310  1.1  mrg 
    311  1.1  mrg   if (path1->length () != path2->length ())
    312  1.1  mrg     return false;
    313  1.1  mrg 
    314  1.1  mrg   for (unsigned int i = 1; i < path1->length (); i++)
    315  1.1  mrg     {
    316  1.1  mrg       if ((*path1)[i]->type != (*path2)[i]->type
    317  1.1  mrg 	  || (*path1)[i]->e != (*path2)[i]->e)
    318  1.1  mrg 	return false;
    319  1.1  mrg     }
    320  1.1  mrg 
    321  1.1  mrg   return true;
    322  1.1  mrg }
    323  1.1  mrg 
    324  1.1  mrg /* Data structure of information to pass to hash table traversal routines.  */
    325  1.1  mrg struct ssa_local_info_t
    326  1.1  mrg {
    327  1.1  mrg   /* The current block we are working on.  */
    328  1.1  mrg   basic_block bb;
    329  1.1  mrg 
    330  1.1  mrg   /* We only create a template block for the first duplicated block in a
    331  1.1  mrg      jump threading path as we may need many duplicates of that block.
    332  1.1  mrg 
    333  1.1  mrg      The second duplicate block in a path is specific to that path.  Creating
    334  1.1  mrg      and sharing a template for that block is considerably more difficult.  */
    335  1.1  mrg   basic_block template_block;
    336  1.1  mrg 
    337  1.1  mrg   /* If we append debug stmts to the template block after creating it,
    338  1.1  mrg      this iterator won't be the last one in the block, and further
    339  1.1  mrg      copies of the template block shouldn't get debug stmts after
    340  1.1  mrg      it.  */
    341  1.1  mrg   gimple_stmt_iterator template_last_to_copy;
    342  1.1  mrg 
    343  1.1  mrg   /* Blocks duplicated for the thread.  */
    344  1.1  mrg   bitmap duplicate_blocks;
    345  1.1  mrg 
    346  1.1  mrg   /* TRUE if we thread one or more jumps, FALSE otherwise.  */
    347  1.1  mrg   bool jumps_threaded;
    348  1.1  mrg 
    349  1.1  mrg   /* When we have multiple paths through a joiner which reach different
    350  1.1  mrg      final destinations, then we may need to correct for potential
    351  1.1  mrg      profile insanities.  */
    352  1.1  mrg   bool need_profile_correction;
    353  1.1  mrg 
    354  1.1  mrg   // Jump threading statistics.
    355  1.1  mrg   unsigned long num_threaded_edges;
    356  1.1  mrg };
    357  1.1  mrg 
    358  1.1  mrg /* When we start updating the CFG for threading, data necessary for jump
    359  1.1  mrg    threading is attached to the AUX field for the incoming edge.  Use these
    360  1.1  mrg    macros to access the underlying structure attached to the AUX field.  */
    361  1.1  mrg #define THREAD_PATH(E) ((vec<jump_thread_edge *> *)(E)->aux)
    362  1.1  mrg 
    363  1.1  mrg /* Remove the last statement in block BB if it is a control statement
    364  1.1  mrg    Also remove all outgoing edges except the edge which reaches DEST_BB.
    365  1.1  mrg    If DEST_BB is NULL, then remove all outgoing edges.  */
    366  1.1  mrg 
    367  1.1  mrg static void
    368  1.1  mrg remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
    369  1.1  mrg {
    370  1.1  mrg   gimple_stmt_iterator gsi;
    371  1.1  mrg   edge e;
    372  1.1  mrg   edge_iterator ei;
    373  1.1  mrg 
    374  1.1  mrg   gsi = gsi_last_bb (bb);
    375  1.1  mrg 
    376  1.1  mrg   /* If the duplicate ends with a control statement, then remove it.
    377  1.1  mrg 
    378  1.1  mrg      Note that if we are duplicating the template block rather than the
    379  1.1  mrg      original basic block, then the duplicate might not have any real
    380  1.1  mrg      statements in it.  */
    381  1.1  mrg   if (!gsi_end_p (gsi)
    382  1.1  mrg       && gsi_stmt (gsi)
    383  1.1  mrg       && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
    384  1.1  mrg 	  || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
    385  1.1  mrg 	  || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH))
    386  1.1  mrg     gsi_remove (&gsi, true);
    387  1.1  mrg 
    388  1.1  mrg   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
    389  1.1  mrg     {
    390  1.1  mrg       if (e->dest != dest_bb)
    391  1.1  mrg 	{
    392  1.1  mrg 	  free_dom_edge_info (e);
    393  1.1  mrg 	  remove_edge (e);
    394  1.1  mrg 	}
    395  1.1  mrg       else
    396  1.1  mrg 	{
    397  1.1  mrg 	  e->probability = profile_probability::always ();
    398  1.1  mrg 	  ei_next (&ei);
    399  1.1  mrg 	}
    400  1.1  mrg     }
    401  1.1  mrg 
    402  1.1  mrg   /* If the remaining edge is a loop exit, there must have
    403  1.1  mrg      a removed edge that was not a loop exit.
    404  1.1  mrg 
    405  1.1  mrg      In that case BB and possibly other blocks were previously
    406  1.1  mrg      in the loop, but are now outside the loop.  Thus, we need
    407  1.1  mrg      to update the loop structures.  */
    408  1.1  mrg   if (single_succ_p (bb)
    409  1.1  mrg       && loop_outer (bb->loop_father)
    410  1.1  mrg       && loop_exit_edge_p (bb->loop_father, single_succ_edge (bb)))
    411  1.1  mrg     loops_state_set (LOOPS_NEED_FIXUP);
    412  1.1  mrg }
    413  1.1  mrg 
    414  1.1  mrg /* Create a duplicate of BB.  Record the duplicate block in an array
    415  1.1  mrg    indexed by COUNT stored in RD.  */
    416  1.1  mrg 
    417  1.1  mrg static void
    418  1.1  mrg create_block_for_threading (basic_block bb,
    419  1.1  mrg 			    struct redirection_data *rd,
    420  1.1  mrg 			    unsigned int count,
    421  1.1  mrg 			    bitmap *duplicate_blocks)
    422  1.1  mrg {
    423  1.1  mrg   edge_iterator ei;
    424  1.1  mrg   edge e;
    425  1.1  mrg 
    426  1.1  mrg   /* We can use the generic block duplication code and simply remove
    427  1.1  mrg      the stuff we do not need.  */
    428  1.1  mrg   rd->dup_blocks[count] = duplicate_block (bb, NULL, NULL);
    429  1.1  mrg 
    430  1.1  mrg   FOR_EACH_EDGE (e, ei, rd->dup_blocks[count]->succs)
    431  1.1  mrg     {
    432  1.1  mrg       e->aux = NULL;
    433  1.1  mrg 
    434  1.1  mrg       /* If we duplicate a block with an outgoing edge marked as
    435  1.1  mrg 	 EDGE_IGNORE, we must clear EDGE_IGNORE so that it doesn't
    436  1.1  mrg 	 leak out of the current pass.
    437  1.1  mrg 
    438  1.1  mrg 	 It would be better to simplify switch statements and remove
    439  1.1  mrg 	 the edges before we get here, but the sequencing is nontrivial.  */
    440  1.1  mrg       e->flags &= ~EDGE_IGNORE;
    441  1.1  mrg     }
    442  1.1  mrg 
    443  1.1  mrg   /* Zero out the profile, since the block is unreachable for now.  */
    444  1.1  mrg   rd->dup_blocks[count]->count = profile_count::uninitialized ();
    445  1.1  mrg   if (duplicate_blocks)
    446  1.1  mrg     bitmap_set_bit (*duplicate_blocks, rd->dup_blocks[count]->index);
    447  1.1  mrg }
    448  1.1  mrg 
    449  1.1  mrg /* Given an outgoing edge E lookup and return its entry in our hash table.
    450  1.1  mrg 
    451  1.1  mrg    If INSERT is true, then we insert the entry into the hash table if
    452  1.1  mrg    it is not already present.  INCOMING_EDGE is added to the list of incoming
    453  1.1  mrg    edges associated with E in the hash table.  */
    454  1.1  mrg 
    455  1.1  mrg redirection_data *
    456  1.1  mrg fwd_jt_path_registry::lookup_redirection_data (edge e, insert_option insert)
    457  1.1  mrg {
    458  1.1  mrg   struct redirection_data **slot;
    459  1.1  mrg   struct redirection_data *elt;
    460  1.1  mrg   vec<jump_thread_edge *> *path = THREAD_PATH (e);
    461  1.1  mrg 
    462  1.1  mrg   /* Build a hash table element so we can see if E is already
    463  1.1  mrg      in the table.  */
    464  1.1  mrg   elt = XNEW (struct redirection_data);
    465  1.1  mrg   elt->path = path;
    466  1.1  mrg   elt->dup_blocks[0] = NULL;
    467  1.1  mrg   elt->dup_blocks[1] = NULL;
    468  1.1  mrg   elt->incoming_edges = NULL;
    469  1.1  mrg 
    470  1.1  mrg   slot = m_redirection_data->find_slot (elt, insert);
    471  1.1  mrg 
    472  1.1  mrg   /* This will only happen if INSERT is false and the entry is not
    473  1.1  mrg      in the hash table.  */
    474  1.1  mrg   if (slot == NULL)
    475  1.1  mrg     {
    476  1.1  mrg       free (elt);
    477  1.1  mrg       return NULL;
    478  1.1  mrg     }
    479  1.1  mrg 
    480  1.1  mrg   /* This will only happen if E was not in the hash table and
    481  1.1  mrg      INSERT is true.  */
    482  1.1  mrg   if (*slot == NULL)
    483  1.1  mrg     {
    484  1.1  mrg       *slot = elt;
    485  1.1  mrg       elt->incoming_edges = XNEW (struct el);
    486  1.1  mrg       elt->incoming_edges->e = e;
    487  1.1  mrg       elt->incoming_edges->next = NULL;
    488  1.1  mrg       return elt;
    489  1.1  mrg     }
    490  1.1  mrg   /* E was in the hash table.  */
    491  1.1  mrg   else
    492  1.1  mrg     {
    493  1.1  mrg       /* Free ELT as we do not need it anymore, we will extract the
    494  1.1  mrg 	 relevant entry from the hash table itself.  */
    495  1.1  mrg       free (elt);
    496  1.1  mrg 
    497  1.1  mrg       /* Get the entry stored in the hash table.  */
    498  1.1  mrg       elt = *slot;
    499  1.1  mrg 
    500  1.1  mrg       /* If insertion was requested, then we need to add INCOMING_EDGE
    501  1.1  mrg 	 to the list of incoming edges associated with E.  */
    502  1.1  mrg       if (insert)
    503  1.1  mrg 	{
    504  1.1  mrg 	  struct el *el = XNEW (struct el);
    505  1.1  mrg 	  el->next = elt->incoming_edges;
    506  1.1  mrg 	  el->e = e;
    507  1.1  mrg 	  elt->incoming_edges = el;
    508  1.1  mrg 	}
    509  1.1  mrg 
    510  1.1  mrg       return elt;
    511  1.1  mrg     }
    512  1.1  mrg }
    513  1.1  mrg 
    514  1.1  mrg /* Similar to copy_phi_args, except that the PHI arg exists, it just
    515  1.1  mrg    does not have a value associated with it.  */
    516  1.1  mrg 
    517  1.1  mrg static void
    518  1.1  mrg copy_phi_arg_into_existing_phi (edge src_e, edge tgt_e)
    519  1.1  mrg {
    520  1.1  mrg   int src_idx = src_e->dest_idx;
    521  1.1  mrg   int tgt_idx = tgt_e->dest_idx;
    522  1.1  mrg 
    523  1.1  mrg   /* Iterate over each PHI in e->dest.  */
    524  1.1  mrg   for (gphi_iterator gsi = gsi_start_phis (src_e->dest),
    525  1.1  mrg 			   gsi2 = gsi_start_phis (tgt_e->dest);
    526  1.1  mrg        !gsi_end_p (gsi);
    527  1.1  mrg        gsi_next (&gsi), gsi_next (&gsi2))
    528  1.1  mrg     {
    529  1.1  mrg       gphi *src_phi = gsi.phi ();
    530  1.1  mrg       gphi *dest_phi = gsi2.phi ();
    531  1.1  mrg       tree val = gimple_phi_arg_def (src_phi, src_idx);
    532  1.1  mrg       location_t locus = gimple_phi_arg_location (src_phi, src_idx);
    533  1.1  mrg 
    534  1.1  mrg       SET_PHI_ARG_DEF (dest_phi, tgt_idx, val);
    535  1.1  mrg       gimple_phi_arg_set_location (dest_phi, tgt_idx, locus);
    536  1.1  mrg     }
    537  1.1  mrg }
    538  1.1  mrg 
    539  1.1  mrg /* Given ssa_name DEF, backtrack jump threading PATH from node IDX
    540  1.1  mrg    to see if it has constant value in a flow sensitive manner.  Set
    541  1.1  mrg    LOCUS to location of the constant phi arg and return the value.
    542  1.1  mrg    Return DEF directly if either PATH or idx is ZERO.  */
    543  1.1  mrg 
    544  1.1  mrg static tree
    545  1.1  mrg get_value_locus_in_path (tree def, vec<jump_thread_edge *> *path,
    546  1.1  mrg 			 basic_block bb, int idx, location_t *locus)
    547  1.1  mrg {
    548  1.1  mrg   tree arg;
    549  1.1  mrg   gphi *def_phi;
    550  1.1  mrg   basic_block def_bb;
    551  1.1  mrg 
    552  1.1  mrg   if (path == NULL || idx == 0)
    553  1.1  mrg     return def;
    554  1.1  mrg 
    555  1.1  mrg   def_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (def));
    556  1.1  mrg   if (!def_phi)
    557  1.1  mrg     return def;
    558  1.1  mrg 
    559  1.1  mrg   def_bb = gimple_bb (def_phi);
    560  1.1  mrg   /* Don't propagate loop invariants into deeper loops.  */
    561  1.1  mrg   if (!def_bb || bb_loop_depth (def_bb) < bb_loop_depth (bb))
    562  1.1  mrg     return def;
    563  1.1  mrg 
    564  1.1  mrg   /* Backtrack jump threading path from IDX to see if def has constant
    565  1.1  mrg      value.  */
    566  1.1  mrg   for (int j = idx - 1; j >= 0; j--)
    567  1.1  mrg     {
    568  1.1  mrg       edge e = (*path)[j]->e;
    569  1.1  mrg       if (e->dest == def_bb)
    570  1.1  mrg 	{
    571  1.1  mrg 	  arg = gimple_phi_arg_def (def_phi, e->dest_idx);
    572  1.1  mrg 	  if (is_gimple_min_invariant (arg))
    573  1.1  mrg 	    {
    574  1.1  mrg 	      *locus = gimple_phi_arg_location (def_phi, e->dest_idx);
    575  1.1  mrg 	      return arg;
    576  1.1  mrg 	    }
    577  1.1  mrg 	  break;
    578  1.1  mrg 	}
    579  1.1  mrg     }
    580  1.1  mrg 
    581  1.1  mrg   return def;
    582  1.1  mrg }
    583  1.1  mrg 
    584  1.1  mrg /* For each PHI in BB, copy the argument associated with SRC_E to TGT_E.
    585  1.1  mrg    Try to backtrack jump threading PATH from node IDX to see if the arg
    586  1.1  mrg    has constant value, copy constant value instead of argument itself
    587  1.1  mrg    if yes.  */
    588  1.1  mrg 
    589  1.1  mrg static void
    590  1.1  mrg copy_phi_args (basic_block bb, edge src_e, edge tgt_e,
    591  1.1  mrg 	       vec<jump_thread_edge *> *path, int idx)
    592  1.1  mrg {
    593  1.1  mrg   gphi_iterator gsi;
    594  1.1  mrg   int src_indx = src_e->dest_idx;
    595  1.1  mrg 
    596  1.1  mrg   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    597  1.1  mrg     {
    598  1.1  mrg       gphi *phi = gsi.phi ();
    599  1.1  mrg       tree def = gimple_phi_arg_def (phi, src_indx);
    600  1.1  mrg       location_t locus = gimple_phi_arg_location (phi, src_indx);
    601  1.1  mrg 
    602  1.1  mrg       if (TREE_CODE (def) == SSA_NAME
    603  1.1  mrg 	  && !virtual_operand_p (gimple_phi_result (phi)))
    604  1.1  mrg 	def = get_value_locus_in_path (def, path, bb, idx, &locus);
    605  1.1  mrg 
    606  1.1  mrg       add_phi_arg (phi, def, tgt_e, locus);
    607  1.1  mrg     }
    608  1.1  mrg }
    609  1.1  mrg 
    610  1.1  mrg /* We have recently made a copy of ORIG_BB, including its outgoing
    611  1.1  mrg    edges.  The copy is NEW_BB.  Every PHI node in every direct successor of
    612  1.1  mrg    ORIG_BB has a new argument associated with edge from NEW_BB to the
    613  1.1  mrg    successor.  Initialize the PHI argument so that it is equal to the PHI
    614  1.1  mrg    argument associated with the edge from ORIG_BB to the successor.
    615  1.1  mrg    PATH and IDX are used to check if the new PHI argument has constant
    616  1.1  mrg    value in a flow sensitive manner.  */
    617  1.1  mrg 
    618  1.1  mrg static void
    619  1.1  mrg update_destination_phis (basic_block orig_bb, basic_block new_bb,
    620  1.1  mrg 			 vec<jump_thread_edge *> *path, int idx)
    621  1.1  mrg {
    622  1.1  mrg   edge_iterator ei;
    623  1.1  mrg   edge e;
    624  1.1  mrg 
    625  1.1  mrg   FOR_EACH_EDGE (e, ei, orig_bb->succs)
    626  1.1  mrg     {
    627  1.1  mrg       edge e2 = find_edge (new_bb, e->dest);
    628  1.1  mrg       copy_phi_args (e->dest, e, e2, path, idx);
    629  1.1  mrg     }
    630  1.1  mrg }
    631  1.1  mrg 
    632  1.1  mrg /* Given a duplicate block and its single destination (both stored
    633  1.1  mrg    in RD).  Create an edge between the duplicate and its single
    634  1.1  mrg    destination.
    635  1.1  mrg 
    636  1.1  mrg    Add an additional argument to any PHI nodes at the single
    637  1.1  mrg    destination.  IDX is the start node in jump threading path
    638  1.1  mrg    we start to check to see if the new PHI argument has constant
    639  1.1  mrg    value along the jump threading path.  */
    640  1.1  mrg 
    641  1.1  mrg static void
    642  1.1  mrg create_edge_and_update_destination_phis (struct redirection_data *rd,
    643  1.1  mrg 					 basic_block bb, int idx)
    644  1.1  mrg {
    645  1.1  mrg   edge e = make_single_succ_edge (bb, rd->path->last ()->e->dest, EDGE_FALLTHRU);
    646  1.1  mrg 
    647  1.1  mrg   rescan_loop_exit (e, true, false);
    648  1.1  mrg 
    649  1.1  mrg   /* We used to copy the thread path here.  That was added in 2007
    650  1.1  mrg      and dutifully updated through the representation changes in 2013.
    651  1.1  mrg 
    652  1.1  mrg      In 2013 we added code to thread from an interior node through
    653  1.1  mrg      the backedge to another interior node.  That runs after the code
    654  1.1  mrg      to thread through loop headers from outside the loop.
    655  1.1  mrg 
    656  1.1  mrg      The latter may delete edges in the CFG, including those
    657  1.1  mrg      which appeared in the jump threading path we copied here.  Thus
    658  1.1  mrg      we'd end up using a dangling pointer.
    659  1.1  mrg 
    660  1.1  mrg      After reviewing the 2007/2011 code, I can't see how anything
    661  1.1  mrg      depended on copying the AUX field and clearly copying the jump
    662  1.1  mrg      threading path is problematical due to embedded edge pointers.
    663  1.1  mrg      It has been removed.  */
    664  1.1  mrg   e->aux = NULL;
    665  1.1  mrg 
    666  1.1  mrg   /* If there are any PHI nodes at the destination of the outgoing edge
    667  1.1  mrg      from the duplicate block, then we will need to add a new argument
    668  1.1  mrg      to them.  The argument should have the same value as the argument
    669  1.1  mrg      associated with the outgoing edge stored in RD.  */
    670  1.1  mrg   copy_phi_args (e->dest, rd->path->last ()->e, e, rd->path, idx);
    671  1.1  mrg }
    672  1.1  mrg 
    673  1.1  mrg /* Look through PATH beginning at START and return TRUE if there are
    674  1.1  mrg    any additional blocks that need to be duplicated.  Otherwise,
    675  1.1  mrg    return FALSE.  */
    676  1.1  mrg static bool
    677  1.1  mrg any_remaining_duplicated_blocks (vec<jump_thread_edge *> *path,
    678  1.1  mrg 				 unsigned int start)
    679  1.1  mrg {
    680  1.1  mrg   for (unsigned int i = start + 1; i < path->length (); i++)
    681  1.1  mrg     {
    682  1.1  mrg       if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK
    683  1.1  mrg 	  || (*path)[i]->type == EDGE_COPY_SRC_BLOCK)
    684  1.1  mrg 	return true;
    685  1.1  mrg     }
    686  1.1  mrg   return false;
    687  1.1  mrg }
    688  1.1  mrg 
    689  1.1  mrg 
    690  1.1  mrg /* Compute the amount of profile count coming into the jump threading
    691  1.1  mrg    path stored in RD that we are duplicating, returned in PATH_IN_COUNT_PTR and
    692  1.1  mrg    PATH_IN_FREQ_PTR, as well as the amount of counts flowing out of the
    693  1.1  mrg    duplicated path, returned in PATH_OUT_COUNT_PTR.  LOCAL_INFO is used to
    694  1.1  mrg    identify blocks duplicated for jump threading, which have duplicated
    695  1.1  mrg    edges that need to be ignored in the analysis.  Return true if path contains
    696  1.1  mrg    a joiner, false otherwise.
    697  1.1  mrg 
    698  1.1  mrg    In the non-joiner case, this is straightforward - all the counts
    699  1.1  mrg    flowing into the jump threading path should flow through the duplicated
    700  1.1  mrg    block and out of the duplicated path.
    701  1.1  mrg 
    702  1.1  mrg    In the joiner case, it is very tricky.  Some of the counts flowing into
    703  1.1  mrg    the original path go offpath at the joiner.  The problem is that while
    704  1.1  mrg    we know how much total count goes off-path in the original control flow,
    705  1.1  mrg    we don't know how many of the counts corresponding to just the jump
    706  1.1  mrg    threading path go offpath at the joiner.
    707  1.1  mrg 
    708  1.1  mrg    For example, assume we have the following control flow and identified
    709  1.1  mrg    jump threading paths:
    710  1.1  mrg 
    711  1.1  mrg 		A     B     C
    712  1.1  mrg 		 \    |    /
    713  1.1  mrg 	       Ea \   |Eb / Ec
    714  1.1  mrg 		   \  |  /
    715  1.1  mrg 		    v v v
    716  1.1  mrg 		      J       <-- Joiner
    717  1.1  mrg 		     / \
    718  1.1  mrg 		Eoff/   \Eon
    719  1.1  mrg 		   /     \
    720  1.1  mrg 		  v       v
    721  1.1  mrg 		Soff     Son  <--- Normal
    722  1.1  mrg 			 /\
    723  1.1  mrg 		      Ed/  \ Ee
    724  1.1  mrg 		       /    \
    725  1.1  mrg 		      v     v
    726  1.1  mrg 		      D      E
    727  1.1  mrg 
    728  1.1  mrg 	    Jump threading paths: A -> J -> Son -> D (path 1)
    729  1.1  mrg 				  C -> J -> Son -> E (path 2)
    730  1.1  mrg 
    731  1.1  mrg    Note that the control flow could be more complicated:
    732  1.1  mrg    - Each jump threading path may have more than one incoming edge.  I.e. A and
    733  1.1  mrg    Ea could represent multiple incoming blocks/edges that are included in
    734  1.1  mrg    path 1.
    735  1.1  mrg    - There could be EDGE_NO_COPY_SRC_BLOCK edges after the joiner (either
    736  1.1  mrg    before or after the "normal" copy block).  These are not duplicated onto
    737  1.1  mrg    the jump threading path, as they are single-successor.
    738  1.1  mrg    - Any of the blocks along the path may have other incoming edges that
    739  1.1  mrg    are not part of any jump threading path, but add profile counts along
    740  1.1  mrg    the path.
    741  1.1  mrg 
    742  1.1  mrg    In the above example, after all jump threading is complete, we will
    743  1.1  mrg    end up with the following control flow:
    744  1.1  mrg 
    745  1.1  mrg 		A	   B	       C
    746  1.1  mrg 		|	   |	       |
    747  1.1  mrg 	      Ea|	   |Eb	       |Ec
    748  1.1  mrg 		|	   |	       |
    749  1.1  mrg 		v	   v	       v
    750  1.1  mrg 	       Ja	   J	      Jc
    751  1.1  mrg 	       / \	  / \Eon'     / \
    752  1.1  mrg 	  Eona/   \   ---/---\--------   \Eonc
    753  1.1  mrg 	     /     \ /  /     \		  \
    754  1.1  mrg 	    v       v  v       v	  v
    755  1.1  mrg 	   Sona     Soff      Son	Sonc
    756  1.1  mrg 	     \		       /\	  /
    757  1.1  mrg 	      \___________    /  \  _____/
    758  1.1  mrg 			  \  /    \/
    759  1.1  mrg 			   vv      v
    760  1.1  mrg 			    D      E
    761  1.1  mrg 
    762  1.1  mrg    The main issue to notice here is that when we are processing path 1
    763  1.1  mrg    (A->J->Son->D) we need to figure out the outgoing edge weights to
    764  1.1  mrg    the duplicated edges Ja->Sona and Ja->Soff, while ensuring that the
    765  1.1  mrg    sum of the incoming weights to D remain Ed.  The problem with simply
    766  1.1  mrg    assuming that Ja (and Jc when processing path 2) has the same outgoing
    767  1.1  mrg    probabilities to its successors as the original block J, is that after
    768  1.1  mrg    all paths are processed and other edges/counts removed (e.g. none
    769  1.1  mrg    of Ec will reach D after processing path 2), we may end up with not
    770  1.1  mrg    enough count flowing along duplicated edge Sona->D.
    771  1.1  mrg 
    772  1.1  mrg    Therefore, in the case of a joiner, we keep track of all counts
    773  1.1  mrg    coming in along the current path, as well as from predecessors not
    774  1.1  mrg    on any jump threading path (Eb in the above example).  While we
    775  1.1  mrg    first assume that the duplicated Eona for Ja->Sona has the same
    776  1.1  mrg    probability as the original, we later compensate for other jump
    777  1.1  mrg    threading paths that may eliminate edges.  We do that by keep track
    778  1.1  mrg    of all counts coming into the original path that are not in a jump
    779  1.1  mrg    thread (Eb in the above example, but as noted earlier, there could
    780  1.1  mrg    be other predecessors incoming to the path at various points, such
    781  1.1  mrg    as at Son).  Call this cumulative non-path count coming into the path
    782  1.1  mrg    before D as Enonpath.  We then ensure that the count from Sona->D is as at
    783  1.1  mrg    least as big as (Ed - Enonpath), but no bigger than the minimum
    784  1.1  mrg    weight along the jump threading path.  The probabilities of both the
    785  1.1  mrg    original and duplicated joiner block J and Ja will be adjusted
    786  1.1  mrg    accordingly after the updates.  */
    787  1.1  mrg 
    788  1.1  mrg static bool
    789  1.1  mrg compute_path_counts (struct redirection_data *rd,
    790  1.1  mrg 		     ssa_local_info_t *local_info,
    791  1.1  mrg 		     profile_count *path_in_count_ptr,
    792  1.1  mrg 		     profile_count *path_out_count_ptr)
    793  1.1  mrg {
    794  1.1  mrg   edge e = rd->incoming_edges->e;
    795  1.1  mrg   vec<jump_thread_edge *> *path = THREAD_PATH (e);
    796  1.1  mrg   edge elast = path->last ()->e;
    797  1.1  mrg   profile_count nonpath_count = profile_count::zero ();
    798  1.1  mrg   bool has_joiner = false;
    799  1.1  mrg   profile_count path_in_count = profile_count::zero ();
    800  1.1  mrg 
    801  1.1  mrg   /* Start by accumulating incoming edge counts to the path's first bb
    802  1.1  mrg      into a couple buckets:
    803  1.1  mrg 	path_in_count: total count of incoming edges that flow into the
    804  1.1  mrg 		  current path.
    805  1.1  mrg 	nonpath_count: total count of incoming edges that are not
    806  1.1  mrg 		  flowing along *any* path.  These are the counts
    807  1.1  mrg 		  that will still flow along the original path after
    808  1.1  mrg 		  all path duplication is done by potentially multiple
    809  1.1  mrg 		  calls to this routine.
    810  1.1  mrg      (any other incoming edge counts are for a different jump threading
    811  1.1  mrg      path that will be handled by a later call to this routine.)
    812  1.1  mrg      To make this easier, start by recording all incoming edges that flow into
    813  1.1  mrg      the current path in a bitmap.  We could add up the path's incoming edge
    814  1.1  mrg      counts here, but we still need to walk all the first bb's incoming edges
    815  1.1  mrg      below to add up the counts of the other edges not included in this jump
    816  1.1  mrg      threading path.  */
    817  1.1  mrg   struct el *next, *el;
    818  1.1  mrg   auto_bitmap in_edge_srcs;
    819  1.1  mrg   for (el = rd->incoming_edges; el; el = next)
    820  1.1  mrg     {
    821  1.1  mrg       next = el->next;
    822  1.1  mrg       bitmap_set_bit (in_edge_srcs, el->e->src->index);
    823  1.1  mrg     }
    824  1.1  mrg   edge ein;
    825  1.1  mrg   edge_iterator ei;
    826  1.1  mrg   FOR_EACH_EDGE (ein, ei, e->dest->preds)
    827  1.1  mrg     {
    828  1.1  mrg       vec<jump_thread_edge *> *ein_path = THREAD_PATH (ein);
    829  1.1  mrg       /* Simply check the incoming edge src against the set captured above.  */
    830  1.1  mrg       if (ein_path
    831  1.1  mrg 	  && bitmap_bit_p (in_edge_srcs, (*ein_path)[0]->e->src->index))
    832  1.1  mrg 	{
    833  1.1  mrg 	  /* It is necessary but not sufficient that the last path edges
    834  1.1  mrg 	     are identical.  There may be different paths that share the
    835  1.1  mrg 	     same last path edge in the case where the last edge has a nocopy
    836  1.1  mrg 	     source block.  */
    837  1.1  mrg 	  gcc_assert (ein_path->last ()->e == elast);
    838  1.1  mrg 	  path_in_count += ein->count ();
    839  1.1  mrg 	}
    840  1.1  mrg       else if (!ein_path)
    841  1.1  mrg 	{
    842  1.1  mrg 	  /* Keep track of the incoming edges that are not on any jump-threading
    843  1.1  mrg 	     path.  These counts will still flow out of original path after all
    844  1.1  mrg 	     jump threading is complete.  */
    845  1.1  mrg 	    nonpath_count += ein->count ();
    846  1.1  mrg 	}
    847  1.1  mrg     }
    848  1.1  mrg 
    849  1.1  mrg   /* Now compute the fraction of the total count coming into the first
    850  1.1  mrg      path bb that is from the current threading path.  */
    851  1.1  mrg   profile_count total_count = e->dest->count;
    852  1.1  mrg   /* Handle incoming profile insanities.  */
    853  1.1  mrg   if (total_count < path_in_count)
    854  1.1  mrg     path_in_count = total_count;
    855  1.1  mrg   profile_probability onpath_scale = path_in_count.probability_in (total_count);
    856  1.1  mrg 
    857  1.1  mrg   /* Walk the entire path to do some more computation in order to estimate
    858  1.1  mrg      how much of the path_in_count will flow out of the duplicated threading
    859  1.1  mrg      path.  In the non-joiner case this is straightforward (it should be
    860  1.1  mrg      the same as path_in_count, although we will handle incoming profile
    861  1.1  mrg      insanities by setting it equal to the minimum count along the path).
    862  1.1  mrg 
    863  1.1  mrg      In the joiner case, we need to estimate how much of the path_in_count
    864  1.1  mrg      will stay on the threading path after the joiner's conditional branch.
    865  1.1  mrg      We don't really know for sure how much of the counts
    866  1.1  mrg      associated with this path go to each successor of the joiner, but we'll
    867  1.1  mrg      estimate based on the fraction of the total count coming into the path
    868  1.1  mrg      bb was from the threading paths (computed above in onpath_scale).
    869  1.1  mrg      Afterwards, we will need to do some fixup to account for other threading
    870  1.1  mrg      paths and possible profile insanities.
    871  1.1  mrg 
    872  1.1  mrg      In order to estimate the joiner case's counts we also need to update
    873  1.1  mrg      nonpath_count with any additional counts coming into the path.  Other
    874  1.1  mrg      blocks along the path may have additional predecessors from outside
    875  1.1  mrg      the path.  */
    876  1.1  mrg   profile_count path_out_count = path_in_count;
    877  1.1  mrg   profile_count min_path_count = path_in_count;
    878  1.1  mrg   for (unsigned int i = 1; i < path->length (); i++)
    879  1.1  mrg     {
    880  1.1  mrg       edge epath = (*path)[i]->e;
    881  1.1  mrg       profile_count cur_count = epath->count ();
    882  1.1  mrg       if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
    883  1.1  mrg 	{
    884  1.1  mrg 	  has_joiner = true;
    885  1.1  mrg 	  cur_count = cur_count.apply_probability (onpath_scale);
    886  1.1  mrg 	}
    887  1.1  mrg       /* In the joiner case we need to update nonpath_count for any edges
    888  1.1  mrg 	 coming into the path that will contribute to the count flowing
    889  1.1  mrg 	 into the path successor.  */
    890  1.1  mrg       if (has_joiner && epath != elast)
    891  1.1  mrg 	{
    892  1.1  mrg 	  /* Look for other incoming edges after joiner.  */
    893  1.1  mrg 	  FOR_EACH_EDGE (ein, ei, epath->dest->preds)
    894  1.1  mrg 	    {
    895  1.1  mrg 	      if (ein != epath
    896  1.1  mrg 		  /* Ignore in edges from blocks we have duplicated for a
    897  1.1  mrg 		     threading path, which have duplicated edge counts until
    898  1.1  mrg 		     they are redirected by an invocation of this routine.  */
    899  1.1  mrg 		  && !bitmap_bit_p (local_info->duplicate_blocks,
    900  1.1  mrg 				    ein->src->index))
    901  1.1  mrg 		nonpath_count += ein->count ();
    902  1.1  mrg 	    }
    903  1.1  mrg 	}
    904  1.1  mrg       if (cur_count < path_out_count)
    905  1.1  mrg 	path_out_count = cur_count;
    906  1.1  mrg       if (epath->count () < min_path_count)
    907  1.1  mrg 	min_path_count = epath->count ();
    908  1.1  mrg     }
    909  1.1  mrg 
    910  1.1  mrg   /* We computed path_out_count above assuming that this path targeted
    911  1.1  mrg      the joiner's on-path successor with the same likelihood as it
    912  1.1  mrg      reached the joiner.  However, other thread paths through the joiner
    913  1.1  mrg      may take a different path through the normal copy source block
    914  1.1  mrg      (i.e. they have a different elast), meaning that they do not
    915  1.1  mrg      contribute any counts to this path's elast.  As a result, it may
    916  1.1  mrg      turn out that this path must have more count flowing to the on-path
    917  1.1  mrg      successor of the joiner.  Essentially, all of this path's elast
    918  1.1  mrg      count must be contributed by this path and any nonpath counts
    919  1.1  mrg      (since any path through the joiner with a different elast will not
    920  1.1  mrg      include a copy of this elast in its duplicated path).
    921  1.1  mrg      So ensure that this path's path_out_count is at least the
    922  1.1  mrg      difference between elast->count () and nonpath_count.  Otherwise the edge
    923  1.1  mrg      counts after threading will not be sane.  */
    924  1.1  mrg   if (local_info->need_profile_correction
    925  1.1  mrg       && has_joiner && path_out_count < elast->count () - nonpath_count)
    926  1.1  mrg     {
    927  1.1  mrg       path_out_count = elast->count () - nonpath_count;
    928  1.1  mrg       /* But neither can we go above the minimum count along the path
    929  1.1  mrg 	 we are duplicating.  This can be an issue due to profile
    930  1.1  mrg 	 insanities coming in to this pass.  */
    931  1.1  mrg       if (path_out_count > min_path_count)
    932  1.1  mrg 	path_out_count = min_path_count;
    933  1.1  mrg     }
    934  1.1  mrg 
    935  1.1  mrg   *path_in_count_ptr = path_in_count;
    936  1.1  mrg   *path_out_count_ptr = path_out_count;
    937  1.1  mrg   return has_joiner;
    938  1.1  mrg }
    939  1.1  mrg 
    940  1.1  mrg 
    941  1.1  mrg /* Update the counts and frequencies for both an original path
    942  1.1  mrg    edge EPATH and its duplicate EDUP.  The duplicate source block
    943  1.1  mrg    will get a count of PATH_IN_COUNT and PATH_IN_FREQ,
    944  1.1  mrg    and the duplicate edge EDUP will have a count of PATH_OUT_COUNT.  */
    945  1.1  mrg static void
    946  1.1  mrg update_profile (edge epath, edge edup, profile_count path_in_count,
    947  1.1  mrg 		profile_count path_out_count)
    948  1.1  mrg {
    949  1.1  mrg 
    950  1.1  mrg   /* First update the duplicated block's count.  */
    951  1.1  mrg   if (edup)
    952  1.1  mrg     {
    953  1.1  mrg       basic_block dup_block = edup->src;
    954  1.1  mrg 
    955  1.1  mrg       /* Edup's count is reduced by path_out_count.  We need to redistribute
    956  1.1  mrg          probabilities to the remaining edges.  */
    957  1.1  mrg 
    958  1.1  mrg       edge esucc;
    959  1.1  mrg       edge_iterator ei;
    960  1.1  mrg       profile_probability edup_prob
    961  1.1  mrg 	 = path_out_count.probability_in (path_in_count);
    962  1.1  mrg 
    963  1.1  mrg       /* Either scale up or down the remaining edges.
    964  1.1  mrg 	 probabilities are always in range <0,1> and thus we can't do
    965  1.1  mrg 	 both by same loop.  */
    966  1.1  mrg       if (edup->probability > edup_prob)
    967  1.1  mrg 	{
    968  1.1  mrg 	   profile_probability rev_scale
    969  1.1  mrg 	     = (profile_probability::always () - edup->probability)
    970  1.1  mrg 	       / (profile_probability::always () - edup_prob);
    971  1.1  mrg 	   FOR_EACH_EDGE (esucc, ei, dup_block->succs)
    972  1.1  mrg 	     if (esucc != edup)
    973  1.1  mrg 	       esucc->probability /= rev_scale;
    974  1.1  mrg 	}
    975  1.1  mrg       else if (edup->probability < edup_prob)
    976  1.1  mrg 	{
    977  1.1  mrg 	   profile_probability scale
    978  1.1  mrg 	     = (profile_probability::always () - edup_prob)
    979  1.1  mrg 	       / (profile_probability::always () - edup->probability);
    980  1.1  mrg 	  FOR_EACH_EDGE (esucc, ei, dup_block->succs)
    981  1.1  mrg 	    if (esucc != edup)
    982  1.1  mrg 	      esucc->probability *= scale;
    983  1.1  mrg 	}
    984  1.1  mrg       if (edup_prob.initialized_p ())
    985  1.1  mrg         edup->probability = edup_prob;
    986  1.1  mrg 
    987  1.1  mrg       gcc_assert (!dup_block->count.initialized_p ());
    988  1.1  mrg       dup_block->count = path_in_count;
    989  1.1  mrg     }
    990  1.1  mrg 
    991  1.1  mrg   if (path_in_count == profile_count::zero ())
    992  1.1  mrg     return;
    993  1.1  mrg 
    994  1.1  mrg   profile_count final_count = epath->count () - path_out_count;
    995  1.1  mrg 
    996  1.1  mrg   /* Now update the original block's count in the
    997  1.1  mrg      opposite manner - remove the counts/freq that will flow
    998  1.1  mrg      into the duplicated block.  Handle underflow due to precision/
    999  1.1  mrg      rounding issues.  */
   1000  1.1  mrg   epath->src->count -= path_in_count;
   1001  1.1  mrg 
   1002  1.1  mrg   /* Next update this path edge's original and duplicated counts.  We know
   1003  1.1  mrg      that the duplicated path will have path_out_count flowing
   1004  1.1  mrg      out of it (in the joiner case this is the count along the duplicated path
   1005  1.1  mrg      out of the duplicated joiner).  This count can then be removed from the
   1006  1.1  mrg      original path edge.  */
   1007  1.1  mrg 
   1008  1.1  mrg   edge esucc;
   1009  1.1  mrg   edge_iterator ei;
   1010  1.1  mrg   profile_probability epath_prob = final_count.probability_in (epath->src->count);
   1011  1.1  mrg 
   1012  1.1  mrg   if (epath->probability > epath_prob)
   1013  1.1  mrg     {
   1014  1.1  mrg        profile_probability rev_scale
   1015  1.1  mrg 	 = (profile_probability::always () - epath->probability)
   1016  1.1  mrg 	   / (profile_probability::always () - epath_prob);
   1017  1.1  mrg        FOR_EACH_EDGE (esucc, ei, epath->src->succs)
   1018  1.1  mrg 	 if (esucc != epath)
   1019  1.1  mrg 	   esucc->probability /= rev_scale;
   1020  1.1  mrg     }
   1021  1.1  mrg   else if (epath->probability < epath_prob)
   1022  1.1  mrg     {
   1023  1.1  mrg        profile_probability scale
   1024  1.1  mrg 	 = (profile_probability::always () - epath_prob)
   1025  1.1  mrg 	   / (profile_probability::always () - epath->probability);
   1026  1.1  mrg       FOR_EACH_EDGE (esucc, ei, epath->src->succs)
   1027  1.1  mrg 	if (esucc != epath)
   1028  1.1  mrg 	  esucc->probability *= scale;
   1029  1.1  mrg     }
   1030  1.1  mrg   if (epath_prob.initialized_p ())
   1031  1.1  mrg     epath->probability = epath_prob;
   1032  1.1  mrg }
   1033  1.1  mrg 
   1034  1.1  mrg /* Wire up the outgoing edges from the duplicate blocks and
   1035  1.1  mrg    update any PHIs as needed.  Also update the profile counts
   1036  1.1  mrg    on the original and duplicate blocks and edges.  */
   1037  1.1  mrg void
   1038  1.1  mrg ssa_fix_duplicate_block_edges (struct redirection_data *rd,
   1039  1.1  mrg 			       ssa_local_info_t *local_info)
   1040  1.1  mrg {
   1041  1.1  mrg   bool multi_incomings = (rd->incoming_edges->next != NULL);
   1042  1.1  mrg   edge e = rd->incoming_edges->e;
   1043  1.1  mrg   vec<jump_thread_edge *> *path = THREAD_PATH (e);
   1044  1.1  mrg   edge elast = path->last ()->e;
   1045  1.1  mrg   profile_count path_in_count = profile_count::zero ();
   1046  1.1  mrg   profile_count path_out_count = profile_count::zero ();
   1047  1.1  mrg 
   1048  1.1  mrg   /* First determine how much profile count to move from original
   1049  1.1  mrg      path to the duplicate path.  This is tricky in the presence of
   1050  1.1  mrg      a joiner (see comments for compute_path_counts), where some portion
   1051  1.1  mrg      of the path's counts will flow off-path from the joiner.  In the
   1052  1.1  mrg      non-joiner case the path_in_count and path_out_count should be the
   1053  1.1  mrg      same.  */
   1054  1.1  mrg   bool has_joiner = compute_path_counts (rd, local_info,
   1055  1.1  mrg 					 &path_in_count, &path_out_count);
   1056  1.1  mrg 
   1057  1.1  mrg   for (unsigned int count = 0, i = 1; i < path->length (); i++)
   1058  1.1  mrg     {
   1059  1.1  mrg       edge epath = (*path)[i]->e;
   1060  1.1  mrg 
   1061  1.1  mrg       /* If we were threading through an joiner block, then we want
   1062  1.1  mrg 	 to keep its control statement and redirect an outgoing edge.
   1063  1.1  mrg 	 Else we want to remove the control statement & edges, then create
   1064  1.1  mrg 	 a new outgoing edge.  In both cases we may need to update PHIs.  */
   1065  1.1  mrg       if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
   1066  1.1  mrg 	{
   1067  1.1  mrg 	  edge victim;
   1068  1.1  mrg 	  edge e2;
   1069  1.1  mrg 
   1070  1.1  mrg 	  gcc_assert (has_joiner);
   1071  1.1  mrg 
   1072  1.1  mrg 	  /* This updates the PHIs at the destination of the duplicate
   1073  1.1  mrg 	     block.  Pass 0 instead of i if we are threading a path which
   1074  1.1  mrg 	     has multiple incoming edges.  */
   1075  1.1  mrg 	  update_destination_phis (local_info->bb, rd->dup_blocks[count],
   1076  1.1  mrg 				   path, multi_incomings ? 0 : i);
   1077  1.1  mrg 
   1078  1.1  mrg 	  /* Find the edge from the duplicate block to the block we're
   1079  1.1  mrg 	     threading through.  That's the edge we want to redirect.  */
   1080  1.1  mrg 	  victim = find_edge (rd->dup_blocks[count], (*path)[i]->e->dest);
   1081  1.1  mrg 
   1082  1.1  mrg 	  /* If there are no remaining blocks on the path to duplicate,
   1083  1.1  mrg 	     then redirect VICTIM to the final destination of the jump
   1084  1.1  mrg 	     threading path.  */
   1085  1.1  mrg 	  if (!any_remaining_duplicated_blocks (path, i))
   1086  1.1  mrg 	    {
   1087  1.1  mrg 	      e2 = redirect_edge_and_branch (victim, elast->dest);
   1088  1.1  mrg 	      /* If we redirected the edge, then we need to copy PHI arguments
   1089  1.1  mrg 		 at the target.  If the edge already existed (e2 != victim
   1090  1.1  mrg 		 case), then the PHIs in the target already have the correct
   1091  1.1  mrg 		 arguments.  */
   1092  1.1  mrg 	      if (e2 == victim)
   1093  1.1  mrg 		copy_phi_args (e2->dest, elast, e2,
   1094  1.1  mrg 			       path, multi_incomings ? 0 : i);
   1095  1.1  mrg 	    }
   1096  1.1  mrg 	  else
   1097  1.1  mrg 	    {
   1098  1.1  mrg 	      /* Redirect VICTIM to the next duplicated block in the path.  */
   1099  1.1  mrg 	      e2 = redirect_edge_and_branch (victim, rd->dup_blocks[count + 1]);
   1100  1.1  mrg 
   1101  1.1  mrg 	      /* We need to update the PHIs in the next duplicated block.  We
   1102  1.1  mrg 		 want the new PHI args to have the same value as they had
   1103  1.1  mrg 		 in the source of the next duplicate block.
   1104  1.1  mrg 
   1105  1.1  mrg 		 Thus, we need to know which edge we traversed into the
   1106  1.1  mrg 		 source of the duplicate.  Furthermore, we may have
   1107  1.1  mrg 		 traversed many edges to reach the source of the duplicate.
   1108  1.1  mrg 
   1109  1.1  mrg 		 Walk through the path starting at element I until we
   1110  1.1  mrg 		 hit an edge marked with EDGE_COPY_SRC_BLOCK.  We want
   1111  1.1  mrg 		 the edge from the prior element.  */
   1112  1.1  mrg 	      for (unsigned int j = i + 1; j < path->length (); j++)
   1113  1.1  mrg 		{
   1114  1.1  mrg 		  if ((*path)[j]->type == EDGE_COPY_SRC_BLOCK)
   1115  1.1  mrg 		    {
   1116  1.1  mrg 		      copy_phi_arg_into_existing_phi ((*path)[j - 1]->e, e2);
   1117  1.1  mrg 		      break;
   1118  1.1  mrg 		    }
   1119  1.1  mrg 		}
   1120  1.1  mrg 	    }
   1121  1.1  mrg 
   1122  1.1  mrg 	  /* Update the counts of both the original block
   1123  1.1  mrg 	     and path edge, and the duplicates.  The path duplicate's
   1124  1.1  mrg 	     incoming count are the totals for all edges
   1125  1.1  mrg 	     incoming to this jump threading path computed earlier.
   1126  1.1  mrg 	     And we know that the duplicated path will have path_out_count
   1127  1.1  mrg 	     flowing out of it (i.e. along the duplicated path out of the
   1128  1.1  mrg 	     duplicated joiner).  */
   1129  1.1  mrg 	  update_profile (epath, e2, path_in_count, path_out_count);
   1130  1.1  mrg 	}
   1131  1.1  mrg       else if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK)
   1132  1.1  mrg 	{
   1133  1.1  mrg 	  remove_ctrl_stmt_and_useless_edges (rd->dup_blocks[count], NULL);
   1134  1.1  mrg 	  create_edge_and_update_destination_phis (rd, rd->dup_blocks[count],
   1135  1.1  mrg 						   multi_incomings ? 0 : i);
   1136  1.1  mrg 	  if (count == 1)
   1137  1.1  mrg 	    single_succ_edge (rd->dup_blocks[1])->aux = NULL;
   1138  1.1  mrg 
   1139  1.1  mrg 	  /* Update the counts of both the original block
   1140  1.1  mrg 	     and path edge, and the duplicates.  Since we are now after
   1141  1.1  mrg 	     any joiner that may have existed on the path, the count
   1142  1.1  mrg 	     flowing along the duplicated threaded path is path_out_count.
   1143  1.1  mrg 	     If we didn't have a joiner, then cur_path_freq was the sum
   1144  1.1  mrg 	     of the total frequencies along all incoming edges to the
   1145  1.1  mrg 	     thread path (path_in_freq).  If we had a joiner, it would have
   1146  1.1  mrg 	     been updated at the end of that handling to the edge frequency
   1147  1.1  mrg 	     along the duplicated joiner path edge.  */
   1148  1.1  mrg 	  update_profile (epath, EDGE_SUCC (rd->dup_blocks[count], 0),
   1149  1.1  mrg 			  path_out_count, path_out_count);
   1150  1.1  mrg 	}
   1151  1.1  mrg       else
   1152  1.1  mrg 	{
   1153  1.1  mrg 	  /* No copy case.  In this case we don't have an equivalent block
   1154  1.1  mrg 	     on the duplicated thread path to update, but we do need
   1155  1.1  mrg 	     to remove the portion of the counts/freqs that were moved
   1156  1.1  mrg 	     to the duplicated path from the counts/freqs flowing through
   1157  1.1  mrg 	     this block on the original path.  Since all the no-copy edges
   1158  1.1  mrg 	     are after any joiner, the removed count is the same as
   1159  1.1  mrg 	     path_out_count.
   1160  1.1  mrg 
   1161  1.1  mrg 	     If we didn't have a joiner, then cur_path_freq was the sum
   1162  1.1  mrg 	     of the total frequencies along all incoming edges to the
   1163  1.1  mrg 	     thread path (path_in_freq).  If we had a joiner, it would have
   1164  1.1  mrg 	     been updated at the end of that handling to the edge frequency
   1165  1.1  mrg 	     along the duplicated joiner path edge.  */
   1166  1.1  mrg 	   update_profile (epath, NULL, path_out_count, path_out_count);
   1167  1.1  mrg 	}
   1168  1.1  mrg 
   1169  1.1  mrg       /* Increment the index into the duplicated path when we processed
   1170  1.1  mrg 	 a duplicated block.  */
   1171  1.1  mrg       if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK
   1172  1.1  mrg 	  || (*path)[i]->type == EDGE_COPY_SRC_BLOCK)
   1173  1.1  mrg 	{
   1174  1.1  mrg 	  count++;
   1175  1.1  mrg 	}
   1176  1.1  mrg     }
   1177  1.1  mrg }
   1178  1.1  mrg 
   1179  1.1  mrg /* Hash table traversal callback routine to create duplicate blocks.  */
   1180  1.1  mrg 
   1181  1.1  mrg int
   1182  1.1  mrg ssa_create_duplicates (struct redirection_data **slot,
   1183  1.1  mrg 		       ssa_local_info_t *local_info)
   1184  1.1  mrg {
   1185  1.1  mrg   struct redirection_data *rd = *slot;
   1186  1.1  mrg 
   1187  1.1  mrg   /* The second duplicated block in a jump threading path is specific
   1188  1.1  mrg      to the path.  So it gets stored in RD rather than in LOCAL_DATA.
   1189  1.1  mrg 
   1190  1.1  mrg      Each time we're called, we have to look through the path and see
   1191  1.1  mrg      if a second block needs to be duplicated.
   1192  1.1  mrg 
   1193  1.1  mrg      Note the search starts with the third edge on the path.  The first
   1194  1.1  mrg      edge is the incoming edge, the second edge always has its source
   1195  1.1  mrg      duplicated.  Thus we start our search with the third edge.  */
   1196  1.1  mrg   vec<jump_thread_edge *> *path = rd->path;
   1197  1.1  mrg   for (unsigned int i = 2; i < path->length (); i++)
   1198  1.1  mrg     {
   1199  1.1  mrg       if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK
   1200  1.1  mrg 	  || (*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
   1201  1.1  mrg 	{
   1202  1.1  mrg 	  create_block_for_threading ((*path)[i]->e->src, rd, 1,
   1203  1.1  mrg 				      &local_info->duplicate_blocks);
   1204  1.1  mrg 	  break;
   1205  1.1  mrg 	}
   1206  1.1  mrg     }
   1207  1.1  mrg 
   1208  1.1  mrg   /* Create a template block if we have not done so already.  Otherwise
   1209  1.1  mrg      use the template to create a new block.  */
   1210  1.1  mrg   if (local_info->template_block == NULL)
   1211  1.1  mrg     {
   1212  1.1  mrg       create_block_for_threading ((*path)[1]->e->src, rd, 0,
   1213  1.1  mrg 				  &local_info->duplicate_blocks);
   1214  1.1  mrg       local_info->template_block = rd->dup_blocks[0];
   1215  1.1  mrg       local_info->template_last_to_copy
   1216  1.1  mrg 	= gsi_last_bb (local_info->template_block);
   1217  1.1  mrg 
   1218  1.1  mrg       /* We do not create any outgoing edges for the template.  We will
   1219  1.1  mrg 	 take care of that in a later traversal.  That way we do not
   1220  1.1  mrg 	 create edges that are going to just be deleted.  */
   1221  1.1  mrg     }
   1222  1.1  mrg   else
   1223  1.1  mrg     {
   1224  1.1  mrg       gimple_seq seq = NULL;
   1225  1.1  mrg       if (gsi_stmt (local_info->template_last_to_copy)
   1226  1.1  mrg 	  != gsi_stmt (gsi_last_bb (local_info->template_block)))
   1227  1.1  mrg 	{
   1228  1.1  mrg 	  if (gsi_end_p (local_info->template_last_to_copy))
   1229  1.1  mrg 	    {
   1230  1.1  mrg 	      seq = bb_seq (local_info->template_block);
   1231  1.1  mrg 	      set_bb_seq (local_info->template_block, NULL);
   1232  1.1  mrg 	    }
   1233  1.1  mrg 	  else
   1234  1.1  mrg 	    seq = gsi_split_seq_after (local_info->template_last_to_copy);
   1235  1.1  mrg 	}
   1236  1.1  mrg       create_block_for_threading (local_info->template_block, rd, 0,
   1237  1.1  mrg 				  &local_info->duplicate_blocks);
   1238  1.1  mrg       if (seq)
   1239  1.1  mrg 	{
   1240  1.1  mrg 	  if (gsi_end_p (local_info->template_last_to_copy))
   1241  1.1  mrg 	    set_bb_seq (local_info->template_block, seq);
   1242  1.1  mrg 	  else
   1243  1.1  mrg 	    gsi_insert_seq_after (&local_info->template_last_to_copy,
   1244  1.1  mrg 				  seq, GSI_SAME_STMT);
   1245  1.1  mrg 	}
   1246  1.1  mrg 
   1247  1.1  mrg       /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
   1248  1.1  mrg 	 block.   */
   1249  1.1  mrg       ssa_fix_duplicate_block_edges (rd, local_info);
   1250  1.1  mrg     }
   1251  1.1  mrg 
   1252  1.1  mrg   if (MAY_HAVE_DEBUG_STMTS)
   1253  1.1  mrg     {
   1254  1.1  mrg       /* Copy debug stmts from each NO_COPY src block to the block
   1255  1.1  mrg 	 that would have been its predecessor, if we can append to it
   1256  1.1  mrg 	 (we can't add stmts after a block-ending stmt), or prepending
   1257  1.1  mrg 	 to the duplicate of the successor, if there is one.  If
   1258  1.1  mrg 	 there's no duplicate successor, we'll mostly drop the blocks
   1259  1.1  mrg 	 on the floor; propagate_threaded_block_debug_into, called
   1260  1.1  mrg 	 elsewhere, will consolidate and preserve the effects of the
   1261  1.1  mrg 	 binds, but none of the markers.  */
   1262  1.1  mrg       gimple_stmt_iterator copy_to = gsi_last_bb (rd->dup_blocks[0]);
   1263  1.1  mrg       if (!gsi_end_p (copy_to))
   1264  1.1  mrg 	{
   1265  1.1  mrg 	  if (stmt_ends_bb_p (gsi_stmt (copy_to)))
   1266  1.1  mrg 	    {
   1267  1.1  mrg 	      if (rd->dup_blocks[1])
   1268  1.1  mrg 		copy_to = gsi_after_labels (rd->dup_blocks[1]);
   1269  1.1  mrg 	      else
   1270  1.1  mrg 		copy_to = gsi_none ();
   1271  1.1  mrg 	    }
   1272  1.1  mrg 	  else
   1273  1.1  mrg 	    gsi_next (&copy_to);
   1274  1.1  mrg 	}
   1275  1.1  mrg       for (unsigned int i = 2, j = 0; i < path->length (); i++)
   1276  1.1  mrg 	if ((*path)[i]->type == EDGE_NO_COPY_SRC_BLOCK
   1277  1.1  mrg 	    && gsi_bb (copy_to))
   1278  1.1  mrg 	  {
   1279  1.1  mrg 	    for (gimple_stmt_iterator gsi = gsi_start_bb ((*path)[i]->e->src);
   1280  1.1  mrg 		 !gsi_end_p (gsi); gsi_next (&gsi))
   1281  1.1  mrg 	      {
   1282  1.1  mrg 		if (!is_gimple_debug (gsi_stmt (gsi)))
   1283  1.1  mrg 		  continue;
   1284  1.1  mrg 		gimple *stmt = gsi_stmt (gsi);
   1285  1.1  mrg 		gimple *copy = gimple_copy (stmt);
   1286  1.1  mrg 		gsi_insert_before (&copy_to, copy, GSI_SAME_STMT);
   1287  1.1  mrg 	      }
   1288  1.1  mrg 	  }
   1289  1.1  mrg 	else if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK
   1290  1.1  mrg 		 || (*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
   1291  1.1  mrg 	  {
   1292  1.1  mrg 	    j++;
   1293  1.1  mrg 	    gcc_assert (j < 2);
   1294  1.1  mrg 	    copy_to = gsi_last_bb (rd->dup_blocks[j]);
   1295  1.1  mrg 	    if (!gsi_end_p (copy_to))
   1296  1.1  mrg 	      {
   1297  1.1  mrg 		if (stmt_ends_bb_p (gsi_stmt (copy_to)))
   1298  1.1  mrg 		  copy_to = gsi_none ();
   1299  1.1  mrg 		else
   1300  1.1  mrg 		  gsi_next (&copy_to);
   1301  1.1  mrg 	      }
   1302  1.1  mrg 	  }
   1303  1.1  mrg     }
   1304  1.1  mrg 
   1305  1.1  mrg   /* Keep walking the hash table.  */
   1306  1.1  mrg   return 1;
   1307  1.1  mrg }
   1308  1.1  mrg 
   1309  1.1  mrg /* We did not create any outgoing edges for the template block during
   1310  1.1  mrg    block creation.  This hash table traversal callback creates the
   1311  1.1  mrg    outgoing edge for the template block.  */
   1312  1.1  mrg 
   1313  1.1  mrg inline int
   1314  1.1  mrg ssa_fixup_template_block (struct redirection_data **slot,
   1315  1.1  mrg 			  ssa_local_info_t *local_info)
   1316  1.1  mrg {
   1317  1.1  mrg   struct redirection_data *rd = *slot;
   1318  1.1  mrg 
   1319  1.1  mrg   /* If this is the template block halt the traversal after updating
   1320  1.1  mrg      it appropriately.
   1321  1.1  mrg 
   1322  1.1  mrg      If we were threading through an joiner block, then we want
   1323  1.1  mrg      to keep its control statement and redirect an outgoing edge.
   1324  1.1  mrg      Else we want to remove the control statement & edges, then create
   1325  1.1  mrg      a new outgoing edge.  In both cases we may need to update PHIs.  */
   1326  1.1  mrg   if (rd->dup_blocks[0] && rd->dup_blocks[0] == local_info->template_block)
   1327  1.1  mrg     {
   1328  1.1  mrg       ssa_fix_duplicate_block_edges (rd, local_info);
   1329  1.1  mrg       return 0;
   1330  1.1  mrg     }
   1331  1.1  mrg 
   1332  1.1  mrg   return 1;
   1333  1.1  mrg }
   1334  1.1  mrg 
   1335  1.1  mrg /* Hash table traversal callback to redirect each incoming edge
   1336  1.1  mrg    associated with this hash table element to its new destination.  */
   1337  1.1  mrg 
   1338  1.1  mrg static int
   1339  1.1  mrg ssa_redirect_edges (struct redirection_data **slot,
   1340  1.1  mrg 		    ssa_local_info_t *local_info)
   1341  1.1  mrg {
   1342  1.1  mrg   struct redirection_data *rd = *slot;
   1343  1.1  mrg   struct el *next, *el;
   1344  1.1  mrg 
   1345  1.1  mrg   /* Walk over all the incoming edges associated with this hash table
   1346  1.1  mrg      entry.  */
   1347  1.1  mrg   for (el = rd->incoming_edges; el; el = next)
   1348  1.1  mrg     {
   1349  1.1  mrg       edge e = el->e;
   1350  1.1  mrg       vec<jump_thread_edge *> *path = THREAD_PATH (e);
   1351  1.1  mrg 
   1352  1.1  mrg       /* Go ahead and free this element from the list.  Doing this now
   1353  1.1  mrg 	 avoids the need for another list walk when we destroy the hash
   1354  1.1  mrg 	 table.  */
   1355  1.1  mrg       next = el->next;
   1356  1.1  mrg       free (el);
   1357  1.1  mrg 
   1358  1.1  mrg       local_info->num_threaded_edges++;
   1359  1.1  mrg 
   1360  1.1  mrg       if (rd->dup_blocks[0])
   1361  1.1  mrg 	{
   1362  1.1  mrg 	  edge e2;
   1363  1.1  mrg 
   1364  1.1  mrg 	  if (dump_file && (dump_flags & TDF_DETAILS))
   1365  1.1  mrg 	    fprintf (dump_file, "  Threaded jump %d --> %d to %d\n",
   1366  1.1  mrg 		     e->src->index, e->dest->index, rd->dup_blocks[0]->index);
   1367  1.1  mrg 
   1368  1.1  mrg 	  /* Redirect the incoming edge (possibly to the joiner block) to the
   1369  1.1  mrg 	     appropriate duplicate block.  */
   1370  1.1  mrg 	  e2 = redirect_edge_and_branch (e, rd->dup_blocks[0]);
   1371  1.1  mrg 	  gcc_assert (e == e2);
   1372  1.1  mrg 	  flush_pending_stmts (e2);
   1373  1.1  mrg 	}
   1374  1.1  mrg 
   1375  1.1  mrg       /* Go ahead and clear E->aux.  It's not needed anymore and failure
   1376  1.1  mrg 	 to clear it will cause all kinds of unpleasant problems later.  */
   1377  1.1  mrg       path->release ();
   1378  1.1  mrg       e->aux = NULL;
   1379  1.1  mrg 
   1380  1.1  mrg     }
   1381  1.1  mrg 
   1382  1.1  mrg   /* Indicate that we actually threaded one or more jumps.  */
   1383  1.1  mrg   if (rd->incoming_edges)
   1384  1.1  mrg     local_info->jumps_threaded = true;
   1385  1.1  mrg 
   1386  1.1  mrg   return 1;
   1387  1.1  mrg }
   1388  1.1  mrg 
   1389  1.1  mrg /* Return true if this block has no executable statements other than
   1390  1.1  mrg    a simple ctrl flow instruction.  When the number of outgoing edges
   1391  1.1  mrg    is one, this is equivalent to a "forwarder" block.  */
   1392  1.1  mrg 
   1393  1.1  mrg static bool
   1394  1.1  mrg redirection_block_p (basic_block bb)
   1395  1.1  mrg {
   1396  1.1  mrg   gimple_stmt_iterator gsi;
   1397  1.1  mrg 
   1398  1.1  mrg   /* Advance to the first executable statement.  */
   1399  1.1  mrg   gsi = gsi_start_bb (bb);
   1400  1.1  mrg   while (!gsi_end_p (gsi)
   1401  1.1  mrg 	 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
   1402  1.1  mrg 	     || is_gimple_debug (gsi_stmt (gsi))
   1403  1.1  mrg 	     || gimple_nop_p (gsi_stmt (gsi))
   1404  1.1  mrg 	     || gimple_clobber_p (gsi_stmt (gsi))))
   1405  1.1  mrg     gsi_next (&gsi);
   1406  1.1  mrg 
   1407  1.1  mrg   /* Check if this is an empty block.  */
   1408  1.1  mrg   if (gsi_end_p (gsi))
   1409  1.1  mrg     return true;
   1410  1.1  mrg 
   1411  1.1  mrg   /* Test that we've reached the terminating control statement.  */
   1412  1.1  mrg   return gsi_stmt (gsi)
   1413  1.1  mrg 	 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
   1414  1.1  mrg 	     || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
   1415  1.1  mrg 	     || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH);
   1416  1.1  mrg }
   1417  1.1  mrg 
   1418  1.1  mrg /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
   1419  1.1  mrg    is reached via one or more specific incoming edges, we know which
   1420  1.1  mrg    outgoing edge from BB will be traversed.
   1421  1.1  mrg 
   1422  1.1  mrg    We want to redirect those incoming edges to the target of the
   1423  1.1  mrg    appropriate outgoing edge.  Doing so avoids a conditional branch
   1424  1.1  mrg    and may expose new optimization opportunities.  Note that we have
   1425  1.1  mrg    to update dominator tree and SSA graph after such changes.
   1426  1.1  mrg 
   1427  1.1  mrg    The key to keeping the SSA graph update manageable is to duplicate
   1428  1.1  mrg    the side effects occurring in BB so that those side effects still
   1429  1.1  mrg    occur on the paths which bypass BB after redirecting edges.
   1430  1.1  mrg 
   1431  1.1  mrg    We accomplish this by creating duplicates of BB and arranging for
   1432  1.1  mrg    the duplicates to unconditionally pass control to one specific
   1433  1.1  mrg    successor of BB.  We then revector the incoming edges into BB to
   1434  1.1  mrg    the appropriate duplicate of BB.
   1435  1.1  mrg 
   1436  1.1  mrg    If NOLOOP_ONLY is true, we only perform the threading as long as it
   1437  1.1  mrg    does not affect the structure of the loops in a nontrivial way.
   1438  1.1  mrg 
   1439  1.1  mrg    If JOINERS is true, then thread through joiner blocks as well.  */
   1440  1.1  mrg 
   1441  1.1  mrg bool
   1442  1.1  mrg fwd_jt_path_registry::thread_block_1 (basic_block bb,
   1443  1.1  mrg 				      bool noloop_only,
   1444  1.1  mrg 				      bool joiners)
   1445  1.1  mrg {
   1446  1.1  mrg   /* E is an incoming edge into BB that we may or may not want to
   1447  1.1  mrg      redirect to a duplicate of BB.  */
   1448  1.1  mrg   edge e, e2;
   1449  1.1  mrg   edge_iterator ei;
   1450  1.1  mrg   ssa_local_info_t local_info;
   1451  1.1  mrg 
   1452  1.1  mrg   local_info.duplicate_blocks = BITMAP_ALLOC (NULL);
   1453  1.1  mrg   local_info.need_profile_correction = false;
   1454  1.1  mrg   local_info.num_threaded_edges = 0;
   1455  1.1  mrg 
   1456  1.1  mrg   /* To avoid scanning a linear array for the element we need we instead
   1457  1.1  mrg      use a hash table.  For normal code there should be no noticeable
   1458  1.1  mrg      difference.  However, if we have a block with a large number of
   1459  1.1  mrg      incoming and outgoing edges such linear searches can get expensive.  */
   1460  1.1  mrg   m_redirection_data
   1461  1.1  mrg     = new hash_table<struct redirection_data> (EDGE_COUNT (bb->succs));
   1462  1.1  mrg 
   1463  1.1  mrg   /* Record each unique threaded destination into a hash table for
   1464  1.1  mrg      efficient lookups.  */
   1465  1.1  mrg   edge last = NULL;
   1466  1.1  mrg   FOR_EACH_EDGE (e, ei, bb->preds)
   1467  1.1  mrg     {
   1468  1.1  mrg       if (e->aux == NULL)
   1469  1.1  mrg 	continue;
   1470  1.1  mrg 
   1471  1.1  mrg       vec<jump_thread_edge *> *path = THREAD_PATH (e);
   1472  1.1  mrg 
   1473  1.1  mrg       if (((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK && !joiners)
   1474  1.1  mrg 	  || ((*path)[1]->type == EDGE_COPY_SRC_BLOCK && joiners))
   1475  1.1  mrg 	continue;
   1476  1.1  mrg 
   1477  1.1  mrg       e2 = path->last ()->e;
   1478  1.1  mrg       if (!e2 || noloop_only)
   1479  1.1  mrg 	{
   1480  1.1  mrg 	  /* If NOLOOP_ONLY is true, we only allow threading through the
   1481  1.1  mrg 	     header of a loop to exit edges.  */
   1482  1.1  mrg 
   1483  1.1  mrg 	  /* One case occurs when there was loop header buried in a jump
   1484  1.1  mrg 	     threading path that crosses loop boundaries.  We do not try
   1485  1.1  mrg 	     and thread this elsewhere, so just cancel the jump threading
   1486  1.1  mrg 	     request by clearing the AUX field now.  */
   1487  1.1  mrg 	  if (bb->loop_father != e2->src->loop_father
   1488  1.1  mrg 	      && (!loop_exit_edge_p (e2->src->loop_father, e2)
   1489  1.1  mrg 		  || flow_loop_nested_p (bb->loop_father,
   1490  1.1  mrg 					 e2->dest->loop_father)))
   1491  1.1  mrg 	    {
   1492  1.1  mrg 	      /* Since this case is not handled by our special code
   1493  1.1  mrg 		 to thread through a loop header, we must explicitly
   1494  1.1  mrg 		 cancel the threading request here.  */
   1495  1.1  mrg 	      cancel_thread (path, "Threading through unhandled loop header");
   1496  1.1  mrg 	      e->aux = NULL;
   1497  1.1  mrg 	      continue;
   1498  1.1  mrg 	    }
   1499  1.1  mrg 
   1500  1.1  mrg 	  /* Another case occurs when trying to thread through our
   1501  1.1  mrg 	     own loop header, possibly from inside the loop.  We will
   1502  1.1  mrg 	     thread these later.  */
   1503  1.1  mrg 	  unsigned int i;
   1504  1.1  mrg 	  for (i = 1; i < path->length (); i++)
   1505  1.1  mrg 	    {
   1506  1.1  mrg 	      if ((*path)[i]->e->src == bb->loop_father->header
   1507  1.1  mrg 		  && (!loop_exit_edge_p (bb->loop_father, e2)
   1508  1.1  mrg 		      || (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK))
   1509  1.1  mrg 		break;
   1510  1.1  mrg 	    }
   1511  1.1  mrg 
   1512  1.1  mrg 	  if (i != path->length ())
   1513  1.1  mrg 	    continue;
   1514  1.1  mrg 
   1515  1.1  mrg 	  /* Loop parallelization can be confused by the result of
   1516  1.1  mrg 	     threading through the loop exit test back into the loop.
   1517  1.1  mrg 	     However, theading those jumps seems to help other codes.
   1518  1.1  mrg 
   1519  1.1  mrg 	     I have been unable to find anything related to the shape of
   1520  1.1  mrg 	     the CFG, the contents of the affected blocks, etc which would
   1521  1.1  mrg 	     allow a more sensible test than what we're using below which
   1522  1.1  mrg 	     merely avoids the optimization when parallelizing loops.  */
   1523  1.1  mrg 	  if (flag_tree_parallelize_loops > 1)
   1524  1.1  mrg 	    {
   1525  1.1  mrg 	      for (i = 1; i < path->length (); i++)
   1526  1.1  mrg 	        if (bb->loop_father == e2->src->loop_father
   1527  1.1  mrg 		    && loop_exits_from_bb_p (bb->loop_father,
   1528  1.1  mrg 					     (*path)[i]->e->src)
   1529  1.1  mrg 		    && !loop_exit_edge_p (bb->loop_father, e2))
   1530  1.1  mrg 		  break;
   1531  1.1  mrg 
   1532  1.1  mrg 	      if (i != path->length ())
   1533  1.1  mrg 		{
   1534  1.1  mrg 		  cancel_thread (path, "Threading through loop exit");
   1535  1.1  mrg 		  e->aux = NULL;
   1536  1.1  mrg 		  continue;
   1537  1.1  mrg 		}
   1538  1.1  mrg 	    }
   1539  1.1  mrg 	}
   1540  1.1  mrg 
   1541  1.1  mrg       /* Insert the outgoing edge into the hash table if it is not
   1542  1.1  mrg 	 already in the hash table.  */
   1543  1.1  mrg       lookup_redirection_data (e, INSERT);
   1544  1.1  mrg 
   1545  1.1  mrg       /* When we have thread paths through a common joiner with different
   1546  1.1  mrg 	 final destinations, then we may need corrections to deal with
   1547  1.1  mrg 	 profile insanities.  See the big comment before compute_path_counts.  */
   1548  1.1  mrg       if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
   1549  1.1  mrg 	{
   1550  1.1  mrg 	  if (!last)
   1551  1.1  mrg 	    last = e2;
   1552  1.1  mrg 	  else if (e2 != last)
   1553  1.1  mrg 	    local_info.need_profile_correction = true;
   1554  1.1  mrg 	}
   1555  1.1  mrg     }
   1556  1.1  mrg 
   1557  1.1  mrg   /* We do not update dominance info.  */
   1558  1.1  mrg   free_dominance_info (CDI_DOMINATORS);
   1559  1.1  mrg 
   1560  1.1  mrg   /* We know we only thread through the loop header to loop exits.
   1561  1.1  mrg      Let the basic block duplication hook know we are not creating
   1562  1.1  mrg      a multiple entry loop.  */
   1563  1.1  mrg   if (noloop_only
   1564  1.1  mrg       && bb == bb->loop_father->header)
   1565  1.1  mrg     set_loop_copy (bb->loop_father, loop_outer (bb->loop_father));
   1566  1.1  mrg 
   1567  1.1  mrg   /* Now create duplicates of BB.
   1568  1.1  mrg 
   1569  1.1  mrg      Note that for a block with a high outgoing degree we can waste
   1570  1.1  mrg      a lot of time and memory creating and destroying useless edges.
   1571  1.1  mrg 
   1572  1.1  mrg      So we first duplicate BB and remove the control structure at the
   1573  1.1  mrg      tail of the duplicate as well as all outgoing edges from the
   1574  1.1  mrg      duplicate.  We then use that duplicate block as a template for
   1575  1.1  mrg      the rest of the duplicates.  */
   1576  1.1  mrg   local_info.template_block = NULL;
   1577  1.1  mrg   local_info.bb = bb;
   1578  1.1  mrg   local_info.jumps_threaded = false;
   1579  1.1  mrg   m_redirection_data->traverse <ssa_local_info_t *, ssa_create_duplicates>
   1580  1.1  mrg 			    (&local_info);
   1581  1.1  mrg 
   1582  1.1  mrg   /* The template does not have an outgoing edge.  Create that outgoing
   1583  1.1  mrg      edge and update PHI nodes as the edge's target as necessary.
   1584  1.1  mrg 
   1585  1.1  mrg      We do this after creating all the duplicates to avoid creating
   1586  1.1  mrg      unnecessary edges.  */
   1587  1.1  mrg   m_redirection_data->traverse <ssa_local_info_t *, ssa_fixup_template_block>
   1588  1.1  mrg 			    (&local_info);
   1589  1.1  mrg 
   1590  1.1  mrg   /* The hash table traversals above created the duplicate blocks (and the
   1591  1.1  mrg      statements within the duplicate blocks).  This loop creates PHI nodes for
   1592  1.1  mrg      the duplicated blocks and redirects the incoming edges into BB to reach
   1593  1.1  mrg      the duplicates of BB.  */
   1594  1.1  mrg   m_redirection_data->traverse <ssa_local_info_t *, ssa_redirect_edges>
   1595  1.1  mrg 			    (&local_info);
   1596  1.1  mrg 
   1597  1.1  mrg   /* Done with this block.  Clear REDIRECTION_DATA.  */
   1598  1.1  mrg   delete m_redirection_data;
   1599  1.1  mrg   m_redirection_data = NULL;
   1600  1.1  mrg 
   1601  1.1  mrg   if (noloop_only
   1602  1.1  mrg       && bb == bb->loop_father->header)
   1603  1.1  mrg     set_loop_copy (bb->loop_father, NULL);
   1604  1.1  mrg 
   1605  1.1  mrg   BITMAP_FREE (local_info.duplicate_blocks);
   1606  1.1  mrg   local_info.duplicate_blocks = NULL;
   1607  1.1  mrg 
   1608  1.1  mrg   m_num_threaded_edges += local_info.num_threaded_edges;
   1609  1.1  mrg 
   1610  1.1  mrg   /* Indicate to our caller whether or not any jumps were threaded.  */
   1611  1.1  mrg   return local_info.jumps_threaded;
   1612  1.1  mrg }
   1613  1.1  mrg 
   1614  1.1  mrg /* Wrapper for thread_block_1 so that we can first handle jump
   1615  1.1  mrg    thread paths which do not involve copying joiner blocks, then
   1616  1.1  mrg    handle jump thread paths which have joiner blocks.
   1617  1.1  mrg 
   1618  1.1  mrg    By doing things this way we can be as aggressive as possible and
   1619  1.1  mrg    not worry that copying a joiner block will create a jump threading
   1620  1.1  mrg    opportunity.  */
   1621  1.1  mrg 
   1622  1.1  mrg bool
   1623  1.1  mrg fwd_jt_path_registry::thread_block (basic_block bb, bool noloop_only)
   1624  1.1  mrg {
   1625  1.1  mrg   bool retval;
   1626  1.1  mrg   retval = thread_block_1 (bb, noloop_only, false);
   1627  1.1  mrg   retval |= thread_block_1 (bb, noloop_only, true);
   1628  1.1  mrg   return retval;
   1629  1.1  mrg }
   1630  1.1  mrg 
   1631  1.1  mrg /* Callback for dfs_enumerate_from.  Returns true if BB is different
   1632  1.1  mrg    from STOP and DBDS_CE_STOP.  */
   1633  1.1  mrg 
   1634  1.1  mrg static basic_block dbds_ce_stop;
   1635  1.1  mrg static bool
   1636  1.1  mrg dbds_continue_enumeration_p (const_basic_block bb, const void *stop)
   1637  1.1  mrg {
   1638  1.1  mrg   return (bb != (const_basic_block) stop
   1639  1.1  mrg 	  && bb != dbds_ce_stop);
   1640  1.1  mrg }
   1641  1.1  mrg 
   1642  1.1  mrg /* Evaluates the dominance relationship of latch of the LOOP and BB, and
   1643  1.1  mrg    returns the state.  */
   1644  1.1  mrg 
   1645  1.1  mrg enum bb_dom_status
   1646  1.1  mrg determine_bb_domination_status (class loop *loop, basic_block bb)
   1647  1.1  mrg {
   1648  1.1  mrg   basic_block *bblocks;
   1649  1.1  mrg   unsigned nblocks, i;
   1650  1.1  mrg   bool bb_reachable = false;
   1651  1.1  mrg   edge_iterator ei;
   1652  1.1  mrg   edge e;
   1653  1.1  mrg 
   1654  1.1  mrg   /* This function assumes BB is a successor of LOOP->header.
   1655  1.1  mrg      If that is not the case return DOMST_NONDOMINATING which
   1656  1.1  mrg      is always safe.  */
   1657  1.1  mrg     {
   1658  1.1  mrg       bool ok = false;
   1659  1.1  mrg 
   1660  1.1  mrg       FOR_EACH_EDGE (e, ei, bb->preds)
   1661  1.1  mrg 	{
   1662  1.1  mrg      	  if (e->src == loop->header)
   1663  1.1  mrg 	    {
   1664  1.1  mrg 	      ok = true;
   1665  1.1  mrg 	      break;
   1666  1.1  mrg 	    }
   1667  1.1  mrg 	}
   1668  1.1  mrg 
   1669  1.1  mrg       if (!ok)
   1670  1.1  mrg 	return DOMST_NONDOMINATING;
   1671  1.1  mrg     }
   1672  1.1  mrg 
   1673  1.1  mrg   if (bb == loop->latch)
   1674  1.1  mrg     return DOMST_DOMINATING;
   1675  1.1  mrg 
   1676  1.1  mrg   /* Check that BB dominates LOOP->latch, and that it is back-reachable
   1677  1.1  mrg      from it.  */
   1678  1.1  mrg 
   1679  1.1  mrg   bblocks = XCNEWVEC (basic_block, loop->num_nodes);
   1680  1.1  mrg   dbds_ce_stop = loop->header;
   1681  1.1  mrg   nblocks = dfs_enumerate_from (loop->latch, 1, dbds_continue_enumeration_p,
   1682  1.1  mrg 				bblocks, loop->num_nodes, bb);
   1683  1.1  mrg   for (i = 0; i < nblocks; i++)
   1684  1.1  mrg     FOR_EACH_EDGE (e, ei, bblocks[i]->preds)
   1685  1.1  mrg       {
   1686  1.1  mrg 	if (e->src == loop->header)
   1687  1.1  mrg 	  {
   1688  1.1  mrg 	    free (bblocks);
   1689  1.1  mrg 	    return DOMST_NONDOMINATING;
   1690  1.1  mrg 	  }
   1691  1.1  mrg 	if (e->src == bb)
   1692  1.1  mrg 	  bb_reachable = true;
   1693  1.1  mrg       }
   1694  1.1  mrg 
   1695  1.1  mrg   free (bblocks);
   1696  1.1  mrg   return (bb_reachable ? DOMST_DOMINATING : DOMST_LOOP_BROKEN);
   1697  1.1  mrg }
   1698  1.1  mrg 
   1699  1.1  mrg /* Thread jumps through the header of LOOP.  Returns true if cfg changes.
   1700  1.1  mrg    If MAY_PEEL_LOOP_HEADERS is false, we avoid threading from entry edges
   1701  1.1  mrg    to the inside of the loop.  */
   1702  1.1  mrg 
   1703  1.1  mrg bool
   1704  1.1  mrg fwd_jt_path_registry::thread_through_loop_header (class loop *loop,
   1705  1.1  mrg 						  bool may_peel_loop_headers)
   1706  1.1  mrg {
   1707  1.1  mrg   basic_block header = loop->header;
   1708  1.1  mrg   edge e, tgt_edge, latch = loop_latch_edge (loop);
   1709  1.1  mrg   edge_iterator ei;
   1710  1.1  mrg   basic_block tgt_bb, atgt_bb;
   1711  1.1  mrg   enum bb_dom_status domst;
   1712  1.1  mrg 
   1713  1.1  mrg   /* We have already threaded through headers to exits, so all the threading
   1714  1.1  mrg      requests now are to the inside of the loop.  We need to avoid creating
   1715  1.1  mrg      irreducible regions (i.e., loops with more than one entry block), and
   1716  1.1  mrg      also loop with several latch edges, or new subloops of the loop (although
   1717  1.1  mrg      there are cases where it might be appropriate, it is difficult to decide,
   1718  1.1  mrg      and doing it wrongly may confuse other optimizers).
   1719  1.1  mrg 
   1720  1.1  mrg      We could handle more general cases here.  However, the intention is to
   1721  1.1  mrg      preserve some information about the loop, which is impossible if its
   1722  1.1  mrg      structure changes significantly, in a way that is not well understood.
   1723  1.1  mrg      Thus we only handle few important special cases, in which also updating
   1724  1.1  mrg      of the loop-carried information should be feasible:
   1725  1.1  mrg 
   1726  1.1  mrg      1) Propagation of latch edge to a block that dominates the latch block
   1727  1.1  mrg 	of a loop.  This aims to handle the following idiom:
   1728  1.1  mrg 
   1729  1.1  mrg 	first = 1;
   1730  1.1  mrg 	while (1)
   1731  1.1  mrg 	  {
   1732  1.1  mrg 	    if (first)
   1733  1.1  mrg 	      initialize;
   1734  1.1  mrg 	    first = 0;
   1735  1.1  mrg 	    body;
   1736  1.1  mrg 	  }
   1737  1.1  mrg 
   1738  1.1  mrg 	After threading the latch edge, this becomes
   1739  1.1  mrg 
   1740  1.1  mrg 	first = 1;
   1741  1.1  mrg 	if (first)
   1742  1.1  mrg 	  initialize;
   1743  1.1  mrg 	while (1)
   1744  1.1  mrg 	  {
   1745  1.1  mrg 	    first = 0;
   1746  1.1  mrg 	    body;
   1747  1.1  mrg 	  }
   1748  1.1  mrg 
   1749  1.1  mrg 	The original header of the loop is moved out of it, and we may thread
   1750  1.1  mrg 	the remaining edges through it without further constraints.
   1751  1.1  mrg 
   1752  1.1  mrg      2) All entry edges are propagated to a single basic block that dominates
   1753  1.1  mrg 	the latch block of the loop.  This aims to handle the following idiom
   1754  1.1  mrg 	(normally created for "for" loops):
   1755  1.1  mrg 
   1756  1.1  mrg 	i = 0;
   1757  1.1  mrg 	while (1)
   1758  1.1  mrg 	  {
   1759  1.1  mrg 	    if (i >= 100)
   1760  1.1  mrg 	      break;
   1761  1.1  mrg 	    body;
   1762  1.1  mrg 	    i++;
   1763  1.1  mrg 	  }
   1764  1.1  mrg 
   1765  1.1  mrg 	This becomes
   1766  1.1  mrg 
   1767  1.1  mrg 	i = 0;
   1768  1.1  mrg 	while (1)
   1769  1.1  mrg 	  {
   1770  1.1  mrg 	    body;
   1771  1.1  mrg 	    i++;
   1772  1.1  mrg 	    if (i >= 100)
   1773  1.1  mrg 	      break;
   1774  1.1  mrg 	  }
   1775  1.1  mrg      */
   1776  1.1  mrg 
   1777  1.1  mrg   /* Threading through the header won't improve the code if the header has just
   1778  1.1  mrg      one successor.  */
   1779  1.1  mrg   if (single_succ_p (header))
   1780  1.1  mrg     goto fail;
   1781  1.1  mrg 
   1782  1.1  mrg   if (!may_peel_loop_headers && !redirection_block_p (loop->header))
   1783  1.1  mrg     goto fail;
   1784  1.1  mrg   else
   1785  1.1  mrg     {
   1786  1.1  mrg       tgt_bb = NULL;
   1787  1.1  mrg       tgt_edge = NULL;
   1788  1.1  mrg       FOR_EACH_EDGE (e, ei, header->preds)
   1789  1.1  mrg 	{
   1790  1.1  mrg 	  if (!e->aux)
   1791  1.1  mrg 	    {
   1792  1.1  mrg 	      if (e == latch)
   1793  1.1  mrg 		continue;
   1794  1.1  mrg 
   1795  1.1  mrg 	      /* If latch is not threaded, and there is a header
   1796  1.1  mrg 		 edge that is not threaded, we would create loop
   1797  1.1  mrg 		 with multiple entries.  */
   1798  1.1  mrg 	      goto fail;
   1799  1.1  mrg 	    }
   1800  1.1  mrg 
   1801  1.1  mrg 	  vec<jump_thread_edge *> *path = THREAD_PATH (e);
   1802  1.1  mrg 
   1803  1.1  mrg 	  if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
   1804  1.1  mrg 	    goto fail;
   1805  1.1  mrg 	  tgt_edge = (*path)[1]->e;
   1806  1.1  mrg 	  atgt_bb = tgt_edge->dest;
   1807  1.1  mrg 	  if (!tgt_bb)
   1808  1.1  mrg 	    tgt_bb = atgt_bb;
   1809  1.1  mrg 	  /* Two targets of threading would make us create loop
   1810  1.1  mrg 	     with multiple entries.  */
   1811  1.1  mrg 	  else if (tgt_bb != atgt_bb)
   1812  1.1  mrg 	    goto fail;
   1813  1.1  mrg 	}
   1814  1.1  mrg 
   1815  1.1  mrg       if (!tgt_bb)
   1816  1.1  mrg 	{
   1817  1.1  mrg 	  /* There are no threading requests.  */
   1818  1.1  mrg 	  return false;
   1819  1.1  mrg 	}
   1820  1.1  mrg 
   1821  1.1  mrg       /* Redirecting to empty loop latch is useless.  */
   1822  1.1  mrg       if (tgt_bb == loop->latch
   1823  1.1  mrg 	  && empty_block_p (loop->latch))
   1824  1.1  mrg 	goto fail;
   1825  1.1  mrg     }
   1826  1.1  mrg 
   1827  1.1  mrg   /* The target block must dominate the loop latch, otherwise we would be
   1828  1.1  mrg      creating a subloop.  */
   1829  1.1  mrg   domst = determine_bb_domination_status (loop, tgt_bb);
   1830  1.1  mrg   if (domst == DOMST_NONDOMINATING)
   1831  1.1  mrg     goto fail;
   1832  1.1  mrg   if (domst == DOMST_LOOP_BROKEN)
   1833  1.1  mrg     {
   1834  1.1  mrg       /* If the loop ceased to exist, mark it as such, and thread through its
   1835  1.1  mrg 	 original header.  */
   1836  1.1  mrg       mark_loop_for_removal (loop);
   1837  1.1  mrg       return thread_block (header, false);
   1838  1.1  mrg     }
   1839  1.1  mrg 
   1840  1.1  mrg   if (tgt_bb->loop_father->header == tgt_bb)
   1841  1.1  mrg     {
   1842  1.1  mrg       /* If the target of the threading is a header of a subloop, we need
   1843  1.1  mrg 	 to create a preheader for it, so that the headers of the two loops
   1844  1.1  mrg 	 do not merge.  */
   1845  1.1  mrg       if (EDGE_COUNT (tgt_bb->preds) > 2)
   1846  1.1  mrg 	{
   1847  1.1  mrg 	  tgt_bb = create_preheader (tgt_bb->loop_father, 0);
   1848  1.1  mrg 	  gcc_assert (tgt_bb != NULL);
   1849  1.1  mrg 	}
   1850  1.1  mrg       else
   1851  1.1  mrg 	tgt_bb = split_edge (tgt_edge);
   1852  1.1  mrg     }
   1853  1.1  mrg 
   1854  1.1  mrg   basic_block new_preheader;
   1855  1.1  mrg 
   1856  1.1  mrg   /* Now consider the case entry edges are redirected to the new entry
   1857  1.1  mrg      block.  Remember one entry edge, so that we can find the new
   1858  1.1  mrg      preheader (its destination after threading).  */
   1859  1.1  mrg   FOR_EACH_EDGE (e, ei, header->preds)
   1860  1.1  mrg     {
   1861  1.1  mrg       if (e->aux)
   1862  1.1  mrg 	break;
   1863  1.1  mrg     }
   1864  1.1  mrg 
   1865  1.1  mrg   /* The duplicate of the header is the new preheader of the loop.  Ensure
   1866  1.1  mrg      that it is placed correctly in the loop hierarchy.  */
   1867  1.1  mrg   set_loop_copy (loop, loop_outer (loop));
   1868  1.1  mrg 
   1869  1.1  mrg   thread_block (header, false);
   1870  1.1  mrg   set_loop_copy (loop, NULL);
   1871  1.1  mrg   new_preheader = e->dest;
   1872  1.1  mrg 
   1873  1.1  mrg   /* Create the new latch block.  This is always necessary, as the latch
   1874  1.1  mrg      must have only a single successor, but the original header had at
   1875  1.1  mrg      least two successors.  */
   1876  1.1  mrg   loop->latch = NULL;
   1877  1.1  mrg   mfb_kj_edge = single_succ_edge (new_preheader);
   1878  1.1  mrg   loop->header = mfb_kj_edge->dest;
   1879  1.1  mrg   latch = make_forwarder_block (tgt_bb, mfb_keep_just, NULL);
   1880  1.1  mrg   loop->header = latch->dest;
   1881  1.1  mrg   loop->latch = latch->src;
   1882  1.1  mrg   return true;
   1883  1.1  mrg 
   1884  1.1  mrg fail:
   1885  1.1  mrg   /* We failed to thread anything.  Cancel the requests.  */
   1886  1.1  mrg   FOR_EACH_EDGE (e, ei, header->preds)
   1887  1.1  mrg     {
   1888  1.1  mrg       vec<jump_thread_edge *> *path = THREAD_PATH (e);
   1889  1.1  mrg 
   1890  1.1  mrg       if (path)
   1891  1.1  mrg 	{
   1892  1.1  mrg 	  cancel_thread (path, "Failure in thread_through_loop_header");
   1893  1.1  mrg 	  e->aux = NULL;
   1894  1.1  mrg 	}
   1895  1.1  mrg     }
   1896  1.1  mrg   return false;
   1897  1.1  mrg }
   1898  1.1  mrg 
   1899  1.1  mrg /* E1 and E2 are edges into the same basic block.  Return TRUE if the
   1900  1.1  mrg    PHI arguments associated with those edges are equal or there are no
   1901  1.1  mrg    PHI arguments, otherwise return FALSE.  */
   1902  1.1  mrg 
   1903  1.1  mrg static bool
   1904  1.1  mrg phi_args_equal_on_edges (edge e1, edge e2)
   1905  1.1  mrg {
   1906  1.1  mrg   gphi_iterator gsi;
   1907  1.1  mrg   int indx1 = e1->dest_idx;
   1908  1.1  mrg   int indx2 = e2->dest_idx;
   1909  1.1  mrg 
   1910  1.1  mrg   for (gsi = gsi_start_phis (e1->dest); !gsi_end_p (gsi); gsi_next (&gsi))
   1911  1.1  mrg     {
   1912  1.1  mrg       gphi *phi = gsi.phi ();
   1913  1.1  mrg 
   1914  1.1  mrg       if (!operand_equal_p (gimple_phi_arg_def (phi, indx1),
   1915  1.1  mrg 			    gimple_phi_arg_def (phi, indx2), 0))
   1916  1.1  mrg 	return false;
   1917  1.1  mrg     }
   1918  1.1  mrg   return true;
   1919  1.1  mrg }
   1920  1.1  mrg 
   1921  1.1  mrg /* Return the number of non-debug statements and non-virtual PHIs in a
   1922  1.1  mrg    block.  */
   1923  1.1  mrg 
   1924  1.1  mrg static unsigned int
   1925  1.1  mrg count_stmts_and_phis_in_block (basic_block bb)
   1926  1.1  mrg {
   1927  1.1  mrg   unsigned int num_stmts = 0;
   1928  1.1  mrg 
   1929  1.1  mrg   gphi_iterator gpi;
   1930  1.1  mrg   for (gpi = gsi_start_phis (bb); !gsi_end_p (gpi); gsi_next (&gpi))
   1931  1.1  mrg     if (!virtual_operand_p (PHI_RESULT (gpi.phi ())))
   1932  1.1  mrg       num_stmts++;
   1933  1.1  mrg 
   1934  1.1  mrg   gimple_stmt_iterator gsi;
   1935  1.1  mrg   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
   1936  1.1  mrg     {
   1937  1.1  mrg       gimple *stmt = gsi_stmt (gsi);
   1938  1.1  mrg       if (!is_gimple_debug (stmt))
   1939  1.1  mrg         num_stmts++;
   1940  1.1  mrg     }
   1941  1.1  mrg 
   1942  1.1  mrg   return num_stmts;
   1943  1.1  mrg }
   1944  1.1  mrg 
   1945  1.1  mrg 
   1946  1.1  mrg /* Walk through the registered jump threads and convert them into a
   1947  1.1  mrg    form convenient for this pass.
   1948  1.1  mrg 
   1949  1.1  mrg    Any block which has incoming edges threaded to outgoing edges
   1950  1.1  mrg    will have its entry in THREADED_BLOCK set.
   1951  1.1  mrg 
   1952  1.1  mrg    Any threaded edge will have its new outgoing edge stored in the
   1953  1.1  mrg    original edge's AUX field.
   1954  1.1  mrg 
   1955  1.1  mrg    This form avoids the need to walk all the edges in the CFG to
   1956  1.1  mrg    discover blocks which need processing and avoids unnecessary
   1957  1.1  mrg    hash table lookups to map from threaded edge to new target.  */
   1958  1.1  mrg 
   1959  1.1  mrg void
   1960  1.1  mrg fwd_jt_path_registry::mark_threaded_blocks (bitmap threaded_blocks)
   1961  1.1  mrg {
   1962  1.1  mrg   unsigned int i;
   1963  1.1  mrg   bitmap_iterator bi;
   1964  1.1  mrg   auto_bitmap tmp;
   1965  1.1  mrg   basic_block bb;
   1966  1.1  mrg   edge e;
   1967  1.1  mrg   edge_iterator ei;
   1968  1.1  mrg 
   1969  1.1  mrg   /* It is possible to have jump threads in which one is a subpath
   1970  1.1  mrg      of the other.  ie, (A, B), (B, C), (C, D) where B is a joiner
   1971  1.1  mrg      block and (B, C), (C, D) where no joiner block exists.
   1972  1.1  mrg 
   1973  1.1  mrg      When this occurs ignore the jump thread request with the joiner
   1974  1.1  mrg      block.  It's totally subsumed by the simpler jump thread request.
   1975  1.1  mrg 
   1976  1.1  mrg      This results in less block copying, simpler CFGs.  More importantly,
   1977  1.1  mrg      when we duplicate the joiner block, B, in this case we will create
   1978  1.1  mrg      a new threading opportunity that we wouldn't be able to optimize
   1979  1.1  mrg      until the next jump threading iteration.
   1980  1.1  mrg 
   1981  1.1  mrg      So first convert the jump thread requests which do not require a
   1982  1.1  mrg      joiner block.  */
   1983  1.1  mrg   for (i = 0; i < m_paths.length (); i++)
   1984  1.1  mrg     {
   1985  1.1  mrg       vec<jump_thread_edge *> *path = m_paths[i];
   1986  1.1  mrg 
   1987  1.1  mrg       if (path->length () > 1
   1988  1.1  mrg 	  && (*path)[1]->type != EDGE_COPY_SRC_JOINER_BLOCK)
   1989  1.1  mrg 	{
   1990  1.1  mrg 	  edge e = (*path)[0]->e;
   1991  1.1  mrg 	  e->aux = (void *)path;
   1992  1.1  mrg 	  bitmap_set_bit (tmp, e->dest->index);
   1993  1.1  mrg 	}
   1994  1.1  mrg     }
   1995  1.1  mrg 
   1996  1.1  mrg   /* Now iterate again, converting cases where we want to thread
   1997  1.1  mrg      through a joiner block, but only if no other edge on the path
   1998  1.1  mrg      already has a jump thread attached to it.  We do this in two passes,
   1999  1.1  mrg      to avoid situations where the order in the paths vec can hide overlapping
   2000  1.1  mrg      threads (the path is recorded on the incoming edge, so we would miss
   2001  1.1  mrg      cases where the second path starts at a downstream edge on the same
   2002  1.1  mrg      path).  First record all joiner paths, deleting any in the unexpected
   2003  1.1  mrg      case where there is already a path for that incoming edge.  */
   2004  1.1  mrg   for (i = 0; i < m_paths.length ();)
   2005  1.1  mrg     {
   2006  1.1  mrg       vec<jump_thread_edge *> *path = m_paths[i];
   2007  1.1  mrg 
   2008  1.1  mrg       if (path->length () > 1
   2009  1.1  mrg 	  && (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
   2010  1.1  mrg 	{
   2011  1.1  mrg 	  /* Attach the path to the starting edge if none is yet recorded.  */
   2012  1.1  mrg 	  if ((*path)[0]->e->aux == NULL)
   2013  1.1  mrg 	    {
   2014  1.1  mrg 	      (*path)[0]->e->aux = path;
   2015  1.1  mrg 	      i++;
   2016  1.1  mrg 	    }
   2017  1.1  mrg 	  else
   2018  1.1  mrg 	    {
   2019  1.1  mrg 	      m_paths.unordered_remove (i);
   2020  1.1  mrg 	      cancel_thread (path);
   2021  1.1  mrg 	    }
   2022  1.1  mrg 	}
   2023  1.1  mrg       else
   2024  1.1  mrg 	{
   2025  1.1  mrg 	  i++;
   2026  1.1  mrg 	}
   2027  1.1  mrg     }
   2028  1.1  mrg 
   2029  1.1  mrg   /* Second, look for paths that have any other jump thread attached to
   2030  1.1  mrg      them, and either finish converting them or cancel them.  */
   2031  1.1  mrg   for (i = 0; i < m_paths.length ();)
   2032  1.1  mrg     {
   2033  1.1  mrg       vec<jump_thread_edge *> *path = m_paths[i];
   2034  1.1  mrg       edge e = (*path)[0]->e;
   2035  1.1  mrg 
   2036  1.1  mrg       if (path->length () > 1
   2037  1.1  mrg 	  && (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK && e->aux == path)
   2038  1.1  mrg 	{
   2039  1.1  mrg 	  unsigned int j;
   2040  1.1  mrg 	  for (j = 1; j < path->length (); j++)
   2041  1.1  mrg 	    if ((*path)[j]->e->aux != NULL)
   2042  1.1  mrg 	      break;
   2043  1.1  mrg 
   2044  1.1  mrg 	  /* If we iterated through the entire path without exiting the loop,
   2045  1.1  mrg 	     then we are good to go, record it.  */
   2046  1.1  mrg 	  if (j == path->length ())
   2047  1.1  mrg 	    {
   2048  1.1  mrg 	      bitmap_set_bit (tmp, e->dest->index);
   2049  1.1  mrg 	      i++;
   2050  1.1  mrg 	    }
   2051  1.1  mrg 	  else
   2052  1.1  mrg 	    {
   2053  1.1  mrg 	      e->aux = NULL;
   2054  1.1  mrg 	      m_paths.unordered_remove (i);
   2055  1.1  mrg 	      cancel_thread (path);
   2056  1.1  mrg 	    }
   2057  1.1  mrg 	}
   2058  1.1  mrg       else
   2059  1.1  mrg 	{
   2060  1.1  mrg 	  i++;
   2061  1.1  mrg 	}
   2062  1.1  mrg     }
   2063  1.1  mrg 
   2064  1.1  mrg   /* When optimizing for size, prune all thread paths where statement
   2065  1.1  mrg      duplication is necessary.
   2066  1.1  mrg 
   2067  1.1  mrg      We walk the jump thread path looking for copied blocks.  There's
   2068  1.1  mrg      two types of copied blocks.
   2069  1.1  mrg 
   2070  1.1  mrg        EDGE_COPY_SRC_JOINER_BLOCK is always copied and thus we will
   2071  1.1  mrg        cancel the jump threading request when optimizing for size.
   2072  1.1  mrg 
   2073  1.1  mrg        EDGE_COPY_SRC_BLOCK which is copied, but some of its statements
   2074  1.1  mrg        will be killed by threading.  If threading does not kill all of
   2075  1.1  mrg        its statements, then we should cancel the jump threading request
   2076  1.1  mrg        when optimizing for size.  */
   2077  1.1  mrg   if (optimize_function_for_size_p (cfun))
   2078  1.1  mrg     {
   2079  1.1  mrg       EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
   2080  1.1  mrg 	{
   2081  1.1  mrg 	  FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (cfun, i)->preds)
   2082  1.1  mrg 	    if (e->aux)
   2083  1.1  mrg 	      {
   2084  1.1  mrg 		vec<jump_thread_edge *> *path = THREAD_PATH (e);
   2085  1.1  mrg 
   2086  1.1  mrg 		unsigned int j;
   2087  1.1  mrg 		for (j = 1; j < path->length (); j++)
   2088  1.1  mrg 		  {
   2089  1.1  mrg 		    bb = (*path)[j]->e->src;
   2090  1.1  mrg 		    if (redirection_block_p (bb))
   2091  1.1  mrg 		      ;
   2092  1.1  mrg 		    else if ((*path)[j]->type == EDGE_COPY_SRC_JOINER_BLOCK
   2093  1.1  mrg 			     || ((*path)[j]->type == EDGE_COPY_SRC_BLOCK
   2094  1.1  mrg 			         && (count_stmts_and_phis_in_block (bb)
   2095  1.1  mrg 				     != estimate_threading_killed_stmts (bb))))
   2096  1.1  mrg 		      break;
   2097  1.1  mrg 		  }
   2098  1.1  mrg 
   2099  1.1  mrg 		if (j != path->length ())
   2100  1.1  mrg 		  {
   2101  1.1  mrg 		    cancel_thread (path);
   2102  1.1  mrg 		    e->aux = NULL;
   2103  1.1  mrg 		  }
   2104  1.1  mrg 		else
   2105  1.1  mrg 		  bitmap_set_bit (threaded_blocks, i);
   2106  1.1  mrg 	      }
   2107  1.1  mrg 	}
   2108  1.1  mrg     }
   2109  1.1  mrg   else
   2110  1.1  mrg     bitmap_copy (threaded_blocks, tmp);
   2111  1.1  mrg 
   2112  1.1  mrg   /* If we have a joiner block (J) which has two successors S1 and S2 and
   2113  1.1  mrg      we are threading though S1 and the final destination of the thread
   2114  1.1  mrg      is S2, then we must verify that any PHI nodes in S2 have the same
   2115  1.1  mrg      PHI arguments for the edge J->S2 and J->S1->...->S2.
   2116  1.1  mrg 
   2117  1.1  mrg      We used to detect this prior to registering the jump thread, but
   2118  1.1  mrg      that prohibits propagation of edge equivalences into non-dominated
   2119  1.1  mrg      PHI nodes as the equivalency test might occur before propagation.
   2120  1.1  mrg 
   2121  1.1  mrg      This must also occur after we truncate any jump threading paths
   2122  1.1  mrg      as this scenario may only show up after truncation.
   2123  1.1  mrg 
   2124  1.1  mrg      This works for now, but will need improvement as part of the FSA
   2125  1.1  mrg      optimization.
   2126  1.1  mrg 
   2127  1.1  mrg      Note since we've moved the thread request data to the edges,
   2128  1.1  mrg      we have to iterate on those rather than the threaded_edges vector.  */
   2129  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
   2130  1.1  mrg     {
   2131  1.1  mrg       bb = BASIC_BLOCK_FOR_FN (cfun, i);
   2132  1.1  mrg       FOR_EACH_EDGE (e, ei, bb->preds)
   2133  1.1  mrg 	{
   2134  1.1  mrg 	  if (e->aux)
   2135  1.1  mrg 	    {
   2136  1.1  mrg 	      vec<jump_thread_edge *> *path = THREAD_PATH (e);
   2137  1.1  mrg 	      bool have_joiner = ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK);
   2138  1.1  mrg 
   2139  1.1  mrg 	      if (have_joiner)
   2140  1.1  mrg 		{
   2141  1.1  mrg 		  basic_block joiner = e->dest;
   2142  1.1  mrg 		  edge final_edge = path->last ()->e;
   2143  1.1  mrg 		  basic_block final_dest = final_edge->dest;
   2144  1.1  mrg 		  edge e2 = find_edge (joiner, final_dest);
   2145  1.1  mrg 
   2146  1.1  mrg 		  if (e2 && !phi_args_equal_on_edges (e2, final_edge))
   2147  1.1  mrg 		    {
   2148  1.1  mrg 		      cancel_thread (path);
   2149  1.1  mrg 		      e->aux = NULL;
   2150  1.1  mrg 		    }
   2151  1.1  mrg 		}
   2152  1.1  mrg 	    }
   2153  1.1  mrg 	}
   2154  1.1  mrg     }
   2155  1.1  mrg 
   2156  1.1  mrg   /* Look for jump threading paths which cross multiple loop headers.
   2157  1.1  mrg 
   2158  1.1  mrg      The code to thread through loop headers will change the CFG in ways
   2159  1.1  mrg      that invalidate the cached loop iteration information.  So we must
   2160  1.1  mrg      detect that case and wipe the cached information.  */
   2161  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
   2162  1.1  mrg     {
   2163  1.1  mrg       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
   2164  1.1  mrg       FOR_EACH_EDGE (e, ei, bb->preds)
   2165  1.1  mrg 	{
   2166  1.1  mrg 	  if (e->aux)
   2167  1.1  mrg 	    {
   2168  1.1  mrg 	      gcc_assert (loops_state_satisfies_p
   2169  1.1  mrg 			    (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS));
   2170  1.1  mrg 	      vec<jump_thread_edge *> *path = THREAD_PATH (e);
   2171  1.1  mrg 
   2172  1.1  mrg 	      for (unsigned int i = 0, crossed_headers = 0;
   2173  1.1  mrg 		   i < path->length ();
   2174  1.1  mrg 		   i++)
   2175  1.1  mrg 		{
   2176  1.1  mrg 		  basic_block dest = (*path)[i]->e->dest;
   2177  1.1  mrg 		  basic_block src = (*path)[i]->e->src;
   2178  1.1  mrg 		  /* If we enter a loop.  */
   2179  1.1  mrg 		  if (flow_loop_nested_p (src->loop_father, dest->loop_father))
   2180  1.1  mrg 		    ++crossed_headers;
   2181  1.1  mrg 		  /* If we step from a block outside an irreducible region
   2182  1.1  mrg 		     to a block inside an irreducible region, then we have
   2183  1.1  mrg 		     crossed into a loop.  */
   2184  1.1  mrg 		  else if (! (src->flags & BB_IRREDUCIBLE_LOOP)
   2185  1.1  mrg 			   && (dest->flags & BB_IRREDUCIBLE_LOOP))
   2186  1.1  mrg 		      ++crossed_headers;
   2187  1.1  mrg 		  if (crossed_headers > 1)
   2188  1.1  mrg 		    {
   2189  1.1  mrg 		      vect_free_loop_info_assumptions
   2190  1.1  mrg 			((*path)[path->length () - 1]->e->dest->loop_father);
   2191  1.1  mrg 		      break;
   2192  1.1  mrg 		    }
   2193  1.1  mrg 		}
   2194  1.1  mrg 	    }
   2195  1.1  mrg 	}
   2196  1.1  mrg     }
   2197  1.1  mrg }
   2198  1.1  mrg 
   2199  1.1  mrg 
   2200  1.1  mrg /* Verify that the REGION is a valid jump thread.  A jump thread is a special
   2201  1.1  mrg    case of SEME Single Entry Multiple Exits region in which all nodes in the
   2202  1.1  mrg    REGION have exactly one incoming edge.  The only exception is the first block
   2203  1.1  mrg    that may not have been connected to the rest of the cfg yet.  */
   2204  1.1  mrg 
   2205  1.1  mrg DEBUG_FUNCTION void
   2206  1.1  mrg verify_jump_thread (basic_block *region, unsigned n_region)
   2207  1.1  mrg {
   2208  1.1  mrg   for (unsigned i = 0; i < n_region; i++)
   2209  1.1  mrg     gcc_assert (EDGE_COUNT (region[i]->preds) <= 1);
   2210  1.1  mrg }
   2211  1.1  mrg 
   2212  1.1  mrg /* Return true when BB is one of the first N items in BBS.  */
   2213  1.1  mrg 
   2214  1.1  mrg static inline bool
   2215  1.1  mrg bb_in_bbs (basic_block bb, basic_block *bbs, int n)
   2216  1.1  mrg {
   2217  1.1  mrg   for (int i = 0; i < n; i++)
   2218  1.1  mrg     if (bb == bbs[i])
   2219  1.1  mrg       return true;
   2220  1.1  mrg 
   2221  1.1  mrg   return false;
   2222  1.1  mrg }
   2223  1.1  mrg 
   2224  1.1  mrg void
   2225  1.1  mrg jt_path_registry::debug_path (FILE *dump_file, int pathno)
   2226  1.1  mrg {
   2227  1.1  mrg   vec<jump_thread_edge *> *p = m_paths[pathno];
   2228  1.1  mrg   fprintf (dump_file, "path: ");
   2229  1.1  mrg   for (unsigned i = 0; i < p->length (); ++i)
   2230  1.1  mrg     fprintf (dump_file, "%d -> %d, ",
   2231  1.1  mrg 	     (*p)[i]->e->src->index, (*p)[i]->e->dest->index);
   2232  1.1  mrg   fprintf (dump_file, "\n");
   2233  1.1  mrg }
   2234  1.1  mrg 
   2235  1.1  mrg void
   2236  1.1  mrg jt_path_registry::debug ()
   2237  1.1  mrg {
   2238  1.1  mrg   for (unsigned i = 0; i < m_paths.length (); ++i)
   2239  1.1  mrg     debug_path (stderr, i);
   2240  1.1  mrg }
   2241  1.1  mrg 
   2242  1.1  mrg /* Rewire a jump_thread_edge so that the source block is now a
   2243  1.1  mrg    threaded source block.
   2244  1.1  mrg 
   2245  1.1  mrg    PATH_NUM is an index into the global path table PATHS.
   2246  1.1  mrg    EDGE_NUM is the jump thread edge number into said path.
   2247  1.1  mrg 
   2248  1.1  mrg    Returns TRUE if we were able to successfully rewire the edge.  */
   2249  1.1  mrg 
   2250  1.1  mrg bool
   2251  1.1  mrg back_jt_path_registry::rewire_first_differing_edge (unsigned path_num,
   2252  1.1  mrg 						    unsigned edge_num)
   2253  1.1  mrg {
   2254  1.1  mrg   vec<jump_thread_edge *> *path = m_paths[path_num];
   2255  1.1  mrg   edge &e = (*path)[edge_num]->e;
   2256  1.1  mrg   if (dump_file && (dump_flags & TDF_DETAILS))
   2257  1.1  mrg     fprintf (dump_file, "rewiring edge candidate: %d -> %d\n",
   2258  1.1  mrg 	     e->src->index, e->dest->index);
   2259  1.1  mrg   basic_block src_copy = get_bb_copy (e->src);
   2260  1.1  mrg   if (src_copy == NULL)
   2261  1.1  mrg     {
   2262  1.1  mrg       if (dump_file && (dump_flags & TDF_DETAILS))
   2263  1.1  mrg 	fprintf (dump_file, "ignoring candidate: there is no src COPY\n");
   2264  1.1  mrg       return false;
   2265  1.1  mrg     }
   2266  1.1  mrg   edge new_edge = find_edge (src_copy, e->dest);
   2267  1.1  mrg   /* If the previously threaded paths created a flow graph where we
   2268  1.1  mrg      can no longer figure out where to go, give up.  */
   2269  1.1  mrg   if (new_edge == NULL)
   2270  1.1  mrg     {
   2271  1.1  mrg       if (dump_file && (dump_flags & TDF_DETAILS))
   2272  1.1  mrg 	fprintf (dump_file, "ignoring candidate: we lost our way\n");
   2273  1.1  mrg       return false;
   2274  1.1  mrg     }
   2275  1.1  mrg   e = new_edge;
   2276  1.1  mrg   return true;
   2277  1.1  mrg }
   2278  1.1  mrg 
   2279  1.1  mrg /* After a path has been jump threaded, adjust the remaining paths
   2280  1.1  mrg    that are subsets of this path, so these paths can be safely
   2281  1.1  mrg    threaded within the context of the new threaded path.
   2282  1.1  mrg 
   2283  1.1  mrg    For example, suppose we have just threaded:
   2284  1.1  mrg 
   2285  1.1  mrg    5 -> 6 -> 7 -> 8 -> 12	=>	5 -> 6' -> 7' -> 8' -> 12'
   2286  1.1  mrg 
   2287  1.1  mrg    And we have an upcoming threading candidate:
   2288  1.1  mrg    5 -> 6 -> 7 -> 8 -> 15 -> 20
   2289  1.1  mrg 
   2290  1.1  mrg    This function adjusts the upcoming path into:
   2291  1.1  mrg    8' -> 15 -> 20
   2292  1.1  mrg 
   2293  1.1  mrg    CURR_PATH_NUM is an index into the global paths table.  It
   2294  1.1  mrg    specifies the path that was just threaded.  */
   2295  1.1  mrg 
   2296  1.1  mrg void
   2297  1.1  mrg back_jt_path_registry::adjust_paths_after_duplication (unsigned curr_path_num)
   2298  1.1  mrg {
   2299  1.1  mrg   vec<jump_thread_edge *> *curr_path = m_paths[curr_path_num];
   2300  1.1  mrg 
   2301  1.1  mrg   /* Iterate through all the other paths and adjust them.  */
   2302  1.1  mrg   for (unsigned cand_path_num = 0; cand_path_num < m_paths.length (); )
   2303  1.1  mrg     {
   2304  1.1  mrg       if (cand_path_num == curr_path_num)
   2305  1.1  mrg 	{
   2306  1.1  mrg 	  ++cand_path_num;
   2307  1.1  mrg 	  continue;
   2308  1.1  mrg 	}
   2309  1.1  mrg       /* Make sure the candidate to adjust starts with the same path
   2310  1.1  mrg 	 as the recently threaded path.  */
   2311  1.1  mrg       vec<jump_thread_edge *> *cand_path = m_paths[cand_path_num];
   2312  1.1  mrg       if ((*cand_path)[0]->e != (*curr_path)[0]->e)
   2313  1.1  mrg 	{
   2314  1.1  mrg 	  ++cand_path_num;
   2315  1.1  mrg 	  continue;
   2316  1.1  mrg 	}
   2317  1.1  mrg       if (dump_file && (dump_flags & TDF_DETAILS))
   2318  1.1  mrg 	{
   2319  1.1  mrg 	  fprintf (dump_file, "adjusting candidate: ");
   2320  1.1  mrg 	  debug_path (dump_file, cand_path_num);
   2321  1.1  mrg 	}
   2322  1.1  mrg 
   2323  1.1  mrg       /* Chop off from the candidate path any prefix it shares with
   2324  1.1  mrg 	 the recently threaded path.  */
   2325  1.1  mrg       unsigned minlength = MIN (curr_path->length (), cand_path->length ());
   2326  1.1  mrg       unsigned j;
   2327  1.1  mrg       for (j = 0; j < minlength; ++j)
   2328  1.1  mrg 	{
   2329  1.1  mrg 	  edge cand_edge = (*cand_path)[j]->e;
   2330  1.1  mrg 	  edge curr_edge = (*curr_path)[j]->e;
   2331  1.1  mrg 
   2332  1.1  mrg 	  /* Once the prefix no longer matches, adjust the first
   2333  1.1  mrg 	     non-matching edge to point from an adjusted edge to
   2334  1.1  mrg 	     wherever it was going.  */
   2335  1.1  mrg 	  if (cand_edge != curr_edge)
   2336  1.1  mrg 	    {
   2337  1.1  mrg 	      gcc_assert (cand_edge->src == curr_edge->src);
   2338  1.1  mrg 	      if (!rewire_first_differing_edge (cand_path_num, j))
   2339  1.1  mrg 		goto remove_candidate_from_list;
   2340  1.1  mrg 	      break;
   2341  1.1  mrg 	    }
   2342  1.1  mrg 	}
   2343  1.1  mrg       if (j == minlength)
   2344  1.1  mrg 	{
   2345  1.1  mrg 	  /* If we consumed the max subgraph we could look at, and
   2346  1.1  mrg 	     still didn't find any different edges, it's the
   2347  1.1  mrg 	     last edge after MINLENGTH.  */
   2348  1.1  mrg 	  if (cand_path->length () > minlength)
   2349  1.1  mrg 	    {
   2350  1.1  mrg 	      if (!rewire_first_differing_edge (cand_path_num, j))
   2351  1.1  mrg 		goto remove_candidate_from_list;
   2352  1.1  mrg 	    }
   2353  1.1  mrg 	  else if (dump_file && (dump_flags & TDF_DETAILS))
   2354  1.1  mrg 	    fprintf (dump_file, "adjusting first edge after MINLENGTH.\n");
   2355  1.1  mrg 	}
   2356  1.1  mrg       if (j > 0)
   2357  1.1  mrg 	{
   2358  1.1  mrg 	  /* If we are removing everything, delete the entire candidate.  */
   2359  1.1  mrg 	  if (j == cand_path->length ())
   2360  1.1  mrg 	    {
   2361  1.1  mrg 	    remove_candidate_from_list:
   2362  1.1  mrg 	      cancel_thread (cand_path, "Adjusted candidate is EMPTY");
   2363  1.1  mrg 	      m_paths.unordered_remove (cand_path_num);
   2364  1.1  mrg 	      continue;
   2365  1.1  mrg 	    }
   2366  1.1  mrg 	  /* Otherwise, just remove the redundant sub-path.  */
   2367  1.1  mrg 	  if (cand_path->length () - j > 1)
   2368  1.1  mrg 	    cand_path->block_remove (0, j);
   2369  1.1  mrg 	  else if (dump_file && (dump_flags & TDF_DETAILS))
   2370  1.1  mrg 	    fprintf (dump_file, "Dropping illformed candidate.\n");
   2371  1.1  mrg 	}
   2372  1.1  mrg       if (dump_file && (dump_flags & TDF_DETAILS))
   2373  1.1  mrg 	{
   2374  1.1  mrg 	  fprintf (dump_file, "adjusted candidate: ");
   2375  1.1  mrg 	  debug_path (dump_file, cand_path_num);
   2376  1.1  mrg 	}
   2377  1.1  mrg       ++cand_path_num;
   2378  1.1  mrg     }
   2379  1.1  mrg }
   2380  1.1  mrg 
   2381  1.1  mrg /* Duplicates a jump-thread path of N_REGION basic blocks.
   2382  1.1  mrg    The ENTRY edge is redirected to the duplicate of the region.
   2383  1.1  mrg 
   2384  1.1  mrg    Remove the last conditional statement in the last basic block in the REGION,
   2385  1.1  mrg    and create a single fallthru edge pointing to the same destination as the
   2386  1.1  mrg    EXIT edge.
   2387  1.1  mrg 
   2388  1.1  mrg    CURRENT_PATH_NO is an index into the global paths[] table
   2389  1.1  mrg    specifying the jump-thread path.
   2390  1.1  mrg 
   2391  1.1  mrg    Returns false if it is unable to copy the region, true otherwise.  */
   2392  1.1  mrg 
   2393  1.1  mrg bool
   2394  1.1  mrg back_jt_path_registry::duplicate_thread_path (edge entry,
   2395  1.1  mrg 					      edge exit,
   2396  1.1  mrg 					      basic_block *region,
   2397  1.1  mrg 					      unsigned n_region,
   2398  1.1  mrg 					      unsigned current_path_no)
   2399  1.1  mrg {
   2400  1.1  mrg   unsigned i;
   2401  1.1  mrg   class loop *loop = entry->dest->loop_father;
   2402  1.1  mrg   edge exit_copy;
   2403  1.1  mrg   edge redirected;
   2404  1.1  mrg   profile_count curr_count;
   2405  1.1  mrg 
   2406  1.1  mrg   if (!can_copy_bbs_p (region, n_region))
   2407  1.1  mrg     return false;
   2408  1.1  mrg 
   2409  1.1  mrg   /* Some sanity checking.  Note that we do not check for all possible
   2410  1.1  mrg      missuses of the functions.  I.e. if you ask to copy something weird,
   2411  1.1  mrg      it will work, but the state of structures probably will not be
   2412  1.1  mrg      correct.  */
   2413  1.1  mrg   for (i = 0; i < n_region; i++)
   2414  1.1  mrg     {
   2415  1.1  mrg       /* We do not handle subloops, i.e. all the blocks must belong to the
   2416  1.1  mrg 	 same loop.  */
   2417  1.1  mrg       if (region[i]->loop_father != loop)
   2418  1.1  mrg 	return false;
   2419  1.1  mrg     }
   2420  1.1  mrg 
   2421  1.1  mrg   initialize_original_copy_tables ();
   2422  1.1  mrg 
   2423  1.1  mrg   set_loop_copy (loop, loop);
   2424  1.1  mrg 
   2425  1.1  mrg   basic_block *region_copy = XNEWVEC (basic_block, n_region);
   2426  1.1  mrg   copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
   2427  1.1  mrg 	    split_edge_bb_loc (entry), false);
   2428  1.1  mrg 
   2429  1.1  mrg   /* Fix up: copy_bbs redirects all edges pointing to copied blocks.  The
   2430  1.1  mrg      following code ensures that all the edges exiting the jump-thread path are
   2431  1.1  mrg      redirected back to the original code: these edges are exceptions
   2432  1.1  mrg      invalidating the property that is propagated by executing all the blocks of
   2433  1.1  mrg      the jump-thread path in order.  */
   2434  1.1  mrg 
   2435  1.1  mrg   curr_count = entry->count ();
   2436  1.1  mrg 
   2437  1.1  mrg   for (i = 0; i < n_region; i++)
   2438  1.1  mrg     {
   2439  1.1  mrg       edge e;
   2440  1.1  mrg       edge_iterator ei;
   2441  1.1  mrg       basic_block bb = region_copy[i];
   2442  1.1  mrg 
   2443  1.1  mrg       /* Watch inconsistent profile.  */
   2444  1.1  mrg       if (curr_count > region[i]->count)
   2445  1.1  mrg 	curr_count = region[i]->count;
   2446  1.1  mrg       /* Scale current BB.  */
   2447  1.1  mrg       if (region[i]->count.nonzero_p () && curr_count.initialized_p ())
   2448  1.1  mrg 	{
   2449  1.1  mrg 	  /* In the middle of the path we only scale the frequencies.
   2450  1.1  mrg 	     In last BB we need to update probabilities of outgoing edges
   2451  1.1  mrg 	     because we know which one is taken at the threaded path.  */
   2452  1.1  mrg 	  if (i + 1 != n_region)
   2453  1.1  mrg 	    scale_bbs_frequencies_profile_count (region + i, 1,
   2454  1.1  mrg 					         region[i]->count - curr_count,
   2455  1.1  mrg 					         region[i]->count);
   2456  1.1  mrg 	  else
   2457  1.1  mrg 	    update_bb_profile_for_threading (region[i],
   2458  1.1  mrg 					     curr_count,
   2459  1.1  mrg 					     exit);
   2460  1.1  mrg 	  scale_bbs_frequencies_profile_count (region_copy + i, 1, curr_count,
   2461  1.1  mrg 					       region_copy[i]->count);
   2462  1.1  mrg 	}
   2463  1.1  mrg 
   2464  1.1  mrg       if (single_succ_p (bb))
   2465  1.1  mrg 	{
   2466  1.1  mrg 	  /* Make sure the successor is the next node in the path.  */
   2467  1.1  mrg 	  gcc_assert (i + 1 == n_region
   2468  1.1  mrg 		      || region_copy[i + 1] == single_succ_edge (bb)->dest);
   2469  1.1  mrg 	  if (i + 1 != n_region)
   2470  1.1  mrg 	    {
   2471  1.1  mrg 	      curr_count = single_succ_edge (bb)->count ();
   2472  1.1  mrg 	    }
   2473  1.1  mrg 	  continue;
   2474  1.1  mrg 	}
   2475  1.1  mrg 
   2476  1.1  mrg       /* Special case the last block on the path: make sure that it does not
   2477  1.1  mrg 	 jump back on the copied path, including back to itself.  */
   2478  1.1  mrg       if (i + 1 == n_region)
   2479  1.1  mrg 	{
   2480  1.1  mrg 	  FOR_EACH_EDGE (e, ei, bb->succs)
   2481  1.1  mrg 	    if (bb_in_bbs (e->dest, region_copy, n_region))
   2482  1.1  mrg 	      {
   2483  1.1  mrg 		basic_block orig = get_bb_original (e->dest);
   2484  1.1  mrg 		if (orig)
   2485  1.1  mrg 		  redirect_edge_and_branch_force (e, orig);
   2486  1.1  mrg 	      }
   2487  1.1  mrg 	  continue;
   2488  1.1  mrg 	}
   2489  1.1  mrg 
   2490  1.1  mrg       /* Redirect all other edges jumping to non-adjacent blocks back to the
   2491  1.1  mrg 	 original code.  */
   2492  1.1  mrg       FOR_EACH_EDGE (e, ei, bb->succs)
   2493  1.1  mrg 	if (region_copy[i + 1] != e->dest)
   2494  1.1  mrg 	  {
   2495  1.1  mrg 	    basic_block orig = get_bb_original (e->dest);
   2496  1.1  mrg 	    if (orig)
   2497  1.1  mrg 	      redirect_edge_and_branch_force (e, orig);
   2498  1.1  mrg 	  }
   2499  1.1  mrg 	else
   2500  1.1  mrg 	  {
   2501  1.1  mrg 	    curr_count = e->count ();
   2502  1.1  mrg 	  }
   2503  1.1  mrg     }
   2504  1.1  mrg 
   2505  1.1  mrg 
   2506  1.1  mrg   if (flag_checking)
   2507  1.1  mrg     verify_jump_thread (region_copy, n_region);
   2508  1.1  mrg 
   2509  1.1  mrg   /* Remove the last branch in the jump thread path.  */
   2510  1.1  mrg   remove_ctrl_stmt_and_useless_edges (region_copy[n_region - 1], exit->dest);
   2511  1.1  mrg 
   2512  1.1  mrg   /* And fixup the flags on the single remaining edge.  */
   2513  1.1  mrg   edge fix_e = find_edge (region_copy[n_region - 1], exit->dest);
   2514  1.1  mrg   fix_e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
   2515  1.1  mrg   fix_e->flags |= EDGE_FALLTHRU;
   2516  1.1  mrg 
   2517  1.1  mrg   edge e = make_edge (region_copy[n_region - 1], exit->dest, EDGE_FALLTHRU);
   2518  1.1  mrg 
   2519  1.1  mrg   if (e)
   2520  1.1  mrg     {
   2521  1.1  mrg       rescan_loop_exit (e, true, false);
   2522  1.1  mrg       e->probability = profile_probability::always ();
   2523  1.1  mrg     }
   2524  1.1  mrg 
   2525  1.1  mrg   /* Redirect the entry and add the phi node arguments.  */
   2526  1.1  mrg   if (entry->dest == loop->header)
   2527  1.1  mrg     mark_loop_for_removal (loop);
   2528  1.1  mrg   redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
   2529  1.1  mrg   gcc_assert (redirected != NULL);
   2530  1.1  mrg   flush_pending_stmts (entry);
   2531  1.1  mrg 
   2532  1.1  mrg   /* Add the other PHI node arguments.  */
   2533  1.1  mrg   add_phi_args_after_copy (region_copy, n_region, NULL);
   2534  1.1  mrg 
   2535  1.1  mrg   free (region_copy);
   2536  1.1  mrg 
   2537  1.1  mrg   adjust_paths_after_duplication (current_path_no);
   2538  1.1  mrg 
   2539  1.1  mrg   free_original_copy_tables ();
   2540  1.1  mrg   return true;
   2541  1.1  mrg }
   2542  1.1  mrg 
   2543  1.1  mrg /* Return true when PATH is a valid jump-thread path.  */
   2544  1.1  mrg 
   2545  1.1  mrg static bool
   2546  1.1  mrg valid_jump_thread_path (vec<jump_thread_edge *> *path)
   2547  1.1  mrg {
   2548  1.1  mrg   unsigned len = path->length ();
   2549  1.1  mrg 
   2550  1.1  mrg   /* Check that the path is connected.  */
   2551  1.1  mrg   for (unsigned int j = 0; j < len - 1; j++)
   2552  1.1  mrg     {
   2553  1.1  mrg       edge e = (*path)[j]->e;
   2554  1.1  mrg       if (e->dest != (*path)[j+1]->e->src)
   2555  1.1  mrg 	return false;
   2556  1.1  mrg     }
   2557  1.1  mrg   return true;
   2558  1.1  mrg }
   2559  1.1  mrg 
   2560  1.1  mrg /* Remove any queued jump threads that include edge E.
   2561  1.1  mrg 
   2562  1.1  mrg    We don't actually remove them here, just record the edges into ax
   2563  1.1  mrg    hash table.  That way we can do the search once per iteration of
   2564  1.1  mrg    DOM/VRP rather than for every case where DOM optimizes away a COND_EXPR.  */
   2565  1.1  mrg 
   2566  1.1  mrg void
   2567  1.1  mrg fwd_jt_path_registry::remove_jump_threads_including (edge_def *e)
   2568  1.1  mrg {
   2569  1.1  mrg   if (!m_paths.exists () || !flag_thread_jumps)
   2570  1.1  mrg     return;
   2571  1.1  mrg 
   2572  1.1  mrg   edge *slot = m_removed_edges->find_slot (e, INSERT);
   2573  1.1  mrg   *slot = e;
   2574  1.1  mrg }
   2575  1.1  mrg 
   2576  1.1  mrg /* Thread all paths that have been queued for jump threading, and
   2577  1.1  mrg    update the CFG accordingly.
   2578  1.1  mrg 
   2579  1.1  mrg    It is the caller's responsibility to fix the dominance information
   2580  1.1  mrg    and rewrite duplicated SSA_NAMEs back into SSA form.
   2581  1.1  mrg 
   2582  1.1  mrg    If PEEL_LOOP_HEADERS is false, avoid threading edges through loop
   2583  1.1  mrg    headers if it does not simplify the loop.
   2584  1.1  mrg 
   2585  1.1  mrg    Returns true if one or more edges were threaded.  */
   2586  1.1  mrg 
   2587  1.1  mrg bool
   2588  1.1  mrg jt_path_registry::thread_through_all_blocks (bool peel_loop_headers)
   2589  1.1  mrg {
   2590  1.1  mrg   if (m_paths.length () == 0)
   2591  1.1  mrg     return false;
   2592  1.1  mrg 
   2593  1.1  mrg   m_num_threaded_edges = 0;
   2594  1.1  mrg 
   2595  1.1  mrg   bool retval = update_cfg (peel_loop_headers);
   2596  1.1  mrg 
   2597  1.1  mrg   statistics_counter_event (cfun, "Jumps threaded", m_num_threaded_edges);
   2598  1.1  mrg 
   2599  1.1  mrg   if (retval)
   2600  1.1  mrg     {
   2601  1.1  mrg       loops_state_set (LOOPS_NEED_FIXUP);
   2602  1.1  mrg       return true;
   2603  1.1  mrg     }
   2604  1.1  mrg   return false;
   2605  1.1  mrg }
   2606  1.1  mrg 
   2607  1.1  mrg /* This is the backward threader version of thread_through_all_blocks
   2608  1.1  mrg    using a generic BB copier.  */
   2609  1.1  mrg 
   2610  1.1  mrg bool
   2611  1.1  mrg back_jt_path_registry::update_cfg (bool /*peel_loop_headers*/)
   2612  1.1  mrg {
   2613  1.1  mrg   bool retval = false;
   2614  1.1  mrg   hash_set<edge> visited_starting_edges;
   2615  1.1  mrg 
   2616  1.1  mrg   while (m_paths.length ())
   2617  1.1  mrg     {
   2618  1.1  mrg       vec<jump_thread_edge *> *path = m_paths[0];
   2619  1.1  mrg       edge entry = (*path)[0]->e;
   2620  1.1  mrg 
   2621  1.1  mrg       /* Do not jump-thread twice from the same starting edge.
   2622  1.1  mrg 
   2623  1.1  mrg 	 Previously we only checked that we weren't threading twice
   2624  1.1  mrg 	 from the same BB, but that was too restrictive.  Imagine a
   2625  1.1  mrg 	 path that starts from GIMPLE_COND(x_123 == 0,...), where both
   2626  1.1  mrg 	 edges out of this conditional yield paths that can be
   2627  1.1  mrg 	 threaded (for example, both lead to an x_123==0 or x_123!=0
   2628  1.1  mrg 	 conditional further down the line.  */
   2629  1.1  mrg       if (visited_starting_edges.contains (entry)
   2630  1.1  mrg 	  /* We may not want to realize this jump thread path for
   2631  1.1  mrg 	     various reasons.  So check it first.  */
   2632  1.1  mrg 	  || !valid_jump_thread_path (path))
   2633  1.1  mrg 	{
   2634  1.1  mrg 	  /* Remove invalid jump-thread paths.  */
   2635  1.1  mrg 	  cancel_thread (path, "Avoiding threading twice from same edge");
   2636  1.1  mrg 	  m_paths.unordered_remove (0);
   2637  1.1  mrg 	  continue;
   2638  1.1  mrg 	}
   2639  1.1  mrg 
   2640  1.1  mrg       unsigned len = path->length ();
   2641  1.1  mrg       edge exit = (*path)[len - 1]->e;
   2642  1.1  mrg       basic_block *region = XNEWVEC (basic_block, len - 1);
   2643  1.1  mrg 
   2644  1.1  mrg       for (unsigned int j = 0; j < len - 1; j++)
   2645  1.1  mrg 	region[j] = (*path)[j]->e->dest;
   2646  1.1  mrg 
   2647  1.1  mrg       if (duplicate_thread_path (entry, exit, region, len - 1, 0))
   2648  1.1  mrg 	{
   2649  1.1  mrg 	  /* We do not update dominance info.  */
   2650  1.1  mrg 	  free_dominance_info (CDI_DOMINATORS);
   2651  1.1  mrg 	  visited_starting_edges.add (entry);
   2652  1.1  mrg 	  retval = true;
   2653  1.1  mrg 	  m_num_threaded_edges++;
   2654  1.1  mrg 	}
   2655  1.1  mrg 
   2656  1.1  mrg       path->release ();
   2657  1.1  mrg       m_paths.unordered_remove (0);
   2658  1.1  mrg       free (region);
   2659  1.1  mrg     }
   2660  1.1  mrg   return retval;
   2661  1.1  mrg }
   2662  1.1  mrg 
   2663  1.1  mrg /* This is the forward threader version of thread_through_all_blocks,
   2664  1.1  mrg    using a custom BB copier.  */
   2665  1.1  mrg 
   2666  1.1  mrg bool
   2667  1.1  mrg fwd_jt_path_registry::update_cfg (bool may_peel_loop_headers)
   2668  1.1  mrg {
   2669  1.1  mrg   bool retval = false;
   2670  1.1  mrg 
   2671  1.1  mrg   /* Remove any paths that referenced removed edges.  */
   2672  1.1  mrg   if (m_removed_edges)
   2673  1.1  mrg     for (unsigned i = 0; i < m_paths.length (); )
   2674  1.1  mrg       {
   2675  1.1  mrg 	unsigned int j;
   2676  1.1  mrg 	vec<jump_thread_edge *> *path = m_paths[i];
   2677  1.1  mrg 
   2678  1.1  mrg 	for (j = 0; j < path->length (); j++)
   2679  1.1  mrg 	  {
   2680  1.1  mrg 	    edge e = (*path)[j]->e;
   2681  1.1  mrg 	    if (m_removed_edges->find_slot (e, NO_INSERT))
   2682  1.1  mrg 	      break;
   2683  1.1  mrg 	  }
   2684  1.1  mrg 
   2685  1.1  mrg 	if (j != path->length ())
   2686  1.1  mrg 	  {
   2687  1.1  mrg 	    cancel_thread (path, "Thread references removed edge");
   2688  1.1  mrg 	    m_paths.unordered_remove (i);
   2689  1.1  mrg 	    continue;
   2690  1.1  mrg 	  }
   2691  1.1  mrg 	i++;
   2692  1.1  mrg       }
   2693  1.1  mrg 
   2694  1.1  mrg   auto_bitmap threaded_blocks;
   2695  1.1  mrg   mark_threaded_blocks (threaded_blocks);
   2696  1.1  mrg 
   2697  1.1  mrg   initialize_original_copy_tables ();
   2698  1.1  mrg 
   2699  1.1  mrg   /* The order in which we process jump threads can be important.
   2700  1.1  mrg 
   2701  1.1  mrg      Consider if we have two jump threading paths A and B.  If the
   2702  1.1  mrg      target edge of A is the starting edge of B and we thread path A
   2703  1.1  mrg      first, then we create an additional incoming edge into B->dest that
   2704  1.1  mrg      we cannot discover as a jump threading path on this iteration.
   2705  1.1  mrg 
   2706  1.1  mrg      If we instead thread B first, then the edge into B->dest will have
   2707  1.1  mrg      already been redirected before we process path A and path A will
   2708  1.1  mrg      natually, with no further work, target the redirected path for B.
   2709  1.1  mrg 
   2710  1.1  mrg      An post-order is sufficient here.  Compute the ordering first, then
   2711  1.1  mrg      process the blocks.  */
   2712  1.1  mrg   if (!bitmap_empty_p (threaded_blocks))
   2713  1.1  mrg     {
   2714  1.1  mrg       int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
   2715  1.1  mrg       unsigned int postorder_num = post_order_compute (postorder, false, false);
   2716  1.1  mrg       for (unsigned int i = 0; i < postorder_num; i++)
   2717  1.1  mrg 	{
   2718  1.1  mrg 	  unsigned int indx = postorder[i];
   2719  1.1  mrg 	  if (bitmap_bit_p (threaded_blocks, indx))
   2720  1.1  mrg 	    {
   2721  1.1  mrg 	      basic_block bb = BASIC_BLOCK_FOR_FN (cfun, indx);
   2722  1.1  mrg 	      retval |= thread_block (bb, true);
   2723  1.1  mrg 	    }
   2724  1.1  mrg 	}
   2725  1.1  mrg       free (postorder);
   2726  1.1  mrg     }
   2727  1.1  mrg 
   2728  1.1  mrg   /* Then perform the threading through loop headers.  We start with the
   2729  1.1  mrg      innermost loop, so that the changes in cfg we perform won't affect
   2730  1.1  mrg      further threading.  */
   2731  1.1  mrg   for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
   2732  1.1  mrg     {
   2733  1.1  mrg       if (!loop->header
   2734  1.1  mrg 	  || !bitmap_bit_p (threaded_blocks, loop->header->index))
   2735  1.1  mrg 	continue;
   2736  1.1  mrg 
   2737  1.1  mrg       retval |= thread_through_loop_header (loop, may_peel_loop_headers);
   2738  1.1  mrg     }
   2739  1.1  mrg 
   2740  1.1  mrg   /* All jump threading paths should have been resolved at this
   2741  1.1  mrg      point.  Verify that is the case.  */
   2742  1.1  mrg   basic_block bb;
   2743  1.1  mrg   FOR_EACH_BB_FN (bb, cfun)
   2744  1.1  mrg     {
   2745  1.1  mrg       edge_iterator ei;
   2746  1.1  mrg       edge e;
   2747  1.1  mrg       FOR_EACH_EDGE (e, ei, bb->preds)
   2748  1.1  mrg 	gcc_assert (e->aux == NULL);
   2749  1.1  mrg     }
   2750  1.1  mrg 
   2751  1.1  mrg   free_original_copy_tables ();
   2752  1.1  mrg 
   2753  1.1  mrg   return retval;
   2754  1.1  mrg }
   2755  1.1  mrg 
   2756  1.1  mrg bool
   2757  1.1  mrg jt_path_registry::cancel_invalid_paths (vec<jump_thread_edge *> &path)
   2758  1.1  mrg {
   2759  1.1  mrg   gcc_checking_assert (!path.is_empty ());
   2760  1.1  mrg   edge entry = path[0]->e;
   2761  1.1  mrg   edge exit = path[path.length () - 1]->e;
   2762  1.1  mrg   bool seen_latch = false;
   2763  1.1  mrg   int loops_crossed = 0;
   2764  1.1  mrg   bool crossed_latch = false;
   2765  1.1  mrg   bool crossed_loop_header = false;
   2766  1.1  mrg   // Use ->dest here instead of ->src to ignore the first block.  The
   2767  1.1  mrg   // first block is allowed to be in a different loop, since it'll be
   2768  1.1  mrg   // redirected.  See similar comment in profitable_path_p: "we don't
   2769  1.1  mrg   // care about that block...".
   2770  1.1  mrg   loop_p loop = entry->dest->loop_father;
   2771  1.1  mrg   loop_p curr_loop = loop;
   2772  1.1  mrg 
   2773  1.1  mrg   for (unsigned int i = 0; i < path.length (); i++)
   2774  1.1  mrg     {
   2775  1.1  mrg       edge e = path[i]->e;
   2776  1.1  mrg 
   2777  1.1  mrg       if (e == NULL)
   2778  1.1  mrg 	{
   2779  1.1  mrg 	  // NULL outgoing edges on a path can happen for jumping to a
   2780  1.1  mrg 	  // constant address.
   2781  1.1  mrg 	  cancel_thread (&path, "Found NULL edge in jump threading path");
   2782  1.1  mrg 	  return true;
   2783  1.1  mrg 	}
   2784  1.1  mrg 
   2785  1.1  mrg       if (loop->latch == e->src || loop->latch == e->dest)
   2786  1.1  mrg 	{
   2787  1.1  mrg 	  seen_latch = true;
   2788  1.1  mrg 	  // Like seen_latch, but excludes the first block.
   2789  1.1  mrg 	  if (e->src != entry->src)
   2790  1.1  mrg 	    crossed_latch = true;
   2791  1.1  mrg 	}
   2792  1.1  mrg 
   2793  1.1  mrg       if (e->dest->loop_father != curr_loop)
   2794  1.1  mrg 	{
   2795  1.1  mrg 	  curr_loop = e->dest->loop_father;
   2796  1.1  mrg 	  ++loops_crossed;
   2797  1.1  mrg 	}
   2798  1.1  mrg 
   2799  1.1  mrg       // ?? Avoid threading through loop headers that remain in the
   2800  1.1  mrg       // loop, as such threadings tend to create sub-loops which
   2801  1.1  mrg       // _might_ be OK ??.
   2802  1.1  mrg       if (e->dest->loop_father->header == e->dest
   2803  1.1  mrg 	  && !flow_loop_nested_p (exit->dest->loop_father,
   2804  1.1  mrg 				  e->dest->loop_father))
   2805  1.1  mrg 	crossed_loop_header = true;
   2806  1.1  mrg 
   2807  1.1  mrg       if (flag_checking && !m_backedge_threads)
   2808  1.1  mrg 	gcc_assert ((path[i]->e->flags & EDGE_DFS_BACK) == 0);
   2809  1.1  mrg     }
   2810  1.1  mrg 
   2811  1.1  mrg   // If we crossed a loop into an outer loop without crossing the
   2812  1.1  mrg   // latch, this is just an early exit from the loop.
   2813  1.1  mrg   if (loops_crossed == 1
   2814  1.1  mrg       && !crossed_latch
   2815  1.1  mrg       && flow_loop_nested_p (exit->dest->loop_father, exit->src->loop_father))
   2816  1.1  mrg     return false;
   2817  1.1  mrg 
   2818  1.1  mrg   if (cfun->curr_properties & PROP_loop_opts_done)
   2819  1.1  mrg     return false;
   2820  1.1  mrg 
   2821  1.1  mrg   if (seen_latch && empty_block_p (loop->latch))
   2822  1.1  mrg     {
   2823  1.1  mrg       cancel_thread (&path, "Threading through latch before loop opts "
   2824  1.1  mrg 		     "would create non-empty latch");
   2825  1.1  mrg       return true;
   2826  1.1  mrg     }
   2827  1.1  mrg   if (loops_crossed)
   2828  1.1  mrg     {
   2829  1.1  mrg       cancel_thread (&path, "Path crosses loops");
   2830  1.1  mrg       return true;
   2831  1.1  mrg     }
   2832  1.1  mrg   // The path should either start and end in the same loop or exit the
   2833  1.1  mrg   // loop it starts in but never enter a loop.  This also catches
   2834  1.1  mrg   // creating irreducible loops, not only rotation.
   2835  1.1  mrg   if (entry->src->loop_father != exit->dest->loop_father
   2836  1.1  mrg       && !flow_loop_nested_p (exit->src->loop_father,
   2837  1.1  mrg 			      entry->dest->loop_father))
   2838  1.1  mrg     {
   2839  1.1  mrg       cancel_thread (&path, "Path rotates loop");
   2840  1.1  mrg       return true;
   2841  1.1  mrg     }
   2842  1.1  mrg   if (crossed_loop_header)
   2843  1.1  mrg     {
   2844  1.1  mrg       cancel_thread (&path, "Path crosses loop header but does not exit it");
   2845  1.1  mrg       return true;
   2846  1.1  mrg     }
   2847  1.1  mrg   return false;
   2848  1.1  mrg }
   2849  1.1  mrg 
   2850  1.1  mrg /* Register a jump threading opportunity.  We queue up all the jump
   2851  1.1  mrg    threading opportunities discovered by a pass and update the CFG
   2852  1.1  mrg    and SSA form all at once.
   2853  1.1  mrg 
   2854  1.1  mrg    E is the edge we can thread, E2 is the new target edge, i.e., we
   2855  1.1  mrg    are effectively recording that E->dest can be changed to E2->dest
   2856  1.1  mrg    after fixing the SSA graph.
   2857  1.1  mrg 
   2858  1.1  mrg    Return TRUE if PATH was successfully threaded.  */
   2859  1.1  mrg 
   2860  1.1  mrg bool
   2861  1.1  mrg jt_path_registry::register_jump_thread (vec<jump_thread_edge *> *path)
   2862  1.1  mrg {
   2863  1.1  mrg   gcc_checking_assert (flag_thread_jumps);
   2864  1.1  mrg 
   2865  1.1  mrg   if (!dbg_cnt (registered_jump_thread))
   2866  1.1  mrg     {
   2867  1.1  mrg       path->release ();
   2868  1.1  mrg       return false;
   2869  1.1  mrg     }
   2870  1.1  mrg 
   2871  1.1  mrg   if (cancel_invalid_paths (*path))
   2872  1.1  mrg     return false;
   2873  1.1  mrg 
   2874  1.1  mrg   if (dump_file && (dump_flags & TDF_DETAILS))
   2875  1.1  mrg     dump_jump_thread_path (dump_file, *path, true);
   2876  1.1  mrg 
   2877  1.1  mrg   m_paths.safe_push (path);
   2878  1.1  mrg   return true;
   2879  1.1  mrg }
   2880  1.1  mrg 
   2881  1.1  mrg /* Return how many uses of T there are within BB, as long as there
   2882  1.1  mrg    aren't any uses outside BB.  If there are any uses outside BB,
   2883  1.1  mrg    return -1 if there's at most one use within BB, or -2 if there is
   2884  1.1  mrg    more than one use within BB.  */
   2885  1.1  mrg 
   2886  1.1  mrg static int
   2887  1.1  mrg uses_in_bb (tree t, basic_block bb)
   2888  1.1  mrg {
   2889  1.1  mrg   int uses = 0;
   2890  1.1  mrg   bool outside_bb = false;
   2891  1.1  mrg 
   2892  1.1  mrg   imm_use_iterator iter;
   2893  1.1  mrg   use_operand_p use_p;
   2894  1.1  mrg   FOR_EACH_IMM_USE_FAST (use_p, iter, t)
   2895  1.1  mrg     {
   2896  1.1  mrg       if (is_gimple_debug (USE_STMT (use_p)))
   2897  1.1  mrg 	continue;
   2898  1.1  mrg 
   2899  1.1  mrg       if (gimple_bb (USE_STMT (use_p)) != bb)
   2900  1.1  mrg 	outside_bb = true;
   2901  1.1  mrg       else
   2902  1.1  mrg 	uses++;
   2903  1.1  mrg 
   2904  1.1  mrg       if (outside_bb && uses > 1)
   2905  1.1  mrg 	return -2;
   2906  1.1  mrg     }
   2907  1.1  mrg 
   2908  1.1  mrg   if (outside_bb)
   2909  1.1  mrg     return -1;
   2910  1.1  mrg 
   2911  1.1  mrg   return uses;
   2912  1.1  mrg }
   2913  1.1  mrg 
   2914  1.1  mrg /* Starting from the final control flow stmt in BB, assuming it will
   2915  1.1  mrg    be removed, follow uses in to-be-removed stmts back to their defs
   2916  1.1  mrg    and count how many defs are to become dead and be removed as
   2917  1.1  mrg    well.  */
   2918  1.1  mrg 
   2919  1.1  mrg unsigned int
   2920  1.1  mrg estimate_threading_killed_stmts (basic_block bb)
   2921  1.1  mrg {
   2922  1.1  mrg   int killed_stmts = 0;
   2923  1.1  mrg   hash_map<tree, int> ssa_remaining_uses;
   2924  1.1  mrg   auto_vec<gimple *, 4> dead_worklist;
   2925  1.1  mrg 
   2926  1.1  mrg   /* If the block has only two predecessors, threading will turn phi
   2927  1.1  mrg      dsts into either src, so count them as dead stmts.  */
   2928  1.1  mrg   bool drop_all_phis = EDGE_COUNT (bb->preds) == 2;
   2929  1.1  mrg 
   2930  1.1  mrg   if (drop_all_phis)
   2931  1.1  mrg     for (gphi_iterator gsi = gsi_start_phis (bb);
   2932  1.1  mrg 	 !gsi_end_p (gsi); gsi_next (&gsi))
   2933  1.1  mrg       {
   2934  1.1  mrg 	gphi *phi = gsi.phi ();
   2935  1.1  mrg 	tree dst = gimple_phi_result (phi);
   2936  1.1  mrg 
   2937  1.1  mrg 	/* We don't count virtual PHIs as stmts in
   2938  1.1  mrg 	   record_temporary_equivalences_from_phis.  */
   2939  1.1  mrg 	if (virtual_operand_p (dst))
   2940  1.1  mrg 	  continue;
   2941  1.1  mrg 
   2942  1.1  mrg 	killed_stmts++;
   2943  1.1  mrg       }
   2944  1.1  mrg 
   2945  1.1  mrg   if (gsi_end_p (gsi_last_bb (bb)))
   2946  1.1  mrg     return killed_stmts;
   2947  1.1  mrg 
   2948  1.1  mrg   gimple *stmt = gsi_stmt (gsi_last_bb (bb));
   2949  1.1  mrg   if (gimple_code (stmt) != GIMPLE_COND
   2950  1.1  mrg       && gimple_code (stmt) != GIMPLE_GOTO
   2951  1.1  mrg       && gimple_code (stmt) != GIMPLE_SWITCH)
   2952  1.1  mrg     return killed_stmts;
   2953  1.1  mrg 
   2954  1.1  mrg   /* The control statement is always dead.  */
   2955  1.1  mrg   killed_stmts++;
   2956  1.1  mrg   dead_worklist.quick_push (stmt);
   2957  1.1  mrg   while (!dead_worklist.is_empty ())
   2958  1.1  mrg     {
   2959  1.1  mrg       stmt = dead_worklist.pop ();
   2960  1.1  mrg 
   2961  1.1  mrg       ssa_op_iter iter;
   2962  1.1  mrg       use_operand_p use_p;
   2963  1.1  mrg       FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
   2964  1.1  mrg 	{
   2965  1.1  mrg 	  tree t = USE_FROM_PTR (use_p);
   2966  1.1  mrg 	  gimple *def = SSA_NAME_DEF_STMT (t);
   2967  1.1  mrg 
   2968  1.1  mrg 	  if (gimple_bb (def) == bb
   2969  1.1  mrg 	      && (gimple_code (def) != GIMPLE_PHI
   2970  1.1  mrg 		  || !drop_all_phis)
   2971  1.1  mrg 	      && !gimple_has_side_effects (def))
   2972  1.1  mrg 	    {
   2973  1.1  mrg 	      int *usesp = ssa_remaining_uses.get (t);
   2974  1.1  mrg 	      int uses;
   2975  1.1  mrg 
   2976  1.1  mrg 	      if (usesp)
   2977  1.1  mrg 		uses = *usesp;
   2978  1.1  mrg 	      else
   2979  1.1  mrg 		uses = uses_in_bb (t, bb);
   2980  1.1  mrg 
   2981  1.1  mrg 	      gcc_assert (uses);
   2982  1.1  mrg 
   2983  1.1  mrg 	      /* Don't bother recording the expected use count if we
   2984  1.1  mrg 		 won't find any further uses within BB.  */
   2985  1.1  mrg 	      if (!usesp && (uses < -1 || uses > 1))
   2986  1.1  mrg 		{
   2987  1.1  mrg 		  usesp = &ssa_remaining_uses.get_or_insert (t);
   2988  1.1  mrg 		  *usesp = uses;
   2989  1.1  mrg 		}
   2990  1.1  mrg 
   2991  1.1  mrg 	      if (uses < 0)
   2992  1.1  mrg 		continue;
   2993  1.1  mrg 
   2994  1.1  mrg 	      --uses;
   2995  1.1  mrg 	      if (usesp)
   2996  1.1  mrg 		*usesp = uses;
   2997  1.1  mrg 
   2998  1.1  mrg 	      if (!uses)
   2999  1.1  mrg 		{
   3000  1.1  mrg 		  killed_stmts++;
   3001  1.1  mrg 		  if (usesp)
   3002  1.1  mrg 		    ssa_remaining_uses.remove (t);
   3003  1.1  mrg 		  if (gimple_code (def) != GIMPLE_PHI)
   3004  1.1  mrg 		    dead_worklist.safe_push (def);
   3005  1.1  mrg 		}
   3006  1.1  mrg 	    }
   3007  1.1  mrg 	}
   3008  1.1  mrg     }
   3009  1.1  mrg 
   3010  1.1  mrg   if (dump_file)
   3011  1.1  mrg     fprintf (dump_file, "threading bb %i kills %i stmts\n",
   3012  1.1  mrg 	     bb->index, killed_stmts);
   3013  1.1  mrg 
   3014  1.1  mrg   return killed_stmts;
   3015  1.1  mrg }
   3016