LCOV - code coverage report
Current view: top level - gcc - tree-ssa-dom.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.4 % 1062 1013
Test Date: 2024-04-13 14:00:49 Functions: 100.0 % 43 43
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* SSA Dominator optimizations for trees
       2                 :             :    Copyright (C) 2001-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by Diego Novillo <dnovillo@redhat.com>
       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 "cfganal.h"
      32                 :             : #include "cfgloop.h"
      33                 :             : #include "gimple-iterator.h"
      34                 :             : #include "gimple-fold.h"
      35                 :             : #include "tree-eh.h"
      36                 :             : #include "tree-inline.h"
      37                 :             : #include "tree-cfg.h"
      38                 :             : #include "tree-into-ssa.h"
      39                 :             : #include "domwalk.h"
      40                 :             : #include "tree-ssa-propagate.h"
      41                 :             : #include "tree-ssa-threadupdate.h"
      42                 :             : #include "tree-ssa-scopedtables.h"
      43                 :             : #include "tree-ssa-threadedge.h"
      44                 :             : #include "tree-ssa-dom.h"
      45                 :             : #include "gimplify.h"
      46                 :             : #include "tree-cfgcleanup.h"
      47                 :             : #include "dbgcnt.h"
      48                 :             : #include "alloc-pool.h"
      49                 :             : #include "tree-vrp.h"
      50                 :             : #include "vr-values.h"
      51                 :             : #include "gimple-range.h"
      52                 :             : #include "gimple-range-path.h"
      53                 :             : #include "alias.h"
      54                 :             : 
      55                 :             : /* This file implements optimizations on the dominator tree.  */
      56                 :             : 
      57                 :             : /* Structure for recording edge equivalences.
      58                 :             : 
      59                 :             :    Computing and storing the edge equivalences instead of creating
      60                 :             :    them on-demand can save significant amounts of time, particularly
      61                 :             :    for pathological cases involving switch statements.
      62                 :             : 
      63                 :             :    These structures live for a single iteration of the dominator
      64                 :             :    optimizer in the edge's AUX field.  At the end of an iteration we
      65                 :             :    free each of these structures.  */
      66                 :             : class edge_info
      67                 :             : {
      68                 :             :  public:
      69                 :             :   typedef std::pair <tree, tree> equiv_pair;
      70                 :             :   edge_info (edge);
      71                 :             :   ~edge_info ();
      72                 :             : 
      73                 :             :   /* Record a simple LHS = RHS equivalence.  This may trigger
      74                 :             :      calls to derive_equivalences.  */
      75                 :             :   void record_simple_equiv (tree, tree);
      76                 :             : 
      77                 :             :   /* If traversing this edge creates simple equivalences, we store
      78                 :             :      them as LHS/RHS pairs within this vector.  */
      79                 :             :   vec<equiv_pair> simple_equivalences;
      80                 :             : 
      81                 :             :   /* Traversing an edge may also indicate one or more particular conditions
      82                 :             :      are true or false.  */
      83                 :             :   vec<cond_equivalence> cond_equivalences;
      84                 :             : 
      85                 :             :  private:
      86                 :             :   /* Derive equivalences by walking the use-def chains.  */
      87                 :             :   void derive_equivalences (tree, tree, int);
      88                 :             : };
      89                 :             : 
      90                 :             : /* Track whether or not we have changed the control flow graph.  */
      91                 :             : static bool cfg_altered;
      92                 :             : 
      93                 :             : /* Bitmap of blocks that have had EH statements cleaned.  We should
      94                 :             :    remove their dead edges eventually.  */
      95                 :             : static bitmap need_eh_cleanup;
      96                 :             : static vec<gimple *> need_noreturn_fixup;
      97                 :             : 
      98                 :             : /* Statistics for dominator optimizations.  */
      99                 :             : struct opt_stats_d
     100                 :             : {
     101                 :             :   long num_stmts;
     102                 :             :   long num_exprs_considered;
     103                 :             :   long num_re;
     104                 :             :   long num_const_prop;
     105                 :             :   long num_copy_prop;
     106                 :             : };
     107                 :             : 
     108                 :             : static struct opt_stats_d opt_stats;
     109                 :             : 
     110                 :             : /* Local functions.  */
     111                 :             : static void record_equality (tree, tree, class const_and_copies *);
     112                 :             : static void record_equivalences_from_phis (basic_block);
     113                 :             : static void record_equivalences_from_incoming_edge (basic_block,
     114                 :             :                                                     class const_and_copies *,
     115                 :             :                                                     class avail_exprs_stack *,
     116                 :             :                                                     bitmap blocks_on_stack);
     117                 :             : static void eliminate_redundant_computations (gimple_stmt_iterator *,
     118                 :             :                                               class const_and_copies *,
     119                 :             :                                               class avail_exprs_stack *);
     120                 :             : static void record_equivalences_from_stmt (gimple *, int,
     121                 :             :                                            class avail_exprs_stack *);
     122                 :             : static void dump_dominator_optimization_stats (FILE *file,
     123                 :             :                                                hash_table<expr_elt_hasher> *);
     124                 :             : static void record_temporary_equivalences (edge, class const_and_copies *,
     125                 :             :                                            class avail_exprs_stack *, bitmap);
     126                 :             : 
     127                 :             : /* Constructor for EDGE_INFO.  An EDGE_INFO instance is always
     128                 :             :    associated with an edge E.  */
     129                 :             : 
     130                 :    31104663 : edge_info::edge_info (edge e)
     131                 :             : {
     132                 :             :   /* Free the old one associated with E, if it exists and
     133                 :             :      associate our new object with E.  */
     134                 :    31104663 :   free_dom_edge_info (e);
     135                 :    31104663 :   e->aux = this;
     136                 :             : 
     137                 :             :   /* And initialize the embedded vectors.  */
     138                 :    31104663 :   simple_equivalences = vNULL;
     139                 :    31104663 :   cond_equivalences = vNULL;
     140                 :    31104663 : }
     141                 :             : 
     142                 :             : /* Destructor just needs to release the vectors.  */
     143                 :             : 
     144                 :    31104663 : edge_info::~edge_info (void)
     145                 :             : {
     146                 :    31104663 :   this->cond_equivalences.release ();
     147                 :    31104663 :   this->simple_equivalences.release ();
     148                 :    31104663 : }
     149                 :             : 
     150                 :             : /* NAME is known to have the value VALUE, which must be a constant.
     151                 :             : 
     152                 :             :    Walk through its use-def chain to see if there are other equivalences
     153                 :             :    we might be able to derive.
     154                 :             : 
     155                 :             :    RECURSION_LIMIT controls how far back we recurse through the use-def
     156                 :             :    chains.  */
     157                 :             : 
     158                 :             : void
     159                 :    11985096 : edge_info::derive_equivalences (tree name, tree value, int recursion_limit)
     160                 :             : {
     161                 :    13765420 :   if (TREE_CODE (name) != SSA_NAME || TREE_CODE (value) != INTEGER_CST)
     162                 :             :     return;
     163                 :             : 
     164                 :             :   /* This records the equivalence for the toplevel object.  Do
     165                 :             :      this before checking the recursion limit.  */
     166                 :    13765176 :   simple_equivalences.safe_push (equiv_pair (name, value));
     167                 :             : 
     168                 :             :   /* Limit how far up the use-def chains we are willing to walk.  */
     169                 :    13765176 :   if (recursion_limit == 0)
     170                 :             :     return;
     171                 :             : 
     172                 :             :   /* We can walk up the use-def chains to potentially find more
     173                 :             :      equivalences.  */
     174                 :    13752922 :   gimple *def_stmt = SSA_NAME_DEF_STMT (name);
     175                 :    13752922 :   if (is_gimple_assign (def_stmt))
     176                 :             :     {
     177                 :     8870816 :       enum tree_code code = gimple_assign_rhs_code (def_stmt);
     178                 :     8870816 :       switch (code)
     179                 :             :         {
     180                 :             :         /* If the result of an OR is zero, then its operands are, too.  */
     181                 :      544248 :         case BIT_IOR_EXPR:
     182                 :      544248 :           if (integer_zerop (value))
     183                 :             :             {
     184                 :      297858 :               tree rhs1 = gimple_assign_rhs1 (def_stmt);
     185                 :      297858 :               tree rhs2 = gimple_assign_rhs2 (def_stmt);
     186                 :             : 
     187                 :      297858 :               value = build_zero_cst (TREE_TYPE (rhs1));
     188                 :      297858 :               derive_equivalences (rhs1, value, recursion_limit - 1);
     189                 :      297858 :               value = build_zero_cst (TREE_TYPE (rhs2));
     190                 :      297858 :               derive_equivalences (rhs2, value, recursion_limit - 1);
     191                 :             :             }
     192                 :             :           break;
     193                 :             : 
     194                 :             :         /* If the result of an AND is nonzero, then its operands are, too.  */
     195                 :      988334 :         case BIT_AND_EXPR:
     196                 :      988334 :           if (!integer_zerop (value))
     197                 :             :             {
     198                 :      404778 :               tree rhs1 = gimple_assign_rhs1 (def_stmt);
     199                 :      404778 :               tree rhs2 = gimple_assign_rhs2 (def_stmt);
     200                 :             : 
     201                 :             :               /* If either operand has a boolean range, then we
     202                 :             :                  know its value must be one, otherwise we just know it
     203                 :             :                  is nonzero.  The former is clearly useful, I haven't
     204                 :             :                  seen cases where the latter is helpful yet.  */
     205                 :      404778 :               if (TREE_CODE (rhs1) == SSA_NAME)
     206                 :             :                 {
     207                 :      404778 :                   if (ssa_name_has_boolean_range (rhs1))
     208                 :             :                     {
     209                 :      247565 :                       value = build_one_cst (TREE_TYPE (rhs1));
     210                 :      247565 :                       derive_equivalences (rhs1, value, recursion_limit - 1);
     211                 :             :                     }
     212                 :             :                 }
     213                 :      404778 :               if (TREE_CODE (rhs2) == SSA_NAME)
     214                 :             :                 {
     215                 :      249591 :                   if (ssa_name_has_boolean_range (rhs2))
     216                 :             :                     {
     217                 :      247325 :                       value = build_one_cst (TREE_TYPE (rhs2));
     218                 :      247325 :                       derive_equivalences (rhs2, value, recursion_limit - 1);
     219                 :             :                     }
     220                 :             :                 }
     221                 :             :             }
     222                 :             :           break;
     223                 :             : 
     224                 :             :         /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
     225                 :             :            set via a widening type conversion, then we may be able to record
     226                 :             :            additional equivalences.  */
     227                 :      364476 :         CASE_CONVERT:
     228                 :      364476 :           {
     229                 :      364476 :             tree rhs = gimple_assign_rhs1 (def_stmt);
     230                 :      364476 :             tree rhs_type = TREE_TYPE (rhs);
     231                 :      364476 :             if (INTEGRAL_TYPE_P (rhs_type)
     232                 :      362452 :                 && (TYPE_PRECISION (TREE_TYPE (name))
     233                 :      362452 :                     >= TYPE_PRECISION (rhs_type))
     234                 :      540670 :                 && int_fits_type_p (value, rhs_type))
     235                 :      169050 :               derive_equivalences (rhs,
     236                 :             :                                    fold_convert (rhs_type, value),
     237                 :             :                                    recursion_limit - 1);
     238                 :             :             break;
     239                 :             :           }
     240                 :             : 
     241                 :             :         /* We can invert the operation of these codes trivially if
     242                 :             :            one of the RHS operands is a constant to produce a known
     243                 :             :            value for the other RHS operand.  */
     244                 :      872540 :         case POINTER_PLUS_EXPR:
     245                 :      872540 :         case PLUS_EXPR:
     246                 :      872540 :           {
     247                 :      872540 :             tree rhs1 = gimple_assign_rhs1 (def_stmt);
     248                 :      872540 :             tree rhs2 = gimple_assign_rhs2 (def_stmt);
     249                 :             : 
     250                 :             :             /* If either argument is a constant, then we can compute
     251                 :             :                a constant value for the nonconstant argument.  */
     252                 :      872540 :             if (TREE_CODE (rhs1) == INTEGER_CST
     253                 :           0 :                 && TREE_CODE (rhs2) == SSA_NAME)
     254                 :           0 :               derive_equivalences (rhs2,
     255                 :           0 :                                    fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
     256                 :             :                                                 value, rhs1),
     257                 :             :                                    recursion_limit - 1);
     258                 :      872540 :             else if (TREE_CODE (rhs2) == INTEGER_CST
     259                 :      828938 :                      && TREE_CODE (rhs1) == SSA_NAME)
     260                 :      828938 :               derive_equivalences (rhs1,
     261                 :      828938 :                                    fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
     262                 :             :                                                 value, rhs2),
     263                 :             :                                    recursion_limit - 1);
     264                 :             :             break;
     265                 :             :           }
     266                 :             : 
     267                 :             :         /* If one of the operands is a constant, then we can compute
     268                 :             :            the value of the other operand.  If both operands are
     269                 :             :            SSA_NAMEs, then they must be equal if the result is zero.  */
     270                 :       69799 :         case MINUS_EXPR:
     271                 :       69799 :           {
     272                 :       69799 :             tree rhs1 = gimple_assign_rhs1 (def_stmt);
     273                 :       69799 :             tree rhs2 = gimple_assign_rhs2 (def_stmt);
     274                 :             : 
     275                 :             :             /* If either argument is a constant, then we can compute
     276                 :             :                a constant value for the nonconstant argument.  */
     277                 :       69799 :             if (TREE_CODE (rhs1) == INTEGER_CST
     278                 :        6454 :                 && TREE_CODE (rhs2) == SSA_NAME)
     279                 :        6454 :               derive_equivalences (rhs2,
     280                 :        6454 :                                    fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
     281                 :             :                                                 rhs1, value),
     282                 :             :                                    recursion_limit - 1);
     283                 :       63345 :             else if (TREE_CODE (rhs2) == INTEGER_CST
     284                 :       26024 :                      && TREE_CODE (rhs1) == SSA_NAME)
     285                 :       26024 :               derive_equivalences (rhs1,
     286                 :       26024 :                                    fold_binary (PLUS_EXPR, TREE_TYPE (rhs1),
     287                 :             :                                                 value, rhs2),
     288                 :             :                                    recursion_limit - 1);
     289                 :       37321 :             else if (integer_zerop (value))
     290                 :             :               {
     291                 :       16255 :                 tree cond = build2 (EQ_EXPR, boolean_type_node,
     292                 :             :                                     gimple_assign_rhs1 (def_stmt),
     293                 :             :                                     gimple_assign_rhs2 (def_stmt));
     294                 :       16255 :                 tree inverted = invert_truthvalue (cond);
     295                 :       16255 :                 record_conditions (&this->cond_equivalences, cond, inverted);
     296                 :             :               }
     297                 :             :             break;
     298                 :             :           }
     299                 :             : 
     300                 :      636150 :         case EQ_EXPR:
     301                 :      636150 :         case NE_EXPR:
     302                 :      636150 :           {
     303                 :      261149 :             if ((code == EQ_EXPR && integer_onep (value))
     304                 :      763260 :                 || (code == NE_EXPR && integer_zerop (value)))
     305                 :             :               {
     306                 :      371507 :                 tree rhs1 = gimple_assign_rhs1 (def_stmt);
     307                 :      371507 :                 tree rhs2 = gimple_assign_rhs2 (def_stmt);
     308                 :             : 
     309                 :             :                 /* If either argument is a constant, then record the
     310                 :             :                    other argument as being the same as that constant.
     311                 :             : 
     312                 :             :                    If neither operand is a constant, then we have a
     313                 :             :                    conditional name == name equivalence.  */
     314                 :      371507 :                 if (TREE_CODE (rhs1) == INTEGER_CST)
     315                 :           0 :                   derive_equivalences (rhs2, rhs1, recursion_limit - 1);
     316                 :      371507 :                 else if (TREE_CODE (rhs2) == INTEGER_CST)
     317                 :      158728 :                   derive_equivalences (rhs1, rhs2, recursion_limit - 1);
     318                 :             :               }
     319                 :             :             else
     320                 :             :               {
     321                 :      264643 :                 tree cond = build2 (code, boolean_type_node,
     322                 :             :                                     gimple_assign_rhs1 (def_stmt),
     323                 :      264643 :                                     gimple_assign_rhs2 (def_stmt));
     324                 :      264643 :                 tree inverted = invert_truthvalue (cond);
     325                 :      264643 :                 if (integer_zerop (value))
     326                 :      127110 :                   std::swap (cond, inverted);
     327                 :      264643 :                 record_conditions (&this->cond_equivalences, cond, inverted);
     328                 :             :               }
     329                 :             :             break;
     330                 :             :           }
     331                 :             : 
     332                 :             :         /* For BIT_NOT and NEGATE, we can just apply the operation to the
     333                 :             :            VALUE to get the new equivalence.  It will always be a constant
     334                 :             :            so we can recurse.  */
     335                 :       45947 :         case BIT_NOT_EXPR:
     336                 :       45947 :         case NEGATE_EXPR:
     337                 :       45947 :           {
     338                 :       45947 :             tree rhs = gimple_assign_rhs1 (def_stmt);
     339                 :       45947 :             tree res;
     340                 :             :             /* If this is a NOT and the operand has a boolean range, then we
     341                 :             :                know its value must be zero or one.  We are not supposed to
     342                 :             :                have a BIT_NOT_EXPR for boolean types with precision > 1 in
     343                 :             :                the general case, see e.g. the handling of TRUTH_NOT_EXPR in
     344                 :             :                the gimplifier, but it can be generated by match.pd out of
     345                 :             :                a BIT_XOR_EXPR wrapped in a BIT_AND_EXPR.  Now the handling
     346                 :             :                of BIT_AND_EXPR above already forces a specific semantics for
     347                 :             :                boolean types with precision > 1 so we must do the same here,
     348                 :             :                otherwise we could change the semantics of TRUTH_NOT_EXPR for
     349                 :             :                boolean types with precision > 1.  */
     350                 :       45947 :             if (code == BIT_NOT_EXPR
     351                 :       45784 :                 && TREE_CODE (rhs) == SSA_NAME
     352                 :       91731 :                 && ssa_name_has_boolean_range (rhs))
     353                 :             :               {
     354                 :       45404 :                 if ((TREE_INT_CST_LOW (value) & 1) == 0)
     355                 :       23086 :                   res = build_one_cst (TREE_TYPE (rhs));
     356                 :             :                 else
     357                 :       22318 :                   res = build_zero_cst (TREE_TYPE (rhs));
     358                 :             :               }
     359                 :             :             else
     360                 :         543 :               res = fold_build1 (code, TREE_TYPE (rhs), value);
     361                 :       45947 :             derive_equivalences (rhs, res, recursion_limit - 1);
     362                 :       45947 :             break;
     363                 :             :           }
     364                 :             : 
     365                 :     5349322 :         default:
     366                 :     5349322 :           {
     367                 :     5349322 :             if (TREE_CODE_CLASS (code) == tcc_comparison)
     368                 :             :               {
     369                 :      496835 :                 tree cond = build2 (code, boolean_type_node,
     370                 :             :                                     gimple_assign_rhs1 (def_stmt),
     371                 :      496835 :                                     gimple_assign_rhs2 (def_stmt));
     372                 :      496835 :                 tree inverted = invert_truthvalue (cond);
     373                 :      496835 :                 if (integer_zerop (value))
     374                 :      210307 :                   std::swap (cond, inverted);
     375                 :      496835 :                 record_conditions (&this->cond_equivalences, cond, inverted);
     376                 :      496835 :                 break;
     377                 :             :               }
     378                 :             :             break;
     379                 :             :           }
     380                 :             :         }
     381                 :             :     }
     382                 :             : }
     383                 :             : 
     384                 :             : void
     385                 :    14044129 : edge_info::record_simple_equiv (tree lhs, tree rhs)
     386                 :             : {
     387                 :             :   /* If the RHS is a constant, then we may be able to derive
     388                 :             :      further equivalences.  Else just record the name = name
     389                 :             :      equivalence.  */
     390                 :    14044129 :   if (TREE_CODE (rhs) == INTEGER_CST)
     391                 :    11439673 :     derive_equivalences (lhs, rhs, 4);
     392                 :             :   else
     393                 :     2604456 :     simple_equivalences.safe_push (equiv_pair (lhs, rhs));
     394                 :    14044129 : }
     395                 :             : 
     396                 :             : /* Free the edge_info data attached to E, if it exists and
     397                 :             :    clear e->aux.  */
     398                 :             : 
     399                 :             : void
     400                 :   114813533 : free_dom_edge_info (edge e)
     401                 :             : {
     402                 :   114813533 :   class edge_info *edge_info = (class edge_info *)e->aux;
     403                 :             : 
     404                 :   114813533 :   if (edge_info)
     405                 :    31104663 :     delete edge_info;
     406                 :   114813533 :   e->aux = NULL;
     407                 :   114813533 : }
     408                 :             : 
     409                 :             : /* Free all EDGE_INFO structures associated with edges in the CFG.
     410                 :             :    If a particular edge can be threaded, copy the redirection
     411                 :             :    target from the EDGE_INFO structure into the edge's AUX field
     412                 :             :    as required by code to update the CFG and SSA graph for
     413                 :             :    jump threading.  */
     414                 :             : 
     415                 :             : static void
     416                 :     1944572 : free_all_edge_infos (void)
     417                 :             : {
     418                 :     1944572 :   basic_block bb;
     419                 :     1944572 :   edge_iterator ei;
     420                 :     1944572 :   edge e;
     421                 :             : 
     422                 :    20978207 :   FOR_EACH_BB_FN (bb, cfun)
     423                 :             :     {
     424                 :    45887481 :       FOR_EACH_EDGE (e, ei, bb->preds)
     425                 :    26853846 :         free_dom_edge_info (e);
     426                 :             :     }
     427                 :     1944572 : }
     428                 :             : 
     429                 :             : /* Return TRUE if BB has precisely two preds, one of which
     430                 :             :    is a backedge from a forwarder block where the forwarder
     431                 :             :    block is a direct successor of BB.  Being a forwarder
     432                 :             :    block, it has no side effects other than transfer of
     433                 :             :    control.  Otherwise return FALSE.  */
     434                 :             : 
     435                 :             : static bool
     436                 :    15474377 : single_block_loop_p (basic_block bb)
     437                 :             : {
     438                 :             :   /* Two preds.  */
     439                 :    15474377 :   if (EDGE_COUNT (bb->preds) != 2)
     440                 :             :     return false;
     441                 :             : 
     442                 :             :   /* One and only one of the edges must be marked with
     443                 :             :      EDGE_DFS_BACK.  */
     444                 :     4506274 :   basic_block pred = NULL;
     445                 :     4506274 :   unsigned int count = 0;
     446                 :     4506274 :   if (EDGE_PRED (bb, 0)->flags & EDGE_DFS_BACK)
     447                 :             :     {
     448                 :     1546630 :       pred = EDGE_PRED (bb, 0)->src;
     449                 :     1546630 :       count++;
     450                 :             :     }
     451                 :     4506274 :   if (EDGE_PRED (bb, 1)->flags & EDGE_DFS_BACK)
     452                 :             :     {
     453                 :      366202 :       pred = EDGE_PRED (bb, 1)->src;
     454                 :      366202 :       count++;
     455                 :             :     }
     456                 :             : 
     457                 :     4506274 :   if (count != 1)
     458                 :             :     return false;
     459                 :             : 
     460                 :             :   /* Now examine PRED.  It should have a single predecessor which
     461                 :             :      is BB and a single successor that is also BB.  */
     462                 :     1912832 :   if (EDGE_COUNT (pred->preds) != 1
     463                 :    16430701 :       || EDGE_COUNT (pred->succs) != 1
     464                 :     1848193 :       || EDGE_PRED (pred, 0)->src != bb
     465                 :     2870353 :       || EDGE_SUCC (pred, 0)->dest != bb)
     466                 :             :     return false;
     467                 :             : 
     468                 :             :   /* This looks good from a CFG standpoint.  Now look at the guts
     469                 :             :      of PRED.  Basically we want to verify there are no PHI nodes
     470                 :             :      and no real statements.  */
     471                 :      957521 :   if (! gimple_seq_empty_p (phi_nodes (pred)))
     472                 :             :     return false;
     473                 :             : 
     474                 :      953191 :   gimple_stmt_iterator gsi;
     475                 :     1952666 :   for (gsi = gsi_last_bb (pred); !gsi_end_p (gsi); gsi_prev (&gsi))
     476                 :             :     {
     477                 :       84464 :       gimple *stmt = gsi_stmt (gsi);
     478                 :             : 
     479                 :       84464 :       switch (gimple_code (stmt))
     480                 :             :         {
     481                 :           0 :           case GIMPLE_LABEL:
     482                 :           0 :             if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
     483                 :             :               return false;
     484                 :             :             break;
     485                 :             : 
     486                 :             :           case GIMPLE_DEBUG:
     487                 :             :             break;
     488                 :             : 
     489                 :             :           default:
     490                 :             :             return false;
     491                 :             :         }
     492                 :             :     }
     493                 :             : 
     494                 :             :   return true;
     495                 :             : }
     496                 :             : 
     497                 :             : /* We have finished optimizing BB, record any information implied by
     498                 :             :    taking a specific outgoing edge from BB.  */
     499                 :             : 
     500                 :             : static void
     501                 :    39974008 : record_edge_info (basic_block bb)
     502                 :             : {
     503                 :    39974008 :   gimple_stmt_iterator gsi = gsi_last_bb (bb);
     504                 :    39974008 :   class edge_info *edge_info;
     505                 :             : 
     506                 :             :   /* Free all the outgoing edge info data associated with
     507                 :             :      BB's outgoing edges.  */
     508                 :    39974008 :   edge e;
     509                 :    39974008 :   edge_iterator ei;
     510                 :    95501888 :   FOR_EACH_EDGE (e, ei, bb->succs)
     511                 :    55527880 :     free_dom_edge_info (e);
     512                 :             : 
     513                 :    39974008 :   if (! gsi_end_p (gsi))
     514                 :             :     {
     515                 :    34234265 :       gimple *stmt = gsi_stmt (gsi);
     516                 :    34234265 :       location_t loc = gimple_location (stmt);
     517                 :             : 
     518                 :    34234265 :       if (gimple_code (stmt) == GIMPLE_SWITCH)
     519                 :             :         {
     520                 :       63622 :           gswitch *switch_stmt = as_a <gswitch *> (stmt);
     521                 :       63622 :           tree index = gimple_switch_index (switch_stmt);
     522                 :             : 
     523                 :       63622 :           if (TREE_CODE (index) == SSA_NAME)
     524                 :             :             {
     525                 :       63591 :               int i;
     526                 :       63591 :               int n_labels = gimple_switch_num_labels (switch_stmt);
     527                 :       63591 :               tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
     528                 :             : 
     529                 :      545970 :               for (i = 0; i < n_labels; i++)
     530                 :             :                 {
     531                 :      482379 :                   tree label = gimple_switch_label (switch_stmt, i);
     532                 :      482379 :                   basic_block target_bb
     533                 :      482379 :                     = label_to_block (cfun, CASE_LABEL (label));
     534                 :      482379 :                   if (CASE_HIGH (label)
     535                 :      453410 :                       || !CASE_LOW (label)
     536                 :      872198 :                       || info[target_bb->index])
     537                 :      142022 :                     info[target_bb->index] = error_mark_node;
     538                 :             :                   else
     539                 :      340357 :                     info[target_bb->index] = label;
     540                 :             :                 }
     541                 :             : 
     542                 :      489372 :               FOR_EACH_EDGE (e, ei, bb->succs)
     543                 :             :                 {
     544                 :      425781 :                   basic_block target_bb = e->dest;
     545                 :      425781 :                   tree label = info[target_bb->index];
     546                 :             : 
     547                 :      425781 :                   if (label != NULL && label != error_mark_node)
     548                 :             :                     {
     549                 :      628878 :                       tree x = fold_convert_loc (loc, TREE_TYPE (index),
     550                 :      314439 :                                                  CASE_LOW (label));
     551                 :      314439 :                       edge_info = new class edge_info (e);
     552                 :      314439 :                       edge_info->record_simple_equiv (index, x);
     553                 :             :                     }
     554                 :             :                 }
     555                 :       63591 :               free (info);
     556                 :             :             }
     557                 :             :         }
     558                 :             : 
     559                 :             :       /* A COND_EXPR may create equivalences too.  */
     560                 :    34234265 :       if (gimple_code (stmt) == GIMPLE_COND)
     561                 :             :         {
     562                 :    15474377 :           edge true_edge;
     563                 :    15474377 :           edge false_edge;
     564                 :             : 
     565                 :    15474377 :           tree op0 = gimple_cond_lhs (stmt);
     566                 :    15474377 :           tree op1 = gimple_cond_rhs (stmt);
     567                 :    15474377 :           enum tree_code code = gimple_cond_code (stmt);
     568                 :             : 
     569                 :    15474377 :           extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
     570                 :             : 
     571                 :             :           /* Special case comparing booleans against a constant as we
     572                 :             :              know the value of OP0 on both arms of the branch.  i.e., we
     573                 :             :              can record an equivalence for OP0 rather than COND. 
     574                 :             : 
     575                 :             :              However, don't do this if the constant isn't zero or one.
     576                 :             :              Such conditionals will get optimized more thoroughly during
     577                 :             :              the domwalk.  */
     578                 :    15474377 :           if ((code == EQ_EXPR || code == NE_EXPR)
     579                 :    12203472 :               && TREE_CODE (op0) == SSA_NAME
     580                 :    11876036 :               && ssa_name_has_boolean_range (op0)
     581                 :     1857942 :               && is_gimple_min_invariant (op1)
     582                 :    17275745 :               && (integer_zerop (op1) || integer_onep (op1)))
     583                 :             :             {
     584                 :     1801368 :               tree true_val = constant_boolean_node (true, TREE_TYPE (op0));
     585                 :     1801368 :               tree false_val = constant_boolean_node (false, TREE_TYPE (op0));
     586                 :             : 
     587                 :     1801368 :               if (code == EQ_EXPR)
     588                 :             :                 {
     589                 :       74940 :                   edge_info = new class edge_info (true_edge);
     590                 :      113072 :                   edge_info->record_simple_equiv (op0,
     591                 :       74940 :                                                   (integer_zerop (op1)
     592                 :             :                                                    ? false_val : true_val));
     593                 :       74940 :                   edge_info = new class edge_info (false_edge);
     594                 :      113072 :                   edge_info->record_simple_equiv (op0,
     595                 :       74940 :                                                   (integer_zerop (op1)
     596                 :             :                                                    ? true_val : false_val));
     597                 :             :                 }
     598                 :             :               else
     599                 :             :                 {
     600                 :     1726428 :                   edge_info = new class edge_info (true_edge);
     601                 :     1734608 :                   edge_info->record_simple_equiv (op0,
     602                 :     1726428 :                                                   (integer_zerop (op1)
     603                 :             :                                                    ? true_val : false_val));
     604                 :     1726428 :                   edge_info = new class edge_info (false_edge);
     605                 :     1734608 :                   edge_info->record_simple_equiv (op0,
     606                 :     1726428 :                                                   (integer_zerop (op1)
     607                 :             :                                                    ? false_val : true_val));
     608                 :             :                 }
     609                 :             :             }
     610                 :             :           /* This can show up in the IL as a result of copy propagation
     611                 :             :              it will eventually be canonicalized, but we have to cope
     612                 :             :              with this case within the pass.  */
     613                 :    13673009 :           else if (is_gimple_min_invariant (op0)
     614                 :    13673009 :                    && TREE_CODE (op1) == SSA_NAME)
     615                 :             :             {
     616                 :      254444 :               tree cond = build2 (code, boolean_type_node, op0, op1);
     617                 :      254444 :               tree inverted = invert_truthvalue_loc (loc, cond);
     618                 :      254444 :               bool can_infer_simple_equiv
     619                 :      254444 :                 = !(HONOR_SIGNED_ZEROS (op0) && real_maybe_zerop (op0))
     620                 :      254444 :                   && !DECIMAL_FLOAT_MODE_P (element_mode (TREE_TYPE (op0)));
     621                 :      254444 :               class edge_info *edge_info;
     622                 :             : 
     623                 :      254444 :               edge_info = new class edge_info (true_edge);
     624                 :      254444 :               record_conditions (&edge_info->cond_equivalences, cond, inverted);
     625                 :             : 
     626                 :      254444 :               if (can_infer_simple_equiv && code == EQ_EXPR)
     627                 :      208973 :                 edge_info->record_simple_equiv (op1, op0);
     628                 :             : 
     629                 :      254444 :               edge_info = new class edge_info (false_edge);
     630                 :      254444 :               record_conditions (&edge_info->cond_equivalences, inverted, cond);
     631                 :             : 
     632                 :      254444 :               if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
     633                 :       39103 :                 edge_info->record_simple_equiv (op1, op0);
     634                 :             :             }
     635                 :             : 
     636                 :    13418565 :           else if (TREE_CODE (op0) == SSA_NAME
     637                 :    13418565 :                    && (TREE_CODE (op1) == SSA_NAME
     638                 :    10012994 :                        || is_gimple_min_invariant (op1)))
     639                 :             :             {
     640                 :    13338951 :               tree cond = build2 (code, boolean_type_node, op0, op1);
     641                 :    13338951 :               tree inverted = invert_truthvalue_loc (loc, cond);
     642                 :    13338951 :               bool can_infer_simple_equiv
     643                 :    13985369 :                 = !(HONOR_SIGNED_ZEROS (op1) && real_maybe_zerop (op1))
     644                 :    13713568 :                   && !DECIMAL_FLOAT_MODE_P (element_mode (TREE_TYPE (op1)));
     645                 :    13338951 :               class edge_info *edge_info;
     646                 :             : 
     647                 :    13338951 :               edge_info = new class edge_info (true_edge);
     648                 :    13338951 :               record_conditions (&edge_info->cond_equivalences, cond, inverted);
     649                 :             : 
     650                 :    13338951 :               if (can_infer_simple_equiv && code == EQ_EXPR)
     651                 :     4479861 :                 edge_info->record_simple_equiv (op0, op1);
     652                 :             : 
     653                 :    13338951 :               edge_info = new class edge_info (false_edge);
     654                 :    13338951 :               record_conditions (&edge_info->cond_equivalences, inverted, cond);
     655                 :             : 
     656                 :    13338951 :               if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
     657                 :     5397468 :                 edge_info->record_simple_equiv (op0, op1);
     658                 :             :             }
     659                 :             : 
     660                 :             :           /* If this block is a single block loop, then we may be able to
     661                 :             :              record some equivalences on the loop's exit edge.  */
     662                 :    15474377 :           if (single_block_loop_p (bb))
     663                 :             :             {
     664                 :             :               /* We know it's a single block loop.  Now look at the loop
     665                 :             :                  exit condition.  What we're looking for is whether or not
     666                 :             :                  the exit condition is loop invariant which we can detect
     667                 :             :                  by checking if all the SSA_NAMEs referenced are defined
     668                 :             :                  outside the loop.  */
     669                 :      891869 :               if ((TREE_CODE (op0) != SSA_NAME
     670                 :      888440 :                    || gimple_bb (SSA_NAME_DEF_STMT (op0)) != bb)
     671                 :     1024735 :                   && (TREE_CODE (op1) != SSA_NAME
     672                 :      134051 :                       || gimple_bb (SSA_NAME_DEF_STMT (op1)) != bb))
     673                 :             :                 {
     674                 :             :                   /* At this point we know the exit condition is loop
     675                 :             :                      invariant.  The only way to get out of the loop is
     676                 :             :                      if it never traverses the backedge to begin with.  This
     677                 :             :                      implies that any PHI nodes create equivalances that we
     678                 :             :                      can attach to the loop exit edge.  */
     679                 :        2357 :                   bool alternative
     680                 :        2357 :                     = (EDGE_PRED (bb, 0)->flags & EDGE_DFS_BACK) ? 1 : 0;
     681                 :             : 
     682                 :        2357 :                   gphi_iterator gsi;
     683                 :        2357 :                   for (gsi = gsi_start_phis (bb);
     684                 :        4149 :                        !gsi_end_p (gsi);
     685                 :        1792 :                        gsi_next (&gsi))
     686                 :             :                     {
     687                 :             :                       /* Now get the EDGE_INFO class so we can append
     688                 :             :                          it to our list.  We want the successor edge
     689                 :             :                          where the destination is not the source of
     690                 :             :                          an incoming edge.  */
     691                 :        1792 :                       gphi *phi = gsi.phi ();
     692                 :        1792 :                       tree src = PHI_ARG_DEF (phi, alternative);
     693                 :        1792 :                       tree dst = PHI_RESULT (phi);
     694                 :             : 
     695                 :             :                       /* If the other alternative is the same as the result,
     696                 :             :                          then this is a degenerate and can be ignored.  */
     697                 :        1792 :                       if (dst == PHI_ARG_DEF (phi, !alternative))
     698                 :         243 :                         continue;
     699                 :             : 
     700                 :        1549 :                       if (EDGE_SUCC (bb, 0)->dest
     701                 :        1549 :                           != EDGE_PRED (bb, !alternative)->src)
     702                 :         257 :                         edge_info = (class edge_info *)EDGE_SUCC (bb, 0)->aux;
     703                 :             :                       else
     704                 :        1292 :                         edge_info = (class edge_info *)EDGE_SUCC (bb, 1)->aux;
     705                 :             : 
     706                 :             :                       /* Note that since this processing is done independently
     707                 :             :                          of other edge equivalency processing, we may not
     708                 :             :                          have an EDGE_INFO structure set up yet.  */
     709                 :        1549 :                       if (edge_info == NULL)
     710                 :         698 :                         edge_info = new class edge_info (false_edge);
     711                 :        1549 :                       edge_info->record_simple_equiv (dst, src);
     712                 :             :                     }
     713                 :             :                 }
     714                 :             :             }
     715                 :             :         }
     716                 :             :     }
     717                 :    39974008 : }
     718                 :             : 
     719                 :             : class dom_jt_state : public jt_state
     720                 :             : {
     721                 :             : public:
     722                 :     1944572 :   dom_jt_state (const_and_copies *copies, avail_exprs_stack *avails)
     723                 :     1944572 :     : m_copies (copies), m_avails (avails)
     724                 :             :   {
     725                 :     1944572 :     bitmap_tree_view (m_blocks_on_stack);
     726                 :     1944572 :   }
     727                 :    14875541 :   void push (edge e) override
     728                 :             :   {
     729                 :    14875541 :     m_copies->push_marker ();
     730                 :    14875541 :     m_avails->push_marker ();
     731                 :    14875541 :     jt_state::push (e);
     732                 :    14875541 :   }
     733                 :    14875541 :   void pop () override
     734                 :             :   {
     735                 :    14875541 :     m_copies->pop_to_marker ();
     736                 :    14875541 :     m_avails->pop_to_marker ();
     737                 :    14875541 :     jt_state::pop ();
     738                 :    14875541 :   }
     739                 :    13830247 :   void register_equivs_edge (edge e) override
     740                 :             :   {
     741                 :    13830247 :     record_temporary_equivalences (e, m_copies, m_avails, m_blocks_on_stack);
     742                 :    13830247 :   }
     743                 :             :   void register_equiv (tree dest, tree src, bool update) override;
     744                 :    62821119 :   bitmap get_blocks_on_stack () { return m_blocks_on_stack; }
     745                 :             : private:
     746                 :             :   const_and_copies *m_copies;
     747                 :             :   avail_exprs_stack *m_avails;
     748                 :             :   /* Set of blocks on the stack, to be used for medium-fast
     749                 :             :      dominance queries in back_propagate_equivalences.  */
     750                 :             :   auto_bitmap m_blocks_on_stack;
     751                 :             : };
     752                 :             : 
     753                 :             : void
     754                 :    19091722 : dom_jt_state::register_equiv (tree dest, tree src, bool)
     755                 :             : {
     756                 :    19091722 :   m_copies->record_const_or_copy (dest, src);
     757                 :    19091722 : }
     758                 :             : 
     759                 :     1944572 : class dom_jt_simplifier : public hybrid_jt_simplifier
     760                 :             : {
     761                 :             : public:
     762                 :     1944572 :   dom_jt_simplifier (avail_exprs_stack *avails, gimple_ranger *ranger,
     763                 :             :                      path_range_query *query)
     764                 :     3889144 :     : hybrid_jt_simplifier (ranger, query), m_avails (avails) { }
     765                 :             : 
     766                 :             : private:
     767                 :             :   tree simplify (gimple *, gimple *, basic_block, jt_state *) override;
     768                 :             :   avail_exprs_stack *m_avails;
     769                 :             : };
     770                 :             : 
     771                 :             : tree
     772                 :    27761714 : dom_jt_simplifier::simplify (gimple *stmt, gimple *within_stmt,
     773                 :             :                              basic_block bb, jt_state *state)
     774                 :             : {
     775                 :             :   /* First see if the conditional is in the hash table.  */
     776                 :    27761714 :   tree cached_lhs =  m_avails->lookup_avail_expr (stmt, false, true);
     777                 :    27761714 :   if (cached_lhs)
     778                 :             :     return cached_lhs;
     779                 :             : 
     780                 :             :   /* Otherwise call the ranger if possible.  */
     781                 :    27130942 :   if (state)
     782                 :     8267995 :     return hybrid_jt_simplifier::simplify (stmt, within_stmt, bb, state);
     783                 :             : 
     784                 :             :   return NULL;
     785                 :             : }
     786                 :             : 
     787                 :     3889144 : class dom_opt_dom_walker : public dom_walker
     788                 :             : {
     789                 :             : public:
     790                 :     1944572 :   dom_opt_dom_walker (cdi_direction direction,
     791                 :             :                       jump_threader *threader,
     792                 :             :                       dom_jt_state *state,
     793                 :             :                       gimple_ranger *ranger,
     794                 :             :                       const_and_copies *const_and_copies,
     795                 :             :                       avail_exprs_stack *avail_exprs_stack)
     796                 :     1944572 :     : dom_walker (direction, REACHABLE_BLOCKS)
     797                 :             :     {
     798                 :     1944572 :       m_ranger = ranger;
     799                 :     1944572 :       m_state = state;
     800                 :     1944572 :       m_dummy_cond = gimple_build_cond (NE_EXPR, integer_zero_node,
     801                 :             :                                         integer_zero_node, NULL, NULL);
     802                 :     1944572 :       m_const_and_copies = const_and_copies;
     803                 :     1944572 :       m_avail_exprs_stack = avail_exprs_stack;
     804                 :     1944572 :       m_threader = threader;
     805                 :     1944572 :     }
     806                 :             : 
     807                 :             :   edge before_dom_children (basic_block) final override;
     808                 :             :   void after_dom_children (basic_block) final override;
     809                 :             : 
     810                 :             : private:
     811                 :             : 
     812                 :             :   /* Unwindable equivalences, both const/copy and expression varieties.  */
     813                 :             :   class const_and_copies *m_const_and_copies;
     814                 :             :   class avail_exprs_stack *m_avail_exprs_stack;
     815                 :             : 
     816                 :             :   /* Dummy condition to avoid creating lots of throw away statements.  */
     817                 :             :   gcond *m_dummy_cond;
     818                 :             : 
     819                 :             :   /* Optimize a single statement within a basic block using the
     820                 :             :      various tables mantained by DOM.  Returns the taken edge if
     821                 :             :      the statement is a conditional with a statically determined
     822                 :             :      value.  */
     823                 :             :   edge optimize_stmt (basic_block, gimple_stmt_iterator *, bool *);
     824                 :             : 
     825                 :             :   void set_global_ranges_from_unreachable_edges (basic_block);
     826                 :             : 
     827                 :             :   void test_for_singularity (gimple *, avail_exprs_stack *);
     828                 :             :   edge fold_cond (gcond *cond);
     829                 :             : 
     830                 :             :   jump_threader *m_threader;
     831                 :             :   gimple_ranger *m_ranger;
     832                 :             :   dom_jt_state *m_state;
     833                 :             : };
     834                 :             : 
     835                 :             : /* Jump threading, redundancy elimination and const/copy propagation.
     836                 :             : 
     837                 :             :    This pass may expose new symbols that need to be renamed into SSA.  For
     838                 :             :    every new symbol exposed, its corresponding bit will be set in
     839                 :             :    VARS_TO_RENAME.  */
     840                 :             : 
     841                 :             : namespace {
     842                 :             : 
     843                 :             : const pass_data pass_data_dominator =
     844                 :             : {
     845                 :             :   GIMPLE_PASS, /* type */
     846                 :             :   "dom", /* name */
     847                 :             :   OPTGROUP_NONE, /* optinfo_flags */
     848                 :             :   TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
     849                 :             :   ( PROP_cfg | PROP_ssa ), /* properties_required */
     850                 :             :   0, /* properties_provided */
     851                 :             :   0, /* properties_destroyed */
     852                 :             :   0, /* todo_flags_start */
     853                 :             :   ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
     854                 :             : };
     855                 :             : 
     856                 :             : class pass_dominator : public gimple_opt_pass
     857                 :             : {
     858                 :             : public:
     859                 :      841365 :   pass_dominator (gcc::context *ctxt)
     860                 :      841365 :     : gimple_opt_pass (pass_data_dominator, ctxt),
     861                 :     1682730 :       may_peel_loop_headers_p (false)
     862                 :             :   {}
     863                 :             : 
     864                 :             :   /* opt_pass methods: */
     865                 :      560910 :   opt_pass * clone () final override { return new pass_dominator (m_ctxt); }
     866                 :      841365 :   void set_pass_param (unsigned int n, bool param) final override
     867                 :             :     {
     868                 :      841365 :       gcc_assert (n == 0);
     869                 :      841365 :       may_peel_loop_headers_p = param;
     870                 :      841365 :     }
     871                 :     1945281 :   bool gate (function *) final override { return flag_tree_dom != 0; }
     872                 :             :   unsigned int execute (function *) final override;
     873                 :             : 
     874                 :             :  private:
     875                 :             :   /* This flag is used to prevent loops from being peeled repeatedly in jump
     876                 :             :      threading; it will be removed once we preserve loop structures throughout
     877                 :             :      the compilation -- we will be able to mark the affected loops directly in
     878                 :             :      jump threading, and avoid peeling them next time.  */
     879                 :             :   bool may_peel_loop_headers_p;
     880                 :             : }; // class pass_dominator
     881                 :             : 
     882                 :             : unsigned int
     883                 :     1944572 : pass_dominator::execute (function *fun)
     884                 :             : {
     885                 :     1944572 :   memset (&opt_stats, 0, sizeof (opt_stats));
     886                 :             : 
     887                 :             :   /* Create our hash tables.  */
     888                 :     1944572 :   hash_table<expr_elt_hasher> *avail_exprs
     889                 :     1944572 :     = new hash_table<expr_elt_hasher> (1024);
     890                 :     1944572 :   class avail_exprs_stack *avail_exprs_stack
     891                 :     1944572 :     = new class avail_exprs_stack (avail_exprs);
     892                 :     1944572 :   class const_and_copies *const_and_copies = new class const_and_copies ();
     893                 :     1944572 :   need_eh_cleanup = BITMAP_ALLOC (NULL);
     894                 :     1944572 :   need_noreturn_fixup.create (0);
     895                 :             : 
     896                 :     1944572 :   calculate_dominance_info (CDI_DOMINATORS);
     897                 :     1944572 :   cfg_altered = false;
     898                 :             : 
     899                 :             :   /* We need to know loop structures in order to avoid destroying them
     900                 :             :      in jump threading.  Note that we still can e.g. thread through loop
     901                 :             :      headers to an exit edge, or through loop header to the loop body, assuming
     902                 :             :      that we update the loop info.
     903                 :             : 
     904                 :             :      TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
     905                 :             :      to several overly conservative bail-outs in jump threading, case
     906                 :             :      gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
     907                 :             :      missing.  We should improve jump threading in future then
     908                 :             :      LOOPS_HAVE_PREHEADERS won't be needed here.  */
     909                 :     1944572 :   loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES
     910                 :             :                        | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
     911                 :             : 
     912                 :             :   /* We need accurate information regarding back edges in the CFG
     913                 :             :      for jump threading; this may include back edges that are not part of
     914                 :             :      a single loop.  */
     915                 :     1944572 :   mark_dfs_back_edges ();
     916                 :             : 
     917                 :             :   /* We want to create the edge info structures before the dominator walk
     918                 :             :      so that they'll be in place for the jump threader, particularly when
     919                 :             :      threading through a join block.
     920                 :             : 
     921                 :             :      The conditions will be lazily updated with global equivalences as
     922                 :             :      we reach them during the dominator walk.  */
     923                 :     1944572 :   basic_block bb;
     924                 :    20978207 :   FOR_EACH_BB_FN (bb, fun)
     925                 :    19033635 :     record_edge_info (bb);
     926                 :             : 
     927                 :             :   /* Recursively walk the dominator tree optimizing statements.  */
     928                 :     1944572 :   gimple_ranger *ranger = enable_ranger (fun);
     929                 :     1944572 :   path_range_query path_query (*ranger);
     930                 :     1944572 :   dom_jt_simplifier simplifier (avail_exprs_stack, ranger, &path_query);
     931                 :     1944572 :   dom_jt_state state (const_and_copies, avail_exprs_stack);
     932                 :     1944572 :   jump_threader threader (&simplifier, &state);
     933                 :     1944572 :   dom_opt_dom_walker walker (CDI_DOMINATORS,
     934                 :             :                              &threader,
     935                 :             :                              &state,
     936                 :             :                              ranger,
     937                 :             :                              const_and_copies,
     938                 :     1944572 :                              avail_exprs_stack);
     939                 :     1944572 :   walker.walk (fun->cfg->x_entry_block_ptr);
     940                 :             : 
     941                 :     1944572 :   ranger->export_global_ranges ();
     942                 :     1944572 :   disable_ranger (fun);
     943                 :             : 
     944                 :             :   /* Look for blocks where we cleared EDGE_EXECUTABLE on an outgoing
     945                 :             :      edge.  When found, remove jump threads which contain any outgoing
     946                 :             :      edge from the affected block.  */
     947                 :     1944572 :   if (cfg_altered)
     948                 :             :     {
     949                 :     2218128 :       FOR_EACH_BB_FN (bb, fun)
     950                 :             :         {
     951                 :     2188361 :           edge_iterator ei;
     952                 :     2188361 :           edge e;
     953                 :             : 
     954                 :             :           /* First see if there are any edges without EDGE_EXECUTABLE
     955                 :             :              set.  */
     956                 :     2188361 :           bool found = false;
     957                 :     5290530 :           FOR_EACH_EDGE (e, ei, bb->succs)
     958                 :             :             {
     959                 :     3200500 :               if ((e->flags & EDGE_EXECUTABLE) == 0)
     960                 :             :                 {
     961                 :             :                   found = true;
     962                 :             :                   break;
     963                 :             :                 }
     964                 :             :             }
     965                 :             : 
     966                 :             :           /* If there were any such edges found, then remove jump threads
     967                 :             :              containing any edge leaving BB.  */
     968                 :     2188361 :           if (found)
     969                 :      281553 :             FOR_EACH_EDGE (e, ei, bb->succs)
     970                 :      183222 :               threader.remove_jump_threads_including (e);
     971                 :             :         }
     972                 :             :     }
     973                 :             : 
     974                 :     1944572 :   {
     975                 :     1944572 :     gimple_stmt_iterator gsi;
     976                 :     1944572 :     basic_block bb;
     977                 :    20978207 :     FOR_EACH_BB_FN (bb, fun)
     978                 :             :       {
     979                 :   175084953 :         for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     980                 :   137017683 :           update_stmt_if_modified (gsi_stmt (gsi));
     981                 :             :       }
     982                 :             :   }
     983                 :             : 
     984                 :             :   /* If we exposed any new variables, go ahead and put them into
     985                 :             :      SSA form now, before we handle jump threading.  This simplifies
     986                 :             :      interactions between rewriting of _DECL nodes into SSA form
     987                 :             :      and rewriting SSA_NAME nodes into SSA form after block
     988                 :             :      duplication and CFG manipulation.  */
     989                 :     1944572 :   update_ssa (TODO_update_ssa);
     990                 :             : 
     991                 :     1944572 :   free_all_edge_infos ();
     992                 :             : 
     993                 :             :   /* Thread jumps, creating duplicate blocks as needed.  */
     994                 :     1944572 :   cfg_altered |= threader.thread_through_all_blocks (may_peel_loop_headers_p);
     995                 :             : 
     996                 :     1944572 :   if (cfg_altered)
     997                 :       97390 :     free_dominance_info (CDI_DOMINATORS);
     998                 :             : 
     999                 :             :   /* Removal of statements may make some EH edges dead.  Purge
    1000                 :             :      such edges from the CFG as needed.  */
    1001                 :     1944572 :   if (!bitmap_empty_p (need_eh_cleanup))
    1002                 :             :     {
    1003                 :         779 :       unsigned i;
    1004                 :         779 :       bitmap_iterator bi;
    1005                 :             : 
    1006                 :             :       /* Jump threading may have created forwarder blocks from blocks
    1007                 :             :          needing EH cleanup; the new successor of these blocks, which
    1008                 :             :          has inherited from the original block, needs the cleanup.
    1009                 :             :          Don't clear bits in the bitmap, as that can break the bitmap
    1010                 :             :          iterator.  */
    1011                 :        2347 :       EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
    1012                 :             :         {
    1013                 :        1568 :           basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
    1014                 :        1568 :           if (bb == NULL)
    1015                 :           0 :             continue;
    1016                 :        3170 :           while (single_succ_p (bb)
    1017                 :        1619 :                  && (single_succ_edge (bb)->flags
    1018                 :          52 :                      & (EDGE_EH|EDGE_DFS_BACK)) == 0)
    1019                 :          51 :             bb = single_succ (bb);
    1020                 :        1568 :           if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
    1021                 :          17 :             continue;
    1022                 :        1551 :           if ((unsigned) bb->index != i)
    1023                 :          27 :             bitmap_set_bit (need_eh_cleanup, bb->index);
    1024                 :             :         }
    1025                 :             : 
    1026                 :         779 :       gimple_purge_all_dead_eh_edges (need_eh_cleanup);
    1027                 :         779 :       bitmap_clear (need_eh_cleanup);
    1028                 :             :     }
    1029                 :             : 
    1030                 :             :   /* Fixup stmts that became noreturn calls.  This may require splitting
    1031                 :             :      blocks and thus isn't possible during the dominator walk or before
    1032                 :             :      jump threading finished.  Do this in reverse order so we don't
    1033                 :             :      inadvertedly remove a stmt we want to fixup by visiting a dominating
    1034                 :             :      now noreturn call first.  */
    1035                 :     1944579 :   while (!need_noreturn_fixup.is_empty ())
    1036                 :             :     {
    1037                 :           7 :       gimple *stmt = need_noreturn_fixup.pop ();
    1038                 :           7 :       if (dump_file && dump_flags & TDF_DETAILS)
    1039                 :             :         {
    1040                 :           0 :           fprintf (dump_file, "Fixing up noreturn call ");
    1041                 :           0 :           print_gimple_stmt (dump_file, stmt, 0);
    1042                 :           0 :           fprintf (dump_file, "\n");
    1043                 :             :         }
    1044                 :           7 :       fixup_noreturn_call (stmt);
    1045                 :             :     }
    1046                 :             : 
    1047                 :     1944572 :   statistics_counter_event (fun, "Redundant expressions eliminated",
    1048                 :     1944572 :                             opt_stats.num_re);
    1049                 :     1944572 :   statistics_counter_event (fun, "Constants propagated",
    1050                 :     1944572 :                             opt_stats.num_const_prop);
    1051                 :     1944572 :   statistics_counter_event (fun, "Copies propagated",
    1052                 :     1944572 :                             opt_stats.num_copy_prop);
    1053                 :             : 
    1054                 :             :   /* Debugging dumps.  */
    1055                 :     1944572 :   if (dump_file && (dump_flags & TDF_STATS))
    1056                 :          35 :     dump_dominator_optimization_stats (dump_file, avail_exprs);
    1057                 :             : 
    1058                 :     1944572 :   loop_optimizer_finalize ();
    1059                 :             : 
    1060                 :             :   /* Delete our main hashtable.  */
    1061                 :     1944572 :   delete avail_exprs;
    1062                 :     1944572 :   avail_exprs = NULL;
    1063                 :             : 
    1064                 :             :   /* Free asserted bitmaps and stacks.  */
    1065                 :     1944572 :   BITMAP_FREE (need_eh_cleanup);
    1066                 :     1944572 :   need_noreturn_fixup.release ();
    1067                 :     1944572 :   delete avail_exprs_stack;
    1068                 :     1944572 :   delete const_and_copies;
    1069                 :             : 
    1070                 :     1944572 :   return 0;
    1071                 :     1944572 : }
    1072                 :             : 
    1073                 :             : } // anon namespace
    1074                 :             : 
    1075                 :             : gimple_opt_pass *
    1076                 :      280455 : make_pass_dominator (gcc::context *ctxt)
    1077                 :             : {
    1078                 :      280455 :   return new pass_dominator (ctxt);
    1079                 :             : }
    1080                 :             : 
    1081                 :             : /* Valueize hook for gimple_fold_stmt_to_constant_1.  */
    1082                 :             : 
    1083                 :             : static tree
    1084                 :    30378310 : dom_valueize (tree t)
    1085                 :             : {
    1086                 :    30378310 :   if (TREE_CODE (t) == SSA_NAME)
    1087                 :             :     {
    1088                 :    21646946 :       tree tem = SSA_NAME_VALUE (t);
    1089                 :    16934168 :       if (tem)
    1090                 :     2653205 :         return tem;
    1091                 :             :     }
    1092                 :             :   return t;
    1093                 :             : }
    1094                 :             : 
    1095                 :             : /* We have just found an equivalence for LHS on an edge E.
    1096                 :             :    Look backwards to other uses of LHS and see if we can derive
    1097                 :             :    additional equivalences that are valid on edge E.  */
    1098                 :             : static void
    1099                 :    10688098 : back_propagate_equivalences (tree lhs, edge e,
    1100                 :             :                              class const_and_copies *const_and_copies,
    1101                 :             :                              bitmap domby)
    1102                 :             : {
    1103                 :    10688098 :   use_operand_p use_p;
    1104                 :    10688098 :   imm_use_iterator iter;
    1105                 :    10688098 :   basic_block dest = e->dest;
    1106                 :    10688098 :   bool domok = (dom_info_state (CDI_DOMINATORS) == DOM_OK);
    1107                 :             : 
    1108                 :             :   /* Iterate over the uses of LHS to see if any dominate E->dest.
    1109                 :             :      If so, they may create useful equivalences too.
    1110                 :             : 
    1111                 :             :      ???  If the code gets re-organized to a worklist to catch more
    1112                 :             :      indirect opportunities and it is made to handle PHIs then this
    1113                 :             :      should only consider use_stmts in basic-blocks we have already visited.  */
    1114                 :    42788135 :   FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
    1115                 :             :     {
    1116                 :    32100037 :       gimple *use_stmt = USE_STMT (use_p);
    1117                 :             : 
    1118                 :             :       /* Often the use is in DEST, which we trivially know we can't use.
    1119                 :             :          This is cheaper than the dominator set tests below.  */
    1120                 :    32100037 :       if (dest == gimple_bb (use_stmt))
    1121                 :      345226 :         continue;
    1122                 :             : 
    1123                 :             :       /* Filter out statements that can never produce a useful
    1124                 :             :          equivalence.  */
    1125                 :    31754811 :       tree lhs2 = gimple_get_lhs (use_stmt);
    1126                 :    31754811 :       if (!lhs2 || TREE_CODE (lhs2) != SSA_NAME)
    1127                 :    23321746 :         continue;
    1128                 :             : 
    1129                 :     8433065 :       if (domok)
    1130                 :             :         {
    1131                 :     5249028 :           if (!dominated_by_p (CDI_DOMINATORS, dest, gimple_bb (use_stmt)))
    1132                 :     2668683 :             continue;
    1133                 :             :         }
    1134                 :             :       else
    1135                 :             :         {
    1136                 :             :           /* We can use the set of BBs on the stack from a domwalk
    1137                 :             :              for a medium fast way to query dominance.  Profiling
    1138                 :             :              has shown non-fast query dominance tests here can be fairly
    1139                 :             :              expensive.  */
    1140                 :             :           /* This tests if USE_STMT does not dominate DEST.  */
    1141                 :     3184037 :           if (!bitmap_bit_p (domby, gimple_bb (use_stmt)->index))
    1142                 :     2346734 :             continue;
    1143                 :             :         }
    1144                 :             : 
    1145                 :             :       /* At this point USE_STMT dominates DEST and may result in a
    1146                 :             :          useful equivalence.  Try to simplify its RHS to a constant
    1147                 :             :          or SSA_NAME.  */
    1148                 :     3417648 :       tree res = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
    1149                 :             :                                                  no_follow_ssa_edges);
    1150                 :     3417648 :       if (res && (TREE_CODE (res) == SSA_NAME || is_gimple_min_invariant (res)))
    1151                 :     2049051 :         record_equality (lhs2, res, const_and_copies);
    1152                 :             :     }
    1153                 :    10688098 : }
    1154                 :             : 
    1155                 :             : /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
    1156                 :             :    by traversing edge E (which are cached in E->aux).
    1157                 :             : 
    1158                 :             :    Callers are responsible for managing the unwinding markers.  */
    1159                 :             : static void
    1160                 :    29257222 : record_temporary_equivalences (edge e,
    1161                 :             :                                class const_and_copies *const_and_copies,
    1162                 :             :                                class avail_exprs_stack *avail_exprs_stack,
    1163                 :             :                                bitmap blocks_on_stack)
    1164                 :             : {
    1165                 :    29257222 :   int i;
    1166                 :    29257222 :   class edge_info *edge_info = (class edge_info *) e->aux;
    1167                 :             : 
    1168                 :             :   /* If we have info associated with this edge, record it into
    1169                 :             :      our equivalence tables.  */
    1170                 :    29257222 :   if (edge_info)
    1171                 :             :     {
    1172                 :             :       cond_equivalence *eq;
    1173                 :             :       /* If we have 0 = COND or 1 = COND equivalences, record them
    1174                 :             :          into our expression hash tables.  */
    1175                 :    77836498 :       for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
    1176                 :    57418177 :         avail_exprs_stack->record_cond (eq);
    1177                 :             : 
    1178                 :             :       edge_info::equiv_pair *seq;
    1179                 :    31106419 :       for (i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
    1180                 :             :         {
    1181                 :    10688098 :           tree lhs = seq->first;
    1182                 :    10688098 :           if (!lhs || TREE_CODE (lhs) != SSA_NAME)
    1183                 :           0 :             continue;
    1184                 :             : 
    1185                 :             :           /* Record the simple NAME = VALUE equivalence.  */
    1186                 :    10688098 :           tree rhs = seq->second;
    1187                 :             : 
    1188                 :             :           /* If this is a SSA_NAME = SSA_NAME equivalence and one operand is
    1189                 :             :              cheaper to compute than the other, then set up the equivalence
    1190                 :             :              such that we replace the expensive one with the cheap one.
    1191                 :             : 
    1192                 :             :              If they are the same cost to compute, then do not record
    1193                 :             :              anything.  */
    1194                 :    10688098 :           if (TREE_CODE (lhs) == SSA_NAME && TREE_CODE (rhs) == SSA_NAME)
    1195                 :             :             {
    1196                 :     1362876 :               gimple *rhs_def = SSA_NAME_DEF_STMT (rhs);
    1197                 :     1362876 :               int rhs_cost = estimate_num_insns (rhs_def, &eni_size_weights);
    1198                 :             : 
    1199                 :     1362876 :               gimple *lhs_def = SSA_NAME_DEF_STMT (lhs);
    1200                 :     1362876 :               int lhs_cost = estimate_num_insns (lhs_def, &eni_size_weights);
    1201                 :             : 
    1202                 :     1362876 :               if (rhs_cost > lhs_cost)
    1203                 :      146038 :                 record_equality (rhs, lhs, const_and_copies);
    1204                 :     1216838 :               else if (rhs_cost < lhs_cost)
    1205                 :      340462 :                 record_equality (lhs, rhs, const_and_copies);
    1206                 :             :             }
    1207                 :             :           else
    1208                 :     9325222 :             record_equality (lhs, rhs, const_and_copies);
    1209                 :             : 
    1210                 :             : 
    1211                 :             :           /* Any equivalence found for LHS may result in additional
    1212                 :             :              equivalences for other uses of LHS that we have already
    1213                 :             :              processed.  */
    1214                 :    10688098 :           back_propagate_equivalences (lhs, e, const_and_copies,
    1215                 :             :                                        blocks_on_stack);
    1216                 :             :         }
    1217                 :             :     }
    1218                 :    29257222 : }
    1219                 :             : 
    1220                 :             : /* PHI nodes can create equivalences too.
    1221                 :             : 
    1222                 :             :    Ignoring any alternatives which are the same as the result, if
    1223                 :             :    all the alternatives are equal, then the PHI node creates an
    1224                 :             :    equivalence.  */
    1225                 :             : 
    1226                 :             : static void
    1227                 :    20940373 : record_equivalences_from_phis (basic_block bb)
    1228                 :             : {
    1229                 :    20940373 :   gphi_iterator gsi;
    1230                 :             : 
    1231                 :    29070246 :   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
    1232                 :             :     {
    1233                 :     8129873 :       gphi *phi = gsi.phi ();
    1234                 :             : 
    1235                 :             :       /* We might eliminate the PHI, so advance GSI now.  */
    1236                 :     8129873 :       gsi_next (&gsi);
    1237                 :             : 
    1238                 :     8129873 :       tree lhs = gimple_phi_result (phi);
    1239                 :     8129873 :       tree rhs = NULL;
    1240                 :     8129873 :       size_t i;
    1241                 :             : 
    1242                 :    14789960 :       for (i = 0; i < gimple_phi_num_args (phi); i++)
    1243                 :             :         {
    1244                 :    13912030 :           tree t = gimple_phi_arg_def (phi, i);
    1245                 :             : 
    1246                 :             :           /* Ignore alternatives which are the same as our LHS.  Since
    1247                 :             :              LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
    1248                 :             :              can simply compare pointers.  */
    1249                 :    13912030 :           if (lhs == t)
    1250                 :       21530 :             continue;
    1251                 :             : 
    1252                 :             :           /* If the associated edge is not marked as executable, then it
    1253                 :             :              can be ignored.  */
    1254                 :    13890500 :           if ((gimple_phi_arg_edge (phi, i)->flags & EDGE_EXECUTABLE) == 0)
    1255                 :       78538 :             continue;
    1256                 :             : 
    1257                 :    13811962 :           t = dom_valueize (t);
    1258                 :             : 
    1259                 :             :           /* If T is an SSA_NAME and its associated edge is a backedge,
    1260                 :             :              then quit as we cannot utilize this equivalence.  */
    1261                 :    13811962 :           if (TREE_CODE (t) == SSA_NAME
    1262                 :    13811962 :               && (gimple_phi_arg_edge (phi, i)->flags & EDGE_DFS_BACK))
    1263                 :             :             break;
    1264                 :             : 
    1265                 :             :           /* If we have not processed an alternative yet, then set
    1266                 :             :              RHS to this alternative.  */
    1267                 :    11261225 :           if (rhs == NULL)
    1268                 :             :             rhs = t;
    1269                 :             :           /* If we have processed an alternative (stored in RHS), then
    1270                 :             :              see if it is equal to this one.  If it isn't, then stop
    1271                 :             :              the search.  */
    1272                 :     5212047 :           else if (! operand_equal_for_phi_arg_p (rhs, t))
    1273                 :             :             break;
    1274                 :             :         }
    1275                 :             : 
    1276                 :             :       /* If we had no interesting alternatives, then all the RHS alternatives
    1277                 :             :          must have been the same as LHS.  */
    1278                 :     8129873 :       if (!rhs)
    1279                 :     2080695 :         rhs = lhs;
    1280                 :             : 
    1281                 :             :       /* If we managed to iterate through each PHI alternative without
    1282                 :             :          breaking out of the loop, then we have a PHI which may create
    1283                 :             :          a useful equivalence.  We do not need to record unwind data for
    1284                 :             :          this, since this is a true assignment and not an equivalence
    1285                 :             :          inferred from a comparison.  All uses of this ssa name are dominated
    1286                 :             :          by this assignment, so unwinding just costs time and space.  */
    1287                 :     8129873 :       if (i == gimple_phi_num_args (phi))
    1288                 :             :         {
    1289                 :      877930 :           if (may_propagate_copy (lhs, rhs))
    1290                 :      475536 :             set_ssa_name_value (lhs, rhs);
    1291                 :      804788 :           else if (virtual_operand_p (lhs))
    1292                 :             :             {
    1293                 :      402157 :               gimple *use_stmt;
    1294                 :      402157 :               imm_use_iterator iter;
    1295                 :      402157 :               use_operand_p use_p;
    1296                 :             :               /* For virtual operands we have to propagate into all uses as
    1297                 :             :                  otherwise we will create overlapping life-ranges.  */
    1298                 :     1110283 :               FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    1299                 :     2153312 :                 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
    1300                 :     1124750 :                   SET_USE (use_p, rhs);
    1301                 :      402157 :               if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
    1302                 :          68 :                 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
    1303                 :      402157 :               gimple_stmt_iterator tmp_gsi = gsi_for_stmt (phi);
    1304                 :      402157 :               remove_phi_node (&tmp_gsi, true);
    1305                 :             :             }
    1306                 :             :         }
    1307                 :             :     }
    1308                 :    20940373 : }
    1309                 :             : 
    1310                 :             : /* Return true if all uses of NAME are dominated by STMT or feed STMT
    1311                 :             :    via a chain of single immediate uses.  */
    1312                 :             : 
    1313                 :             : static bool
    1314                 :       43642 : all_uses_feed_or_dominated_by_stmt (tree name, gimple *stmt)
    1315                 :             : {
    1316                 :       43642 :   use_operand_p use_p, use2_p;
    1317                 :       43642 :   imm_use_iterator iter;
    1318                 :       43642 :   basic_block stmt_bb = gimple_bb (stmt);
    1319                 :             : 
    1320                 :      105058 :   FOR_EACH_IMM_USE_FAST (use_p, iter, name)
    1321                 :             :     {
    1322                 :       84004 :       gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
    1323                 :      144805 :       if (use_stmt == stmt
    1324                 :       63981 :           || is_gimple_debug (use_stmt)
    1325                 :      129481 :           || (gimple_bb (use_stmt) != stmt_bb
    1326                 :       38983 :               && dominated_by_p (CDI_DOMINATORS,
    1327                 :       38983 :                                  gimple_bb (use_stmt), stmt_bb)))
    1328                 :       60801 :         continue;
    1329                 :             :       while (use_stmt != stmt
    1330                 :       34709 :              && is_gimple_assign (use_stmt)
    1331                 :       27126 :              && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
    1332                 :       61112 :              && single_imm_use (gimple_assign_lhs (use_stmt),
    1333                 :             :                                 &use2_p, &use_stmt2))
    1334                 :       12121 :         use_stmt = use_stmt2;
    1335                 :       23203 :       if (use_stmt != stmt)
    1336                 :       22588 :         return false;
    1337                 :             :     }
    1338                 :             :   return true;
    1339                 :             : }
    1340                 :             : 
    1341                 :             : /* Handle
    1342                 :             :    _4 = x_3 & 31;
    1343                 :             :    if (_4 != 0)
    1344                 :             :      goto <bb 6>;
    1345                 :             :    else
    1346                 :             :      goto <bb 7>;
    1347                 :             :    <bb 6>:
    1348                 :             :    __builtin_unreachable ();
    1349                 :             :    <bb 7>:
    1350                 :             : 
    1351                 :             :    If x_3 has no other immediate uses (checked by caller), var is the
    1352                 :             :    x_3 var, we can clear low 5 bits from the non-zero bitmask.  */
    1353                 :             : 
    1354                 :             : static void
    1355                 :       18076 : maybe_set_nonzero_bits (edge e, tree var)
    1356                 :             : {
    1357                 :       18076 :   basic_block cond_bb = e->src;
    1358                 :       36152 :   gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (cond_bb));
    1359                 :       18076 :   tree cst;
    1360                 :             : 
    1361                 :       18076 :   if (cond == NULL
    1362                 :       18076 :       || gimple_cond_code (cond) != ((e->flags & EDGE_TRUE_VALUE)
    1363                 :       18076 :                                      ? EQ_EXPR : NE_EXPR)
    1364                 :        3858 :       || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
    1365                 :        1477 :       || !integer_zerop (gimple_cond_rhs (cond)))
    1366                 :       16693 :     return;
    1367                 :             : 
    1368                 :        1383 :   gimple *stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond));
    1369                 :        1383 :   if (!is_gimple_assign (stmt)
    1370                 :        1361 :       || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
    1371                 :        1412 :       || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
    1372                 :             :     return;
    1373                 :           1 :   if (gimple_assign_rhs1 (stmt) != var)
    1374                 :             :     {
    1375                 :           1 :       gimple *stmt2;
    1376                 :             : 
    1377                 :           1 :       if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
    1378                 :             :         return;
    1379                 :           1 :       stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
    1380                 :           1 :       if (!gimple_assign_cast_p (stmt2)
    1381                 :           0 :           || gimple_assign_rhs1 (stmt2) != var
    1382                 :           0 :           || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
    1383                 :           1 :           || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
    1384                 :           0 :                               != TYPE_PRECISION (TREE_TYPE (var))))
    1385                 :             :         return;
    1386                 :             :     }
    1387                 :           0 :   cst = gimple_assign_rhs2 (stmt);
    1388                 :           0 :   if (POINTER_TYPE_P (TREE_TYPE (var)))
    1389                 :             :     {
    1390                 :           0 :       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (var);
    1391                 :           0 :       if (pi && pi->misalign)
    1392                 :           0 :         return;
    1393                 :           0 :       wide_int w = wi::bit_not (wi::to_wide (cst));
    1394                 :           0 :       unsigned int bits = wi::ctz (w);
    1395                 :           0 :       if (bits == 0 || bits >= HOST_BITS_PER_INT)
    1396                 :           0 :         return;
    1397                 :           0 :       unsigned int align = 1U << bits;
    1398                 :           0 :       if (pi == NULL || pi->align < align)
    1399                 :           0 :         set_ptr_info_alignment (get_ptr_info (var), align, 0);
    1400                 :           0 :     }
    1401                 :             :   else
    1402                 :           0 :     set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
    1403                 :           0 :                                             wi::to_wide (cst)));
    1404                 :             : }
    1405                 :             : 
    1406                 :             : /* Set global ranges that can be determined from the C->M edge:
    1407                 :             : 
    1408                 :             :    <bb C>:
    1409                 :             :    ...
    1410                 :             :    if (something)
    1411                 :             :      goto <bb N>;
    1412                 :             :    else
    1413                 :             :      goto <bb M>;
    1414                 :             :    <bb N>:
    1415                 :             :    __builtin_unreachable ();
    1416                 :             :    <bb M>:
    1417                 :             : */
    1418                 :             : 
    1419                 :             : void
    1420                 :    20940373 : dom_opt_dom_walker::set_global_ranges_from_unreachable_edges (basic_block bb)
    1421                 :             : {
    1422                 :    20940373 :   edge pred_e = single_pred_edge_ignoring_loop_edges (bb, false);
    1423                 :    20940373 :   if (!pred_e)
    1424                 :             :     return;
    1425                 :             : 
    1426                 :    15424177 :   gimple *stmt = *gsi_last_bb (pred_e->src);
    1427                 :    15424177 :   if (!stmt
    1428                 :    12859488 :       || gimple_code (stmt) != GIMPLE_COND
    1429                 :    25852121 :       || !assert_unreachable_fallthru_edge_p (pred_e))
    1430                 :    15396241 :     return;
    1431                 :             : 
    1432                 :       27936 :   tree name;
    1433                 :       27936 :   gori_compute &gori = m_ranger->gori ();
    1434                 :       71578 :   FOR_EACH_GORI_EXPORT_NAME (gori, pred_e->src, name)
    1435                 :       43642 :     if (all_uses_feed_or_dominated_by_stmt (name, stmt)
    1436                 :             :         // The condition must post-dominate the definition point.
    1437                 :       64696 :         && (SSA_NAME_IS_DEFAULT_DEF (name)
    1438                 :       20624 :             || (gimple_bb (SSA_NAME_DEF_STMT (name))
    1439                 :       20624 :                 == pred_e->src)))
    1440                 :             :       {
    1441                 :       20519 :         Value_Range r (TREE_TYPE (name));
    1442                 :             : 
    1443                 :       20519 :         if (m_ranger->range_on_edge (r, pred_e, name)
    1444                 :       20519 :             && !r.varying_p ()
    1445                 :       38625 :             && !r.undefined_p ())
    1446                 :             :           {
    1447                 :       18076 :             set_range_info (name, r);
    1448                 :       18076 :             maybe_set_nonzero_bits (pred_e, name);
    1449                 :             :           }
    1450                 :       20519 :       }
    1451                 :             : }
    1452                 :             : 
    1453                 :             : /* Record any equivalences created by the incoming edge to BB into
    1454                 :             :    CONST_AND_COPIES and AVAIL_EXPRS_STACK.  If BB has more than one
    1455                 :             :    incoming edge, then no equivalence is created.  */
    1456                 :             : 
    1457                 :             : static void
    1458                 :    20940373 : record_equivalences_from_incoming_edge (basic_block bb,
    1459                 :             :     class const_and_copies *const_and_copies,
    1460                 :             :     class avail_exprs_stack *avail_exprs_stack,
    1461                 :             :     bitmap blocks_on_stack)
    1462                 :             : {
    1463                 :    20940373 :   edge e;
    1464                 :    20940373 :   basic_block parent;
    1465                 :             : 
    1466                 :             :   /* If our parent block ended with a control statement, then we may be
    1467                 :             :      able to record some equivalences based on which outgoing edge from
    1468                 :             :      the parent was followed.  */
    1469                 :    20940373 :   parent = get_immediate_dominator (CDI_DOMINATORS, bb);
    1470                 :             : 
    1471                 :    20940373 :   e = single_pred_edge_ignoring_loop_edges (bb, true);
    1472                 :             : 
    1473                 :             :   /* If we had a single incoming edge from our parent block, then enter
    1474                 :             :      any data associated with the edge into our tables.  */
    1475                 :    20940373 :   if (e && e->src == parent)
    1476                 :    15426975 :     record_temporary_equivalences (e, const_and_copies, avail_exprs_stack,
    1477                 :             :                                    blocks_on_stack);
    1478                 :    20940373 : }
    1479                 :             : 
    1480                 :             : /* Dump statistics for the hash table HTAB.  */
    1481                 :             : 
    1482                 :             : static void
    1483                 :          35 : htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
    1484                 :             : {
    1485                 :          66 :   fprintf (file, "size " HOST_SIZE_T_PRINT_DEC ", " HOST_SIZE_T_PRINT_DEC
    1486                 :             :            " elements, %f collision/search ratio\n",
    1487                 :          35 :            (fmt_size_t) htab.size (),
    1488                 :          35 :            (fmt_size_t) htab.elements (),
    1489                 :             :            htab.collisions ());
    1490                 :          35 : }
    1491                 :             : 
    1492                 :             : /* Dump SSA statistics on FILE.  */
    1493                 :             : 
    1494                 :             : static void
    1495                 :          35 : dump_dominator_optimization_stats (FILE *file,
    1496                 :             :                                    hash_table<expr_elt_hasher> *avail_exprs)
    1497                 :             : {
    1498                 :          35 :   fprintf (file, "Total number of statements:                   %6ld\n\n",
    1499                 :             :            opt_stats.num_stmts);
    1500                 :          35 :   fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
    1501                 :             :            opt_stats.num_exprs_considered);
    1502                 :             : 
    1503                 :          35 :   fprintf (file, "\nHash table statistics:\n");
    1504                 :             : 
    1505                 :          35 :   fprintf (file, "    avail_exprs: ");
    1506                 :          35 :   htab_statistics (file, *avail_exprs);
    1507                 :          35 : }
    1508                 :             : 
    1509                 :             : 
    1510                 :             : /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
    1511                 :             :    This constrains the cases in which we may treat this as assignment.  */
    1512                 :             : 
    1513                 :             : static void
    1514                 :    11860773 : record_equality (tree x, tree y, class const_and_copies *const_and_copies)
    1515                 :             : {
    1516                 :    11860773 :   tree prev_x = NULL, prev_y = NULL;
    1517                 :             : 
    1518                 :    11860773 :   if (tree_swap_operands_p (x, y))
    1519                 :      745238 :     std::swap (x, y);
    1520                 :             : 
    1521                 :             :   /* Most of the time tree_swap_operands_p does what we want.  But there
    1522                 :             :      are cases where we know one operand is better for copy propagation than
    1523                 :             :      the other.  Given no other code cares about ordering of equality
    1524                 :             :      comparison operators for that purpose, we just handle the special cases
    1525                 :             :      here.  */
    1526                 :    11860773 :   if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
    1527                 :             :     {
    1528                 :             :       /* If one operand is a single use operand, then make it
    1529                 :             :          X.  This will preserve its single use properly and if this
    1530                 :             :          conditional is eliminated, the computation of X can be
    1531                 :             :          eliminated as well.  */
    1532                 :      894854 :       if (has_single_use (y) && ! has_single_use (x))
    1533                 :             :         std::swap (x, y);
    1534                 :             :     }
    1535                 :    11860773 :   if (TREE_CODE (x) == SSA_NAME)
    1536                 :    11636840 :     prev_x = SSA_NAME_VALUE (x);
    1537                 :    11860773 :   if (TREE_CODE (y) == SSA_NAME)
    1538                 :     1118787 :     prev_y = SSA_NAME_VALUE (y);
    1539                 :             : 
    1540                 :             :   /* If one of the previous values is invariant, or invariant in more loops
    1541                 :             :      (by depth), then use that.
    1542                 :             :      Otherwise it doesn't matter which value we choose, just so
    1543                 :             :      long as we canonicalize on one value.  */
    1544                 :    11860773 :   if (is_gimple_min_invariant (y))
    1545                 :             :     ;
    1546                 :     1118787 :   else if (is_gimple_min_invariant (x))
    1547                 :             :     prev_x = x, x = y, y = prev_x, prev_x = prev_y;
    1548                 :      894854 :   else if (prev_x && is_gimple_min_invariant (prev_x))
    1549                 :             :     x = y, y = prev_x, prev_x = prev_y;
    1550                 :      813624 :   else if (prev_y)
    1551                 :    11860773 :     y = prev_y;
    1552                 :             : 
    1553                 :             :   /* After the swapping, we must have one SSA_NAME.  */
    1554                 :    11860773 :   if (TREE_CODE (x) != SSA_NAME)
    1555                 :             :     return;
    1556                 :             : 
    1557                 :             :   /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
    1558                 :             :      variable compared against zero.  If we're honoring signed zeros,
    1559                 :             :      then we cannot record this value unless we know that the value is
    1560                 :             :      nonzero.  */
    1561                 :    11860773 :   if (HONOR_SIGNED_ZEROS (x)
    1562                 :    11860773 :       && (TREE_CODE (y) != REAL_CST
    1563                 :      167805 :           || real_equal (&dconst0, &TREE_REAL_CST (y))))
    1564                 :         498 :     return;
    1565                 :             : 
    1566                 :    11860275 :   const_and_copies->record_const_or_copy (x, y, prev_x);
    1567                 :             : }
    1568                 :             : 
    1569                 :             : /* Returns true when STMT is a simple iv increment.  It detects the
    1570                 :             :    following situation:
    1571                 :             : 
    1572                 :             :    i_1 = phi (..., i_k)
    1573                 :             :    [...]
    1574                 :             :    i_j = i_{j-1}  for each j : 2 <= j <= k-1
    1575                 :             :    [...]
    1576                 :             :    i_k = i_{k-1} +/- ...  */
    1577                 :             : 
    1578                 :             : bool
    1579                 :    38017278 : simple_iv_increment_p (gimple *stmt)
    1580                 :             : {
    1581                 :    38017278 :   enum tree_code code;
    1582                 :    38017278 :   tree lhs, preinc;
    1583                 :    38017278 :   gimple *phi;
    1584                 :    38017278 :   size_t i;
    1585                 :             : 
    1586                 :    38017278 :   if (gimple_code (stmt) != GIMPLE_ASSIGN)
    1587                 :             :     return false;
    1588                 :             : 
    1589                 :    29168420 :   lhs = gimple_assign_lhs (stmt);
    1590                 :    29168420 :   if (TREE_CODE (lhs) != SSA_NAME)
    1591                 :             :     return false;
    1592                 :             : 
    1593                 :    29168420 :   code = gimple_assign_rhs_code (stmt);
    1594                 :    29168420 :   if (code != PLUS_EXPR
    1595                 :    29168420 :       && code != MINUS_EXPR
    1596                 :    29168420 :       && code != POINTER_PLUS_EXPR)
    1597                 :             :     return false;
    1598                 :             : 
    1599                 :     6750017 :   preinc = gimple_assign_rhs1 (stmt);
    1600                 :     6750017 :   if (TREE_CODE (preinc) != SSA_NAME)
    1601                 :             :     return false;
    1602                 :             : 
    1603                 :     6504297 :   phi = SSA_NAME_DEF_STMT (preinc);
    1604                 :     6521558 :   while (gimple_code (phi) != GIMPLE_PHI)
    1605                 :             :     {
    1606                 :             :       /* Follow trivial copies, but not the DEF used in a back edge,
    1607                 :             :          so that we don't prevent coalescing.  */
    1608                 :     4444156 :       if (!gimple_assign_ssa_name_copy_p (phi))
    1609                 :             :         return false;
    1610                 :       17261 :       preinc = gimple_assign_rhs1 (phi);
    1611                 :       17261 :       phi = SSA_NAME_DEF_STMT (preinc);
    1612                 :             :     }
    1613                 :             : 
    1614                 :     4061566 :   for (i = 0; i < gimple_phi_num_args (phi); i++)
    1615                 :     3287455 :     if (gimple_phi_arg_def (phi, i) == lhs)
    1616                 :             :       return true;
    1617                 :             : 
    1618                 :             :   return false;
    1619                 :             : }
    1620                 :             : 
    1621                 :             : /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
    1622                 :             :    successors of BB.  */
    1623                 :             : 
    1624                 :             : static void
    1625                 :    20940373 : cprop_into_successor_phis (basic_block bb,
    1626                 :             :                            class const_and_copies *const_and_copies)
    1627                 :             : {
    1628                 :    20940373 :   edge e;
    1629                 :    20940373 :   edge_iterator ei;
    1630                 :             : 
    1631                 :    49660774 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1632                 :             :     {
    1633                 :    28720401 :       int indx;
    1634                 :    28720401 :       gphi_iterator gsi;
    1635                 :             : 
    1636                 :             :       /* If this is an abnormal edge, then we do not want to copy propagate
    1637                 :             :          into the PHI alternative associated with this edge.  */
    1638                 :    28720401 :       if (e->flags & EDGE_ABNORMAL)
    1639                 :    16986076 :         continue;
    1640                 :             : 
    1641                 :    28707565 :       gsi = gsi_start_phis (e->dest);
    1642                 :    28707565 :       if (gsi_end_p (gsi))
    1643                 :    16973240 :         continue;
    1644                 :             : 
    1645                 :             :       /* We may have an equivalence associated with this edge.  While
    1646                 :             :          we cannot propagate it into non-dominated blocks, we can
    1647                 :             :          propagate them into PHIs in non-dominated blocks.  */
    1648                 :             : 
    1649                 :             :       /* Push the unwind marker so we can reset the const and copies
    1650                 :             :          table back to its original state after processing this edge.  */
    1651                 :    11734325 :       const_and_copies->push_marker ();
    1652                 :             : 
    1653                 :             :       /* Extract and record any simple NAME = VALUE equivalences.
    1654                 :             : 
    1655                 :             :          Don't bother with [01] = COND equivalences, they're not useful
    1656                 :             :          here.  */
    1657                 :    11734325 :       class edge_info *edge_info = (class edge_info *) e->aux;
    1658                 :             : 
    1659                 :    11734325 :       if (edge_info)
    1660                 :             :         {
    1661                 :             :           edge_info::equiv_pair *seq;
    1662                 :     7154098 :           for (int i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
    1663                 :             :             {
    1664                 :     2733975 :               tree lhs = seq->first;
    1665                 :     2733975 :               tree rhs = seq->second;
    1666                 :             : 
    1667                 :     2733975 :               if (lhs && TREE_CODE (lhs) == SSA_NAME)
    1668                 :     2733975 :                 const_and_copies->record_const_or_copy (lhs, rhs);
    1669                 :             :             }
    1670                 :             : 
    1671                 :             :         }
    1672                 :             : 
    1673                 :    11734325 :       indx = e->dest_idx;
    1674                 :    31498468 :       for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
    1675                 :             :         {
    1676                 :    19764143 :           tree new_val;
    1677                 :    19764143 :           use_operand_p orig_p;
    1678                 :    19764143 :           tree orig_val;
    1679                 :    19764143 :           gphi *phi = gsi.phi ();
    1680                 :             : 
    1681                 :             :           /* The alternative may be associated with a constant, so verify
    1682                 :             :              it is an SSA_NAME before doing anything with it.  */
    1683                 :    19764143 :           orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
    1684                 :    19764143 :           orig_val = get_use_from_ptr (orig_p);
    1685                 :    19764143 :           if (TREE_CODE (orig_val) != SSA_NAME)
    1686                 :     2736145 :             continue;
    1687                 :             : 
    1688                 :             :           /* If we have *ORIG_P in our constant/copy table, then replace
    1689                 :             :              ORIG_P with its value in our constant/copy table.  */
    1690                 :    17027998 :           new_val = SSA_NAME_VALUE (orig_val);
    1691                 :    17027998 :           if (new_val
    1692                 :    17027998 :               && new_val != orig_val
    1693                 :    17027998 :               && may_propagate_copy (orig_val, new_val))
    1694                 :      871726 :             propagate_value (orig_p, new_val);
    1695                 :             :         }
    1696                 :             : 
    1697                 :    11734325 :       const_and_copies->pop_to_marker ();
    1698                 :             :     }
    1699                 :    20940373 : }
    1700                 :             : 
    1701                 :             : edge
    1702                 :    20940373 : dom_opt_dom_walker::before_dom_children (basic_block bb)
    1703                 :             : {
    1704                 :    20940373 :   gimple_stmt_iterator gsi;
    1705                 :             : 
    1706                 :    20940373 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1707                 :         721 :     fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
    1708                 :             : 
    1709                 :             :   /* Push a marker on the stacks of local information so that we know how
    1710                 :             :      far to unwind when we finalize this block.  */
    1711                 :    20940373 :   m_avail_exprs_stack->push_marker ();
    1712                 :    20940373 :   m_const_and_copies->push_marker ();
    1713                 :    20940373 :   bitmap_set_bit (m_state->get_blocks_on_stack (), bb->index);
    1714                 :             : 
    1715                 :    20940373 :   record_equivalences_from_incoming_edge (bb, m_const_and_copies,
    1716                 :             :                                           m_avail_exprs_stack,
    1717                 :    20940373 :                                           m_state->get_blocks_on_stack ());
    1718                 :    20940373 :   set_global_ranges_from_unreachable_edges (bb);
    1719                 :             : 
    1720                 :             :   /* PHI nodes can create equivalences too.  */
    1721                 :    20940373 :   record_equivalences_from_phis (bb);
    1722                 :             : 
    1723                 :             :   /* Create equivalences from redundant PHIs.  PHIs are only truly
    1724                 :             :      redundant when they exist in the same block, so push another
    1725                 :             :      marker and unwind right afterwards.  */
    1726                 :    20940373 :   m_avail_exprs_stack->push_marker ();
    1727                 :    28668089 :   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    1728                 :     7727716 :     eliminate_redundant_computations (&gsi, m_const_and_copies,
    1729                 :             :                                       m_avail_exprs_stack);
    1730                 :    20940373 :   m_avail_exprs_stack->pop_to_marker ();
    1731                 :             : 
    1732                 :    20940373 :   edge taken_edge = NULL;
    1733                 :             :   /* Initialize visited flag ahead of us, it has undefined state on
    1734                 :             :      pass entry.  */
    1735                 :   178581484 :   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    1736                 :   136700738 :     gimple_set_visited (gsi_stmt (gsi), false);
    1737                 :   294286425 :   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
    1738                 :             :     {
    1739                 :             :       /* Do not optimize a stmt twice, substitution might end up with
    1740                 :             :          _3 = _3 which is not valid.  */
    1741                 :   273346052 :       if (gimple_visited_p (gsi_stmt (gsi)))
    1742                 :             :         {
    1743                 :   136645198 :           gsi_next (&gsi);
    1744                 :   136645198 :           continue;
    1745                 :             :         }
    1746                 :             : 
    1747                 :   136700854 :       bool removed_p = false;
    1748                 :   136700854 :       taken_edge = this->optimize_stmt (bb, &gsi, &removed_p);
    1749                 :   136700854 :       if (!removed_p)
    1750                 :   136645198 :         gimple_set_visited (gsi_stmt (gsi), true);
    1751                 :             : 
    1752                 :             :       /* Go back and visit stmts inserted by folding after substituting
    1753                 :             :          into the stmt at gsi.  */
    1754                 :   136700854 :       if (gsi_end_p (gsi))
    1755                 :             :         {
    1756                 :        1889 :           gcc_checking_assert (removed_p);
    1757                 :        1889 :           gsi = gsi_last_bb (bb);
    1758                 :        3423 :           while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)))
    1759                 :        1889 :             gsi_prev (&gsi);
    1760                 :             :         }
    1761                 :             :       else
    1762                 :             :         {
    1763                 :   136699081 :           do
    1764                 :             :             {
    1765                 :   136699081 :               gsi_prev (&gsi);
    1766                 :             :             }
    1767                 :   273398046 :           while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)));
    1768                 :             :         }
    1769                 :   136700854 :       if (gsi_end_p (gsi))
    1770                 :    34227534 :         gsi = gsi_start_bb (bb);
    1771                 :             :       else
    1772                 :   119587087 :         gsi_next (&gsi);
    1773                 :             :     }
    1774                 :             : 
    1775                 :             :   /* Now prepare to process dominated blocks.  */
    1776                 :    20940373 :   record_edge_info (bb);
    1777                 :    20940373 :   cprop_into_successor_phis (bb, m_const_and_copies);
    1778                 :    20940373 :   if (taken_edge && !dbg_cnt (dom_unreachable_edges))
    1779                 :             :     return NULL;
    1780                 :             : 
    1781                 :             :   return taken_edge;
    1782                 :             : }
    1783                 :             : 
    1784                 :             : /* We have finished processing the dominator children of BB, perform
    1785                 :             :    any finalization actions in preparation for leaving this node in
    1786                 :             :    the dominator tree.  */
    1787                 :             : 
    1788                 :             : void
    1789                 :    20940373 : dom_opt_dom_walker::after_dom_children (basic_block bb)
    1790                 :             : {
    1791                 :    20940373 :   m_threader->thread_outgoing_edges (bb);
    1792                 :    20940373 :   bitmap_clear_bit (m_state->get_blocks_on_stack (), bb->index);
    1793                 :    20940373 :   m_avail_exprs_stack->pop_to_marker ();
    1794                 :    20940373 :   m_const_and_copies->pop_to_marker ();
    1795                 :    20940373 : }
    1796                 :             : 
    1797                 :             : /* Search for redundant computations in STMT.  If any are found, then
    1798                 :             :    replace them with the variable holding the result of the computation.
    1799                 :             : 
    1800                 :             :    If safe, record this expression into AVAIL_EXPRS_STACK and
    1801                 :             :    CONST_AND_COPIES.  */
    1802                 :             : 
    1803                 :             : static void
    1804                 :    56229663 : eliminate_redundant_computations (gimple_stmt_iterator* gsi,
    1805                 :             :                                   class const_and_copies *const_and_copies,
    1806                 :             :                                   class avail_exprs_stack *avail_exprs_stack)
    1807                 :             : {
    1808                 :    56229663 :   tree expr_type;
    1809                 :    56229663 :   tree cached_lhs;
    1810                 :    56229663 :   tree def;
    1811                 :    56229663 :   bool insert = true;
    1812                 :    56229663 :   bool assigns_var_p = false;
    1813                 :             : 
    1814                 :    56229663 :   gimple *stmt = gsi_stmt (*gsi);
    1815                 :             : 
    1816                 :    56229663 :   if (gimple_code (stmt) == GIMPLE_PHI)
    1817                 :     7727716 :     def = gimple_phi_result (stmt);
    1818                 :             :   else
    1819                 :    48501947 :     def = gimple_get_lhs (stmt);
    1820                 :             : 
    1821                 :             :   /* Certain expressions on the RHS can be optimized away, but cannot
    1822                 :             :      themselves be entered into the hash tables.  */
    1823                 :    56229663 :   if (! def
    1824                 :    48499106 :       || TREE_CODE (def) != SSA_NAME
    1825                 :    37105122 :       || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
    1826                 :    29376223 :       || gimple_vdef (stmt)
    1827                 :             :       /* Do not record equivalences for increments of ivs.  This would create
    1828                 :             :          overlapping live ranges for a very questionable gain.  */
    1829                 :    93325320 :       || simple_iv_increment_p (stmt))
    1830                 :             :     insert = false;
    1831                 :             : 
    1832                 :             :   /* Check if the expression has been computed before.  */
    1833                 :    56229663 :   cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, insert, true);
    1834                 :             : 
    1835                 :    56229663 :   opt_stats.num_exprs_considered++;
    1836                 :             : 
    1837                 :             :   /* Get the type of the expression we are trying to optimize.  */
    1838                 :    56229663 :   if (is_gimple_assign (stmt))
    1839                 :             :     {
    1840                 :    39622986 :       expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
    1841                 :    39622986 :       assigns_var_p = true;
    1842                 :             :     }
    1843                 :    16606677 :   else if (gimple_code (stmt) == GIMPLE_COND)
    1844                 :     7698753 :     expr_type = boolean_type_node;
    1845                 :     8907924 :   else if (is_gimple_call (stmt))
    1846                 :             :     {
    1847                 :     1148404 :       gcc_assert (gimple_call_lhs (stmt));
    1848                 :     1148404 :       expr_type = TREE_TYPE (gimple_call_lhs (stmt));
    1849                 :     1148404 :       assigns_var_p = true;
    1850                 :             :     }
    1851                 :     7759520 :   else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
    1852                 :       31804 :     expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
    1853                 :     7727716 :   else if (gimple_code (stmt) == GIMPLE_PHI)
    1854                 :             :     /* We can't propagate into a phi, so the logic below doesn't apply.
    1855                 :             :        Instead record an equivalence between the cached LHS and the
    1856                 :             :        PHI result of this statement, provided they are in the same block.
    1857                 :             :        This should be sufficient to kill the redundant phi.  */
    1858                 :             :     {
    1859                 :     7727716 :       if (def && cached_lhs)
    1860                 :       15873 :         const_and_copies->record_const_or_copy (def, cached_lhs);
    1861                 :     7727716 :       return;
    1862                 :             :     }
    1863                 :             :   else
    1864                 :           0 :     gcc_unreachable ();
    1865                 :             : 
    1866                 :    48501947 :   if (!cached_lhs)
    1867                 :             :     return;
    1868                 :             : 
    1869                 :             :   /* It is safe to ignore types here since we have already done
    1870                 :             :      type checking in the hashing and equality routines.  In fact
    1871                 :             :      type checking here merely gets in the way of constant
    1872                 :             :      propagation.  Also, make sure that it is safe to propagate
    1873                 :             :      CACHED_LHS into the expression in STMT.  */
    1874                 :      294196 :   if ((TREE_CODE (cached_lhs) != SSA_NAME
    1875                 :       28660 :        && (assigns_var_p
    1876                 :        1765 :            || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
    1877                 :      294196 :       || may_propagate_copy_into_stmt (stmt, cached_lhs))
    1878                 :             :   {
    1879                 :      294168 :       gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
    1880                 :             :                            || is_gimple_min_invariant (cached_lhs));
    1881                 :             : 
    1882                 :      294168 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1883                 :             :         {
    1884                 :          16 :           fprintf (dump_file, "  Replaced redundant expr '");
    1885                 :          16 :           print_gimple_expr (dump_file, stmt, 0, dump_flags);
    1886                 :          16 :           fprintf (dump_file, "' with '");
    1887                 :          16 :           print_generic_expr (dump_file, cached_lhs, dump_flags);
    1888                 :          16 :           fprintf (dump_file, "'\n");
    1889                 :             :         }
    1890                 :             : 
    1891                 :      294168 :       opt_stats.num_re++;
    1892                 :             : 
    1893                 :      294168 :       if (assigns_var_p
    1894                 :      294168 :           && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
    1895                 :           0 :         cached_lhs = fold_convert (expr_type, cached_lhs);
    1896                 :             : 
    1897                 :      294168 :       propagate_tree_value_into_stmt (gsi, cached_lhs);
    1898                 :             : 
    1899                 :             :       /* Since it is always necessary to mark the result as modified,
    1900                 :             :          perhaps we should move this into propagate_tree_value_into_stmt
    1901                 :             :          itself.  */
    1902                 :      294168 :       gimple_set_modified (gsi_stmt (*gsi), true);
    1903                 :             :   }
    1904                 :             : }
    1905                 :             : 
    1906                 :             : /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
    1907                 :             :    the available expressions table or the const_and_copies table.
    1908                 :             :    Detect and record those equivalences into AVAIL_EXPRS_STACK. 
    1909                 :             : 
    1910                 :             :    We handle only very simple copy equivalences here.  The heavy
    1911                 :             :    lifing is done by eliminate_redundant_computations.  */
    1912                 :             : 
    1913                 :             : static void
    1914                 :    43067196 : record_equivalences_from_stmt (gimple *stmt, int may_optimize_p,
    1915                 :             :                                class avail_exprs_stack *avail_exprs_stack)
    1916                 :             : {
    1917                 :    43067196 :   tree lhs;
    1918                 :    43067196 :   enum tree_code lhs_code;
    1919                 :             : 
    1920                 :    43067196 :   gcc_assert (is_gimple_assign (stmt));
    1921                 :             : 
    1922                 :    43067196 :   lhs = gimple_assign_lhs (stmt);
    1923                 :    43067196 :   lhs_code = TREE_CODE (lhs);
    1924                 :             : 
    1925                 :    43067196 :   if (lhs_code == SSA_NAME
    1926                 :    43067196 :       && gimple_assign_single_p (stmt))
    1927                 :             :     {
    1928                 :    14232425 :       tree rhs = gimple_assign_rhs1 (stmt);
    1929                 :             : 
    1930                 :             :       /* If the RHS of the assignment is a constant or another variable that
    1931                 :             :          may be propagated, register it in the CONST_AND_COPIES table.  We
    1932                 :             :          do not need to record unwind data for this, since this is a true
    1933                 :             :          assignment and not an equivalence inferred from a comparison.  All
    1934                 :             :          uses of this ssa name are dominated by this assignment, so unwinding
    1935                 :             :          just costs time and space.  */
    1936                 :    14232425 :       if (may_optimize_p
    1937                 :    14232425 :           && (TREE_CODE (rhs) == SSA_NAME
    1938                 :    12406106 :               || is_gimple_min_invariant (rhs)))
    1939                 :             :         {
    1940                 :     1689922 :           rhs = dom_valueize (rhs);
    1941                 :             : 
    1942                 :     1689922 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1943                 :             :             {
    1944                 :          74 :               fprintf (dump_file, "==== ASGN ");
    1945                 :          74 :               print_generic_expr (dump_file, lhs);
    1946                 :          74 :               fprintf (dump_file, " = ");
    1947                 :          74 :               print_generic_expr (dump_file, rhs);
    1948                 :          74 :               fprintf (dump_file, "\n");
    1949                 :             :             }
    1950                 :             : 
    1951                 :     1689922 :           set_ssa_name_value (lhs, rhs);
    1952                 :             :         }
    1953                 :             :     }
    1954                 :             : 
    1955                 :             :   /* Make sure we can propagate &x + CST.  */
    1956                 :    43067196 :   if (lhs_code == SSA_NAME
    1957                 :    28835149 :       && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
    1958                 :     1388305 :       && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
    1959                 :    43238853 :       && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
    1960                 :             :     {
    1961                 :        4127 :       tree op0 = gimple_assign_rhs1 (stmt);
    1962                 :        4127 :       tree op1 = gimple_assign_rhs2 (stmt);
    1963                 :        4127 :       tree new_rhs
    1964                 :        8254 :         = build1 (ADDR_EXPR, TREE_TYPE (op0),
    1965                 :        4127 :                   fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)),
    1966                 :             :                                unshare_expr (op0), fold_convert (ptr_type_node,
    1967                 :             :                                                                  op1)));
    1968                 :        4127 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1969                 :             :         {
    1970                 :           0 :           fprintf (dump_file, "==== ASGN ");
    1971                 :           0 :           print_generic_expr (dump_file, lhs);
    1972                 :           0 :           fprintf (dump_file, " = ");
    1973                 :           0 :           print_generic_expr (dump_file, new_rhs);
    1974                 :           0 :           fprintf (dump_file, "\n");
    1975                 :             :         }
    1976                 :             : 
    1977                 :        4127 :       set_ssa_name_value (lhs, new_rhs);
    1978                 :             :     }
    1979                 :             : 
    1980                 :             :   /* A memory store, even an aliased store, creates a useful
    1981                 :             :      equivalence.  By exchanging the LHS and RHS, creating suitable
    1982                 :             :      vops and recording the result in the available expression table,
    1983                 :             :      we may be able to expose more redundant loads.  */
    1984                 :    82635091 :   if (!gimple_has_volatile_ops (stmt)
    1985                 :    64782904 :       && gimple_references_memory_p (stmt)
    1986                 :    21715708 :       && gimple_assign_single_p (stmt)
    1987                 :    21715708 :       && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
    1988                 :    17030429 :           || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
    1989                 :    53270313 :       && !is_gimple_reg (lhs))
    1990                 :             :     {
    1991                 :    10077106 :       tree rhs = gimple_assign_rhs1 (stmt);
    1992                 :    10077106 :       gassign *new_stmt;
    1993                 :             : 
    1994                 :             :       /* Build a new statement with the RHS and LHS exchanged.  */
    1995                 :    10077106 :       if (TREE_CODE (rhs) == SSA_NAME)
    1996                 :             :         {
    1997                 :             :           /* NOTE tuples.  The call to gimple_build_assign below replaced
    1998                 :             :              a call to build_gimple_modify_stmt, which did not set the
    1999                 :             :              SSA_NAME_DEF_STMT on the LHS of the assignment.  Doing so
    2000                 :             :              may cause an SSA validation failure, as the LHS may be a
    2001                 :             :              default-initialized name and should have no definition.  I'm
    2002                 :             :              a bit dubious of this, as the artificial statement that we
    2003                 :             :              generate here may in fact be ill-formed, but it is simply
    2004                 :             :              used as an internal device in this pass, and never becomes
    2005                 :             :              part of the CFG.  */
    2006                 :     4580987 :           gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
    2007                 :     4580987 :           new_stmt = gimple_build_assign (rhs, lhs);
    2008                 :     4580987 :           SSA_NAME_DEF_STMT (rhs) = defstmt;
    2009                 :             :         }
    2010                 :             :       else
    2011                 :     5496119 :         new_stmt = gimple_build_assign (rhs, lhs);
    2012                 :             : 
    2013                 :    20154212 :       gimple_set_vuse (new_stmt, gimple_vdef (stmt));
    2014                 :             : 
    2015                 :             :       /* Finally enter the statement into the available expression
    2016                 :             :          table.  */
    2017                 :    10077106 :       avail_exprs_stack->lookup_avail_expr (new_stmt, true, true);
    2018                 :             :     }
    2019                 :    43067196 : }
    2020                 :             : 
    2021                 :             : /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
    2022                 :             :    CONST_AND_COPIES.  */
    2023                 :             : 
    2024                 :             : static void
    2025                 :    68664693 : cprop_operand (gimple *stmt, use_operand_p op_p, range_query *query)
    2026                 :             : {
    2027                 :    68664693 :   tree val;
    2028                 :    68664693 :   tree op = USE_FROM_PTR (op_p);
    2029                 :             : 
    2030                 :             :   /* If the operand has a known constant value or it is known to be a
    2031                 :             :      copy of some other variable, use the value or copy stored in
    2032                 :             :      CONST_AND_COPIES.  */
    2033                 :    68664693 :   val = SSA_NAME_VALUE (op);
    2034                 :    44855365 :   if (!val)
    2035                 :             :     {
    2036                 :    65411939 :       Value_Range r (TREE_TYPE (op));
    2037                 :    65411939 :       tree single;
    2038                 :   128815172 :       if (query->range_of_expr (r, op, stmt) && r.singleton_p (&single))
    2039                 :       15354 :         val = single;
    2040                 :    65411939 :     }
    2041                 :             : 
    2042                 :    68664693 :   if (val && val != op)
    2043                 :             :     {
    2044                 :             :       /* Do not replace hard register operands in asm statements.  */
    2045                 :     3266739 :       if (gimple_code (stmt) == GIMPLE_ASM
    2046                 :     3266739 :           && !may_propagate_copy_into_asm (op))
    2047                 :             :         return;
    2048                 :             : 
    2049                 :             :       /* Certain operands are not allowed to be copy propagated due
    2050                 :             :          to their interaction with exception handling and some GCC
    2051                 :             :          extensions.  */
    2052                 :     3266739 :       if (!may_propagate_copy (op, val))
    2053                 :             :         return;
    2054                 :             : 
    2055                 :             :       /* Do not propagate copies into BIVs.
    2056                 :             :          See PR23821 and PR62217 for how this can disturb IV and
    2057                 :             :          number of iteration analysis.  */
    2058                 :     3265268 :       if (TREE_CODE (val) != INTEGER_CST)
    2059                 :             :         {
    2060                 :     2713931 :           gimple *def = SSA_NAME_DEF_STMT (op);
    2061                 :     2713931 :           if (gimple_code (def) == GIMPLE_PHI
    2062                 :     2713931 :               && gimple_bb (def)->loop_father->header == gimple_bb (def))
    2063                 :             :             return;
    2064                 :             :         }
    2065                 :             : 
    2066                 :             :       /* Dump details.  */
    2067                 :     3249655 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2068                 :             :         {
    2069                 :          76 :           fprintf (dump_file, "  Replaced '");
    2070                 :          76 :           print_generic_expr (dump_file, op, dump_flags);
    2071                 :          76 :           fprintf (dump_file, "' with %s '",
    2072                 :          76 :                    (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
    2073                 :          76 :           print_generic_expr (dump_file, val, dump_flags);
    2074                 :          76 :           fprintf (dump_file, "'\n");
    2075                 :             :         }
    2076                 :             : 
    2077                 :     3249655 :       if (TREE_CODE (val) != SSA_NAME)
    2078                 :      716120 :         opt_stats.num_const_prop++;
    2079                 :             :       else
    2080                 :     2533535 :         opt_stats.num_copy_prop++;
    2081                 :             : 
    2082                 :     3249655 :       propagate_value (op_p, val);
    2083                 :             : 
    2084                 :             :       /* And note that we modified this statement.  This is now
    2085                 :             :          safe, even if we changed virtual operands since we will
    2086                 :             :          rescan the statement and rewrite its operands again.  */
    2087                 :     3249655 :       gimple_set_modified (stmt, true);
    2088                 :             :     }
    2089                 :             : }
    2090                 :             : 
    2091                 :             : /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
    2092                 :             :    known value for that SSA_NAME (or NULL if no value is known).
    2093                 :             : 
    2094                 :             :    Propagate values from CONST_AND_COPIES into the uses, vuses and
    2095                 :             :    vdef_ops of STMT.  */
    2096                 :             : 
    2097                 :             : static void
    2098                 :   136700854 : cprop_into_stmt (gimple *stmt, range_query *query)
    2099                 :             : {
    2100                 :   136700854 :   use_operand_p op_p;
    2101                 :   136700854 :   ssa_op_iter iter;
    2102                 :   136700854 :   tree last_copy_propagated_op = NULL;
    2103                 :             : 
    2104                 :   205366168 :   FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
    2105                 :             :     {
    2106                 :    68665314 :       tree old_op = USE_FROM_PTR (op_p);
    2107                 :             : 
    2108                 :             :       /* If we have A = B and B = A in the copy propagation tables
    2109                 :             :          (due to an equality comparison), avoid substituting B for A
    2110                 :             :          then A for B in the trivially discovered cases.   This allows
    2111                 :             :          optimization of statements were A and B appear as input
    2112                 :             :          operands.  */
    2113                 :    68665314 :       if (old_op != last_copy_propagated_op)
    2114                 :             :         {
    2115                 :    68664693 :           cprop_operand (stmt, op_p, query);
    2116                 :             : 
    2117                 :    68664693 :           tree new_op = USE_FROM_PTR (op_p);
    2118                 :    68664693 :           if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
    2119                 :    68665314 :             last_copy_propagated_op = new_op;
    2120                 :             :         }
    2121                 :             :     }
    2122                 :   136700854 : }
    2123                 :             : 
    2124                 :             : /* If STMT contains a relational test, try to convert it into an
    2125                 :             :    equality test if there is only a single value which can ever
    2126                 :             :    make the test true.
    2127                 :             : 
    2128                 :             :    For example, if the expression hash table contains:
    2129                 :             : 
    2130                 :             :     TRUE = (i <= 1)
    2131                 :             : 
    2132                 :             :    And we have a test within statement of i >= 1, then we can safely
    2133                 :             :    rewrite the test as i == 1 since there only a single value where
    2134                 :             :    the test is true.
    2135                 :             : 
    2136                 :             :    This is similar to code in VRP.  */
    2137                 :             : 
    2138                 :             : void
    2139                 :    48446291 : dom_opt_dom_walker::test_for_singularity (gimple *stmt,
    2140                 :             :                                           avail_exprs_stack *avail_exprs_stack)
    2141                 :             : {
    2142                 :             :   /* We want to support gimple conditionals as well as assignments
    2143                 :             :      where the RHS contains a conditional.  */
    2144                 :    48446291 :   if (is_gimple_assign (stmt) || gimple_code (stmt) == GIMPLE_COND)
    2145                 :             :     {
    2146                 :    47266648 :       enum tree_code code = ERROR_MARK;
    2147                 :    47266648 :       tree lhs, rhs;
    2148                 :             : 
    2149                 :             :       /* Extract the condition of interest from both forms we support.  */
    2150                 :    47266648 :       if (is_gimple_assign (stmt))
    2151                 :             :         {
    2152                 :    39567895 :           code = gimple_assign_rhs_code (stmt);
    2153                 :    39567895 :           lhs = gimple_assign_rhs1 (stmt);
    2154                 :    39567895 :           rhs = gimple_assign_rhs2 (stmt);
    2155                 :             :         }
    2156                 :     7698753 :       else if (gimple_code (stmt) == GIMPLE_COND)
    2157                 :             :         {
    2158                 :     7698753 :           code = gimple_cond_code (as_a <gcond *> (stmt));
    2159                 :     7698753 :           lhs = gimple_cond_lhs (as_a <gcond *> (stmt));
    2160                 :     7698753 :           rhs = gimple_cond_rhs (as_a <gcond *> (stmt));
    2161                 :             :         }
    2162                 :             : 
    2163                 :             :       /* We're looking for a relational test using LE/GE.  Also note we can
    2164                 :             :          canonicalize LT/GT tests against constants into LE/GT tests.  */
    2165                 :    47266648 :       if (code == LE_EXPR || code == GE_EXPR
    2166                 :    46694503 :           || ((code == LT_EXPR || code == GT_EXPR)
    2167                 :     1330467 :                && TREE_CODE (rhs) == INTEGER_CST))
    2168                 :             :         {
    2169                 :             :           /* For LT_EXPR and GT_EXPR, canonicalize to LE_EXPR and GE_EXPR.  */
    2170                 :      665315 :           if (code == LT_EXPR)
    2171                 :       90101 :             rhs = fold_build2 (MINUS_EXPR, TREE_TYPE (rhs),
    2172                 :             :                                rhs, build_int_cst (TREE_TYPE (rhs), 1));
    2173                 :             : 
    2174                 :     1237460 :           if (code == GT_EXPR)
    2175                 :      575214 :             rhs = fold_build2 (PLUS_EXPR, TREE_TYPE (rhs),
    2176                 :             :                                rhs, build_int_cst (TREE_TYPE (rhs), 1));
    2177                 :             : 
    2178                 :             :           /* Determine the code we want to check for in the hash table.  */
    2179                 :     1237460 :           enum tree_code test_code;
    2180                 :     1237460 :           if (code == GE_EXPR || code == GT_EXPR)
    2181                 :             :             test_code = LE_EXPR;
    2182                 :             :           else
    2183                 :      432602 :             test_code = GE_EXPR;
    2184                 :             : 
    2185                 :             :           /* Update the dummy statement so we can query the hash tables.  */
    2186                 :     1237460 :           gimple_cond_set_code (m_dummy_cond, test_code);
    2187                 :     1237460 :           gimple_cond_set_lhs (m_dummy_cond, lhs);
    2188                 :     1237460 :           gimple_cond_set_rhs (m_dummy_cond, rhs);
    2189                 :     1237460 :           tree cached_lhs
    2190                 :     1237460 :             = avail_exprs_stack->lookup_avail_expr (m_dummy_cond,
    2191                 :             :                                                     false, false);
    2192                 :             : 
    2193                 :             :           /* If the lookup returned 1 (true), then the expression we
    2194                 :             :              queried was in the hash table.  As a result there is only
    2195                 :             :              one value that makes the original conditional true.  Update
    2196                 :             :              STMT accordingly.  */
    2197                 :     1237460 :           if (cached_lhs && integer_onep (cached_lhs))
    2198                 :             :             {
    2199                 :        1450 :               if (is_gimple_assign (stmt))
    2200                 :             :                 {
    2201                 :          91 :                   gimple_assign_set_rhs_code (stmt, EQ_EXPR);
    2202                 :          91 :                   gimple_assign_set_rhs2 (stmt, rhs);
    2203                 :          91 :                   gimple_set_modified (stmt, true);
    2204                 :             :                 }
    2205                 :             :               else
    2206                 :             :                 {
    2207                 :        1359 :                   gimple_set_modified (stmt, true);
    2208                 :        1359 :                   gimple_cond_set_code (as_a <gcond *> (stmt), EQ_EXPR);
    2209                 :        1359 :                   gimple_cond_set_rhs (as_a <gcond *> (stmt), rhs);
    2210                 :        1359 :                   gimple_set_modified (stmt, true);
    2211                 :             :                 }
    2212                 :             :             }
    2213                 :             :         }
    2214                 :             :     }
    2215                 :    48446291 : }
    2216                 :             : 
    2217                 :             : /* If STMT is a comparison of two uniform vectors reduce it to a comparison
    2218                 :             :    of scalar objects, otherwise leave STMT unchanged.  */
    2219                 :             : 
    2220                 :             : static void
    2221                 :   136700854 : reduce_vector_comparison_to_scalar_comparison (gimple *stmt)
    2222                 :             : {
    2223                 :   136700854 :   if (gimple_code (stmt) == GIMPLE_COND)
    2224                 :             :     {
    2225                 :     7733058 :       tree lhs = gimple_cond_lhs (stmt);
    2226                 :     7733058 :       tree rhs = gimple_cond_rhs (stmt);
    2227                 :             : 
    2228                 :             :       /* We may have a vector comparison where both arms are uniform
    2229                 :             :          vectors.  If so, we can simplify the vector comparison down
    2230                 :             :          to a scalar comparison.  */
    2231                 :     7733058 :       if (VECTOR_TYPE_P (TREE_TYPE (lhs))
    2232                 :     7733058 :           && VECTOR_TYPE_P (TREE_TYPE (rhs)))
    2233                 :             :         {
    2234                 :             :           /* If either operand is an SSA_NAME, then look back to its
    2235                 :             :              defining statement to try and get at a suitable source.  */
    2236                 :        2628 :           if (TREE_CODE (rhs) == SSA_NAME)
    2237                 :             :             {
    2238                 :           0 :               gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
    2239                 :           0 :               if (gimple_assign_single_p (def_stmt))
    2240                 :           0 :                 rhs = gimple_assign_rhs1 (def_stmt);
    2241                 :             :             }
    2242                 :             : 
    2243                 :        2628 :           if (TREE_CODE (lhs) == SSA_NAME)
    2244                 :             :             {
    2245                 :        2628 :               gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
    2246                 :        2628 :               if (gimple_assign_single_p (def_stmt))
    2247                 :           2 :                 lhs = gimple_assign_rhs1 (def_stmt);
    2248                 :             :             }
    2249                 :             : 
    2250                 :             :           /* Now see if they are both uniform vectors and if so replace
    2251                 :             :              the vector comparison with a scalar comparison.  */
    2252                 :        2628 :           tree rhs_elem = rhs ? uniform_vector_p (rhs) : NULL_TREE;
    2253                 :        2628 :           tree lhs_elem = lhs ? uniform_vector_p (lhs) : NULL_TREE;
    2254                 :        2628 :           if (rhs_elem && lhs_elem)
    2255                 :             :             {
    2256                 :           2 :               if (dump_file && dump_flags & TDF_DETAILS)
    2257                 :             :                 {
    2258                 :           0 :                   fprintf (dump_file, "Reducing vector comparison: ");
    2259                 :           0 :                   print_gimple_stmt (dump_file, stmt, 0);
    2260                 :             :                 }
    2261                 :             : 
    2262                 :           2 :               gimple_cond_set_rhs (as_a <gcond *>(stmt), rhs_elem);
    2263                 :           2 :               gimple_cond_set_lhs (as_a <gcond *>(stmt), lhs_elem);
    2264                 :           2 :               gimple_set_modified (stmt, true);
    2265                 :             : 
    2266                 :           2 :               if (dump_file && dump_flags & TDF_DETAILS)
    2267                 :             :                 {
    2268                 :           0 :                   fprintf (dump_file, "To scalar equivalent: ");
    2269                 :           0 :                   print_gimple_stmt (dump_file, stmt, 0);
    2270                 :           0 :                   fprintf (dump_file, "\n");
    2271                 :             :                 }
    2272                 :             :             }
    2273                 :             :         }
    2274                 :             :     }
    2275                 :   136700854 : }
    2276                 :             : 
    2277                 :             : /* If possible, rewrite the conditional as TRUE or FALSE, and return
    2278                 :             :    the taken edge.  Otherwise, return NULL.  */
    2279                 :             : 
    2280                 :             : edge
    2281                 :     7564488 : dom_opt_dom_walker::fold_cond (gcond *cond)
    2282                 :             : {
    2283                 :     7564488 :   simplify_using_ranges simplify (m_ranger);
    2284                 :     7564488 :   if (simplify.fold_cond (cond))
    2285                 :             :     {
    2286                 :       34305 :       basic_block bb = gimple_bb (cond);
    2287                 :       34305 :       if (gimple_cond_true_p (cond))
    2288                 :       17046 :         return find_taken_edge (bb, boolean_true_node);
    2289                 :       17259 :       if (gimple_cond_false_p (cond))
    2290                 :       17259 :         return find_taken_edge (bb, boolean_false_node);
    2291                 :             :     }
    2292                 :             :   return NULL;
    2293                 :     7564488 : }
    2294                 :             : 
    2295                 :             : /* Optimize the statement in block BB pointed to by iterator SI.
    2296                 :             : 
    2297                 :             :    We try to perform some simplistic global redundancy elimination and
    2298                 :             :    constant propagation:
    2299                 :             : 
    2300                 :             :    1- To detect global redundancy, we keep track of expressions that have
    2301                 :             :       been computed in this block and its dominators.  If we find that the
    2302                 :             :       same expression is computed more than once, we eliminate repeated
    2303                 :             :       computations by using the target of the first one.
    2304                 :             : 
    2305                 :             :    2- Constant values and copy assignments.  This is used to do very
    2306                 :             :       simplistic constant and copy propagation.  When a constant or copy
    2307                 :             :       assignment is found, we map the value on the RHS of the assignment to
    2308                 :             :       the variable in the LHS in the CONST_AND_COPIES table.
    2309                 :             : 
    2310                 :             :    3- Very simple redundant store elimination is performed.
    2311                 :             : 
    2312                 :             :    4- We can simplify a condition to a constant or from a relational
    2313                 :             :       condition to an equality condition.  */
    2314                 :             : 
    2315                 :             : edge
    2316                 :   136700854 : dom_opt_dom_walker::optimize_stmt (basic_block bb, gimple_stmt_iterator *si,
    2317                 :             :                                    bool *removed_p)
    2318                 :             : {
    2319                 :   136700854 :   gimple *stmt, *old_stmt;
    2320                 :   136700854 :   bool may_optimize_p;
    2321                 :   136700854 :   bool modified_p = false;
    2322                 :   136700854 :   bool was_noreturn;
    2323                 :   136700854 :   edge retval = NULL;
    2324                 :             : 
    2325                 :   136700854 :   old_stmt = stmt = gsi_stmt (*si);
    2326                 :   136700854 :   was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
    2327                 :             : 
    2328                 :   136700854 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2329                 :             :     {
    2330                 :        1579 :       fprintf (dump_file, "Optimizing statement ");
    2331                 :        1579 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    2332                 :             :     }
    2333                 :             : 
    2334                 :             :   /* STMT may be a comparison of uniform vectors that we can simplify
    2335                 :             :      down to a comparison of scalars.  Do that transformation first
    2336                 :             :      so that all the scalar optimizations from here onward apply.  */
    2337                 :   136700854 :   reduce_vector_comparison_to_scalar_comparison (stmt);
    2338                 :             : 
    2339                 :   136700854 :   update_stmt_if_modified (stmt);
    2340                 :   136700854 :   opt_stats.num_stmts++;
    2341                 :             : 
    2342                 :             :   /* Const/copy propagate into USES, VUSES and the RHS of VDEFs.  */
    2343                 :   136700854 :   cprop_into_stmt (stmt, m_ranger);
    2344                 :             : 
    2345                 :             :   /* If the statement has been modified with constant replacements,
    2346                 :             :      fold its RHS before checking for redundant computations.  */
    2347                 :   273126674 :   if (gimple_modified_p (stmt))
    2348                 :             :     {
    2349                 :     3127668 :       tree rhs = NULL;
    2350                 :             : 
    2351                 :             :       /* Try to fold the statement making sure that STMT is kept
    2352                 :             :          up to date.  */
    2353                 :     3127668 :       if (fold_stmt (si))
    2354                 :             :         {
    2355                 :      298568 :           stmt = gsi_stmt (*si);
    2356                 :      298568 :           gimple_set_modified (stmt, true);
    2357                 :             : 
    2358                 :      298568 :           if (dump_file && (dump_flags & TDF_DETAILS))
    2359                 :             :             {
    2360                 :          12 :               fprintf (dump_file, "  Folded to: ");
    2361                 :          12 :               print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    2362                 :             :             }
    2363                 :             :         }
    2364                 :             : 
    2365                 :             :       /* We only need to consider cases that can yield a gimple operand.  */
    2366                 :     3127668 :       if (gimple_assign_single_p (stmt))
    2367                 :     1207267 :         rhs = gimple_assign_rhs1 (stmt);
    2368                 :     1920401 :       else if (gimple_code (stmt) == GIMPLE_GOTO)
    2369                 :           1 :         rhs = gimple_goto_dest (stmt);
    2370                 :   138621254 :       else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
    2371                 :             :         /* This should never be an ADDR_EXPR.  */
    2372                 :        2014 :         rhs = gimple_switch_index (swtch_stmt);
    2373                 :             : 
    2374                 :     1209282 :       if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
    2375                 :       79139 :         recompute_tree_invariant_for_addr_expr (rhs);
    2376                 :             : 
    2377                 :             :       /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
    2378                 :             :          even if fold_stmt updated the stmt already and thus cleared
    2379                 :             :          gimple_modified_p flag on it.  */
    2380                 :             :       modified_p = true;
    2381                 :             :     }
    2382                 :             : 
    2383                 :             :   /* Check for redundant computations.  Do this optimization only
    2384                 :             :      for assignments that have no volatile ops and conditionals.  */
    2385                 :   136700854 :   may_optimize_p = (!gimple_has_side_effects (stmt)
    2386                 :   136700854 :                     && (is_gimple_assign (stmt)
    2387                 :    84858499 :                         || (is_gimple_call (stmt)
    2388                 :     1148662 :                             && gimple_call_lhs (stmt) != NULL_TREE)
    2389                 :    83710061 :                         || gimple_code (stmt) == GIMPLE_COND
    2390                 :    75977003 :                         || gimple_code (stmt) == GIMPLE_SWITCH));
    2391                 :             : 
    2392                 :    48536252 :   if (may_optimize_p)
    2393                 :             :     {
    2394                 :    48536252 :       if (gimple_code (stmt) == GIMPLE_CALL)
    2395                 :             :         {
    2396                 :             :           /* Resolve __builtin_constant_p.  If it hasn't been
    2397                 :             :              folded to integer_one_node by now, it's fairly
    2398                 :             :              certain that the value simply isn't constant.  */
    2399                 :     1148438 :           tree callee = gimple_call_fndecl (stmt);
    2400                 :     1148438 :           if (callee
    2401                 :     1148438 :               && fndecl_built_in_p (callee, BUILT_IN_CONSTANT_P))
    2402                 :             :             {
    2403                 :          34 :               propagate_tree_value_into_stmt (si, integer_zero_node);
    2404                 :          34 :               stmt = gsi_stmt (*si);
    2405                 :             :             }
    2406                 :             :         }
    2407                 :             : 
    2408                 :    48536252 :       if (gimple_code (stmt) == GIMPLE_COND)
    2409                 :             :         {
    2410                 :     7733058 :           tree lhs = gimple_cond_lhs (stmt);
    2411                 :     7733058 :           tree rhs = gimple_cond_rhs (stmt);
    2412                 :             : 
    2413                 :             :           /* If the LHS has a range [0..1] and the RHS has a range ~[0..1],
    2414                 :             :              then this conditional is computable at compile time.  We can just
    2415                 :             :              shove either 0 or 1 into the LHS, mark the statement as modified
    2416                 :             :              and all the right things will just happen below.
    2417                 :             : 
    2418                 :             :              Note this would apply to any case where LHS has a range
    2419                 :             :              narrower than its type implies and RHS is outside that
    2420                 :             :              narrower range.  Future work.  */
    2421                 :     7733058 :           if (TREE_CODE (lhs) == SSA_NAME
    2422                 :     7564517 :               && ssa_name_has_boolean_range (lhs)
    2423                 :      910890 :               && TREE_CODE (rhs) == INTEGER_CST
    2424                 :     8615751 :               && ! (integer_zerop (rhs) || integer_onep (rhs)))
    2425                 :             :             {
    2426                 :          29 :               gimple_cond_set_lhs (as_a <gcond *> (stmt),
    2427                 :          29 :                                    fold_convert (TREE_TYPE (lhs),
    2428                 :             :                                                  integer_zero_node));
    2429                 :          29 :               gimple_set_modified (stmt, true);
    2430                 :             :             }
    2431                 :     7733029 :           else if (TREE_CODE (lhs) == SSA_NAME)
    2432                 :             :             {
    2433                 :             :               /* Exploiting EVRP data is not yet fully integrated into DOM
    2434                 :             :                  but we need to do something for this case to avoid regressing
    2435                 :             :                  udr4.f90 and new1.C which have unexecutable blocks with
    2436                 :             :                  undefined behavior that get diagnosed if they're left in the
    2437                 :             :                  IL because we've attached range information to new
    2438                 :             :                  SSA_NAMES.  */
    2439                 :     7564488 :               update_stmt_if_modified (stmt);
    2440                 :     7564488 :               edge taken_edge = fold_cond (as_a <gcond *> (stmt));
    2441                 :     7564488 :               if (taken_edge)
    2442                 :             :                 {
    2443                 :       34305 :                   gimple_set_modified (stmt, true);
    2444                 :       34305 :                   update_stmt (stmt);
    2445                 :       34305 :                   cfg_altered = true;
    2446                 :       34305 :                   return taken_edge;
    2447                 :             :                 }
    2448                 :             :             }
    2449                 :             :         }
    2450                 :             : 
    2451                 :    48501947 :       update_stmt_if_modified (stmt);
    2452                 :    48501947 :       eliminate_redundant_computations (si, m_const_and_copies,
    2453                 :             :                                         m_avail_exprs_stack);
    2454                 :    48501947 :       stmt = gsi_stmt (*si);
    2455                 :             : 
    2456                 :             :       /* Perform simple redundant store elimination.  */
    2457                 :    48501947 :       if (gimple_assign_single_p (stmt)
    2458                 :    48501947 :           && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
    2459                 :             :         {
    2460                 :    11375052 :           tree lhs = gimple_assign_lhs (stmt);
    2461                 :    11375052 :           tree rhs = gimple_assign_rhs1 (stmt);
    2462                 :    11375052 :           tree cached_lhs;
    2463                 :    11375052 :           gassign *new_stmt;
    2464                 :    11375052 :           rhs = dom_valueize (rhs);
    2465                 :             :           /* Build a new statement with the RHS and LHS exchanged.  */
    2466                 :    11375052 :           if (TREE_CODE (rhs) == SSA_NAME)
    2467                 :             :             {
    2468                 :     4634875 :               gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
    2469                 :     4634875 :               new_stmt = gimple_build_assign (rhs, lhs);
    2470                 :     4634875 :               SSA_NAME_DEF_STMT (rhs) = defstmt;
    2471                 :             :             }
    2472                 :             :           else
    2473                 :     6740177 :             new_stmt = gimple_build_assign (rhs, lhs);
    2474                 :    22750104 :           gimple_set_vuse (new_stmt, gimple_vuse (stmt));
    2475                 :    11375052 :           expr_hash_elt *elt = NULL;
    2476                 :    11375052 :           cached_lhs = m_avail_exprs_stack->lookup_avail_expr (new_stmt, false,
    2477                 :             :                                                                false, &elt);
    2478                 :    11375052 :           if (cached_lhs
    2479                 :      635051 :               && operand_equal_p (rhs, cached_lhs, 0)
    2480                 :    11487170 :               && refs_same_for_tbaa_p (elt->expr ()->kind == EXPR_SINGLE
    2481                 :       56059 :                                        ? elt->expr ()->ops.single.rhs
    2482                 :             :                                        : NULL_TREE, lhs))
    2483                 :             :             {
    2484                 :       55656 :               basic_block bb = gimple_bb (stmt);
    2485                 :       55656 :               unlink_stmt_vdef (stmt);
    2486                 :       55656 :               if (gsi_remove (si, true))
    2487                 :             :                 {
    2488                 :           0 :                   bitmap_set_bit (need_eh_cleanup, bb->index);
    2489                 :           0 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    2490                 :           0 :                     fprintf (dump_file, "  Flagged to clear EH edges.\n");
    2491                 :             :                 }
    2492                 :       55656 :               release_defs (stmt);
    2493                 :       55656 :               *removed_p = true;
    2494                 :       55656 :               return retval;
    2495                 :             :             }
    2496                 :             :         }
    2497                 :             : 
    2498                 :             :       /* If this statement was not redundant, we may still be able to simplify
    2499                 :             :          it, which may in turn allow other part of DOM or other passes to do
    2500                 :             :          a better job.  */
    2501                 :    48446291 :       test_for_singularity (stmt, m_avail_exprs_stack);
    2502                 :             :     }
    2503                 :             : 
    2504                 :             :   /* Record any additional equivalences created by this statement.  */
    2505                 :   136610893 :   if (is_gimple_assign (stmt))
    2506                 :    43067196 :     record_equivalences_from_stmt (stmt, may_optimize_p, m_avail_exprs_stack);
    2507                 :             : 
    2508                 :             :   /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
    2509                 :             :      know where it goes.  */
    2510                 :   273221786 :   if (gimple_modified_p (stmt) || modified_p)
    2511                 :             :     {
    2512                 :     3336564 :       tree val = NULL;
    2513                 :             : 
    2514                 :     3336564 :       if (gimple_code (stmt) == GIMPLE_COND)
    2515                 :      368669 :         val = fold_binary_loc (gimple_location (stmt),
    2516                 :             :                                gimple_cond_code (stmt), boolean_type_node,
    2517                 :             :                                gimple_cond_lhs (stmt),
    2518                 :             :                                gimple_cond_rhs (stmt));
    2519                 :     2967895 :       else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
    2520                 :        2014 :         val = gimple_switch_index (swtch_stmt);
    2521                 :             : 
    2522                 :      370683 :       if (val && TREE_CODE (val) == INTEGER_CST)
    2523                 :             :         {
    2524                 :       41462 :           retval = find_taken_edge (bb, val);
    2525                 :       41462 :           if (retval)
    2526                 :             :             {
    2527                 :             :               /* Fix the condition to be either true or false.  */
    2528                 :       41462 :               if (gimple_code (stmt) == GIMPLE_COND)
    2529                 :             :                 {
    2530                 :       41431 :                   if (integer_zerop (val))
    2531                 :       21618 :                     gimple_cond_make_false (as_a <gcond *> (stmt));
    2532                 :       19813 :                   else if (integer_onep (val))
    2533                 :       19813 :                     gimple_cond_make_true (as_a <gcond *> (stmt));
    2534                 :             :                   else
    2535                 :           0 :                     gcc_unreachable ();
    2536                 :             : 
    2537                 :       41431 :                   gimple_set_modified (stmt, true);
    2538                 :             :                 }
    2539                 :             : 
    2540                 :             :               /* Further simplifications may be possible.  */
    2541                 :       41462 :               cfg_altered = true;
    2542                 :             :             }
    2543                 :             :         }
    2544                 :             : 
    2545                 :     3336564 :       update_stmt_if_modified (stmt);
    2546                 :             : 
    2547                 :             :       /* If we simplified a statement in such a way as to be shown that it
    2548                 :             :          cannot trap, update the eh information and the cfg to match.  */
    2549                 :     3336564 :       if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
    2550                 :             :         {
    2551                 :        1624 :           bitmap_set_bit (need_eh_cleanup, bb->index);
    2552                 :        1624 :           if (dump_file && (dump_flags & TDF_DETAILS))
    2553                 :           0 :             fprintf (dump_file, "  Flagged to clear EH edges.\n");
    2554                 :             :         }
    2555                 :             : 
    2556                 :     3336564 :       if (!was_noreturn
    2557                 :     3336564 :           && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
    2558                 :           7 :         need_noreturn_fixup.safe_push (stmt);
    2559                 :             :     }
    2560                 :             :   return retval;
    2561                 :             : }
        

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.