LCOV - code coverage report
Current view: top level - gcc - tree-ssa-phiprop.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 97.3 % 221 215
Test Date: 2025-07-05 13:26:22 Functions: 100.0 % 7 7
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Backward propagation of indirect loads through PHIs.
       2                 :             :    Copyright (C) 2007-2025 Free Software Foundation, Inc.
       3                 :             :    Contributed by Richard Guenther <rguenther@suse.de>
       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 "tree-pass.h"
      28                 :             : #include "ssa.h"
      29                 :             : #include "gimple-pretty-print.h"
      30                 :             : #include "fold-const.h"
      31                 :             : #include "tree-eh.h"
      32                 :             : #include "gimplify.h"
      33                 :             : #include "gimple-iterator.h"
      34                 :             : #include "stor-layout.h"
      35                 :             : #include "tree-ssa-loop.h"
      36                 :             : #include "tree-cfg.h"
      37                 :             : #include "tree-ssa-dce.h"
      38                 :             : 
      39                 :             : /* This pass propagates indirect loads through the PHI node for its
      40                 :             :    address to make the load source possibly non-addressable and to
      41                 :             :    allow for PHI optimization to trigger.
      42                 :             : 
      43                 :             :    For example the pass changes
      44                 :             : 
      45                 :             :      # addr_1 = PHI <&a, &b>
      46                 :             :      tmp_1 = *addr_1;
      47                 :             : 
      48                 :             :    to
      49                 :             : 
      50                 :             :      # tmp_1 = PHI <a, b>
      51                 :             : 
      52                 :             :    but also handles more complex scenarios like
      53                 :             : 
      54                 :             :      D.2077_2 = &this_1(D)->a1;
      55                 :             :      ...
      56                 :             : 
      57                 :             :      # b_12 = PHI <&c(2), D.2077_2(3)>
      58                 :             :      D.2114_13 = *b_12;
      59                 :             :      ...
      60                 :             : 
      61                 :             :      # b_15 = PHI <b_12(4), &b(5)>
      62                 :             :      D.2080_5 = &this_1(D)->a0;
      63                 :             :      ...
      64                 :             : 
      65                 :             :      # b_18 = PHI <D.2080_5(6), &c(7)>
      66                 :             :      ...
      67                 :             : 
      68                 :             :      # b_21 = PHI <b_15(8), b_18(9)>
      69                 :             :      D.2076_8 = *b_21;
      70                 :             : 
      71                 :             :    where the addresses loaded are defined by PHIs itself.
      72                 :             :    The above happens for
      73                 :             : 
      74                 :             :      std::max(std::min(a0, c), std::min(std::max(a1, c), b))
      75                 :             : 
      76                 :             :    where this pass transforms it to a form later PHI optimization
      77                 :             :    recognizes and transforms it to the simple
      78                 :             : 
      79                 :             :      D.2109_10 = this_1(D)->a1;
      80                 :             :      D.2110_11 = c;
      81                 :             :      D.2114_31 = MAX_EXPR <D.2109_10, D.2110_11>;
      82                 :             :      D.2115_14 = b;
      83                 :             :      D.2125_17 = MIN_EXPR <D.2115_14, D.2114_31>;
      84                 :             :      D.2119_16 = this_1(D)->a0;
      85                 :             :      D.2124_32 = MIN_EXPR <D.2110_11, D.2119_16>;
      86                 :             :      D.2076_33 = MAX_EXPR <D.2125_17, D.2124_32>;
      87                 :             : 
      88                 :             :    The pass does a dominator walk processing loads using a basic-block
      89                 :             :    local analysis and stores the result for use by transformations on
      90                 :             :    dominated basic-blocks.  */
      91                 :             : 
      92                 :             : 
      93                 :             : /* Structure to keep track of the value of a dereferenced PHI result
      94                 :             :    and the virtual operand used for that dereference.  */
      95                 :             : 
      96                 :             : struct phiprop_d
      97                 :             : {
      98                 :             :   tree value;
      99                 :             :   tree vuse;
     100                 :             : };
     101                 :             : 
     102                 :             : /* Insert a new phi node for the dereference of PHI at basic_block
     103                 :             :    BB with the virtual operands from USE_STMT.  */
     104                 :             : 
     105                 :             : static tree
     106                 :       15412 : phiprop_insert_phi (basic_block bb, gphi *phi, gimple *use_stmt,
     107                 :             :                     struct phiprop_d *phivn, size_t n,
     108                 :             :                     bitmap dce_ssa_names)
     109                 :             : {
     110                 :       15412 :   tree res;
     111                 :       15412 :   gphi *new_phi = NULL;
     112                 :       15412 :   edge_iterator ei;
     113                 :       15412 :   edge e;
     114                 :       15412 :   tree phi_result = PHI_RESULT (phi);
     115                 :       15412 :   bitmap_set_bit (dce_ssa_names, SSA_NAME_VERSION (phi_result));
     116                 :             : 
     117                 :       15412 :   gcc_assert (is_gimple_assign (use_stmt)
     118                 :             :               && gimple_assign_rhs_code (use_stmt) == MEM_REF);
     119                 :             : 
     120                 :             :   /* Build a new PHI node to replace the definition of
     121                 :             :      the indirect reference lhs.  */
     122                 :       15412 :   res = gimple_assign_lhs (use_stmt);
     123                 :       15412 :   if (TREE_CODE (res) == SSA_NAME)
     124                 :       15262 :     new_phi = create_phi_node (res, bb);
     125                 :             : 
     126                 :       15412 :   if (dump_file && (dump_flags & TDF_DETAILS))
     127                 :             :     {
     128                 :           3 :       fprintf (dump_file, "Inserting PHI for result of load ");
     129                 :           3 :       print_gimple_stmt (dump_file, use_stmt, 0);
     130                 :             :     }
     131                 :             : 
     132                 :       15412 :   gphi *vphi = get_virtual_phi (bb);
     133                 :             : 
     134                 :             :   /* Add PHI arguments for each edge inserting loads of the
     135                 :             :      addressable operands.  */
     136                 :       44742 :   FOR_EACH_EDGE (e, ei, bb->preds)
     137                 :             :     {
     138                 :       29330 :       tree old_arg, new_var;
     139                 :       29330 :       gassign *tmp;
     140                 :       29330 :       location_t locus;
     141                 :             : 
     142                 :       29330 :       old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
     143                 :       29330 :       locus = gimple_phi_arg_location_from_edge (phi, e);
     144                 :       29330 :       while (TREE_CODE (old_arg) == SSA_NAME
     145                 :       30763 :              && (SSA_NAME_VERSION (old_arg) >= n
     146                 :        1524 :                  || phivn[SSA_NAME_VERSION (old_arg)].value == NULL_TREE))
     147                 :             :         {
     148                 :        1433 :           gimple *def_stmt = SSA_NAME_DEF_STMT (old_arg);
     149                 :        1433 :           old_arg = gimple_assign_rhs1 (def_stmt);
     150                 :        1433 :           locus = gimple_location (def_stmt);
     151                 :             :         }
     152                 :             : 
     153                 :       29330 :       if (TREE_CODE (old_arg) == SSA_NAME)
     154                 :             :         {
     155                 :          91 :           if (dump_file && (dump_flags & TDF_DETAILS))
     156                 :             :             {
     157                 :           0 :               fprintf (dump_file, "  for edge defining ");
     158                 :           0 :               print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e));
     159                 :           0 :               fprintf (dump_file, " reusing PHI result ");
     160                 :           0 :               print_generic_expr (dump_file,
     161                 :           0 :                                   phivn[SSA_NAME_VERSION (old_arg)].value);
     162                 :           0 :               fprintf (dump_file, "\n");
     163                 :             :             }
     164                 :             :           /* Reuse a formerly created dereference.  */
     165                 :          91 :           new_var = phivn[SSA_NAME_VERSION (old_arg)].value;
     166                 :             :         }
     167                 :             :       else
     168                 :             :         {
     169                 :       29239 :           tree rhs = gimple_assign_rhs1 (use_stmt);
     170                 :       29239 :           gcc_assert (TREE_CODE (old_arg) == ADDR_EXPR);
     171                 :       29239 :           tree vuse = NULL_TREE;
     172                 :       29239 :           if (TREE_CODE (res) == SSA_NAME)
     173                 :             :             {
     174                 :       28912 :               new_var = make_ssa_name (TREE_TYPE (rhs));
     175                 :       28912 :               if (vphi)
     176                 :         102 :                 vuse = PHI_ARG_DEF_FROM_EDGE (vphi, e);
     177                 :             :               else
     178                 :       28810 :                 vuse = gimple_vuse (use_stmt);
     179                 :             :             }
     180                 :             :           else
     181                 :             :             /* For the aggregate copy case updating virtual operands
     182                 :             :                we'd have to possibly insert a virtual PHI and we have
     183                 :             :                to split the existing VUSE lifetime.  Leave that to
     184                 :             :                the generic SSA updating.  */
     185                 :         327 :             new_var = unshare_expr (res);
     186                 :       29239 :           if (!is_gimple_min_invariant (old_arg))
     187                 :        1410 :             old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
     188                 :             :           else
     189                 :       27829 :             old_arg = unshare_expr (old_arg);
     190                 :       29239 :           tmp = gimple_build_assign (new_var,
     191                 :       29239 :                                      fold_build2 (MEM_REF, TREE_TYPE (rhs),
     192                 :             :                                                   old_arg,
     193                 :             :                                                   TREE_OPERAND (rhs, 1)));
     194                 :       29239 :           gimple_set_location (tmp, locus);
     195                 :       29239 :           if (vuse)
     196                 :       28912 :             gimple_set_vuse (tmp, vuse);
     197                 :             : 
     198                 :       29239 :           gsi_insert_on_edge (e, tmp);
     199                 :       29239 :           update_stmt (tmp);
     200                 :             : 
     201                 :       29239 :           if (dump_file && (dump_flags & TDF_DETAILS))
     202                 :             :             {
     203                 :           6 :               fprintf (dump_file, "  for edge defining ");
     204                 :           6 :               print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e));
     205                 :           6 :               fprintf (dump_file, " inserting load ");
     206                 :           6 :               print_gimple_stmt (dump_file, tmp, 0);
     207                 :             :             }
     208                 :             :         }
     209                 :             : 
     210                 :       29330 :       if (new_phi)
     211                 :       29003 :         add_phi_arg (new_phi, new_var, e, locus);
     212                 :             :     }
     213                 :             : 
     214                 :       15412 :   if (new_phi)
     215                 :             :     {
     216                 :       15262 :       update_stmt (new_phi);
     217                 :             : 
     218                 :       15262 :       if (dump_file && (dump_flags & TDF_DETAILS))
     219                 :           3 :         print_gimple_stmt (dump_file, new_phi, 0);
     220                 :             :     }
     221                 :             : 
     222                 :       15412 :   return res;
     223                 :             : }
     224                 :             : 
     225                 :             : /* Verify if *idx is available at *DATA.  */
     226                 :             : 
     227                 :             : static bool
     228                 :          48 : chk_uses (tree, tree *idx, void *data)
     229                 :             : {
     230                 :          48 :   basic_block dom = (basic_block) data;
     231                 :          48 :   if (TREE_CODE (*idx) == SSA_NAME)
     232                 :          30 :     return (SSA_NAME_IS_DEFAULT_DEF (*idx)
     233                 :          30 :             || ! dominated_by_p (CDI_DOMINATORS,
     234                 :          18 :                                  gimple_bb (SSA_NAME_DEF_STMT (*idx)), dom));
     235                 :             :   return true;
     236                 :             : }
     237                 :             : 
     238                 :             : /* Propagate between the phi node arguments of PHI in BB and phi result
     239                 :             :    users.  For now this matches
     240                 :             :         # p_2 = PHI <&x, &y>
     241                 :             :       <Lx>:;
     242                 :             :         p_3 = p_2;
     243                 :             :         z_2 = *p_3;
     244                 :             :    and converts it to
     245                 :             :         # z_2 = PHI <x, y>
     246                 :             :       <Lx>:;
     247                 :             :    Returns true if a transformation was done and edge insertions
     248                 :             :    need to be committed.  Global data PHIVN and N is used to track
     249                 :             :    past transformation results.  VPHI is the virtual PHI node in BB
     250                 :             :    if there is one.  We need to be especially careful here
     251                 :             :    with aliasing issues as we are moving memory reads.  */
     252                 :             : 
     253                 :             : static bool
     254                 :     7976265 : propagate_with_phi (basic_block bb, gphi *vphi, gphi *phi,
     255                 :             :                     struct phiprop_d *phivn, size_t n, bitmap dce_ssa_names)
     256                 :             : {
     257                 :     7976265 :   tree ptr = PHI_RESULT (phi);
     258                 :     7976265 :   gimple *use_stmt;
     259                 :     7976265 :   tree res = NULL_TREE;
     260                 :     7976265 :   gimple_stmt_iterator gsi;
     261                 :     7976265 :   imm_use_iterator ui;
     262                 :     7976265 :   use_operand_p arg_p, use;
     263                 :     7976265 :   ssa_op_iter i;
     264                 :     7976265 :   bool phi_inserted;
     265                 :     7976265 :   bool changed;
     266                 :     7976265 :   tree type = NULL_TREE;
     267                 :             : 
     268                 :    15201035 :   if (!POINTER_TYPE_P (TREE_TYPE (ptr))
     269                 :     8001550 :       || (!is_gimple_reg_type (TREE_TYPE (TREE_TYPE (ptr)))
     270                 :      347218 :           && TYPE_MODE (TREE_TYPE (TREE_TYPE (ptr))) == BLKmode))
     271                 :     7454995 :     return false;
     272                 :             : 
     273                 :      521270 :   tree up_vuse = NULL_TREE;
     274                 :             :   /* Check if we can "cheaply" dereference all phi arguments.  */
     275                 :      641604 :   FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
     276                 :             :     {
     277                 :      607918 :       tree arg = USE_FROM_PTR (arg_p);
     278                 :             :       /* Walk the ssa chain until we reach a ssa name we already
     279                 :             :          created a value for or we reach a definition of the form
     280                 :             :          ssa_name_n = &var;  */
     281                 :      607918 :       while (TREE_CODE (arg) == SSA_NAME
     282                 :      507691 :              && !SSA_NAME_IS_DEFAULT_DEF (arg)
     283                 :     1276659 :              && (SSA_NAME_VERSION (arg) >= n
     284                 :      449661 :                  || phivn[SSA_NAME_VERSION (arg)].value == NULL_TREE))
     285                 :             :         {
     286                 :      449565 :           gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
     287                 :      449565 :           if (!gimple_assign_single_p (def_stmt))
     288                 :             :             return false;
     289                 :      219070 :           arg = gimple_assign_rhs1 (def_stmt);
     290                 :             :         }
     291                 :      377423 :       if (TREE_CODE (arg) == ADDR_EXPR)
     292                 :             :         ;
     293                 :             :       /* When we have an SSA name see if we previously encountered a
     294                 :             :          dereference of it.  */
     295                 :      257193 :       else if (TREE_CODE (arg) == SSA_NAME
     296                 :       58126 :                && SSA_NAME_VERSION (arg) < n
     297                 :       58126 :                && phivn[SSA_NAME_VERSION (arg)].value != NULL_TREE
     298                 :      257299 :                && (!type
     299                 :           3 :                    || types_compatible_p
     300                 :           3 :                        (type, TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value))))
     301                 :             :         {
     302                 :             :           /* The dereference should be under the VUSE that's active in BB.
     303                 :             :              If the BB has no virtual PHI then record the common "incoming"
     304                 :             :              vuse.  */
     305                 :         106 :           if (vphi)
     306                 :           2 :             up_vuse = gimple_phi_arg_def (vphi, phi_arg_index_from_use (arg_p));
     307                 :         106 :           if (!up_vuse)
     308                 :         101 :             up_vuse = phivn[SSA_NAME_VERSION (arg)].vuse;
     309                 :           5 :           else if (up_vuse != phivn[SSA_NAME_VERSION (arg)].vuse)
     310                 :             :             return false;
     311                 :             :         }
     312                 :             :       else
     313                 :      257087 :         return false;
     314                 :      120334 :       if (!type
     315                 :      120276 :           && TREE_CODE (arg) == SSA_NAME)
     316                 :         101 :         type = TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value);
     317                 :             :     }
     318                 :             : 
     319                 :             :   /* Find a dereferencing use.  First follow (single use) ssa
     320                 :             :      copy chains for ptr.  */
     321                 :       34680 :   while (single_imm_use (ptr, &use, &use_stmt)
     322                 :       34680 :          && gimple_assign_ssa_name_copy_p (use_stmt))
     323                 :         994 :     ptr = gimple_assign_lhs (use_stmt);
     324                 :             : 
     325                 :             :   /* Replace the first dereference of *ptr if there is one and if we
     326                 :             :      can move the loads to the place of the ptr phi node.  */
     327                 :       33686 :   phi_inserted = false;
     328                 :       33686 :   changed = false;
     329                 :       81969 :   FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
     330                 :             :     {
     331                 :       48283 :       gimple *def_stmt;
     332                 :       48283 :       tree vuse;
     333                 :             : 
     334                 :       48283 :       if (!dom_info_available_p (cfun, CDI_POST_DOMINATORS))
     335                 :       22609 :         calculate_dominance_info (CDI_POST_DOMINATORS);
     336                 :             : 
     337                 :             :       /* Only replace loads in blocks that post-dominate the PHI node.  That
     338                 :             :          makes sure we don't end up speculating loads.  */
     339                 :       48283 :       if (!dominated_by_p (CDI_POST_DOMINATORS,
     340                 :       48283 :                            bb, gimple_bb (use_stmt)))
     341                 :        6687 :         continue;
     342                 :             : 
     343                 :             :       /* Check whether this is a load of *ptr.  */
     344                 :       41596 :       if (!(is_gimple_assign (use_stmt)
     345                 :       19821 :             && gimple_assign_rhs_code (use_stmt) == MEM_REF
     346                 :       15878 :             && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == ptr
     347                 :       15878 :             && integer_zerop (TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 1))
     348                 :       15866 :             && (!type
     349                 :         113 :                 || types_compatible_p
     350                 :         113 :                      (TREE_TYPE (gimple_assign_lhs (use_stmt)), type))
     351                 :             :             /* We cannot replace a load that may throw or is volatile.
     352                 :             :                For volatiles the transform can change the number of
     353                 :             :                executions if the load is inside a loop but the address
     354                 :             :                computations outside (PR91812).  We could relax this
     355                 :             :                if we guard against that appropriately.  For loads that can
     356                 :             :                throw we could relax things if the moved loads all are
     357                 :             :                known to not throw.  */
     358                 :       15866 :             && !stmt_can_throw_internal (cfun, use_stmt)
     359                 :       31688 :             && !gimple_has_volatile_ops (use_stmt)))
     360                 :       25761 :         continue;
     361                 :             : 
     362                 :             :       /* Check if we can move the loads.  This is when the virtual use
     363                 :             :          is the same as the one active at the start of BB which we know
     364                 :             :          either from its virtual PHI def or from the common incoming
     365                 :             :          VUSE.  If neither is present make sure the def stmt of the virtual
     366                 :             :          use is in a different basic block dominating BB.  When the
     367                 :             :          def is an edge-inserted one we know it dominates us.  */
     368                 :       15835 :       vuse = gimple_vuse (use_stmt);
     369                 :       15835 :       if (vphi)
     370                 :             :         {
     371                 :         105 :           if (vuse != gimple_phi_result (vphi))
     372                 :          20 :             goto next;
     373                 :             :         }
     374                 :       15730 :       else if (up_vuse)
     375                 :             :         {
     376                 :         101 :           if (vuse != up_vuse)
     377                 :          13 :             goto next;
     378                 :             :         }
     379                 :             :       else
     380                 :             :         {
     381                 :       15629 :           def_stmt = SSA_NAME_DEF_STMT (vuse);
     382                 :       15629 :           if (!SSA_NAME_IS_DEFAULT_DEF (vuse)
     383                 :       15629 :               && (gimple_bb (def_stmt) == bb
     384                 :       13949 :                   || !dominated_by_p (CDI_DOMINATORS,
     385                 :       13949 :                                       bb, gimple_bb (def_stmt))))
     386                 :         348 :             goto next;
     387                 :             :         }
     388                 :             : 
     389                 :             :       /* Found a proper dereference with an aggregate copy.  Just
     390                 :             :          insert aggregate copies on the edges instead.  */
     391                 :       15454 :       if (!is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (use_stmt))))
     392                 :             :         {
     393                 :         360 :           if (!gimple_vdef (use_stmt))
     394                 :          30 :             goto next;
     395                 :             : 
     396                 :             :           /* As we replicate the lhs on each incoming edge all
     397                 :             :              used SSA names have to be available there.  */
     398                 :         180 :           if (! for_each_index (gimple_assign_lhs_ptr (use_stmt),
     399                 :             :                                 chk_uses,
     400                 :         180 :                                 get_immediate_dominator (CDI_DOMINATORS,
     401                 :             :                                                          gimple_bb (phi))))
     402                 :          18 :             goto next;
     403                 :             : 
     404                 :         162 :           gimple *vuse_stmt;
     405                 :         162 :           imm_use_iterator vui;
     406                 :         162 :           use_operand_p vuse_p;
     407                 :             :           /* In order to move the aggregate copies earlier, make sure
     408                 :             :              there are no statements that could read from memory
     409                 :             :              aliasing the lhs in between the start of bb and use_stmt.
     410                 :             :              As we require use_stmt to have a VDEF above, loads after
     411                 :             :              use_stmt will use a different virtual SSA_NAME.  When
     412                 :             :              we reach an edge inserted load the constraints we place
     413                 :             :              on processing guarantees that program order is preserved
     414                 :             :              so we can avoid checking those.  */
     415                 :         848 :           FOR_EACH_IMM_USE_FAST (vuse_p, vui, vuse)
     416                 :             :             {
     417                 :         698 :               vuse_stmt = USE_STMT (vuse_p);
     418                 :         698 :               if (vuse_stmt == use_stmt)
     419                 :         157 :                 continue;
     420                 :         541 :               if (!gimple_bb (vuse_stmt)
     421                 :        1050 :                   || !dominated_by_p (CDI_DOMINATORS,
     422                 :         509 :                                       gimple_bb (vuse_stmt), bb))
     423                 :         529 :                 continue;
     424                 :          12 :               if (ref_maybe_used_by_stmt_p (vuse_stmt,
     425                 :             :                                             gimple_assign_lhs (use_stmt)))
     426                 :          12 :                 goto next;
     427                 :             :             }
     428                 :             : 
     429                 :         150 :           phiprop_insert_phi (bb, phi, use_stmt, phivn, n, dce_ssa_names);
     430                 :             : 
     431                 :             :           /* Remove old stmt. The phi and all of maybe its depedencies
     432                 :             :              will be removed later via simple_dce_from_worklist. */
     433                 :         150 :           gsi = gsi_for_stmt (use_stmt);
     434                 :             :           /* Unlinking the VDEF here is fine as we are sure that we process
     435                 :             :              stmts in execution order due to aggregate copies having VDEFs
     436                 :             :              and we emit loads on the edges in the very same order.
     437                 :             :              We get multiple copies (or intermediate register loads) handled
     438                 :             :              only by walking PHIs or immediate uses in a lucky order though,
     439                 :             :              so we could signal the caller to re-start iterating over PHIs
     440                 :             :              when we come here which would make it quadratic in the number
     441                 :             :              of PHIs.  */
     442                 :         150 :           unlink_stmt_vdef (use_stmt);
     443                 :         150 :           gsi_remove (&gsi, true);
     444                 :             : 
     445                 :         150 :           changed = true;
     446                 :             :         }
     447                 :             : 
     448                 :             :       /* Found a proper dereference.  Insert a phi node if this
     449                 :             :          is the first load transformation.  */
     450                 :       15274 :       else if (!phi_inserted)
     451                 :             :         {
     452                 :       15262 :           res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n, dce_ssa_names);
     453                 :       15262 :           type = TREE_TYPE (res);
     454                 :             : 
     455                 :             :           /* Remember the value we created for *ptr.  */
     456                 :       15262 :           phivn[SSA_NAME_VERSION (ptr)].value = res;
     457                 :       15262 :           phivn[SSA_NAME_VERSION (ptr)].vuse = vuse;
     458                 :             : 
     459                 :             :           /* Remove old stmt.  The phi and all of maybe its depedencies
     460                 :             :              will be removed later via simple_dce_from_worklist. */
     461                 :       15262 :           gsi = gsi_for_stmt (use_stmt);
     462                 :       15262 :           gsi_remove (&gsi, true);
     463                 :             : 
     464                 :       15262 :           phi_inserted = true;
     465                 :       15262 :           changed = true;
     466                 :             :         }
     467                 :             :       else
     468                 :             :         {
     469                 :             :           /* Further replacements are easy, just make a copy out of the
     470                 :             :              load.  */
     471                 :          12 :           gimple_assign_set_rhs1 (use_stmt, res);
     472                 :          12 :           update_stmt (use_stmt);
     473                 :          12 :           changed = true;
     474                 :             :         }
     475                 :             : 
     476                 :       48283 : next:;
     477                 :             :       /* Continue searching for a proper dereference.  */
     478                 :       33686 :     }
     479                 :             : 
     480                 :       33686 :   return changed;
     481                 :             : }
     482                 :             : 
     483                 :             : /* Main entry for phiprop pass.  */
     484                 :             : 
     485                 :             : namespace {
     486                 :             : 
     487                 :             : const pass_data pass_data_phiprop =
     488                 :             : {
     489                 :             :   GIMPLE_PASS, /* type */
     490                 :             :   "phiprop", /* name */
     491                 :             :   OPTGROUP_NONE, /* optinfo_flags */
     492                 :             :   TV_TREE_PHIPROP, /* tv_id */
     493                 :             :   ( PROP_cfg | PROP_ssa ), /* properties_required */
     494                 :             :   0, /* properties_provided */
     495                 :             :   0, /* properties_destroyed */
     496                 :             :   0, /* todo_flags_start */
     497                 :             :   0, /* todo_flags_finish */
     498                 :             : };
     499                 :             : 
     500                 :             : class pass_phiprop : public gimple_opt_pass
     501                 :             : {
     502                 :             : public:
     503                 :      573364 :   pass_phiprop (gcc::context *ctxt)
     504                 :     1146728 :     : gimple_opt_pass (pass_data_phiprop, ctxt)
     505                 :             :   {}
     506                 :             : 
     507                 :             :   /* opt_pass methods: */
     508                 :      286682 :   opt_pass * clone () final override { return new pass_phiprop (m_ctxt); }
     509                 :     3452401 :   bool gate (function *) final override { return flag_tree_phiprop; }
     510                 :             :   unsigned int execute (function *) final override;
     511                 :             : 
     512                 :             : }; // class pass_phiprop
     513                 :             : 
     514                 :             : unsigned int
     515                 :     3452227 : pass_phiprop::execute (function *fun)
     516                 :             : {
     517                 :     3452227 :   struct phiprop_d *phivn;
     518                 :     3452227 :   bool did_something = false;
     519                 :     3452227 :   basic_block bb;
     520                 :     3452227 :   gphi_iterator gsi;
     521                 :     3452227 :   unsigned i;
     522                 :     3452227 :   size_t n;
     523                 :     3452227 :   auto_bitmap dce_ssa_names;
     524                 :             : 
     525                 :     3452227 :   calculate_dominance_info (CDI_DOMINATORS);
     526                 :             : 
     527                 :     3452227 :   n = num_ssa_names;
     528                 :     3452227 :   phivn = XCNEWVEC (struct phiprop_d, n);
     529                 :             : 
     530                 :             :   /* Walk the dominator tree in preorder.  */
     531                 :     3452227 :   auto_vec<basic_block> bbs
     532                 :             :   = get_all_dominated_blocks (CDI_DOMINATORS,
     533                 :     3452227 :                               single_succ (ENTRY_BLOCK_PTR_FOR_FN (fun)));
     534                 :    28322718 :   FOR_EACH_VEC_ELT (bbs, i, bb)
     535                 :             :     {
     536                 :             :       /* Since we're going to move dereferences across predecessor
     537                 :             :          edges avoid blocks with abnormal predecessors.  */
     538                 :    24870491 :       if (bb_has_abnormal_pred (bb))
     539                 :        8427 :         continue;
     540                 :    24862064 :       gphi *vphi = get_virtual_phi (bb);
     541                 :    32838329 :       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     542                 :     7976265 :         did_something |= propagate_with_phi (bb, vphi, gsi.phi (),
     543                 :             :                                              phivn, n, dce_ssa_names);
     544                 :             :     }
     545                 :             : 
     546                 :     3452227 :   if (did_something)
     547                 :             :     {
     548                 :       13028 :       gsi_commit_edge_inserts ();
     549                 :       13028 :       simple_dce_from_worklist (dce_ssa_names);
     550                 :             :     }
     551                 :             : 
     552                 :     3452227 :   free (phivn);
     553                 :             : 
     554                 :     3452227 :   free_dominance_info (CDI_POST_DOMINATORS);
     555                 :             : 
     556                 :     3452227 :   return did_something ? TODO_update_ssa_only_virtuals : 0;
     557                 :     3452227 : }
     558                 :             : 
     559                 :             : } // anon namespace
     560                 :             : 
     561                 :             : gimple_opt_pass *
     562                 :      286682 : make_pass_phiprop (gcc::context *ctxt)
     563                 :             : {
     564                 :      286682 :   return new pass_phiprop (ctxt);
     565                 :             : }
        

Generated by: LCOV version 2.1-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.