LCOV - code coverage report
Current view: top level - gcc - tree-ssa-sink.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 99.4 % 339 337
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 13 13
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Code sinking for trees
       2              :    Copyright (C) 2001-2026 Free Software Foundation, Inc.
       3              :    Contributed by Daniel Berlin <dan@dberlin.org>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify
       8              : it under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful,
      13              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15              : GNU General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "backend.h"
      25              : #include "tree.h"
      26              : #include "gimple.h"
      27              : #include "cfghooks.h"
      28              : #include "tree-pass.h"
      29              : #include "ssa.h"
      30              : #include "gimple-pretty-print.h"
      31              : #include "fold-const.h"
      32              : #include "stor-layout.h"
      33              : #include "cfganal.h"
      34              : #include "gimple-iterator.h"
      35              : #include "tree-cfg.h"
      36              : #include "cfgloop.h"
      37              : #include "tree-eh.h"
      38              : #include "tree-ssa-live.h"
      39              : #include "tree-dfa.h"
      40              : 
      41              : /* TODO:
      42              :    1. Sinking store only using scalar promotion (IE without moving the RHS):
      43              : 
      44              :    *q = p;
      45              :    p = p + 1;
      46              :    if (something)
      47              :      *q = <not p>;
      48              :    else
      49              :      y = *q;
      50              : 
      51              : 
      52              :    should become
      53              :    sinktemp = p;
      54              :    p = p + 1;
      55              :    if (something)
      56              :      *q = <not p>;
      57              :    else
      58              :    {
      59              :      *q = sinktemp;
      60              :      y = *q
      61              :    }
      62              :    Store copy propagation will take care of the store elimination above.
      63              : 
      64              : 
      65              :    2. Sinking using Partial Dead Code Elimination.  */
      66              : 
      67              : 
      68              : static struct
      69              : {
      70              :   /* The number of statements sunk down the flowgraph by code sinking.  */
      71              :   int sunk;
      72              : 
      73              :   /* The number of stores commoned and sunk down by store commoning.  */
      74              :   int commoned;
      75              : } sink_stats;
      76              : 
      77              : 
      78              : /* Given a PHI, and one of its arguments (DEF), find the edge for
      79              :    that argument and return it.  If the argument occurs twice in the PHI node,
      80              :    we return NULL.  */
      81              : 
      82              : static basic_block
      83       734318 : find_bb_for_arg (gphi *phi, tree def)
      84              : {
      85       734318 :   size_t i;
      86       734318 :   bool foundone = false;
      87       734318 :   basic_block result = NULL;
      88      2380081 :   for (i = 0; i < gimple_phi_num_args (phi); i++)
      89      1662304 :     if (PHI_ARG_DEF (phi, i) == def)
      90              :       {
      91       750859 :         if (foundone)
      92              :           return NULL;
      93       734318 :         foundone = true;
      94       734318 :         result = gimple_phi_arg_edge (phi, i)->src;
      95              :       }
      96              :   return result;
      97              : }
      98              : 
      99              : /* When the first immediate use is in a statement, then return true if all
     100              :    immediate uses in IMM are in the same statement.
     101              :    We could also do the case where  the first immediate use is in a phi node,
     102              :    and all the other uses are in phis in the same basic block, but this
     103              :    requires some expensive checking later (you have to make sure no def/vdef
     104              :    in the statement occurs for multiple edges in the various phi nodes it's
     105              :    used in, so that you only have one place you can sink it to.  */
     106              : 
     107              : static bool
     108     13705711 : all_immediate_uses_same_place (def_operand_p def_p)
     109              : {
     110     13705711 :   tree var = DEF_FROM_PTR (def_p);
     111     13705711 :   imm_use_iterator imm_iter;
     112     13705711 :   use_operand_p use_p;
     113              : 
     114     13705711 :   gimple *firstuse = NULL;
     115     29425716 :   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
     116              :     {
     117     19716381 :       if (is_gimple_debug (USE_STMT (use_p)))
     118      1904580 :         continue;
     119     17811801 :       if (firstuse == NULL)
     120              :         firstuse = USE_STMT (use_p);
     121              :       else
     122      4106090 :         if (firstuse != USE_STMT (use_p))
     123      3996376 :           return false;
     124      3996376 :     }
     125              : 
     126      9709335 :   return true;
     127              : }
     128              : 
     129              : /* Find the nearest common dominator of all of the immediate uses in IMM.  */
     130              : 
     131              : static basic_block
     132     11898499 : nearest_common_dominator_of_uses (def_operand_p def_p, bool *debug_stmts)
     133              : {
     134     11898499 :   tree var = DEF_FROM_PTR (def_p);
     135     11898499 :   auto_bitmap blocks;
     136     11898499 :   basic_block commondom;
     137     11898499 :   unsigned int j;
     138     11898499 :   bitmap_iterator bi;
     139     11898499 :   imm_use_iterator imm_iter;
     140     11898499 :   use_operand_p use_p;
     141              : 
     142     42603693 :   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
     143              :     {
     144     30705194 :       gimple *usestmt = USE_STMT (use_p);
     145     30705194 :       basic_block useblock;
     146              : 
     147     30705194 :       if (gphi *phi = dyn_cast <gphi *> (usestmt))
     148              :         {
     149      3802217 :           int idx = PHI_ARG_INDEX_FROM_USE (use_p);
     150              : 
     151      3802217 :           useblock = gimple_phi_arg_edge (phi, idx)->src;
     152              :         }
     153     26902977 :       else if (is_gimple_debug (usestmt))
     154              :         {
     155      6553685 :           *debug_stmts = true;
     156      6553685 :           continue;
     157              :         }
     158              :       else
     159              :         {
     160     20349292 :           useblock = gimple_bb (usestmt);
     161              :         }
     162              : 
     163              :       /* Short circuit. Nothing dominates the entry block.  */
     164     24151509 :       if (useblock == ENTRY_BLOCK_PTR_FOR_FN (cfun))
     165            0 :         return NULL;
     166              : 
     167     24151509 :       bitmap_set_bit (blocks, useblock->index);
     168            0 :     }
     169     11898499 :   commondom = BASIC_BLOCK_FOR_FN (cfun, bitmap_first_set_bit (blocks));
     170     33018727 :   EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi)
     171     21120228 :     commondom = nearest_common_dominator (CDI_DOMINATORS, commondom,
     172     21120228 :                                           BASIC_BLOCK_FOR_FN (cfun, j));
     173              :   return commondom;
     174     11898499 : }
     175              : 
     176              : /* Return whether sinking STMT from EARLY_BB to BEST_BB should be avoided.  */
     177              : 
     178              : static bool
     179      4237450 : do_not_sink (gimple *stmt, basic_block early_bb, basic_block best_bb)
     180              : {
     181              :   /* Placing a statement before a setjmp-like function would be invalid
     182              :      (it cannot be reevaluated when execution follows an abnormal edge).
     183              :      If we selected a block with abnormal predecessors, just punt.  */
     184      4237450 :   if (bb_has_abnormal_pred (best_bb))
     185              :     return true;
     186              : 
     187              :   /* If the latch block is empty, don't make it non-empty by sinking
     188              :      something into it.  */
     189      4237347 :   if (best_bb == early_bb->loop_father->latch
     190      4237347 :       && empty_block_p (best_bb))
     191              :     return true;
     192              : 
     193              :   /* Avoid turning an unconditional read into a conditional one when we
     194              :      still might want to perform vectorization.  */
     195      3934588 :   if (best_bb->loop_father == early_bb->loop_father
     196      2681552 :       && loop_outer (best_bb->loop_father)
     197       692053 :       && !best_bb->loop_father->inner
     198       356995 :       && gimple_vuse (stmt)
     199       112991 :       && !gimple_vdef (stmt)
     200       102480 :       && flag_tree_loop_vectorize
     201        96608 :       && !(cfun->curr_properties & PROP_loop_opts_done)
     202        60298 :       && dominated_by_p (CDI_DOMINATORS, best_bb->loop_father->latch, early_bb)
     203      3978551 :       && !dominated_by_p (CDI_DOMINATORS, best_bb->loop_father->latch, best_bb))
     204        17771 :     return true;
     205              : 
     206              :   return false;
     207              : }
     208              : 
     209              : /* Given EARLY_BB and LATE_BB, two blocks in a path through the dominator
     210              :    tree, return the best basic block between them (inclusive) to place
     211              :    statements.
     212              : 
     213              :    We want the most control dependent block in the shallowest loop nest.
     214              : 
     215              :    If the resulting block is in a shallower loop nest, then use it.  */
     216              : 
     217              : static basic_block
     218     16682427 : select_best_block (basic_block early_bb,
     219              :                    basic_block late_bb,
     220              :                    gimple *stmt)
     221              : {
     222              :   /* First pick a block we do not disqualify.  */
     223     16682427 :   while (late_bb != early_bb
     224     17002901 :          && do_not_sink (stmt, early_bb, late_bb))
     225       320474 :     late_bb = get_immediate_dominator (CDI_DOMINATORS, late_bb);
     226              : 
     227     16682427 :   basic_block best_bb = late_bb;
     228     16682427 :   basic_block temp_bb = late_bb;
     229     19818028 :   while (temp_bb != early_bb)
     230              :     {
     231              :       /* Walk up the dominator tree, hopefully we'll find a shallower
     232              :          loop nest.  */
     233      3135601 :       temp_bb = get_immediate_dominator (CDI_DOMINATORS, temp_bb);
     234              : 
     235              :       /* Do not consider blocks we do not want to sink to.  */
     236      3135601 :       if (temp_bb != early_bb && do_not_sink (stmt, early_bb, temp_bb))
     237              :         ;
     238              : 
     239              :       /* If we've moved into a lower loop nest, then that becomes
     240              :          our best block.  */
     241      3135442 :       else if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb))
     242              :         best_bb = temp_bb;
     243              : 
     244              :       /* A higher loop nest is always worse.  */
     245      2546615 :       else if (bb_loop_depth (temp_bb) > bb_loop_depth (best_bb))
     246              :         ;
     247              : 
     248              :       /* Likewise an irreducible region inside an otherwise same loop
     249              :          depth.  */
     250      2275950 :       else if ((temp_bb->flags & BB_IRREDUCIBLE_LOOP)
     251         5995 :                && !(best_bb->flags & BB_IRREDUCIBLE_LOOP))
     252              :         ;
     253              : 
     254              :       /* But sink the least distance, if the new candidate on the same
     255              :          loop depth is post-dominated by the current best block pick
     256              :          the new candidate.  */
     257      2275545 :       else if (dominated_by_p (CDI_POST_DOMINATORS, temp_bb, best_bb))
     258              :         best_bb = temp_bb;
     259              : 
     260              :       /* Avoid sinking across a conditional branching to exceptional
     261              :          code.  In practice this does not reduce the number of dynamic
     262              :          executions of the sunk statement (this includes EH and
     263              :          branches leading to abort for example).  Treat this case as
     264              :          post-dominating.  */
     265      1623581 :       else if (single_pred_p (best_bb)
     266      1458662 :                && single_pred_edge (best_bb)->src == temp_bb
     267      2575796 :                && (single_pred_edge (best_bb)->flags & EDGE_FALLTHRU
     268       911047 :                    || (single_pred_edge (best_bb)->probability
     269      2373109 :                        >= profile_probability::always ())))
     270      3135601 :         best_bb = temp_bb;
     271              :     }
     272              : 
     273     16682427 :   gcc_checking_assert (best_bb == early_bb
     274              :                        || !do_not_sink (stmt, early_bb, best_bb));
     275              : 
     276     16682427 :   return best_bb;
     277              : }
     278              : 
     279              : /* Given a statement (STMT) and the basic block it is currently in (FROMBB),
     280              :    determine the location to sink the statement to, if any.
     281              :    Returns true if there is such location; in that case, TOGSI points to the
     282              :    statement before that STMT should be moved.  */
     283              : 
     284              : static bool
     285    119562635 : statement_sink_location (gimple *stmt, basic_block frombb,
     286              :                          gimple_stmt_iterator *togsi, bool *zero_uses_p,
     287              :                          virtual_operand_live &vop_live)
     288              : {
     289    119562635 :   gimple *use;
     290    119562635 :   use_operand_p one_use = NULL_USE_OPERAND_P;
     291    119562635 :   basic_block sinkbb;
     292    119562635 :   use_operand_p use_p;
     293    119562635 :   def_operand_p def_p;
     294    119562635 :   ssa_op_iter iter;
     295    119562635 :   imm_use_iterator imm_iter;
     296              : 
     297    119562635 :   *zero_uses_p = false;
     298              : 
     299              :   /* We only can sink assignments and const/pure calls that are guaranteed
     300              :      to return exactly once.  */
     301    119562635 :   int cf;
     302    119562635 :   if (!is_gimple_assign (stmt)
     303    119562635 :       && (!is_gimple_call (stmt)
     304      5422557 :           || !((cf = gimple_call_flags (stmt)) & (ECF_CONST|ECF_PURE))
     305      1063693 :           || (cf & (ECF_LOOPING_CONST_OR_PURE|ECF_RETURNS_TWICE))))
     306              :     return false;
     307              : 
     308              :   /* We only can sink stmts with a single definition.  */
     309     33115219 :   def_p = single_ssa_def_operand (stmt, SSA_OP_ALL_DEFS);
     310     33115219 :   if (def_p == NULL_DEF_OPERAND_P)
     311              :     return false;
     312              : 
     313              :   /* There are a few classes of things we can't or don't move, some because we
     314              :      don't have code to handle it, some because it's not profitable and some
     315              :      because it's not legal.
     316              : 
     317              :      We can't sink things that may be global stores, at least not without
     318              :      calculating a lot more information, because we may cause it to no longer
     319              :      be seen by an external routine that needs it depending on where it gets
     320              :      moved to.
     321              : 
     322              :      We can't sink statements that end basic blocks without splitting the
     323              :      incoming edge for the sink location to place it there.
     324              : 
     325              :      We can't sink statements that have volatile operands.
     326              : 
     327              :      We can't sink statements that may throw when dead exceptions must be
     328              :      preserved, since sinking into a conditional block drops the exception
     329              :      on the paths that bypass it.
     330              : 
     331              :      We don't want to sink dead code, so anything with 0 immediate uses is not
     332              :      sunk.
     333              : 
     334              :      Don't sink BLKmode assignments if current function has any local explicit
     335              :      register variables, as BLKmode assignments may involve memcpy or memset
     336              :      calls or, on some targets, inline expansion thereof that sometimes need
     337              :      to use specific hard registers.
     338              : 
     339              :   */
     340     33115153 :   if (stmt_ends_bb_p (stmt)
     341     32935230 :       || gimple_has_side_effects (stmt)
     342     30946033 :       || (!cfun->can_delete_dead_exceptions
     343     12648449 :           && stmt_could_throw_p (cfun, stmt))
     344     62799765 :       || (cfun->has_local_explicit_reg_vars
     345         2241 :           && TYPE_MODE (TREE_TYPE (gimple_get_lhs (stmt))) == BLKmode))
     346              :     return false;
     347              : 
     348              :   /* Return if there are no immediate uses of this stmt.  */
     349     29684611 :   if (has_zero_uses (DEF_FROM_PTR (def_p)))
     350              :     {
     351        59047 :       *zero_uses_p = true;
     352        59047 :       return false;
     353              :     }
     354              : 
     355     29625564 :   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p)))
     356              :     return false;
     357              : 
     358     71978671 :   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
     359              :     {
     360     42355436 :       tree use = USE_FROM_PTR (use_p);
     361     42355436 :       if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use))
     362              :         return false;
     363              :     }
     364              : 
     365     29623235 :   use = NULL;
     366              : 
     367              :   /* If stmt is a store the one and only use needs to be a VUSE on
     368              :      the live path.  */
     369     29623235 :   if (virtual_operand_p (DEF_FROM_PTR (def_p)))
     370              :     {
     371      8015401 :       tree lhs = gimple_get_lhs (stmt);
     372      8015401 :       ao_ref ref;
     373      8015401 :       ao_ref_init (&ref, lhs);
     374     16854914 :       FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
     375              :         {
     376     10420406 :           gimple *use_stmt = USE_STMT (use_p);
     377              : 
     378              :           /* A killing definition is not a use.  */
     379     10420406 :           if (gimple_vdef (use_stmt)
     380     10848567 :               && ((gimple_has_lhs (use_stmt)
     381      6691756 :                    && operand_equal_p (lhs,
     382      6691756 :                                        gimple_get_lhs (use_stmt), 0))
     383      7706778 :                   || stmt_kills_ref_p (use_stmt, &ref)))
     384              :             {
     385              :               /* If use_stmt is or might be a nop assignment then USE_STMT
     386              :                  acts as a use as well as definition.  */
     387        44987 :               if (stmt != use_stmt
     388        44987 :                   && ref_maybe_used_by_stmt_p (use_stmt, &ref))
     389              :                 {
     390         2123 :                   if (use && use != use_stmt)
     391              :                     return false;
     392              :                   use = use_stmt;
     393              :                 }
     394        44893 :               continue;
     395              :             }
     396              : 
     397     10375419 :           if (is_a <gphi *> (use_stmt)
     398     10375419 :               || ref_maybe_used_by_stmt_p (use_stmt, &ref))
     399              :             {
     400      3249797 :               if (use && use != use_stmt)
     401              :                 return false;
     402      2308615 :               use = use_stmt;
     403      2308615 :               continue;
     404              :             }
     405              : 
     406     21997495 :           if (gimple_vdef (use_stmt))
     407              :             {
     408      6032360 :               if (stmt_may_clobber_ref_p_1 (use_stmt, &ref, false))
     409              :                 return false;
     410              :               /* We do not look past VDEFs, so treat them as sink location.  */
     411      5613871 :               if (use && use != use_stmt)
     412              :                 return false;
     413              :               use = use_stmt;
     414              :             }
     415      1580893 :         }
     416      6434508 :       if (!use)
     417              :         return false;
     418              :     }
     419              :   /* If all the immediate uses are not in the same place, find the nearest
     420              :      common dominator of all the immediate uses.  For PHI nodes, we have to
     421              :      find the nearest common dominator of all of the predecessor blocks, since
     422              :      that is where insertion would have to take place.  */
     423     21607834 :   else if (gimple_vuse (stmt)
     424     21607834 :            || !all_immediate_uses_same_place (def_p))
     425              :     {
     426     11898499 :       bool debug_stmts = false;
     427     11898499 :       basic_block commondom = nearest_common_dominator_of_uses (def_p,
     428              :                                                                 &debug_stmts);
     429              : 
     430     11898499 :       if (commondom == frombb)
     431              :         return false;
     432              : 
     433              :       /* If this is a load then do not sink past any stores.  */
     434      1825350 :       if (gimple_vuse (stmt))
     435              :         {
     436              :           /* Do not sink loads from hard registers.  */
     437       675070 :           if (gimple_assign_single_p (stmt)
     438       671808 :               && VAR_P (gimple_assign_rhs1 (stmt))
     439       700445 :               && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
     440              :             return false;
     441              : 
     442              :           /* When the live virtual operand at the intended sink location is
     443              :              not the same as the one from the load walk up the dominator tree
     444              :              for a new candidate location.  */
     445              :           while (commondom != frombb
     446      3165688 :                  && vop_live.get_live_in (commondom) != gimple_vuse (stmt))
     447      1085406 :             commondom = get_immediate_dominator (CDI_DOMINATORS, commondom);
     448       675066 :           if (commondom == frombb)
     449              :             return false;
     450              :         }
     451              : 
     452              :       /* Our common dominator has to be dominated by frombb in order to be a
     453              :          trivially safe place to put this statement, since it has multiple
     454              :          uses.  */
     455       557415 :       if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
     456              :         return false;
     457              : 
     458       557415 :       commondom = select_best_block (frombb, commondom, stmt);
     459              : 
     460       557415 :       if (commondom == frombb)
     461              :         return false;
     462              : 
     463       305034 :       *togsi = gsi_after_labels (commondom);
     464              : 
     465       305034 :       return true;
     466              :     }
     467              :   else
     468              :     {
     469     10118376 :       FOR_EACH_IMM_USE_FAST (one_use, imm_iter, DEF_FROM_PTR (def_p))
     470              :         {
     471     10118376 :           if (is_gimple_debug (USE_STMT (one_use)))
     472       409041 :             continue;
     473              :           break;
     474      9709335 :         }
     475      9709335 :       use = USE_STMT (one_use);
     476              :     }
     477              : 
     478     16141553 :   if (gimple_code (use) != GIMPLE_PHI)
     479              :     {
     480     15407235 :       sinkbb = select_best_block (frombb, gimple_bb (use), stmt);
     481              : 
     482     15407235 :       if (sinkbb == frombb)
     483              :         return false;
     484              : 
     485              :       /* The SSA update for sinking of stores cannot insert PHIs, the
     486              :          sink location has to lead to exit without crossing any CFG
     487              :          merge points to paths not dominated by the sink location.  */
     488       362607 :       if (gimple_vdef (stmt)
     489       372301 :           && (!single_succ_p (sinkbb)
     490         6474 :               || single_succ (sinkbb)->index != EXIT_BLOCK))
     491              :         return false;
     492              : 
     493       353498 :       *togsi = gsi_after_labels (sinkbb);
     494              : 
     495       353498 :       return true;
     496              :     }
     497              : 
     498       734318 :   sinkbb = find_bb_for_arg (as_a <gphi *> (use), DEF_FROM_PTR (def_p));
     499              : 
     500              :   /* This can happen if there are multiple uses in a PHI.  */
     501       734318 :   if (!sinkbb)
     502              :     return false;
     503              : 
     504       717777 :   basic_block bestbb = select_best_block (frombb, sinkbb, stmt);
     505       717777 :   if (bestbb == frombb
     506              :       /* When we sink a store make sure there's not a path to any of
     507              :          the possibly skipped killing defs as that wrecks the virtual
     508              :          operand update, requiring inserting of a PHI node.  */
     509       116943 :       || (gimple_vdef (stmt)
     510         3218 :           && bestbb != sinkbb
     511           10 :           && !dominated_by_p (CDI_POST_DOMINATORS, bestbb, sinkbb))
     512              :       /* Likewise avoid placing VDEFs into an irreducible region.  */
     513       831502 :       || (gimple_vdef (stmt)
     514         3209 :           && (bestbb->flags & BB_IRREDUCIBLE_LOOP)))
     515              :     return false;
     516              : 
     517       113712 :   *togsi = gsi_after_labels (bestbb);
     518              : 
     519       113712 :   return true;
     520              : }
     521              : 
     522              : /* Very simplistic code to sink common stores from the predecessor through
     523              :    our virtual PHI.  We do this before sinking stmts from BB as it might
     524              :    expose sinking opportunities of the merged stores.
     525              :    Once we have partial dead code elimination through sth like SSU-PRE this
     526              :    should be moved there.  */
     527              : 
     528              : static unsigned
     529     32765101 : sink_common_stores_to_bb (basic_block bb)
     530              : {
     531     32765101 :   unsigned todo = 0;
     532     32765101 :   gphi *phi;
     533              : 
     534     32765101 :   if (EDGE_COUNT (bb->preds) > 1
     535     30628719 :       && (phi = get_virtual_phi (bb)))
     536              :     {
     537              :       /* Repeat until no more common stores are found.  */
     538      3831672 :       while (1)
     539              :         {
     540      3765462 :           gassign *first_store = NULL;
     541      3765462 :           auto_vec <tree, 5> vdefs;
     542      3765462 :           gimple_stmt_iterator gsi;
     543              : 
     544              :           /* Search for common stores defined by all virtual PHI args.
     545              :              ???  Common stores not present in all predecessors could
     546              :              be handled by inserting a forwarder to sink to.  Generally
     547              :              this involves deciding which stores to do this for if
     548              :              multiple common stores are present for different sets of
     549              :              predecessors.  See PR11832 for an interesting case.  */
     550      4605617 :           for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
     551              :             {
     552      4536332 :               tree arg = gimple_phi_arg_def (phi, i);
     553      4536332 :               gassign *def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (arg));
     554      1978527 :               if (! def
     555      1978527 :                   || stmt_can_throw_internal (cfun, def)
     556      1963952 :                   || (gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
     557      1963948 :                   || stmt_references_abnormal_ssa_name (def))
     558              :                 {
     559              :                   /* ???  We could handle some cascading with the def being
     560              :                      another PHI.  We'd have to insert multiple PHIs for
     561              :                      the rhs then though (if they are not all equal).  */
     562              :                   first_store = NULL;
     563      3696177 :                   break;
     564              :                 }
     565              :               /* ???  Do not try to do anything fancy with aliasing, thus
     566              :                  do not sink across non-aliased loads (or even stores,
     567              :                  so different store order will make the sinking fail).  */
     568      1963930 :               bool all_uses_on_phi = true;
     569      1963930 :               imm_use_iterator iter;
     570      1963930 :               use_operand_p use_p;
     571      3486112 :               FOR_EACH_IMM_USE_FAST (use_p, iter, arg)
     572      2548295 :                 if (USE_STMT (use_p) != phi)
     573              :                   {
     574              :                     all_uses_on_phi = false;
     575              :                     break;
     576      1963930 :                   }
     577      1963930 :               if (! all_uses_on_phi)
     578              :                 {
     579              :                   first_store = NULL;
     580              :                   break;
     581              :                 }
     582              :               /* Check all stores are to the same LHS.  */
     583       937817 :               if (! first_store)
     584              :                 first_store = def;
     585              :               /* ??? We could handle differing SSA uses in the LHS by inserting
     586              :                  PHIs for them.  */
     587       260991 :               else if (! operand_equal_p (gimple_assign_lhs (first_store),
     588       260991 :                                           gimple_assign_lhs (def), 0)
     589       424320 :                        || (gimple_clobber_p (first_store)
     590       163329 :                            != gimple_clobber_p (def)))
     591              :                 {
     592              :                   first_store = NULL;
     593              :                   break;
     594              :                 }
     595       840155 :               vdefs.safe_push (arg);
     596              :             }
     597      3765462 :           if (! first_store)
     598              :             break;
     599              : 
     600              :           /* Check if we need a PHI node to merge the stored values.  */
     601        69285 :           bool allsame = true;
     602        69285 :           if (!gimple_clobber_p (first_store))
     603        87269 :             for (unsigned i = 1; i < vdefs.length (); ++i)
     604              :               {
     605        80653 :                 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
     606        80653 :                 if (! operand_equal_p (gimple_assign_rhs1 (first_store),
     607        80653 :                                        gimple_assign_rhs1 (def), 0))
     608              :                   {
     609              :                     allsame = false;
     610              :                     break;
     611              :                   }
     612              :               }
     613              : 
     614              :           /* We cannot handle aggregate values if we need to merge them.  */
     615        69285 :           tree type = TREE_TYPE (gimple_assign_lhs (first_store));
     616        69285 :           if (! allsame
     617        69285 :               && ! is_gimple_reg_type (type))
     618              :             break;
     619              : 
     620        66210 :           if (dump_enabled_p ())
     621              :             {
     622           18 :               dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
     623            9 :                                first_store,
     624              :                                "sinking common stores %sto ",
     625              :                                allsame ? "with same value " : "");
     626            9 :               dump_generic_expr (MSG_OPTIMIZED_LOCATIONS, TDF_SLIM,
     627              :                                  gimple_assign_lhs (first_store));
     628            9 :               dump_printf (MSG_OPTIMIZED_LOCATIONS, "\n");
     629              :             }
     630              : 
     631              :           /* Insert a PHI to merge differing stored values if necessary.
     632              :              Note that in general inserting PHIs isn't a very good idea as
     633              :              it makes the job of coalescing and register allocation harder.
     634              :              Even common SSA uses on the rhs/lhs might extend their lifetime
     635              :              across multiple edges by this code motion which makes
     636              :              register allocation harder.  */
     637        66210 :           tree from;
     638        66210 :           if (! allsame)
     639              :             {
     640        51627 :               from = make_ssa_name (type);
     641        51627 :               gphi *newphi = create_phi_node (from, bb);
     642       268261 :               for (unsigned i = 0; i < vdefs.length (); ++i)
     643              :                 {
     644       165007 :                   gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
     645       165007 :                   add_phi_arg (newphi, gimple_assign_rhs1 (def),
     646       165007 :                                EDGE_PRED (bb, i), UNKNOWN_LOCATION);
     647              :                 }
     648              :             }
     649              :           else
     650        14583 :             from = gimple_assign_rhs1 (first_store);
     651              : 
     652              :           /* Remove all stores.  */
     653       538424 :           for (unsigned i = 0; i < vdefs.length (); ++i)
     654       203002 :             TREE_VISITED (vdefs[i]) = 1;
     655       269212 :           for (unsigned i = 0; i < vdefs.length (); ++i)
     656              :             /* If we have more than one use of a VDEF on the PHI make sure
     657              :                we remove the defining stmt only once.  */
     658       203002 :             if (TREE_VISITED (vdefs[i]))
     659              :               {
     660       199575 :                 TREE_VISITED (vdefs[i]) = 0;
     661       199575 :                 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
     662       199575 :                 gsi = gsi_for_stmt (def);
     663       199575 :                 unlink_stmt_vdef (def);
     664       199575 :                 gsi_remove (&gsi, true);
     665       199575 :                 release_defs (def);
     666              :               }
     667              : 
     668              :           /* Insert the first store at the beginning of the merge BB.  */
     669        66210 :           gimple_set_vdef (first_store, gimple_phi_result (phi));
     670       132420 :           SSA_NAME_DEF_STMT (gimple_vdef (first_store)) = first_store;
     671        66210 :           gimple_phi_set_result (phi, make_ssa_name (gimple_vop (cfun)));
     672        66210 :           gimple_set_vuse (first_store, gimple_phi_result (phi));
     673        66210 :           gimple_assign_set_rhs1 (first_store, from);
     674              :           /* ???  Should we reset first_stores location?  */
     675        66210 :           gsi = gsi_after_labels (bb);
     676        66210 :           gsi_insert_before (&gsi, first_store, GSI_SAME_STMT);
     677        66210 :           sink_stats.commoned++;
     678              : 
     679        66210 :           todo |= TODO_cleanup_cfg;
     680      3765462 :         }
     681              : 
     682              :       /* We could now have empty predecessors that we could remove,
     683              :          forming a proper CFG for further sinking.  Note that even
     684              :          CFG cleanup doesn't do this fully at the moment and it
     685              :          doesn't preserve post-dominators in the process either.
     686              :          The mergephi pass might do it though.  gcc.dg/tree-ssa/ssa-sink-13.c
     687              :          shows this nicely if you disable tail merging or (same effect)
     688              :          make the stored values unequal.  */
     689              :     }
     690              : 
     691     32765101 :   return todo;
     692              : }
     693              : 
     694              : /* Perform code sinking on BB */
     695              : 
     696              : static unsigned
     697     32765101 : sink_code_in_bb (basic_block bb, virtual_operand_live &vop_live)
     698              : {
     699     32765101 :   gimple_stmt_iterator gsi;
     700     32765101 :   edge_iterator ei;
     701     32765101 :   edge e;
     702     32765101 :   bool last = true;
     703     32765101 :   unsigned todo = 0;
     704              : 
     705              :   /* Sink common stores from the predecessor through our virtual PHI.  */
     706     32765101 :   todo |= sink_common_stores_to_bb (bb);
     707              : 
     708              :   /* If this block doesn't dominate anything, there can't be any place to sink
     709              :      the statements to.  */
     710     32765101 :   if (first_dom_son (CDI_DOMINATORS, bb) == NULL)
     711              :     return todo;
     712              : 
     713              :   /* We can't move things across abnormal edges, so don't try.  */
     714     38683406 :   FOR_EACH_EDGE (e, ei, bb->succs)
     715     24528011 :     if (e->flags & EDGE_ABNORMAL)
     716              :       return todo;
     717              : 
     718    147873425 :   for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
     719              :     {
     720    119562635 :       gimple *stmt = gsi_stmt (gsi);
     721    119562635 :       gimple_stmt_iterator togsi;
     722    119562635 :       bool zero_uses_p;
     723              : 
     724    119562635 :       if (!statement_sink_location (stmt, bb, &togsi, &zero_uses_p, vop_live))
     725              :         {
     726    118790391 :           gimple_stmt_iterator saved = gsi;
     727    118790391 :           if (!gsi_end_p (gsi))
     728    129897429 :             gsi_prev (&gsi);
     729              :           /* If we face a dead stmt remove it as it possibly blocks
     730              :              sinking of uses.  */
     731    118790391 :           if (zero_uses_p
     732        59047 :               && !gimple_vdef (stmt)
     733    118845641 :               && (cfun->can_delete_dead_exceptions
     734        29714 :                   || !stmt_could_throw_p (cfun, stmt)))
     735              :             {
     736        55250 :               gsi_remove (&saved, true);
     737        55250 :               release_defs (stmt);
     738              :             }
     739              :           else
     740              :             last = false;
     741    118790391 :           continue;
     742    118790391 :         }
     743       772244 :       if (dump_file)
     744              :         {
     745           33 :           fprintf (dump_file, "Sinking ");
     746           33 :           print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS);
     747           33 :           fprintf (dump_file, " from bb %d to bb %d\n",
     748           33 :                    bb->index, (gsi_bb (togsi))->index);
     749              :         }
     750              : 
     751              :       /* Update virtual operands of statements in the path we
     752              :          do not sink to.  */
     753      1544488 :       if (gimple_vdef (stmt))
     754              :         {
     755         3781 :           imm_use_iterator iter;
     756         3781 :           use_operand_p use_p;
     757         3781 :           gimple *vuse_stmt;
     758              : 
     759        15807 :           FOR_EACH_IMM_USE_STMT (vuse_stmt, iter, gimple_vdef (stmt))
     760        12026 :             if (gimple_code (vuse_stmt) != GIMPLE_PHI
     761        12026 :                 && !dominated_by_p (CDI_DOMINATORS, gimple_bb (vuse_stmt),
     762         8830 :                                     gsi_bb (togsi)))
     763        16082 :               FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
     764        19863 :                 SET_USE (use_p, gimple_vuse (stmt));
     765              :         }
     766              : 
     767              :       /* If this is the end of the basic block, we need to insert at the end
     768              :          of the basic block.  */
     769       772244 :       if (gsi_end_p (togsi))
     770       127830 :         gsi_move_to_bb_end (&gsi, gsi_bb (togsi));
     771              :       else
     772       644414 :         gsi_move_before (&gsi, &togsi);
     773              : 
     774       772244 :       sink_stats.sunk++;
     775              : 
     776              :       /* If we've just removed the last statement of the BB, the
     777              :          gsi_end_p() test below would fail, but gsi_prev() would have
     778              :          succeeded, and we want it to succeed.  So we keep track of
     779              :          whether we're at the last statement and pick up the new last
     780              :          statement.  */
     781       772244 :       if (last)
     782              :         {
     783        14529 :           gsi = gsi_last_bb (bb);
     784        14529 :           continue;
     785              :         }
     786              : 
     787       757715 :       last = false;
     788       757715 :       if (!gsi_end_p (gsi))
     789       840154 :         gsi_prev (&gsi);
     790              : 
     791              :     }
     792              : 
     793              :   return todo;
     794              : }
     795              : 
     796              : /* Perform code sinking.
     797              :    This moves code down the flowgraph when we know it would be
     798              :    profitable to do so, or it wouldn't increase the number of
     799              :    executions of the statement.
     800              : 
     801              :    IE given
     802              : 
     803              :    a_1 = b + c;
     804              :    if (<something>)
     805              :    {
     806              :    }
     807              :    else
     808              :    {
     809              :      foo (&b, &c);
     810              :      a_5 = b + c;
     811              :    }
     812              :    a_6 = PHI (a_5, a_1);
     813              :    USE a_6.
     814              : 
     815              :    we'll transform this into:
     816              : 
     817              :    if (<something>)
     818              :    {
     819              :       a_1 = b + c;
     820              :    }
     821              :    else
     822              :    {
     823              :       foo (&b, &c);
     824              :       a_5 = b + c;
     825              :    }
     826              :    a_6 = PHI (a_5, a_1);
     827              :    USE a_6.
     828              : 
     829              :    Note that this reduces the number of computations of a = b + c to 1
     830              :    when we take the else edge, instead of 2.
     831              : */
     832              : namespace {
     833              : 
     834              : const pass_data pass_data_sink_code =
     835              : {
     836              :   GIMPLE_PASS, /* type */
     837              :   "sink", /* name */
     838              :   OPTGROUP_NONE, /* optinfo_flags */
     839              :   TV_TREE_SINK, /* tv_id */
     840              :   /* PROP_no_crit_edges is ensured by running split_edges_for_insertion in
     841              :      pass_data_sink_code::execute ().  */
     842              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
     843              :   0, /* properties_provided */
     844              :   0, /* properties_destroyed */
     845              :   0, /* todo_flags_start */
     846              :   TODO_update_ssa, /* todo_flags_finish */
     847              : };
     848              : 
     849              : class pass_sink_code : public gimple_opt_pass
     850              : {
     851              : public:
     852       605970 :   pass_sink_code (gcc::context *ctxt)
     853      1211940 :     : gimple_opt_pass (pass_data_sink_code, ctxt), unsplit_edges (false)
     854              :   {}
     855              : 
     856              :   /* opt_pass methods: */
     857      2136602 :   bool gate (function *) final override { return flag_tree_sink != 0; }
     858              :   unsigned int execute (function *) final override;
     859       302985 :   opt_pass *clone (void) final override { return new pass_sink_code (m_ctxt); }
     860       605970 :   void set_pass_param (unsigned n, bool param) final override
     861              :     {
     862       605970 :       gcc_assert (n == 0);
     863       605970 :       unsplit_edges = param;
     864       605970 :     }
     865              : 
     866              : private:
     867              :   bool unsplit_edges;
     868              : }; // class pass_sink_code
     869              : 
     870              : unsigned int
     871      2136382 : pass_sink_code::execute (function *fun)
     872              : {
     873      2136382 :   loop_optimizer_init (LOOPS_NORMAL);
     874      2136382 :   split_edges_for_insertion ();
     875              :   /* Arrange for the critical edge splitting to be undone if requested.  */
     876      2136382 :   unsigned todo = unsplit_edges ? TODO_cleanup_cfg : 0;
     877      2136382 :   connect_infinite_loops_to_exit ();
     878      2136382 :   mark_dfs_back_edges (fun);
     879      2136382 :   memset (&sink_stats, 0, sizeof (sink_stats));
     880      2136382 :   calculate_dominance_info (CDI_DOMINATORS);
     881      2136382 :   calculate_dominance_info (CDI_POST_DOMINATORS);
     882              : 
     883      2136382 :   virtual_operand_live vop_live;
     884              : 
     885      2136382 :   int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
     886      2136382 :   int n = inverted_rev_post_order_compute (fun, rpo);
     887     37037865 :   for (int i = 0; i < n; ++i)
     888     32765101 :     todo |= sink_code_in_bb (BASIC_BLOCK_FOR_FN (fun, rpo[i]), vop_live);
     889      2136382 :   free (rpo);
     890              : 
     891      2136382 :   statistics_counter_event (fun, "Sunk statements", sink_stats.sunk);
     892      2136382 :   statistics_counter_event (fun, "Commoned stores", sink_stats.commoned);
     893      2136382 :   free_dominance_info (CDI_POST_DOMINATORS);
     894      2136382 :   remove_fake_exit_edges ();
     895      2136382 :   loop_optimizer_finalize ();
     896              : 
     897      2136382 :   return todo;
     898      2136382 : }
     899              : 
     900              : } // anon namespace
     901              : 
     902              : gimple_opt_pass *
     903       302985 : make_pass_sink_code (gcc::context *ctxt)
     904              : {
     905       302985 :   return new pass_sink_code (ctxt);
     906              : }
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.