LCOV - code coverage report
Current view: top level - gcc - tree-ssa-loop-im.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 98.3 % 1609 1581
Test Date: 2024-04-20 14:03:02 Functions: 91.0 % 78 71
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Loop invariant motion.
       2                 :             :    Copyright (C) 2003-2024 Free Software Foundation, Inc.
       3                 :             : 
       4                 :             : This file is part of GCC.
       5                 :             : 
       6                 :             : GCC is free software; you can redistribute it and/or modify it
       7                 :             : under the terms of the GNU General Public License as published by the
       8                 :             : Free Software Foundation; either version 3, or (at your option) any
       9                 :             : later version.
      10                 :             : 
      11                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT
      12                 :             : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14                 :             : for more details.
      15                 :             : 
      16                 :             : You should have received a copy of the GNU General Public License
      17                 :             : along with GCC; see the file COPYING3.  If not see
      18                 :             : <http://www.gnu.org/licenses/>.  */
      19                 :             : 
      20                 :             : #include "config.h"
      21                 :             : #include "system.h"
      22                 :             : #include "coretypes.h"
      23                 :             : #include "backend.h"
      24                 :             : #include "tree.h"
      25                 :             : #include "gimple.h"
      26                 :             : #include "cfghooks.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 "tree-eh.h"
      33                 :             : #include "gimplify.h"
      34                 :             : #include "gimple-iterator.h"
      35                 :             : #include "tree-cfg.h"
      36                 :             : #include "tree-ssa-loop-manip.h"
      37                 :             : #include "tree-ssa-loop.h"
      38                 :             : #include "tree-into-ssa.h"
      39                 :             : #include "cfgloop.h"
      40                 :             : #include "tree-affine.h"
      41                 :             : #include "tree-ssa-propagate.h"
      42                 :             : #include "trans-mem.h"
      43                 :             : #include "gimple-fold.h"
      44                 :             : #include "tree-scalar-evolution.h"
      45                 :             : #include "tree-ssa-loop-niter.h"
      46                 :             : #include "alias.h"
      47                 :             : #include "builtins.h"
      48                 :             : #include "tree-dfa.h"
      49                 :             : #include "tree-ssa.h"
      50                 :             : #include "dbgcnt.h"
      51                 :             : #include "insn-codes.h"
      52                 :             : #include "optabs-tree.h"
      53                 :             : 
      54                 :             : /* TODO:  Support for predicated code motion.  I.e.
      55                 :             : 
      56                 :             :    while (1)
      57                 :             :      {
      58                 :             :        if (cond)
      59                 :             :          {
      60                 :             :            a = inv;
      61                 :             :            something;
      62                 :             :          }
      63                 :             :      }
      64                 :             : 
      65                 :             :    Where COND and INV are invariants, but evaluating INV may trap or be
      66                 :             :    invalid from some other reason if !COND.  This may be transformed to
      67                 :             : 
      68                 :             :    if (cond)
      69                 :             :      a = inv;
      70                 :             :    while (1)
      71                 :             :      {
      72                 :             :        if (cond)
      73                 :             :          something;
      74                 :             :      }  */
      75                 :             : 
      76                 :             : /* The auxiliary data kept for each statement.  */
      77                 :             : 
      78                 :             : struct lim_aux_data
      79                 :             : {
      80                 :             :   class loop *max_loop; /* The outermost loop in that the statement
      81                 :             :                                    is invariant.  */
      82                 :             : 
      83                 :             :   class loop *tgt_loop; /* The loop out of that we want to move the
      84                 :             :                                    invariant.  */
      85                 :             : 
      86                 :             :   class loop *always_executed_in;
      87                 :             :                                 /* The outermost loop for that we are sure
      88                 :             :                                    the statement is executed if the loop
      89                 :             :                                    is entered.  */
      90                 :             : 
      91                 :             :   unsigned cost;                /* Cost of the computation performed by the
      92                 :             :                                    statement.  */
      93                 :             : 
      94                 :             :   unsigned ref;                 /* The simple_mem_ref in this stmt or 0.  */
      95                 :             : 
      96                 :             :   vec<gimple *> depends;  /* Vector of statements that must be also
      97                 :             :                                    hoisted out of the loop when this statement
      98                 :             :                                    is hoisted; i.e. those that define the
      99                 :             :                                    operands of the statement and are inside of
     100                 :             :                                    the MAX_LOOP loop.  */
     101                 :             : };
     102                 :             : 
     103                 :             : /* Maps statements to their lim_aux_data.  */
     104                 :             : 
     105                 :             : static hash_map<gimple *, lim_aux_data *> *lim_aux_data_map;
     106                 :             : 
     107                 :             : /* Description of a memory reference location.  */
     108                 :             : 
     109                 :             : struct mem_ref_loc
     110                 :             : {
     111                 :             :   tree *ref;                    /* The reference itself.  */
     112                 :             :   gimple *stmt;                 /* The statement in that it occurs.  */
     113                 :             : };
     114                 :             : 
     115                 :             : 
     116                 :             : /* Description of a memory reference.  */
     117                 :             : 
     118                 :             : class im_mem_ref
     119                 :             : {
     120                 :             : public:
     121                 :             :   unsigned id : 30;             /* ID assigned to the memory reference
     122                 :             :                                    (its index in memory_accesses.refs_list)  */
     123                 :             :   unsigned ref_canonical : 1;   /* Whether mem.ref was canonicalized.  */
     124                 :             :   unsigned ref_decomposed : 1;  /* Whether the ref was hashed from mem.  */
     125                 :             :   hashval_t hash;               /* Its hash value.  */
     126                 :             : 
     127                 :             :   /* The memory access itself and associated caching of alias-oracle
     128                 :             :      query meta-data.  We are using mem.ref == error_mark_node for the
     129                 :             :      case the reference is represented by its single access stmt
     130                 :             :      in accesses_in_loop[0].  */
     131                 :             :   ao_ref mem;
     132                 :             : 
     133                 :             :   bitmap stored;                /* The set of loops in that this memory location
     134                 :             :                                    is stored to.  */
     135                 :             :   bitmap loaded;                /* The set of loops in that this memory location
     136                 :             :                                    is loaded from.  */
     137                 :             :   vec<mem_ref_loc>                accesses_in_loop;
     138                 :             :                                 /* The locations of the accesses.  */
     139                 :             : 
     140                 :             :   /* The following set is computed on demand.  */
     141                 :             :   bitmap_head dep_loop;         /* The set of loops in that the memory
     142                 :             :                                    reference is {in,}dependent in
     143                 :             :                                    different modes.  */
     144                 :             : };
     145                 :             : 
     146                 :             : /* We use six bits per loop in the ref->dep_loop bitmap to record
     147                 :             :    the dep_kind x dep_state combinations.  */
     148                 :             : 
     149                 :             : enum dep_kind { lim_raw, sm_war, sm_waw };
     150                 :             : enum dep_state { dep_unknown, dep_independent, dep_dependent };
     151                 :             : 
     152                 :             : /* coldest outermost loop for given loop.  */
     153                 :             : vec<class loop *> coldest_outermost_loop;
     154                 :             : /* hotter outer loop nearest to given loop.  */
     155                 :             : vec<class loop *> hotter_than_inner_loop;
     156                 :             : 
     157                 :             : /* Populate the loop dependence cache of REF for LOOP, KIND with STATE.  */
     158                 :             : 
     159                 :             : static void
     160                 :     1812275 : record_loop_dependence (class loop *loop, im_mem_ref *ref,
     161                 :             :                         dep_kind kind, dep_state state)
     162                 :             : {
     163                 :     1812275 :   gcc_assert (state != dep_unknown);
     164                 :     1812275 :   unsigned bit = 6 * loop->num + kind * 2 + state == dep_dependent ? 1 : 0;
     165                 :     1812275 :   bitmap_set_bit (&ref->dep_loop, bit);
     166                 :     1812275 : }
     167                 :             : 
     168                 :             : /* Query the loop dependence cache of REF for LOOP, KIND.  */
     169                 :             : 
     170                 :             : static dep_state
     171                 :      567030 : query_loop_dependence (class loop *loop, im_mem_ref *ref, dep_kind kind)
     172                 :             : {
     173                 :      567030 :   unsigned first_bit = 6 * loop->num + kind * 2;
     174                 :      567030 :   if (bitmap_bit_p (&ref->dep_loop, first_bit))
     175                 :             :     return dep_independent;
     176                 :      567030 :   else if (bitmap_bit_p (&ref->dep_loop, first_bit + 1))
     177                 :           0 :     return dep_dependent;
     178                 :             :   return dep_unknown;
     179                 :             : }
     180                 :             : 
     181                 :             : /* Mem_ref hashtable helpers.  */
     182                 :             : 
     183                 :             : struct mem_ref_hasher : nofree_ptr_hash <im_mem_ref>
     184                 :             : {
     185                 :             :   typedef ao_ref *compare_type;
     186                 :             :   static inline hashval_t hash (const im_mem_ref *);
     187                 :             :   static inline bool equal (const im_mem_ref *, const ao_ref *);
     188                 :             : };
     189                 :             : 
     190                 :             : /* A hash function for class im_mem_ref object OBJ.  */
     191                 :             : 
     192                 :             : inline hashval_t
     193                 :     9867936 : mem_ref_hasher::hash (const im_mem_ref *mem)
     194                 :             : {
     195                 :     9867936 :   return mem->hash;
     196                 :             : }
     197                 :             : 
     198                 :             : /* An equality function for class im_mem_ref object MEM1 with
     199                 :             :    memory reference OBJ2.  */
     200                 :             : 
     201                 :             : inline bool
     202                 :    11476740 : mem_ref_hasher::equal (const im_mem_ref *mem1, const ao_ref *obj2)
     203                 :             : {
     204                 :    11476740 :   if (obj2->max_size_known_p ())
     205                 :    10233074 :     return (mem1->ref_decomposed
     206                 :     9808863 :             && ((TREE_CODE (mem1->mem.base) == MEM_REF
     207                 :     4100977 :                  && TREE_CODE (obj2->base) == MEM_REF
     208                 :     2497107 :                  && operand_equal_p (TREE_OPERAND (mem1->mem.base, 0),
     209                 :     2497107 :                                      TREE_OPERAND (obj2->base, 0), 0)
     210                 :    11829032 :                  && known_eq (mem_ref_offset (mem1->mem.base) * BITS_PER_UNIT + mem1->mem.offset,
     211                 :             :                               mem_ref_offset (obj2->base) * BITS_PER_UNIT + obj2->offset))
     212                 :     9449981 :                 || (operand_equal_p (mem1->mem.base, obj2->base, 0)
     213                 :      780779 :                     && known_eq (mem1->mem.offset, obj2->offset)))
     214                 :      823531 :             && known_eq (mem1->mem.size, obj2->size)
     215                 :      820933 :             && known_eq (mem1->mem.max_size, obj2->max_size)
     216                 :      820933 :             && mem1->mem.volatile_p == obj2->volatile_p
     217                 :      820922 :             && (mem1->mem.ref_alias_set == obj2->ref_alias_set
     218                 :             :                 /* We are not canonicalizing alias-sets but for the
     219                 :             :                    special-case we didn't canonicalize yet and the
     220                 :             :                    incoming ref is a alias-set zero MEM we pick
     221                 :             :                    the correct one already.  */
     222                 :        6215 :                 || (!mem1->ref_canonical
     223                 :        5490 :                     && (TREE_CODE (obj2->ref) == MEM_REF
     224                 :        5490 :                         || TREE_CODE (obj2->ref) == TARGET_MEM_REF)
     225                 :        3385 :                     && obj2->ref_alias_set == 0)
     226                 :             :                 /* Likewise if there's a canonical ref with alias-set zero.  */
     227                 :        4913 :                 || (mem1->ref_canonical && mem1->mem.ref_alias_set == 0))
     228                 :    11049103 :             && types_compatible_p (TREE_TYPE (mem1->mem.ref),
     229                 :      816029 :                                    TREE_TYPE (obj2->ref)));
     230                 :             :   else
     231                 :     1243666 :     return operand_equal_p (mem1->mem.ref, obj2->ref, 0);
     232                 :             : }
     233                 :             : 
     234                 :             : 
     235                 :             : /* Description of memory accesses in loops.  */
     236                 :             : 
     237                 :             : static struct
     238                 :             : {
     239                 :             :   /* The hash table of memory references accessed in loops.  */
     240                 :             :   hash_table<mem_ref_hasher> *refs;
     241                 :             : 
     242                 :             :   /* The list of memory references.  */
     243                 :             :   vec<im_mem_ref *> refs_list;
     244                 :             : 
     245                 :             :   /* The set of memory references accessed in each loop.  */
     246                 :             :   vec<bitmap_head> refs_loaded_in_loop;
     247                 :             : 
     248                 :             :   /* The set of memory references stored in each loop.  */
     249                 :             :   vec<bitmap_head> refs_stored_in_loop;
     250                 :             : 
     251                 :             :   /* The set of memory references stored in each loop, including subloops .  */
     252                 :             :   vec<bitmap_head> all_refs_stored_in_loop;
     253                 :             : 
     254                 :             :   /* Cache for expanding memory addresses.  */
     255                 :             :   hash_map<tree, name_expansion *> *ttae_cache;
     256                 :             : } memory_accesses;
     257                 :             : 
     258                 :             : /* Obstack for the bitmaps in the above data structures.  */
     259                 :             : static bitmap_obstack lim_bitmap_obstack;
     260                 :             : static obstack mem_ref_obstack;
     261                 :             : 
     262                 :             : static bool ref_indep_loop_p (class loop *, im_mem_ref *, dep_kind);
     263                 :             : static bool ref_always_accessed_p (class loop *, im_mem_ref *, bool);
     264                 :             : static bool refs_independent_p (im_mem_ref *, im_mem_ref *, bool = true);
     265                 :             : 
     266                 :             : /* Minimum cost of an expensive expression.  */
     267                 :             : #define LIM_EXPENSIVE ((unsigned) param_lim_expensive)
     268                 :             : 
     269                 :             : /* The outermost loop for which execution of the header guarantees that the
     270                 :             :    block will be executed.  */
     271                 :             : #define ALWAYS_EXECUTED_IN(BB) ((class loop *) (BB)->aux)
     272                 :             : #define SET_ALWAYS_EXECUTED_IN(BB, VAL) ((BB)->aux = (void *) (VAL))
     273                 :             : 
     274                 :             : /* ID of the shared unanalyzable mem.  */
     275                 :             : #define UNANALYZABLE_MEM_ID 0
     276                 :             : 
     277                 :             : /* Whether the reference was analyzable.  */
     278                 :             : #define MEM_ANALYZABLE(REF) ((REF)->id != UNANALYZABLE_MEM_ID)
     279                 :             : 
     280                 :             : static struct lim_aux_data *
     281                 :    15844698 : init_lim_data (gimple *stmt)
     282                 :             : {
     283                 :    15844698 :   lim_aux_data *p = XCNEW (struct lim_aux_data);
     284                 :    15844698 :   lim_aux_data_map->put (stmt, p);
     285                 :             : 
     286                 :    15844698 :   return p;
     287                 :             : }
     288                 :             : 
     289                 :             : static struct lim_aux_data *
     290                 :    79439853 : get_lim_data (gimple *stmt)
     291                 :             : {
     292                 :    79439853 :   lim_aux_data **p = lim_aux_data_map->get (stmt);
     293                 :    79439853 :   if (!p)
     294                 :             :     return NULL;
     295                 :             : 
     296                 :    42529565 :   return *p;
     297                 :             : }
     298                 :             : 
     299                 :             : /* Releases the memory occupied by DATA.  */
     300                 :             : 
     301                 :             : static void
     302                 :    15844698 : free_lim_aux_data (struct lim_aux_data *data)
     303                 :             : {
     304                 :           0 :   data->depends.release ();
     305                 :    15844698 :   free (data);
     306                 :           0 : }
     307                 :             : 
     308                 :             : static void
     309                 :    15844698 : clear_lim_data (gimple *stmt)
     310                 :             : {
     311                 :    15844698 :   lim_aux_data **p = lim_aux_data_map->get (stmt);
     312                 :    15844698 :   if (!p)
     313                 :             :     return;
     314                 :             : 
     315                 :    15844698 :   free_lim_aux_data (*p);
     316                 :    15844698 :   *p = NULL;
     317                 :             : }
     318                 :             : 
     319                 :             : 
     320                 :             : /* The possibilities of statement movement.  */
     321                 :             : enum move_pos
     322                 :             :   {
     323                 :             :     MOVE_IMPOSSIBLE,            /* No movement -- side effect expression.  */
     324                 :             :     MOVE_PRESERVE_EXECUTION,    /* Must not cause the non-executed statement
     325                 :             :                                    become executed -- memory accesses, ... */
     326                 :             :     MOVE_POSSIBLE               /* Unlimited movement.  */
     327                 :             :   };
     328                 :             : 
     329                 :             : 
     330                 :             : /* If it is possible to hoist the statement STMT unconditionally,
     331                 :             :    returns MOVE_POSSIBLE.
     332                 :             :    If it is possible to hoist the statement STMT, but we must avoid making
     333                 :             :    it executed if it would not be executed in the original program (e.g.
     334                 :             :    because it may trap), return MOVE_PRESERVE_EXECUTION.
     335                 :             :    Otherwise return MOVE_IMPOSSIBLE.  */
     336                 :             : 
     337                 :             : static enum move_pos
     338                 :    37880467 : movement_possibility_1 (gimple *stmt)
     339                 :             : {
     340                 :    37880467 :   tree lhs;
     341                 :    37880467 :   enum move_pos ret = MOVE_POSSIBLE;
     342                 :             : 
     343                 :    37880467 :   if (flag_unswitch_loops
     344                 :    37880467 :       && gimple_code (stmt) == GIMPLE_COND)
     345                 :             :     {
     346                 :             :       /* If we perform unswitching, force the operands of the invariant
     347                 :             :          condition to be moved out of the loop.  */
     348                 :             :       return MOVE_POSSIBLE;
     349                 :             :     }
     350                 :             : 
     351                 :    37620383 :   if (gimple_code (stmt) == GIMPLE_PHI
     352                 :     2302792 :       && gimple_phi_num_args (stmt) <= 2
     353                 :     4370666 :       && !virtual_operand_p (gimple_phi_result (stmt))
     354                 :    38869810 :       && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt)))
     355                 :             :     return MOVE_POSSIBLE;
     356                 :             : 
     357                 :    36371153 :   if (gimple_get_lhs (stmt) == NULL_TREE)
     358                 :             :     return MOVE_IMPOSSIBLE;
     359                 :             : 
     360                 :    28375002 :   if (gimple_vdef (stmt))
     361                 :             :     return MOVE_IMPOSSIBLE;
     362                 :             : 
     363                 :    11740998 :   if (stmt_ends_bb_p (stmt)
     364                 :    10644338 :       || gimple_has_volatile_ops (stmt)
     365                 :    11669494 :       || gimple_has_side_effects (stmt)
     366                 :    23405550 :       || stmt_could_throw_p (cfun, stmt))
     367                 :      439974 :     return MOVE_IMPOSSIBLE;
     368                 :             : 
     369                 :    11301024 :   if (is_gimple_call (stmt))
     370                 :             :     {
     371                 :             :       /* While pure or const call is guaranteed to have no side effects, we
     372                 :             :          cannot move it arbitrarily.  Consider code like
     373                 :             : 
     374                 :             :          char *s = something ();
     375                 :             : 
     376                 :             :          while (1)
     377                 :             :            {
     378                 :             :              if (s)
     379                 :             :                t = strlen (s);
     380                 :             :              else
     381                 :             :                t = 0;
     382                 :             :            }
     383                 :             : 
     384                 :             :          Here the strlen call cannot be moved out of the loop, even though
     385                 :             :          s is invariant.  In addition to possibly creating a call with
     386                 :             :          invalid arguments, moving out a function call that is not executed
     387                 :             :          may cause performance regressions in case the call is costly and
     388                 :             :          not executed at all.  */
     389                 :      124951 :       ret = MOVE_PRESERVE_EXECUTION;
     390                 :      124951 :       lhs = gimple_call_lhs (stmt);
     391                 :             :     }
     392                 :    11176073 :   else if (is_gimple_assign (stmt))
     393                 :    10122511 :     lhs = gimple_assign_lhs (stmt);
     394                 :             :   else
     395                 :             :     return MOVE_IMPOSSIBLE;
     396                 :             : 
     397                 :    10247462 :   if (TREE_CODE (lhs) == SSA_NAME
     398                 :    10247462 :       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
     399                 :             :     return MOVE_IMPOSSIBLE;
     400                 :             : 
     401                 :    10247275 :   if (TREE_CODE (lhs) != SSA_NAME
     402                 :    10247275 :       || gimple_could_trap_p (stmt))
     403                 :     2118533 :     return MOVE_PRESERVE_EXECUTION;
     404                 :             : 
     405                 :     8128742 :   if (is_gimple_assign (stmt))
     406                 :             :     {
     407                 :     8004439 :       auto code = gimple_assign_rhs_code (stmt);
     408                 :     8004439 :       tree type = TREE_TYPE (gimple_assign_rhs1 (stmt));
     409                 :             :       /* For shifts and rotates and possibly out-of-bound shift operands
     410                 :             :          we currently cannot rewrite them into something unconditionally
     411                 :             :          well-defined.  */
     412                 :     8004439 :       if ((code == LSHIFT_EXPR
     413                 :             :            || code == RSHIFT_EXPR
     414                 :             :            || code == LROTATE_EXPR
     415                 :     8004439 :            || code == RROTATE_EXPR)
     416                 :     8004439 :           && (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST
     417                 :             :               /* We cannot use ranges at 'stmt' here.  */
     418                 :      107733 :               || wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (stmt)),
     419                 :     7969319 :                             element_precision (type))))
     420                 :      142853 :         ret = MOVE_PRESERVE_EXECUTION;
     421                 :             :     }
     422                 :             : 
     423                 :             :   /* Non local loads in a transaction cannot be hoisted out.  Well,
     424                 :             :      unless the load happens on every path out of the loop, but we
     425                 :             :      don't take this into account yet.  */
     426                 :     8128742 :   if (flag_tm
     427                 :         415 :       && gimple_in_transaction (stmt)
     428                 :     8128852 :       && gimple_assign_single_p (stmt))
     429                 :             :     {
     430                 :          18 :       tree rhs = gimple_assign_rhs1 (stmt);
     431                 :          18 :       if (DECL_P (rhs) && is_global_var (rhs))
     432                 :             :         {
     433                 :          18 :           if (dump_file)
     434                 :             :             {
     435                 :           2 :               fprintf (dump_file, "Cannot hoist conditional load of ");
     436                 :           2 :               print_generic_expr (dump_file, rhs, TDF_SLIM);
     437                 :           2 :               fprintf (dump_file, " because it is in a transaction.\n");
     438                 :             :             }
     439                 :          18 :           return MOVE_IMPOSSIBLE;
     440                 :             :         }
     441                 :             :     }
     442                 :             : 
     443                 :             :   return ret;
     444                 :             : }
     445                 :             : 
     446                 :             : static enum move_pos
     447                 :    37880467 : movement_possibility (gimple *stmt)
     448                 :             : {
     449                 :    37880467 :   enum move_pos pos = movement_possibility_1 (stmt);
     450                 :    37880467 :   if (pos == MOVE_POSSIBLE)
     451                 :             :     {
     452                 :     9370882 :       use_operand_p use_p;
     453                 :     9370882 :       ssa_op_iter ssa_iter;
     454                 :    30289667 :       FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, ssa_iter, SSA_OP_USE)
     455                 :    11563519 :         if (TREE_CODE (USE_FROM_PTR (use_p)) == SSA_NAME
     456                 :    11563519 :             && ssa_name_maybe_undef_p (USE_FROM_PTR (use_p)))
     457                 :       15616 :           return MOVE_PRESERVE_EXECUTION;
     458                 :             :     }
     459                 :             :   return pos;
     460                 :             : }
     461                 :             : 
     462                 :             : 
     463                 :             : /* Compare the profile count inequality of bb and loop's preheader, it is
     464                 :             :    three-state as stated in profile-count.h, FALSE is returned if inequality
     465                 :             :    cannot be decided.  */
     466                 :             : bool
     467                 :    12092144 : bb_colder_than_loop_preheader (basic_block bb, class loop *loop)
     468                 :             : {
     469                 :    12092144 :   gcc_assert (bb && loop);
     470                 :    12092144 :   return bb->count < loop_preheader_edge (loop)->src->count;
     471                 :             : }
     472                 :             : 
     473                 :             : /* Check coldest loop between OUTERMOST_LOOP and LOOP by comparing profile
     474                 :             :    count.
     475                 :             :   It does three steps check:
     476                 :             :   1) Check whether CURR_BB is cold in it's own loop_father, if it is cold, just
     477                 :             :   return NULL which means it should not be moved out at all;
     478                 :             :   2) CURR_BB is NOT cold, check if pre-computed COLDEST_LOOP is outside of
     479                 :             :   OUTERMOST_LOOP, if it is inside of OUTERMOST_LOOP, return the COLDEST_LOOP;
     480                 :             :   3) If COLDEST_LOOP is outside of OUTERMOST_LOOP, check whether there is a
     481                 :             :   hotter loop between OUTERMOST_LOOP and loop in pre-computed
     482                 :             :   HOTTER_THAN_INNER_LOOP, return it's nested inner loop, otherwise return
     483                 :             :   OUTERMOST_LOOP.
     484                 :             :   At last, the coldest_loop is inside of OUTERMOST_LOOP, just return it as
     485                 :             :   the hoist target.  */
     486                 :             : 
     487                 :             : static class loop *
     488                 :    10701801 : get_coldest_out_loop (class loop *outermost_loop, class loop *loop,
     489                 :             :                       basic_block curr_bb)
     490                 :             : {
     491                 :    10701801 :   gcc_assert (outermost_loop == loop
     492                 :             :               || flow_loop_nested_p (outermost_loop, loop));
     493                 :             : 
     494                 :             :   /* If bb_colder_than_loop_preheader returns false due to three-state
     495                 :             :     comparision, OUTERMOST_LOOP is returned finally to preserve the behavior.
     496                 :             :     Otherwise, return the coldest loop between OUTERMOST_LOOP and LOOP.  */
     497                 :    10701801 :   if (curr_bb && bb_colder_than_loop_preheader (curr_bb, loop))
     498                 :             :     return NULL;
     499                 :             : 
     500                 :     9839082 :   class loop *coldest_loop = coldest_outermost_loop[loop->num];
     501                 :    29517246 :   if (loop_depth (coldest_loop) < loop_depth (outermost_loop))
     502                 :             :     {
     503                 :      208557 :       class loop *hotter_loop = hotter_than_inner_loop[loop->num];
     504                 :      208557 :       if (!hotter_loop
     505                 :      213659 :           || loop_depth (hotter_loop) < loop_depth (outermost_loop))
     506                 :             :         return outermost_loop;
     507                 :             : 
     508                 :             :       /*  hotter_loop is between OUTERMOST_LOOP and LOOP like:
     509                 :             :         [loop tree root, ..., coldest_loop, ..., outermost_loop, ...,
     510                 :             :         hotter_loop, second_coldest_loop, ..., loop]
     511                 :             :         return second_coldest_loop to be the hoist target.  */
     512                 :           3 :       class loop *aloop;
     513                 :           3 :       for (aloop = hotter_loop->inner; aloop; aloop = aloop->next)
     514                 :           3 :         if (aloop == loop || flow_loop_nested_p (aloop, loop))
     515                 :           3 :           return aloop;
     516                 :             :     }
     517                 :             :   return coldest_loop;
     518                 :             : }
     519                 :             : 
     520                 :             : /* Suppose that operand DEF is used inside the LOOP.  Returns the outermost
     521                 :             :    loop to that we could move the expression using DEF if it did not have
     522                 :             :    other operands, i.e. the outermost loop enclosing LOOP in that the value
     523                 :             :    of DEF is invariant.  */
     524                 :             : 
     525                 :             : static class loop *
     526                 :    16973846 : outermost_invariant_loop (tree def, class loop *loop)
     527                 :             : {
     528                 :    16973846 :   gimple *def_stmt;
     529                 :    16973846 :   basic_block def_bb;
     530                 :    16973846 :   class loop *max_loop;
     531                 :    16973846 :   struct lim_aux_data *lim_data;
     532                 :             : 
     533                 :    16973846 :   if (!def)
     534                 :      857565 :     return superloop_at_depth (loop, 1);
     535                 :             : 
     536                 :    16116281 :   if (TREE_CODE (def) != SSA_NAME)
     537                 :             :     {
     538                 :     3301373 :       gcc_assert (is_gimple_min_invariant (def));
     539                 :     3301373 :       return superloop_at_depth (loop, 1);
     540                 :             :     }
     541                 :             : 
     542                 :    12814908 :   def_stmt = SSA_NAME_DEF_STMT (def);
     543                 :    12814908 :   def_bb = gimple_bb (def_stmt);
     544                 :    12814908 :   if (!def_bb)
     545                 :       62466 :     return superloop_at_depth (loop, 1);
     546                 :             : 
     547                 :    12752442 :   max_loop = find_common_loop (loop, def_bb->loop_father);
     548                 :             : 
     549                 :    12752442 :   lim_data = get_lim_data (def_stmt);
     550                 :    12752442 :   if (lim_data != NULL && lim_data->max_loop != NULL)
     551                 :      612030 :     max_loop = find_common_loop (max_loop,
     552                 :             :                                  loop_outer (lim_data->max_loop));
     553                 :    12752442 :   if (max_loop == loop)
     554                 :             :     return NULL;
     555                 :     1608329 :   max_loop = superloop_at_depth (loop, loop_depth (max_loop) + 1);
     556                 :             : 
     557                 :     1608329 :   return max_loop;
     558                 :             : }
     559                 :             : 
     560                 :             : /* DATA is a structure containing information associated with a statement
     561                 :             :    inside LOOP.  DEF is one of the operands of this statement.
     562                 :             : 
     563                 :             :    Find the outermost loop enclosing LOOP in that value of DEF is invariant
     564                 :             :    and record this in DATA->max_loop field.  If DEF itself is defined inside
     565                 :             :    this loop as well (i.e. we need to hoist it out of the loop if we want
     566                 :             :    to hoist the statement represented by DATA), record the statement in that
     567                 :             :    DEF is defined to the DATA->depends list.  Additionally if ADD_COST is true,
     568                 :             :    add the cost of the computation of DEF to the DATA->cost.
     569                 :             : 
     570                 :             :    If DEF is not invariant in LOOP, return false.  Otherwise return TRUE.  */
     571                 :             : 
     572                 :             : static bool
     573                 :     9931437 : add_dependency (tree def, struct lim_aux_data *data, class loop *loop,
     574                 :             :                 bool add_cost)
     575                 :             : {
     576                 :     9931437 :   gimple *def_stmt = SSA_NAME_DEF_STMT (def);
     577                 :     9931437 :   basic_block def_bb = gimple_bb (def_stmt);
     578                 :     9931437 :   class loop *max_loop;
     579                 :     9931437 :   struct lim_aux_data *def_data;
     580                 :             : 
     581                 :     9931437 :   if (!def_bb)
     582                 :             :     return true;
     583                 :             : 
     584                 :     9678527 :   max_loop = outermost_invariant_loop (def, loop);
     585                 :     9678527 :   if (!max_loop)
     586                 :             :     return false;
     587                 :             : 
     588                 :     1212886 :   if (flow_loop_nested_p (data->max_loop, max_loop))
     589                 :      385605 :     data->max_loop = max_loop;
     590                 :             : 
     591                 :     1212886 :   def_data = get_lim_data (def_stmt);
     592                 :     1212886 :   if (!def_data)
     593                 :             :     return true;
     594                 :             : 
     595                 :      601711 :   if (add_cost
     596                 :             :       /* Only add the cost if the statement defining DEF is inside LOOP,
     597                 :             :          i.e. if it is likely that by moving the invariants dependent
     598                 :             :          on it, we will be able to avoid creating a new register for
     599                 :             :          it (since it will be only used in these dependent invariants).  */
     600                 :      580193 :       && def_bb->loop_father == loop)
     601                 :      410362 :     data->cost += def_data->cost;
     602                 :             : 
     603                 :      601711 :   data->depends.safe_push (def_stmt);
     604                 :             : 
     605                 :      601711 :   return true;
     606                 :             : }
     607                 :             : 
     608                 :             : /* Returns an estimate for a cost of statement STMT.  The values here
     609                 :             :    are just ad-hoc constants, similar to costs for inlining.  */
     610                 :             : 
     611                 :             : static unsigned
     612                 :      667365 : stmt_cost (gimple *stmt)
     613                 :             : {
     614                 :             :   /* Always try to create possibilities for unswitching.  */
     615                 :      667365 :   if (gimple_code (stmt) == GIMPLE_COND
     616                 :      667365 :       || gimple_code (stmt) == GIMPLE_PHI)
     617                 :       11019 :     return LIM_EXPENSIVE;
     618                 :             : 
     619                 :             :   /* We should be hoisting calls if possible.  */
     620                 :      656346 :   if (is_gimple_call (stmt))
     621                 :             :     {
     622                 :         565 :       tree fndecl;
     623                 :             : 
     624                 :             :       /* Unless the call is a builtin_constant_p; this always folds to a
     625                 :             :          constant, so moving it is useless.  */
     626                 :         565 :       fndecl = gimple_call_fndecl (stmt);
     627                 :         565 :       if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_CONSTANT_P))
     628                 :             :         return 0;
     629                 :             : 
     630                 :         565 :       return LIM_EXPENSIVE;
     631                 :             :     }
     632                 :             : 
     633                 :             :   /* Hoisting memory references out should almost surely be a win.  */
     634                 :      655781 :   if (gimple_references_memory_p (stmt))
     635                 :       54635 :     return LIM_EXPENSIVE;
     636                 :             : 
     637                 :      601146 :   if (gimple_code (stmt) != GIMPLE_ASSIGN)
     638                 :             :     return 1;
     639                 :             : 
     640                 :      601146 :   enum tree_code code = gimple_assign_rhs_code (stmt);
     641                 :      601146 :   switch (code)
     642                 :             :     {
     643                 :      103138 :     case MULT_EXPR:
     644                 :      103138 :     case WIDEN_MULT_EXPR:
     645                 :      103138 :     case WIDEN_MULT_PLUS_EXPR:
     646                 :      103138 :     case WIDEN_MULT_MINUS_EXPR:
     647                 :      103138 :     case DOT_PROD_EXPR:
     648                 :      103138 :     case TRUNC_DIV_EXPR:
     649                 :      103138 :     case CEIL_DIV_EXPR:
     650                 :      103138 :     case FLOOR_DIV_EXPR:
     651                 :      103138 :     case ROUND_DIV_EXPR:
     652                 :      103138 :     case EXACT_DIV_EXPR:
     653                 :      103138 :     case CEIL_MOD_EXPR:
     654                 :      103138 :     case FLOOR_MOD_EXPR:
     655                 :      103138 :     case ROUND_MOD_EXPR:
     656                 :      103138 :     case TRUNC_MOD_EXPR:
     657                 :      103138 :     case RDIV_EXPR:
     658                 :             :       /* Division and multiplication are usually expensive.  */
     659                 :      103138 :       return LIM_EXPENSIVE;
     660                 :             : 
     661                 :        3803 :     case LSHIFT_EXPR:
     662                 :        3803 :     case RSHIFT_EXPR:
     663                 :        3803 :     case WIDEN_LSHIFT_EXPR:
     664                 :        3803 :     case LROTATE_EXPR:
     665                 :        3803 :     case RROTATE_EXPR:
     666                 :             :       /* Shifts and rotates are usually expensive.  */
     667                 :        3803 :       return LIM_EXPENSIVE;
     668                 :             : 
     669                 :         560 :     case COND_EXPR:
     670                 :         560 :     case VEC_COND_EXPR:
     671                 :             :       /* Conditionals are expensive.  */
     672                 :         560 :       return LIM_EXPENSIVE;
     673                 :             : 
     674                 :        1733 :     case CONSTRUCTOR:
     675                 :             :       /* Make vector construction cost proportional to the number
     676                 :             :          of elements.  */
     677                 :        1733 :       return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
     678                 :             : 
     679                 :             :     case SSA_NAME:
     680                 :             :     case PAREN_EXPR:
     681                 :             :       /* Whether or not something is wrapped inside a PAREN_EXPR
     682                 :             :          should not change move cost.  Nor should an intermediate
     683                 :             :          unpropagated SSA name copy.  */
     684                 :             :       return 0;
     685                 :             : 
     686                 :      455272 :     default:
     687                 :             :       /* Comparisons are usually expensive.  */
     688                 :      455272 :       if (TREE_CODE_CLASS (code) == tcc_comparison)
     689                 :        7360 :         return LIM_EXPENSIVE;
     690                 :             :       return 1;
     691                 :             :     }
     692                 :             : }
     693                 :             : 
     694                 :             : /* Finds the outermost loop between OUTER and LOOP in that the memory reference
     695                 :             :    REF is independent.  If REF is not independent in LOOP, NULL is returned
     696                 :             :    instead.  */
     697                 :             : 
     698                 :             : static class loop *
     699                 :      701914 : outermost_indep_loop (class loop *outer, class loop *loop, im_mem_ref *ref)
     700                 :             : {
     701                 :      701914 :   class loop *aloop;
     702                 :             : 
     703                 :      701914 :   if (ref->stored && bitmap_bit_p (ref->stored, loop->num))
     704                 :             :     return NULL;
     705                 :             : 
     706                 :       84303 :   for (aloop = outer;
     707                 :      662114 :        aloop != loop;
     708                 :       84303 :        aloop = superloop_at_depth (loop, loop_depth (aloop) + 1))
     709                 :        9248 :     if ((!ref->stored || !bitmap_bit_p (ref->stored, aloop->num))
     710                 :       99060 :         && ref_indep_loop_p (aloop, ref, lim_raw))
     711                 :       13108 :       return aloop;
     712                 :             : 
     713                 :      564703 :   if (ref_indep_loop_p (loop, ref, lim_raw))
     714                 :             :     return loop;
     715                 :             :   else
     716                 :             :     return NULL;
     717                 :             : }
     718                 :             : 
     719                 :             : /* If there is a simple load or store to a memory reference in STMT, returns
     720                 :             :    the location of the memory reference, and sets IS_STORE according to whether
     721                 :             :    it is a store or load.  Otherwise, returns NULL.  */
     722                 :             : 
     723                 :             : static tree *
     724                 :     6602851 : simple_mem_ref_in_stmt (gimple *stmt, bool *is_store)
     725                 :             : {
     726                 :     6602851 :   tree *lhs, *rhs;
     727                 :             : 
     728                 :             :   /* Recognize SSA_NAME = MEM and MEM = (SSA_NAME | invariant) patterns.  */
     729                 :     6602851 :   if (!gimple_assign_single_p (stmt))
     730                 :             :     return NULL;
     731                 :             : 
     732                 :     5393858 :   lhs = gimple_assign_lhs_ptr (stmt);
     733                 :     5393858 :   rhs = gimple_assign_rhs1_ptr (stmt);
     734                 :             : 
     735                 :     8300714 :   if (TREE_CODE (*lhs) == SSA_NAME && gimple_vuse (stmt))
     736                 :             :     {
     737                 :     2906856 :       *is_store = false;
     738                 :     2906856 :       return rhs;
     739                 :             :     }
     740                 :     2487002 :   else if (gimple_vdef (stmt)
     741                 :     2487002 :            && (TREE_CODE (*rhs) == SSA_NAME || is_gimple_min_invariant (*rhs)))
     742                 :             :     {
     743                 :     1875466 :       *is_store = true;
     744                 :     1875466 :       return lhs;
     745                 :             :     }
     746                 :             :   else
     747                 :      611536 :     return NULL;
     748                 :             : }
     749                 :             : 
     750                 :             : /* From a controlling predicate in DOM determine the arguments from
     751                 :             :    the PHI node PHI that are chosen if the predicate evaluates to
     752                 :             :    true and false and store them to *TRUE_ARG_P and *FALSE_ARG_P if
     753                 :             :    they are non-NULL.  Returns true if the arguments can be determined,
     754                 :             :    else return false.  */
     755                 :             : 
     756                 :             : static bool
     757                 :        9893 : extract_true_false_args_from_phi (basic_block dom, gphi *phi,
     758                 :             :                                   tree *true_arg_p, tree *false_arg_p)
     759                 :             : {
     760                 :        9893 :   edge te, fe;
     761                 :        9893 :   if (! extract_true_false_controlled_edges (dom, gimple_bb (phi),
     762                 :             :                                              &te, &fe))
     763                 :             :     return false;
     764                 :             : 
     765                 :        7934 :   if (true_arg_p)
     766                 :         859 :     *true_arg_p = PHI_ARG_DEF (phi, te->dest_idx);
     767                 :        7934 :   if (false_arg_p)
     768                 :         859 :     *false_arg_p = PHI_ARG_DEF (phi, fe->dest_idx);
     769                 :             : 
     770                 :             :   return true;
     771                 :             : }
     772                 :             : 
     773                 :             : /* Determine the outermost loop to that it is possible to hoist a statement
     774                 :             :    STMT and store it to LIM_DATA (STMT)->max_loop.  To do this we determine
     775                 :             :    the outermost loop in that the value computed by STMT is invariant.
     776                 :             :    If MUST_PRESERVE_EXEC is true, additionally choose such a loop that
     777                 :             :    we preserve the fact whether STMT is executed.  It also fills other related
     778                 :             :    information to LIM_DATA (STMT).
     779                 :             : 
     780                 :             :    The function returns false if STMT cannot be hoisted outside of the loop it
     781                 :             :    is defined in, and true otherwise.  */
     782                 :             : 
     783                 :             : static bool
     784                 :    10660545 : determine_max_movement (gimple *stmt, bool must_preserve_exec)
     785                 :             : {
     786                 :    10660545 :   basic_block bb = gimple_bb (stmt);
     787                 :    10660545 :   class loop *loop = bb->loop_father;
     788                 :    10660545 :   class loop *level;
     789                 :    10660545 :   struct lim_aux_data *lim_data = get_lim_data (stmt);
     790                 :    10660545 :   tree val;
     791                 :    10660545 :   ssa_op_iter iter;
     792                 :             : 
     793                 :    10660545 :   if (must_preserve_exec)
     794                 :     1299942 :     level = ALWAYS_EXECUTED_IN (bb);
     795                 :             :   else
     796                 :     9360603 :     level = superloop_at_depth (loop, 1);
     797                 :    10660545 :   lim_data->max_loop = get_coldest_out_loop (level, loop, bb);
     798                 :    10660545 :   if (!lim_data->max_loop)
     799                 :             :     return false;
     800                 :             : 
     801                 :     9799335 :   if (gphi *phi = dyn_cast <gphi *> (stmt))
     802                 :             :     {
     803                 :     1203130 :       use_operand_p use_p;
     804                 :     1203130 :       unsigned min_cost = UINT_MAX;
     805                 :     1203130 :       unsigned total_cost = 0;
     806                 :     1203130 :       struct lim_aux_data *def_data;
     807                 :             : 
     808                 :             :       /* We will end up promoting dependencies to be unconditionally
     809                 :             :          evaluated.  For this reason the PHI cost (and thus the
     810                 :             :          cost we remove from the loop by doing the invariant motion)
     811                 :             :          is that of the cheapest PHI argument dependency chain.  */
     812                 :     1405030 :       FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE)
     813                 :             :         {
     814                 :     1378987 :           val = USE_FROM_PTR (use_p);
     815                 :             : 
     816                 :     1378987 :           if (TREE_CODE (val) != SSA_NAME)
     817                 :             :             {
     818                 :             :               /* Assign const 1 to constants.  */
     819                 :      124435 :               min_cost = MIN (min_cost, 1);
     820                 :      124435 :               total_cost += 1;
     821                 :      124435 :               continue;
     822                 :             :             }
     823                 :     1254552 :           if (!add_dependency (val, lim_data, loop, false))
     824                 :             :             return false;
     825                 :             : 
     826                 :       77465 :           gimple *def_stmt = SSA_NAME_DEF_STMT (val);
     827                 :       77465 :           if (gimple_bb (def_stmt)
     828                 :       77465 :               && gimple_bb (def_stmt)->loop_father == loop)
     829                 :             :             {
     830                 :        2067 :               def_data = get_lim_data (def_stmt);
     831                 :        2067 :               if (def_data)
     832                 :             :                 {
     833                 :        2067 :                   min_cost = MIN (min_cost, def_data->cost);
     834                 :        2067 :                   total_cost += def_data->cost;
     835                 :             :                 }
     836                 :             :             }
     837                 :             :         }
     838                 :             : 
     839                 :       26043 :       min_cost = MIN (min_cost, total_cost);
     840                 :       26043 :       lim_data->cost += min_cost;
     841                 :             : 
     842                 :       26043 :       if (gimple_phi_num_args (phi) > 1)
     843                 :             :         {
     844                 :       24809 :           basic_block dom = get_immediate_dominator (CDI_DOMINATORS, bb);
     845                 :       24809 :           gimple *cond;
     846                 :       49618 :           if (gsi_end_p (gsi_last_bb (dom)))
     847                 :             :             return false;
     848                 :       14061 :           cond = gsi_stmt (gsi_last_bb (dom));
     849                 :       14061 :           if (gimple_code (cond) != GIMPLE_COND)
     850                 :             :             return false;
     851                 :             :           /* Verify that this is an extended form of a diamond and
     852                 :             :              the PHI arguments are completely controlled by the
     853                 :             :              predicate in DOM.  */
     854                 :        9034 :           if (!extract_true_false_args_from_phi (dom, phi, NULL, NULL))
     855                 :             :             return false;
     856                 :             : 
     857                 :             :         /* Check if one of the depedent statement is a vector compare whether
     858                 :             :            the target supports it,  otherwise it's invalid to hoist it out of
     859                 :             :            the gcond it belonged to.  */
     860                 :        7075 :         if (VECTOR_TYPE_P (TREE_TYPE (gimple_cond_lhs (cond))))
     861                 :             :           {
     862                 :           2 :             tree type = TREE_TYPE (gimple_cond_lhs (cond));
     863                 :           2 :             auto code = gimple_cond_code (cond);
     864                 :           2 :             if (!target_supports_op_p (type, code, optab_vector))
     865                 :             :               return false;
     866                 :             :           }
     867                 :             : 
     868                 :             :           /* Fold in dependencies and cost of the condition.  */
     869                 :        8524 :           FOR_EACH_SSA_TREE_OPERAND (val, cond, iter, SSA_OP_USE)
     870                 :             :             {
     871                 :        7587 :               if (!add_dependency (val, lim_data, loop, false))
     872                 :             :                 return false;
     873                 :        1451 :               def_data = get_lim_data (SSA_NAME_DEF_STMT (val));
     874                 :        1451 :               if (def_data)
     875                 :         844 :                 lim_data->cost += def_data->cost;
     876                 :             :             }
     877                 :             : 
     878                 :             :           /* We want to avoid unconditionally executing very expensive
     879                 :             :              operations.  As costs for our dependencies cannot be
     880                 :             :              negative just claim we are not invariand for this case.
     881                 :             :              We also are not sure whether the control-flow inside the
     882                 :             :              loop will vanish.  */
     883                 :         937 :           if (total_cost - min_cost >= 2 * LIM_EXPENSIVE
     884                 :          99 :               && !(min_cost != 0
     885                 :          99 :                    && total_cost / min_cost <= 2))
     886                 :             :             return false;
     887                 :             : 
     888                 :             :           /* Assume that the control-flow in the loop will vanish.
     889                 :             :              ???  We should verify this and not artificially increase
     890                 :             :              the cost if that is not the case.  */
     891                 :         859 :           lim_data->cost += stmt_cost (stmt);
     892                 :             :         }
     893                 :             : 
     894                 :        2093 :       return true;
     895                 :             :     }
     896                 :             : 
     897                 :             :   /* A stmt that receives abnormal edges cannot be hoisted.  */
     898                 :     8596205 :   if (is_a <gcall *> (stmt)
     899                 :     8596205 :       && (gimple_call_flags (stmt) & ECF_RETURNS_TWICE))
     900                 :             :     return false;
     901                 :             : 
     902                 :     9983062 :   FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_USE)
     903                 :     8668431 :     if (!add_dependency (val, lim_data, loop, true))
     904                 :             :       return false;
     905                 :             : 
     906                 :     2619102 :   if (gimple_vuse (stmt))
     907                 :             :     {
     908                 :      702781 :       im_mem_ref *ref
     909                 :      702781 :         = lim_data ? memory_accesses.refs_list[lim_data->ref] : NULL;
     910                 :      702781 :       if (ref
     911                 :      702781 :           && MEM_ANALYZABLE (ref))
     912                 :             :         {
     913                 :      701914 :           lim_data->max_loop = outermost_indep_loop (lim_data->max_loop,
     914                 :             :                                                      loop, ref);
     915                 :      701914 :           if (!lim_data->max_loop)
     916                 :             :             return false;
     917                 :             :         }
     918                 :         867 :       else if (! add_dependency (gimple_vuse (stmt), lim_data, loop, false))
     919                 :             :         return false;
     920                 :             :     }
     921                 :             : 
     922                 :      666506 :   lim_data->cost += stmt_cost (stmt);
     923                 :             : 
     924                 :      666506 :   return true;
     925                 :             : }
     926                 :             : 
     927                 :             : /* Suppose that some statement in ORIG_LOOP is hoisted to the loop LEVEL,
     928                 :             :    and that one of the operands of this statement is computed by STMT.
     929                 :             :    Ensure that STMT (together with all the statements that define its
     930                 :             :    operands) is hoisted at least out of the loop LEVEL.  */
     931                 :             : 
     932                 :             : static void
     933                 :      529589 : set_level (gimple *stmt, class loop *orig_loop, class loop *level)
     934                 :             : {
     935                 :      529589 :   class loop *stmt_loop = gimple_bb (stmt)->loop_father;
     936                 :      529589 :   struct lim_aux_data *lim_data;
     937                 :      529589 :   gimple *dep_stmt;
     938                 :      529589 :   unsigned i;
     939                 :             : 
     940                 :      529589 :   stmt_loop = find_common_loop (orig_loop, stmt_loop);
     941                 :      529589 :   lim_data = get_lim_data (stmt);
     942                 :      529589 :   if (lim_data != NULL && lim_data->tgt_loop != NULL)
     943                 :      113539 :     stmt_loop = find_common_loop (stmt_loop,
     944                 :             :                                   loop_outer (lim_data->tgt_loop));
     945                 :      529589 :   if (flow_loop_nested_p (stmt_loop, level))
     946                 :      529589 :     return;
     947                 :             : 
     948                 :      370988 :   gcc_assert (level == lim_data->max_loop
     949                 :             :               || flow_loop_nested_p (lim_data->max_loop, level));
     950                 :             : 
     951                 :      370988 :   lim_data->tgt_loop = level;
     952                 :      643477 :   FOR_EACH_VEC_ELT (lim_data->depends, i, dep_stmt)
     953                 :      272489 :     set_level (dep_stmt, orig_loop, level);
     954                 :             : }
     955                 :             : 
     956                 :             : /* Determines an outermost loop from that we want to hoist the statement STMT.
     957                 :             :    For now we chose the outermost possible loop.  TODO -- use profiling
     958                 :             :    information to set it more sanely.  */
     959                 :             : 
     960                 :             : static void
     961                 :      254193 : set_profitable_level (gimple *stmt)
     962                 :             : {
     963                 :      254193 :   set_level (stmt, gimple_bb (stmt)->loop_father, get_lim_data (stmt)->max_loop);
     964                 :      254193 : }
     965                 :             : 
     966                 :             : /* Returns true if STMT is a call that has side effects.  */
     967                 :             : 
     968                 :             : static bool
     969                 :    97682934 : nonpure_call_p (gimple *stmt)
     970                 :             : {
     971                 :           0 :   if (gimple_code (stmt) != GIMPLE_CALL)
     972                 :             :     return false;
     973                 :             : 
     974                 :     4899675 :   return gimple_has_side_effects (stmt);
     975                 :             : }
     976                 :             : 
     977                 :             : /* Rewrite a/b to a*(1/b).  Return the invariant stmt to process.  */
     978                 :             : 
     979                 :             : static gimple *
     980                 :          36 : rewrite_reciprocal (gimple_stmt_iterator *bsi)
     981                 :             : {
     982                 :          36 :   gassign *stmt, *stmt1, *stmt2;
     983                 :          36 :   tree name, lhs, type;
     984                 :          36 :   tree real_one;
     985                 :          36 :   gimple_stmt_iterator gsi;
     986                 :             : 
     987                 :          36 :   stmt = as_a <gassign *> (gsi_stmt (*bsi));
     988                 :          36 :   lhs = gimple_assign_lhs (stmt);
     989                 :          36 :   type = TREE_TYPE (lhs);
     990                 :             : 
     991                 :          36 :   real_one = build_one_cst (type);
     992                 :             : 
     993                 :          36 :   name = make_temp_ssa_name (type, NULL, "reciptmp");
     994                 :          72 :   stmt1 = gimple_build_assign (name, RDIV_EXPR, real_one,
     995                 :             :                                gimple_assign_rhs2 (stmt));
     996                 :          36 :   stmt2 = gimple_build_assign (lhs, MULT_EXPR, name,
     997                 :             :                                gimple_assign_rhs1 (stmt));
     998                 :             : 
     999                 :             :   /* Replace division stmt with reciprocal and multiply stmts.
    1000                 :             :      The multiply stmt is not invariant, so update iterator
    1001                 :             :      and avoid rescanning.  */
    1002                 :          36 :   gsi = *bsi;
    1003                 :          36 :   gsi_insert_before (bsi, stmt1, GSI_NEW_STMT);
    1004                 :          36 :   gsi_replace (&gsi, stmt2, true);
    1005                 :             : 
    1006                 :             :   /* Continue processing with invariant reciprocal statement.  */
    1007                 :          36 :   return stmt1;
    1008                 :             : }
    1009                 :             : 
    1010                 :             : /* Check if the pattern at *BSI is a bittest of the form
    1011                 :             :    (A >> B) & 1 != 0 and in this case rewrite it to A & (1 << B) != 0.  */
    1012                 :             : 
    1013                 :             : static gimple *
    1014                 :        8247 : rewrite_bittest (gimple_stmt_iterator *bsi)
    1015                 :             : {
    1016                 :        8247 :   gassign *stmt;
    1017                 :        8247 :   gimple *stmt1;
    1018                 :        8247 :   gassign *stmt2;
    1019                 :        8247 :   gimple *use_stmt;
    1020                 :        8247 :   gcond *cond_stmt;
    1021                 :        8247 :   tree lhs, name, t, a, b;
    1022                 :        8247 :   use_operand_p use;
    1023                 :             : 
    1024                 :        8247 :   stmt = as_a <gassign *> (gsi_stmt (*bsi));
    1025                 :        8247 :   lhs = gimple_assign_lhs (stmt);
    1026                 :             : 
    1027                 :             :   /* Verify that the single use of lhs is a comparison against zero.  */
    1028                 :        8247 :   if (TREE_CODE (lhs) != SSA_NAME
    1029                 :        8247 :       || !single_imm_use (lhs, &use, &use_stmt))
    1030                 :         249 :     return stmt;
    1031                 :       12287 :   cond_stmt = dyn_cast <gcond *> (use_stmt);
    1032                 :        4100 :   if (!cond_stmt)
    1033                 :             :     return stmt;
    1034                 :        4100 :   if (gimple_cond_lhs (cond_stmt) != lhs
    1035                 :        3946 :       || (gimple_cond_code (cond_stmt) != NE_EXPR
    1036                 :        1822 :           && gimple_cond_code (cond_stmt) != EQ_EXPR)
    1037                 :        8046 :       || !integer_zerop (gimple_cond_rhs (cond_stmt)))
    1038                 :         184 :     return stmt;
    1039                 :             : 
    1040                 :             :   /* Get at the operands of the shift.  The rhs is TMP1 & 1.  */
    1041                 :        3916 :   stmt1 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
    1042                 :        3916 :   if (gimple_code (stmt1) != GIMPLE_ASSIGN)
    1043                 :             :     return stmt;
    1044                 :             : 
    1045                 :             :   /* There is a conversion in between possibly inserted by fold.  */
    1046                 :        3848 :   if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt1)))
    1047                 :             :     {
    1048                 :         206 :       t = gimple_assign_rhs1 (stmt1);
    1049                 :         206 :       if (TREE_CODE (t) != SSA_NAME
    1050                 :         206 :           || !has_single_use (t))
    1051                 :             :         return stmt;
    1052                 :         158 :       stmt1 = SSA_NAME_DEF_STMT (t);
    1053                 :         158 :       if (gimple_code (stmt1) != GIMPLE_ASSIGN)
    1054                 :             :         return stmt;
    1055                 :             :     }
    1056                 :             : 
    1057                 :             :   /* Verify that B is loop invariant but A is not.  Verify that with
    1058                 :             :      all the stmt walking we are still in the same loop.  */
    1059                 :        3758 :   if (gimple_assign_rhs_code (stmt1) != RSHIFT_EXPR
    1060                 :        9324 :       || loop_containing_stmt (stmt1) != loop_containing_stmt (stmt))
    1061                 :             :     return stmt;
    1062                 :             : 
    1063                 :        2783 :   a = gimple_assign_rhs1 (stmt1);
    1064                 :        2783 :   b = gimple_assign_rhs2 (stmt1);
    1065                 :             : 
    1066                 :        2783 :   if (outermost_invariant_loop (b, loop_containing_stmt (stmt1)) != NULL
    1067                 :        2843 :       && outermost_invariant_loop (a, loop_containing_stmt (stmt1)) == NULL)
    1068                 :             :     {
    1069                 :          60 :       gimple_stmt_iterator rsi;
    1070                 :             : 
    1071                 :             :       /* 1 << B */
    1072                 :          60 :       t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (a),
    1073                 :             :                        build_int_cst (TREE_TYPE (a), 1), b);
    1074                 :          60 :       name = make_temp_ssa_name (TREE_TYPE (a), NULL, "shifttmp");
    1075                 :          60 :       stmt1 = gimple_build_assign (name, t);
    1076                 :             : 
    1077                 :             :       /* A & (1 << B) */
    1078                 :          60 :       t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (a), a, name);
    1079                 :          60 :       name = make_temp_ssa_name (TREE_TYPE (a), NULL, "shifttmp");
    1080                 :          60 :       stmt2 = gimple_build_assign (name, t);
    1081                 :             : 
    1082                 :             :       /* Replace the SSA_NAME we compare against zero.  Adjust
    1083                 :             :          the type of zero accordingly.  */
    1084                 :          60 :       SET_USE (use, name);
    1085                 :          60 :       gimple_cond_set_rhs (cond_stmt,
    1086                 :          60 :                            build_int_cst_type (TREE_TYPE (name),
    1087                 :          60 :                                                0));
    1088                 :             : 
    1089                 :             :       /* Don't use gsi_replace here, none of the new assignments sets
    1090                 :             :          the variable originally set in stmt.  Move bsi to stmt1, and
    1091                 :             :          then remove the original stmt, so that we get a chance to
    1092                 :             :          retain debug info for it.  */
    1093                 :          60 :       rsi = *bsi;
    1094                 :          60 :       gsi_insert_before (bsi, stmt1, GSI_NEW_STMT);
    1095                 :          60 :       gsi_insert_before (&rsi, stmt2, GSI_SAME_STMT);
    1096                 :          60 :       gimple *to_release = gsi_stmt (rsi);
    1097                 :          60 :       gsi_remove (&rsi, true);
    1098                 :          60 :       release_defs (to_release);
    1099                 :             : 
    1100                 :          60 :       return stmt1;
    1101                 :             :     }
    1102                 :             : 
    1103                 :             :   return stmt;
    1104                 :             : }
    1105                 :             : 
    1106                 :             : /* Determine the outermost loops in that statements in basic block BB are
    1107                 :             :    invariant, and record them to the LIM_DATA associated with the
    1108                 :             :    statements.  */
    1109                 :             : 
    1110                 :             : static void
    1111                 :    12815904 : compute_invariantness (basic_block bb)
    1112                 :             : {
    1113                 :    12815904 :   enum move_pos pos;
    1114                 :    12815904 :   gimple_stmt_iterator bsi;
    1115                 :    12815904 :   gimple *stmt;
    1116                 :    12815904 :   bool maybe_never = ALWAYS_EXECUTED_IN (bb) == NULL;
    1117                 :    12815904 :   class loop *outermost = ALWAYS_EXECUTED_IN (bb);
    1118                 :    12815904 :   struct lim_aux_data *lim_data;
    1119                 :             : 
    1120                 :    12815904 :   if (!loop_outer (bb->loop_father))
    1121                 :     7457870 :     return;
    1122                 :             : 
    1123                 :     5358034 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1124                 :         339 :     fprintf (dump_file, "Basic block %d (loop %d -- depth %d):\n\n",
    1125                 :         339 :              bb->index, bb->loop_father->num, loop_depth (bb->loop_father));
    1126                 :             : 
    1127                 :             :   /* Look at PHI nodes, but only if there is at most two.
    1128                 :             :      ???  We could relax this further by post-processing the inserted
    1129                 :             :      code and transforming adjacent cond-exprs with the same predicate
    1130                 :             :      to control flow again.  */
    1131                 :     5358034 :   bsi = gsi_start_phis (bb);
    1132                 :     5358034 :   if (!gsi_end_p (bsi)
    1133                 :     5358034 :       && ((gsi_next (&bsi), gsi_end_p (bsi))
    1134                 :     1169722 :           || (gsi_next (&bsi), gsi_end_p (bsi))))
    1135                 :     3841249 :     for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    1136                 :             :       {
    1137                 :     2302792 :         stmt = gsi_stmt (bsi);
    1138                 :             : 
    1139                 :     2302792 :         pos = movement_possibility (stmt);
    1140                 :     2302792 :         if (pos == MOVE_IMPOSSIBLE)
    1141                 :     1053562 :           continue;
    1142                 :             : 
    1143                 :     1249230 :         lim_data = get_lim_data (stmt);
    1144                 :     1249230 :         if (! lim_data)
    1145                 :     1249230 :           lim_data = init_lim_data (stmt);
    1146                 :     1249230 :         lim_data->always_executed_in = outermost;
    1147                 :             : 
    1148                 :     1249230 :         if (!determine_max_movement (stmt, false))
    1149                 :             :           {
    1150                 :     1247137 :             lim_data->max_loop = NULL;
    1151                 :     1247137 :             continue;
    1152                 :             :           }
    1153                 :             : 
    1154                 :        2093 :         if (dump_file && (dump_flags & TDF_DETAILS))
    1155                 :             :           {
    1156                 :           2 :             print_gimple_stmt (dump_file, stmt, 2);
    1157                 :           2 :             fprintf (dump_file, "  invariant up to level %d, cost %d.\n\n",
    1158                 :           2 :                      loop_depth (lim_data->max_loop),
    1159                 :             :                      lim_data->cost);
    1160                 :             :           }
    1161                 :             : 
    1162                 :        2093 :         if (lim_data->cost >= LIM_EXPENSIVE)
    1163                 :         859 :           set_profitable_level (stmt);
    1164                 :             :       }
    1165                 :             : 
    1166                 :    46293743 :   for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    1167                 :             :     {
    1168                 :    35577675 :       stmt = gsi_stmt (bsi);
    1169                 :             : 
    1170                 :    35577675 :       pos = movement_possibility (stmt);
    1171                 :    35577675 :       if (pos == MOVE_IMPOSSIBLE)
    1172                 :             :         {
    1173                 :    26210350 :           if (nonpure_call_p (stmt))
    1174                 :             :             {
    1175                 :             :               maybe_never = true;
    1176                 :             :               outermost = NULL;
    1177                 :             :             }
    1178                 :             :           /* Make sure to note always_executed_in for stores to make
    1179                 :             :              store-motion work.  */
    1180                 :    23935739 :           else if (stmt_makes_single_store (stmt))
    1181                 :             :             {
    1182                 :     2476697 :               struct lim_aux_data *lim_data = get_lim_data (stmt);
    1183                 :     2476697 :               if (! lim_data)
    1184                 :           0 :                 lim_data = init_lim_data (stmt);
    1185                 :     2476697 :               lim_data->always_executed_in = outermost;
    1186                 :             :             }
    1187                 :    25070334 :           continue;
    1188                 :    25070334 :         }
    1189                 :             : 
    1190                 :    10507341 :       if (is_gimple_assign (stmt)
    1191                 :    10507341 :           && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
    1192                 :             :               == GIMPLE_BINARY_RHS))
    1193                 :             :         {
    1194                 :     4998553 :           tree op0 = gimple_assign_rhs1 (stmt);
    1195                 :     4998553 :           tree op1 = gimple_assign_rhs2 (stmt);
    1196                 :     9997106 :           class loop *ol1 = outermost_invariant_loop (op1,
    1197                 :             :                                         loop_containing_stmt (stmt));
    1198                 :             : 
    1199                 :             :           /* If divisor is invariant, convert a/b to a*(1/b), allowing reciprocal
    1200                 :             :              to be hoisted out of loop, saving expensive divide.  */
    1201                 :     4998553 :           if (pos == MOVE_POSSIBLE
    1202                 :     4468216 :               && gimple_assign_rhs_code (stmt) == RDIV_EXPR
    1203                 :         560 :               && flag_unsafe_math_optimizations
    1204                 :         378 :               && !flag_trapping_math
    1205                 :         378 :               && ol1 != NULL
    1206                 :     4998592 :               && outermost_invariant_loop (op0, ol1) == NULL)
    1207                 :          36 :             stmt = rewrite_reciprocal (&bsi);
    1208                 :             : 
    1209                 :             :           /* If the shift count is invariant, convert (A >> B) & 1 to
    1210                 :             :              A & (1 << B) allowing the bit mask to be hoisted out of the loop
    1211                 :             :              saving an expensive shift.  */
    1212                 :     4998553 :           if (pos == MOVE_POSSIBLE
    1213                 :     4468216 :               && gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
    1214                 :      182726 :               && integer_onep (op1)
    1215                 :       14354 :               && TREE_CODE (op0) == SSA_NAME
    1216                 :     5012907 :               && has_single_use (op0))
    1217                 :        8247 :             stmt = rewrite_bittest (&bsi);
    1218                 :             :         }
    1219                 :             : 
    1220                 :    10507341 :       lim_data = get_lim_data (stmt);
    1221                 :    10507341 :       if (! lim_data)
    1222                 :     7950895 :         lim_data = init_lim_data (stmt);
    1223                 :    10507341 :       lim_data->always_executed_in = outermost;
    1224                 :             : 
    1225                 :    10507341 :       if (maybe_never && pos == MOVE_PRESERVE_EXECUTION)
    1226                 :     1096026 :         continue;
    1227                 :             : 
    1228                 :     9411315 :       if (!determine_max_movement (stmt, pos == MOVE_PRESERVE_EXECUTION))
    1229                 :             :         {
    1230                 :     8744809 :           lim_data->max_loop = NULL;
    1231                 :     8744809 :           continue;
    1232                 :             :         }
    1233                 :             : 
    1234                 :      666506 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1235                 :             :         {
    1236                 :         273 :           print_gimple_stmt (dump_file, stmt, 2);
    1237                 :         273 :           fprintf (dump_file, "  invariant up to level %d, cost %d.\n\n",
    1238                 :         273 :                    loop_depth (lim_data->max_loop),
    1239                 :             :                    lim_data->cost);
    1240                 :             :         }
    1241                 :             : 
    1242                 :      666506 :       if (lim_data->cost >= LIM_EXPENSIVE)
    1243                 :      253334 :         set_profitable_level (stmt);
    1244                 :             :     }
    1245                 :             : }
    1246                 :             : 
    1247                 :             : /* Hoist the statements in basic block BB out of the loops prescribed by
    1248                 :             :    data stored in LIM_DATA structures associated with each statement.  Callback
    1249                 :             :    for walk_dominator_tree.  */
    1250                 :             : 
    1251                 :             : unsigned int
    1252                 :    12867396 : move_computations_worker (basic_block bb)
    1253                 :             : {
    1254                 :    12867396 :   class loop *level;
    1255                 :    12867396 :   unsigned cost = 0;
    1256                 :    12867396 :   struct lim_aux_data *lim_data;
    1257                 :    12867396 :   unsigned int todo = 0;
    1258                 :             : 
    1259                 :    12867396 :   if (!loop_outer (bb->loop_father))
    1260                 :             :     return todo;
    1261                 :             : 
    1262                 :     9316655 :   for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi); )
    1263                 :             :     {
    1264                 :     3949738 :       gassign *new_stmt;
    1265                 :     3949738 :       gphi *stmt = bsi.phi ();
    1266                 :             : 
    1267                 :     3949738 :       lim_data = get_lim_data (stmt);
    1268                 :     3949738 :       if (lim_data == NULL)
    1269                 :             :         {
    1270                 :     2700508 :           gsi_next (&bsi);
    1271                 :     2700508 :           continue;
    1272                 :             :         }
    1273                 :             : 
    1274                 :     1249230 :       cost = lim_data->cost;
    1275                 :     1249230 :       level = lim_data->tgt_loop;
    1276                 :     1249230 :       clear_lim_data (stmt);
    1277                 :             : 
    1278                 :     1249230 :       if (!level)
    1279                 :             :         {
    1280                 :     1248367 :           gsi_next (&bsi);
    1281                 :     1248367 :           continue;
    1282                 :             :         }
    1283                 :             : 
    1284                 :         863 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1285                 :             :         {
    1286                 :           2 :           fprintf (dump_file, "Moving PHI node\n");
    1287                 :           2 :           print_gimple_stmt (dump_file, stmt, 0);
    1288                 :           2 :           fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
    1289                 :             :                    cost, level->num);
    1290                 :             :         }
    1291                 :             : 
    1292                 :         863 :       if (gimple_phi_num_args (stmt) == 1)
    1293                 :             :         {
    1294                 :           4 :           tree arg = PHI_ARG_DEF (stmt, 0);
    1295                 :           4 :           new_stmt = gimple_build_assign (gimple_phi_result (stmt),
    1296                 :           4 :                                           TREE_CODE (arg), arg);
    1297                 :             :         }
    1298                 :             :       else
    1299                 :             :         {
    1300                 :         859 :           basic_block dom = get_immediate_dominator (CDI_DOMINATORS, bb);
    1301                 :         859 :           gimple *cond = gsi_stmt (gsi_last_bb (dom));
    1302                 :         859 :           tree arg0 = NULL_TREE, arg1 = NULL_TREE, t;
    1303                 :             :           /* Get the PHI arguments corresponding to the true and false
    1304                 :             :              edges of COND.  */
    1305                 :         859 :           extract_true_false_args_from_phi (dom, stmt, &arg0, &arg1);
    1306                 :         859 :           gcc_assert (arg0 && arg1);
    1307                 :         859 :           t = make_ssa_name (boolean_type_node);
    1308                 :         859 :           new_stmt = gimple_build_assign (t, gimple_cond_code (cond),
    1309                 :             :                                           gimple_cond_lhs (cond),
    1310                 :             :                                           gimple_cond_rhs (cond));
    1311                 :         859 :           gsi_insert_on_edge (loop_preheader_edge (level), new_stmt);
    1312                 :         859 :           new_stmt = gimple_build_assign (gimple_phi_result (stmt),
    1313                 :             :                                           COND_EXPR, t, arg0, arg1);
    1314                 :         859 :           todo |= TODO_cleanup_cfg;
    1315                 :             :         }
    1316                 :         863 :       if (!ALWAYS_EXECUTED_IN (bb)
    1317                 :         863 :           || (ALWAYS_EXECUTED_IN (bb) != level
    1318                 :         102 :               && !flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb), level)))
    1319                 :         425 :         reset_flow_sensitive_info (gimple_assign_lhs (new_stmt));
    1320                 :         863 :       gsi_insert_on_edge (loop_preheader_edge (level), new_stmt);
    1321                 :         863 :       remove_phi_node (&bsi, false);
    1322                 :             :     }
    1323                 :             : 
    1324                 :    46378886 :   for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi); )
    1325                 :             :     {
    1326                 :    35645052 :       edge e;
    1327                 :             : 
    1328                 :    35645052 :       gimple *stmt = gsi_stmt (bsi);
    1329                 :             : 
    1330                 :    35645052 :       lim_data = get_lim_data (stmt);
    1331                 :    35645052 :       if (lim_data == NULL)
    1332                 :             :         {
    1333                 :    21049584 :           gsi_next (&bsi);
    1334                 :    21049584 :           continue;
    1335                 :             :         }
    1336                 :             : 
    1337                 :    14595468 :       cost = lim_data->cost;
    1338                 :    14595468 :       level = lim_data->tgt_loop;
    1339                 :    14595468 :       clear_lim_data (stmt);
    1340                 :             : 
    1341                 :    14595468 :       if (!level)
    1342                 :             :         {
    1343                 :    14183665 :           gsi_next (&bsi);
    1344                 :    14183665 :           continue;
    1345                 :             :         }
    1346                 :             : 
    1347                 :             :       /* We do not really want to move conditionals out of the loop; we just
    1348                 :             :          placed it here to force its operands to be moved if necessary.  */
    1349                 :      411803 :       if (gimple_code (stmt) == GIMPLE_COND)
    1350                 :             :         {
    1351                 :       10160 :           gsi_next (&bsi);
    1352                 :       10160 :           continue;
    1353                 :             :         }
    1354                 :             : 
    1355                 :      401643 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1356                 :             :         {
    1357                 :         303 :           fprintf (dump_file, "Moving statement\n");
    1358                 :         303 :           print_gimple_stmt (dump_file, stmt, 0);
    1359                 :         303 :           fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
    1360                 :             :                    cost, level->num);
    1361                 :             :         }
    1362                 :             : 
    1363                 :      401643 :       e = loop_preheader_edge (level);
    1364                 :      803286 :       gcc_assert (!gimple_vdef (stmt));
    1365                 :      803286 :       if (gimple_vuse (stmt))
    1366                 :             :         {
    1367                 :             :           /* The new VUSE is the one from the virtual PHI in the loop
    1368                 :             :              header or the one already present.  */
    1369                 :       69917 :           gphi_iterator gsi2;
    1370                 :       69917 :           for (gsi2 = gsi_start_phis (e->dest);
    1371                 :      161896 :                !gsi_end_p (gsi2); gsi_next (&gsi2))
    1372                 :             :             {
    1373                 :      148736 :               gphi *phi = gsi2.phi ();
    1374                 :      297472 :               if (virtual_operand_p (gimple_phi_result (phi)))
    1375                 :             :                 {
    1376                 :       56757 :                   SET_USE (gimple_vuse_op (stmt),
    1377                 :             :                            PHI_ARG_DEF_FROM_EDGE (phi, e));
    1378                 :       56757 :                   break;
    1379                 :             :                 }
    1380                 :             :             }
    1381                 :             :         }
    1382                 :      401643 :       gsi_remove (&bsi, false);
    1383                 :      401643 :       if (gimple_has_lhs (stmt)
    1384                 :      401643 :           && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME
    1385                 :      360486 :           && (!ALWAYS_EXECUTED_IN (bb)
    1386                 :      290749 :               || !(ALWAYS_EXECUTED_IN (bb) == level
    1387                 :       83984 :                    || flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb), level))))
    1388                 :      188238 :         reset_flow_sensitive_info (gimple_get_lhs (stmt));
    1389                 :             :       /* In case this is a stmt that is not unconditionally executed
    1390                 :             :          when the target loop header is executed and the stmt may
    1391                 :             :          invoke undefined integer or pointer overflow rewrite it to
    1392                 :             :          unsigned arithmetic.  */
    1393                 :      401643 :       if (is_gimple_assign (stmt)
    1394                 :      401078 :           && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt)))
    1395                 :      660100 :           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (gimple_assign_lhs (stmt)))
    1396                 :      110474 :           && arith_code_with_undefined_signed_overflow
    1397                 :      110474 :                (gimple_assign_rhs_code (stmt))
    1398                 :      779440 :           && (!ALWAYS_EXECUTED_IN (bb)
    1399                 :       48184 :               || !(ALWAYS_EXECUTED_IN (bb) == level
    1400                 :       13206 :                    || flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb), level))))
    1401                 :       18767 :         gsi_insert_seq_on_edge (e, rewrite_to_defined_overflow (stmt));
    1402                 :             :       else
    1403                 :      382876 :         gsi_insert_on_edge (e, stmt);
    1404                 :             :     }
    1405                 :             : 
    1406                 :     5366917 :   return todo;
    1407                 :             : }
    1408                 :             : 
    1409                 :             : /* Checks whether the statement defining variable *INDEX can be hoisted
    1410                 :             :    out of the loop passed in DATA.  Callback for for_each_index.  */
    1411                 :             : 
    1412                 :             : static bool
    1413                 :     1434206 : may_move_till (tree ref, tree *index, void *data)
    1414                 :             : {
    1415                 :     1434206 :   class loop *loop = (class loop *) data, *max_loop;
    1416                 :             : 
    1417                 :             :   /* If REF is an array reference, check also that the step and the lower
    1418                 :             :      bound is invariant in LOOP.  */
    1419                 :     1434206 :   if (TREE_CODE (ref) == ARRAY_REF)
    1420                 :             :     {
    1421                 :      429839 :       tree step = TREE_OPERAND (ref, 3);
    1422                 :      429839 :       tree lbound = TREE_OPERAND (ref, 2);
    1423                 :             : 
    1424                 :      429839 :       max_loop = outermost_invariant_loop (step, loop);
    1425                 :      429839 :       if (!max_loop)
    1426                 :             :         return false;
    1427                 :             : 
    1428                 :      429839 :       max_loop = outermost_invariant_loop (lbound, loop);
    1429                 :      429839 :       if (!max_loop)
    1430                 :             :         return false;
    1431                 :             :     }
    1432                 :             : 
    1433                 :     1434206 :   max_loop = outermost_invariant_loop (*index, loop);
    1434                 :     1434206 :   if (!max_loop)
    1435                 :             :     return false;
    1436                 :             : 
    1437                 :             :   return true;
    1438                 :             : }
    1439                 :             : 
    1440                 :             : /* If OP is SSA NAME, force the statement that defines it to be
    1441                 :             :    moved out of the LOOP.  ORIG_LOOP is the loop in that EXPR is used.  */
    1442                 :             : 
    1443                 :             : static void
    1444                 :       39423 : force_move_till_op (tree op, class loop *orig_loop, class loop *loop)
    1445                 :             : {
    1446                 :       39423 :   gimple *stmt;
    1447                 :             : 
    1448                 :       39423 :   if (!op
    1449                 :       39423 :       || is_gimple_min_invariant (op))
    1450                 :       35048 :     return;
    1451                 :             : 
    1452                 :        4375 :   gcc_assert (TREE_CODE (op) == SSA_NAME);
    1453                 :             : 
    1454                 :        4375 :   stmt = SSA_NAME_DEF_STMT (op);
    1455                 :        4375 :   if (gimple_nop_p (stmt))
    1456                 :             :     return;
    1457                 :             : 
    1458                 :        2907 :   set_level (stmt, orig_loop, loop);
    1459                 :             : }
    1460                 :             : 
    1461                 :             : /* Forces statement defining invariants in REF (and *INDEX) to be moved out of
    1462                 :             :    the LOOP.  The reference REF is used in the loop ORIG_LOOP.  Callback for
    1463                 :             :    for_each_index.  */
    1464                 :             : 
    1465                 :             : struct fmt_data
    1466                 :             : {
    1467                 :             :   class loop *loop;
    1468                 :             :   class loop *orig_loop;
    1469                 :             : };
    1470                 :             : 
    1471                 :             : static bool
    1472                 :       20387 : force_move_till (tree ref, tree *index, void *data)
    1473                 :             : {
    1474                 :       20387 :   struct fmt_data *fmt_data = (struct fmt_data *) data;
    1475                 :             : 
    1476                 :       20387 :   if (TREE_CODE (ref) == ARRAY_REF)
    1477                 :             :     {
    1478                 :        9518 :       tree step = TREE_OPERAND (ref, 3);
    1479                 :        9518 :       tree lbound = TREE_OPERAND (ref, 2);
    1480                 :             : 
    1481                 :        9518 :       force_move_till_op (step, fmt_data->orig_loop, fmt_data->loop);
    1482                 :        9518 :       force_move_till_op (lbound, fmt_data->orig_loop, fmt_data->loop);
    1483                 :             :     }
    1484                 :             : 
    1485                 :       20387 :   force_move_till_op (*index, fmt_data->orig_loop, fmt_data->loop);
    1486                 :             : 
    1487                 :       20387 :   return true;
    1488                 :             : }
    1489                 :             : 
    1490                 :             : /* A function to free the mem_ref object OBJ.  */
    1491                 :             : 
    1492                 :             : static void
    1493                 :     4950259 : memref_free (class im_mem_ref *mem)
    1494                 :             : {
    1495                 :           0 :   mem->accesses_in_loop.release ();
    1496                 :           0 : }
    1497                 :             : 
    1498                 :             : /* Allocates and returns a memory reference description for MEM whose hash
    1499                 :             :    value is HASH and id is ID.  */
    1500                 :             : 
    1501                 :             : static im_mem_ref *
    1502                 :     4950259 : mem_ref_alloc (ao_ref *mem, unsigned hash, unsigned id)
    1503                 :             : {
    1504                 :     4950259 :   im_mem_ref *ref = XOBNEW (&mem_ref_obstack, class im_mem_ref);
    1505                 :     4950259 :   if (mem)
    1506                 :     3898121 :     ref->mem = *mem;
    1507                 :             :   else
    1508                 :     1052138 :     ao_ref_init (&ref->mem, error_mark_node);
    1509                 :     4950259 :   ref->id = id;
    1510                 :     4950259 :   ref->ref_canonical = false;
    1511                 :     4950259 :   ref->ref_decomposed = false;
    1512                 :     4950259 :   ref->hash = hash;
    1513                 :     4950259 :   ref->stored = NULL;
    1514                 :     4950259 :   ref->loaded = NULL;
    1515                 :     4950259 :   bitmap_initialize (&ref->dep_loop, &lim_bitmap_obstack);
    1516                 :     4950259 :   ref->accesses_in_loop.create (1);
    1517                 :             : 
    1518                 :     4950259 :   return ref;
    1519                 :             : }
    1520                 :             : 
    1521                 :             : /* Records memory reference location *LOC in LOOP to the memory reference
    1522                 :             :    description REF.  The reference occurs in statement STMT.  */
    1523                 :             : 
    1524                 :             : static void
    1525                 :     5393858 : record_mem_ref_loc (im_mem_ref *ref, gimple *stmt, tree *loc)
    1526                 :             : {
    1527                 :     5393858 :   mem_ref_loc aref;
    1528                 :     5393858 :   aref.stmt = stmt;
    1529                 :     5393858 :   aref.ref = loc;
    1530                 :           0 :   ref->accesses_in_loop.safe_push (aref);
    1531                 :           0 : }
    1532                 :             : 
    1533                 :             : /* Set the LOOP bit in REF stored bitmap and allocate that if
    1534                 :             :    necessary.  Return whether a bit was changed.  */
    1535                 :             : 
    1536                 :             : static bool
    1537                 :     4309369 : set_ref_stored_in_loop (im_mem_ref *ref, class loop *loop)
    1538                 :             : {
    1539                 :     4309369 :   if (!ref->stored)
    1540                 :     2493445 :     ref->stored = BITMAP_ALLOC (&lim_bitmap_obstack);
    1541                 :     4309369 :   return bitmap_set_bit (ref->stored, loop->num);
    1542                 :             : }
    1543                 :             : 
    1544                 :             : /* Marks reference REF as stored in LOOP.  */
    1545                 :             : 
    1546                 :             : static void
    1547                 :     3618145 : mark_ref_stored (im_mem_ref *ref, class loop *loop)
    1548                 :             : {
    1549                 :     3618145 :   while (loop != current_loops->tree_root
    1550                 :     6948195 :          && set_ref_stored_in_loop (ref, loop))
    1551                 :     3330050 :     loop = loop_outer (loop);
    1552                 :     3618145 : }
    1553                 :             : 
    1554                 :             : /* Set the LOOP bit in REF loaded bitmap and allocate that if
    1555                 :             :    necessary.  Return whether a bit was changed.  */
    1556                 :             : 
    1557                 :             : static bool
    1558                 :     5839226 : set_ref_loaded_in_loop (im_mem_ref *ref, class loop *loop)
    1559                 :             : {
    1560                 :     5839226 :   if (!ref->loaded)
    1561                 :     3257180 :     ref->loaded = BITMAP_ALLOC (&lim_bitmap_obstack);
    1562                 :     5839226 :   return bitmap_set_bit (ref->loaded, loop->num);
    1563                 :             : }
    1564                 :             : 
    1565                 :             : /* Marks reference REF as loaded in LOOP.  */
    1566                 :             : 
    1567                 :             : static void
    1568                 :     4727385 : mark_ref_loaded (im_mem_ref *ref, class loop *loop)
    1569                 :             : {
    1570                 :     4727385 :   while (loop != current_loops->tree_root
    1571                 :     9289603 :          && set_ref_loaded_in_loop (ref, loop))
    1572                 :     4562218 :     loop = loop_outer (loop);
    1573                 :     4727385 : }
    1574                 :             : 
    1575                 :             : /* Gathers memory references in statement STMT in LOOP, storing the
    1576                 :             :    information about them in the memory_accesses structure.  Marks
    1577                 :             :    the vops accessed through unrecognized statements there as
    1578                 :             :    well.  */
    1579                 :             : 
    1580                 :             : static void
    1581                 :    35577579 : gather_mem_refs_stmt (class loop *loop, gimple *stmt)
    1582                 :             : {
    1583                 :    35577579 :   tree *mem = NULL;
    1584                 :    35577579 :   hashval_t hash;
    1585                 :    35577579 :   im_mem_ref **slot;
    1586                 :    35577579 :   im_mem_ref *ref;
    1587                 :    35577579 :   bool is_stored;
    1588                 :    35577579 :   unsigned id;
    1589                 :             : 
    1590                 :    49895029 :   if (!gimple_vuse (stmt))
    1591                 :             :     return;
    1592                 :             : 
    1593                 :     6602851 :   mem = simple_mem_ref_in_stmt (stmt, &is_stored);
    1594                 :     6602851 :   if (!mem && is_gimple_assign (stmt))
    1595                 :             :     {
    1596                 :             :       /* For aggregate copies record distinct references but use them
    1597                 :             :          only for disambiguation purposes.  */
    1598                 :      611536 :       id = memory_accesses.refs_list.length ();
    1599                 :      611536 :       ref = mem_ref_alloc (NULL, 0, id);
    1600                 :      611536 :       memory_accesses.refs_list.safe_push (ref);
    1601                 :      611536 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1602                 :             :         {
    1603                 :          28 :           fprintf (dump_file, "Unhandled memory reference %u: ", id);
    1604                 :          28 :           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    1605                 :             :         }
    1606                 :      611536 :       record_mem_ref_loc (ref, stmt, mem);
    1607                 :     1223072 :       is_stored = gimple_vdef (stmt);
    1608                 :             :     }
    1609                 :     5991315 :   else if (!mem)
    1610                 :             :     {
    1611                 :             :       /* We use the shared mem_ref for all unanalyzable refs.  */
    1612                 :     1208993 :       id = UNANALYZABLE_MEM_ID;
    1613                 :     1208993 :       ref = memory_accesses.refs_list[id];
    1614                 :     1208993 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1615                 :             :         {
    1616                 :           9 :           fprintf (dump_file, "Unanalyzed memory reference %u: ", id);
    1617                 :           9 :           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    1618                 :             :         }
    1619                 :     2417986 :       is_stored = gimple_vdef (stmt);
    1620                 :             :     }
    1621                 :             :   else
    1622                 :             :     {
    1623                 :             :       /* We are looking for equal refs that might differ in structure
    1624                 :             :          such as a.b vs. MEM[&a + 4].  So we key off the ao_ref but
    1625                 :             :          make sure we can canonicalize the ref in the hashtable if
    1626                 :             :          non-operand_equal_p refs are found.  For the lookup we mark
    1627                 :             :          the case we want strict equality with aor.max_size == -1.  */
    1628                 :     4782322 :       ao_ref aor;
    1629                 :     4782322 :       ao_ref_init (&aor, *mem);
    1630                 :     4782322 :       ao_ref_base (&aor);
    1631                 :     4782322 :       ao_ref_alias_set (&aor);
    1632                 :     4782322 :       HOST_WIDE_INT offset, size, max_size;
    1633                 :     4782322 :       poly_int64 saved_maxsize = aor.max_size, mem_off;
    1634                 :     4782322 :       tree mem_base;
    1635                 :     4782322 :       bool ref_decomposed;
    1636                 :     4782322 :       if (aor.max_size_known_p ()
    1637                 :     4630527 :           && aor.offset.is_constant (&offset)
    1638                 :     4630527 :           && aor.size.is_constant (&size)
    1639                 :     4630527 :           && aor.max_size.is_constant (&max_size)
    1640                 :     4630527 :           && size == max_size
    1641                 :     4077207 :           && (size % BITS_PER_UNIT) == 0
    1642                 :             :           /* We're canonicalizing to a MEM where TYPE_SIZE specifies the
    1643                 :             :              size.  Make sure this is consistent with the extraction.  */
    1644                 :     4063953 :           && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (*mem)))
    1645                 :     8846275 :           && known_eq (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (*mem))),
    1646                 :             :                        aor.size)
    1647                 :     8846174 :           && (mem_base = get_addr_base_and_unit_offset (aor.ref, &mem_off)))
    1648                 :             :         {
    1649                 :     4062817 :           ref_decomposed = true;
    1650                 :     4062817 :           tree base = ao_ref_base (&aor);
    1651                 :     4062817 :           poly_int64 moffset;
    1652                 :     4062817 :           HOST_WIDE_INT mcoffset;
    1653                 :     4062817 :           if (TREE_CODE (base) == MEM_REF
    1654                 :     1699297 :               && (mem_ref_offset (base) * BITS_PER_UNIT + offset).to_shwi (&moffset)
    1655                 :     4062817 :               && moffset.is_constant (&mcoffset))
    1656                 :             :             {
    1657                 :     1699292 :               hash = iterative_hash_expr (TREE_OPERAND (base, 0), 0);
    1658                 :     1699292 :               hash = iterative_hash_host_wide_int (mcoffset, hash);
    1659                 :             :             }
    1660                 :             :           else
    1661                 :             :             {
    1662                 :     2363525 :               hash = iterative_hash_expr (base, 0);
    1663                 :     2363525 :               hash = iterative_hash_host_wide_int (offset, hash);
    1664                 :             :             }
    1665                 :     4062817 :           hash = iterative_hash_host_wide_int (size, hash);
    1666                 :             :         }
    1667                 :             :       else
    1668                 :             :         {
    1669                 :      719505 :           ref_decomposed = false;
    1670                 :      719505 :           hash = iterative_hash_expr (aor.ref, 0);
    1671                 :      719505 :           aor.max_size = -1;
    1672                 :             :         }
    1673                 :     4782322 :       slot = memory_accesses.refs->find_slot_with_hash (&aor, hash, INSERT);
    1674                 :     4782322 :       aor.max_size = saved_maxsize;
    1675                 :     4782322 :       if (*slot)
    1676                 :             :         {
    1677                 :      884201 :           if (!(*slot)->ref_canonical 
    1678                 :      884201 :               && !operand_equal_p (*mem, (*slot)->mem.ref, 0))
    1679                 :             :             {
    1680                 :             :               /* If we didn't yet canonicalize the hashtable ref (which
    1681                 :             :                  we'll end up using for code insertion) and hit a second
    1682                 :             :                  equal ref that is not structurally equivalent create
    1683                 :             :                  a canonical ref which is a bare MEM_REF.  */
    1684                 :       80358 :               if (TREE_CODE (*mem) == MEM_REF
    1685                 :       80358 :                   || TREE_CODE (*mem) == TARGET_MEM_REF)
    1686                 :             :                 {
    1687                 :       22267 :                   (*slot)->mem.ref = *mem;
    1688                 :       22267 :                   (*slot)->mem.base_alias_set = ao_ref_base_alias_set (&aor);
    1689                 :             :                 }
    1690                 :             :               else
    1691                 :             :                 {
    1692                 :       58091 :                   tree ref_alias_type = reference_alias_ptr_type (*mem);
    1693                 :       58091 :                   unsigned int ref_align = get_object_alignment (*mem);
    1694                 :       58091 :                   tree ref_type = TREE_TYPE (*mem);
    1695                 :       58091 :                   tree tmp = build1 (ADDR_EXPR, ptr_type_node,
    1696                 :             :                                      unshare_expr (mem_base));
    1697                 :       58091 :                   if (TYPE_ALIGN (ref_type) != ref_align)
    1698                 :       22188 :                     ref_type = build_aligned_type (ref_type, ref_align);
    1699                 :       58091 :                   tree new_ref
    1700                 :       58091 :                     = fold_build2 (MEM_REF, ref_type, tmp,
    1701                 :             :                                    build_int_cst (ref_alias_type, mem_off));
    1702                 :       58091 :                   if ((*slot)->mem.volatile_p)
    1703                 :        1590 :                     TREE_THIS_VOLATILE (new_ref) = 1;
    1704                 :       58091 :                   (*slot)->mem.ref = new_ref;
    1705                 :             :                   /* Make sure the recorded base and offset are consistent
    1706                 :             :                      with the newly built ref.  */
    1707                 :       58091 :                   if (TREE_CODE (TREE_OPERAND (new_ref, 0)) == ADDR_EXPR)
    1708                 :             :                     ;
    1709                 :             :                   else
    1710                 :             :                     {
    1711                 :       25190 :                       (*slot)->mem.base = new_ref;
    1712                 :       25190 :                       (*slot)->mem.offset = 0;
    1713                 :             :                     }
    1714                 :       58091 :                   gcc_checking_assert (TREE_CODE ((*slot)->mem.ref) == MEM_REF
    1715                 :             :                                        && is_gimple_mem_ref_addr
    1716                 :             :                                             (TREE_OPERAND ((*slot)->mem.ref,
    1717                 :             :                                                            0)));
    1718                 :       58091 :                   (*slot)->mem.base_alias_set = (*slot)->mem.ref_alias_set;
    1719                 :             :                 }
    1720                 :       80358 :               (*slot)->ref_canonical = true;
    1721                 :             :             }
    1722                 :      884201 :           ref = *slot;
    1723                 :      884201 :           id = ref->id;
    1724                 :             :         }
    1725                 :             :       else
    1726                 :             :         {
    1727                 :     3898121 :           id = memory_accesses.refs_list.length ();
    1728                 :     3898121 :           ref = mem_ref_alloc (&aor, hash, id);
    1729                 :     3898121 :           ref->ref_decomposed = ref_decomposed;
    1730                 :     3898121 :           memory_accesses.refs_list.safe_push (ref);
    1731                 :     3898121 :           *slot = ref;
    1732                 :             : 
    1733                 :     3898121 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1734                 :             :             {
    1735                 :         438 :               fprintf (dump_file, "Memory reference %u: ", id);
    1736                 :         438 :               print_generic_expr (dump_file, ref->mem.ref, TDF_SLIM);
    1737                 :         438 :               fprintf (dump_file, "\n");
    1738                 :             :             }
    1739                 :             :         }
    1740                 :             : 
    1741                 :     4782322 :       record_mem_ref_loc (ref, stmt, mem);
    1742                 :             :     }
    1743                 :     6602851 :   if (is_stored)
    1744                 :             :     {
    1745                 :     3618145 :       bitmap_set_bit (&memory_accesses.refs_stored_in_loop[loop->num], ref->id);
    1746                 :     3618145 :       mark_ref_stored (ref, loop);
    1747                 :             :     }
    1748                 :             :   /* A not simple memory op is also a read when it is a write.  */
    1749                 :     6602851 :   if (!is_stored || id == UNANALYZABLE_MEM_ID
    1750                 :     2487002 :       || ref->mem.ref == error_mark_node)
    1751                 :             :     {
    1752                 :     4727385 :       bitmap_set_bit (&memory_accesses.refs_loaded_in_loop[loop->num], ref->id);
    1753                 :     4727385 :       mark_ref_loaded (ref, loop);
    1754                 :             :     }
    1755                 :     6602851 :   init_lim_data (stmt)->ref = ref->id;
    1756                 :     6602851 :   return;
    1757                 :             : }
    1758                 :             : 
    1759                 :             : static unsigned *bb_loop_postorder;
    1760                 :             : 
    1761                 :             : /* qsort sort function to sort blocks after their loop fathers postorder.  */
    1762                 :             : 
    1763                 :             : static int
    1764                 :   139017863 : sort_bbs_in_loop_postorder_cmp (const void *bb1_, const void *bb2_,
    1765                 :             :                                 void *bb_loop_postorder_)
    1766                 :             : {
    1767                 :   139017863 :   unsigned *bb_loop_postorder = (unsigned *)bb_loop_postorder_;
    1768                 :   139017863 :   basic_block bb1 = *(const basic_block *)bb1_;
    1769                 :   139017863 :   basic_block bb2 = *(const basic_block *)bb2_;
    1770                 :   139017863 :   class loop *loop1 = bb1->loop_father;
    1771                 :   139017863 :   class loop *loop2 = bb2->loop_father;
    1772                 :   139017863 :   if (loop1->num == loop2->num)
    1773                 :    69693144 :     return bb1->index - bb2->index;
    1774                 :    69324719 :   return bb_loop_postorder[loop1->num] < bb_loop_postorder[loop2->num] ? -1 : 1;
    1775                 :             : }
    1776                 :             : 
    1777                 :             : /* qsort sort function to sort ref locs after their loop fathers postorder.  */
    1778                 :             : 
    1779                 :             : static int
    1780                 :      884186 : sort_locs_in_loop_postorder_cmp (const void *loc1_, const void *loc2_,
    1781                 :             :                                  void *bb_loop_postorder_)
    1782                 :             : {
    1783                 :      884186 :   unsigned *bb_loop_postorder = (unsigned *)bb_loop_postorder_;
    1784                 :      884186 :   const mem_ref_loc *loc1 = (const mem_ref_loc *)loc1_;
    1785                 :      884186 :   const mem_ref_loc *loc2 = (const mem_ref_loc *)loc2_;
    1786                 :      884186 :   class loop *loop1 = gimple_bb (loc1->stmt)->loop_father;
    1787                 :      884186 :   class loop *loop2 = gimple_bb (loc2->stmt)->loop_father;
    1788                 :      884186 :   if (loop1->num == loop2->num)
    1789                 :             :     return 0;
    1790                 :      121812 :   return bb_loop_postorder[loop1->num] < bb_loop_postorder[loop2->num] ? -1 : 1;
    1791                 :             : }
    1792                 :             : 
    1793                 :             : /* Gathers memory references in loops.  */
    1794                 :             : 
    1795                 :             : static void
    1796                 :      440602 : analyze_memory_references (bool store_motion)
    1797                 :             : {
    1798                 :      440602 :   gimple_stmt_iterator bsi;
    1799                 :      440602 :   basic_block bb, *bbs;
    1800                 :      440602 :   class loop *outer;
    1801                 :      440602 :   unsigned i, n;
    1802                 :             : 
    1803                 :             :   /* Collect all basic-blocks in loops and sort them after their
    1804                 :             :      loops postorder.  */
    1805                 :      440602 :   i = 0;
    1806                 :      440602 :   bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
    1807                 :    13256506 :   FOR_EACH_BB_FN (bb, cfun)
    1808                 :    12815904 :     if (bb->loop_father != current_loops->tree_root)
    1809                 :     5358034 :       bbs[i++] = bb;
    1810                 :      440602 :   n = i;
    1811                 :      440602 :   gcc_sort_r (bbs, n, sizeof (basic_block), sort_bbs_in_loop_postorder_cmp,
    1812                 :             :               bb_loop_postorder);
    1813                 :             : 
    1814                 :             :   /* Visit blocks in loop postorder and assign mem-ref IDs in that order.
    1815                 :             :      That results in better locality for all the bitmaps.  It also
    1816                 :             :      automatically sorts the location list of gathered memory references
    1817                 :             :      after their loop postorder number allowing to binary-search it.  */
    1818                 :     5798636 :   for (i = 0; i < n; ++i)
    1819                 :             :     {
    1820                 :     5358034 :       basic_block bb = bbs[i];
    1821                 :    46293647 :       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    1822                 :    35577579 :         gather_mem_refs_stmt (bb->loop_father, gsi_stmt (bsi));
    1823                 :             :     }
    1824                 :             : 
    1825                 :             :   /* Verify the list of gathered memory references is sorted after their
    1826                 :             :      loop postorder number.  */
    1827                 :      440602 :   if (flag_checking)
    1828                 :             :     {
    1829                 :             :       im_mem_ref *ref;
    1830                 :    10341048 :       FOR_EACH_VEC_ELT (memory_accesses.refs_list, i, ref)
    1831                 :    11668830 :         for (unsigned j = 1; j < ref->accesses_in_loop.length (); ++j)
    1832                 :      884186 :           gcc_assert (sort_locs_in_loop_postorder_cmp
    1833                 :             :                         (&ref->accesses_in_loop[j-1], &ref->accesses_in_loop[j],
    1834                 :             :                          bb_loop_postorder) <= 0);
    1835                 :             :     }
    1836                 :             : 
    1837                 :      440602 :   free (bbs);
    1838                 :             : 
    1839                 :      440602 :   if (!store_motion)
    1840                 :      440602 :     return;
    1841                 :             : 
    1842                 :             :   /* Propagate the information about accessed memory references up
    1843                 :             :      the loop hierarchy.  */
    1844                 :     2456111 :   for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
    1845                 :             :     {
    1846                 :             :       /* Finalize the overall touched references (including subloops).  */
    1847                 :     1134476 :       bitmap_ior_into (&memory_accesses.all_refs_stored_in_loop[loop->num],
    1848                 :     1134476 :                        &memory_accesses.refs_stored_in_loop[loop->num]);
    1849                 :             : 
    1850                 :             :       /* Propagate the information about accessed memory references up
    1851                 :             :          the loop hierarchy.  */
    1852                 :     1134476 :       outer = loop_outer (loop);
    1853                 :     1134476 :       if (outer == current_loops->tree_root)
    1854                 :      883572 :         continue;
    1855                 :             : 
    1856                 :      250904 :       bitmap_ior_into (&memory_accesses.all_refs_stored_in_loop[outer->num],
    1857                 :      250904 :                        &memory_accesses.all_refs_stored_in_loop[loop->num]);
    1858                 :      440545 :     }
    1859                 :             : }
    1860                 :             : 
    1861                 :             : /* Returns true if MEM1 and MEM2 may alias.  TTAE_CACHE is used as a cache in
    1862                 :             :    tree_to_aff_combination_expand.  */
    1863                 :             : 
    1864                 :             : static bool
    1865                 :      484888 : mem_refs_may_alias_p (im_mem_ref *mem1, im_mem_ref *mem2,
    1866                 :             :                       hash_map<tree, name_expansion *> **ttae_cache,
    1867                 :             :                       bool tbaa_p)
    1868                 :             : {
    1869                 :      484888 :   gcc_checking_assert (mem1->mem.ref != error_mark_node
    1870                 :             :                        && mem2->mem.ref != error_mark_node);
    1871                 :             : 
    1872                 :             :   /* Perform BASE + OFFSET analysis -- if MEM1 and MEM2 are based on the same
    1873                 :             :      object and their offset differ in such a way that the locations cannot
    1874                 :             :      overlap, then they cannot alias.  */
    1875                 :             :   poly_widest_int size1, size2;
    1876                 :      969776 :   aff_tree off1, off2;
    1877                 :             : 
    1878                 :             :   /* Perform basic offset and type-based disambiguation.  */
    1879                 :      484888 :   if (!refs_may_alias_p_1 (&mem1->mem, &mem2->mem, tbaa_p))
    1880                 :             :     return false;
    1881                 :             : 
    1882                 :             :   /* The expansion of addresses may be a bit expensive, thus we only do
    1883                 :             :      the check at -O2 and higher optimization levels.  */
    1884                 :       55459 :   if (optimize < 2)
    1885                 :             :     return true;
    1886                 :             : 
    1887                 :       47906 :   get_inner_reference_aff (mem1->mem.ref, &off1, &size1);
    1888                 :       47906 :   get_inner_reference_aff (mem2->mem.ref, &off2, &size2);
    1889                 :       47906 :   aff_combination_expand (&off1, ttae_cache);
    1890                 :       47906 :   aff_combination_expand (&off2, ttae_cache);
    1891                 :       47906 :   aff_combination_scale (&off1, -1);
    1892                 :       47906 :   aff_combination_add (&off2, &off1);
    1893                 :             : 
    1894                 :       47906 :   if (aff_comb_cannot_overlap_p (&off2, size1, size2))
    1895                 :             :     return false;
    1896                 :             : 
    1897                 :             :   return true;
    1898                 :      484888 : }
    1899                 :             : 
    1900                 :             : /* Compare function for bsearch searching for reference locations
    1901                 :             :    in a loop.  */
    1902                 :             : 
    1903                 :             : static int
    1904                 :      249609 : find_ref_loc_in_loop_cmp (const void *loop_, const void *loc_,
    1905                 :             :                           void *bb_loop_postorder_)
    1906                 :             : {
    1907                 :      249609 :   unsigned *bb_loop_postorder = (unsigned *)bb_loop_postorder_;
    1908                 :      249609 :   class loop *loop = (class loop *)const_cast<void *>(loop_);
    1909                 :      249609 :   mem_ref_loc *loc = (mem_ref_loc *)const_cast<void *>(loc_);
    1910                 :      249609 :   class loop *loc_loop = gimple_bb (loc->stmt)->loop_father;
    1911                 :      249609 :   if (loop->num  == loc_loop->num
    1912                 :      249609 :       || flow_loop_nested_p (loop, loc_loop))
    1913                 :      192594 :     return 0;
    1914                 :       57015 :   return (bb_loop_postorder[loop->num] < bb_loop_postorder[loc_loop->num]
    1915                 :       57015 :           ? -1 : 1);
    1916                 :             : }
    1917                 :             : 
    1918                 :             : /* Iterates over all locations of REF in LOOP and its subloops calling
    1919                 :             :    fn.operator() with the location as argument.  When that operator
    1920                 :             :    returns true the iteration is stopped and true is returned.
    1921                 :             :    Otherwise false is returned.  */
    1922                 :             : 
    1923                 :             : template <typename FN>
    1924                 :             : static bool
    1925                 :      192594 : for_all_locs_in_loop (class loop *loop, im_mem_ref *ref, FN fn)
    1926                 :             : {
    1927                 :             :   unsigned i;
    1928                 :             :   mem_ref_loc *loc;
    1929                 :             : 
    1930                 :             :   /* Search for the cluster of locs in the accesses_in_loop vector
    1931                 :             :      which is sorted after postorder index of the loop father.  */
    1932                 :      192594 :   loc = ref->accesses_in_loop.bsearch (loop, find_ref_loc_in_loop_cmp,
    1933                 :             :                                        bb_loop_postorder);
    1934                 :      192594 :   if (!loc)
    1935                 :           0 :     return false;
    1936                 :             : 
    1937                 :             :   /* We have found one location inside loop or its sub-loops.  Iterate
    1938                 :             :      both forward and backward to cover the whole cluster.  */
    1939                 :      192594 :   i = loc - ref->accesses_in_loop.address ();
    1940                 :      267939 :   while (i > 0)
    1941                 :             :     {
    1942                 :      129639 :       --i;
    1943                 :      129639 :       mem_ref_loc *l = &ref->accesses_in_loop[i];
    1944                 :      129639 :       if (!flow_bb_inside_loop_p (loop, gimple_bb (l->stmt)))
    1945                 :             :         break;
    1946                 :      107069 :       if (fn (l))
    1947                 :             :         return true;
    1948                 :             :     }
    1949                 :      440741 :   for (i = loc - ref->accesses_in_loop.address ();
    1950                 :      559742 :        i < ref->accesses_in_loop.length (); ++i)
    1951                 :             :     {
    1952                 :      208951 :       mem_ref_loc *l = &ref->accesses_in_loop[i];
    1953                 :      208951 :       if (!flow_bb_inside_loop_p (loop, gimple_bb (l->stmt)))
    1954                 :             :         break;
    1955                 :      191061 :       if (fn (l))
    1956                 :             :         return true;
    1957                 :             :     }
    1958                 :             : 
    1959                 :             :   return false;
    1960                 :             : }
    1961                 :             : 
    1962                 :             : /* Rewrites location LOC by TMP_VAR.  */
    1963                 :             : 
    1964                 :             : class rewrite_mem_ref_loc
    1965                 :             : {
    1966                 :             : public:
    1967                 :       32815 :   rewrite_mem_ref_loc (tree tmp_var_) : tmp_var (tmp_var_) {}
    1968                 :             :   bool operator () (mem_ref_loc *loc);
    1969                 :             :   tree tmp_var;
    1970                 :             : };
    1971                 :             : 
    1972                 :             : bool
    1973                 :       52251 : rewrite_mem_ref_loc::operator () (mem_ref_loc *loc)
    1974                 :             : {
    1975                 :       52251 :   *loc->ref = tmp_var;
    1976                 :       52251 :   update_stmt (loc->stmt);
    1977                 :       52251 :   return false;
    1978                 :             : }
    1979                 :             : 
    1980                 :             : /* Rewrites all references to REF in LOOP by variable TMP_VAR.  */
    1981                 :             : 
    1982                 :             : static void
    1983                 :       32815 : rewrite_mem_refs (class loop *loop, im_mem_ref *ref, tree tmp_var)
    1984                 :             : {
    1985                 :           0 :   for_all_locs_in_loop (loop, ref, rewrite_mem_ref_loc (tmp_var));
    1986                 :           0 : }
    1987                 :             : 
    1988                 :             : /* Stores the first reference location in LOCP.  */
    1989                 :             : 
    1990                 :             : class first_mem_ref_loc_1
    1991                 :             : {
    1992                 :             : public:
    1993                 :       32815 :   first_mem_ref_loc_1 (mem_ref_loc **locp_) : locp (locp_) {}
    1994                 :             :   bool operator () (mem_ref_loc *loc);
    1995                 :             :   mem_ref_loc **locp;
    1996                 :             : };
    1997                 :             : 
    1998                 :             : bool
    1999                 :       32815 : first_mem_ref_loc_1::operator () (mem_ref_loc *loc)
    2000                 :             : {
    2001                 :       32815 :   *locp = loc;
    2002                 :       32815 :   return true;
    2003                 :             : }
    2004                 :             : 
    2005                 :             : /* Returns the first reference location to REF in LOOP.  */
    2006                 :             : 
    2007                 :             : static mem_ref_loc *
    2008                 :       32815 : first_mem_ref_loc (class loop *loop, im_mem_ref *ref)
    2009                 :             : {
    2010                 :       32815 :   mem_ref_loc *locp = NULL;
    2011                 :           0 :   for_all_locs_in_loop (loop, ref, first_mem_ref_loc_1 (&locp));
    2012                 :       32815 :   return locp;
    2013                 :             : }
    2014                 :             : 
    2015                 :             : /* Helper function for execute_sm.  Emit code to store TMP_VAR into
    2016                 :             :    MEM along edge EX.
    2017                 :             : 
    2018                 :             :    The store is only done if MEM has changed.  We do this so no
    2019                 :             :    changes to MEM occur on code paths that did not originally store
    2020                 :             :    into it.
    2021                 :             : 
    2022                 :             :    The common case for execute_sm will transform:
    2023                 :             : 
    2024                 :             :      for (...) {
    2025                 :             :        if (foo)
    2026                 :             :          stuff;
    2027                 :             :        else
    2028                 :             :          MEM = TMP_VAR;
    2029                 :             :      }
    2030                 :             : 
    2031                 :             :    into:
    2032                 :             : 
    2033                 :             :      lsm = MEM;
    2034                 :             :      for (...) {
    2035                 :             :        if (foo)
    2036                 :             :          stuff;
    2037                 :             :        else
    2038                 :             :          lsm = TMP_VAR;
    2039                 :             :      }
    2040                 :             :      MEM = lsm;
    2041                 :             : 
    2042                 :             :   This function will generate:
    2043                 :             : 
    2044                 :             :      lsm = MEM;
    2045                 :             : 
    2046                 :             :      lsm_flag = false;
    2047                 :             :      ...
    2048                 :             :      for (...) {
    2049                 :             :        if (foo)
    2050                 :             :          stuff;
    2051                 :             :        else {
    2052                 :             :          lsm = TMP_VAR;
    2053                 :             :          lsm_flag = true;
    2054                 :             :        }
    2055                 :             :      }
    2056                 :             :      if (lsm_flag)      <--
    2057                 :             :        MEM = lsm;       <-- (X)
    2058                 :             : 
    2059                 :             :   In case MEM and TMP_VAR are NULL the function will return the then
    2060                 :             :   block so the caller can insert (X) and other related stmts. 
    2061                 :             : */
    2062                 :             : 
    2063                 :             : static basic_block
    2064                 :       17383 : execute_sm_if_changed (edge ex, tree mem, tree tmp_var, tree flag,
    2065                 :             :                        edge preheader, hash_set <basic_block> *flag_bbs,
    2066                 :             :                        edge &append_cond_position, edge &last_cond_fallthru)
    2067                 :             : {
    2068                 :       17383 :   basic_block new_bb, then_bb, old_dest;
    2069                 :       17383 :   bool loop_has_only_one_exit;
    2070                 :       17383 :   edge then_old_edge;
    2071                 :       17383 :   gimple_stmt_iterator gsi;
    2072                 :       17383 :   gimple *stmt;
    2073                 :       17383 :   bool irr = ex->flags & EDGE_IRREDUCIBLE_LOOP;
    2074                 :             : 
    2075                 :       17383 :   profile_count count_sum = profile_count::zero ();
    2076                 :       17383 :   int nbbs = 0, ncount = 0;
    2077                 :       17383 :   profile_probability flag_probability = profile_probability::uninitialized ();
    2078                 :             : 
    2079                 :             :   /* Flag is set in FLAG_BBS. Determine probability that flag will be true
    2080                 :             :      at loop exit.
    2081                 :             : 
    2082                 :             :      This code may look fancy, but it cannot update profile very realistically
    2083                 :             :      because we do not know the probability that flag will be true at given
    2084                 :             :      loop exit.
    2085                 :             : 
    2086                 :             :      We look for two interesting extremes
    2087                 :             :        - when exit is dominated by block setting the flag, we know it will
    2088                 :             :          always be true.  This is a common case.
    2089                 :             :        - when all blocks setting the flag have very low frequency we know
    2090                 :             :          it will likely be false.
    2091                 :             :      In all other cases we default to 2/3 for flag being true.  */
    2092                 :             : 
    2093                 :       17383 :   for (hash_set<basic_block>::iterator it = flag_bbs->begin ();
    2094                 :       60167 :        it != flag_bbs->end (); ++it)
    2095                 :             :     {
    2096                 :       21392 :        if ((*it)->count.initialized_p ())
    2097                 :       21379 :          count_sum += (*it)->count, ncount ++;
    2098                 :       21392 :        if (dominated_by_p (CDI_DOMINATORS, ex->src, *it))
    2099                 :        2901 :          flag_probability = profile_probability::always ();
    2100                 :       21392 :        nbbs++;
    2101                 :             :     }
    2102                 :             : 
    2103                 :       17383 :   profile_probability cap
    2104                 :       17383 :           = profile_probability::guessed_always ().apply_scale (2, 3);
    2105                 :             : 
    2106                 :       17383 :   if (flag_probability.initialized_p ())
    2107                 :             :     ;
    2108                 :       14520 :   else if (ncount == nbbs
    2109                 :       29584 :            && preheader->count () >= count_sum && preheader->count ().nonzero_p ())
    2110                 :             :     {
    2111                 :         451 :       flag_probability = count_sum.probability_in (preheader->count ());
    2112                 :         451 :       if (flag_probability > cap)
    2113                 :          63 :         flag_probability = cap;
    2114                 :             :     }
    2115                 :             : 
    2116                 :       17383 :   if (!flag_probability.initialized_p ())
    2117                 :       14069 :     flag_probability = cap;
    2118                 :             : 
    2119                 :             :   /* ?? Insert store after previous store if applicable.  See note
    2120                 :             :      below.  */
    2121                 :       17383 :   if (append_cond_position)
    2122                 :        6220 :     ex = append_cond_position;
    2123                 :             : 
    2124                 :       17383 :   loop_has_only_one_exit = single_pred_p (ex->dest);
    2125                 :             : 
    2126                 :       17383 :   if (loop_has_only_one_exit)
    2127                 :        8133 :     ex = split_block_after_labels (ex->dest);
    2128                 :             :   else
    2129                 :             :     {
    2130                 :        9250 :       for (gphi_iterator gpi = gsi_start_phis (ex->dest);
    2131                 :       13215 :            !gsi_end_p (gpi); gsi_next (&gpi))
    2132                 :             :         {
    2133                 :        5114 :           gphi *phi = gpi.phi ();
    2134                 :       10228 :           if (virtual_operand_p (gimple_phi_result (phi)))
    2135                 :        3965 :             continue;
    2136                 :             : 
    2137                 :             :           /* When the destination has a non-virtual PHI node with multiple
    2138                 :             :              predecessors make sure we preserve the PHI structure by
    2139                 :             :              forcing a forwarder block so that hoisting of that PHI will
    2140                 :             :              still work.  */
    2141                 :        1149 :           split_edge (ex);
    2142                 :        1149 :           break;
    2143                 :             :         }
    2144                 :             :     }
    2145                 :             : 
    2146                 :       17383 :   old_dest = ex->dest;
    2147                 :       17383 :   new_bb = split_edge (ex);
    2148                 :       17383 :   if (append_cond_position)
    2149                 :        6220 :     new_bb->count += last_cond_fallthru->count ();
    2150                 :       17383 :   then_bb = create_empty_bb (new_bb);
    2151                 :       17383 :   then_bb->count = new_bb->count.apply_probability (flag_probability);
    2152                 :       17383 :   if (irr)
    2153                 :          51 :     then_bb->flags = BB_IRREDUCIBLE_LOOP;
    2154                 :       17383 :   add_bb_to_loop (then_bb, new_bb->loop_father);
    2155                 :             : 
    2156                 :       17383 :   gsi = gsi_start_bb (new_bb);
    2157                 :       17383 :   stmt = gimple_build_cond (NE_EXPR, flag, boolean_false_node,
    2158                 :             :                             NULL_TREE, NULL_TREE);
    2159                 :       17383 :   gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
    2160                 :             : 
    2161                 :             :   /* Insert actual store.  */
    2162                 :       17383 :   if (mem)
    2163                 :             :     {
    2164                 :       14442 :       gsi = gsi_start_bb (then_bb);
    2165                 :       14442 :       stmt = gimple_build_assign (unshare_expr (mem), tmp_var);
    2166                 :       14442 :       gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
    2167                 :             :     }
    2168                 :             : 
    2169                 :       17383 :   edge e1 = single_succ_edge (new_bb);
    2170                 :       34715 :   edge e2 = make_edge (new_bb, then_bb,
    2171                 :             :                        EDGE_TRUE_VALUE | (irr ? EDGE_IRREDUCIBLE_LOOP : 0));
    2172                 :       17383 :   e2->probability = flag_probability;
    2173                 :             : 
    2174                 :       17383 :   e1->flags |= EDGE_FALSE_VALUE | (irr ? EDGE_IRREDUCIBLE_LOOP : 0);
    2175                 :       17383 :   e1->flags &= ~EDGE_FALLTHRU;
    2176                 :             : 
    2177                 :       17383 :   e1->probability = flag_probability.invert ();
    2178                 :             : 
    2179                 :       34715 :   then_old_edge = make_single_succ_edge (then_bb, old_dest,
    2180                 :             :                              EDGE_FALLTHRU | (irr ? EDGE_IRREDUCIBLE_LOOP : 0));
    2181                 :             : 
    2182                 :       17383 :   set_immediate_dominator (CDI_DOMINATORS, then_bb, new_bb);
    2183                 :             : 
    2184                 :       17383 :   if (append_cond_position)
    2185                 :             :     {
    2186                 :        6220 :       basic_block prevbb = last_cond_fallthru->src;
    2187                 :        6220 :       redirect_edge_succ (last_cond_fallthru, new_bb);
    2188                 :        6220 :       set_immediate_dominator (CDI_DOMINATORS, new_bb, prevbb);
    2189                 :        6220 :       set_immediate_dominator (CDI_DOMINATORS, old_dest,
    2190                 :             :                                recompute_dominator (CDI_DOMINATORS, old_dest));
    2191                 :             :     }
    2192                 :             : 
    2193                 :             :   /* ?? Because stores may alias, they must happen in the exact
    2194                 :             :      sequence they originally happened.  Save the position right after
    2195                 :             :      the (_lsm) store we just created so we can continue appending after
    2196                 :             :      it and maintain the original order.  */
    2197                 :       17383 :   append_cond_position = then_old_edge;
    2198                 :       17383 :   last_cond_fallthru = find_edge (new_bb, old_dest);
    2199                 :             : 
    2200                 :       17383 :   if (!loop_has_only_one_exit)
    2201                 :        9250 :     for (gphi_iterator gpi = gsi_start_phis (old_dest);
    2202                 :       13105 :          !gsi_end_p (gpi); gsi_next (&gpi))
    2203                 :             :       {
    2204                 :        3855 :         gphi *phi = gpi.phi ();
    2205                 :        3855 :         unsigned i;
    2206                 :             : 
    2207                 :       32441 :         for (i = 0; i < gimple_phi_num_args (phi); i++)
    2208                 :       28586 :           if (gimple_phi_arg_edge (phi, i)->src == new_bb)
    2209                 :             :             {
    2210                 :        3855 :               tree arg = gimple_phi_arg_def (phi, i);
    2211                 :        3855 :               add_phi_arg (phi, arg, then_old_edge, UNKNOWN_LOCATION);
    2212                 :        3855 :               update_stmt (phi);
    2213                 :             :             }
    2214                 :             :       }
    2215                 :             : 
    2216                 :       17383 :   return then_bb;
    2217                 :             : }
    2218                 :             : 
    2219                 :             : /* When REF is set on the location, set flag indicating the store.  */
    2220                 :             : 
    2221                 :             : class sm_set_flag_if_changed
    2222                 :             : {
    2223                 :             : public:
    2224                 :        8907 :   sm_set_flag_if_changed (tree flag_, hash_set <basic_block> *bbs_)
    2225                 :        8907 :          : flag (flag_), bbs (bbs_) {}
    2226                 :             :   bool operator () (mem_ref_loc *loc);
    2227                 :             :   tree flag;
    2228                 :             :   hash_set <basic_block> *bbs;
    2229                 :             : };
    2230                 :             : 
    2231                 :             : bool
    2232                 :       16902 : sm_set_flag_if_changed::operator () (mem_ref_loc *loc)
    2233                 :             : {
    2234                 :             :   /* Only set the flag for writes.  */
    2235                 :       16902 :   if (is_gimple_assign (loc->stmt)
    2236                 :       16902 :       && gimple_assign_lhs_ptr (loc->stmt) == loc->ref)
    2237                 :             :     {
    2238                 :       10523 :       gimple_stmt_iterator gsi = gsi_for_stmt (loc->stmt);
    2239                 :       10523 :       gimple *stmt = gimple_build_assign (flag, boolean_true_node);
    2240                 :       10523 :       gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
    2241                 :       10523 :       bbs->add (gimple_bb (stmt));
    2242                 :             :     }
    2243                 :       16902 :   return false;
    2244                 :             : }
    2245                 :             : 
    2246                 :             : /* Helper function for execute_sm.  On every location where REF is
    2247                 :             :    set, set an appropriate flag indicating the store.  */
    2248                 :             : 
    2249                 :             : static tree
    2250                 :        8907 : execute_sm_if_changed_flag_set (class loop *loop, im_mem_ref *ref,
    2251                 :             :                                 hash_set <basic_block> *bbs)
    2252                 :             : {
    2253                 :        8907 :   tree flag;
    2254                 :        8907 :   char *str = get_lsm_tmp_name (ref->mem.ref, ~0, "_flag");
    2255                 :        8907 :   flag = create_tmp_reg (boolean_type_node, str);
    2256                 :        8907 :   for_all_locs_in_loop (loop, ref, sm_set_flag_if_changed (flag, bbs));
    2257                 :        8907 :   return flag;
    2258                 :             : }
    2259                 :             : 
    2260                 :       32815 : struct sm_aux
    2261                 :             : {
    2262                 :             :   tree tmp_var;
    2263                 :             :   tree store_flag;
    2264                 :             :   hash_set <basic_block> flag_bbs;
    2265                 :             : };
    2266                 :             : 
    2267                 :             : /* Executes store motion of memory reference REF from LOOP.
    2268                 :             :    Exits from the LOOP are stored in EXITS.  The initialization of the
    2269                 :             :    temporary variable is put to the preheader of the loop, and assignments
    2270                 :             :    to the reference from the temporary variable are emitted to exits.  */
    2271                 :             : 
    2272                 :             : static void
    2273                 :       32815 : execute_sm (class loop *loop, im_mem_ref *ref,
    2274                 :             :             hash_map<im_mem_ref *, sm_aux *> &aux_map, bool maybe_mt,
    2275                 :             :             bool use_other_flag_var)
    2276                 :             : {
    2277                 :       32815 :   gassign *load;
    2278                 :       32815 :   struct fmt_data fmt_data;
    2279                 :       32815 :   struct lim_aux_data *lim_data;
    2280                 :       32815 :   bool multi_threaded_model_p = false;
    2281                 :       32815 :   gimple_stmt_iterator gsi;
    2282                 :       32815 :   sm_aux *aux = new sm_aux;
    2283                 :             : 
    2284                 :       32815 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2285                 :             :     {
    2286                 :          85 :       fprintf (dump_file, "Executing store motion of ");
    2287                 :          85 :       print_generic_expr (dump_file, ref->mem.ref);
    2288                 :          85 :       fprintf (dump_file, " from loop %d\n", loop->num);
    2289                 :             :     }
    2290                 :             : 
    2291                 :       32815 :   aux->tmp_var = create_tmp_reg (TREE_TYPE (ref->mem.ref),
    2292                 :       32815 :                                  get_lsm_tmp_name (ref->mem.ref, ~0));
    2293                 :             : 
    2294                 :       32815 :   fmt_data.loop = loop;
    2295                 :       32815 :   fmt_data.orig_loop = loop;
    2296                 :       32815 :   for_each_index (&ref->mem.ref, force_move_till, &fmt_data);
    2297                 :             : 
    2298                 :       65630 :   bool always_stored = ref_always_accessed_p (loop, ref, true);
    2299                 :       32815 :   if (maybe_mt
    2300                 :       10685 :       && (bb_in_transaction (loop_preheader_edge (loop)->src)
    2301                 :       10684 :           || (! flag_store_data_races && ! always_stored)))
    2302                 :             :     multi_threaded_model_p = true;
    2303                 :             : 
    2304                 :       32815 :   if (multi_threaded_model_p && !use_other_flag_var)
    2305                 :        8907 :     aux->store_flag
    2306                 :        8907 :       = execute_sm_if_changed_flag_set (loop, ref, &aux->flag_bbs);
    2307                 :             :   else
    2308                 :       23908 :     aux->store_flag = NULL_TREE;
    2309                 :             : 
    2310                 :             :   /* Remember variable setup.  */
    2311                 :       32815 :   aux_map.put (ref, aux);
    2312                 :             : 
    2313                 :       32815 :   rewrite_mem_refs (loop, ref, aux->tmp_var);
    2314                 :             : 
    2315                 :             :   /* Emit the load code on a random exit edge or into the latch if
    2316                 :             :      the loop does not exit, so that we are sure it will be processed
    2317                 :             :      by move_computations after all dependencies.  */
    2318                 :       32815 :   gsi = gsi_for_stmt (first_mem_ref_loc (loop, ref)->stmt);
    2319                 :             : 
    2320                 :             :   /* Avoid doing a load if there was no load of the ref in the loop.
    2321                 :             :      Esp. when the ref is not always stored we cannot optimize it
    2322                 :             :      away later.  But when it is not always stored we must use a conditional
    2323                 :             :      store then.  */
    2324                 :       32815 :   if ((!always_stored && !multi_threaded_model_p)
    2325                 :       32815 :       || (ref->loaded && bitmap_bit_p (ref->loaded, loop->num)))
    2326                 :       15458 :     load = gimple_build_assign (aux->tmp_var, unshare_expr (ref->mem.ref));
    2327                 :             :   else
    2328                 :             :     {
    2329                 :             :       /* If not emitting a load mark the uninitialized state on the
    2330                 :             :          loop entry as not to be warned for.  */
    2331                 :       17357 :       tree uninit = create_tmp_reg (TREE_TYPE (aux->tmp_var));
    2332                 :       17357 :       suppress_warning (uninit, OPT_Wuninitialized);
    2333                 :       17357 :       load = gimple_build_assign (aux->tmp_var, uninit);
    2334                 :             :     }
    2335                 :       32815 :   lim_data = init_lim_data (load);
    2336                 :       32815 :   lim_data->max_loop = loop;
    2337                 :       32815 :   lim_data->tgt_loop = loop;
    2338                 :       32815 :   gsi_insert_before (&gsi, load, GSI_SAME_STMT);
    2339                 :             : 
    2340                 :       32815 :   if (aux->store_flag)
    2341                 :             :     {
    2342                 :        8907 :       load = gimple_build_assign (aux->store_flag, boolean_false_node);
    2343                 :        8907 :       lim_data = init_lim_data (load);
    2344                 :        8907 :       lim_data->max_loop = loop;
    2345                 :        8907 :       lim_data->tgt_loop = loop;
    2346                 :        8907 :       gsi_insert_before (&gsi, load, GSI_SAME_STMT);
    2347                 :             :     }
    2348                 :       32815 : }
    2349                 :             : 
    2350                 :             : /* sm_ord is used for ordinary stores we can retain order with respect
    2351                 :             :        to other stores
    2352                 :             :    sm_unord is used for conditional executed stores which need to be
    2353                 :             :        able to execute in arbitrary order with respect to other stores
    2354                 :             :    sm_other is used for stores we do not try to apply store motion to.  */
    2355                 :             : enum sm_kind { sm_ord, sm_unord, sm_other };
    2356                 :             : struct seq_entry
    2357                 :             : {
    2358                 :             :   seq_entry () = default;
    2359                 :       51688 :   seq_entry (unsigned f, sm_kind k, tree fr = NULL)
    2360                 :       51688 :     : first (f), second (k), from (fr) {}
    2361                 :             :   unsigned first;
    2362                 :             :   sm_kind second;
    2363                 :             :   tree from;
    2364                 :             : };
    2365                 :             : 
    2366                 :             : static void
    2367                 :       30824 : execute_sm_exit (class loop *loop, edge ex, vec<seq_entry> &seq,
    2368                 :             :                  hash_map<im_mem_ref *, sm_aux *> &aux_map, sm_kind kind,
    2369                 :             :                  edge &append_cond_position, edge &last_cond_fallthru)
    2370                 :             : {
    2371                 :             :   /* Sink the stores to exit from the loop.  */
    2372                 :      108613 :   for (unsigned i = seq.length (); i > 0; --i)
    2373                 :             :     {
    2374                 :       46965 :       im_mem_ref *ref = memory_accesses.refs_list[seq[i-1].first];
    2375                 :       46965 :       if (seq[i-1].second == sm_other)
    2376                 :             :         {
    2377                 :        4872 :           gcc_assert (kind == sm_ord && seq[i-1].from != NULL_TREE);
    2378                 :        4872 :           if (dump_file && (dump_flags & TDF_DETAILS))
    2379                 :             :             {
    2380                 :           0 :               fprintf (dump_file, "Re-issueing dependent store of ");
    2381                 :           0 :               print_generic_expr (dump_file, ref->mem.ref);
    2382                 :           0 :               fprintf (dump_file, " from loop %d on exit %d -> %d\n",
    2383                 :           0 :                        loop->num, ex->src->index, ex->dest->index);
    2384                 :             :             }
    2385                 :        4872 :           gassign *store = gimple_build_assign (unshare_expr (ref->mem.ref),
    2386                 :        4872 :                                                 seq[i-1].from);
    2387                 :        4872 :           gsi_insert_on_edge (ex, store);
    2388                 :             :         }
    2389                 :             :       else
    2390                 :             :         {
    2391                 :       42093 :           sm_aux *aux = *aux_map.get (ref);
    2392                 :       42093 :           if (!aux->store_flag || kind == sm_ord)
    2393                 :             :             {
    2394                 :       27651 :               gassign *store;
    2395                 :       27651 :               store = gimple_build_assign (unshare_expr (ref->mem.ref),
    2396                 :             :                                            aux->tmp_var);
    2397                 :       27651 :               gsi_insert_on_edge (ex, store);
    2398                 :       27651 :             }
    2399                 :             :           else
    2400                 :       14442 :             execute_sm_if_changed (ex, ref->mem.ref, aux->tmp_var,
    2401                 :             :                                    aux->store_flag,
    2402                 :             :                                    loop_preheader_edge (loop), &aux->flag_bbs,
    2403                 :             :                                    append_cond_position, last_cond_fallthru);
    2404                 :             :         }
    2405                 :             :     }
    2406                 :       30824 : }
    2407                 :             : 
    2408                 :             : /* Push the SM candidate at index PTR in the sequence SEQ down until
    2409                 :             :    we hit the next SM candidate.  Return true if that went OK and
    2410                 :             :    false if we could not disambiguate agains another unrelated ref.
    2411                 :             :    Update *AT to the index where the candidate now resides.  */
    2412                 :             : 
    2413                 :             : static bool
    2414                 :       35940 : sm_seq_push_down (vec<seq_entry> &seq, unsigned ptr, unsigned *at)
    2415                 :             : {
    2416                 :       35940 :   *at = ptr;
    2417                 :       36731 :   for (; ptr > 0; --ptr)
    2418                 :             :     {
    2419                 :       16975 :       seq_entry &new_cand = seq[ptr];
    2420                 :       16975 :       seq_entry &against = seq[ptr-1];
    2421                 :       16975 :       if (against.second == sm_ord
    2422                 :        5969 :           || (against.second == sm_other && against.from != NULL_TREE))
    2423                 :             :         /* Found the tail of the sequence.  */
    2424                 :             :         break;
    2425                 :             :       /* We may not ignore self-dependences here.  */
    2426                 :        1400 :       if (new_cand.first == against.first
    2427                 :        2232 :           || !refs_independent_p (memory_accesses.refs_list[new_cand.first],
    2428                 :         832 :                                   memory_accesses.refs_list[against.first],
    2429                 :             :                                   false))
    2430                 :             :         /* ???  Prune new_cand from the list of refs to apply SM to.  */
    2431                 :         609 :         return false;
    2432                 :         791 :       std::swap (new_cand, against);
    2433                 :         791 :       *at = ptr - 1;
    2434                 :             :     }
    2435                 :             :   return true;
    2436                 :             : }
    2437                 :             : 
    2438                 :             : /* Computes the sequence of stores from candidates in REFS_NOT_IN_SEQ to SEQ
    2439                 :             :    walking backwards from VDEF (or the end of BB if VDEF is NULL).  */
    2440                 :             : 
    2441                 :             : static int
    2442                 :       35265 : sm_seq_valid_bb (class loop *loop, basic_block bb, tree vdef,
    2443                 :             :                  vec<seq_entry> &seq, bitmap refs_not_in_seq,
    2444                 :             :                  bitmap refs_not_supported, bool forked,
    2445                 :             :                  bitmap fully_visited)
    2446                 :             : {
    2447                 :       35265 :   if (!vdef)
    2448                 :       25006 :     for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
    2449                 :      107788 :          gsi_prev (&gsi))
    2450                 :             :       {
    2451                 :      133573 :         vdef = gimple_vdef (gsi_stmt (gsi));
    2452                 :       50791 :         if (vdef)
    2453                 :             :           break;
    2454                 :             :       }
    2455                 :       25006 :   if (!vdef)
    2456                 :             :     {
    2457                 :        8009 :       gphi *vphi = get_virtual_phi (bb);
    2458                 :        8009 :       if (vphi)
    2459                 :        3924 :         vdef = gimple_phi_result (vphi);
    2460                 :             :     }
    2461                 :       35265 :   if (!vdef)
    2462                 :             :     {
    2463                 :        4085 :       if (single_pred_p (bb))
    2464                 :             :         /* This handles the perfect nest case.  */
    2465                 :        3375 :         return sm_seq_valid_bb (loop, single_pred (bb), vdef,
    2466                 :             :                                 seq, refs_not_in_seq, refs_not_supported,
    2467                 :        3375 :                                 forked, fully_visited);
    2468                 :             :       return 0;
    2469                 :             :     }
    2470                 :       56201 :   do
    2471                 :             :     {
    2472                 :      112402 :       gimple *def = SSA_NAME_DEF_STMT (vdef);
    2473                 :       56201 :       if (gimple_bb (def) != bb)
    2474                 :             :         {
    2475                 :             :           /* If we forked by processing a PHI do not allow our walk to
    2476                 :             :              merge again until we handle that robustly.  */
    2477                 :        3102 :           if (forked)
    2478                 :             :             {
    2479                 :             :               /* Mark refs_not_in_seq as unsupported.  */
    2480                 :        1511 :               bitmap_ior_into (refs_not_supported, refs_not_in_seq);
    2481                 :        1511 :               return 1;
    2482                 :             :             }
    2483                 :             :           /* Otherwise it doesn't really matter if we end up in different
    2484                 :             :              BBs.  */
    2485                 :             :           bb = gimple_bb (def);
    2486                 :             :         }
    2487                 :       54690 :       if (gphi *phi = dyn_cast <gphi *> (def))
    2488                 :             :         {
    2489                 :             :           /* Handle CFG merges.  Until we handle forks (gimple_bb (def) != bb)
    2490                 :             :              this is still linear.
    2491                 :             :              Eventually we want to cache intermediate results per BB
    2492                 :             :              (but we can't easily cache for different exits?).  */
    2493                 :             :           /* Stop at PHIs with possible backedges.  */
    2494                 :       10974 :           if (bb == bb->loop_father->header
    2495                 :        6145 :               || bb->flags & BB_IRREDUCIBLE_LOOP)
    2496                 :             :             {
    2497                 :             :               /* Mark refs_not_in_seq as unsupported.  */
    2498                 :        4831 :               bitmap_ior_into (refs_not_supported, refs_not_in_seq);
    2499                 :        4831 :               return 1;
    2500                 :             :             }
    2501                 :        6143 :           if (gimple_phi_num_args (phi) == 1)
    2502                 :        2014 :             return sm_seq_valid_bb (loop, gimple_phi_arg_edge (phi, 0)->src,
    2503                 :             :                                     gimple_phi_arg_def (phi, 0), seq,
    2504                 :             :                                     refs_not_in_seq, refs_not_supported,
    2505                 :        2014 :                                     false, fully_visited);
    2506                 :        8258 :           if (bitmap_bit_p (fully_visited,
    2507                 :        4129 :                             SSA_NAME_VERSION (gimple_phi_result (phi))))
    2508                 :             :             return 1;
    2509                 :        3989 :           auto_vec<seq_entry> first_edge_seq;
    2510                 :        3989 :           auto_bitmap tem_refs_not_in_seq (&lim_bitmap_obstack);
    2511                 :        3989 :           int eret;
    2512                 :        3989 :           bitmap_copy (tem_refs_not_in_seq, refs_not_in_seq);
    2513                 :        3989 :           eret = sm_seq_valid_bb (loop, gimple_phi_arg_edge (phi, 0)->src,
    2514                 :             :                                   gimple_phi_arg_def (phi, 0),
    2515                 :             :                                   first_edge_seq,
    2516                 :             :                                   tem_refs_not_in_seq, refs_not_supported,
    2517                 :             :                                   true, fully_visited);
    2518                 :        3989 :           if (eret != 1)
    2519                 :             :             return -1;
    2520                 :             :           /* Simplify our lives by pruning the sequence of !sm_ord.  */
    2521                 :        5496 :           while (!first_edge_seq.is_empty ()
    2522                 :       12742 :                  && first_edge_seq.last ().second != sm_ord)
    2523                 :        3257 :             first_edge_seq.pop ();
    2524                 :        8245 :           for (unsigned int i = 1; i < gimple_phi_num_args (phi); ++i)
    2525                 :             :             {
    2526                 :        6272 :               tree vuse = gimple_phi_arg_def (phi, i);
    2527                 :        6272 :               edge e = gimple_phi_arg_edge (phi, i);
    2528                 :        6272 :               auto_vec<seq_entry> edge_seq;
    2529                 :        6272 :               bitmap_and_compl (tem_refs_not_in_seq,
    2530                 :             :                                 refs_not_in_seq, refs_not_supported);
    2531                 :             :               /* If we've marked all refs we search for as unsupported
    2532                 :             :                  we can stop processing and use the sequence as before
    2533                 :             :                  the PHI.  */
    2534                 :        6272 :               if (bitmap_empty_p (tem_refs_not_in_seq))
    2535                 :             :                 return 1;
    2536                 :        4256 :               eret = sm_seq_valid_bb (loop, e->src, vuse, edge_seq,
    2537                 :             :                                       tem_refs_not_in_seq, refs_not_supported,
    2538                 :             :                                       true, fully_visited);
    2539                 :        4256 :               if (eret != 1)
    2540                 :             :                 return -1;
    2541                 :             :               /* Simplify our lives by pruning the sequence of !sm_ord.  */
    2542                 :        5340 :               while (!edge_seq.is_empty ()
    2543                 :       12410 :                      && edge_seq.last ().second != sm_ord)
    2544                 :        2814 :                 edge_seq.pop ();
    2545                 :        8512 :               unsigned min_len = MIN(first_edge_seq.length (),
    2546                 :             :                                      edge_seq.length ());
    2547                 :             :               /* Incrementally merge seqs into first_edge_seq.  */
    2548                 :        4256 :               int first_uneq = -1;
    2549                 :        4256 :               auto_vec<seq_entry, 2> extra_refs;
    2550                 :        6887 :               for (unsigned int i = 0; i < min_len; ++i)
    2551                 :             :                 {
    2552                 :             :                   /* ???  We can more intelligently merge when we face different
    2553                 :             :                      order by additional sinking operations in one sequence.
    2554                 :             :                      For now we simply mark them as to be processed by the
    2555                 :             :                      not order-preserving SM code.  */
    2556                 :        2631 :                   if (first_edge_seq[i].first != edge_seq[i].first)
    2557                 :             :                     {
    2558                 :          71 :                       if (first_edge_seq[i].second == sm_ord)
    2559                 :          28 :                         bitmap_set_bit (refs_not_supported,
    2560                 :          28 :                                         first_edge_seq[i].first);
    2561                 :          71 :                       if (edge_seq[i].second == sm_ord)
    2562                 :          55 :                         bitmap_set_bit (refs_not_supported, edge_seq[i].first);
    2563                 :          71 :                       first_edge_seq[i].second = sm_other;
    2564                 :          71 :                       first_edge_seq[i].from = NULL_TREE;
    2565                 :             :                       /* Record the dropped refs for later processing.  */
    2566                 :          71 :                       if (first_uneq == -1)
    2567                 :          61 :                         first_uneq = i;
    2568                 :          71 :                       extra_refs.safe_push (seq_entry (edge_seq[i].first,
    2569                 :             :                                                        sm_other, NULL_TREE));
    2570                 :             :                     }
    2571                 :             :                   /* sm_other prevails.  */
    2572                 :        2560 :                   else if (first_edge_seq[i].second != edge_seq[i].second)
    2573                 :             :                     {
    2574                 :             :                       /* Make sure the ref is marked as not supported.  */
    2575                 :           4 :                       bitmap_set_bit (refs_not_supported,
    2576                 :           4 :                                       first_edge_seq[i].first);
    2577                 :           4 :                       first_edge_seq[i].second = sm_other;
    2578                 :           4 :                       first_edge_seq[i].from = NULL_TREE;
    2579                 :             :                     }
    2580                 :        2556 :                   else if (first_edge_seq[i].second == sm_other
    2581                 :          21 :                            && first_edge_seq[i].from != NULL_TREE
    2582                 :        2577 :                            && (edge_seq[i].from == NULL_TREE
    2583                 :          21 :                                || !operand_equal_p (first_edge_seq[i].from,
    2584                 :          21 :                                                     edge_seq[i].from, 0)))
    2585                 :          15 :                     first_edge_seq[i].from = NULL_TREE;
    2586                 :             :                 }
    2587                 :             :               /* Any excess elements become sm_other since they are now
    2588                 :             :                  coonditionally executed.  */
    2589                 :       12124 :               if (first_edge_seq.length () > edge_seq.length ())
    2590                 :             :                 {
    2591                 :        2292 :                   for (unsigned i = edge_seq.length ();
    2592                 :       30680 :                        i < first_edge_seq.length (); ++i)
    2593                 :             :                     {
    2594                 :       13048 :                       if (first_edge_seq[i].second == sm_ord)
    2595                 :        2964 :                         bitmap_set_bit (refs_not_supported,
    2596                 :        2964 :                                         first_edge_seq[i].first);
    2597                 :       13048 :                       first_edge_seq[i].second = sm_other;
    2598                 :             :                     }
    2599                 :             :                 }
    2600                 :        1964 :               else if (edge_seq.length () > first_edge_seq.length ())
    2601                 :             :                 {
    2602                 :          13 :                   if (first_uneq == -1)
    2603                 :           0 :                     first_uneq = first_edge_seq.length ();
    2604                 :          13 :                   for (unsigned i = first_edge_seq.length ();
    2605                 :          72 :                        i < edge_seq.length (); ++i)
    2606                 :             :                     {
    2607                 :          23 :                       if (edge_seq[i].second == sm_ord)
    2608                 :          13 :                         bitmap_set_bit (refs_not_supported, edge_seq[i].first);
    2609                 :          23 :                       extra_refs.safe_push (seq_entry (edge_seq[i].first,
    2610                 :             :                                                        sm_other, NULL_TREE));
    2611                 :             :                     }
    2612                 :             :                 }
    2613                 :             :               /* Put unmerged refs at first_uneq to force dependence checking
    2614                 :             :                  on them.  */
    2615                 :        4256 :               if (first_uneq != -1)
    2616                 :             :                 {
    2617                 :             :                   /* Missing ordered_splice_at.  */
    2618                 :         122 :                   if ((unsigned)first_uneq == first_edge_seq.length ())
    2619                 :           0 :                     first_edge_seq.safe_splice (extra_refs);
    2620                 :             :                   else
    2621                 :             :                     {
    2622                 :          61 :                       unsigned fes_length = first_edge_seq.length ();
    2623                 :          61 :                       first_edge_seq.safe_grow (fes_length
    2624                 :          61 :                                                 + extra_refs.length ());
    2625                 :          61 :                       memmove (&first_edge_seq[first_uneq + extra_refs.length ()],
    2626                 :          61 :                                &first_edge_seq[first_uneq],
    2627                 :          61 :                                (fes_length - first_uneq) * sizeof (seq_entry));
    2628                 :         122 :                       memcpy (&first_edge_seq[first_uneq],
    2629                 :          61 :                               extra_refs.address (),
    2630                 :          61 :                               extra_refs.length () * sizeof (seq_entry));
    2631                 :             :                     }
    2632                 :             :                 }
    2633                 :        6272 :             }
    2634                 :             :           /* Use the sequence from the first edge and push SMs down.  */
    2635                 :       12298 :           for (unsigned i = 0; i < first_edge_seq.length (); ++i)
    2636                 :             :             {
    2637                 :        4176 :               unsigned id = first_edge_seq[i].first;
    2638                 :        4176 :               seq.safe_push (first_edge_seq[i]);
    2639                 :        4176 :               unsigned new_idx;
    2640                 :        4176 :               if ((first_edge_seq[i].second == sm_ord
    2641                 :        3658 :                    || (first_edge_seq[i].second == sm_other
    2642                 :        3658 :                        && first_edge_seq[i].from != NULL_TREE))
    2643                 :        5695 :                   && !sm_seq_push_down (seq, seq.length () - 1, &new_idx))
    2644                 :             :                 {
    2645                 :         475 :                   if (first_edge_seq[i].second == sm_ord)
    2646                 :           0 :                     bitmap_set_bit (refs_not_supported, id);
    2647                 :             :                   /* Mark it sm_other.  */
    2648                 :         475 :                   seq[new_idx].second = sm_other;
    2649                 :         475 :                   seq[new_idx].from = NULL_TREE;
    2650                 :             :                 }
    2651                 :             :             }
    2652                 :        1973 :           bitmap_set_bit (fully_visited,
    2653                 :        1973 :                           SSA_NAME_VERSION (gimple_phi_result (phi)));
    2654                 :        1973 :           return 1;
    2655                 :        3989 :         }
    2656                 :       43716 :       lim_aux_data *data = get_lim_data (def);
    2657                 :       43716 :       gcc_assert (data);
    2658                 :       43716 :       if (data->ref == UNANALYZABLE_MEM_ID)
    2659                 :             :         return -1;
    2660                 :             :       /* Stop at memory references which we can't move.  */
    2661                 :       43716 :       else if (memory_accesses.refs_list[data->ref]->mem.ref == error_mark_node
    2662                 :       43716 :                || TREE_THIS_VOLATILE
    2663                 :             :                     (memory_accesses.refs_list[data->ref]->mem.ref))
    2664                 :             :         {
    2665                 :             :           /* Mark refs_not_in_seq as unsupported.  */
    2666                 :         512 :           bitmap_ior_into (refs_not_supported, refs_not_in_seq);
    2667                 :         512 :           return 1;
    2668                 :             :         }
    2669                 :             :       /* One of the stores we want to apply SM to and we've not yet seen.  */
    2670                 :       43204 :       else if (bitmap_clear_bit (refs_not_in_seq, data->ref))
    2671                 :             :         {
    2672                 :       31765 :           seq.safe_push (seq_entry (data->ref, sm_ord));
    2673                 :             : 
    2674                 :             :           /* 1) push it down the queue until a SMed
    2675                 :             :              and not ignored ref is reached, skipping all not SMed refs
    2676                 :             :              and ignored refs via non-TBAA disambiguation.  */
    2677                 :       31765 :           unsigned new_idx;
    2678                 :       63530 :           if (!sm_seq_push_down (seq, seq.length () - 1, &new_idx)
    2679                 :             :               /* If that fails but we did not fork yet continue, we'll see
    2680                 :             :                  to re-materialize all of the stores in the sequence then.
    2681                 :             :                  Further stores will only be pushed up to this one.  */
    2682                 :       31765 :               && forked)
    2683                 :             :             {
    2684                 :           0 :               bitmap_set_bit (refs_not_supported, data->ref);
    2685                 :             :               /* Mark it sm_other.  */
    2686                 :           0 :               seq[new_idx].second = sm_other;
    2687                 :             :             }
    2688                 :             : 
    2689                 :             :           /* 2) check whether we've seen all refs we want to SM and if so
    2690                 :             :              declare success for the active exit  */
    2691                 :       31765 :           if (bitmap_empty_p (refs_not_in_seq))
    2692                 :       18183 :             return 1;
    2693                 :             :         }
    2694                 :             :       else
    2695                 :             :         /* Another store not part of the final sequence.  Simply push it.  */
    2696                 :       11439 :         seq.safe_push (seq_entry (data->ref, sm_other,
    2697                 :             :                                   gimple_assign_rhs1 (def)));
    2698                 :             : 
    2699                 :       81222 :       vdef = gimple_vuse (def);
    2700                 :             :     }
    2701                 :             :   while (1);
    2702                 :             : }
    2703                 :             : 
    2704                 :             : /* Hoists memory references MEM_REFS out of LOOP.  EXITS is the list of exit
    2705                 :             :    edges of the LOOP.  */
    2706                 :             : 
    2707                 :             : static void
    2708                 :       20185 : hoist_memory_references (class loop *loop, bitmap mem_refs,
    2709                 :             :                          const vec<edge> &exits)
    2710                 :             : {
    2711                 :       20185 :   im_mem_ref *ref;
    2712                 :       20185 :   unsigned  i;
    2713                 :       20185 :   bitmap_iterator bi;
    2714                 :             : 
    2715                 :             :   /* There's a special case we can use ordered re-materialization for
    2716                 :             :      conditionally excuted stores which is when all stores in the loop
    2717                 :             :      happen in the same basic-block.  In that case we know we'll reach
    2718                 :             :      all stores and thus can simply process that BB and emit a single
    2719                 :             :      conditional block of ordered materializations.  See PR102436.  */
    2720                 :       20185 :   basic_block single_store_bb = NULL;
    2721                 :       29389 :   EXECUTE_IF_SET_IN_BITMAP (&memory_accesses.all_refs_stored_in_loop[loop->num],
    2722                 :             :                             0, i, bi)
    2723                 :             :     {
    2724                 :       27455 :       bool fail = false;
    2725                 :       27455 :       ref = memory_accesses.refs_list[i];
    2726                 :      125330 :       for (auto loc : ref->accesses_in_loop)
    2727                 :      134378 :         if (!gimple_vdef (loc.stmt))
    2728                 :             :           ;
    2729                 :       30197 :         else if (!single_store_bb)
    2730                 :             :           {
    2731                 :       20185 :             single_store_bb = gimple_bb (loc.stmt);
    2732                 :       20185 :             bool conditional = false;
    2733                 :       72849 :             for (edge e : exits)
    2734                 :       21730 :               if (!dominated_by_p (CDI_DOMINATORS, e->src, single_store_bb))
    2735                 :             :                 {
    2736                 :             :                   /* Conditional as seen from e.  */
    2737                 :             :                   conditional = true;
    2738                 :             :                   break;
    2739                 :             :                 }
    2740                 :       20185 :             if (!conditional)
    2741                 :             :               {
    2742                 :             :                 fail = true;
    2743                 :             :                 break;
    2744                 :             :               }
    2745                 :             :           }
    2746                 :       10012 :         else if (single_store_bb != gimple_bb (loc.stmt))
    2747                 :             :           {
    2748                 :             :             fail = true;
    2749                 :             :             break;
    2750                 :             :           }
    2751                 :       27455 :       if (fail)
    2752                 :             :         {
    2753                 :             :           single_store_bb = NULL;
    2754                 :             :           break;
    2755                 :             :         }
    2756                 :             :     }
    2757                 :       20185 :   if (single_store_bb)
    2758                 :             :     {
    2759                 :             :       /* Analyze the single block with stores.  */
    2760                 :        1934 :       auto_bitmap fully_visited;
    2761                 :        1934 :       auto_bitmap refs_not_supported;
    2762                 :        1934 :       auto_bitmap refs_not_in_seq;
    2763                 :        1934 :       auto_vec<seq_entry> seq;
    2764                 :        1934 :       bitmap_copy (refs_not_in_seq, mem_refs);
    2765                 :        1934 :       int res = sm_seq_valid_bb (loop, single_store_bb, NULL_TREE,
    2766                 :             :                                  seq, refs_not_in_seq, refs_not_supported,
    2767                 :             :                                  false, fully_visited);
    2768                 :        1934 :       if (res != 1)
    2769                 :             :         {
    2770                 :             :           /* Unhandled refs can still fail this.  */
    2771                 :           0 :           bitmap_clear (mem_refs);
    2772                 :           0 :           return;
    2773                 :             :         }
    2774                 :             : 
    2775                 :             :       /* We cannot handle sm_other since we neither remember the
    2776                 :             :          stored location nor the value at the point we execute them.  */
    2777                 :        9094 :       for (unsigned i = 0; i < seq.length (); ++i)
    2778                 :             :         {
    2779                 :        2655 :           unsigned new_i;
    2780                 :        2655 :           if (seq[i].second == sm_other
    2781                 :        2655 :               && seq[i].from != NULL_TREE)
    2782                 :         326 :             seq[i].from = NULL_TREE;
    2783                 :        2329 :           else if ((seq[i].second == sm_ord
    2784                 :           0 :                     || (seq[i].second == sm_other
    2785                 :           0 :                         && seq[i].from != NULL_TREE))
    2786                 :        2329 :                    && !sm_seq_push_down (seq, i, &new_i))
    2787                 :             :             {
    2788                 :          34 :               bitmap_set_bit (refs_not_supported, seq[new_i].first);
    2789                 :          34 :               seq[new_i].second = sm_other;
    2790                 :          34 :               seq[new_i].from = NULL_TREE;
    2791                 :             :             }
    2792                 :             :         }
    2793                 :        1934 :       bitmap_and_compl_into (mem_refs, refs_not_supported);
    2794                 :        1934 :       if (bitmap_empty_p (mem_refs))
    2795                 :             :         return;
    2796                 :             : 
    2797                 :             :       /* Prune seq.  */
    2798                 :        2131 :       while (seq.last ().second == sm_other
    2799                 :        2131 :              && seq.last ().from == NULL_TREE)
    2800                 :         305 :         seq.pop ();
    2801                 :             : 
    2802                 :        1826 :       hash_map<im_mem_ref *, sm_aux *> aux_map;
    2803                 :             : 
    2804                 :             :       /* Execute SM but delay the store materialization for ordered
    2805                 :             :          sequences on exit.  */
    2806                 :        1826 :       bool first_p = true;
    2807                 :        4121 :       EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi)
    2808                 :             :         {
    2809                 :        2295 :           ref = memory_accesses.refs_list[i];
    2810                 :        2295 :           execute_sm (loop, ref, aux_map, true, !first_p);
    2811                 :        2295 :           first_p = false;
    2812                 :             :         }
    2813                 :             : 
    2814                 :             :       /* Get at the single flag variable we eventually produced.  */
    2815                 :        1826 :       im_mem_ref *ref
    2816                 :        1826 :         = memory_accesses.refs_list[bitmap_first_set_bit (mem_refs)];
    2817                 :        1826 :       sm_aux *aux = *aux_map.get (ref);
    2818                 :             : 
    2819                 :             :       /* Materialize ordered store sequences on exits.  */
    2820                 :        1826 :       edge e;
    2821                 :        4794 :       FOR_EACH_VEC_ELT (exits, i, e)
    2822                 :             :         {
    2823                 :        2968 :           edge append_cond_position = NULL;
    2824                 :        2968 :           edge last_cond_fallthru = NULL;
    2825                 :        2968 :           edge insert_e = e;
    2826                 :             :           /* Construct the single flag variable control flow and insert
    2827                 :             :              the ordered seq of stores in the then block.  With
    2828                 :             :              -fstore-data-races we can do the stores unconditionally.  */
    2829                 :        2968 :           if (aux->store_flag)
    2830                 :        2941 :             insert_e
    2831                 :             :               = single_pred_edge
    2832                 :        2941 :                   (execute_sm_if_changed (e, NULL_TREE, NULL_TREE,
    2833                 :             :                                           aux->store_flag,
    2834                 :             :                                           loop_preheader_edge (loop),
    2835                 :             :                                           &aux->flag_bbs, append_cond_position,
    2836                 :             :                                           last_cond_fallthru));
    2837                 :        2968 :           execute_sm_exit (loop, insert_e, seq, aux_map, sm_ord,
    2838                 :             :                            append_cond_position, last_cond_fallthru);
    2839                 :        2968 :           gsi_commit_one_edge_insert (insert_e, NULL);
    2840                 :             :         }
    2841                 :             : 
    2842                 :        4121 :       for (hash_map<im_mem_ref *, sm_aux *>::iterator iter = aux_map.begin ();
    2843                 :        6416 :            iter != aux_map.end (); ++iter)
    2844                 :        4590 :         delete (*iter).second;
    2845                 :             : 
    2846                 :        1826 :       return;
    2847                 :        3760 :     }
    2848                 :             : 
    2849                 :             :   /* To address PR57359 before actually applying store-motion check
    2850                 :             :      the candidates found for validity with regards to reordering
    2851                 :             :      relative to other stores which we until here disambiguated using
    2852                 :             :      TBAA which isn't valid.
    2853                 :             :      What matters is the order of the last stores to the mem_refs
    2854                 :             :      with respect to the other stores of the loop at the point of the
    2855                 :             :      loop exits.  */
    2856                 :             : 
    2857                 :             :   /* For each exit compute the store order, pruning from mem_refs
    2858                 :             :      on the fly.  */
    2859                 :             :   /* The complexity of this is at least
    2860                 :             :      O(number of exits * number of SM refs) but more approaching
    2861                 :             :      O(number of exits * number of SM refs * number of stores).  */
    2862                 :             :   /* ???  Somehow do this in a single sweep over the loop body.  */
    2863                 :       18251 :   auto_vec<std::pair<edge, vec<seq_entry> > > sms;
    2864                 :       18251 :   auto_bitmap refs_not_supported (&lim_bitmap_obstack);
    2865                 :       18251 :   edge e;
    2866                 :       37238 :   FOR_EACH_VEC_ELT (exits, i, e)
    2867                 :             :     {
    2868                 :       21175 :       vec<seq_entry> seq;
    2869                 :       21175 :       seq.create (4);
    2870                 :       21175 :       auto_bitmap refs_not_in_seq (&lim_bitmap_obstack);
    2871                 :       21175 :       bitmap_and_compl (refs_not_in_seq, mem_refs, refs_not_supported);
    2872                 :       21175 :       if (bitmap_empty_p (refs_not_in_seq))
    2873                 :             :         {
    2874                 :        1478 :           seq.release ();
    2875                 :        1478 :           break;
    2876                 :             :         }
    2877                 :       19697 :       auto_bitmap fully_visited;
    2878                 :       19697 :       int res = sm_seq_valid_bb (loop, e->src, NULL_TREE,
    2879                 :             :                                  seq, refs_not_in_seq,
    2880                 :             :                                  refs_not_supported, false,
    2881                 :             :                                  fully_visited);
    2882                 :       19697 :       if (res != 1)
    2883                 :             :         {
    2884                 :         710 :           bitmap_copy (refs_not_supported, mem_refs);
    2885                 :         710 :           seq.release ();
    2886                 :         710 :           break;
    2887                 :             :         }
    2888                 :       18987 :       sms.safe_push (std::make_pair (e, seq));
    2889                 :       21885 :     }
    2890                 :             : 
    2891                 :             :   /* Prune pruned mem_refs from earlier processed exits.  */
    2892                 :       18251 :   bool changed = !bitmap_empty_p (refs_not_supported);
    2893                 :       18251 :   while (changed)
    2894                 :             :     {
    2895                 :        5946 :       changed = false;
    2896                 :        5946 :       std::pair<edge, vec<seq_entry> > *seq;
    2897                 :       38307 :       FOR_EACH_VEC_ELT (sms, i, seq)
    2898                 :             :         {
    2899                 :             :           bool need_to_push = false;
    2900                 :       23472 :           for (unsigned i = 0; i < seq->second.length (); ++i)
    2901                 :             :             {
    2902                 :        5696 :               sm_kind kind = seq->second[i].second;
    2903                 :        5696 :               if (kind == sm_other && seq->second[i].from == NULL_TREE)
    2904                 :             :                 break;
    2905                 :        4774 :               unsigned id = seq->second[i].first;
    2906                 :        4774 :               unsigned new_idx;
    2907                 :        4774 :               if (kind == sm_ord
    2908                 :        4774 :                   && bitmap_bit_p (refs_not_supported, id))
    2909                 :             :                 {
    2910                 :        2201 :                   seq->second[i].second = sm_other;
    2911                 :        2201 :                   gcc_assert (seq->second[i].from == NULL_TREE);
    2912                 :             :                   need_to_push = true;
    2913                 :             :                 }
    2914                 :        2573 :               else if (need_to_push
    2915                 :        2573 :                        && !sm_seq_push_down (seq->second, i, &new_idx))
    2916                 :             :                 {
    2917                 :             :                   /* We need to push down both sm_ord and sm_other
    2918                 :             :                      but for the latter we need to disqualify all
    2919                 :             :                      following refs.  */
    2920                 :         100 :                   if (kind == sm_ord)
    2921                 :             :                     {
    2922                 :           7 :                       if (bitmap_set_bit (refs_not_supported, id))
    2923                 :           7 :                         changed = true;
    2924                 :           7 :                       seq->second[new_idx].second = sm_other;
    2925                 :             :                     }
    2926                 :             :                   else
    2927                 :             :                     {
    2928                 :         352 :                       for (unsigned j = seq->second.length () - 1;
    2929                 :         259 :                            j > new_idx; --j)
    2930                 :         166 :                         if (seq->second[j].second == sm_ord
    2931                 :         267 :                             && bitmap_set_bit (refs_not_supported,
    2932                 :         101 :                                                seq->second[j].first))
    2933                 :             :                           changed = true;
    2934                 :          93 :                       seq->second.truncate (new_idx);
    2935                 :          93 :                       break;
    2936                 :             :                     }
    2937                 :             :                 }
    2938                 :             :             }
    2939                 :             :         }
    2940                 :             :     }
    2941                 :       18251 :   std::pair<edge, vec<seq_entry> > *seq;
    2942                 :       56225 :   FOR_EACH_VEC_ELT (sms, i, seq)
    2943                 :             :     {
    2944                 :             :       /* Prune sm_other from the end.  */
    2945                 :       59691 :       while (!seq->second.is_empty ()
    2946                 :       40704 :              && seq->second.last ().second == sm_other)
    2947                 :        4432 :         seq->second.pop ();
    2948                 :             :       /* Prune duplicates from the start.  */
    2949                 :       18987 :       auto_bitmap seen (&lim_bitmap_obstack);
    2950                 :       18987 :       unsigned j, k;
    2951                 :       91036 :       for (j = k = 0; j < seq->second.length (); ++j)
    2952                 :       26531 :         if (bitmap_set_bit (seen, seq->second[j].first))
    2953                 :             :           {
    2954                 :       26400 :             if (k != j)
    2955                 :         107 :               seq->second[k] = seq->second[j];
    2956                 :       26400 :             ++k;
    2957                 :             :           }
    2958                 :       18987 :       seq->second.truncate (k);
    2959                 :             :       /* And verify.  */
    2960                 :       18987 :       seq_entry *e;
    2961                 :       83361 :       FOR_EACH_VEC_ELT (seq->second, j, e)
    2962                 :       26400 :         gcc_assert (e->second == sm_ord
    2963                 :             :                     || (e->second == sm_other && e->from != NULL_TREE));
    2964                 :       18987 :     }
    2965                 :             : 
    2966                 :             :   /* Verify dependence for refs we cannot handle with the order preserving
    2967                 :             :      code (refs_not_supported) or prune them from mem_refs.  */
    2968                 :       18251 :   auto_vec<seq_entry> unord_refs;
    2969                 :       33448 :   EXECUTE_IF_SET_IN_BITMAP (refs_not_supported, 0, i, bi)
    2970                 :             :     {
    2971                 :       15197 :       ref = memory_accesses.refs_list[i];
    2972                 :       15197 :       if (!ref_indep_loop_p (loop, ref, sm_waw))
    2973                 :        6807 :         bitmap_clear_bit (mem_refs, i);
    2974                 :             :       /* We've now verified store order for ref with respect to all other
    2975                 :             :          stores in the loop does not matter.  */
    2976                 :             :       else
    2977                 :        8390 :         unord_refs.safe_push (seq_entry (i, sm_unord));
    2978                 :             :     }
    2979                 :             : 
    2980                 :       18251 :   hash_map<im_mem_ref *, sm_aux *> aux_map;
    2981                 :             : 
    2982                 :             :   /* Execute SM but delay the store materialization for ordered
    2983                 :             :      sequences on exit.  */
    2984                 :       48771 :   EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi)
    2985                 :             :     {
    2986                 :       30520 :       ref = memory_accesses.refs_list[i];
    2987                 :       30520 :       execute_sm (loop, ref, aux_map, bitmap_bit_p (refs_not_supported, i),
    2988                 :             :                   false);
    2989                 :             :     }
    2990                 :             : 
    2991                 :             :   /* Materialize ordered store sequences on exits.  */
    2992                 :       40897 :   FOR_EACH_VEC_ELT (exits, i, e)
    2993                 :             :     {
    2994                 :       22646 :       edge append_cond_position = NULL;
    2995                 :       22646 :       edge last_cond_fallthru = NULL;
    2996                 :       44598 :       if (i < sms.length ())
    2997                 :             :         {
    2998                 :       18987 :           gcc_assert (sms[i].first == e);
    2999                 :       18987 :           execute_sm_exit (loop, e, sms[i].second, aux_map, sm_ord,
    3000                 :             :                            append_cond_position, last_cond_fallthru);
    3001                 :       18987 :           sms[i].second.release ();
    3002                 :             :         }
    3003                 :       22646 :       if (!unord_refs.is_empty ())
    3004                 :        8869 :         execute_sm_exit (loop, e, unord_refs, aux_map, sm_unord,
    3005                 :             :                          append_cond_position, last_cond_fallthru);
    3006                 :             :       /* Commit edge inserts here to preserve the order of stores
    3007                 :             :          when an exit exits multiple loops.  */
    3008                 :       22646 :       gsi_commit_one_edge_insert (e, NULL);
    3009                 :             :     }
    3010                 :             : 
    3011                 :       48771 :   for (hash_map<im_mem_ref *, sm_aux *>::iterator iter = aux_map.begin ();
    3012                 :       97542 :        iter != aux_map.end (); ++iter)
    3013                 :       61040 :     delete (*iter).second;
    3014                 :       18251 : }
    3015                 :             : 
    3016                 :             : class ref_always_accessed
    3017                 :             : {
    3018                 :             : public:
    3019                 :       77488 :   ref_always_accessed (class loop *loop_, bool stored_p_)
    3020                 :       77488 :       : loop (loop_), stored_p (stored_p_) {}
    3021                 :             :   bool operator () (mem_ref_loc *loc);
    3022                 :             :   class loop *loop;
    3023                 :             :   bool stored_p;
    3024                 :             : };
    3025                 :             : 
    3026                 :             : bool
    3027                 :      154906 : ref_always_accessed::operator () (mem_ref_loc *loc)
    3028                 :             : {
    3029                 :      154906 :   class loop *must_exec;
    3030                 :             : 
    3031                 :      154906 :   struct lim_aux_data *lim_data = get_lim_data (loc->stmt);
    3032                 :      154906 :   if (!lim_data)
    3033                 :             :     return false;
    3034                 :             : 
    3035                 :             :   /* If we require an always executed store make sure the statement
    3036                 :             :      is a store.  */
    3037                 :      154906 :   if (stored_p)
    3038                 :             :     {
    3039                 :      154906 :       tree lhs = gimple_get_lhs (loc->stmt);
    3040                 :      154906 :       if (!lhs
    3041                 :      154906 :           || !(DECL_P (lhs) || REFERENCE_CLASS_P (lhs)))
    3042                 :             :         return false;
    3043                 :             :     }
    3044                 :             : 
    3045                 :       96274 :   must_exec = lim_data->always_executed_in;
    3046                 :       96274 :   if (!must_exec)
    3047                 :             :     return false;
    3048                 :             : 
    3049                 :       33757 :   if (must_exec == loop
    3050                 :       33757 :       || flow_loop_nested_p (must_exec, loop))
    3051                 :       31222 :     return true;
    3052                 :             : 
    3053                 :             :   return false;
    3054                 :             : }
    3055                 :             : 
    3056                 :             : /* Returns true if REF is always accessed in LOOP.  If STORED_P is true
    3057                 :             :    make sure REF is always stored to in LOOP.  */
    3058                 :             : 
    3059                 :             : static bool
    3060                 :       77488 : ref_always_accessed_p (class loop *loop, im_mem_ref *ref, bool stored_p)
    3061                 :             : {
    3062                 :       77488 :   return for_all_locs_in_loop (loop, ref,
    3063                 :           0 :                                ref_always_accessed (loop, stored_p));
    3064                 :             : }
    3065                 :             : 
    3066                 :             : /* Returns true if REF1 and REF2 are independent.  */
    3067                 :             : 
    3068                 :             : static bool
    3069                 :      538983 : refs_independent_p (im_mem_ref *ref1, im_mem_ref *ref2, bool tbaa_p)
    3070                 :             : {
    3071                 :      538983 :   if (ref1 == ref2)
    3072                 :             :     return true;
    3073                 :             : 
    3074                 :      484888 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3075                 :        3785 :     fprintf (dump_file, "Querying dependency of refs %u and %u: ",
    3076                 :        3785 :              ref1->id, ref2->id);
    3077                 :             : 
    3078                 :      484888 :   if (mem_refs_may_alias_p (ref1, ref2, &memory_accesses.ttae_cache, tbaa_p))
    3079                 :             :     {
    3080                 :       53481 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3081                 :         124 :         fprintf (dump_file, "dependent.\n");
    3082                 :       53481 :       return false;
    3083                 :             :     }
    3084                 :             :   else
    3085                 :             :     {
    3086                 :      431407 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3087                 :        3661 :         fprintf (dump_file, "independent.\n");
    3088                 :      431407 :       return true;
    3089                 :             :     }
    3090                 :             : }
    3091                 :             : 
    3092                 :             : /* Returns true if REF is independent on all other accessess in LOOP.
    3093                 :             :    KIND specifies the kind of dependence to consider.
    3094                 :             :      lim_raw assumes REF is not stored in LOOP and disambiguates RAW
    3095                 :             :              dependences so if true REF can be hoisted out of LOOP
    3096                 :             :      sm_war disambiguates a store REF against all other loads to see
    3097                 :             :             whether the store can be sunk across loads out of LOOP
    3098                 :             :      sm_waw disambiguates a store REF against all other stores to see
    3099                 :             :             whether the store can be sunk across stores out of LOOP.  */
    3100                 :             : 
    3101                 :             : static bool
    3102                 :     1812275 : ref_indep_loop_p (class loop *loop, im_mem_ref *ref, dep_kind kind)
    3103                 :             : {
    3104                 :     1812275 :   bool indep_p = true;
    3105                 :     1812275 :   bitmap refs_to_check;
    3106                 :             : 
    3107                 :     1812275 :   if (kind == sm_war)
    3108                 :      821135 :     refs_to_check = &memory_accesses.refs_loaded_in_loop[loop->num];
    3109                 :             :   else
    3110                 :      991140 :     refs_to_check = &memory_accesses.refs_stored_in_loop[loop->num];
    3111                 :             : 
    3112                 :     1812275 :   if (bitmap_bit_p (refs_to_check, UNANALYZABLE_MEM_ID)
    3113                 :     1812275 :       || ref->mem.ref == error_mark_node)
    3114                 :             :     indep_p = false;
    3115                 :             :   else
    3116                 :             :     {
    3117                 :             :       /* tri-state, { unknown, independent, dependent }  */
    3118                 :      567030 :       dep_state state = query_loop_dependence (loop, ref, kind);
    3119                 :      567030 :       if (state != dep_unknown)
    3120                 :           0 :         return state == dep_independent ? true : false;
    3121                 :             : 
    3122                 :      567030 :       class loop *inner = loop->inner;
    3123                 :      701672 :       while (inner)
    3124                 :             :         {
    3125                 :      384454 :           if (!ref_indep_loop_p (inner, ref, kind))
    3126                 :             :             {
    3127                 :             :               indep_p = false;
    3128                 :             :               break;
    3129                 :             :             }
    3130                 :      134642 :           inner = inner->next;
    3131                 :             :         }
    3132                 :             : 
    3133                 :      567030 :       if (indep_p)
    3134                 :             :         {
    3135                 :      317218 :           unsigned i;
    3136                 :      317218 :           bitmap_iterator bi;
    3137                 :      834936 :           EXECUTE_IF_SET_IN_BITMAP (refs_to_check, 0, i, bi)
    3138                 :             :             {
    3139                 :      576535 :               im_mem_ref *aref = memory_accesses.refs_list[i];
    3140                 :      576535 :               if (aref->mem.ref == error_mark_node)
    3141                 :             :                 {
    3142                 :       38384 :                   gimple *stmt = aref->accesses_in_loop[0].stmt;
    3143                 :       38384 :                   if ((kind == sm_war
    3144                 :       16282 :                        && ref_maybe_used_by_stmt_p (stmt, &ref->mem,
    3145                 :             :                                                     kind != sm_waw))
    3146                 :       54220 :                       || stmt_may_clobber_ref_p_1 (stmt, &ref->mem,
    3147                 :             :                                                    kind != sm_waw))
    3148                 :             :                     {
    3149                 :             :                       indep_p = false;
    3150                 :             :                       break;
    3151                 :             :                     }
    3152                 :             :                 }
    3153                 :      538151 :               else if (!refs_independent_p (ref, aref, kind != sm_waw))
    3154                 :             :                 {
    3155                 :             :                   indep_p = false;
    3156                 :             :                   break;
    3157                 :             :                 }
    3158                 :             :             }
    3159                 :             :         }
    3160                 :             :     }
    3161                 :             : 
    3162                 :     1812275 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3163                 :         833 :     fprintf (dump_file, "Querying %s dependencies of ref %u in loop %d: %s\n",
    3164                 :             :              kind == lim_raw ? "RAW" : (kind == sm_war ? "SM WAR" : "SM WAW"),
    3165                 :         404 :              ref->id, loop->num, indep_p ? "independent" : "dependent");
    3166                 :             : 
    3167                 :             :   /* Record the computed result in the cache.  */
    3168                 :     1812275 :   record_loop_dependence (loop, ref, kind,
    3169                 :             :                           indep_p ? dep_independent : dep_dependent);
    3170                 :             : 
    3171                 :     1812275 :   return indep_p;
    3172                 :             : }
    3173                 :             : 
    3174                 :             : class ref_in_loop_hot_body
    3175                 :             : {
    3176                 :             : public:
    3177                 :       40569 :   ref_in_loop_hot_body (class loop *loop_) : l (loop_) {}
    3178                 :             :   bool operator () (mem_ref_loc *loc);
    3179                 :             :   class loop *l;
    3180                 :             : };
    3181                 :             : 
    3182                 :             : /* Check the coldest loop between loop L and innermost loop.  If there is one
    3183                 :             :    cold loop between L and INNER_LOOP, store motion can be performed, otherwise
    3184                 :             :    no cold loop means no store motion.  get_coldest_out_loop also handles cases
    3185                 :             :    when l is inner_loop.  */
    3186                 :             : bool
    3187                 :       41256 : ref_in_loop_hot_body::operator () (mem_ref_loc *loc)
    3188                 :             : {
    3189                 :       41256 :   basic_block curr_bb = gimple_bb (loc->stmt);
    3190                 :       41256 :   class loop *inner_loop = curr_bb->loop_father;
    3191                 :       41256 :   return get_coldest_out_loop (l, inner_loop, curr_bb);
    3192                 :             : }
    3193                 :             : 
    3194                 :             : 
    3195                 :             : /* Returns true if we can perform store motion of REF from LOOP.  */
    3196                 :             : 
    3197                 :             : static bool
    3198                 :     2490571 : can_sm_ref_p (class loop *loop, im_mem_ref *ref)
    3199                 :             : {
    3200                 :     2490571 :   tree base;
    3201                 :             : 
    3202                 :             :   /* Can't hoist unanalyzable refs.  */
    3203                 :     2490571 :   if (!MEM_ANALYZABLE (ref))
    3204                 :             :     return false;
    3205                 :             : 
    3206                 :             :   /* Can't hoist/sink aggregate copies.  */
    3207                 :     2170947 :   if (ref->mem.ref == error_mark_node)
    3208                 :             :     return false;
    3209                 :             : 
    3210                 :             :   /* It should be movable.  */
    3211                 :     1736186 :   if (!is_gimple_reg_type (TREE_TYPE (ref->mem.ref))
    3212                 :     1735924 :       || TREE_THIS_VOLATILE (ref->mem.ref)
    3213                 :     3455543 :       || !for_each_index (&ref->mem.ref, may_move_till, loop))
    3214                 :      948862 :     return false;
    3215                 :             : 
    3216                 :             :   /* If it can throw fail, we do not properly update EH info.  */
    3217                 :      787324 :   if (tree_could_throw_p (ref->mem.ref))
    3218                 :             :     return false;
    3219                 :             : 
    3220                 :             :   /* If it can trap, it must be always executed in LOOP.
    3221                 :             :      Readonly memory locations may trap when storing to them, but
    3222                 :             :      tree_could_trap_p is a predicate for rvalues, so check that
    3223                 :             :      explicitly.  */
    3224                 :      773579 :   base = get_base_address (ref->mem.ref);
    3225                 :      773579 :   if ((tree_could_trap_p (ref->mem.ref)
    3226                 :      729075 :        || (DECL_P (base) && TREE_READONLY (base)))
    3227                 :             :       /* ???  We can at least use false here, allowing loads?  We
    3228                 :             :          are forcing conditional stores if the ref is not always
    3229                 :             :          stored to later anyway.  So this would only guard
    3230                 :             :          the load we need to emit.  Thus when the ref is not
    3231                 :             :          loaded we can elide this completely?  */
    3232                 :      818252 :       && !ref_always_accessed_p (loop, ref, true))
    3233                 :             :     return false;
    3234                 :             : 
    3235                 :             :   /* Verify all loads of ref can be hoisted.  */
    3236                 :      737944 :   if (ref->loaded
    3237                 :       76972 :       && bitmap_bit_p (ref->loaded, loop->num)
    3238                 :      810951 :       && !ref_indep_loop_p (loop, ref, lim_raw))
    3239                 :             :     return false;
    3240                 :             : 
    3241                 :             :   /* Verify the candidate can be disambiguated against all loads,
    3242                 :             :      that is, we can elide all in-loop stores.  Disambiguation
    3243                 :             :      against stores is done later when we cannot guarantee preserving
    3244                 :             :      the order of stores.  */
    3245                 :      685102 :   if (!ref_indep_loop_p (loop, ref, sm_war))
    3246                 :             :     return false;
    3247                 :             : 
    3248                 :             :   /* Verify whether the candidate is hot for LOOP.  Only do store motion if the
    3249                 :             :     candidate's profile count is hot.  Statement in cold BB shouldn't be moved
    3250                 :             :     out of it's loop_father.  */
    3251                 :       40569 :   if (!for_all_locs_in_loop (loop, ref, ref_in_loop_hot_body (loop)))
    3252                 :             :     return false;
    3253                 :             : 
    3254                 :             :   return true;
    3255                 :             : }
    3256                 :             : 
    3257                 :             : /* Marks the references in LOOP for that store motion should be performed
    3258                 :             :    in REFS_TO_SM.  SM_EXECUTED is the set of references for that store
    3259                 :             :    motion was performed in one of the outer loops.  */
    3260                 :             : 
    3261                 :             : static void
    3262                 :     1087087 : find_refs_for_sm (class loop *loop, bitmap sm_executed, bitmap refs_to_sm)
    3263                 :             : {
    3264                 :     1087087 :   bitmap refs = &memory_accesses.all_refs_stored_in_loop[loop->num];
    3265                 :     1087087 :   unsigned i;
    3266                 :     1087087 :   bitmap_iterator bi;
    3267                 :     1087087 :   im_mem_ref *ref;
    3268                 :             : 
    3269                 :     3577658 :   EXECUTE_IF_AND_COMPL_IN_BITMAP (refs, sm_executed, 0, i, bi)
    3270                 :             :     {
    3271                 :     2490571 :       ref = memory_accesses.refs_list[i];
    3272                 :     2490571 :       if (can_sm_ref_p (loop, ref) && dbg_cnt (lim))
    3273                 :       39747 :         bitmap_set_bit (refs_to_sm, i);
    3274                 :             :     }
    3275                 :     1087087 : }
    3276                 :             : 
    3277                 :             : /* Checks whether LOOP (with exits stored in EXITS array) is suitable
    3278                 :             :    for a store motion optimization (i.e. whether we can insert statement
    3279                 :             :    on its exits).  */
    3280                 :             : 
    3281                 :             : static bool
    3282                 :     1134476 : loop_suitable_for_sm (class loop *loop ATTRIBUTE_UNUSED,
    3283                 :             :                       const vec<edge> &exits)
    3284                 :             : {
    3285                 :     1134476 :   unsigned i;
    3286                 :     1134476 :   edge ex;
    3287                 :             : 
    3288                 :     2906912 :   FOR_EACH_VEC_ELT (exits, i, ex)
    3289                 :     1819825 :     if (ex->flags & (EDGE_ABNORMAL | EDGE_EH))
    3290                 :             :       return false;
    3291                 :             : 
    3292                 :             :   return true;
    3293                 :             : }
    3294                 :             : 
    3295                 :             : /* Try to perform store motion for all memory references modified inside
    3296                 :             :    LOOP.  SM_EXECUTED is the bitmap of the memory references for that
    3297                 :             :    store motion was executed in one of the outer loops.  */
    3298                 :             : 
    3299                 :             : static void
    3300                 :     1134476 : store_motion_loop (class loop *loop, bitmap sm_executed)
    3301                 :             : {
    3302                 :     1134476 :   auto_vec<edge> exits = get_loop_exit_edges (loop);
    3303                 :     1134476 :   class loop *subloop;
    3304                 :     1134476 :   bitmap sm_in_loop = BITMAP_ALLOC (&lim_bitmap_obstack);
    3305                 :             : 
    3306                 :     1134476 :   if (loop_suitable_for_sm (loop, exits))
    3307                 :             :     {
    3308                 :     1087087 :       find_refs_for_sm (loop, sm_executed, sm_in_loop);
    3309                 :     1087087 :       if (!bitmap_empty_p (sm_in_loop))
    3310                 :       20185 :         hoist_memory_references (loop, sm_in_loop, exits);
    3311                 :             :     }
    3312                 :             : 
    3313                 :     1134476 :   bitmap_ior_into (sm_executed, sm_in_loop);
    3314                 :     1385380 :   for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
    3315                 :      250904 :     store_motion_loop (subloop, sm_executed);
    3316                 :     1134476 :   bitmap_and_compl_into (sm_executed, sm_in_loop);
    3317                 :     1134476 :   BITMAP_FREE (sm_in_loop);
    3318                 :     1134476 : }
    3319                 :             : 
    3320                 :             : /* Try to perform store motion for all memory references modified inside
    3321                 :             :    loops.  */
    3322                 :             : 
    3323                 :             : static void
    3324                 :      440545 : do_store_motion (void)
    3325                 :             : {
    3326                 :      440545 :   class loop *loop;
    3327                 :      440545 :   bitmap sm_executed = BITMAP_ALLOC (&lim_bitmap_obstack);
    3328                 :             : 
    3329                 :     1324117 :   for (loop = current_loops->tree_root->inner; loop != NULL; loop = loop->next)
    3330                 :      883572 :     store_motion_loop (loop, sm_executed);
    3331                 :             : 
    3332                 :      440545 :   BITMAP_FREE (sm_executed);
    3333                 :      440545 : }
    3334                 :             : 
    3335                 :             : /* Fills ALWAYS_EXECUTED_IN information for basic blocks of LOOP, i.e.
    3336                 :             :    for each such basic block bb records the outermost loop for that execution
    3337                 :             :    of its header implies execution of bb.  CONTAINS_CALL is the bitmap of
    3338                 :             :    blocks that contain a nonpure call.  */
    3339                 :             : 
    3340                 :             : static void
    3341                 :     1134634 : fill_always_executed_in_1 (class loop *loop, sbitmap contains_call)
    3342                 :             : {
    3343                 :     1134634 :   basic_block bb = NULL, last = NULL;
    3344                 :     1134634 :   edge e;
    3345                 :     1134634 :   class loop *inn_loop = loop;
    3346                 :             : 
    3347                 :     1134634 :   if (ALWAYS_EXECUTED_IN (loop->header) == NULL)
    3348                 :             :     {
    3349                 :     1038552 :       auto_vec<basic_block, 64> worklist;
    3350                 :     1038552 :       worklist.reserve_exact (loop->num_nodes);
    3351                 :     1038552 :       worklist.quick_push (loop->header);
    3352                 :     2230394 :       do
    3353                 :             :         {
    3354                 :     2230394 :           edge_iterator ei;
    3355                 :     2230394 :           bb = worklist.pop ();
    3356                 :             : 
    3357                 :     2230394 :           if (!flow_bb_inside_loop_p (inn_loop, bb))
    3358                 :             :             {
    3359                 :             :               /* When we are leaving a possibly infinite inner loop
    3360                 :             :                  we have to stop processing.  */
    3361                 :      130633 :               if (!finite_loop_p (inn_loop))
    3362                 :             :                 break;
    3363                 :             :               /* If the loop was finite we can continue with processing
    3364                 :             :                  the loop we exited to.  */
    3365                 :      123196 :               inn_loop = bb->loop_father;
    3366                 :             :             }
    3367                 :             : 
    3368                 :     2222957 :           if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
    3369                 :     1316242 :             last = bb;
    3370                 :             : 
    3371                 :     2222957 :           if (bitmap_bit_p (contains_call, bb->index))
    3372                 :             :             break;
    3373                 :             : 
    3374                 :             :           /* If LOOP exits from this BB stop processing.  */
    3375                 :     4207975 :           FOR_EACH_EDGE (e, ei, bb->succs)
    3376                 :     3006853 :             if (!flow_bb_inside_loop_p (loop, e->dest))
    3377                 :             :               break;
    3378                 :     1939368 :           if (e)
    3379                 :             :             break;
    3380                 :             : 
    3381                 :             :           /* A loop might be infinite (TODO use simple loop analysis
    3382                 :             :              to disprove this if possible).  */
    3383                 :     1201122 :           if (bb->flags & BB_IRREDUCIBLE_LOOP)
    3384                 :             :             break;
    3385                 :             : 
    3386                 :     1201038 :           if (bb->loop_father->header == bb)
    3387                 :             :             /* Record that we enter into a subloop since it might not
    3388                 :             :                be finite.  */
    3389                 :             :             /* ???  Entering into a not always executed subloop makes
    3390                 :             :                fill_always_executed_in quadratic in loop depth since
    3391                 :             :                we walk those loops N times.  This is not a problem
    3392                 :             :                in practice though, see PR102253 for a worst-case testcase.  */
    3393                 :      462508 :             inn_loop = bb->loop_father;
    3394                 :             : 
    3395                 :             :           /* Walk the body of LOOP sorted by dominance relation.  Additionally,
    3396                 :             :              if a basic block S dominates the latch, then only blocks dominated
    3397                 :             :              by S are after it.
    3398                 :             :              This is get_loop_body_in_dom_order using a worklist algorithm and
    3399                 :             :              stopping once we are no longer interested in visiting further
    3400                 :             :              blocks.  */
    3401                 :     1201038 :           unsigned old_len = worklist.length ();
    3402                 :     1201038 :           unsigned postpone = 0;
    3403                 :     1201038 :           for (basic_block son = first_dom_son (CDI_DOMINATORS, bb);
    3404                 :     2621028 :                son;
    3405                 :     1419990 :                son = next_dom_son (CDI_DOMINATORS, son))
    3406                 :             :             {
    3407                 :     1419990 :               if (!flow_bb_inside_loop_p (loop, son))
    3408                 :       17272 :                 continue;
    3409                 :     1402718 :               if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
    3410                 :      395127 :                 postpone = worklist.length ();
    3411                 :     1402718 :               worklist.quick_push (son);
    3412                 :             :             }
    3413                 :     1201038 :           if (postpone)
    3414                 :             :             /* Postponing the block that dominates the latch means
    3415                 :             :                processing it last and thus putting it earliest in the
    3416                 :             :                worklist.  */
    3417                 :      121160 :             std::swap (worklist[old_len], worklist[postpone]);
    3418                 :             :         }
    3419                 :     2402076 :       while (!worklist.is_empty ());
    3420                 :             : 
    3421                 :     1593932 :       while (1)
    3422                 :             :         {
    3423                 :     1316242 :           if (dump_enabled_p ())
    3424                 :        1704 :             dump_printf (MSG_NOTE, "BB %d is always executed in loop %d\n",
    3425                 :             :                          last->index, loop->num);
    3426                 :     1316242 :           SET_ALWAYS_EXECUTED_IN (last, loop);
    3427                 :     1316242 :           if (last == loop->header)
    3428                 :             :             break;
    3429                 :      277690 :           last = get_immediate_dominator (CDI_DOMINATORS, last);
    3430                 :             :         }
    3431                 :     1038552 :     }
    3432                 :             : 
    3433                 :     1385628 :   for (loop = loop->inner; loop; loop = loop->next)
    3434                 :      250994 :     fill_always_executed_in_1 (loop, contains_call);
    3435                 :     1134634 : }
    3436                 :             : 
    3437                 :             : /* Fills ALWAYS_EXECUTED_IN information for basic blocks, i.e.
    3438                 :             :    for each such basic block bb records the outermost loop for that execution
    3439                 :             :    of its header implies execution of bb.  */
    3440                 :             : 
    3441                 :             : static void
    3442                 :      440602 : fill_always_executed_in (void)
    3443                 :             : {
    3444                 :      440602 :   basic_block bb;
    3445                 :      440602 :   class loop *loop;
    3446                 :             : 
    3447                 :      440602 :   auto_sbitmap contains_call (last_basic_block_for_fn (cfun));
    3448                 :      440602 :   bitmap_clear (contains_call);
    3449                 :    13256506 :   FOR_EACH_BB_FN (bb, cfun)
    3450                 :             :     {
    3451                 :    12815904 :       gimple_stmt_iterator gsi;
    3452                 :    94944657 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    3453                 :             :         {
    3454                 :    76372259 :           if (nonpure_call_p (gsi_stmt (gsi)))
    3455                 :             :             break;
    3456                 :             :         }
    3457                 :             : 
    3458                 :    12815904 :       if (!gsi_end_p (gsi))
    3459                 :     3299751 :         bitmap_set_bit (contains_call, bb->index);
    3460                 :             :     }
    3461                 :             : 
    3462                 :     1324242 :   for (loop = current_loops->tree_root->inner; loop; loop = loop->next)
    3463                 :      883640 :     fill_always_executed_in_1 (loop, contains_call);
    3464                 :      440602 : }
    3465                 :             : 
    3466                 :             : /* Find the coldest loop preheader for LOOP, also find the nearest hotter loop
    3467                 :             :    to LOOP.  Then recursively iterate each inner loop.  */
    3468                 :             : 
    3469                 :             : void
    3470                 :     1134634 : fill_coldest_and_hotter_out_loop (class loop *coldest_loop,
    3471                 :             :                                   class loop *hotter_loop, class loop *loop)
    3472                 :             : {
    3473                 :     1134634 :   if (bb_colder_than_loop_preheader (loop_preheader_edge (loop)->src,
    3474                 :             :                                      coldest_loop))
    3475                 :       19059 :     coldest_loop = loop;
    3476                 :             : 
    3477                 :     1134634 :   coldest_outermost_loop[loop->num] = coldest_loop;
    3478                 :             : 
    3479                 :     1134634 :   hotter_than_inner_loop[loop->num] = NULL;
    3480                 :     1134634 :   class loop *outer_loop = loop_outer (loop);
    3481                 :     1134634 :   if (hotter_loop
    3482                 :     1134634 :       && bb_colder_than_loop_preheader (loop_preheader_edge (loop)->src,
    3483                 :             :                                         hotter_loop))
    3484                 :        2254 :     hotter_than_inner_loop[loop->num] = hotter_loop;
    3485                 :             : 
    3486                 :     1134634 :   if (outer_loop && outer_loop != current_loops->tree_root
    3487                 :     1385628 :       && bb_colder_than_loop_preheader (loop_preheader_edge (loop)->src,
    3488                 :             :                                         outer_loop))
    3489                 :       23301 :     hotter_than_inner_loop[loop->num] = outer_loop;
    3490                 :             : 
    3491                 :     1134634 :   if (dump_enabled_p ())
    3492                 :             :     {
    3493                 :        1440 :       dump_printf (MSG_NOTE, "loop %d's coldest_outermost_loop is %d, ",
    3494                 :             :                    loop->num, coldest_loop->num);
    3495                 :        1440 :       if (hotter_than_inner_loop[loop->num])
    3496                 :         137 :         dump_printf (MSG_NOTE, "hotter_than_inner_loop is %d\n",
    3497                 :         137 :                      hotter_than_inner_loop[loop->num]->num);
    3498                 :             :       else
    3499                 :        1303 :         dump_printf (MSG_NOTE, "hotter_than_inner_loop is NULL\n");
    3500                 :             :     }
    3501                 :             : 
    3502                 :     1134634 :   class loop *inner_loop;
    3503                 :     1385628 :   for (inner_loop = loop->inner; inner_loop; inner_loop = inner_loop->next)
    3504                 :      501988 :     fill_coldest_and_hotter_out_loop (coldest_loop,
    3505                 :      250994 :                                       hotter_than_inner_loop[loop->num],
    3506                 :             :                                       inner_loop);
    3507                 :     1134634 : }
    3508                 :             : 
    3509                 :             : /* Compute the global information needed by the loop invariant motion pass.  */
    3510                 :             : 
    3511                 :             : static void
    3512                 :      440602 : tree_ssa_lim_initialize (bool store_motion)
    3513                 :             : {
    3514                 :      440602 :   unsigned i;
    3515                 :             : 
    3516                 :      440602 :   bitmap_obstack_initialize (&lim_bitmap_obstack);
    3517                 :      440602 :   gcc_obstack_init (&mem_ref_obstack);
    3518                 :      440602 :   lim_aux_data_map = new hash_map<gimple *, lim_aux_data *>;
    3519                 :             : 
    3520                 :      440602 :   if (flag_tm)
    3521                 :         118 :     compute_transaction_bits ();
    3522                 :             : 
    3523                 :      440602 :   memory_accesses.refs = new hash_table<mem_ref_hasher> (100);
    3524                 :      440602 :   memory_accesses.refs_list.create (100);
    3525                 :             :   /* Allocate a special, unanalyzable mem-ref with ID zero.  */
    3526                 :      440602 :   memory_accesses.refs_list.quick_push
    3527                 :      440602 :     (mem_ref_alloc (NULL, 0, UNANALYZABLE_MEM_ID));
    3528                 :             : 
    3529                 :      881204 :   memory_accesses.refs_loaded_in_loop.create (number_of_loops (cfun));
    3530                 :      881204 :   memory_accesses.refs_loaded_in_loop.quick_grow_cleared (number_of_loops (cfun));
    3531                 :      881204 :   memory_accesses.refs_stored_in_loop.create (number_of_loops (cfun));
    3532                 :      881204 :   memory_accesses.refs_stored_in_loop.quick_grow_cleared (number_of_loops (cfun));
    3533                 :      440602 :   if (store_motion)
    3534                 :             :     {
    3535                 :      881090 :       memory_accesses.all_refs_stored_in_loop.create (number_of_loops (cfun));
    3536                 :      440545 :       memory_accesses.all_refs_stored_in_loop.quick_grow_cleared
    3537                 :      881090 :                                                       (number_of_loops (cfun));
    3538                 :             :     }
    3539                 :             : 
    3540                 :     5281104 :   for (i = 0; i < number_of_loops (cfun); i++)
    3541                 :             :     {
    3542                 :     2199950 :       bitmap_initialize (&memory_accesses.refs_loaded_in_loop[i],
    3543                 :             :                          &lim_bitmap_obstack);
    3544                 :     2199950 :       bitmap_initialize (&memory_accesses.refs_stored_in_loop[i],
    3545                 :             :                          &lim_bitmap_obstack);
    3546                 :     2199950 :       if (store_motion)
    3547                 :     2199712 :         bitmap_initialize (&memory_accesses.all_refs_stored_in_loop[i],
    3548                 :             :                            &lim_bitmap_obstack);
    3549                 :             :     }
    3550                 :             : 
    3551                 :      440602 :   memory_accesses.ttae_cache = NULL;
    3552                 :             : 
    3553                 :             :   /* Initialize bb_loop_postorder with a mapping from loop->num to
    3554                 :             :      its postorder index.  */
    3555                 :      440602 :   i = 0;
    3556                 :      881204 :   bb_loop_postorder = XNEWVEC (unsigned, number_of_loops (cfun));
    3557                 :     2456440 :   for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
    3558                 :     1134634 :     bb_loop_postorder[loop->num] = i++;
    3559                 :      440602 : }
    3560                 :             : 
    3561                 :             : /* Cleans up after the invariant motion pass.  */
    3562                 :             : 
    3563                 :             : static void
    3564                 :      440602 : tree_ssa_lim_finalize (void)
    3565                 :             : {
    3566                 :      440602 :   basic_block bb;
    3567                 :      440602 :   unsigned i;
    3568                 :      440602 :   im_mem_ref *ref;
    3569                 :             : 
    3570                 :    13307998 :   FOR_EACH_BB_FN (bb, cfun)
    3571                 :    12867396 :     SET_ALWAYS_EXECUTED_IN (bb, NULL);
    3572                 :             : 
    3573                 :      440602 :   bitmap_obstack_release (&lim_bitmap_obstack);
    3574                 :      881204 :   delete lim_aux_data_map;
    3575                 :             : 
    3576                 :      440602 :   delete memory_accesses.refs;
    3577                 :      440602 :   memory_accesses.refs = NULL;
    3578                 :             : 
    3579                 :     5390861 :   FOR_EACH_VEC_ELT (memory_accesses.refs_list, i, ref)
    3580                 :     4950259 :     memref_free (ref);
    3581                 :      440602 :   memory_accesses.refs_list.release ();
    3582                 :      440602 :   obstack_free (&mem_ref_obstack, NULL);
    3583                 :             : 
    3584                 :      440602 :   memory_accesses.refs_loaded_in_loop.release ();
    3585                 :      440602 :   memory_accesses.refs_stored_in_loop.release ();
    3586                 :      440602 :   memory_accesses.all_refs_stored_in_loop.release ();
    3587                 :             : 
    3588                 :      440602 :   if (memory_accesses.ttae_cache)
    3589                 :        3095 :     free_affine_expand_cache (&memory_accesses.ttae_cache);
    3590                 :             : 
    3591                 :      440602 :   free (bb_loop_postorder);
    3592                 :             : 
    3593                 :      440602 :   coldest_outermost_loop.release ();
    3594                 :      440602 :   hotter_than_inner_loop.release ();
    3595                 :      440602 : }
    3596                 :             : 
    3597                 :             : /* Moves invariants from loops.  Only "expensive" invariants are moved out --
    3598                 :             :    i.e. those that are likely to be win regardless of the register pressure.
    3599                 :             :    Only perform store motion if STORE_MOTION is true.  */
    3600                 :             : 
    3601                 :             : unsigned int
    3602                 :      440602 : loop_invariant_motion_in_fun (function *fun, bool store_motion)
    3603                 :             : {
    3604                 :      440602 :   unsigned int todo = 0;
    3605                 :             : 
    3606                 :      440602 :   tree_ssa_lim_initialize (store_motion);
    3607                 :             : 
    3608                 :      440602 :   mark_ssa_maybe_undefs ();
    3609                 :             : 
    3610                 :             :   /* Gathers information about memory accesses in the loops.  */
    3611                 :      440602 :   analyze_memory_references (store_motion);
    3612                 :             : 
    3613                 :             :   /* Fills ALWAYS_EXECUTED_IN information for basic blocks.  */
    3614                 :      440602 :   fill_always_executed_in ();
    3615                 :             : 
    3616                 :             :   /* Pre-compute coldest outermost loop and nearest hotter loop of each loop.
    3617                 :             :    */
    3618                 :      440602 :   class loop *loop;
    3619                 :      881204 :   coldest_outermost_loop.create (number_of_loops (cfun));
    3620                 :      881204 :   coldest_outermost_loop.safe_grow_cleared (number_of_loops (cfun));
    3621                 :      881204 :   hotter_than_inner_loop.create (number_of_loops (cfun));
    3622                 :      881204 :   hotter_than_inner_loop.safe_grow_cleared (number_of_loops (cfun));
    3623                 :     1324242 :   for (loop = current_loops->tree_root->inner; loop != NULL; loop = loop->next)
    3624                 :      883640 :     fill_coldest_and_hotter_out_loop (loop, NULL, loop);
    3625                 :             : 
    3626                 :      440602 :   int *rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
    3627                 :      440602 :   int n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, false);
    3628                 :             : 
    3629                 :             :   /* For each statement determine the outermost loop in that it is
    3630                 :             :      invariant and cost for computing the invariant.  */
    3631                 :    13256506 :   for (int i = 0; i < n; ++i)
    3632                 :    12815904 :     compute_invariantness (BASIC_BLOCK_FOR_FN (fun, rpo[i]));
    3633                 :             : 
    3634                 :             :   /* Execute store motion.  Force the necessary invariants to be moved
    3635                 :             :      out of the loops as well.  */
    3636                 :      440602 :   if (store_motion)
    3637                 :      440545 :     do_store_motion ();
    3638                 :             : 
    3639                 :      440602 :   free (rpo);
    3640                 :      440602 :   rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
    3641                 :      440602 :   n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, false);
    3642                 :             : 
    3643                 :             :   /* Move the expressions that are expensive enough.  */
    3644                 :    13307998 :   for (int i = 0; i < n; ++i)
    3645                 :    12867396 :     todo |= move_computations_worker (BASIC_BLOCK_FOR_FN (fun, rpo[i]));
    3646                 :             : 
    3647                 :      440602 :   free (rpo);
    3648                 :             : 
    3649                 :      440602 :   gsi_commit_edge_inserts ();
    3650                 :      440602 :   if (need_ssa_update_p (fun))
    3651                 :       13738 :     rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
    3652                 :             : 
    3653                 :      440602 :   tree_ssa_lim_finalize ();
    3654                 :             : 
    3655                 :      440602 :   return todo;
    3656                 :             : }
    3657                 :             : 
    3658                 :             : /* Loop invariant motion pass.  */
    3659                 :             : 
    3660                 :             : namespace {
    3661                 :             : 
    3662                 :             : const pass_data pass_data_lim =
    3663                 :             : {
    3664                 :             :   GIMPLE_PASS, /* type */
    3665                 :             :   "lim", /* name */
    3666                 :             :   OPTGROUP_LOOP, /* optinfo_flags */
    3667                 :             :   TV_LIM, /* tv_id */
    3668                 :             :   PROP_cfg, /* properties_required */
    3669                 :             :   0, /* properties_provided */
    3670                 :             :   0, /* properties_destroyed */
    3671                 :             :   0, /* todo_flags_start */
    3672                 :             :   0, /* todo_flags_finish */
    3673                 :             : };
    3674                 :             : 
    3675                 :             : class pass_lim : public gimple_opt_pass
    3676                 :             : {
    3677                 :             : public:
    3678                 :     1127656 :   pass_lim (gcc::context *ctxt)
    3679                 :     2255312 :     : gimple_opt_pass (pass_data_lim, ctxt)
    3680                 :             :   {}
    3681                 :             : 
    3682                 :             :   /* opt_pass methods: */
    3683                 :      845742 :   opt_pass * clone () final override { return new pass_lim (m_ctxt); }
    3684                 :     1206630 :   bool gate (function *) final override { return flag_tree_loop_im != 0; }
    3685                 :             :   unsigned int execute (function *) final override;
    3686                 :             : 
    3687                 :             : }; // class pass_lim
    3688                 :             : 
    3689                 :             : unsigned int
    3690                 :     1206338 : pass_lim::execute (function *fun)
    3691                 :             : {
    3692                 :     1206338 :   bool in_loop_pipeline = scev_initialized_p ();
    3693                 :     1206338 :   if (!in_loop_pipeline)
    3694                 :      986321 :     loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
    3695                 :             : 
    3696                 :     2412676 :   if (number_of_loops (fun) <= 1)
    3697                 :             :     return 0;
    3698                 :      440561 :   unsigned int todo = loop_invariant_motion_in_fun (fun, flag_move_loop_stores);
    3699                 :             : 
    3700                 :      440561 :   if (!in_loop_pipeline)
    3701                 :      220544 :     loop_optimizer_finalize ();
    3702                 :             :   else
    3703                 :      220017 :     scev_reset ();
    3704                 :             :   return todo;
    3705                 :             : }
    3706                 :             : 
    3707                 :             : } // anon namespace
    3708                 :             : 
    3709                 :             : gimple_opt_pass *
    3710                 :      281914 : make_pass_lim (gcc::context *ctxt)
    3711                 :             : {
    3712                 :      281914 :   return new pass_lim (ctxt);
    3713                 :             : }
    3714                 :             : 
    3715                 :             : 
        

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.