LCOV - code coverage report
Current view: top level - gcc - tree-ssa-pre.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.9 % 2144 2013
Test Date: 2026-12-05 16:10:02 Functions: 92.4 % 66 61
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE.
       2              :    Copyright (C) 2001-2026 Free Software Foundation, Inc.
       3              :    Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
       4              :    <stevenb@suse.de>
       5              : 
       6              : This file is part of GCC.
       7              : 
       8              : GCC is free software; you can redistribute it and/or modify
       9              : it under the terms of the GNU General Public License as published by
      10              : the Free Software Foundation; either version 3, or (at your option)
      11              : any later version.
      12              : 
      13              : GCC is distributed in the hope that it will be useful,
      14              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      15              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16              : GNU General Public License for more details.
      17              : 
      18              : You should have received a copy of the GNU General Public License
      19              : along with GCC; see the file COPYING3.  If not see
      20              : <http://www.gnu.org/licenses/>.  */
      21              : 
      22              : #include "config.h"
      23              : #include "system.h"
      24              : #include "coretypes.h"
      25              : #include "backend.h"
      26              : #include "rtl.h"
      27              : #include "tree.h"
      28              : #include "gimple.h"
      29              : #include "predict.h"
      30              : #include "alloc-pool.h"
      31              : #include "tree-pass.h"
      32              : #include "ssa.h"
      33              : #include "cgraph.h"
      34              : #include "gimple-pretty-print.h"
      35              : #include "fold-const.h"
      36              : #include "cfganal.h"
      37              : #include "gimple-iterator.h"
      38              : #include "gimple-fold.h"
      39              : #include "tree-eh.h"
      40              : #include "tree-cfg.h"
      41              : #include "tree-into-ssa.h"
      42              : #include "tree-dfa.h"
      43              : #include "tree-ssa.h"
      44              : #include "cfgloop.h"
      45              : #include "tree-ssa-sccvn.h"
      46              : #include "tree-scalar-evolution.h"
      47              : #include "dbgcnt.h"
      48              : #include "domwalk.h"
      49              : #include "tree-ssa-propagate.h"
      50              : #include "tree-ssa-dce.h"
      51              : #include "tree-cfgcleanup.h"
      52              : #include "alias.h"
      53              : #include "gimple-range.h"
      54              : 
      55              : /* Even though this file is called tree-ssa-pre.cc, we actually
      56              :    implement a bit more than just PRE here.  All of them piggy-back
      57              :    on GVN which is implemented in tree-ssa-sccvn.cc.
      58              : 
      59              :      1. Full Redundancy Elimination (FRE)
      60              :         This is the elimination phase of GVN.
      61              : 
      62              :      2. Partial Redundancy Elimination (PRE)
      63              :         This is adds computation of AVAIL_OUT and ANTIC_IN and
      64              :         doing expression insertion to form GVN-PRE.
      65              : 
      66              :      3. Code hoisting
      67              :         This optimization uses the ANTIC_IN sets computed for PRE
      68              :         to move expressions further up than PRE would do, to make
      69              :         multiple computations of the same value fully redundant.
      70              :         This pass is explained below (after the explanation of the
      71              :         basic algorithm for PRE).
      72              : */
      73              : 
      74              : /* TODO:
      75              : 
      76              :    1. Avail sets can be shared by making an avail_find_leader that
      77              :       walks up the dominator tree and looks in those avail sets.
      78              :       This might affect code optimality, it's unclear right now.
      79              :       Currently the AVAIL_OUT sets are the remaining quadraticness in
      80              :       memory of GVN-PRE.
      81              :    2. Strength reduction can be performed by anticipating expressions
      82              :       we can repair later on.
      83              :    3. We can do back-substitution or smarter value numbering to catch
      84              :       commutative expressions split up over multiple statements.
      85              : */
      86              : 
      87              : /* For ease of terminology, "expression node" in the below refers to
      88              :    every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
      89              :    represent the actual statement containing the expressions we care about,
      90              :    and we cache the value number by putting it in the expression.  */
      91              : 
      92              : /* Basic algorithm for Partial Redundancy Elimination:
      93              : 
      94              :    First we walk the statements to generate the AVAIL sets, the
      95              :    EXP_GEN sets, and the tmp_gen sets.  EXP_GEN sets represent the
      96              :    generation of values/expressions by a given block.  We use them
      97              :    when computing the ANTIC sets.  The AVAIL sets consist of
      98              :    SSA_NAME's that represent values, so we know what values are
      99              :    available in what blocks.  AVAIL is a forward dataflow problem.  In
     100              :    SSA, values are never killed, so we don't need a kill set, or a
     101              :    fixpoint iteration, in order to calculate the AVAIL sets.  In
     102              :    traditional parlance, AVAIL sets tell us the downsafety of the
     103              :    expressions/values.
     104              : 
     105              :    Next, we generate the ANTIC sets.  These sets represent the
     106              :    anticipatable expressions.  ANTIC is a backwards dataflow
     107              :    problem.  An expression is anticipatable in a given block if it could
     108              :    be generated in that block.  This means that if we had to perform
     109              :    an insertion in that block, of the value of that expression, we
     110              :    could.  Calculating the ANTIC sets requires phi translation of
     111              :    expressions, because the flow goes backwards through phis.  We must
     112              :    iterate to a fixpoint of the ANTIC sets, because we have a kill
     113              :    set.  Even in SSA form, values are not live over the entire
     114              :    function, only from their definition point onwards.  So we have to
     115              :    remove values from the ANTIC set once we go past the definition
     116              :    point of the leaders that make them up.
     117              :    compute_antic/compute_antic_aux performs this computation.
     118              : 
     119              :    Third, we perform insertions to make partially redundant
     120              :    expressions fully redundant.
     121              : 
     122              :    An expression is partially redundant (excluding partial
     123              :    anticipation) if:
     124              : 
     125              :    1. It is AVAIL in some, but not all, of the predecessors of a
     126              :       given block.
     127              :    2. It is ANTIC in all the predecessors.
     128              : 
     129              :    In order to make it fully redundant, we insert the expression into
     130              :    the predecessors where it is not available, but is ANTIC.
     131              : 
     132              :    When optimizing for size, we only eliminate the partial redundancy
     133              :    if we need to insert in only one predecessor.  This avoids almost
     134              :    completely the code size increase that PRE usually causes.
     135              : 
     136              :    For the partial anticipation case, we only perform insertion if it
     137              :    is partially anticipated in some block, and fully available in all
     138              :    of the predecessors.
     139              : 
     140              :    do_pre_regular_insertion/do_pre_partial_partial_insertion
     141              :    performs these steps, driven by insert/insert_aux.
     142              : 
     143              :    Fourth, we eliminate fully redundant expressions.
     144              :    This is a simple statement walk that replaces redundant
     145              :    calculations with the now available values.  */
     146              : 
     147              : /* Basic algorithm for Code Hoisting:
     148              : 
     149              :    Code hoisting is: Moving value computations up in the control flow
     150              :    graph to make multiple copies redundant.  Typically this is a size
     151              :    optimization, but there are cases where it also is helpful for speed.
     152              : 
     153              :    A simple code hoisting algorithm is implemented that piggy-backs on
     154              :    the PRE infrastructure.  For code hoisting, we have to know ANTIC_OUT
     155              :    which is effectively ANTIC_IN - AVAIL_OUT.  The latter two have to be
     156              :    computed for PRE, and we can use them to perform a limited version of
     157              :    code hoisting, too.
     158              : 
     159              :    For the purpose of this implementation, a value is hoistable to a basic
     160              :    block B if the following properties are met:
     161              : 
     162              :    1. The value is in ANTIC_IN(B) -- the value will be computed on all
     163              :       paths from B to function exit and it can be computed in B);
     164              : 
     165              :    2. The value is not in AVAIL_OUT(B) -- there would be no need to
     166              :       compute the value again and make it available twice;
     167              : 
     168              :    3. All successors of B are dominated by B -- makes sure that inserting
     169              :       a computation of the value in B will make the remaining
     170              :       computations fully redundant;
     171              : 
     172              :    4. At least one successor has the value in AVAIL_OUT -- to avoid
     173              :       hoisting values up too far;
     174              : 
     175              :    5. There are at least two successors of B -- hoisting in straight
     176              :       line code is pointless.
     177              : 
     178              :    The third condition is not strictly necessary, but it would complicate
     179              :    the hoisting pass a lot.  In fact, I don't know of any code hoisting
     180              :    algorithm that does not have this requirement.  Fortunately, experiments
     181              :    have show that most candidate hoistable values are in regions that meet
     182              :    this condition (e.g. diamond-shape regions).
     183              : 
     184              :    The forth condition is necessary to avoid hoisting things up too far
     185              :    away from the uses of the value.  Nothing else limits the algorithm
     186              :    from hoisting everything up as far as ANTIC_IN allows.  Experiments
     187              :    with SPEC and CSiBE have shown that hoisting up too far results in more
     188              :    spilling, less benefits for code size, and worse benchmark scores.
     189              :    Fortunately, in practice most of the interesting hoisting opportunities
     190              :    are caught despite this limitation.
     191              : 
     192              :    For hoistable values that meet all conditions, expressions are inserted
     193              :    to make the calculation of the hoistable value fully redundant.  We
     194              :    perform code hoisting insertions after each round of PRE insertions,
     195              :    because code hoisting never exposes new PRE opportunities, but PRE can
     196              :    create new code hoisting opportunities.
     197              : 
     198              :    The code hoisting algorithm is implemented in do_hoist_insert, driven
     199              :    by insert/insert_aux.  */
     200              : 
     201              : /* Representations of value numbers:
     202              : 
     203              :    Value numbers are represented by a representative SSA_NAME.  We
     204              :    will create fake SSA_NAME's in situations where we need a
     205              :    representative but do not have one (because it is a complex
     206              :    expression).  In order to facilitate storing the value numbers in
     207              :    bitmaps, and keep the number of wasted SSA_NAME's down, we also
     208              :    associate a value_id with each value number, and create full blown
     209              :    ssa_name's only where we actually need them (IE in operands of
     210              :    existing expressions).
     211              : 
     212              :    Theoretically you could replace all the value_id's with
     213              :    SSA_NAME_VERSION, but this would allocate a large number of
     214              :    SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
     215              :    It would also require an additional indirection at each point we
     216              :    use the value id.  */
     217              : 
     218              : /* Representation of expressions on value numbers:
     219              : 
     220              :    Expressions consisting of value numbers are represented the same
     221              :    way as our VN internally represents them, with an additional
     222              :    "pre_expr" wrapping around them in order to facilitate storing all
     223              :    of the expressions in the same sets.  */
     224              : 
     225              : /* Representation of sets:
     226              : 
     227              :    The dataflow sets do not need to be sorted in any particular order
     228              :    for the majority of their lifetime, are simply represented as two
     229              :    bitmaps, one that keeps track of values present in the set, and one
     230              :    that keeps track of expressions present in the set.
     231              : 
     232              :    When we need them in topological order, we produce it on demand by
     233              :    transforming the bitmap into an array and sorting it into topo
     234              :    order.  */
     235              : 
     236              : /* Type of expression, used to know which member of the PRE_EXPR union
     237              :    is valid.  */
     238              : 
     239              : enum pre_expr_kind
     240              : {
     241              :     NAME,
     242              :     NARY,
     243              :     REFERENCE,
     244              :     CONSTANT
     245              : };
     246              : 
     247              : union pre_expr_union
     248              : {
     249              :   tree name;
     250              :   tree constant;
     251              :   vn_nary_op_t nary;
     252              :   vn_reference_t reference;
     253              : };
     254              : 
     255              : typedef struct pre_expr_d : nofree_ptr_hash <pre_expr_d>
     256              : {
     257              :   enum pre_expr_kind kind;
     258              :   unsigned int id;
     259              :   unsigned value_id;
     260              :   location_t loc;
     261              :   pre_expr_union u;
     262              : 
     263              :   /* hash_table support.  */
     264              :   static inline hashval_t hash (const pre_expr_d *);
     265              :   static inline int equal (const pre_expr_d *, const pre_expr_d *);
     266              : } *pre_expr;
     267              : 
     268              : #define PRE_EXPR_NAME(e) (e)->u.name
     269              : #define PRE_EXPR_NARY(e) (e)->u.nary
     270              : #define PRE_EXPR_REFERENCE(e) (e)->u.reference
     271              : #define PRE_EXPR_CONSTANT(e) (e)->u.constant
     272              : 
     273              : /* Compare E1 and E1 for equality.  */
     274              : 
     275              : inline int
     276     59049666 : pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
     277              : {
     278     59049666 :   if (e1->kind != e2->kind)
     279              :     return false;
     280              : 
     281     37327439 :   switch (e1->kind)
     282              :     {
     283      4878876 :     case CONSTANT:
     284      4878876 :       return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
     285      4878876 :                                        PRE_EXPR_CONSTANT (e2));
     286       159908 :     case NAME:
     287       159908 :       return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
     288     23666045 :     case NARY:
     289     23666045 :       return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
     290      8622610 :     case REFERENCE:
     291      8622610 :       return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
     292      8622610 :                               PRE_EXPR_REFERENCE (e2), true);
     293            0 :     default:
     294            0 :       gcc_unreachable ();
     295              :     }
     296              : }
     297              : 
     298              : /* Hash E.  */
     299              : 
     300              : inline hashval_t
     301     94074949 : pre_expr_d::hash (const pre_expr_d *e)
     302              : {
     303     94074949 :   switch (e->kind)
     304              :     {
     305      7472087 :     case CONSTANT:
     306      7472087 :       return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
     307            0 :     case NAME:
     308            0 :       return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
     309     58749118 :     case NARY:
     310     58749118 :       return PRE_EXPR_NARY (e)->hashcode;
     311     27853744 :     case REFERENCE:
     312     27853744 :       return PRE_EXPR_REFERENCE (e)->hashcode;
     313            0 :     default:
     314            0 :       gcc_unreachable ();
     315              :     }
     316              : }
     317              : 
     318              : /* Next global expression id number.  */
     319              : static unsigned int next_expression_id;
     320              : 
     321              : /* Mapping from expression to id number we can use in bitmap sets.  */
     322              : static vec<pre_expr> expressions;
     323              : static hash_table<pre_expr_d> *expression_to_id;
     324              : static vec<unsigned> name_to_id;
     325              : static obstack pre_expr_obstack;
     326              : 
     327              : /* Allocate an expression id for EXPR.  */
     328              : 
     329              : static inline unsigned int
     330     44746448 : alloc_expression_id (pre_expr expr)
     331              : {
     332     44746448 :   struct pre_expr_d **slot;
     333              :   /* Make sure we won't overflow. */
     334     44746448 :   gcc_assert (next_expression_id + 1 > next_expression_id);
     335     44746448 :   expr->id = next_expression_id++;
     336     44746448 :   expressions.safe_push (expr);
     337     44746448 :   if (expr->kind == NAME)
     338              :     {
     339     24383646 :       unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
     340              :       /* vec::safe_grow_cleared allocates no headroom.  Avoid frequent
     341              :          re-allocations by using vec::reserve upfront.  */
     342     24383646 :       unsigned old_len = name_to_id.length ();
     343     48767292 :       name_to_id.reserve (num_ssa_names - old_len);
     344     48767292 :       name_to_id.quick_grow_cleared (num_ssa_names);
     345     24383646 :       gcc_assert (name_to_id[version] == 0);
     346     24383646 :       name_to_id[version] = expr->id;
     347              :     }
     348              :   else
     349              :     {
     350     20362802 :       slot = expression_to_id->find_slot (expr, INSERT);
     351     20362802 :       gcc_assert (!*slot);
     352     20362802 :       *slot = expr;
     353              :     }
     354     44746448 :   return next_expression_id - 1;
     355              : }
     356              : 
     357              : /* Return the expression id for tree EXPR.  */
     358              : 
     359              : static inline unsigned int
     360    263113054 : get_expression_id (const pre_expr expr)
     361              : {
     362    263113054 :   return expr->id;
     363              : }
     364              : 
     365              : static inline unsigned int
     366     80507856 : lookup_expression_id (const pre_expr expr)
     367              : {
     368     80507856 :   struct pre_expr_d **slot;
     369              : 
     370     80507856 :   if (expr->kind == NAME)
     371              :     {
     372     54079438 :       unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
     373     54079438 :       if (name_to_id.length () <= version)
     374              :         return 0;
     375     50990226 :       return name_to_id[version];
     376              :     }
     377              :   else
     378              :     {
     379     26428418 :       slot = expression_to_id->find_slot (expr, NO_INSERT);
     380     26428418 :       if (!slot)
     381              :         return 0;
     382      6065616 :       return ((pre_expr)*slot)->id;
     383              :     }
     384              : }
     385              : 
     386              : /* Return the expression that has expression id ID */
     387              : 
     388              : static inline pre_expr
     389    641185132 : expression_for_id (unsigned int id)
     390              : {
     391   1282370264 :   return expressions[id];
     392              : }
     393              : 
     394              : static object_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes");
     395              : 
     396              : /* Given an SSA_NAME NAME, get or create a pre_expr to represent it.  */
     397              : 
     398              : static pre_expr
     399     54079438 : get_or_alloc_expr_for_name (tree name)
     400              : {
     401     54079438 :   struct pre_expr_d expr;
     402     54079438 :   pre_expr result;
     403     54079438 :   unsigned int result_id;
     404              : 
     405     54079438 :   expr.kind = NAME;
     406     54079438 :   expr.id = 0;
     407     54079438 :   PRE_EXPR_NAME (&expr) = name;
     408     54079438 :   result_id = lookup_expression_id (&expr);
     409     54079438 :   if (result_id != 0)
     410     29695792 :     return expression_for_id (result_id);
     411              : 
     412     24383646 :   result = pre_expr_pool.allocate ();
     413     24383646 :   result->kind = NAME;
     414     24383646 :   result->loc = UNKNOWN_LOCATION;
     415     24383646 :   result->value_id = VN_INFO (name)->value_id;
     416     24383646 :   PRE_EXPR_NAME (result) = name;
     417     24383646 :   alloc_expression_id (result);
     418     24383646 :   return result;
     419              : }
     420              : 
     421              : /* Given an NARY, get or create a pre_expr to represent it.  Assign
     422              :    VALUE_ID to it or allocate a new value-id if it is zero.  Record
     423              :    LOC as the original location of the expression.  */
     424              : 
     425              : static pre_expr
     426     13972220 : get_or_alloc_expr_for_nary (vn_nary_op_t nary, unsigned value_id,
     427              :                             location_t loc = UNKNOWN_LOCATION)
     428              : {
     429     13972220 :   struct pre_expr_d expr;
     430     13972220 :   pre_expr result;
     431     13972220 :   unsigned int result_id;
     432              : 
     433     13972220 :   gcc_assert (value_id == 0 || !value_id_constant_p (value_id));
     434     13972220 :   gcc_assert (nary->opcode != SSA_NAME
     435              :               && TREE_CODE_CLASS (nary->opcode) != tcc_constant);
     436              : 
     437     13972220 :   expr.kind = NARY;
     438     13972220 :   expr.id = 0;
     439     13972220 :   nary->hashcode = vn_nary_op_compute_hash (nary);
     440     13972220 :   PRE_EXPR_NARY (&expr) = nary;
     441     13972220 :   result_id = lookup_expression_id (&expr);
     442     13972220 :   if (result_id != 0)
     443      1006302 :     return expression_for_id (result_id);
     444              : 
     445     12965918 :   result = pre_expr_pool.allocate ();
     446     12965918 :   result->kind = NARY;
     447     12965918 :   result->loc = loc;
     448     12965918 :   result->value_id = value_id ? value_id : get_next_value_id ();
     449     12965918 :   PRE_EXPR_NARY (result)
     450     12965918 :     = alloc_vn_nary_op_noinit (nary->length, &pre_expr_obstack);
     451     12965918 :   memcpy (PRE_EXPR_NARY (result), nary, sizeof_vn_nary_op (nary->length));
     452     12965918 :   alloc_expression_id (result);
     453     12965918 :   return result;
     454              : }
     455              : 
     456              : /* Given an REFERENCE, get or create a pre_expr to represent it.  Assign
     457              :    VALUE_ID to it or allocate a new value-id if it is zero.  Record
     458              :    LOC as the original location of the expression.  If MOVE_OPERANDS
     459              :    is true then ownership of REFERENCE->operands is transferred, otherwise
     460              :    a copy is made if necessary.  */
     461              : 
     462              : static pre_expr
     463      7260099 : get_or_alloc_expr_for_reference (vn_reference_t reference,
     464              :                                  unsigned value_id,
     465              :                                  location_t loc = UNKNOWN_LOCATION,
     466              :                                  bool move_operands = false)
     467              : {
     468      7260099 :   struct pre_expr_d expr;
     469      7260099 :   pre_expr result;
     470      7260099 :   unsigned int result_id;
     471              : 
     472      7260099 :   expr.kind = REFERENCE;
     473      7260099 :   expr.id = 0;
     474      7260099 :   PRE_EXPR_REFERENCE (&expr) = reference;
     475      7260099 :   result_id = lookup_expression_id (&expr);
     476      7260099 :   if (result_id != 0)
     477              :     {
     478       740422 :       if (move_operands)
     479       727854 :         reference->operands.release ();
     480       740422 :       return expression_for_id (result_id);
     481              :     }
     482              : 
     483      6519677 :   result = pre_expr_pool.allocate ();
     484      6519677 :   result->kind = REFERENCE;
     485      6519677 :   result->loc = loc;
     486      6519677 :   result->value_id = value_id ? value_id : get_next_value_id ();
     487      6519677 :   vn_reference_t ref = XOBNEW (&pre_expr_obstack, struct vn_reference_s);
     488      6519677 :   *ref = *reference;
     489      6519677 :   if (!move_operands)
     490       455761 :     ref->operands = ref->operands.copy ();
     491      6519677 :   PRE_EXPR_REFERENCE (result) = ref;
     492      6519677 :   alloc_expression_id (result);
     493      6519677 :   return result;
     494              : }
     495              : 
     496              : 
     497              : /* An unordered bitmap set.  One bitmap tracks values, the other,
     498              :    expressions.  */
     499    148903713 : typedef class bitmap_set
     500              : {
     501              : public:
     502              :   bitmap_head expressions;
     503              :   bitmap_head values;
     504              : } *bitmap_set_t;
     505              : 
     506              : #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi)            \
     507              :   EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
     508              : 
     509              : #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi)           \
     510              :   EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
     511              : 
     512              : /* Mapping from value id to expressions with that value_id.  */
     513              : static vec<bitmap> value_expressions;
     514              : /* We just record a single expression for each constant value,
     515              :    one of kind CONSTANT.  */
     516              : static vec<pre_expr> constant_value_expressions;
     517              : 
     518              : 
     519              : /* This structure is used to keep track of statistics on what
     520              :    optimization PRE was able to perform.  */
     521              : static struct
     522              : {
     523              :   /* The number of new expressions/temporaries generated by PRE.  */
     524              :   int insertions;
     525              : 
     526              :   /* The number of inserts found due to partial anticipation  */
     527              :   int pa_insert;
     528              : 
     529              :   /* The number of inserts made for code hoisting.  */
     530              :   int hoist_insert;
     531              : 
     532              :   /* The number of new PHI nodes added by PRE.  */
     533              :   int phis;
     534              : } pre_stats;
     535              : 
     536              : static bool do_partial_partial;
     537              : static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
     538              : static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
     539              : static bool bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
     540              : static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
     541              : static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
     542              : static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
     543              : static bitmap_set_t bitmap_set_new (void);
     544              : static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
     545              :                                          tree);
     546              : static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
     547              : static unsigned int get_expr_value_id (pre_expr);
     548              : 
     549              : /* We can add and remove elements and entries to and from sets
     550              :    and hash tables, so we use alloc pools for them.  */
     551              : 
     552              : static object_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets");
     553              : static bitmap_obstack grand_bitmap_obstack;
     554              : 
     555              : /* A three tuple {e, pred, v} used to cache phi translations in the
     556              :    phi_translate_table.  */
     557              : 
     558              : typedef struct expr_pred_trans_d : public typed_noop_remove <expr_pred_trans_d>
     559              : {
     560              :   typedef expr_pred_trans_d value_type;
     561              :   typedef expr_pred_trans_d compare_type;
     562              : 
     563              :   /* The expression ID.  */
     564              :   unsigned e;
     565              : 
     566              :   /* The value expression ID that resulted from the translation.  */
     567              :   unsigned v;
     568              : 
     569              :   /* hash_table support.  */
     570              :   static inline void mark_empty (expr_pred_trans_d &);
     571              :   static inline bool is_empty (const expr_pred_trans_d &);
     572              :   static inline void mark_deleted (expr_pred_trans_d &);
     573              :   static inline bool is_deleted (const expr_pred_trans_d &);
     574              :   static const bool empty_zero_p = true;
     575              :   static inline hashval_t hash (const expr_pred_trans_d &);
     576              :   static inline int equal (const expr_pred_trans_d &, const expr_pred_trans_d &);
     577              : } *expr_pred_trans_t;
     578              : typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
     579              : 
     580              : inline bool
     581   1414179968 : expr_pred_trans_d::is_empty (const expr_pred_trans_d &e)
     582              : {
     583   1414179968 :   return e.e == 0;
     584              : }
     585              : 
     586              : inline bool
     587    283924400 : expr_pred_trans_d::is_deleted (const expr_pred_trans_d &e)
     588              : {
     589    283924400 :   return e.e == -1u;
     590              : }
     591              : 
     592              : inline void
     593      2356809 : expr_pred_trans_d::mark_empty (expr_pred_trans_d &e)
     594              : {
     595      2356809 :   e.e = 0;
     596              : }
     597              : 
     598              : inline void
     599      4012926 : expr_pred_trans_d::mark_deleted (expr_pred_trans_d &e)
     600              : {
     601      4012926 :   e.e = -1u;
     602              : }
     603              : 
     604              : inline hashval_t
     605              : expr_pred_trans_d::hash (const expr_pred_trans_d &e)
     606              : {
     607              :   return e.e;
     608              : }
     609              : 
     610              : inline int
     611    221526034 : expr_pred_trans_d::equal (const expr_pred_trans_d &ve1,
     612              :                           const expr_pred_trans_d &ve2)
     613              : {
     614    221526034 :   return ve1.e == ve2.e;
     615              : }
     616              : 
     617              : /* Sets that we need to keep track of.  */
     618              : typedef struct bb_bitmap_sets
     619              : {
     620              :   /* The EXP_GEN set, which represents expressions/values generated in
     621              :      a basic block.  */
     622              :   bitmap_set_t exp_gen;
     623              : 
     624              :   /* The PHI_GEN set, which represents PHI results generated in a
     625              :      basic block.  */
     626              :   bitmap_set_t phi_gen;
     627              : 
     628              :   /* The TMP_GEN set, which represents results/temporaries generated
     629              :      in a basic block. IE the LHS of an expression.  */
     630              :   bitmap_set_t tmp_gen;
     631              : 
     632              :   /* The AVAIL_OUT set, which represents which values are available in
     633              :      a given basic block.  */
     634              :   bitmap_set_t avail_out;
     635              : 
     636              :   /* The ANTIC_IN set, which represents which values are anticipatable
     637              :      in a given basic block.  */
     638              :   bitmap_set_t antic_in;
     639              : 
     640              :   /* The PA_IN set, which represents which values are
     641              :      partially anticipatable in a given basic block.  */
     642              :   bitmap_set_t pa_in;
     643              : 
     644              :   /* The NEW_SETS set, which is used during insertion to augment the
     645              :      AVAIL_OUT set of blocks with the new insertions performed during
     646              :      the current iteration.  */
     647              :   bitmap_set_t new_sets;
     648              : 
     649              :   /* A cache for value_dies_in_block_x.  */
     650              :   bitmap expr_dies;
     651              : 
     652              :   /* The live virtual operand on successor edges.  */
     653              :   tree vop_on_exit;
     654              : 
     655              :   /* PHI translate cache for the single successor edge.  */
     656              :   hash_table<expr_pred_trans_d> *phi_translate_table;
     657              : 
     658              :   /* True if we have visited this block during ANTIC calculation.  */
     659              :   unsigned int visited : 1;
     660              : 
     661              :   /* True when the block contains a call that might not return.  */
     662              :   unsigned int contains_may_not_return_call : 1;
     663              : } *bb_value_sets_t;
     664              : 
     665              : #define EXP_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->exp_gen
     666              : #define PHI_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->phi_gen
     667              : #define TMP_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->tmp_gen
     668              : #define AVAIL_OUT(BB)   ((bb_value_sets_t) ((BB)->aux))->avail_out
     669              : #define ANTIC_IN(BB)    ((bb_value_sets_t) ((BB)->aux))->antic_in
     670              : #define PA_IN(BB)       ((bb_value_sets_t) ((BB)->aux))->pa_in
     671              : #define NEW_SETS(BB)    ((bb_value_sets_t) ((BB)->aux))->new_sets
     672              : #define EXPR_DIES(BB)   ((bb_value_sets_t) ((BB)->aux))->expr_dies
     673              : #define PHI_TRANS_TABLE(BB) ((bb_value_sets_t) ((BB)->aux))->phi_translate_table
     674              : #define BB_VISITED(BB)  ((bb_value_sets_t) ((BB)->aux))->visited
     675              : #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
     676              : #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
     677              : 
     678              : 
     679              : /* Add the tuple mapping from {expression E, basic block PRED} to
     680              :    the phi translation table and return whether it pre-existed.  */
     681              : 
     682              : static inline bool
     683     87559330 : phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
     684              : {
     685     87559330 :   if (!PHI_TRANS_TABLE (pred))
     686       198425 :     PHI_TRANS_TABLE (pred) = new hash_table<expr_pred_trans_d> (11);
     687              : 
     688     87559330 :   expr_pred_trans_t slot;
     689     87559330 :   expr_pred_trans_d tem;
     690     87559330 :   unsigned id = get_expression_id (e);
     691     87559330 :   tem.e = id;
     692     87559330 :   slot = PHI_TRANS_TABLE (pred)->find_slot_with_hash (tem, id, INSERT);
     693     87559330 :   if (slot->e)
     694              :     {
     695     63549887 :       *entry = slot;
     696     63549887 :       return true;
     697              :     }
     698              : 
     699     24009443 :   *entry = slot;
     700     24009443 :   slot->e = id;
     701     24009443 :   return false;
     702              : }
     703              : 
     704              : 
     705              : /* Add expression E to the expression set of value id V.  */
     706              : 
     707              : static void
     708     46493172 : add_to_value (unsigned int v, pre_expr e)
     709              : {
     710            0 :   gcc_checking_assert (get_expr_value_id (e) == v);
     711              : 
     712     46493172 :   if (value_id_constant_p (v))
     713              :     {
     714       923664 :       if (e->kind != CONSTANT)
     715              :         return;
     716              : 
     717       877207 :       if (-v >= constant_value_expressions.length ())
     718       525307 :         constant_value_expressions.safe_grow_cleared (-v + 1);
     719              : 
     720       877207 :       pre_expr leader = constant_value_expressions[-v];
     721       877207 :       if (!leader)
     722       877207 :         constant_value_expressions[-v] = e;
     723              :     }
     724              :   else
     725              :     {
     726     45569508 :       if (v >= value_expressions.length ())
     727      7214536 :         value_expressions.safe_grow_cleared (v + 1);
     728              : 
     729     45569508 :       bitmap set = value_expressions[v];
     730     45569508 :       if (!set)
     731              :         {
     732     25478396 :           set = BITMAP_ALLOC (&grand_bitmap_obstack);
     733     25478396 :           value_expressions[v] = set;
     734              :         }
     735     45569508 :       bitmap_set_bit (set, get_expression_id (e));
     736              :     }
     737              : }
     738              : 
     739              : /* Create a new bitmap set and return it.  */
     740              : 
     741              : static bitmap_set_t
     742    148903713 : bitmap_set_new (void)
     743              : {
     744    148903713 :   bitmap_set_t ret = bitmap_set_pool.allocate ();
     745    148903713 :   bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
     746    148903713 :   bitmap_initialize (&ret->values, &grand_bitmap_obstack);
     747    148903713 :   return ret;
     748              : }
     749              : 
     750              : /* Return the value id for a PRE expression EXPR.  */
     751              : 
     752              : static unsigned int
     753    538115287 : get_expr_value_id (pre_expr expr)
     754              : {
     755              :   /* ???  We cannot assert that expr has a value-id (it can be 0), because
     756              :      we assign value-ids only to expressions that have a result
     757              :      in set_hashtable_value_ids.  */
     758     46493172 :   return expr->value_id;
     759              : }
     760              : 
     761              : /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL.  */
     762              : 
     763              : static tree
     764      1269241 : vn_valnum_from_value_id (unsigned int val)
     765              : {
     766      1269241 :   if (value_id_constant_p (val))
     767              :     {
     768            0 :       pre_expr vexpr = constant_value_expressions[-val];
     769            0 :       if (vexpr)
     770            0 :         return PRE_EXPR_CONSTANT (vexpr);
     771              :       return NULL_TREE;
     772              :     }
     773              : 
     774      1269241 :   bitmap exprset = value_expressions[val];
     775      1269241 :   bitmap_iterator bi;
     776      1269241 :   unsigned int i;
     777      1882329 :   EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
     778              :     {
     779      1539235 :       pre_expr vexpr = expression_for_id (i);
     780      1539235 :       if (vexpr->kind == NAME)
     781       926147 :         return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
     782              :     }
     783              :   return NULL_TREE;
     784              : }
     785              : 
     786              : /* Insert an expression EXPR into a bitmapped set.  */
     787              : 
     788              : static void
     789     77112594 : bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
     790              : {
     791     77112594 :   unsigned int val = get_expr_value_id (expr);
     792     77112594 :   if (! value_id_constant_p (val))
     793              :     {
     794              :       /* Note this is the only function causing multiple expressions
     795              :          for the same value to appear in a set.  This is needed for
     796              :          TMP_GEN, PHI_GEN and NEW_SETs.  */
     797     74090586 :       bitmap_set_bit (&set->values, val);
     798     74090586 :       bitmap_set_bit (&set->expressions, get_expression_id (expr));
     799              :     }
     800     77112594 : }
     801              : 
     802              : /* Copy a bitmapped set ORIG, into bitmapped set DEST.  */
     803              : 
     804              : static void
     805     27442159 : bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
     806              : {
     807     27442159 :   bitmap_copy (&dest->expressions, &orig->expressions);
     808     27442159 :   bitmap_copy (&dest->values, &orig->values);
     809     27442159 : }
     810              : 
     811              : 
     812              : /* Free memory used up by SET.  */
     813              : static void
     814     74501854 : bitmap_set_free (bitmap_set_t set)
     815              : {
     816            0 :   bitmap_clear (&set->expressions);
     817     19831685 :   bitmap_clear (&set->values);
     818            0 : }
     819              : 
     820              : 
     821              : /* Sort pre_expr after their value-id.  */
     822              : 
     823              : static int
     824    432509929 : expr_cmp (const void *a_, const void *b_, void *)
     825              : {
     826    432509929 :   pre_expr a = *(pre_expr const *) a_;
     827    432509929 :   pre_expr b = *(pre_expr const *) b_;
     828    432509929 :   return a->value_id - b->value_id;
     829              : }
     830              : 
     831              : /* Return an expression that is a valid representation to insert for
     832              :    both A and B reaching expressions.  Return NULL if neither works,
     833              :    in this case all expressions of the value will be elided.  */
     834              : 
     835              : static pre_expr
     836       261676 : prefer (pre_expr a, pre_expr b)
     837              : {
     838       261676 :   if (a->kind == REFERENCE && b->kind == REFERENCE)
     839              :     {
     840        63347 :       auto refa = PRE_EXPR_REFERENCE (a);
     841        63347 :       auto refb = PRE_EXPR_REFERENCE (b);
     842        63347 :       auto &oprsa = refa->operands;
     843        63347 :       auto &oprsb = refb->operands;
     844        63347 :       pre_expr palias = NULL;
     845        63347 :       if (refa->set == refb->set
     846        60257 :           && refa->base_set == refb->base_set)
     847              :         ;
     848        11237 :       else if ((refb->set == refa->set
     849         3090 :                 || alias_set_subset_of (refb->set, refa->set))
     850        12666 :                && (refb->base_set == refa->base_set
     851         8827 :                    || alias_set_subset_of (refb->base_set, refa->base_set)))
     852              :         palias = a;
     853         4226 :       else if ((refa->set == refb->set
     854         1738 :                 || alias_set_subset_of (refa->set, refb->set))
     855         5771 :                && (refa->base_set == refb->base_set
     856         3249 :                    || alias_set_subset_of (refa->base_set, refb->base_set)))
     857              :         palias = b;
     858              :       else
     859              :         /* We have to chose an expression representation that can stand
     860              :            in for all others - there can be none, in which case we have
     861              :            to drop this PRE/hoisting opportunity.
     862              :            ???  Previously we've arranged for alias-set zero being used
     863              :            as fallback, but we do not really want to allocate a new expression
     864              :            here unless it proves to be absolutely necessary.  */
     865              :         return NULL;
     866        63044 :       pre_expr p = palias;
     867       126088 :       if (oprsa.length () > 1 && oprsb.length () > 1)
     868              :         {
     869        63044 :           vn_reference_op_t vroa = &oprsa[oprsa.length () - 2];
     870        63044 :           vn_reference_op_t vrob = &oprsb[oprsb.length () - 2];
     871        63044 :           if (vroa->opcode == MEM_REF && vrob->opcode == MEM_REF)
     872              :             {
     873              :               /* We have to canonicalize to the more conservative alignment.
     874              :                  gcc.dg/torture/pr65270-?.c.*/
     875        63019 :               pre_expr palign = NULL;
     876        63019 :               if (TYPE_ALIGN (vroa->type) < TYPE_ALIGN (vrob->type))
     877              :                 palign = a;
     878        62791 :               else if (TYPE_ALIGN (vroa->type) > TYPE_ALIGN (vrob->type))
     879              :                 palign = b;
     880          491 :               if (palign)
     881              :                 {
     882          491 :                   if (p && p != palign)
     883              :                     return NULL;
     884              :                   p = palign;
     885              :                 }
     886              :               /* We have to canonicalize to the more conservative (smaller)
     887              :                  innermost object access size.  gcc.dg/torture/pr110799.c.  */
     888        62626 :               if (TYPE_SIZE (vroa->type) != TYPE_SIZE (vrob->type))
     889              :                 {
     890         4839 :                   pre_expr psize = NULL;
     891         4839 :                   if (!TYPE_SIZE (vroa->type))
     892              :                     psize = a;
     893         4839 :                   else if (!TYPE_SIZE (vrob->type))
     894              :                     psize = b;
     895         4839 :                   else if (TREE_CODE (TYPE_SIZE (vroa->type)) == INTEGER_CST
     896         4839 :                            && TREE_CODE (TYPE_SIZE (vrob->type)) == INTEGER_CST)
     897              :                     {
     898         4820 :                       int cmp = tree_int_cst_compare (TYPE_SIZE (vroa->type),
     899         4820 :                                                       TYPE_SIZE (vrob->type));
     900         4820 :                       if (cmp < 0)
     901              :                         psize = a;
     902         2699 :                       else if (cmp > 0)
     903              :                         psize = b;
     904              :                     }
     905              :                   /* ???  What about non-constant sizes?  */
     906         4820 :                   if (psize)
     907              :                     {
     908         4820 :                       if (p && p != psize)
     909              :                         return NULL;
     910              :                       p = psize;
     911              :                     }
     912              :                 }
     913              :               /* When restrict info differs we have to pick the non-restricted
     914              :                  variant if available.  */
     915        58939 :               if (vroa->clique != vrob->clique
     916        51316 :                   || vroa->base != vrob->base)
     917              :                 {
     918         7629 :                   pre_expr prestrict = NULL;
     919         7629 :                   if (vroa->clique == 0)
     920              :                     prestrict = a;
     921         5753 :                   else if (vrob->clique == 0)
     922              :                     prestrict = b;
     923              :                   else
     924              :                     /* There's no conservative variant to pick.  */
     925              :                     return NULL;
     926         3366 :                   if (p && p != prestrict)
     927              :                     return NULL;
     928              :                   p = prestrict;
     929              :                 }
     930              :             }
     931              :         }
     932              :       /* Note we cannot leave it undecided because when having
     933              :          more than two expressions we have to keep doing
     934              :          pariwise reduction.  */
     935        54684 :       return p ? p : b;
     936              :     }
     937              :   /* Always prefer an non-REFERENCE, avoiding the above mess.  */
     938       198329 :   else if (a->kind == REFERENCE)
     939              :     return b;
     940       194841 :   else if (b->kind == REFERENCE)
     941              :     return a;
     942       163602 :   else if (a->kind == b->kind)
     943              :     ;
     944              :   /* And prefer NAME over anything else.  */
     945        10987 :   else if (b->kind == NAME)
     946              :     return b;
     947         8601 :   else if (a->kind == NAME)
     948         8601 :     return a;
     949              :   return b;
     950              : }
     951              : 
     952              : static void
     953              : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap exclusions,
     954              :               bitmap val_visited, vec<pre_expr> &post);
     955              : 
     956              : /* DFS walk leaders of VAL to their operands with leaders in SET, collecting
     957              :    expressions in SET in postorder into POST.  */
     958              : 
     959              : static void
     960    193174690 : pre_expr_DFS (unsigned val, bitmap_set_t set, bitmap exclusions,
     961              :               bitmap val_visited, vec<pre_expr> &post)
     962              : {
     963    193174690 :   unsigned int i;
     964    193174690 :   bitmap_iterator bi;
     965              : 
     966              :   /* Iterate over all leaders and DFS recurse.  Borrowed from
     967              :      bitmap_find_leader.  */
     968    193174690 :   bitmap exprset = value_expressions[val];
     969    193174690 :   if (!exprset->first->next)
     970              :     {
     971    455974573 :       EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
     972    290648467 :         if (bitmap_bit_p (&set->expressions, i)
     973    290648467 :             && !bitmap_bit_p (exclusions, i))
     974    165611755 :           pre_expr_DFS (expression_for_id (i), set, exclusions,
     975              :                         val_visited, post);
     976    165326106 :       return;
     977              :     }
     978              : 
     979     56436201 :   EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
     980     28587617 :     if (!bitmap_bit_p (exclusions, i))
     981     28228149 :       pre_expr_DFS (expression_for_id (i), set, exclusions,
     982              :                     val_visited, post);
     983              : }
     984              : 
     985              : /* DFS walk EXPR to its operands with leaders in SET, collecting
     986              :    expressions in SET in postorder into POST.  */
     987              : 
     988              : static void
     989    193839904 : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap exclusions,
     990              :               bitmap val_visited, vec<pre_expr> &post)
     991              : {
     992    193839904 :   switch (expr->kind)
     993              :     {
     994     89305773 :     case NARY:
     995     89305773 :       {
     996     89305773 :         vn_nary_op_t nary = PRE_EXPR_NARY (expr);
     997    239430224 :         for (unsigned i = 0; i < nary->length; i++)
     998              :           {
     999    150124451 :             if (TREE_CODE (nary->op[i]) != SSA_NAME)
    1000     47452932 :               continue;
    1001    102671519 :             unsigned int op_val_id = VN_INFO (nary->op[i])->value_id;
    1002              :             /* If we already found a leader for the value we've
    1003              :                recursed already.  Avoid the costly bitmap_find_leader.  */
    1004    102671519 :             if (bitmap_bit_p (&set->values, op_val_id)
    1005    102671519 :                 && bitmap_set_bit (val_visited, op_val_id))
    1006     64758236 :               pre_expr_DFS (op_val_id, set, exclusions, val_visited, post);
    1007              :           }
    1008              :         break;
    1009              :       }
    1010     30338276 :     case REFERENCE:
    1011     30338276 :       {
    1012     30338276 :         vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
    1013     30338276 :         vec<vn_reference_op_s> operands = ref->operands;
    1014     30338276 :         vn_reference_op_t operand;
    1015    119925620 :         for (unsigned i = 0; operands.iterate (i, &operand); i++)
    1016              :           {
    1017     89587344 :             tree op[3];
    1018     89587344 :             op[0] = operand->op0;
    1019     89587344 :             op[1] = operand->op1;
    1020     89587344 :             op[2] = operand->op2;
    1021    358349376 :             for (unsigned n = 0; n < 3; ++n)
    1022              :               {
    1023    268762032 :                 if (!op[n] || TREE_CODE (op[n]) != SSA_NAME)
    1024    246823731 :                   continue;
    1025     21938301 :                 unsigned op_val_id = VN_INFO (op[n])->value_id;
    1026     21938301 :                 if (bitmap_bit_p (&set->values, op_val_id)
    1027     21938301 :                     && bitmap_set_bit (val_visited, op_val_id))
    1028     11430841 :                   pre_expr_DFS (op_val_id, set, exclusions, val_visited, post);
    1029              :               }
    1030              :           }
    1031              :         break;
    1032              :       }
    1033    193839904 :     default:;
    1034              :     }
    1035    193839904 :   post.quick_push (expr);
    1036    193839904 : }
    1037              : 
    1038              : /* Generate an topological-ordered array of bitmap set SET.  If FOR_INSERTION
    1039              :    is true then perform canonexpr on the set.  */
    1040              : 
    1041              : static vec<pre_expr>
    1042     18732351 : sorted_array_from_bitmap_set (bitmap_set_t set, bool for_insertion)
    1043              : {
    1044     18732351 :   unsigned int i;
    1045     18732351 :   bitmap_iterator bi;
    1046     18732351 :   vec<pre_expr> result;
    1047              : 
    1048              :   /* Pre-allocate enough space for the array.  */
    1049     18732351 :   unsigned cnt = bitmap_count_bits (&set->expressions);
    1050     18732351 :   result.create (cnt);
    1051              : 
    1052              :   /* For expressions with the same value from EXPRESSIONS retain only
    1053              :      expressions that can be inserted in place of all others.  */
    1054     18732351 :   auto_bitmap exclusions;
    1055     18732351 :   bitmap_tree_view (exclusions);
    1056     18732351 :   if (for_insertion && cnt > 1)
    1057              :     {
    1058     26821800 :       EXECUTE_IF_SET_IN_BITMAP (&set->expressions, 0, i, bi)
    1059     24156732 :         result.safe_push (expression_for_id (i));
    1060      2665068 :       result.sort (expr_cmp, NULL);
    1061     48296254 :       for (unsigned i = 0; i < result.length () - 1; ++i)
    1062     21483059 :         if (result[i]->value_id == result[i+1]->value_id)
    1063              :           {
    1064       261676 :             if (pre_expr p = prefer (result[i], result[i+1]))
    1065              :               {
    1066              :                 /* We retain and iterate on exprs[i+1], if we want to
    1067              :                    retain exprs[i], swap both.  */
    1068       253013 :                 if (p == result[i])
    1069        46314 :                   std::swap (result[i], result[i+1]);
    1070       253013 :                 bitmap_set_bit (exclusions, get_expression_id (result[i]));
    1071              :               }
    1072              :             else
    1073              :               {
    1074              :                 /* If neither works for pairwise choosing a conservative
    1075              :                    alternative, drop all REFERENCE expressions for this value.
    1076              :                    REFERENCE are always toplevel, so no chain should be
    1077              :                    interrupted by pruning them.  */
    1078              :                 unsigned j, k;
    1079           23 :                 for (j = i;; --j)
    1080         8686 :                   if (j == 0
    1081         8686 :                       || result[j - 1]->value_id != result[i]->value_id)
    1082              :                     break;
    1083              :                 for (k = j;; ++k)
    1084              :                   {
    1085        18490 :                     if (result[k]->kind == REFERENCE)
    1086        18490 :                       bitmap_set_bit (exclusions,
    1087        18490 :                                       get_expression_id (result[k]));
    1088        18490 :                     if (k == result.length () - 1
    1089        18490 :                         || result[k + 1]->value_id != result[i]->value_id)
    1090              :                       break;
    1091              :                   }
    1092              :                 i = k;
    1093              :               }
    1094              :           }
    1095      2665068 :       result.truncate (0);
    1096              :     }
    1097              : 
    1098     18732351 :   bool single_p = true;
    1099     18732351 :   auto_bitmap val_visited (&grand_bitmap_obstack);
    1100     18732351 :   bitmap_tree_view (val_visited);
    1101    102946266 :   FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
    1102     84213915 :     if (bitmap_set_bit (val_visited, i))
    1103              :       {
    1104     74351100 :         if (!result.is_empty ())
    1105              :           {
    1106     61398201 :             single_p = false;
    1107     61398201 :             result.truncate (0);
    1108              :           }
    1109     74351100 :         pre_expr_DFS (i, set, exclusions, val_visited, result);
    1110              :         /* Mark i as entry that is not forward reachable.  Note we do
    1111              :            have cycles in the value graph so eventually i reaches itself.  */
    1112     74351100 :         bitmap_clear_bit (val_visited, i);
    1113              :       }
    1114              :   /* If we didn't by luck visit only a single entry to the value
    1115              :      graph visit now all not forward reachable values.  */
    1116     18732351 :   if (!single_p)
    1117              :     {
    1118      9741286 :       result.truncate (0);
    1119      9741286 :       auto_bitmap val_visited2 (&grand_bitmap_obstack);
    1120      9741286 :       bitmap_tree_view (val_visited2);
    1121     90193897 :       FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
    1122     80452611 :         if (!bitmap_bit_p (val_visited, i))
    1123              :           {
    1124     42634513 :             if (bitmap_set_bit (val_visited2, i))
    1125     42634513 :               pre_expr_DFS (i, set, exclusions, val_visited2, result);
    1126              :             else
    1127            0 :               gcc_unreachable ();
    1128              :           }
    1129      9741286 :       if (flag_checking)
    1130              :         {
    1131      9741235 :           bitmap_list_view (val_visited2);
    1132      9741235 :           gcc_assert (bitmap_equal_p (&set->values, val_visited2));
    1133              :         }
    1134      9741286 :     }
    1135              : 
    1136     18732351 :   return result;
    1137     18732351 : }
    1138              : 
    1139              : /* Subtract all expressions contained in ORIG from DEST.  */
    1140              : 
    1141              : static bitmap_set_t
    1142     33195866 : bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig,
    1143              :                                  bool copy_values = false)
    1144              : {
    1145     33195866 :   bitmap_set_t result = bitmap_set_new ();
    1146     33195866 :   bitmap_iterator bi;
    1147     33195866 :   unsigned int i;
    1148              : 
    1149     33195866 :   bitmap_and_compl (&result->expressions, &dest->expressions,
    1150     33195866 :                     &orig->expressions);
    1151              : 
    1152     33195866 :   if (copy_values)
    1153       673550 :     bitmap_copy (&result->values, &dest->values);
    1154              :   else
    1155    110271551 :     FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
    1156              :       {
    1157     77749235 :         pre_expr expr = expression_for_id (i);
    1158     77749235 :         unsigned int value_id = get_expr_value_id (expr);
    1159     77749235 :         bitmap_set_bit (&result->values, value_id);
    1160              :       }
    1161              : 
    1162     33195866 :   return result;
    1163              : }
    1164              : 
    1165              : /* Subtract all values in bitmap set B from bitmap set A.  */
    1166              : 
    1167              : static void
    1168      1280316 : bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
    1169              : {
    1170      1280316 :   unsigned int i;
    1171      1280316 :   bitmap_iterator bi;
    1172      1280316 :   unsigned to_remove = -1U;
    1173      1280316 :   bitmap_and_compl_into (&a->values, &b->values);
    1174      9165778 :   FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
    1175              :     {
    1176      7885462 :       if (to_remove != -1U)
    1177              :         {
    1178      1501033 :           bitmap_clear_bit (&a->expressions, to_remove);
    1179      1501033 :           to_remove = -1U;
    1180              :         }
    1181      7885462 :       pre_expr expr = expression_for_id (i);
    1182      7885462 :       if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
    1183      1560403 :         to_remove = i;
    1184              :     }
    1185      1280316 :   if (to_remove != -1U)
    1186        59370 :     bitmap_clear_bit (&a->expressions, to_remove);
    1187      1280316 : }
    1188              : 
    1189              : 
    1190              : /* Return true if bitmapped set SET contains the value VALUE_ID.  */
    1191              : 
    1192              : static bool
    1193    194662289 : bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
    1194              : {
    1195            0 :   if (value_id_constant_p (value_id))
    1196              :     return true;
    1197              : 
    1198     89969540 :   return bitmap_bit_p (&set->values, value_id);
    1199              : }
    1200              : 
    1201              : /* Return true if two bitmap sets are equal.  */
    1202              : 
    1203              : static bool
    1204     15957775 : bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
    1205              : {
    1206            0 :   return bitmap_equal_p (&a->values, &b->values);
    1207              : }
    1208              : 
    1209              : /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
    1210              :    and add it otherwise.  Return true if any changes were made.  */
    1211              : 
    1212              : static bool
    1213     34717167 : bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
    1214              : {
    1215     34717167 :   unsigned int val = get_expr_value_id (expr);
    1216     34717167 :   if (value_id_constant_p (val))
    1217              :     return false;
    1218              : 
    1219     34717167 :   if (bitmap_set_contains_value (set, val))
    1220              :     {
    1221              :       /* The number of expressions having a given value is usually
    1222              :          significantly less than the total number of expressions in SET.
    1223              :          Thus, rather than check, for each expression in SET, whether it
    1224              :          has the value LOOKFOR, we walk the reverse mapping that tells us
    1225              :          what expressions have a given value, and see if any of those
    1226              :          expressions are in our set.  For large testcases, this is about
    1227              :          5-10x faster than walking the bitmap.  If this is somehow a
    1228              :          significant lose for some cases, we can choose which set to walk
    1229              :          based on the set size.  */
    1230     14836462 :       unsigned int i;
    1231     14836462 :       bitmap_iterator bi;
    1232     14836462 :       bitmap exprset = value_expressions[val];
    1233     17264114 :       EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
    1234              :         {
    1235     17264114 :           if (bitmap_clear_bit (&set->expressions, i))
    1236              :             {
    1237     14836462 :               bitmap_set_bit (&set->expressions, get_expression_id (expr));
    1238     14836462 :               return i != get_expression_id (expr);
    1239              :             }
    1240              :         }
    1241            0 :       gcc_unreachable ();
    1242              :     }
    1243              : 
    1244     19880705 :   bitmap_insert_into_set (set, expr);
    1245     19880705 :   return true;
    1246              : }
    1247              : 
    1248              : /* Insert EXPR into SET if EXPR's value is not already present in
    1249              :    SET.  */
    1250              : 
    1251              : static void
    1252     61933260 : bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
    1253              : {
    1254     61933260 :   unsigned int val = get_expr_value_id (expr);
    1255              : 
    1256     61933260 :   gcc_checking_assert (expr->id == get_expression_id (expr));
    1257              : 
    1258              :   /* Constant values are always considered to be part of the set.  */
    1259     61933260 :   if (value_id_constant_p (val))
    1260              :     return;
    1261              : 
    1262              :   /* If the value membership changed, add the expression.  */
    1263     61873857 :   if (bitmap_set_bit (&set->values, val))
    1264     47182361 :     bitmap_set_bit (&set->expressions, expr->id);
    1265              : }
    1266              : 
    1267              : /* Print out EXPR to outfile.  */
    1268              : 
    1269              : static void
    1270         4573 : print_pre_expr (FILE *outfile, const pre_expr expr)
    1271              : {
    1272         4573 :   if (! expr)
    1273              :     {
    1274            0 :       fprintf (outfile, "NULL");
    1275            0 :       return;
    1276              :     }
    1277         4573 :   switch (expr->kind)
    1278              :     {
    1279            0 :     case CONSTANT:
    1280            0 :       print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr));
    1281            0 :       break;
    1282         3214 :     case NAME:
    1283         3214 :       print_generic_expr (outfile, PRE_EXPR_NAME (expr));
    1284         3214 :       break;
    1285         1072 :     case NARY:
    1286         1072 :       {
    1287         1072 :         unsigned int i;
    1288         1072 :         vn_nary_op_t nary = PRE_EXPR_NARY (expr);
    1289         1072 :         fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
    1290         4103 :         for (i = 0; i < nary->length; i++)
    1291              :           {
    1292         1959 :             print_generic_expr (outfile, nary->op[i]);
    1293         1959 :             if (i != (unsigned) nary->length - 1)
    1294          887 :               fprintf (outfile, ",");
    1295              :           }
    1296         1072 :         fprintf (outfile, "}");
    1297              :       }
    1298         1072 :       break;
    1299              : 
    1300          287 :     case REFERENCE:
    1301          287 :       {
    1302          287 :         vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
    1303          287 :         print_vn_reference_ops (outfile, ref->operands);
    1304          287 :         if (ref->vuse)
    1305              :           {
    1306          275 :             fprintf (outfile, "@");
    1307          275 :             print_generic_expr (outfile, ref->vuse);
    1308              :           }
    1309              :       }
    1310              :       break;
    1311              :     }
    1312              : }
    1313              : void debug_pre_expr (pre_expr);
    1314              : 
    1315              : /* Like print_pre_expr but always prints to stderr.  */
    1316              : DEBUG_FUNCTION void
    1317            0 : debug_pre_expr (pre_expr e)
    1318              : {
    1319            0 :   print_pre_expr (stderr, e);
    1320            0 :   fprintf (stderr, "\n");
    1321            0 : }
    1322              : 
    1323              : /* Print out SET to OUTFILE.  */
    1324              : 
    1325              : static void
    1326          913 : print_bitmap_set (FILE *outfile, bitmap_set_t set,
    1327              :                   const char *setname, int blockindex)
    1328              : {
    1329          913 :   fprintf (outfile, "%s[%d] := { ", setname, blockindex);
    1330          913 :   if (set)
    1331              :     {
    1332          913 :       bool first = true;
    1333          913 :       unsigned i;
    1334          913 :       bitmap_iterator bi;
    1335              : 
    1336         5345 :       FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
    1337              :         {
    1338         4432 :           const pre_expr expr = expression_for_id (i);
    1339              : 
    1340         4432 :           if (!first)
    1341         3806 :             fprintf (outfile, ", ");
    1342         4432 :           first = false;
    1343         4432 :           print_pre_expr (outfile, expr);
    1344              : 
    1345         4432 :           fprintf (outfile, " (%04d)", get_expr_value_id (expr));
    1346              :         }
    1347              :     }
    1348          913 :   fprintf (outfile, " }\n");
    1349          913 : }
    1350              : 
    1351              : void debug_bitmap_set (bitmap_set_t);
    1352              : 
    1353              : DEBUG_FUNCTION void
    1354            0 : debug_bitmap_set (bitmap_set_t set)
    1355              : {
    1356            0 :   print_bitmap_set (stderr, set, "debug", 0);
    1357            0 : }
    1358              : 
    1359              : void debug_bitmap_sets_for (basic_block);
    1360              : 
    1361              : DEBUG_FUNCTION void
    1362            0 : debug_bitmap_sets_for (basic_block bb)
    1363              : {
    1364            0 :   print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
    1365            0 :   print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
    1366            0 :   print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
    1367            0 :   print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
    1368            0 :   print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
    1369            0 :   if (do_partial_partial)
    1370            0 :     print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
    1371            0 :   print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
    1372            0 : }
    1373              : 
    1374              : /* Print out the expressions that have VAL to OUTFILE.  */
    1375              : 
    1376              : static void
    1377            0 : print_value_expressions (FILE *outfile, unsigned int val)
    1378              : {
    1379            0 :   bitmap set = value_expressions[val];
    1380            0 :   if (set)
    1381              :     {
    1382            0 :       bitmap_set x;
    1383            0 :       char s[10];
    1384            0 :       sprintf (s, "%04d", val);
    1385            0 :       x.expressions = *set;
    1386            0 :       print_bitmap_set (outfile, &x, s, 0);
    1387              :     }
    1388            0 : }
    1389              : 
    1390              : 
    1391              : DEBUG_FUNCTION void
    1392            0 : debug_value_expressions (unsigned int val)
    1393              : {
    1394            0 :   print_value_expressions (stderr, val);
    1395            0 : }
    1396              : 
    1397              : /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
    1398              :    represent it.  */
    1399              : 
    1400              : static pre_expr
    1401      5196099 : get_or_alloc_expr_for_constant (tree constant)
    1402              : {
    1403      5196099 :   unsigned int result_id;
    1404      5196099 :   struct pre_expr_d expr;
    1405      5196099 :   pre_expr newexpr;
    1406              : 
    1407      5196099 :   expr.kind = CONSTANT;
    1408      5196099 :   PRE_EXPR_CONSTANT (&expr) = constant;
    1409      5196099 :   result_id = lookup_expression_id (&expr);
    1410      5196099 :   if (result_id != 0)
    1411      4318892 :     return expression_for_id (result_id);
    1412              : 
    1413       877207 :   newexpr = pre_expr_pool.allocate ();
    1414       877207 :   newexpr->kind = CONSTANT;
    1415       877207 :   newexpr->loc = UNKNOWN_LOCATION;
    1416       877207 :   PRE_EXPR_CONSTANT (newexpr) = constant;
    1417       877207 :   alloc_expression_id (newexpr);
    1418       877207 :   newexpr->value_id = get_or_alloc_constant_value_id (constant);
    1419       877207 :   add_to_value (newexpr->value_id, newexpr);
    1420       877207 :   return newexpr;
    1421              : }
    1422              : 
    1423              : /* Translate the VUSE backwards through phi nodes in E->dest, so that
    1424              :    it has the value it would have in E->src.  Set *SAME_VALID to true
    1425              :    in case the new vuse doesn't change the value id of the OPERANDS.  */
    1426              : 
    1427              : static tree
    1428      4579955 : translate_vuse_through_block (vec<vn_reference_op_s> operands,
    1429              :                               alias_set_type set, alias_set_type base_set,
    1430              :                               tree type, tree vuse, edge e, bool *same_valid)
    1431              : {
    1432      4579955 :   basic_block phiblock = e->dest;
    1433      4579955 :   gimple *def = SSA_NAME_DEF_STMT (vuse);
    1434      4579955 :   ao_ref ref;
    1435              : 
    1436      4579955 :   if (same_valid)
    1437      3284917 :     *same_valid = true;
    1438              : 
    1439              :   /* If value-numbering provided a memory state for this
    1440              :      that dominates PHIBLOCK we can just use that.  */
    1441      4579955 :   if (gimple_nop_p (def)
    1442      4579955 :       || (gimple_bb (def) != phiblock
    1443      1213662 :           && dominated_by_p (CDI_DOMINATORS, phiblock, gimple_bb (def))))
    1444              :     return vuse;
    1445              : 
    1446              :   /* We have pruned expressions that are killed in PHIBLOCK via
    1447              :      prune_clobbered_mems but we have not rewritten the VUSE to the one
    1448              :      live at the start of the block.  If there is no virtual PHI to translate
    1449              :      through return the VUSE live at entry.  Otherwise the VUSE to translate
    1450              :      is the def of the virtual PHI node.  */
    1451      2667473 :   gphi *phi = get_virtual_phi (phiblock);
    1452      2667473 :   if (!phi)
    1453        97556 :     return BB_LIVE_VOP_ON_EXIT
    1454              :              (get_immediate_dominator (CDI_DOMINATORS, phiblock));
    1455              : 
    1456      2569917 :   if (same_valid
    1457      2569917 :       && ao_ref_init_from_vn_reference (&ref, set, base_set, type, operands))
    1458              :     {
    1459      1871475 :       bitmap visited = NULL;
    1460              :       /* Try to find a vuse that dominates this phi node by skipping
    1461              :          non-clobbering statements.  */
    1462      1871475 :       unsigned int cnt = param_sccvn_max_alias_queries_per_access;
    1463      1871475 :       vuse = get_continuation_for_phi (phi, &ref, true,
    1464              :                                        cnt, &visited, NULL, NULL);
    1465      1871475 :       if (visited)
    1466      1860752 :         BITMAP_FREE (visited);
    1467              :     }
    1468              :   else
    1469              :     vuse = NULL_TREE;
    1470              :   /* If we didn't find any, the value ID can't stay the same.  */
    1471      2569917 :   if (!vuse && same_valid)
    1472      1619372 :     *same_valid = false;
    1473              : 
    1474              :   /* ??? We would like to return vuse here as this is the canonical
    1475              :      upmost vdef that this reference is associated with.  But during
    1476              :      insertion of the references into the hash tables we only ever
    1477              :      directly insert with their direct gimple_vuse, hence returning
    1478              :      something else would make us not find the other expression.  */
    1479      2569917 :   return PHI_ARG_DEF (phi, e->dest_idx);
    1480              : }
    1481              : 
    1482              : /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
    1483              :    SET2 *or* SET3.  This is used to avoid making a set consisting of the union
    1484              :    of PA_IN and ANTIC_IN during insert and phi-translation.  */
    1485              : 
    1486              : static inline pre_expr
    1487     25007004 : find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
    1488              :                      bitmap_set_t set3 = NULL)
    1489              : {
    1490     25007004 :   pre_expr result = NULL;
    1491              : 
    1492     25007004 :   if (set1)
    1493     24880545 :     result = bitmap_find_leader (set1, val);
    1494     25007004 :   if (!result && set2)
    1495      1642090 :     result = bitmap_find_leader (set2, val);
    1496     25007004 :   if (!result && set3)
    1497            0 :     result = bitmap_find_leader (set3, val);
    1498     25007004 :   return result;
    1499              : }
    1500              : 
    1501              : /* Get the tree type for our PRE expression e.  */
    1502              : 
    1503              : static tree
    1504      7610605 : get_expr_type (const pre_expr e)
    1505              : {
    1506      7610605 :   switch (e->kind)
    1507              :     {
    1508      1008735 :     case NAME:
    1509      1008735 :       return TREE_TYPE (PRE_EXPR_NAME (e));
    1510       193814 :     case CONSTANT:
    1511       193814 :       return TREE_TYPE (PRE_EXPR_CONSTANT (e));
    1512      1368984 :     case REFERENCE:
    1513      1368984 :       return PRE_EXPR_REFERENCE (e)->type;
    1514      5039072 :     case NARY:
    1515      5039072 :       return PRE_EXPR_NARY (e)->type;
    1516              :     }
    1517            0 :   gcc_unreachable ();
    1518              : }
    1519              : 
    1520              : /* Get a representative SSA_NAME for a given expression that is available in B.
    1521              :    Since all of our sub-expressions are treated as values, we require
    1522              :    them to be SSA_NAME's for simplicity.
    1523              :    Prior versions of GVNPRE used to use "value handles" here, so that
    1524              :    an expression would be VH.11 + VH.10 instead of d_3 + e_6.  In
    1525              :    either case, the operands are really values (IE we do not expect
    1526              :    them to be usable without finding leaders).  */
    1527              : 
    1528              : static tree
    1529     19879340 : get_representative_for (const pre_expr e, basic_block b = NULL)
    1530              : {
    1531     19879340 :   tree name, valnum = NULL_TREE;
    1532     19879340 :   unsigned int value_id = get_expr_value_id (e);
    1533              : 
    1534     19879340 :   switch (e->kind)
    1535              :     {
    1536      9091746 :     case NAME:
    1537      9091746 :       return PRE_EXPR_NAME (e);
    1538      1961475 :     case CONSTANT:
    1539      1961475 :       return PRE_EXPR_CONSTANT (e);
    1540      8826119 :     case NARY:
    1541      8826119 :     case REFERENCE:
    1542      8826119 :       {
    1543              :         /* Go through all of the expressions representing this value
    1544              :            and pick out an SSA_NAME.  */
    1545      8826119 :         unsigned int i;
    1546      8826119 :         bitmap_iterator bi;
    1547      8826119 :         bitmap exprs = value_expressions[value_id];
    1548     22736345 :         EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
    1549              :           {
    1550     18561269 :             pre_expr rep = expression_for_id (i);
    1551     18561269 :             if (rep->kind == NAME)
    1552              :               {
    1553      8397095 :                 tree name = PRE_EXPR_NAME (rep);
    1554      8397095 :                 valnum = VN_INFO (name)->valnum;
    1555      8397095 :                 gimple *def = SSA_NAME_DEF_STMT (name);
    1556              :                 /* We have to return either a new representative or one
    1557              :                    that can be used for expression simplification and thus
    1558              :                    is available in B.  */
    1559      8397095 :                 if (! b
    1560      8061565 :                     || gimple_nop_p (def)
    1561     12414668 :                     || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
    1562      4651043 :                   return name;
    1563              :               }
    1564     10164174 :             else if (rep->kind == CONSTANT)
    1565            0 :               return PRE_EXPR_CONSTANT (rep);
    1566              :           }
    1567              :       }
    1568      4175076 :       break;
    1569              :     }
    1570              : 
    1571              :   /* If we reached here we couldn't find an SSA_NAME.  This can
    1572              :      happen when we've discovered a value that has never appeared in
    1573              :      the program as set to an SSA_NAME, as the result of phi translation.
    1574              :      Create one here.
    1575              :      ???  We should be able to re-use this when we insert the statement
    1576              :      to compute it.  */
    1577      4175076 :   name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
    1578      4175076 :   vn_ssa_aux_t vn_info = VN_INFO (name);
    1579      4175076 :   vn_info->value_id = value_id;
    1580      4175076 :   vn_info->valnum = valnum ? valnum : name;
    1581      4175076 :   vn_info->visited = true;
    1582              :   /* ???  For now mark this SSA name for release by VN.  */
    1583      4175076 :   vn_info->needs_insertion = true;
    1584      4175076 :   add_to_value (value_id, get_or_alloc_expr_for_name (name));
    1585      4175076 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1586              :     {
    1587           47 :       fprintf (dump_file, "Created SSA_NAME representative ");
    1588           47 :       print_generic_expr (dump_file, name);
    1589           47 :       fprintf (dump_file, " for expression:");
    1590           47 :       print_pre_expr (dump_file, e);
    1591           47 :       fprintf (dump_file, " (%04d)\n", value_id);
    1592              :     }
    1593              : 
    1594              :   return name;
    1595              : }
    1596              : 
    1597              : 
    1598              : static pre_expr
    1599              : phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
    1600              : 
    1601              : /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
    1602              :    the phis in PRED.  Return NULL if we can't find a leader for each part
    1603              :    of the translated expression.  */
    1604              : 
    1605              : static pre_expr
    1606     49927904 : phi_translate_1 (bitmap_set_t dest,
    1607              :                  pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
    1608              : {
    1609     49927904 :   basic_block pred = e->src;
    1610     49927904 :   basic_block phiblock = e->dest;
    1611     49927904 :   location_t expr_loc = expr->loc;
    1612     49927904 :   switch (expr->kind)
    1613              :     {
    1614     18912223 :     case NARY:
    1615     18912223 :       {
    1616     18912223 :         unsigned int i;
    1617     18912223 :         bool changed = false;
    1618     18912223 :         vn_nary_op_t nary = PRE_EXPR_NARY (expr);
    1619     18912223 :         vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
    1620              :                                            sizeof_vn_nary_op (nary->length));
    1621     18912223 :         memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
    1622              : 
    1623     44724873 :         for (i = 0; i < newnary->length; i++)
    1624              :           {
    1625     29426018 :             if (TREE_CODE (newnary->op[i]) != SSA_NAME)
    1626      9122165 :               continue;
    1627              :             else
    1628              :               {
    1629     20303853 :                 pre_expr leader, result;
    1630     20303853 :                 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
    1631     20303853 :                 leader = find_leader_in_sets (op_val_id, set1, set2);
    1632     20303853 :                 result = phi_translate (dest, leader, set1, set2, e);
    1633     20303853 :                 if (result)
    1634              :                   /* If op has a leader in the sets we translate make
    1635              :                      sure to use the value of the translated expression.
    1636              :                      We might need a new representative for that.  */
    1637     16690485 :                   newnary->op[i] = get_representative_for (result, pred);
    1638              :                 else if (!result)
    1639              :                   return NULL;
    1640              : 
    1641     16690485 :                 changed |= newnary->op[i] != nary->op[i];
    1642              :               }
    1643              :           }
    1644     15298855 :         if (changed)
    1645              :           {
    1646      7781949 :             unsigned int new_val_id;
    1647              : 
    1648      7781949 :             vn_nary_op_t saved_newnary
    1649      7781949 :               = XALLOCAVAR (struct vn_nary_op_s,
    1650              :                             sizeof_vn_nary_op (newnary->length));
    1651      7781949 :             memcpy (saved_newnary, newnary,
    1652              :                     sizeof_vn_nary_op (newnary->length));
    1653              : 
    1654              :             /* Try to simplify the new NARY.  */
    1655      7781949 :             tree res = vn_nary_simplify (newnary);
    1656      7781949 :             if (res)
    1657              :               {
    1658      2480044 :                 if (is_gimple_min_invariant (res))
    1659      1281266 :                   return get_or_alloc_expr_for_constant (res);
    1660              : 
    1661              :                 /* For non-CONSTANTs we have to make sure we can eventually
    1662              :                    insert the expression.  Which means we need to have a
    1663              :                    leader for it.  */
    1664      1198778 :                 gcc_assert (TREE_CODE (res) == SSA_NAME);
    1665              : 
    1666              :                 /* Do not allow simplifications to non-constants over
    1667              :                    backedges as this will likely result in a loop PHI node
    1668              :                    to be inserted and increased register pressure.
    1669              :                    See PR77498 - this avoids doing predcoms work in
    1670              :                    a less efficient way.  */
    1671      1198778 :                 if (e->flags & EDGE_DFS_BACK)
    1672              :                   ;
    1673              :                 else
    1674              :                   {
    1675      1115750 :                     unsigned value_id = VN_INFO (res)->value_id;
    1676              :                     /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
    1677              :                        dest has what we computed into ANTIC_OUT sofar
    1678              :                        so pick from that - since topological sorting
    1679              :                        by sorted_array_from_bitmap_set isn't perfect
    1680              :                        we may lose some cases here.  */
    1681      2231500 :                     pre_expr constant = find_leader_in_sets (value_id, dest,
    1682      1115750 :                                                              AVAIL_OUT (pred));
    1683      1115750 :                     if (constant)
    1684              :                       {
    1685       342657 :                         if (dump_file && (dump_flags & TDF_DETAILS))
    1686              :                           {
    1687            7 :                             fprintf (dump_file, "simplifying ");
    1688            7 :                             print_pre_expr (dump_file, expr);
    1689            7 :                             fprintf (dump_file, " translated %d -> %d to ",
    1690              :                                      phiblock->index, pred->index);
    1691            7 :                             PRE_EXPR_NARY (expr) = newnary;
    1692            7 :                             print_pre_expr (dump_file, expr);
    1693            7 :                             PRE_EXPR_NARY (expr) = nary;
    1694            7 :                             fprintf (dump_file, " to ");
    1695            7 :                             print_pre_expr (dump_file, constant);
    1696            7 :                             fprintf (dump_file, "\n");
    1697              :                           }
    1698              :                         return constant;
    1699              :                       }
    1700              :                   }
    1701              :                 /* Restore the unsimplified newnary, it was simplified
    1702              :                    to a NAME that we do not want (not as NARY anyway).  */
    1703       856121 :                 memcpy (newnary, saved_newnary,
    1704       856121 :                         sizeof_vn_nary_op (saved_newnary->length));
    1705              :               }
    1706              : 
    1707     12316052 :             tree result = vn_nary_op_lookup_pieces (newnary->length,
    1708      6158026 :                                                     newnary->opcode,
    1709              :                                                     newnary->type,
    1710              :                                                     &newnary->op[0],
    1711              :                                                     &nary);
    1712      6158026 :             if (result && is_gimple_min_invariant (result))
    1713            0 :               return get_or_alloc_expr_for_constant (result);
    1714              : 
    1715      6158026 :             if (!nary || nary->predicated_values)
    1716              :               new_val_id = 0;
    1717              :             else
    1718       818326 :               new_val_id = nary->value_id;
    1719      6158026 :             expr = get_or_alloc_expr_for_nary (newnary, new_val_id, expr_loc);
    1720      6158026 :             add_to_value (get_expr_value_id (expr), expr);
    1721              :           }
    1722              :         return expr;
    1723              :       }
    1724      5097220 :       break;
    1725              : 
    1726      5097220 :     case REFERENCE:
    1727      5097220 :       {
    1728      5097220 :         vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
    1729      5097220 :         vec<vn_reference_op_s> operands = ref->operands;
    1730      5097220 :         tree vuse = ref->vuse;
    1731      5097220 :         tree newvuse = vuse;
    1732      5097220 :         vec<vn_reference_op_s> newoperands = vNULL;
    1733      5097220 :         bool changed = false, same_valid = true;
    1734      5097220 :         unsigned int i, n;
    1735      5097220 :         vn_reference_op_t operand;
    1736      5097220 :         vn_reference_t newref;
    1737              : 
    1738     19335681 :         for (i = 0; operands.iterate (i, &operand); i++)
    1739              :           {
    1740     14637007 :             pre_expr opresult;
    1741     14637007 :             pre_expr leader;
    1742     14637007 :             tree op[3];
    1743     14637007 :             tree type = operand->type;
    1744     14637007 :             vn_reference_op_s newop = *operand;
    1745     14637007 :             op[0] = operand->op0;
    1746     14637007 :             op[1] = operand->op1;
    1747     14637007 :             op[2] = operand->op2;
    1748     57352510 :             for (n = 0; n < 3; ++n)
    1749              :               {
    1750     43114049 :                 unsigned int op_val_id;
    1751     43114049 :                 if (!op[n])
    1752     26095948 :                   continue;
    1753     17018101 :                 if (TREE_CODE (op[n]) != SSA_NAME)
    1754              :                   {
    1755              :                     /* We can't possibly insert these.  */
    1756     13430700 :                     if (n != 0
    1757     13430700 :                         && !is_gimple_min_invariant (op[n]))
    1758              :                       break;
    1759     13430700 :                     continue;
    1760              :                   }
    1761      3587401 :                 op_val_id = VN_INFO (op[n])->value_id;
    1762      3587401 :                 leader = find_leader_in_sets (op_val_id, set1, set2);
    1763      3587401 :                 opresult = phi_translate (dest, leader, set1, set2, e);
    1764      3587401 :                 if (opresult)
    1765              :                   {
    1766      3188855 :                     tree name = get_representative_for (opresult);
    1767      3188855 :                     changed |= name != op[n];
    1768      3188855 :                     op[n] = name;
    1769              :                   }
    1770              :                 else if (!opresult)
    1771              :                   break;
    1772              :               }
    1773     14637007 :             if (n != 3)
    1774              :               {
    1775       398546 :                 newoperands.release ();
    1776       398546 :                 return NULL;
    1777              :               }
    1778              :             /* When we translate a MEM_REF across a backedge and we have
    1779              :                restrict info that's not from our functions parameters
    1780              :                we have to remap it since we now may deal with a different
    1781              :                instance where the dependence info is no longer valid.
    1782              :                See PR102970.  Note instead of keeping a remapping table
    1783              :                per backedge we simply throw away restrict info.  */
    1784     14238461 :             if ((newop.opcode == MEM_REF
    1785     14238461 :                  || newop.opcode == TARGET_MEM_REF)
    1786      4820366 :                 && newop.clique > 1
    1787       163200 :                 && (e->flags & EDGE_DFS_BACK))
    1788              :               {
    1789              :                 newop.clique = 0;
    1790              :                 newop.base = 0;
    1791              :                 changed = true;
    1792              :               }
    1793     14215745 :             if (!changed)
    1794     11642079 :               continue;
    1795      2596382 :             if (!newoperands.exists ())
    1796      1330259 :               newoperands = operands.copy ();
    1797              :             /* We may have changed from an SSA_NAME to a constant */
    1798      2596382 :             if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
    1799              :               newop.opcode = TREE_CODE (op[0]);
    1800      2596382 :             newop.type = type;
    1801      2596382 :             newop.op0 = op[0];
    1802      2596382 :             newop.op1 = op[1];
    1803      2596382 :             newop.op2 = op[2];
    1804      2596382 :             newoperands[i] = newop;
    1805              :           }
    1806      9397348 :         gcc_checking_assert (i == operands.length ());
    1807              : 
    1808      4698674 :         if (vuse)
    1809              :           {
    1810     11149789 :             newvuse = translate_vuse_through_block (newoperands.exists ()
    1811      4579955 :                                                     ? newoperands : operands,
    1812              :                                                     ref->set, ref->base_set,
    1813              :                                                     ref->type, vuse, e,
    1814              :                                                     changed
    1815              :                                                     ? NULL : &same_valid);
    1816      4579955 :             if (newvuse == NULL_TREE)
    1817              :               {
    1818            0 :                 newoperands.release ();
    1819            0 :                 return NULL;
    1820              :               }
    1821              :           }
    1822              : 
    1823      4698674 :         if (changed || newvuse != vuse)
    1824              :           {
    1825      3311795 :             unsigned int new_val_id;
    1826              : 
    1827      5295480 :             tree result = vn_reference_lookup_pieces (newvuse, ref->set,
    1828              :                                                       ref->base_set,
    1829              :                                                       ref->type,
    1830      3311795 :                                                       newoperands.exists ()
    1831      3311795 :                                                       ? newoperands : operands,
    1832              :                                                       &newref, VN_WALK);
    1833              : 
    1834              :             /* We can always insert constants, so if we have a partial
    1835              :                redundant constant load of another type try to translate it
    1836              :                to a constant of appropriate type.  */
    1837      3311795 :             if (result && is_gimple_min_invariant (result))
    1838              :               {
    1839        79707 :                 tree tem = result;
    1840        79707 :                 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
    1841              :                   {
    1842           85 :                     tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
    1843           85 :                     if (tem && !is_gimple_min_invariant (tem))
    1844              :                       tem = NULL_TREE;
    1845              :                   }
    1846        79707 :                 if (tem)
    1847              :                   {
    1848        79707 :                     newoperands.release ();
    1849        79707 :                     return get_or_alloc_expr_for_constant (tem);
    1850              :                   }
    1851              :               }
    1852              : 
    1853              :             /* If we'd have to convert things we would need to validate
    1854              :                if we can insert the translated expression.  So fail
    1855              :                here for now - we cannot insert an alias with a different
    1856              :                type in the VN tables either, as that would assert.  */
    1857      3232088 :             if (result
    1858      3232088 :                 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
    1859              :               {
    1860         1012 :                 newoperands.release ();
    1861         1012 :                 return NULL;
    1862              :               }
    1863      2657737 :             else if (!result && newref
    1864      3231076 :                      && !useless_type_conversion_p (ref->type, newref->type))
    1865              :               {
    1866            0 :                 newoperands.release ();
    1867            0 :                 return NULL;
    1868              :               }
    1869              : 
    1870      3231076 :             if (newref)
    1871              :               {
    1872       573339 :                 new_val_id = newref->value_id;
    1873       573339 :                 newvuse = newref->vuse;
    1874              :               }
    1875              :             else
    1876              :               {
    1877      2657737 :                 if (changed || !same_valid)
    1878              :                   new_val_id = 0;
    1879              :                 else
    1880       133560 :                   new_val_id = ref->value_id;
    1881              :               }
    1882      3231076 :             newref = XALLOCAVAR (struct vn_reference_s,
    1883              :                                  sizeof (vn_reference_s));
    1884      3231076 :             memcpy (newref, ref, sizeof (vn_reference_s));
    1885      3231076 :             newref->next = NULL;
    1886      3231076 :             newref->value_id = new_val_id;
    1887      3231076 :             newref->vuse = newvuse;
    1888      6462152 :             newref->operands
    1889      3231076 :               = newoperands.exists () ? newoperands : operands.copy ();
    1890      3231076 :             newoperands = vNULL;
    1891      3231076 :             newref->type = ref->type;
    1892      3231076 :             newref->result = result;
    1893      3231076 :             newref->hashcode = vn_reference_compute_hash (newref);
    1894      3231076 :             expr = get_or_alloc_expr_for_reference (newref, new_val_id,
    1895              :                                                     expr_loc, true);
    1896      3231076 :             add_to_value (get_expr_value_id (expr), expr);
    1897              :           }
    1898      4617955 :         newoperands.release ();
    1899      4617955 :         return expr;
    1900              :       }
    1901     25918461 :       break;
    1902              : 
    1903     25918461 :     case NAME:
    1904     25918461 :       {
    1905     25918461 :         tree name = PRE_EXPR_NAME (expr);
    1906     25918461 :         gimple *def_stmt = SSA_NAME_DEF_STMT (name);
    1907              :         /* If the SSA name is defined by a PHI node in this block,
    1908              :            translate it.  */
    1909     25918461 :         if (gimple_code (def_stmt) == GIMPLE_PHI
    1910     25918461 :             && gimple_bb (def_stmt) == phiblock)
    1911              :           {
    1912      8087599 :             tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
    1913              : 
    1914              :             /* Handle constant. */
    1915      8087599 :             if (is_gimple_min_invariant (def))
    1916      2388207 :               return get_or_alloc_expr_for_constant (def);
    1917              : 
    1918      5699392 :             return get_or_alloc_expr_for_name (def);
    1919              :           }
    1920              :         /* Otherwise return it unchanged - it will get removed if its
    1921              :            value is not available in PREDs AVAIL_OUT set of expressions
    1922              :            by the subtraction of TMP_GEN.  */
    1923              :         return expr;
    1924              :       }
    1925              : 
    1926            0 :     default:
    1927            0 :       gcc_unreachable ();
    1928              :     }
    1929              : }
    1930              : 
    1931              : /* Wrapper around phi_translate_1 providing caching functionality.  */
    1932              : 
    1933              : static pre_expr
    1934     91441599 : phi_translate (bitmap_set_t dest, pre_expr expr,
    1935              :                bitmap_set_t set1, bitmap_set_t set2, edge e)
    1936              : {
    1937     91441599 :   expr_pred_trans_t slot = NULL;
    1938     91441599 :   pre_expr phitrans;
    1939              : 
    1940     91441599 :   if (!expr)
    1941              :     return NULL;
    1942              : 
    1943              :   /* Constants contain no values that need translation.  */
    1944     89468568 :   if (expr->kind == CONSTANT)
    1945              :     return expr;
    1946              : 
    1947     89468348 :   if (value_id_constant_p (get_expr_value_id (expr)))
    1948              :     return expr;
    1949              : 
    1950              :   /* Don't add translations of NAMEs as those are cheap to translate.  */
    1951     89468348 :   if (expr->kind != NAME)
    1952              :     {
    1953     63549887 :       if (phi_trans_add (&slot, expr, e->src))
    1954     39540444 :         return slot->v == 0 ? NULL : expression_for_id (slot->v);
    1955              :       /* Store NULL for the value we want to return in the case of
    1956              :          recursing.  */
    1957     24009443 :       slot->v = 0;
    1958              :     }
    1959              : 
    1960              :   /* Translate.  */
    1961     49927904 :   basic_block saved_valueize_bb = vn_context_bb;
    1962     49927904 :   vn_context_bb = e->src;
    1963     49927904 :   phitrans = phi_translate_1 (dest, expr, set1, set2, e);
    1964     49927904 :   vn_context_bb = saved_valueize_bb;
    1965              : 
    1966     49927904 :   if (slot)
    1967              :     {
    1968              :       /* We may have reallocated.  */
    1969     24009443 :       phi_trans_add (&slot, expr, e->src);
    1970     24009443 :       if (phitrans)
    1971     19996517 :         slot->v = get_expression_id (phitrans);
    1972              :       else
    1973              :         /* Remove failed translations again, they cause insert
    1974              :            iteration to not pick up new opportunities reliably.  */
    1975      4012926 :         PHI_TRANS_TABLE (e->src)->clear_slot (slot);
    1976              :     }
    1977              : 
    1978              :   return phitrans;
    1979              : }
    1980              : 
    1981              : 
    1982              : /* For each expression in SET, translate the values through phi nodes
    1983              :    in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
    1984              :    expressions in DEST.  */
    1985              : 
    1986              : static void
    1987     20913862 : phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
    1988              : {
    1989     20913862 :   bitmap_iterator bi;
    1990     20913862 :   unsigned int i;
    1991              : 
    1992     20913862 :   if (gimple_seq_empty_p (phi_nodes (e->dest)))
    1993              :     {
    1994     13950813 :       bitmap_set_copy (dest, set);
    1995     13950813 :       return;
    1996              :     }
    1997              : 
    1998              :   /* Allocate the phi-translation cache where we have an idea about
    1999              :      its size.  hash-table implementation internals tell us that
    2000              :      allocating the table to fit twice the number of elements will
    2001              :      make sure we do not usually re-allocate.  */
    2002      6963049 :   if (!PHI_TRANS_TABLE (e->src))
    2003      6062721 :     PHI_TRANS_TABLE (e->src) = new hash_table<expr_pred_trans_d>
    2004      6062721 :                                    (2 * bitmap_count_bits (&set->expressions));
    2005     46234714 :   FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
    2006              :     {
    2007     39271665 :       pre_expr expr = expression_for_id (i);
    2008     39271665 :       pre_expr translated = phi_translate (dest, expr, set, NULL, e);
    2009     39271665 :       if (!translated)
    2010      1973281 :         continue;
    2011              : 
    2012     37298384 :       bitmap_insert_into_set (dest, translated);
    2013              :     }
    2014              : }
    2015              : 
    2016              : /* Find the leader for a value (i.e., the name representing that
    2017              :    value) in a given set, and return it.  Return NULL if no leader
    2018              :    is found.  */
    2019              : 
    2020              : static pre_expr
    2021     57731838 : bitmap_find_leader (bitmap_set_t set, unsigned int val)
    2022              : {
    2023     57731838 :   if (value_id_constant_p (val))
    2024      1820572 :     return constant_value_expressions[-val];
    2025              : 
    2026     55911266 :   if (bitmap_set_contains_value (set, val))
    2027              :     {
    2028              :       /* Rather than walk the entire bitmap of expressions, and see
    2029              :          whether any of them has the value we are looking for, we look
    2030              :          at the reverse mapping, which tells us the set of expressions
    2031              :          that have a given value (IE value->expressions with that
    2032              :          value) and see if any of those expressions are in our set.
    2033              :          The number of expressions per value is usually significantly
    2034              :          less than the number of expressions in the set.  In fact, for
    2035              :          large testcases, doing it this way is roughly 5-10x faster
    2036              :          than walking the bitmap.
    2037              :          If this is somehow a significant lose for some cases, we can
    2038              :          choose which set to walk based on which set is smaller.  */
    2039     26318527 :       unsigned int i;
    2040     26318527 :       bitmap_iterator bi;
    2041     26318527 :       bitmap exprset = value_expressions[val];
    2042              : 
    2043     26318527 :       if (!exprset->first->next)
    2044     33440927 :         EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
    2045     31068473 :           if (bitmap_bit_p (&set->expressions, i))
    2046     23852978 :             return expression_for_id (i);
    2047              : 
    2048      6391668 :       EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
    2049      3926119 :         return expression_for_id (i);
    2050              :     }
    2051              :   return NULL;
    2052              : }
    2053              : 
    2054              : /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
    2055              :    BLOCK by seeing if it is not killed in the block.  Note that we are
    2056              :    only determining whether there is a store that kills it.  Because
    2057              :    of the order in which clean iterates over values, we are guaranteed
    2058              :    that altered operands will have caused us to be eliminated from the
    2059              :    ANTIC_IN set already.  */
    2060              : 
    2061              : static bool
    2062      1689230 : value_dies_in_block_x (pre_expr expr, basic_block block)
    2063              : {
    2064      1689230 :   tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
    2065      1689230 :   vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
    2066      1689230 :   gimple *def;
    2067      1689230 :   gimple_stmt_iterator gsi;
    2068      1689230 :   unsigned id = get_expression_id (expr);
    2069      1689230 :   bool res = false;
    2070      1689230 :   ao_ref ref;
    2071              : 
    2072      1689230 :   if (!vuse)
    2073              :     return false;
    2074              : 
    2075              :   /* Lookup a previously calculated result.  */
    2076      1689230 :   if (EXPR_DIES (block)
    2077      1689230 :       && bitmap_bit_p (EXPR_DIES (block), id * 2))
    2078       140227 :     return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
    2079              : 
    2080              :   /* A memory expression {e, VUSE} dies in the block if there is a
    2081              :      statement that may clobber e.  If, starting statement walk from the
    2082              :      top of the basic block, a statement uses VUSE there can be no kill
    2083              :      in between that use and the original statement that loaded {e, VUSE},
    2084              :      so we can stop walking.  */
    2085      1549003 :   ref.base = NULL_TREE;
    2086     13718207 :   for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
    2087              :     {
    2088     11678236 :       tree def_vuse, def_vdef;
    2089     11678236 :       def = gsi_stmt (gsi);
    2090     11678236 :       def_vuse = gimple_vuse (def);
    2091     11678236 :       def_vdef = gimple_vdef (def);
    2092              : 
    2093              :       /* Not a memory statement.  */
    2094     11678236 :       if (!def_vuse)
    2095      8292953 :         continue;
    2096              : 
    2097              :       /* Not a may-def.  */
    2098      3385283 :       if (!def_vdef)
    2099              :         {
    2100              :           /* A load with the same VUSE, we're done.  */
    2101       975923 :           if (def_vuse == vuse)
    2102              :             break;
    2103              : 
    2104       682437 :           continue;
    2105              :         }
    2106              : 
    2107              :       /* Init ref only if we really need it.  */
    2108      2409360 :       if (ref.base == NULL_TREE
    2109      3577649 :           && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->base_set,
    2110      1168289 :                                              refx->type, refx->operands))
    2111              :         {
    2112              :           res = true;
    2113              :           break;
    2114              :         }
    2115              :       /* If the statement may clobber expr, it dies.  */
    2116      2374170 :       if (stmt_may_clobber_ref_p_1 (def, &ref))
    2117              :         {
    2118              :           res = true;
    2119              :           break;
    2120              :         }
    2121              :     }
    2122              : 
    2123              :   /* Remember the result.  */
    2124      1549003 :   if (!EXPR_DIES (block))
    2125       727400 :     EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
    2126      1549003 :   bitmap_set_bit (EXPR_DIES (block), id * 2);
    2127      1549003 :   if (res)
    2128       764549 :     bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
    2129              : 
    2130              :   return res;
    2131              : }
    2132              : 
    2133              : 
    2134              : /* Determine if OP is valid in SET1 U SET2, which it is when the union
    2135              :    contains its value-id.  */
    2136              : 
    2137              : static bool
    2138    267221975 : op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
    2139              : {
    2140    267221975 :   if (op && TREE_CODE (op) == SSA_NAME)
    2141              :     {
    2142     72587692 :       unsigned int value_id = VN_INFO (op)->value_id;
    2143    145173727 :       if (!(bitmap_set_contains_value (set1, value_id)
    2144      2310596 :             || (set2 && bitmap_set_contains_value  (set2, value_id))))
    2145      2256429 :         return false;
    2146              :     }
    2147              :   return true;
    2148              : }
    2149              : 
    2150              : /* Determine if the expression EXPR is valid in SET1 U SET2.
    2151              :    ONLY SET2 CAN BE NULL.
    2152              :    This means that we have a leader for each part of the expression
    2153              :    (if it consists of values), or the expression is an SSA_NAME.
    2154              :    For loads/calls, we also see if the vuse is killed in this block.  */
    2155              : 
    2156              : static bool
    2157    126411221 : valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
    2158              : {
    2159    126411221 :   switch (expr->kind)
    2160              :     {
    2161              :     case NAME:
    2162              :       /* By construction all NAMEs are available.  Non-available
    2163              :          NAMEs are removed by subtracting TMP_GEN from the sets.  */
    2164              :       return true;
    2165     54871633 :     case NARY:
    2166     54871633 :       {
    2167     54871633 :         unsigned int i;
    2168     54871633 :         vn_nary_op_t nary = PRE_EXPR_NARY (expr);
    2169    141798313 :         for (i = 0; i < nary->length; i++)
    2170     89037223 :           if (!op_valid_in_sets (set1, set2, nary->op[i]))
    2171              :             return false;
    2172              :         return true;
    2173              :       }
    2174     20373274 :       break;
    2175     20373274 :     case REFERENCE:
    2176     20373274 :       {
    2177     20373274 :         vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
    2178     20373274 :         vn_reference_op_t vro;
    2179     20373274 :         unsigned int i;
    2180              : 
    2181     79719540 :         FOR_EACH_VEC_ELT (ref->operands, i, vro)
    2182              :           {
    2183     59492152 :             if (!op_valid_in_sets (set1, set2, vro->op0)
    2184     59346300 :                 || !op_valid_in_sets (set1, set2, vro->op1)
    2185    118838452 :                 || !op_valid_in_sets (set1, set2, vro->op2))
    2186              :               return false;
    2187              :           }
    2188              :         return true;
    2189              :       }
    2190            0 :     default:
    2191            0 :       gcc_unreachable ();
    2192              :     }
    2193              : }
    2194              : 
    2195              : /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
    2196              :    This means expressions that are made up of values we have no leaders for
    2197              :    in SET1 or SET2.  */
    2198              : 
    2199              : static void
    2200     14771662 : clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
    2201              : {
    2202     14771662 :   vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1, false);
    2203     15751004 :   bool changed;
    2204              : 
    2205     15751004 :   do
    2206              :     {
    2207     15751004 :       unsigned j = 0;
    2208     15751004 :       changed = false;
    2209     82779871 :       for (unsigned i = 0; i < exprs.length (); ++i)
    2210              :         {
    2211     67028867 :           pre_expr expr = exprs[i];
    2212     67028867 :           if (!valid_in_sets (set1, set2, expr))
    2213              :             {
    2214      2256424 :               unsigned int val = get_expr_value_id (expr);
    2215      2256424 :               bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
    2216              :               /* We are entered with possibly multiple expressions for a value
    2217              :                  so before removing a value from the set see if there's an
    2218              :                  expression for it left.  */
    2219      2256424 :               if (! bitmap_find_leader (set1, val))
    2220              :                 {
    2221      2245496 :                   bitmap_clear_bit (&set1->values, val);
    2222      2245496 :                   changed = true;
    2223              :                 }
    2224              :             }
    2225              :           else
    2226              :             {
    2227     64772443 :               exprs[j] = expr;
    2228     64772443 :               ++j;
    2229              :             }
    2230              :         }
    2231     15751004 :       exprs.truncate (j);
    2232              :     }
    2233              :   /* As the value graph can have cycles we have to iterate here.  */
    2234              :   while (changed);
    2235     14771662 :   exprs.release ();
    2236              : 
    2237     14771662 :   if (flag_checking)
    2238              :     {
    2239     14771475 :       unsigned j;
    2240     14771475 :       bitmap_iterator bi;
    2241     73931270 :       FOR_EACH_EXPR_ID_IN_SET (set1, j, bi)
    2242     59159795 :         gcc_assert (valid_in_sets (set1, set2, expression_for_id (j)));
    2243              :     }
    2244     14771662 : }
    2245              : 
    2246              : /* Clean the set of expressions that are no longer valid in SET because
    2247              :    they are clobbered in BLOCK or because they trap and may not be executed.
    2248              :    When CLEAN_TRAPS is true remove all possibly trapping expressions.  */
    2249              : 
    2250              : static void
    2251     17238091 : prune_clobbered_mems (bitmap_set_t set, basic_block block, bool clean_traps)
    2252              : {
    2253     17238091 :   bitmap_iterator bi;
    2254     17238091 :   unsigned i;
    2255     17238091 :   unsigned to_remove = -1U;
    2256     17238091 :   bool any_removed = false;
    2257              : 
    2258     75848157 :   FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
    2259              :     {
    2260              :       /* Remove queued expr.  */
    2261     58610066 :       if (to_remove != -1U)
    2262              :         {
    2263       608674 :           bitmap_clear_bit (&set->expressions, to_remove);
    2264       608674 :           any_removed = true;
    2265       608674 :           to_remove = -1U;
    2266              :         }
    2267              : 
    2268     58610066 :       pre_expr expr = expression_for_id (i);
    2269     58610066 :       if (expr->kind == REFERENCE)
    2270              :         {
    2271      8282962 :           vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
    2272      8282962 :           if (ref->vuse)
    2273              :             {
    2274      7519471 :               gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
    2275      7519471 :               if (!gimple_nop_p (def_stmt)
    2276              :                   /* If value-numbering provided a memory state for this
    2277              :                      that dominates BLOCK we're done, otherwise we have
    2278              :                      to check if the value dies in BLOCK.  */
    2279      9278946 :                   && !(gimple_bb (def_stmt) != block
    2280      3908184 :                        && dominated_by_p (CDI_DOMINATORS,
    2281      3908184 :                                           block, gimple_bb (def_stmt)))
    2282      9208701 :                   && value_dies_in_block_x (expr, block))
    2283              :                 to_remove = i;
    2284              :             }
    2285              :           /* If the REFERENCE may trap make sure the block does not contain
    2286              :              a possible exit point.
    2287              :              ???  This is overly conservative if we translate AVAIL_OUT
    2288              :              as the available expression might be after the exit point.  */
    2289      7596016 :           if ((BB_MAY_NOTRETURN (block) || clean_traps)
    2290      8540520 :               && vn_reference_may_trap (ref))
    2291              :             to_remove = i;
    2292              :         }
    2293     50327104 :       else if (expr->kind == NARY)
    2294              :         {
    2295     25497814 :           vn_nary_op_t nary = PRE_EXPR_NARY (expr);
    2296              :           /* If the NARY may trap make sure the block does not contain
    2297              :              a possible exit point.
    2298              :              ???  This is overly conservative if we translate AVAIL_OUT
    2299              :              as the available expression might be after the exit point.  */
    2300     21281876 :           if ((BB_MAY_NOTRETURN (block) || clean_traps)
    2301     26385103 :               && vn_nary_may_trap (nary))
    2302              :             to_remove = i;
    2303              :         }
    2304              :     }
    2305              : 
    2306              :   /* Remove queued expr.  */
    2307     17238091 :   if (to_remove != -1U)
    2308              :     {
    2309       433396 :       bitmap_clear_bit (&set->expressions, to_remove);
    2310       433396 :       any_removed = true;
    2311              :     }
    2312              : 
    2313              :   /* Above we only removed expressions, now clean the set of values
    2314              :      which no longer have any corresponding expression.  We cannot
    2315              :      clear the value at the time we remove an expression since there
    2316              :      may be multiple expressions per value.
    2317              :      If we'd queue possibly to be removed values we could use
    2318              :      the bitmap_find_leader way to see if there's still an expression
    2319              :      for it.  For some ratio of to be removed values and number of
    2320              :      values/expressions in the set this might be faster than rebuilding
    2321              :      the value-set.
    2322              :      Note when there's a MAX solution on one edge (clean_traps) do not
    2323              :      prune values as we need to consider the resulting expression set MAX
    2324              :      as well.  This avoids a later growing ANTIC_IN value-set during
    2325              :      iteration, when the explicitly represented expression set grows. */
    2326     17238091 :   if (any_removed && !clean_traps)
    2327              :     {
    2328       496797 :       bitmap_clear (&set->values);
    2329      2816760 :       FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
    2330              :         {
    2331      2319963 :           pre_expr expr = expression_for_id (i);
    2332      2319963 :           unsigned int value_id = get_expr_value_id (expr);
    2333      2319963 :           bitmap_set_bit (&set->values, value_id);
    2334              :         }
    2335              :     }
    2336     17238091 : }
    2337              : 
    2338              : /* Compute the ANTIC set for BLOCK.
    2339              : 
    2340              :    If succs(BLOCK) > 1 then
    2341              :      ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
    2342              :    else if succs(BLOCK) == 1 then
    2343              :      ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
    2344              : 
    2345              :    ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
    2346              : 
    2347              :    Note that clean() is deferred until after the iteration.  */
    2348              : 
    2349              : static bool
    2350     15962485 : compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
    2351              : {
    2352     15962485 :   bitmap_set_t S, old, ANTIC_OUT;
    2353     15962485 :   edge e;
    2354     15962485 :   edge_iterator ei;
    2355              : 
    2356     15962485 :   bool was_visited = BB_VISITED (block);
    2357     15962485 :   bool changed = ! BB_VISITED (block);
    2358     15962485 :   bool any_max_on_edge = false;
    2359              : 
    2360     15962485 :   BB_VISITED (block) = 1;
    2361     15962485 :   old = ANTIC_OUT = S = NULL;
    2362              : 
    2363              :   /* If any edges from predecessors are abnormal, antic_in is empty,
    2364              :      so do nothing.  */
    2365     15962485 :   if (block_has_abnormal_pred_edge)
    2366         4710 :     goto maybe_dump_sets;
    2367              : 
    2368     15957775 :   old = ANTIC_IN (block);
    2369     15957775 :   ANTIC_OUT = bitmap_set_new ();
    2370              : 
    2371              :   /* If the block has no successors, ANTIC_OUT is empty.  */
    2372     15957775 :   if (EDGE_COUNT (block->succs) == 0)
    2373              :     ;
    2374              :   /* If we have one successor, we could have some phi nodes to
    2375              :      translate through.  */
    2376     15957775 :   else if (single_succ_p (block))
    2377              :     {
    2378     10188649 :       e = single_succ_edge (block);
    2379     10188649 :       gcc_assert (BB_VISITED (e->dest));
    2380     10188649 :       phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
    2381              :     }
    2382              :   /* If we have multiple successors, we take the intersection of all of
    2383              :      them.  Note that in the case of loop exit phi nodes, we may have
    2384              :      phis to translate through.  */
    2385              :   else
    2386              :     {
    2387      5769126 :       size_t i;
    2388      5769126 :       edge first = NULL;
    2389              : 
    2390      5769126 :       auto_vec<edge> worklist (EDGE_COUNT (block->succs));
    2391     17421701 :       FOR_EACH_EDGE (e, ei, block->succs)
    2392              :         {
    2393     11652575 :           if (!first
    2394      6255507 :               && BB_VISITED (e->dest))
    2395              :             first = e;
    2396      5883449 :           else if (BB_VISITED (e->dest))
    2397      5205812 :             worklist.quick_push (e);
    2398              :           else
    2399              :             {
    2400              :               /* Unvisited successors get their ANTIC_IN replaced by the
    2401              :                  maximal set to arrive at a maximum ANTIC_IN solution.
    2402              :                  We can ignore them in the intersection operation and thus
    2403              :                  need not explicitly represent that maximum solution.  */
    2404       677637 :               any_max_on_edge = true;
    2405       677637 :               if (dump_file && (dump_flags & TDF_DETAILS))
    2406           18 :                 fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
    2407           18 :                          e->src->index, e->dest->index);
    2408              :             }
    2409              :         }
    2410              : 
    2411              :       /* Of multiple successors we have to have visited one already
    2412              :          which is guaranteed by iteration order.  */
    2413      5769126 :       gcc_assert (first != NULL);
    2414              : 
    2415      5769126 :       phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
    2416              : 
    2417              :       /* If we have multiple successors we need to intersect the ANTIC_OUT
    2418              :          sets.  For values that's a simple intersection but for
    2419              :          expressions it is a union.  Given we want to have a single
    2420              :          expression per value in our sets we have to canonicalize.
    2421              :          Avoid randomness and running into cycles like for PR82129 and
    2422              :          canonicalize the expression we choose to the one with the
    2423              :          lowest id.  This requires we actually compute the union first.  */
    2424     22513190 :       FOR_EACH_VEC_ELT (worklist, i, e)
    2425              :         {
    2426      5205812 :           if (!gimple_seq_empty_p (phi_nodes (e->dest)))
    2427              :             {
    2428         2246 :               bitmap_set_t tmp = bitmap_set_new ();
    2429         2246 :               phi_translate_set (tmp, ANTIC_IN (e->dest), e);
    2430         2246 :               bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
    2431         2246 :               bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
    2432         2246 :               bitmap_set_free (tmp);
    2433              :             }
    2434              :           else
    2435              :             {
    2436      5203566 :               bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
    2437      5203566 :               bitmap_ior_into (&ANTIC_OUT->expressions,
    2438      5203566 :                                &ANTIC_IN (e->dest)->expressions);
    2439              :             }
    2440              :         }
    2441     11538252 :       if (! worklist.is_empty ())
    2442              :         {
    2443              :           /* Prune expressions not in the value set.  */
    2444      5096750 :           bitmap_iterator bi;
    2445      5096750 :           unsigned int i;
    2446      5096750 :           unsigned int to_clear = -1U;
    2447     37073598 :           FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
    2448              :             {
    2449     31976848 :               if (to_clear != -1U)
    2450              :                 {
    2451     16829971 :                   bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
    2452     16829971 :                   to_clear = -1U;
    2453              :                 }
    2454     31976848 :               pre_expr expr = expression_for_id (i);
    2455     31976848 :               unsigned int value_id = get_expr_value_id (expr);
    2456     31976848 :               if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
    2457     20597877 :                 to_clear = i;
    2458              :             }
    2459      5096750 :           if (to_clear != -1U)
    2460      3767906 :             bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
    2461              :         }
    2462      5769126 :     }
    2463              : 
    2464              :   /* Dump ANTIC_OUT before it's pruned.  */
    2465     15957775 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2466          151 :     print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
    2467              : 
    2468              :   /* Prune expressions that are clobbered in block and thus become
    2469              :      invalid if translated from ANTIC_OUT to ANTIC_IN.  */
    2470     15957775 :   prune_clobbered_mems (ANTIC_OUT, block, any_max_on_edge);
    2471              : 
    2472              :   /* Generate ANTIC_OUT - TMP_GEN.  Note when there's a MAX solution
    2473              :      on one edge do not prune values as we need to consider the resulting
    2474              :      expression set MAX as well.  This avoids a later growing ANTIC_IN
    2475              :      value-set during iteration, when the explicitly represented
    2476              :      expression set grows.  */
    2477     15957775 :   S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block),
    2478              :                                        any_max_on_edge);
    2479              : 
    2480              :   /* Start ANTIC_IN with EXP_GEN - TMP_GEN.  */
    2481     31915550 :   ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
    2482     15957775 :                                                       TMP_GEN (block));
    2483              : 
    2484              :   /* Then union in the ANTIC_OUT - TMP_GEN values,
    2485              :      to get ANTIC_OUT U EXP_GEN - TMP_GEN */
    2486     15957775 :   bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
    2487     15957775 :   bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
    2488              : 
    2489              :   /* clean (ANTIC_IN (block)) is deferred to after the iteration converged
    2490              :      because it can cause non-convergence, see for example PR81181.  */
    2491              : 
    2492     15957775 :   if (was_visited
    2493     15957775 :       && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
    2494              :     {
    2495         1886 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2496            0 :         fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
    2497              :                  "shrinks the set\n");
    2498              :       /* Prune expressions not in the value set.  */
    2499         1886 :       bitmap_iterator bi;
    2500         1886 :       unsigned int i;
    2501         1886 :       unsigned int to_clear = -1U;
    2502        19954 :       FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
    2503              :         {
    2504        18068 :           if (to_clear != -1U)
    2505              :             {
    2506         1539 :               bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
    2507         1539 :               to_clear = -1U;
    2508              :             }
    2509        18068 :           pre_expr expr = expression_for_id (i);
    2510        18068 :           unsigned int value_id = get_expr_value_id (expr);
    2511        18068 :           if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
    2512         2961 :             to_clear = i;
    2513              :         }
    2514         1886 :       if (to_clear != -1U)
    2515         1422 :         bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
    2516              :     }
    2517              : 
    2518     15957775 :   if (!bitmap_set_equal (old, ANTIC_IN (block)))
    2519     10375511 :     changed = true;
    2520              : 
    2521      5582264 :  maybe_dump_sets:
    2522     15962485 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2523              :     {
    2524          151 :       if (changed)
    2525          129 :         fprintf (dump_file, "[changed] ");
    2526          151 :       print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
    2527              :                         block->index);
    2528              : 
    2529          151 :       if (S)
    2530          151 :         print_bitmap_set (dump_file, S, "S", block->index);
    2531              :     }
    2532     15962485 :   if (old)
    2533     15957775 :     bitmap_set_free (old);
    2534     15962485 :   if (S)
    2535     15957775 :     bitmap_set_free (S);
    2536     15962485 :   if (ANTIC_OUT)
    2537     15957775 :     bitmap_set_free (ANTIC_OUT);
    2538     15962485 :   return changed;
    2539              : }
    2540              : 
    2541              : /* Compute PARTIAL_ANTIC for BLOCK.
    2542              : 
    2543              :    If succs(BLOCK) > 1 then
    2544              :      PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
    2545              :      in ANTIC_OUT for all succ(BLOCK)
    2546              :    else if succs(BLOCK) == 1 then
    2547              :      PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
    2548              : 
    2549              :    PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
    2550              : 
    2551              : */
    2552              : static void
    2553      1281737 : compute_partial_antic_aux (basic_block block,
    2554              :                            bool block_has_abnormal_pred_edge)
    2555              : {
    2556      1281737 :   bitmap_set_t old_PA_IN;
    2557      1281737 :   bitmap_set_t PA_OUT;
    2558      1281737 :   edge e;
    2559      1281737 :   edge_iterator ei;
    2560      1281737 :   unsigned long max_pa = param_max_partial_antic_length;
    2561              : 
    2562      1281737 :   old_PA_IN = PA_OUT = NULL;
    2563              : 
    2564              :   /* If any edges from predecessors are abnormal, antic_in is empty,
    2565              :      so do nothing.  */
    2566      1281737 :   if (block_has_abnormal_pred_edge)
    2567          854 :     goto maybe_dump_sets;
    2568              : 
    2569              :   /* If there are too many partially anticipatable values in the
    2570              :      block, phi_translate_set can take an exponential time: stop
    2571              :      before the translation starts.  */
    2572      1280883 :   if (max_pa
    2573      1186185 :       && single_succ_p (block)
    2574      2083439 :       && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
    2575          567 :     goto maybe_dump_sets;
    2576              : 
    2577      1280316 :   old_PA_IN = PA_IN (block);
    2578      1280316 :   PA_OUT = bitmap_set_new ();
    2579              : 
    2580              :   /* If the block has no successors, ANTIC_OUT is empty.  */
    2581      1280316 :   if (EDGE_COUNT (block->succs) == 0)
    2582              :     ;
    2583              :   /* If we have one successor, we could have some phi nodes to
    2584              :      translate through.  Note that we can't phi translate across DFS
    2585              :      back edges in partial antic, because it uses a union operation on
    2586              :      the successors.  For recurrences like IV's, we will end up
    2587              :      generating a new value in the set on each go around (i + 3 (VH.1)
    2588              :      VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever.  */
    2589      1185620 :   else if (single_succ_p (block))
    2590              :     {
    2591       801991 :       e = single_succ_edge (block);
    2592       801991 :       if (!(e->flags & EDGE_DFS_BACK))
    2593       719112 :         phi_translate_set (PA_OUT, PA_IN (e->dest), e);
    2594              :     }
    2595              :   /* If we have multiple successors, we take the union of all of
    2596              :      them.  */
    2597              :   else
    2598              :     {
    2599       383629 :       size_t i;
    2600              : 
    2601       383629 :       auto_vec<edge> worklist (EDGE_COUNT (block->succs));
    2602      1156613 :       FOR_EACH_EDGE (e, ei, block->succs)
    2603              :         {
    2604       772984 :           if (e->flags & EDGE_DFS_BACK)
    2605          311 :             continue;
    2606       772673 :           worklist.quick_push (e);
    2607              :         }
    2608       383629 :       if (worklist.length () > 0)
    2609              :         {
    2610      1156302 :           FOR_EACH_VEC_ELT (worklist, i, e)
    2611              :             {
    2612       772673 :               unsigned int i;
    2613       772673 :               bitmap_iterator bi;
    2614              : 
    2615       772673 :               if (!gimple_seq_empty_p (phi_nodes (e->dest)))
    2616              :                 {
    2617          763 :                   bitmap_set_t antic_in = bitmap_set_new ();
    2618          763 :                   phi_translate_set (antic_in, ANTIC_IN (e->dest), e);
    2619         1543 :                   FOR_EACH_EXPR_ID_IN_SET (antic_in, i, bi)
    2620          780 :                     bitmap_value_insert_into_set (PA_OUT,
    2621              :                                                   expression_for_id (i));
    2622          763 :                   bitmap_set_free (antic_in);
    2623          763 :                   bitmap_set_t pa_in = bitmap_set_new ();
    2624          763 :                   phi_translate_set (pa_in, PA_IN (e->dest), e);
    2625          763 :                   FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
    2626            0 :                     bitmap_value_insert_into_set (PA_OUT,
    2627              :                                                   expression_for_id (i));
    2628          763 :                   bitmap_set_free (pa_in);
    2629              :                 }
    2630              :               else
    2631              :                 {
    2632      5022779 :                   FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
    2633      4250869 :                     bitmap_value_insert_into_set (PA_OUT,
    2634              :                                                   expression_for_id (i));
    2635      4941885 :                   FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
    2636      4169975 :                     bitmap_value_insert_into_set (PA_OUT,
    2637              :                                                   expression_for_id (i));
    2638              :                 }
    2639              :             }
    2640              :         }
    2641       383629 :     }
    2642              : 
    2643              :   /* Prune expressions that are clobbered in block and thus become
    2644              :      invalid if translated from PA_OUT to PA_IN.  */
    2645      1280316 :   prune_clobbered_mems (PA_OUT, block, false);
    2646              : 
    2647              :   /* PA_IN starts with PA_OUT - TMP_GEN.
    2648              :      Then we subtract things from ANTIC_IN.  */
    2649      1280316 :   PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
    2650              : 
    2651              :   /* For partial antic, we want to put back in the phi results, since
    2652              :      we will properly avoid making them partially antic over backedges.  */
    2653      1280316 :   bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
    2654      1280316 :   bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
    2655              : 
    2656              :   /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
    2657      1280316 :   bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
    2658              : 
    2659      1280316 :   clean (PA_IN (block), ANTIC_IN (block));
    2660              : 
    2661      1281737 :  maybe_dump_sets:
    2662      1281737 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2663              :     {
    2664            0 :       if (PA_OUT)
    2665            0 :         print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
    2666              : 
    2667            0 :       print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
    2668              :     }
    2669      1281737 :   if (old_PA_IN)
    2670      1280316 :     bitmap_set_free (old_PA_IN);
    2671      1281737 :   if (PA_OUT)
    2672      1280316 :     bitmap_set_free (PA_OUT);
    2673      1281737 : }
    2674              : 
    2675              : /* Compute ANTIC and partial ANTIC sets.  */
    2676              : 
    2677              : static void
    2678       987726 : compute_antic (void)
    2679              : {
    2680       987726 :   bool changed = true;
    2681       987726 :   int num_iterations = 0;
    2682       987726 :   basic_block block;
    2683       987726 :   int i;
    2684       987726 :   edge_iterator ei;
    2685       987726 :   edge e;
    2686              : 
    2687              :   /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
    2688              :      We pre-build the map of blocks with incoming abnormal edges here.  */
    2689       987726 :   auto_sbitmap has_abnormal_preds (last_basic_block_for_fn (cfun));
    2690       987726 :   bitmap_clear (has_abnormal_preds);
    2691              : 
    2692     16454524 :   FOR_ALL_BB_FN (block, cfun)
    2693              :     {
    2694     15466798 :       BB_VISITED (block) = 0;
    2695              : 
    2696     34883872 :       FOR_EACH_EDGE (e, ei, block->preds)
    2697     19420725 :         if (e->flags & EDGE_ABNORMAL)
    2698              :           {
    2699         3651 :             bitmap_set_bit (has_abnormal_preds, block->index);
    2700         3651 :             break;
    2701              :           }
    2702              : 
    2703              :       /* While we are here, give empty ANTIC_IN sets to each block.  */
    2704     15466798 :       ANTIC_IN (block) = bitmap_set_new ();
    2705     15466798 :       if (do_partial_partial)
    2706      1281737 :         PA_IN (block) = bitmap_set_new ();
    2707              :     }
    2708              : 
    2709              :   /* At the exit block we anticipate nothing.  */
    2710       987726 :   BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
    2711              : 
    2712              :   /* For ANTIC computation we need a postorder that also guarantees that
    2713              :      a block with a single successor is visited after its successor.
    2714              :      RPO on the inverted CFG has this property.  */
    2715       987726 :   int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
    2716       987726 :   int n = inverted_rev_post_order_compute (cfun, rpo);
    2717              : 
    2718       987726 :   auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
    2719       987726 :   bitmap_clear (worklist);
    2720      2854215 :   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
    2721      1866489 :     bitmap_set_bit (worklist, e->src->index);
    2722      3056903 :   while (changed)
    2723              :     {
    2724      2069177 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2725           35 :         fprintf (dump_file, "Starting iteration %d\n", num_iterations);
    2726              :       /* ???  We need to clear our PHI translation cache here as the
    2727              :          ANTIC sets shrink and we restrict valid translations to
    2728              :          those having operands with leaders in ANTIC.  Same below
    2729              :          for PA ANTIC computation.  */
    2730      2069177 :       num_iterations++;
    2731      2069177 :       changed = false;
    2732     39099171 :       for (i = 0; i < n; ++i)
    2733              :         {
    2734     37029994 :           if (bitmap_bit_p (worklist, rpo[i]))
    2735              :             {
    2736     15962485 :               basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
    2737     15962485 :               bitmap_clear_bit (worklist, block->index);
    2738     15962485 :               if (compute_antic_aux (block,
    2739     15962485 :                                      bitmap_bit_p (has_abnormal_preds,
    2740              :                                                    block->index)))
    2741              :                 {
    2742     33588855 :                   FOR_EACH_EDGE (e, ei, block->preds)
    2743     18483582 :                     bitmap_set_bit (worklist, e->src->index);
    2744              :                   changed = true;
    2745              :                 }
    2746              :             }
    2747              :         }
    2748              :       /* Theoretically possible, but *highly* unlikely.  */
    2749      2069177 :       gcc_checking_assert (num_iterations < 500);
    2750              :     }
    2751              : 
    2752              :   /* We have to clean after the dataflow problem converged as cleaning
    2753              :      can cause non-convergence because it is based on expressions
    2754              :      rather than values.  */
    2755     14479072 :   FOR_EACH_BB_FN (block, cfun)
    2756     13491346 :     clean (ANTIC_IN (block));
    2757              : 
    2758       987726 :   statistics_histogram_event (cfun, "compute_antic iterations",
    2759              :                               num_iterations);
    2760              : 
    2761       987726 :   if (do_partial_partial)
    2762              :     {
    2763              :       /* For partial antic we ignore backedges and thus we do not need
    2764              :          to perform any iteration when we process blocks in rpo.  */
    2765      1376433 :       for (i = 0; i < n; ++i)
    2766              :         {
    2767      1281737 :           basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
    2768      1281737 :           compute_partial_antic_aux (block,
    2769      1281737 :                                      bitmap_bit_p (has_abnormal_preds,
    2770              :                                                    block->index));
    2771              :         }
    2772              :     }
    2773              : 
    2774       987726 :   free (rpo);
    2775       987726 : }
    2776              : 
    2777              : 
    2778              : /* Inserted expressions are placed onto this worklist, which is used
    2779              :    for performing quick dead code elimination of insertions we made
    2780              :    that didn't turn out to be necessary.   */
    2781              : static bitmap inserted_exprs;
    2782              : 
    2783              : /* The actual worker for create_component_ref_by_pieces.  */
    2784              : 
    2785              : static tree
    2786      1131236 : create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
    2787              :                                   unsigned int *operand, gimple_seq *stmts)
    2788              : {
    2789      1131236 :   vn_reference_op_t currop = &ref->operands[*operand];
    2790      1131236 :   tree genop;
    2791      1131236 :   ++*operand;
    2792      1131236 :   switch (currop->opcode)
    2793              :     {
    2794            0 :     case CALL_EXPR:
    2795            0 :       gcc_unreachable ();
    2796              : 
    2797       392114 :     case MEM_REF:
    2798       392114 :       {
    2799       392114 :         tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
    2800              :                                                         stmts);
    2801       392114 :         if (!baseop)
    2802              :           return NULL_TREE;
    2803       392106 :         tree offset = currop->op0;
    2804       392106 :         if (TREE_CODE (baseop) == ADDR_EXPR
    2805       392106 :             && handled_component_p (TREE_OPERAND (baseop, 0)))
    2806              :           {
    2807           96 :             poly_int64 off;
    2808           96 :             tree base;
    2809           96 :             base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
    2810              :                                                   &off);
    2811           96 :             gcc_assert (base);
    2812           96 :             offset = int_const_binop (PLUS_EXPR, offset,
    2813           96 :                                       build_int_cst (TREE_TYPE (offset),
    2814              :                                                      off));
    2815           96 :             baseop = build_fold_addr_expr (base);
    2816              :           }
    2817       392106 :         genop = build2 (MEM_REF, currop->type, baseop, offset);
    2818       392106 :         MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
    2819       392106 :         MR_DEPENDENCE_BASE (genop) = currop->base;
    2820       392106 :         REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
    2821       392106 :         return genop;
    2822              :       }
    2823              : 
    2824            0 :     case TARGET_MEM_REF:
    2825            0 :       {
    2826            0 :         tree genop0 = NULL_TREE, genop1 = NULL_TREE;
    2827            0 :         vn_reference_op_t nextop = &ref->operands[(*operand)++];
    2828            0 :         tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
    2829              :                                                         stmts);
    2830            0 :         if (!baseop)
    2831              :           return NULL_TREE;
    2832            0 :         if (currop->op0)
    2833              :           {
    2834            0 :             genop0 = find_or_generate_expression (block, currop->op0, stmts);
    2835            0 :             if (!genop0)
    2836              :               return NULL_TREE;
    2837              :           }
    2838            0 :         if (nextop->op0)
    2839              :           {
    2840            0 :             genop1 = find_or_generate_expression (block, nextop->op0, stmts);
    2841            0 :             if (!genop1)
    2842              :               return NULL_TREE;
    2843              :           }
    2844            0 :         genop = build5 (TARGET_MEM_REF, currop->type,
    2845              :                         baseop, currop->op2, genop0, currop->op1, genop1);
    2846              : 
    2847            0 :         MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
    2848            0 :         MR_DEPENDENCE_BASE (genop) = currop->base;
    2849            0 :         return genop;
    2850              :       }
    2851              : 
    2852       242246 :     case ADDR_EXPR:
    2853       242246 :       if (currop->op0)
    2854              :         {
    2855       239925 :           gcc_assert (is_gimple_min_invariant (currop->op0));
    2856       239925 :           return currop->op0;
    2857              :         }
    2858              :       /* Fallthrough.  */
    2859         6856 :     case REALPART_EXPR:
    2860         6856 :     case IMAGPART_EXPR:
    2861         6856 :     case VIEW_CONVERT_EXPR:
    2862         6856 :       {
    2863         6856 :         tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
    2864              :                                                         stmts);
    2865         6856 :         if (!genop0)
    2866              :           return NULL_TREE;
    2867         6852 :         return build1 (currop->opcode, currop->type, genop0);
    2868              :       }
    2869              : 
    2870            4 :     case WITH_SIZE_EXPR:
    2871            4 :       {
    2872            4 :         tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
    2873              :                                                         stmts);
    2874            4 :         if (!genop0)
    2875              :           return NULL_TREE;
    2876            4 :         tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
    2877            4 :         if (!genop1)
    2878              :           return NULL_TREE;
    2879            4 :         return build2 (currop->opcode, currop->type, genop0, genop1);
    2880              :       }
    2881              : 
    2882         2874 :     case BIT_FIELD_REF:
    2883         2874 :       {
    2884         2874 :         tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
    2885              :                                                         stmts);
    2886         2874 :         if (!genop0)
    2887              :           return NULL_TREE;
    2888         2874 :         tree op1 = currop->op0;
    2889         2874 :         tree op2 = currop->op1;
    2890         2874 :         tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
    2891         2874 :         REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
    2892         2874 :         return t;
    2893              :       }
    2894              : 
    2895              :       /* For array ref vn_reference_op's, operand 1 of the array ref
    2896              :          is op0 of the reference op and operand 3 of the array ref is
    2897              :          op1.  */
    2898        62801 :     case ARRAY_RANGE_REF:
    2899        62801 :     case ARRAY_REF:
    2900        62801 :       {
    2901        62801 :         tree genop0;
    2902        62801 :         tree genop1 = currop->op0;
    2903        62801 :         tree genop2 = currop->op1;
    2904        62801 :         tree genop3 = currop->op2;
    2905        62801 :         genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
    2906              :                                                    stmts);
    2907        62801 :         if (!genop0)
    2908              :           return NULL_TREE;
    2909        62801 :         genop1 = find_or_generate_expression (block, genop1, stmts);
    2910        62801 :         if (!genop1)
    2911              :           return NULL_TREE;
    2912        62801 :         if (genop2)
    2913              :           {
    2914        62801 :             tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
    2915              :             /* Drop zero minimum index if redundant.  */
    2916        62801 :             if (integer_zerop (genop2)
    2917        62801 :                 && (!domain_type
    2918        61715 :                     || integer_zerop (TYPE_MIN_VALUE (domain_type))))
    2919              :               genop2 = NULL_TREE;
    2920              :             else
    2921              :               {
    2922          659 :                 genop2 = find_or_generate_expression (block, genop2, stmts);
    2923          659 :                 if (!genop2)
    2924              :                   return NULL_TREE;
    2925              :               }
    2926              :           }
    2927        62801 :         if (genop3)
    2928              :           {
    2929        62801 :             tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
    2930              :             /* We can't always put a size in units of the element alignment
    2931              :                here as the element alignment may be not visible.  See
    2932              :                PR43783.  Simply drop the element size for constant
    2933              :                sizes.  */
    2934        62801 :             if ((TREE_CODE (genop3) == INTEGER_CST
    2935        62797 :                 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
    2936        62797 :                 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
    2937        62797 :                              (wi::to_offset (genop3) * vn_ref_op_align_unit (currop))))
    2938        62801 :               || (TREE_CODE (genop3) == EXACT_DIV_EXPR
    2939            0 :                 && TREE_CODE (TREE_OPERAND (genop3, 1)) == INTEGER_CST
    2940            0 :                 && operand_equal_p (TREE_OPERAND (genop3, 0), TYPE_SIZE_UNIT (elmt_type))
    2941            0 :                 && wi::eq_p (wi::to_offset (TREE_OPERAND (genop3, 1)),
    2942        62797 :                              vn_ref_op_align_unit (currop))))
    2943              :               genop3 = NULL_TREE;
    2944              :             else
    2945              :               {
    2946            4 :                 genop3 = find_or_generate_expression (block, genop3, stmts);
    2947            4 :                 if (!genop3)
    2948              :                   return NULL_TREE;
    2949              :               }
    2950              :           }
    2951        62801 :         return build4 (currop->opcode, currop->type, genop0, genop1,
    2952        62801 :                        genop2, genop3);
    2953              :       }
    2954       269995 :     case COMPONENT_REF:
    2955       269995 :       {
    2956       269995 :         tree op0;
    2957       269995 :         tree op1;
    2958       269995 :         tree genop2 = currop->op1;
    2959       269995 :         op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
    2960       269995 :         if (!op0)
    2961              :           return NULL_TREE;
    2962              :         /* op1 should be a FIELD_DECL, which are represented by themselves.  */
    2963       269983 :         op1 = currop->op0;
    2964       269983 :         if (genop2)
    2965              :           {
    2966            0 :             genop2 = find_or_generate_expression (block, genop2, stmts);
    2967            0 :             if (!genop2)
    2968              :               return NULL_TREE;
    2969              :           }
    2970       269983 :         return build3 (COMPONENT_REF, currop->type, op0, op1, genop2);
    2971              :       }
    2972              : 
    2973       154701 :     case SSA_NAME:
    2974       154701 :       {
    2975       154701 :         genop = find_or_generate_expression (block, currop->op0, stmts);
    2976       154701 :         return genop;
    2977              :       }
    2978         1966 :     case STRING_CST:
    2979         1966 :     case INTEGER_CST:
    2980         1966 :     case POLY_INT_CST:
    2981         1966 :     case COMPLEX_CST:
    2982         1966 :     case VECTOR_CST:
    2983         1966 :     case REAL_CST:
    2984         1966 :     case CONSTRUCTOR:
    2985         1966 :     case VAR_DECL:
    2986         1966 :     case PARM_DECL:
    2987         1966 :     case CONST_DECL:
    2988         1966 :     case RESULT_DECL:
    2989         1966 :     case FUNCTION_DECL:
    2990         1966 :       return currop->op0;
    2991              : 
    2992            0 :     default:
    2993            0 :       gcc_unreachable ();
    2994              :     }
    2995              : }
    2996              : 
    2997              : /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
    2998              :    COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
    2999              :    trying to rename aggregates into ssa form directly, which is a no no.
    3000              : 
    3001              :    Thus, this routine doesn't create temporaries, it just builds a
    3002              :    single access expression for the array, calling
    3003              :    find_or_generate_expression to build the innermost pieces.
    3004              : 
    3005              :    This function is a subroutine of create_expression_by_pieces, and
    3006              :    should not be called on it's own unless you really know what you
    3007              :    are doing.  */
    3008              : 
    3009              : static tree
    3010       392141 : create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
    3011              :                                 gimple_seq *stmts)
    3012              : {
    3013       392141 :   unsigned int op = 0;
    3014       392141 :   return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
    3015              : }
    3016              : 
    3017              : /* Find a simple leader for an expression, or generate one using
    3018              :    create_expression_by_pieces from a NARY expression for the value.
    3019              :    BLOCK is the basic_block we are looking for leaders in.
    3020              :    OP is the tree expression to find a leader for or generate.
    3021              :    Returns the leader or NULL_TREE on failure.  */
    3022              : 
    3023              : static tree
    3024       881022 : find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
    3025              : {
    3026              :   /* Constants are always leaders.  */
    3027       881022 :   if (is_gimple_min_invariant (op))
    3028              :     return op;
    3029              : 
    3030       674867 :   gcc_assert (TREE_CODE (op) == SSA_NAME);
    3031       674867 :   vn_ssa_aux_t info = VN_INFO (op);
    3032       674867 :   unsigned int lookfor = info->value_id;
    3033       674867 :   if (value_id_constant_p (lookfor))
    3034            0 :     return info->valnum;
    3035              : 
    3036       674867 :   pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
    3037       674867 :   if (leader)
    3038              :     {
    3039       633837 :       if (leader->kind == NAME)
    3040              :         {
    3041       633837 :           tree name = PRE_EXPR_NAME (leader);
    3042       633837 :           if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
    3043              :             return NULL_TREE;
    3044       633809 :           return name;
    3045              :         }
    3046            0 :       else if (leader->kind == CONSTANT)
    3047            0 :         return PRE_EXPR_CONSTANT (leader);
    3048              : 
    3049              :       /* Defer.  */
    3050              :       return NULL_TREE;
    3051              :     }
    3052        41030 :   gcc_assert (!value_id_constant_p (lookfor));
    3053              : 
    3054              :   /* It must be a complex expression, so generate it recursively.  Note
    3055              :      that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre-28.c
    3056              :      where the insert algorithm fails to insert a required expression.  */
    3057        41030 :   bitmap exprset = value_expressions[lookfor];
    3058        41030 :   bitmap_iterator bi;
    3059        41030 :   unsigned int i;
    3060        41030 :   if (exprset)
    3061        65705 :     EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
    3062              :       {
    3063        60963 :         pre_expr temp = expression_for_id (i);
    3064              :         /* We cannot insert random REFERENCE expressions at arbitrary
    3065              :            places.  We can insert NARYs which eventually re-materializes
    3066              :            its operand values.  */
    3067        60963 :         if (temp->kind == NARY)
    3068              :           {
    3069        36288 :             static int depth;
    3070        36288 :             if (depth > 8)
    3071              :               return NULL_TREE;
    3072              : 
    3073        36274 :             depth++;
    3074        36274 :             tree res = create_expression_by_pieces (block, temp, stmts,
    3075        36274 :                                                     TREE_TYPE (op));
    3076        36274 :             depth--;
    3077        36274 :             return res;
    3078              :           }
    3079              :       }
    3080              : 
    3081              :   /* Defer.  */
    3082              :   return NULL_TREE;
    3083              : }
    3084              : 
    3085              : /* Create an expression in pieces, so that we can handle very complex
    3086              :    expressions that may be ANTIC, but not necessary GIMPLE.
    3087              :    BLOCK is the basic block the expression will be inserted into,
    3088              :    EXPR is the expression to insert (in value form)
    3089              :    STMTS is a statement list to append the necessary insertions into.
    3090              : 
    3091              :    This function will die if we hit some value that shouldn't be
    3092              :    ANTIC but is (IE there is no leader for it, or its components).
    3093              :    The function returns NULL_TREE in case a different antic expression
    3094              :    has to be inserted first.
    3095              :    This function may also generate expressions that are themselves
    3096              :    partially or fully redundant.  Those that are will be either made
    3097              :    fully redundant during the next iteration of insert (for partially
    3098              :    redundant ones), or eliminated by eliminate (for fully redundant
    3099              :    ones).  */
    3100              : 
    3101              : static tree
    3102      2975797 : create_expression_by_pieces (basic_block block, pre_expr expr,
    3103              :                              gimple_seq *stmts, tree type)
    3104              : {
    3105      2975797 :   tree name;
    3106      2975797 :   tree folded;
    3107      2975797 :   gimple_seq forced_stmts = NULL;
    3108      2975797 :   unsigned int value_id;
    3109      2975797 :   gimple_stmt_iterator gsi;
    3110      2975797 :   tree exprtype = type ? type : get_expr_type (expr);
    3111      2975797 :   pre_expr nameexpr;
    3112      2975797 :   gassign *newstmt;
    3113              : 
    3114      2975797 :   switch (expr->kind)
    3115              :     {
    3116              :     /* We may hit the NAME/CONSTANT case if we have to convert types
    3117              :        that value numbering saw through.  */
    3118       743310 :     case NAME:
    3119       743310 :       folded = PRE_EXPR_NAME (expr);
    3120       743310 :       if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
    3121              :         return NULL_TREE;
    3122       743305 :       if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
    3123              :         return folded;
    3124              :       break;
    3125      1446868 :     case CONSTANT:
    3126      1446868 :       {
    3127      1446868 :         folded = PRE_EXPR_CONSTANT (expr);
    3128      1446868 :         tree tem = fold_convert (exprtype, folded);
    3129      1446868 :         if (is_gimple_min_invariant (tem))
    3130              :           return tem;
    3131              :         break;
    3132              :       }
    3133       395250 :     case REFERENCE:
    3134       395250 :       if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
    3135              :         {
    3136         3109 :           vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
    3137         3109 :           unsigned int operand = 1;
    3138         3109 :           vn_reference_op_t currop = &ref->operands[0];
    3139         3109 :           tree sc = NULL_TREE;
    3140         3109 :           tree fn = NULL_TREE;
    3141         3109 :           if (currop->op0)
    3142              :             {
    3143         2967 :               fn = find_or_generate_expression (block, currop->op0, stmts);
    3144         2967 :               if (!fn)
    3145            6 :                 return NULL_TREE;
    3146              :             }
    3147         3109 :           if (currop->op1)
    3148              :             {
    3149            0 :               sc = find_or_generate_expression (block, currop->op1, stmts);
    3150            0 :               if (!sc)
    3151              :                 return NULL_TREE;
    3152              :             }
    3153         6218 :           auto_vec<tree> args (ref->operands.length () - 1);
    3154        10663 :           while (operand < ref->operands.length ())
    3155              :             {
    3156         4451 :               tree arg = create_component_ref_by_pieces_1 (block, ref,
    3157         4451 :                                                            &operand, stmts);
    3158         4451 :               if (!arg)
    3159            6 :                 return NULL_TREE;
    3160         4445 :               args.quick_push (arg);
    3161              :             }
    3162         3103 :           gcall *call;
    3163         3103 :           if (currop->op0)
    3164              :             {
    3165         2961 :               call = gimple_build_call_vec (fn, args);
    3166         2961 :               gimple_call_set_fntype (call, currop->type);
    3167              :             }
    3168              :           else
    3169          142 :             call = gimple_build_call_internal_vec ((internal_fn)currop->clique,
    3170              :                                                    args);
    3171         3103 :           gimple_set_location (call, expr->loc);
    3172         3103 :           if (sc)
    3173            0 :             gimple_call_set_chain (call, sc);
    3174         3103 :           tree forcedname = make_ssa_name (ref->type);
    3175         3103 :           gimple_call_set_lhs (call, forcedname);
    3176              :           /* There's no CCP pass after PRE which would re-compute alignment
    3177              :              information so make sure we re-materialize this here.  */
    3178         3103 :           if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
    3179            0 :               && args.length () - 2 <= 1
    3180            0 :               && tree_fits_uhwi_p (args[1])
    3181         3103 :               && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
    3182              :             {
    3183            0 :               unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
    3184            0 :               unsigned HOST_WIDE_INT hmisalign
    3185            0 :                 = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
    3186            0 :               if ((halign & (halign - 1)) == 0
    3187            0 :                   && (hmisalign & ~(halign - 1)) == 0
    3188            0 :                   && (unsigned int)halign != 0)
    3189            0 :                 set_ssa_name_alignment (forcedname, halign, hmisalign);
    3190              :             }
    3191         3103 :           gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
    3192         3103 :           gimple_seq_add_stmt_without_update (&forced_stmts, call);
    3193         3103 :           folded = forcedname;
    3194         3109 :         }
    3195              :       else
    3196              :         {
    3197       392141 :           folded = create_component_ref_by_pieces (block,
    3198              :                                                    PRE_EXPR_REFERENCE (expr),
    3199              :                                                    stmts);
    3200       392141 :           if (!folded)
    3201              :             return NULL_TREE;
    3202       392133 :           name = make_temp_ssa_name (exprtype, NULL, "pretmp");
    3203       392133 :           newstmt = gimple_build_assign (name, folded);
    3204       392133 :           gimple_set_location (newstmt, expr->loc);
    3205       392133 :           gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
    3206       392133 :           gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
    3207       392133 :           folded = name;
    3208              :         }
    3209              :       break;
    3210       390369 :     case NARY:
    3211       390369 :       {
    3212       390369 :         vn_nary_op_t nary = PRE_EXPR_NARY (expr);
    3213       390369 :         tree *genop = XALLOCAVEC (tree, nary->length);
    3214       390369 :         unsigned i;
    3215      1036642 :         for (i = 0; i < nary->length; ++i)
    3216              :           {
    3217       659886 :             genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
    3218       659886 :             if (!genop[i])
    3219              :               return NULL_TREE;
    3220              :             /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR.  It
    3221              :                may have conversions stripped.  */
    3222       646273 :             if (nary->opcode == POINTER_PLUS_EXPR)
    3223              :               {
    3224       107243 :                 if (i == 0)
    3225        53659 :                   genop[i] = gimple_convert (&forced_stmts,
    3226              :                                              nary->type, genop[i]);
    3227        53584 :                 else if (i == 1)
    3228        53584 :                   genop[i] = gimple_convert (&forced_stmts,
    3229              :                                              sizetype, genop[i]);
    3230              :               }
    3231              :             else
    3232       539030 :               genop[i] = gimple_convert (&forced_stmts,
    3233       539030 :                                          TREE_TYPE (nary->op[i]), genop[i]);
    3234              :           }
    3235       376756 :         if (nary->opcode == CONSTRUCTOR)
    3236              :           {
    3237            8 :             vec<constructor_elt, va_gc> *elts = NULL;
    3238           40 :             for (i = 0; i < nary->length; ++i)
    3239           32 :               CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
    3240            8 :             folded = build_constructor (nary->type, elts);
    3241            8 :             name = make_temp_ssa_name (exprtype, NULL, "pretmp");
    3242            8 :             newstmt = gimple_build_assign (name, folded);
    3243            8 :             gimple_set_location (newstmt, expr->loc);
    3244            8 :             gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
    3245            8 :             folded = name;
    3246              :           }
    3247              :         else
    3248              :           {
    3249       376748 :             switch (nary->length)
    3250              :               {
    3251       110471 :               case 1:
    3252       110471 :                 folded = gimple_build (&forced_stmts, expr->loc,
    3253              :                                        nary->opcode, nary->type, genop[0]);
    3254       110471 :                 break;
    3255       266060 :               case 2:
    3256       266060 :                 folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
    3257              :                                        nary->type, genop[0], genop[1]);
    3258       266060 :                 break;
    3259          217 :               case 3:
    3260          217 :                 folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
    3261              :                                        nary->type, genop[0], genop[1],
    3262              :                                        genop[2]);
    3263          217 :                 break;
    3264            0 :               default:
    3265            0 :                 gcc_unreachable ();
    3266              :               }
    3267              :           }
    3268              :       }
    3269              :       break;
    3270            0 :     default:
    3271            0 :       gcc_unreachable ();
    3272              :     }
    3273              : 
    3274       866945 :   folded = gimple_convert (&forced_stmts, exprtype, folded);
    3275              : 
    3276              :   /* If there is nothing to insert, return the simplified result.  */
    3277       866945 :   if (gimple_seq_empty_p (forced_stmts))
    3278              :     return folded;
    3279              :   /* If we simplified to a constant return it and discard eventually
    3280              :      built stmts.  */
    3281       771905 :   if (is_gimple_min_invariant (folded))
    3282              :     {
    3283            0 :       gimple_seq_discard (forced_stmts);
    3284            0 :       return folded;
    3285              :     }
    3286              :   /* Likewise if we simplified to sth not queued for insertion.  */
    3287       771905 :   bool found = false;
    3288       771905 :   gsi = gsi_last (forced_stmts);
    3289       771905 :   for (; !gsi_end_p (gsi); gsi_prev (&gsi))
    3290              :     {
    3291       771905 :       gimple *stmt = gsi_stmt (gsi);
    3292       771905 :       tree forcedname = gimple_get_lhs (stmt);
    3293       771905 :       if (forcedname == folded)
    3294              :         {
    3295              :           found = true;
    3296              :           break;
    3297              :         }
    3298              :     }
    3299       771905 :   if (! found)
    3300              :     {
    3301            0 :       gimple_seq_discard (forced_stmts);
    3302            0 :       return folded;
    3303              :     }
    3304       771905 :   gcc_assert (TREE_CODE (folded) == SSA_NAME);
    3305              : 
    3306              :   /* If we have any intermediate expressions to the value sets, add them
    3307              :      to the value sets and chain them in the instruction stream.  */
    3308       771905 :   if (forced_stmts)
    3309              :     {
    3310       771905 :       gsi = gsi_start (forced_stmts);
    3311      1544306 :       for (; !gsi_end_p (gsi); gsi_next (&gsi))
    3312              :         {
    3313       772401 :           gimple *stmt = gsi_stmt (gsi);
    3314       772401 :           tree forcedname = gimple_get_lhs (stmt);
    3315       772401 :           pre_expr nameexpr;
    3316              : 
    3317       772401 :           if (forcedname != folded)
    3318              :             {
    3319          496 :               vn_ssa_aux_t vn_info = VN_INFO (forcedname);
    3320          496 :               vn_info->valnum = forcedname;
    3321          496 :               vn_info->value_id = get_next_value_id ();
    3322          496 :               nameexpr = get_or_alloc_expr_for_name (forcedname);
    3323          496 :               add_to_value (vn_info->value_id, nameexpr);
    3324          496 :               if (NEW_SETS (block))
    3325          496 :                 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
    3326          496 :               bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
    3327              :             }
    3328              : 
    3329       772401 :           bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
    3330              :         }
    3331       771905 :       gimple_seq_add_seq (stmts, forced_stmts);
    3332              :     }
    3333              : 
    3334       771905 :   name = folded;
    3335              : 
    3336              :   /* Fold the last statement.  */
    3337       771905 :   gsi = gsi_last (*stmts);
    3338       771905 :   if (fold_stmt_inplace (&gsi))
    3339       208065 :     update_stmt (gsi_stmt (gsi));
    3340              : 
    3341              :   /* Add a value number to the temporary.
    3342              :      The value may already exist in either NEW_SETS, or AVAIL_OUT, because
    3343              :      we are creating the expression by pieces, and this particular piece of
    3344              :      the expression may have been represented.  There is no harm in replacing
    3345              :      here.  */
    3346       771905 :   value_id = get_expr_value_id (expr);
    3347       771905 :   vn_ssa_aux_t vn_info = VN_INFO (name);
    3348       771905 :   vn_info->value_id = value_id;
    3349       771905 :   vn_info->valnum = vn_valnum_from_value_id (value_id);
    3350       771905 :   if (vn_info->valnum == NULL_TREE)
    3351       243068 :     vn_info->valnum = name;
    3352       771905 :   gcc_assert (vn_info->valnum != NULL_TREE);
    3353       771905 :   nameexpr = get_or_alloc_expr_for_name (name);
    3354       771905 :   add_to_value (value_id, nameexpr);
    3355       771905 :   if (NEW_SETS (block))
    3356       550904 :     bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
    3357       771905 :   bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
    3358              : 
    3359       771905 :   pre_stats.insertions++;
    3360       771905 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3361              :     {
    3362           18 :       fprintf (dump_file, "Inserted ");
    3363           36 :       print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
    3364           18 :       fprintf (dump_file, " in predecessor %d (%04d)\n",
    3365              :                block->index, value_id);
    3366              :     }
    3367              : 
    3368              :   return name;
    3369              : }
    3370              : 
    3371              : 
    3372              : /* Insert the to-be-made-available values of expression EXPRNUM for each
    3373              :    predecessor, stored in AVAIL, into the predecessors of BLOCK, and
    3374              :    merge the result with a phi node, given the same value number as
    3375              :    NODE.  Return true if we have inserted new stuff.  */
    3376              : 
    3377              : static bool
    3378      2007032 : insert_into_preds_of_block (basic_block block, unsigned int exprnum,
    3379              :                             vec<pre_expr> &avail)
    3380              : {
    3381      2007032 :   pre_expr expr = expression_for_id (exprnum);
    3382      2007032 :   pre_expr newphi;
    3383      2007032 :   unsigned int val = get_expr_value_id (expr);
    3384      2007032 :   edge pred;
    3385      2007032 :   bool insertions = false;
    3386      2007032 :   bool nophi = false;
    3387      2007032 :   basic_block bprime;
    3388      2007032 :   pre_expr eprime;
    3389      2007032 :   edge_iterator ei;
    3390      2007032 :   tree type = get_expr_type (expr);
    3391      2007032 :   tree temp;
    3392      2007032 :   gphi *phi;
    3393              : 
    3394              :   /* Make sure we aren't creating an induction variable.  */
    3395      2007032 :   if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
    3396              :     {
    3397      1670665 :       bool firstinsideloop = false;
    3398      1670665 :       bool secondinsideloop = false;
    3399      5011995 :       firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
    3400      1670665 :                                                EDGE_PRED (block, 0)->src);
    3401      5011995 :       secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
    3402      1670665 :                                                 EDGE_PRED (block, 1)->src);
    3403              :       /* Induction variables only have one edge inside the loop.  */
    3404      1670665 :       if ((firstinsideloop ^ secondinsideloop)
    3405      1592907 :           && expr->kind != REFERENCE)
    3406              :         {
    3407      1509860 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3408           56 :             fprintf (dump_file, "Skipping insertion of phi for partial "
    3409              :                      "redundancy: Looks like an induction variable\n");
    3410              :           nophi = true;
    3411              :         }
    3412              :     }
    3413              : 
    3414              :   /* Make the necessary insertions.  */
    3415      6244383 :   FOR_EACH_EDGE (pred, ei, block->preds)
    3416              :     {
    3417              :       /* When we are not inserting a PHI node do not bother inserting
    3418              :          into places that do not dominate the anticipated computations.  */
    3419      4237351 :       if (nophi && !dominated_by_p (CDI_DOMINATORS, block, pred->src))
    3420      1523618 :         continue;
    3421      2716969 :       gimple_seq stmts = NULL;
    3422      2716969 :       tree builtexpr;
    3423      2716969 :       bprime = pred->src;
    3424      2716969 :       eprime = avail[pred->dest_idx];
    3425      2716969 :       builtexpr = create_expression_by_pieces (bprime, eprime,
    3426              :                                                &stmts, type);
    3427      2716969 :       gcc_assert (!(pred->flags & EDGE_ABNORMAL));
    3428      2716969 :       if (!gimple_seq_empty_p (stmts))
    3429              :         {
    3430       523507 :           basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
    3431       523507 :           gcc_assert (! new_bb);
    3432              :           insertions = true;
    3433              :         }
    3434      2716969 :       if (!builtexpr)
    3435              :         {
    3436              :           /* We cannot insert a PHI node if we failed to insert
    3437              :              on one edge.  */
    3438         3236 :           nophi = true;
    3439         3236 :           continue;
    3440              :         }
    3441      2713733 :       if (is_gimple_min_invariant (builtexpr))
    3442      1446919 :         avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
    3443              :       else
    3444      1266814 :         avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
    3445              :     }
    3446              :   /* If we didn't want a phi node, and we made insertions, we still have
    3447              :      inserted new stuff, and thus return true.  If we didn't want a phi node,
    3448              :      and didn't make insertions, we haven't added anything new, so return
    3449              :      false.  */
    3450      2007032 :   if (nophi && insertions)
    3451              :     return true;
    3452      1998171 :   else if (nophi && !insertions)
    3453              :     return false;
    3454              : 
    3455              :   /* Now build a phi for the new variable.  */
    3456       493942 :   temp = make_temp_ssa_name (type, NULL, "prephitmp");
    3457       493942 :   phi = create_phi_node (temp, block);
    3458              : 
    3459       493942 :   vn_ssa_aux_t vn_info = VN_INFO (temp);
    3460       493942 :   vn_info->value_id = val;
    3461       493942 :   vn_info->valnum = vn_valnum_from_value_id (val);
    3462       493942 :   if (vn_info->valnum == NULL_TREE)
    3463        99416 :     vn_info->valnum = temp;
    3464       493942 :   bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
    3465      1696491 :   FOR_EACH_EDGE (pred, ei, block->preds)
    3466              :     {
    3467      1202549 :       pre_expr ae = avail[pred->dest_idx];
    3468      1202549 :       gcc_assert (get_expr_type (ae) == type
    3469              :                   || useless_type_conversion_p (type, get_expr_type (ae)));
    3470      1202549 :       if (ae->kind == CONSTANT)
    3471       193814 :         add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
    3472              :                      pred, UNKNOWN_LOCATION);
    3473              :       else
    3474      1008735 :         add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
    3475              :     }
    3476              : 
    3477       493942 :   newphi = get_or_alloc_expr_for_name (temp);
    3478       493942 :   add_to_value (val, newphi);
    3479              : 
    3480              :   /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
    3481              :      this insertion, since we test for the existence of this value in PHI_GEN
    3482              :      before proceeding with the partial redundancy checks in insert_aux.
    3483              : 
    3484              :      The value may exist in AVAIL_OUT, in particular, it could be represented
    3485              :      by the expression we are trying to eliminate, in which case we want the
    3486              :      replacement to occur.  If it's not existing in AVAIL_OUT, we want it
    3487              :      inserted there.
    3488              : 
    3489              :      Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
    3490              :      this block, because if it did, it would have existed in our dominator's
    3491              :      AVAIL_OUT, and would have been skipped due to the full redundancy check.
    3492              :   */
    3493              : 
    3494       493942 :   bitmap_insert_into_set (PHI_GEN (block), newphi);
    3495       493942 :   bitmap_value_replace_in_set (AVAIL_OUT (block),
    3496              :                                newphi);
    3497       493942 :   if (NEW_SETS (block))
    3498       493942 :     bitmap_insert_into_set (NEW_SETS (block), newphi);
    3499              : 
    3500              :   /* If we insert a PHI node for a conversion of another PHI node
    3501              :      in the same basic-block try to preserve range information.
    3502              :      This is important so that followup loop passes receive optimal
    3503              :      number of iteration analysis results.  See PR61743.  */
    3504       493942 :   if (expr->kind == NARY
    3505       191190 :       && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
    3506        57301 :       && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
    3507        57126 :       && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
    3508        47079 :       && INTEGRAL_TYPE_P (type)
    3509        46248 :       && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
    3510        45165 :       && (TYPE_PRECISION (type)
    3511        45165 :           >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
    3512       531182 :       && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
    3513              :     {
    3514        23246 :       int_range_max r;
    3515        46492 :       if (get_range_query (cfun)->range_of_expr (r, expr->u.nary->op[0])
    3516        23246 :           && !r.undefined_p ()
    3517        23246 :           && !r.varying_p ()
    3518        46492 :           && !wi::neg_p (r.lower_bound (), SIGNED)
    3519        62410 :           && !wi::neg_p (r.upper_bound (), SIGNED))
    3520              :         {
    3521              :           /* Just handle extension and sign-changes of all-positive ranges.  */
    3522        15056 :           range_cast (r, type);
    3523        15056 :           set_range_info (temp, r);
    3524              :         }
    3525        23246 :     }
    3526              : 
    3527       493942 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3528              :     {
    3529            8 :       fprintf (dump_file, "Created phi ");
    3530            8 :       print_gimple_stmt (dump_file, phi, 0);
    3531            8 :       fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
    3532              :     }
    3533       493942 :   pre_stats.phis++;
    3534       493942 :   return true;
    3535              : }
    3536              : 
    3537              : 
    3538              : 
    3539              : /* Perform insertion of partially redundant or hoistable values.
    3540              :    For BLOCK, do the following:
    3541              :    1.  Propagate the NEW_SETS of the dominator into the current block.
    3542              :    If the block has multiple predecessors,
    3543              :        2a. Iterate over the ANTIC expressions for the block to see if
    3544              :            any of them are partially redundant.
    3545              :        2b. If so, insert them into the necessary predecessors to make
    3546              :            the expression fully redundant.
    3547              :        2c. Insert a new PHI merging the values of the predecessors.
    3548              :        2d. Insert the new PHI, and the new expressions, into the
    3549              :            NEW_SETS set.
    3550              :    If the block has multiple successors,
    3551              :        3a. Iterate over the ANTIC values for the block to see if
    3552              :            any of them are good candidates for hoisting.
    3553              :        3b. If so, insert expressions computing the values in BLOCK,
    3554              :            and add the new expressions into the NEW_SETS set.
    3555              :    4. Recursively call ourselves on the dominator children of BLOCK.
    3556              : 
    3557              :    Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
    3558              :    do_pre_regular_insertion and do_partial_insertion.  3a and 3b are
    3559              :    done in do_hoist_insertion.
    3560              : */
    3561              : 
    3562              : static bool
    3563      3747619 : do_pre_regular_insertion (basic_block block, basic_block dom,
    3564              :                           vec<pre_expr> exprs)
    3565              : {
    3566      3747619 :   bool new_stuff = false;
    3567      3747619 :   pre_expr expr;
    3568      3747619 :   auto_vec<pre_expr, 2> avail;
    3569      3747619 :   int i;
    3570              : 
    3571      3747619 :   avail.safe_grow (EDGE_COUNT (block->preds), true);
    3572              : 
    3573     29743987 :   FOR_EACH_VEC_ELT (exprs, i, expr)
    3574              :     {
    3575     22248749 :       if (expr->kind == NARY
    3576     22248749 :           || expr->kind == REFERENCE)
    3577              :         {
    3578     12563320 :           unsigned int val;
    3579     12563320 :           bool by_some = false;
    3580     12563320 :           bool cant_insert = false;
    3581     12563320 :           bool all_same = true;
    3582     12563320 :           unsigned num_inserts = 0;
    3583     12563320 :           unsigned num_const = 0;
    3584     12563320 :           pre_expr first_s = NULL;
    3585     12563320 :           edge pred;
    3586     12563320 :           basic_block bprime;
    3587     12563320 :           pre_expr eprime = NULL;
    3588     12563320 :           edge_iterator ei;
    3589     12563320 :           pre_expr edoubleprime = NULL;
    3590     12563320 :           bool do_insertion = false;
    3591              : 
    3592     12563320 :           val = get_expr_value_id (expr);
    3593     25126640 :           if (bitmap_set_contains_value (PHI_GEN (block), val))
    3594      1072400 :             continue;
    3595     11753262 :           if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
    3596              :             {
    3597       262342 :               if (dump_file && (dump_flags & TDF_DETAILS))
    3598              :                 {
    3599            7 :                   fprintf (dump_file, "Found fully redundant value: ");
    3600            7 :                   print_pre_expr (dump_file, expr);
    3601            7 :                   fprintf (dump_file, "\n");
    3602              :                 }
    3603       262342 :               continue;
    3604              :             }
    3605              : 
    3606     37471607 :           FOR_EACH_EDGE (pred, ei, block->preds)
    3607              :             {
    3608     25981433 :               unsigned int vprime;
    3609              : 
    3610              :               /* We should never run insertion for the exit block
    3611              :                  and so not come across fake pred edges.  */
    3612     25981433 :               gcc_assert (!(pred->flags & EDGE_FAKE));
    3613     25981433 :               bprime = pred->src;
    3614              :               /* We are looking at ANTIC_OUT of bprime.  */
    3615     25981433 :               eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
    3616              : 
    3617              :               /* eprime will generally only be NULL if the
    3618              :                  value of the expression, translated
    3619              :                  through the PHI for this predecessor, is
    3620              :                  undefined.  If that is the case, we can't
    3621              :                  make the expression fully redundant,
    3622              :                  because its value is undefined along a
    3623              :                  predecessor path.  We can thus break out
    3624              :                  early because it doesn't matter what the
    3625              :                  rest of the results are.  */
    3626     25981433 :               if (eprime == NULL)
    3627              :                 {
    3628          746 :                   avail[pred->dest_idx] = NULL;
    3629          746 :                   cant_insert = true;
    3630          746 :                   break;
    3631              :                 }
    3632              : 
    3633     25980687 :               vprime = get_expr_value_id (eprime);
    3634     25980687 :               edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
    3635              :                                                  vprime);
    3636     25980687 :               if (edoubleprime == NULL)
    3637              :                 {
    3638     23291818 :                   avail[pred->dest_idx] = eprime;
    3639     23291818 :                   all_same = false;
    3640     23291818 :                   num_inserts++;
    3641              :                 }
    3642              :               else
    3643              :                 {
    3644      2688869 :                   avail[pred->dest_idx] = edoubleprime;
    3645      2688869 :                   by_some = true;
    3646      2688869 :                   if (edoubleprime->kind == CONSTANT)
    3647      1787419 :                     num_const++;
    3648              :                   /* We want to perform insertions to remove a redundancy on
    3649              :                      a path in the CFG we want to optimize for speed.  */
    3650      2688869 :                   if (optimize_edge_for_speed_p (pred))
    3651      2256825 :                     do_insertion = true;
    3652      2688869 :                   if (first_s == NULL)
    3653              :                     first_s = edoubleprime;
    3654       302226 :                   else if (!pre_expr_d::equal (first_s, edoubleprime))
    3655       232374 :                     all_same = false;
    3656              :                 }
    3657              :             }
    3658              :           /* If we can insert it, it's not the same value
    3659              :              already existing along every predecessor, and
    3660              :              it's defined by some predecessor, it is
    3661              :              partially redundant.  */
    3662     11490920 :           if (!cant_insert && !all_same && by_some)
    3663              :             {
    3664              :               /* If the expression is redundant on all edges and we need
    3665              :                  to at most insert one copy from a constant do the PHI
    3666              :                  insertion even when not optimizing a path that's to be
    3667              :                  optimized for speed.  */
    3668      2383223 :               if (num_inserts == 0 && num_const <= 1)
    3669              :                 do_insertion = true;
    3670      2229923 :               if (!do_insertion)
    3671              :                 {
    3672       382459 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    3673              :                     {
    3674            0 :                       fprintf (dump_file, "Skipping partial redundancy for "
    3675              :                                "expression ");
    3676            0 :                       print_pre_expr (dump_file, expr);
    3677            0 :                       fprintf (dump_file, " (%04d), no redundancy on to be "
    3678              :                                "optimized for speed edge\n", val);
    3679              :                     }
    3680              :                 }
    3681      2000764 :               else if (dbg_cnt (treepre_insert))
    3682              :                 {
    3683      2000764 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    3684              :                     {
    3685           64 :                       fprintf (dump_file, "Found partial redundancy for "
    3686              :                                "expression ");
    3687           64 :                       print_pre_expr (dump_file, expr);
    3688           64 :                       fprintf (dump_file, " (%04d)\n",
    3689              :                                get_expr_value_id (expr));
    3690              :                     }
    3691      2000764 :                   if (insert_into_preds_of_block (block,
    3692              :                                                   get_expression_id (expr),
    3693              :                                                   avail))
    3694     11490920 :                     new_stuff = true;
    3695              :                 }
    3696              :             }
    3697              :           /* If all edges produce the same value and that value is
    3698              :              an invariant, then the PHI has the same value on all
    3699              :              edges.  Note this.  */
    3700      9107697 :           else if (!cant_insert
    3701      9107697 :                    && all_same
    3702      9107697 :                    && (edoubleprime->kind != NAME
    3703         1793 :                        || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI
    3704              :                              (PRE_EXPR_NAME (edoubleprime))))
    3705              :             {
    3706         3394 :               gcc_assert (edoubleprime->kind == CONSTANT
    3707              :                           || edoubleprime->kind == NAME);
    3708              : 
    3709         3394 :               tree temp = make_temp_ssa_name (get_expr_type (expr),
    3710              :                                               NULL, "pretmp");
    3711         3394 :               gassign *assign
    3712         3394 :                 = gimple_build_assign (temp,
    3713         3394 :                                        edoubleprime->kind == CONSTANT ?
    3714              :                                        PRE_EXPR_CONSTANT (edoubleprime) :
    3715              :                                        PRE_EXPR_NAME (edoubleprime));
    3716         3394 :               gimple_stmt_iterator gsi = gsi_after_labels (block);
    3717         3394 :               gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
    3718              : 
    3719         3394 :               vn_ssa_aux_t vn_info = VN_INFO (temp);
    3720         3394 :               vn_info->value_id = val;
    3721         3394 :               vn_info->valnum = vn_valnum_from_value_id (val);
    3722         3394 :               if (vn_info->valnum == NULL_TREE)
    3723          610 :                 vn_info->valnum = temp;
    3724         3394 :               bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
    3725         3394 :               pre_expr newe = get_or_alloc_expr_for_name (temp);
    3726         3394 :               add_to_value (val, newe);
    3727         3394 :               bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
    3728         3394 :               bitmap_insert_into_set (NEW_SETS (block), newe);
    3729         3394 :               bitmap_insert_into_set (PHI_GEN (block), newe);
    3730              :             }
    3731              :         }
    3732              :     }
    3733              : 
    3734      3747619 :   return new_stuff;
    3735      3747619 : }
    3736              : 
    3737              : 
    3738              : /* Perform insertion for partially anticipatable expressions.  There
    3739              :    is only one case we will perform insertion for these.  This case is
    3740              :    if the expression is partially anticipatable, and fully available.
    3741              :    In this case, we know that putting it earlier will enable us to
    3742              :    remove the later computation.  */
    3743              : 
    3744              : static bool
    3745       347978 : do_pre_partial_partial_insertion (basic_block block, basic_block dom,
    3746              :                                   vec<pre_expr> exprs)
    3747              : {
    3748       347978 :   bool new_stuff = false;
    3749       347978 :   pre_expr expr;
    3750       347978 :   auto_vec<pre_expr, 2> avail;
    3751       347978 :   int i;
    3752              : 
    3753       347978 :   avail.safe_grow (EDGE_COUNT (block->preds), true);
    3754              : 
    3755      3691900 :   FOR_EACH_VEC_ELT (exprs, i, expr)
    3756              :     {
    3757      2995944 :       if (expr->kind == NARY
    3758      2995944 :           || expr->kind == REFERENCE)
    3759              :         {
    3760      2287030 :           unsigned int val;
    3761      2287030 :           bool by_all = true;
    3762      2287030 :           bool cant_insert = false;
    3763      2287030 :           edge pred;
    3764      2287030 :           basic_block bprime;
    3765      2287030 :           pre_expr eprime = NULL;
    3766      2287030 :           edge_iterator ei;
    3767              : 
    3768      2287030 :           val = get_expr_value_id (expr);
    3769      4574060 :           if (bitmap_set_contains_value (PHI_GEN (block), val))
    3770        57972 :             continue;
    3771      2277756 :           if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
    3772        48698 :             continue;
    3773              : 
    3774      2307339 :           FOR_EACH_EDGE (pred, ei, block->preds)
    3775              :             {
    3776      2297247 :               unsigned int vprime;
    3777      2297247 :               pre_expr edoubleprime;
    3778              : 
    3779              :               /* We should never run insertion for the exit block
    3780              :                  and so not come across fake pred edges.  */
    3781      2297247 :               gcc_assert (!(pred->flags & EDGE_FAKE));
    3782      2297247 :               bprime = pred->src;
    3783      4594494 :               eprime = phi_translate (NULL, expr, ANTIC_IN (block),
    3784      2297247 :                                       PA_IN (block), pred);
    3785              : 
    3786              :               /* eprime will generally only be NULL if the
    3787              :                  value of the expression, translated
    3788              :                  through the PHI for this predecessor, is
    3789              :                  undefined.  If that is the case, we can't
    3790              :                  make the expression fully redundant,
    3791              :                  because its value is undefined along a
    3792              :                  predecessor path.  We can thus break out
    3793              :                  early because it doesn't matter what the
    3794              :                  rest of the results are.  */
    3795      2297247 :               if (eprime == NULL)
    3796              :                 {
    3797           22 :                   avail[pred->dest_idx] = NULL;
    3798           22 :                   cant_insert = true;
    3799           22 :                   break;
    3800              :                 }
    3801              : 
    3802      2297225 :               vprime = get_expr_value_id (eprime);
    3803      2297225 :               edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
    3804      2297225 :               avail[pred->dest_idx] = edoubleprime;
    3805      2297225 :               if (edoubleprime == NULL)
    3806              :                 {
    3807              :                   by_all = false;
    3808              :                   break;
    3809              :                 }
    3810              :             }
    3811              : 
    3812              :           /* If we can insert it, it's not the same value
    3813              :              already existing along every predecessor, and
    3814              :              it's defined by some predecessor, it is
    3815              :              partially redundant.  */
    3816      2229058 :           if (!cant_insert && by_all)
    3817              :             {
    3818        10092 :               edge succ;
    3819        10092 :               bool do_insertion = false;
    3820              : 
    3821              :               /* Insert only if we can remove a later expression on a path
    3822              :                  that we want to optimize for speed.
    3823              :                  The phi node that we will be inserting in BLOCK is not free,
    3824              :                  and inserting it for the sake of !optimize_for_speed successor
    3825              :                  may cause regressions on the speed path.  */
    3826        27978 :               FOR_EACH_EDGE (succ, ei, block->succs)
    3827              :                 {
    3828        17886 :                   if (bitmap_set_contains_value (PA_IN (succ->dest), val)
    3829        17886 :                       || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
    3830              :                     {
    3831         9414 :                       if (optimize_edge_for_speed_p (succ))
    3832        17886 :                         do_insertion = true;
    3833              :                     }
    3834              :                 }
    3835              : 
    3836        10092 :               if (!do_insertion)
    3837              :                 {
    3838         3824 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    3839              :                     {
    3840            0 :                       fprintf (dump_file, "Skipping partial partial redundancy "
    3841              :                                "for expression ");
    3842            0 :                       print_pre_expr (dump_file, expr);
    3843            0 :                       fprintf (dump_file, " (%04d), not (partially) anticipated "
    3844              :                                "on any to be optimized for speed edges\n", val);
    3845              :                     }
    3846              :                 }
    3847         6268 :               else if (dbg_cnt (treepre_insert))
    3848              :                 {
    3849         6268 :                   pre_stats.pa_insert++;
    3850         6268 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    3851              :                     {
    3852            0 :                       fprintf (dump_file, "Found partial partial redundancy "
    3853              :                                "for expression ");
    3854            0 :                       print_pre_expr (dump_file, expr);
    3855            0 :                       fprintf (dump_file, " (%04d)\n",
    3856              :                                get_expr_value_id (expr));
    3857              :                     }
    3858         6268 :                   if (insert_into_preds_of_block (block,
    3859              :                                                   get_expression_id (expr),
    3860              :                                                   avail))
    3861        10092 :                     new_stuff = true;
    3862              :                 }
    3863              :             }
    3864              :         }
    3865              :     }
    3866              : 
    3867       347978 :   return new_stuff;
    3868       347978 : }
    3869              : 
    3870              : /* Insert expressions in BLOCK to compute hoistable values up.
    3871              :    Return TRUE if something was inserted, otherwise return FALSE.
    3872              :    The caller has to make sure that BLOCK has at least two successors.  */
    3873              : 
    3874              : static bool
    3875      4840108 : do_hoist_insertion (basic_block block)
    3876              : {
    3877      4840108 :   edge e;
    3878      4840108 :   edge_iterator ei;
    3879      4840108 :   bool new_stuff = false;
    3880      4840108 :   unsigned i;
    3881      4840108 :   gimple_stmt_iterator last;
    3882              : 
    3883              :   /* At least two successors, or else...  */
    3884      4840108 :   gcc_assert (EDGE_COUNT (block->succs) >= 2);
    3885              : 
    3886              :   /* Check that all successors of BLOCK are dominated by block.
    3887              :      We could use dominated_by_p() for this, but actually there is a much
    3888              :      quicker check: any successor that is dominated by BLOCK can't have
    3889              :      more than one predecessor edge.  */
    3890     14614850 :   FOR_EACH_EDGE (e, ei, block->succs)
    3891      9782334 :     if (! single_pred_p (e->dest))
    3892              :       return false;
    3893              : 
    3894              :   /* Determine the insertion point.  If we cannot safely insert before
    3895              :      the last stmt if we'd have to, bail out.  */
    3896      4832516 :   last = gsi_last_bb (block);
    3897      4832516 :   if (!gsi_end_p (last)
    3898      4832063 :       && !is_ctrl_stmt (gsi_stmt (last))
    3899      5432412 :       && stmt_ends_bb_p (gsi_stmt (last)))
    3900              :     return false;
    3901              : 
    3902              :   /* We have multiple successors, compute ANTIC_OUT by taking the intersection
    3903              :      of all of ANTIC_IN translating through PHI nodes.  Track the union
    3904              :      of the expression sets so we can pick a representative that is
    3905              :      fully generatable out of hoistable expressions.  */
    3906      4233196 :   bitmap_set_t ANTIC_OUT = bitmap_set_new ();
    3907      4233196 :   bool first = true;
    3908     12803107 :   FOR_EACH_EDGE (e, ei, block->succs)
    3909              :     {
    3910      8569911 :       if (first)
    3911              :         {
    3912      4233196 :           phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
    3913      4233196 :           first = false;
    3914              :         }
    3915      4336715 :       else if (!gimple_seq_empty_p (phi_nodes (e->dest)))
    3916              :         {
    3917            7 :           bitmap_set_t tmp = bitmap_set_new ();
    3918            7 :           phi_translate_set (tmp, ANTIC_IN (e->dest), e);
    3919            7 :           bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
    3920            7 :           bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
    3921            7 :           bitmap_set_free (tmp);
    3922              :         }
    3923              :       else
    3924              :         {
    3925      4336708 :           bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
    3926      4336708 :           bitmap_ior_into (&ANTIC_OUT->expressions,
    3927      4336708 :                            &ANTIC_IN (e->dest)->expressions);
    3928              :         }
    3929              :     }
    3930              : 
    3931              :   /* Compute the set of hoistable expressions from ANTIC_OUT.  First compute
    3932              :      hoistable values.  */
    3933      4233196 :   bitmap_set hoistable_set;
    3934              : 
    3935              :   /* A hoistable value must be in ANTIC_OUT(block)
    3936              :      but not in AVAIL_OUT(BLOCK).  */
    3937      4233196 :   bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
    3938      4233196 :   bitmap_and_compl (&hoistable_set.values,
    3939      4233196 :                     &ANTIC_OUT->values, &AVAIL_OUT (block)->values);
    3940              : 
    3941              :   /* Short-cut for a common case: hoistable_set is empty.  */
    3942      4233196 :   if (bitmap_empty_p (&hoistable_set.values))
    3943              :     {
    3944      3463441 :       bitmap_set_free (ANTIC_OUT);
    3945      3463441 :       return false;
    3946              :     }
    3947              : 
    3948              :   /* Compute which of the hoistable values is in AVAIL_OUT of
    3949              :      at least one of the successors of BLOCK.  */
    3950       769755 :   bitmap_head availout_in_some;
    3951       769755 :   bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
    3952      2326175 :   FOR_EACH_EDGE (e, ei, block->succs)
    3953              :     /* Do not consider expressions solely because their availability
    3954              :        on loop exits.  They'd be ANTIC-IN throughout the whole loop
    3955              :        and thus effectively hoisted across loops by combination of
    3956              :        PRE and hoisting.  */
    3957      1556420 :     if (! loop_exit_edge_p (block->loop_father, e))
    3958      1383011 :       bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
    3959      1383011 :                            &AVAIL_OUT (e->dest)->values);
    3960       769755 :   bitmap_clear (&hoistable_set.values);
    3961              : 
    3962              :   /* Short-cut for a common case: availout_in_some is empty.  */
    3963       769755 :   if (bitmap_empty_p (&availout_in_some))
    3964              :     {
    3965       626253 :       bitmap_set_free (ANTIC_OUT);
    3966       626253 :       return false;
    3967              :     }
    3968              : 
    3969              :   /* Hack hoistable_set in-place so we can use sorted_array_from_bitmap_set.  */
    3970       143502 :   bitmap_move (&hoistable_set.values, &availout_in_some);
    3971       143502 :   hoistable_set.expressions = ANTIC_OUT->expressions;
    3972              : 
    3973              :   /* Now finally construct the topological-ordered expression set.  */
    3974       143502 :   vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set, true);
    3975              : 
    3976              :   /* If there are candidate values for hoisting, insert expressions
    3977              :      strategically to make the hoistable expressions fully redundant.  */
    3978       143502 :   pre_expr expr;
    3979       509563 :   FOR_EACH_VEC_ELT (exprs, i, expr)
    3980              :     {
    3981              :       /* While we try to sort expressions topologically above the
    3982              :          sorting doesn't work out perfectly.  Catch expressions we
    3983              :          already inserted.  */
    3984       222559 :       unsigned int value_id = get_expr_value_id (expr);
    3985       445118 :       if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
    3986              :         {
    3987            0 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3988              :             {
    3989            0 :               fprintf (dump_file,
    3990              :                        "Already inserted expression for ");
    3991            0 :               print_pre_expr (dump_file, expr);
    3992            0 :               fprintf (dump_file, " (%04d)\n", value_id);
    3993              :             }
    3994         1558 :           continue;
    3995              :         }
    3996              : 
    3997              :       /* If we end up with a punned expression representation and this
    3998              :          happens to be a float typed one give up - we can't know for
    3999              :          sure whether all paths perform the floating-point load we are
    4000              :          about to insert and on some targets this can cause correctness
    4001              :          issues.  See PR88240.  */
    4002       222559 :       if (expr->kind == REFERENCE
    4003        96627 :           && PRE_EXPR_REFERENCE (expr)->punned
    4004       222559 :           && FLOAT_TYPE_P (get_expr_type (expr)))
    4005            0 :         continue;
    4006              : 
    4007              :       /* Only hoist if the full expression is available for hoisting.
    4008              :          This avoids hoisting values that are not common and for
    4009              :          example evaluate an expression that's not valid to evaluate
    4010              :          unconditionally (PR112310).  */
    4011       222559 :       if (!valid_in_sets (&hoistable_set, AVAIL_OUT (block), expr))
    4012            5 :         continue;
    4013              : 
    4014              :       /* OK, we should hoist this value.  Perform the transformation.  */
    4015       222554 :       pre_stats.hoist_insert++;
    4016       222554 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4017              :         {
    4018            2 :           fprintf (dump_file,
    4019              :                    "Inserting expression in block %d for code hoisting: ",
    4020              :                    block->index);
    4021            2 :           print_pre_expr (dump_file, expr);
    4022            2 :           fprintf (dump_file, " (%04d)\n", value_id);
    4023              :         }
    4024              : 
    4025       222554 :       gimple_seq stmts = NULL;
    4026       222554 :       tree res = create_expression_by_pieces (block, expr, &stmts,
    4027              :                                               get_expr_type (expr));
    4028              : 
    4029              :       /* Do not return true if expression creation ultimately
    4030              :          did not insert any statements.  */
    4031       222554 :       if (gimple_seq_empty_p (stmts))
    4032              :         res = NULL_TREE;
    4033              :       else
    4034              :         {
    4035       221001 :           if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
    4036       221001 :             gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
    4037              :           else
    4038            0 :             gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
    4039              :         }
    4040              : 
    4041              :       /* Make sure to not return true if expression creation ultimately
    4042              :          failed but also make sure to insert any stmts produced as they
    4043              :          are tracked in inserted_exprs.  */
    4044       221001 :       if (! res)
    4045         1553 :         continue;
    4046              : 
    4047       221001 :       new_stuff = true;
    4048              :     }
    4049              : 
    4050       143502 :   exprs.release ();
    4051       143502 :   bitmap_clear (&hoistable_set.values);
    4052       143502 :   bitmap_set_free (ANTIC_OUT);
    4053              : 
    4054       143502 :   return new_stuff;
    4055              : }
    4056              : 
    4057              : /* Perform insertion of partially redundant and hoistable values.  */
    4058              : 
    4059              : static void
    4060       987726 : insert (void)
    4061              : {
    4062       987726 :   basic_block bb;
    4063              : 
    4064     16454524 :   FOR_ALL_BB_FN (bb, cfun)
    4065     15466798 :     NEW_SETS (bb) = bitmap_set_new ();
    4066              : 
    4067       987726 :   int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
    4068       987726 :   int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun) + 1);
    4069       987726 :   int rpo_num = pre_and_rev_post_order_compute (NULL, rpo, false);
    4070     15466798 :   for (int i = 0; i < rpo_num; ++i)
    4071     13491346 :     bb_rpo[rpo[i]] = i;
    4072              : 
    4073              :   int num_iterations = 0;
    4074      1040850 :   bool changed;
    4075      1040850 :   do
    4076              :     {
    4077      1040850 :       num_iterations++;
    4078      1040850 :       if (dump_file && dump_flags & TDF_DETAILS)
    4079           18 :         fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
    4080              : 
    4081      1040850 :       changed = false;
    4082     18790072 :       for (int idx = 0; idx < rpo_num; ++idx)
    4083              :         {
    4084     17749222 :           basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
    4085     17749222 :           basic_block dom = get_immediate_dominator (CDI_DOMINATORS, block);
    4086     17749222 :           if (dom)
    4087              :             {
    4088     17749222 :               unsigned i;
    4089     17749222 :               bitmap_iterator bi;
    4090     17749222 :               bitmap_set_t newset;
    4091              : 
    4092              :               /* First, update the AVAIL_OUT set with anything we may have
    4093              :                  inserted higher up in the dominator tree.  */
    4094     17749222 :               newset = NEW_SETS (dom);
    4095              : 
    4096              :               /* Note that we need to value_replace both NEW_SETS, and
    4097              :                  AVAIL_OUT. For both the case of NEW_SETS, the value may be
    4098              :                  represented by some non-simple expression here that we want
    4099              :                  to replace it with.  */
    4100     17749222 :               bool avail_out_changed = false;
    4101     34197237 :               FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
    4102              :                 {
    4103     16448015 :                   pre_expr expr = expression_for_id (i);
    4104     16448015 :                   bitmap_value_replace_in_set (NEW_SETS (block), expr);
    4105     16448015 :                   avail_out_changed
    4106     16448015 :                     |= bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
    4107              :                 }
    4108              :               /* We need to iterate if AVAIL_OUT of an already processed
    4109              :                  block source changed.  */
    4110     17749222 :               if (avail_out_changed && !changed)
    4111              :                 {
    4112      1774169 :                   edge_iterator ei;
    4113      1774169 :                   edge e;
    4114      4228667 :                   FOR_EACH_EDGE (e, ei, block->succs)
    4115      2454498 :                     if (e->dest->index != EXIT_BLOCK
    4116      2342692 :                         && bb_rpo[e->dest->index] < idx)
    4117      2454498 :                       changed = true;
    4118              :                 }
    4119              : 
    4120              :               /* Insert expressions for partial redundancies.  */
    4121     35497328 :               if (flag_tree_pre && !single_pred_p (block))
    4122              :                 {
    4123      3472234 :                   vec<pre_expr> exprs
    4124      3472234 :                     = sorted_array_from_bitmap_set (ANTIC_IN (block), true);
    4125              :                   /* Sorting is not perfect, iterate locally.  */
    4126      7219853 :                   while (do_pre_regular_insertion (block, dom, exprs))
    4127              :                     ;
    4128      3472234 :                   exprs.release ();
    4129      3472234 :                   if (do_partial_partial)
    4130              :                     {
    4131       344953 :                       exprs = sorted_array_from_bitmap_set (PA_IN (block),
    4132              :                                                             true);
    4133       692931 :                       while (do_pre_partial_partial_insertion (block, dom,
    4134              :                                                                exprs))
    4135              :                         ;
    4136       344953 :                       exprs.release ();
    4137              :                     }
    4138              :                 }
    4139              :             }
    4140              :         }
    4141              : 
    4142              :       /* Clear the NEW sets before the next iteration.  We have already
    4143              :          fully propagated its contents.  */
    4144      1040850 :       if (changed)
    4145      4417248 :         FOR_ALL_BB_FN (bb, cfun)
    4146      8728248 :           bitmap_set_free (NEW_SETS (bb));
    4147              :     }
    4148              :   while (changed);
    4149              : 
    4150       987726 :   statistics_histogram_event (cfun, "insert iterations", num_iterations);
    4151              : 
    4152              :   /* AVAIL_OUT is not needed after insertion so we don't have to
    4153              :      propagate NEW_SETS from hoist insertion.  */
    4154     16454524 :   FOR_ALL_BB_FN (bb, cfun)
    4155              :     {
    4156     15466798 :       bitmap_set_free (NEW_SETS (bb));
    4157     15466798 :       bitmap_set_pool.remove (NEW_SETS (bb));
    4158     15466798 :       NEW_SETS (bb) = NULL;
    4159              :     }
    4160              : 
    4161              :   /* Insert expressions for hoisting.  Do a backward walk here since
    4162              :      inserting into BLOCK exposes new opportunities in its predecessors.
    4163              :      Since PRE and hoist insertions can cause back-to-back iteration
    4164              :      and we are interested in PRE insertion exposed hoisting opportunities
    4165              :      but not in hoisting exposed PRE ones do hoist insertion only after
    4166              :      PRE insertion iteration finished and do not iterate it.  */
    4167       987726 :   if (flag_code_hoisting)
    4168     14478523 :     for (int idx = rpo_num - 1; idx >= 0; --idx)
    4169              :       {
    4170     13490850 :         basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
    4171     18330958 :         if (EDGE_COUNT (block->succs) >= 2)
    4172      4840108 :           changed |= do_hoist_insertion (block);
    4173              :       }
    4174              : 
    4175       987726 :   free (rpo);
    4176       987726 :   free (bb_rpo);
    4177       987726 : }
    4178              : 
    4179              : 
    4180              : /* Compute the AVAIL set for all basic blocks.
    4181              : 
    4182              :    This function performs value numbering of the statements in each basic
    4183              :    block.  The AVAIL sets are built from information we glean while doing
    4184              :    this value numbering, since the AVAIL sets contain only one entry per
    4185              :    value.
    4186              : 
    4187              :    AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
    4188              :    AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK].  */
    4189              : 
    4190              : static void
    4191       987726 : compute_avail (function *fun)
    4192              : {
    4193              : 
    4194       987726 :   basic_block block, son;
    4195       987726 :   basic_block *worklist;
    4196       987726 :   size_t sp = 0;
    4197       987726 :   unsigned i;
    4198       987726 :   tree name;
    4199              : 
    4200              :   /* We pretend that default definitions are defined in the entry block.
    4201              :      This includes function arguments and the static chain decl.  */
    4202     48470147 :   FOR_EACH_SSA_NAME (i, name, fun)
    4203              :     {
    4204     34642387 :       pre_expr e;
    4205     34642387 :       if (!SSA_NAME_IS_DEFAULT_DEF (name)
    4206      2983150 :           || has_zero_uses (name)
    4207     37080047 :           || virtual_operand_p (name))
    4208     33191643 :         continue;
    4209              : 
    4210      1450744 :       e = get_or_alloc_expr_for_name (name);
    4211      1450744 :       add_to_value (get_expr_value_id (e), e);
    4212      1450744 :       bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)), e);
    4213      1450744 :       bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
    4214              :                                     e);
    4215              :     }
    4216              : 
    4217       987726 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4218              :     {
    4219           14 :       print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)),
    4220              :                         "tmp_gen", ENTRY_BLOCK);
    4221           14 :       print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
    4222              :                         "avail_out", ENTRY_BLOCK);
    4223              :     }
    4224              : 
    4225              :   /* Allocate the worklist.  */
    4226       987726 :   worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
    4227              : 
    4228              :   /* Seed the algorithm by putting the dominator children of the entry
    4229              :      block on the worklist.  */
    4230       987726 :   for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (fun));
    4231      1975452 :        son;
    4232       987726 :        son = next_dom_son (CDI_DOMINATORS, son))
    4233       987726 :     worklist[sp++] = son;
    4234              : 
    4235      1975452 :   BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (fun))
    4236       987726 :     = ssa_default_def (fun, gimple_vop (fun));
    4237              : 
    4238              :   /* Loop until the worklist is empty.  */
    4239     14479072 :   while (sp)
    4240              :     {
    4241     13491346 :       gimple *stmt;
    4242     13491346 :       basic_block dom;
    4243              : 
    4244              :       /* Pick a block from the worklist.  */
    4245     13491346 :       block = worklist[--sp];
    4246     13491346 :       vn_context_bb = block;
    4247              : 
    4248              :       /* Initially, the set of available values in BLOCK is that of
    4249              :          its immediate dominator.  */
    4250     13491346 :       dom = get_immediate_dominator (CDI_DOMINATORS, block);
    4251     13491346 :       if (dom)
    4252              :         {
    4253     13491346 :           bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
    4254     13491346 :           BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
    4255              :         }
    4256              : 
    4257              :       /* Generate values for PHI nodes.  */
    4258     17428116 :       for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
    4259      3936770 :            gsi_next (&gsi))
    4260              :         {
    4261      3936770 :           tree result = gimple_phi_result (gsi.phi ());
    4262              : 
    4263              :           /* We have no need for virtual phis, as they don't represent
    4264              :              actual computations.  */
    4265      7873540 :           if (virtual_operand_p (result))
    4266              :             {
    4267      1783619 :               BB_LIVE_VOP_ON_EXIT (block) = result;
    4268      1783619 :               continue;
    4269              :             }
    4270              : 
    4271      2153151 :           pre_expr e = get_or_alloc_expr_for_name (result);
    4272      2153151 :           add_to_value (get_expr_value_id (e), e);
    4273      2153151 :           bitmap_value_insert_into_set (AVAIL_OUT (block), e);
    4274      2153151 :           bitmap_insert_into_set (PHI_GEN (block), e);
    4275              :         }
    4276              : 
    4277     13491346 :       BB_MAY_NOTRETURN (block) = 0;
    4278              : 
    4279              :       /* Now compute value numbers and populate value sets with all
    4280              :          the expressions computed in BLOCK.  */
    4281     13491346 :       bool set_bb_may_notreturn = false;
    4282    112690367 :       for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
    4283     85707675 :            gsi_next (&gsi))
    4284              :         {
    4285     85707675 :           ssa_op_iter iter;
    4286     85707675 :           tree op;
    4287              : 
    4288     85707675 :           stmt = gsi_stmt (gsi);
    4289              : 
    4290     85707675 :           if (set_bb_may_notreturn)
    4291              :             {
    4292      2789772 :               BB_MAY_NOTRETURN (block) = 1;
    4293      2789772 :               set_bb_may_notreturn = false;
    4294              :             }
    4295              : 
    4296              :           /* Cache whether the basic-block has any non-visible side-effect
    4297              :              or control flow.
    4298              :              If this isn't a call or it is the last stmt in the
    4299              :              basic-block then the CFG represents things correctly.  */
    4300     85707675 :           if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
    4301              :             {
    4302              :               /* Non-looping const functions always return normally.
    4303              :                  Otherwise the call might not return or have side-effects
    4304              :                  that forbids hoisting possibly trapping expressions
    4305              :                  before it.  */
    4306      3925551 :               int flags = gimple_call_flags (stmt);
    4307      3925551 :               if (!(flags & (ECF_CONST|ECF_PURE))
    4308       595775 :                   || (flags & ECF_LOOPING_CONST_OR_PURE)
    4309      4494640 :                   || stmt_can_throw_external (fun, stmt))
    4310              :                 /* Defer setting of BB_MAY_NOTRETURN to avoid it
    4311              :                    influencing the processing of the call itself.  */
    4312              :                 set_bb_may_notreturn = true;
    4313              :             }
    4314              : 
    4315    101042613 :           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
    4316              :             {
    4317     15334938 :               pre_expr e = get_or_alloc_expr_for_name (op);
    4318     15334938 :               add_to_value (get_expr_value_id (e), e);
    4319     15334938 :               bitmap_insert_into_set (TMP_GEN (block), e);
    4320     15334938 :               bitmap_value_insert_into_set (AVAIL_OUT (block), e);
    4321              :             }
    4322              : 
    4323    113386736 :           if (gimple_vdef (stmt))
    4324     12296648 :             BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
    4325              : 
    4326     85707675 :           if (gimple_has_side_effects (stmt)
    4327     79224967 :               || stmt_could_throw_p (fun, stmt)
    4328    163766915 :               || is_gimple_debug (stmt))
    4329     80338930 :             continue;
    4330              : 
    4331     48337834 :           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
    4332              :             {
    4333     22787674 :               if (ssa_undefined_value_p (op))
    4334        58088 :                 continue;
    4335     22729586 :               pre_expr e = get_or_alloc_expr_for_name (op);
    4336     22729586 :               bitmap_value_insert_into_set (EXP_GEN (block), e);
    4337              :             }
    4338              : 
    4339     25550160 :           switch (gimple_code (stmt))
    4340              :             {
    4341       962555 :             case GIMPLE_RETURN:
    4342       962555 :               continue;
    4343              : 
    4344       567088 :             case GIMPLE_CALL:
    4345       567088 :               {
    4346       567088 :                 vn_reference_t ref;
    4347       567088 :                 vn_reference_s ref1;
    4348       567088 :                 pre_expr result = NULL;
    4349              : 
    4350       567088 :                 vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
    4351              :                 /* There is no point to PRE a call without a value.  */
    4352       567088 :                 if (!ref || !ref->result)
    4353        31542 :                   continue;
    4354              : 
    4355              :                 /* If the value of the call is not invalidated in
    4356              :                    this block until it is computed, add the expression
    4357              :                    to EXP_GEN.  */
    4358       535546 :                 if ((!gimple_vuse (stmt)
    4359       304910 :                      || gimple_code
    4360       304910 :                           (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
    4361       276759 :                      || gimple_bb (SSA_NAME_DEF_STMT
    4362              :                                    (gimple_vuse (stmt))) != block)
    4363              :                     /* If the REFERENCE traps and there was a preceding
    4364              :                        point in the block that might not return avoid
    4365              :                        adding the reference to EXP_GEN.  */
    4366       783458 :                     && (!BB_MAY_NOTRETURN (block)
    4367        10219 :                         || !vn_reference_may_trap (ref)))
    4368              :                   {
    4369       468329 :                     result = get_or_alloc_expr_for_reference
    4370       468329 :                                (ref, ref->value_id, gimple_location (stmt));
    4371       468329 :                     add_to_value (get_expr_value_id (result), result);
    4372       468329 :                     bitmap_value_insert_into_set (EXP_GEN (block), result);
    4373              :                   }
    4374       535546 :                 continue;
    4375       535546 :               }
    4376              : 
    4377     18651772 :             case GIMPLE_ASSIGN:
    4378     18651772 :               {
    4379     18651772 :                 pre_expr result = NULL;
    4380     18651772 :                 switch (vn_get_stmt_kind (stmt))
    4381              :                   {
    4382      7793659 :                   case VN_NARY:
    4383      7793659 :                     {
    4384      7793659 :                       enum tree_code code = gimple_assign_rhs_code (stmt);
    4385      7793659 :                       vn_nary_op_t nary;
    4386              : 
    4387              :                       /* COND_EXPR is awkward in that it contains an
    4388              :                          embedded complex expression.
    4389              :                          Don't even try to shove it through PRE.  */
    4390      7793659 :                       if (code == COND_EXPR)
    4391       152048 :                         continue;
    4392              : 
    4393      7789268 :                       vn_nary_op_lookup_stmt (stmt, &nary);
    4394      7789268 :                       if (!nary || nary->predicated_values)
    4395       115241 :                         continue;
    4396              : 
    4397      7674027 :                       unsigned value_id = nary->value_id;
    4398      7674027 :                       if (value_id_constant_p (value_id))
    4399            0 :                         continue;
    4400              : 
    4401              :                       /* Record the un-valueized expression for EXP_GEN.  */
    4402      7674027 :                       nary = XALLOCAVAR (struct vn_nary_op_s,
    4403              :                                          sizeof_vn_nary_op
    4404              :                                            (vn_nary_length_from_stmt (stmt)));
    4405      7674027 :                       init_vn_nary_op_from_stmt (nary, as_a <gassign *> (stmt));
    4406              : 
    4407              :                       /* If the NARY traps and there was a preceding
    4408              :                          point in the block that might not return avoid
    4409              :                          adding the nary to EXP_GEN.  */
    4410      7706443 :                       if (BB_MAY_NOTRETURN (block)
    4411      7674027 :                           && vn_nary_may_trap (nary))
    4412        32416 :                         continue;
    4413              : 
    4414      7641611 :                       result = get_or_alloc_expr_for_nary
    4415      7641611 :                                  (nary, value_id, gimple_location (stmt));
    4416      7641611 :                       break;
    4417              :                     }
    4418              : 
    4419      5229072 :                   case VN_REFERENCE:
    4420      5229072 :                     {
    4421      5229072 :                       tree rhs1 = gimple_assign_rhs1 (stmt);
    4422              :                       /* There is no point in trying to handle aggregates,
    4423              :                          even when via punning we might get a value number
    4424              :                          corresponding to a register typed load.  */
    4425      5229072 :                       if (!is_gimple_reg_type (TREE_TYPE (rhs1)))
    4426      1495795 :                         continue;
    4427      4859995 :                       ao_ref rhs1_ref;
    4428      4859995 :                       ao_ref_init (&rhs1_ref, rhs1);
    4429      4859995 :                       alias_set_type set = ao_ref_alias_set (&rhs1_ref);
    4430      4859995 :                       alias_set_type base_set
    4431      4859995 :                         = ao_ref_base_alias_set (&rhs1_ref);
    4432      4859995 :                       vec<vn_reference_op_s> operands
    4433      4859995 :                         = vn_reference_operands_for_lookup (rhs1);
    4434      4859995 :                       vn_reference_t ref;
    4435              : 
    4436              :                       /* We handle &MEM[ptr + 5].b[1].c as
    4437              :                          POINTER_PLUS_EXPR.  */
    4438      4859995 :                       if (operands[0].opcode == ADDR_EXPR
    4439      5119595 :                           && operands.last ().opcode == SSA_NAME)
    4440              :                         {
    4441       259589 :                           tree ops[2];
    4442       259589 :                           if (vn_pp_nary_for_addr (operands, ops))
    4443              :                             {
    4444       172595 :                               vn_nary_op_t nary;
    4445       172595 :                               vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
    4446       172595 :                                                         TREE_TYPE (rhs1), ops,
    4447              :                                                         &nary);
    4448       172595 :                               operands.release ();
    4449       172595 :                               if (nary && !nary->predicated_values)
    4450              :                                 {
    4451       172583 :                                   unsigned value_id = nary->value_id;
    4452       172583 :                                   if (value_id_constant_p (value_id))
    4453           12 :                                     continue;
    4454       172583 :                                   result = get_or_alloc_expr_for_nary
    4455       172583 :                                       (nary, value_id, gimple_location (stmt));
    4456       172583 :                                   break;
    4457              :                                 }
    4458           12 :                               continue;
    4459           12 :                             }
    4460              :                         }
    4461              : 
    4462      9374800 :                       vn_reference_lookup_pieces (gimple_vuse (stmt), set,
    4463      4687400 :                                                   base_set, TREE_TYPE (rhs1),
    4464              :                                                   operands, &ref, VN_WALK);
    4465              :                       /* When there is no value recorded or the value was
    4466              :                          recorded for a different type, fail, similar as
    4467              :                          how we do during PHI translation.  */
    4468      4690816 :                       if (!ref
    4469      4687400 :                           || !useless_type_conversion_p (TREE_TYPE (rhs1),
    4470              :                                                          ref->type))
    4471              :                         {
    4472         3416 :                           operands.release ();
    4473         3416 :                           continue;
    4474              :                         }
    4475      4683984 :                       operands.release ();
    4476              : 
    4477              :                       /* If the REFERENCE traps and there was a preceding
    4478              :                          point in the block that might not return avoid
    4479              :                          adding the reference to EXP_GEN.  */
    4480      4822012 :                       if (BB_MAY_NOTRETURN (block)
    4481      4683984 :                           && gimple_could_trap_p_1 (stmt, true, false))
    4482       138028 :                         continue;
    4483              : 
    4484              :                       /* If the value of the reference is not invalidated in
    4485              :                          this block until it is computed, add the expression
    4486              :                          to EXP_GEN.  */
    4487      9091912 :                       if (gimple_vuse (stmt))
    4488              :                         {
    4489      4458967 :                           gimple *def_stmt;
    4490      4458967 :                           bool ok = true;
    4491      4458967 :                           def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
    4492      7202483 :                           while (!gimple_nop_p (def_stmt)
    4493      6200417 :                                  && gimple_code (def_stmt) != GIMPLE_PHI
    4494     12182504 :                                  && gimple_bb (def_stmt) == block)
    4495              :                             {
    4496      3728778 :                               if (stmt_may_clobber_ref_p
    4497      3728778 :                                     (def_stmt, gimple_assign_rhs1 (stmt)))
    4498              :                                 {
    4499              :                                   ok = false;
    4500              :                                   break;
    4501              :                                 }
    4502      2743516 :                               def_stmt
    4503      2743516 :                                 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
    4504              :                             }
    4505      4458967 :                           if (!ok)
    4506       985262 :                             continue;
    4507              :                         }
    4508              : 
    4509              :                       /* Record the un-valueized expression for EXP_GEN.  */
    4510      3560694 :                       copy_reference_ops_from_ref (rhs1, &operands);
    4511      3560694 :                       vn_reference_t newref
    4512      3560694 :                         = XALLOCAVAR (struct vn_reference_s,
    4513              :                                       sizeof (vn_reference_s));
    4514      3560694 :                       memset (newref, 0, sizeof (vn_reference_s));
    4515      3560694 :                       newref->value_id = ref->value_id;
    4516      3560694 :                       newref->vuse = ref->vuse;
    4517      3560694 :                       newref->operands = operands;
    4518      3560694 :                       newref->type = TREE_TYPE (rhs1);
    4519      3560694 :                       newref->set = set;
    4520      3560694 :                       newref->base_set = base_set;
    4521      3560694 :                       newref->offset = 0;
    4522      3560694 :                       newref->max_size = -1;
    4523      3560694 :                       newref->result = ref->result;
    4524      3560694 :                       newref->hashcode = vn_reference_compute_hash (newref);
    4525              : 
    4526      3560694 :                       result = get_or_alloc_expr_for_reference
    4527      3560694 :                                  (newref, newref->value_id,
    4528              :                                   gimple_location (stmt), true);
    4529      3560694 :                       break;
    4530              :                     }
    4531              : 
    4532      5629041 :                   default:
    4533      5629041 :                     continue;
    4534      5629041 :                   }
    4535              : 
    4536     11374888 :                 add_to_value (get_expr_value_id (result), result);
    4537     11374888 :                 bitmap_value_insert_into_set (EXP_GEN (block), result);
    4538     11374888 :                 continue;
    4539     11374888 :               }
    4540      5368745 :             default:
    4541      5368745 :               break;
    4542       962555 :             }
    4543              :         }
    4544     13491346 :       if (set_bb_may_notreturn)
    4545              :         {
    4546       568689 :           BB_MAY_NOTRETURN (block) = 1;
    4547       568689 :           set_bb_may_notreturn = false;
    4548              :         }
    4549              : 
    4550     13491346 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4551              :         {
    4552          108 :           print_bitmap_set (dump_file, EXP_GEN (block),
    4553              :                             "exp_gen", block->index);
    4554          108 :           print_bitmap_set (dump_file, PHI_GEN (block),
    4555              :                             "phi_gen", block->index);
    4556          108 :           print_bitmap_set (dump_file, TMP_GEN (block),
    4557              :                             "tmp_gen", block->index);
    4558          108 :           print_bitmap_set (dump_file, AVAIL_OUT (block),
    4559              :                             "avail_out", block->index);
    4560              :         }
    4561              : 
    4562              :       /* Put the dominator children of BLOCK on the worklist of blocks
    4563              :          to compute available sets for.  */
    4564     13491346 :       for (son = first_dom_son (CDI_DOMINATORS, block);
    4565     25994966 :            son;
    4566     12503620 :            son = next_dom_son (CDI_DOMINATORS, son))
    4567     12503620 :         worklist[sp++] = son;
    4568              :     }
    4569       987726 :   vn_context_bb = NULL;
    4570              : 
    4571       987726 :   free (worklist);
    4572       987726 : }
    4573              : 
    4574              : 
    4575              : /* Initialize data structures used by PRE.  */
    4576              : 
    4577              : static void
    4578       987733 : init_pre (void)
    4579              : {
    4580       987733 :   basic_block bb;
    4581              : 
    4582       987733 :   next_expression_id = 1;
    4583       987733 :   expressions.create (0);
    4584       987733 :   expressions.safe_push (NULL);
    4585       987733 :   value_expressions.create (get_max_value_id () + 1);
    4586       987733 :   value_expressions.quick_grow_cleared (get_max_value_id () + 1);
    4587       987733 :   constant_value_expressions.create (get_max_constant_value_id () + 1);
    4588       987733 :   constant_value_expressions.quick_grow_cleared (get_max_constant_value_id () + 1);
    4589       987733 :   name_to_id.create (0);
    4590       987733 :   gcc_obstack_init (&pre_expr_obstack);
    4591              : 
    4592       987733 :   inserted_exprs = BITMAP_ALLOC (NULL);
    4593              : 
    4594       987733 :   connect_infinite_loops_to_exit ();
    4595       987733 :   memset (&pre_stats, 0, sizeof (pre_stats));
    4596              : 
    4597       987733 :   alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
    4598              : 
    4599       987733 :   calculate_dominance_info (CDI_DOMINATORS);
    4600              : 
    4601       987733 :   bitmap_obstack_initialize (&grand_bitmap_obstack);
    4602      1975466 :   expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
    4603     16492095 :   FOR_ALL_BB_FN (bb, cfun)
    4604              :     {
    4605     15504362 :       EXP_GEN (bb) = bitmap_set_new ();
    4606     15504362 :       PHI_GEN (bb) = bitmap_set_new ();
    4607     15504362 :       TMP_GEN (bb) = bitmap_set_new ();
    4608     15504362 :       AVAIL_OUT (bb) = bitmap_set_new ();
    4609     15504362 :       PHI_TRANS_TABLE (bb) = NULL;
    4610              :     }
    4611       987733 : }
    4612              : 
    4613              : 
    4614              : /* Deallocate data structures used by PRE.  */
    4615              : 
    4616              : static void
    4617       987733 : fini_pre ()
    4618              : {
    4619       987733 :   value_expressions.release ();
    4620       987733 :   constant_value_expressions.release ();
    4621     46721914 :   for (unsigned i = 1; i < expressions.length (); ++i)
    4622     44746448 :     if (expressions[i]->kind == REFERENCE)
    4623      6519677 :       PRE_EXPR_REFERENCE (expressions[i])->operands.release ();
    4624       987733 :   expressions.release ();
    4625       987733 :   bitmap_obstack_release (&grand_bitmap_obstack);
    4626       987733 :   bitmap_set_pool.release ();
    4627       987733 :   pre_expr_pool.release ();
    4628       987733 :   delete expression_to_id;
    4629       987733 :   expression_to_id = NULL;
    4630       987733 :   name_to_id.release ();
    4631       987733 :   obstack_free (&pre_expr_obstack, NULL);
    4632              : 
    4633       987733 :   basic_block bb;
    4634     16491781 :   FOR_ALL_BB_FN (bb, cfun)
    4635     15504048 :     if (bb->aux && PHI_TRANS_TABLE (bb))
    4636      6260836 :       delete PHI_TRANS_TABLE (bb);
    4637       987733 :   free_aux_for_blocks ();
    4638       987733 : }
    4639              : 
    4640              : namespace {
    4641              : 
    4642              : const pass_data pass_data_pre =
    4643              : {
    4644              :   GIMPLE_PASS, /* type */
    4645              :   "pre", /* name */
    4646              :   OPTGROUP_NONE, /* optinfo_flags */
    4647              :   TV_TREE_PRE, /* tv_id */
    4648              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    4649              :   0, /* properties_provided */
    4650              :   0, /* properties_destroyed */
    4651              :   TODO_rebuild_alias, /* todo_flags_start */
    4652              :   0, /* todo_flags_finish */
    4653              : };
    4654              : 
    4655              : class pass_pre : public gimple_opt_pass
    4656              : {
    4657              : public:
    4658       302985 :   pass_pre (gcc::context *ctxt)
    4659       605970 :     : gimple_opt_pass (pass_data_pre, ctxt)
    4660              :   {}
    4661              : 
    4662              :   /* opt_pass methods: */
    4663      1068301 :   bool gate (function *) final override
    4664      1068301 :     { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
    4665              :   unsigned int execute (function *) final override;
    4666              : 
    4667              : }; // class pass_pre
    4668              : 
    4669              : /* Valueization hook for RPO VN when we are calling back to it
    4670              :    at ANTIC compute time.  */
    4671              : 
    4672              : static tree
    4673    129271195 : pre_valueize (tree name)
    4674              : {
    4675    129271195 :   if (TREE_CODE (name) == SSA_NAME)
    4676              :     {
    4677    128985325 :       tree tem = VN_INFO (name)->valnum;
    4678    128985325 :       if (tem != VN_TOP && tem != name)
    4679              :         {
    4680     17070870 :           if (TREE_CODE (tem) != SSA_NAME
    4681     17070870 :               || SSA_NAME_IS_DEFAULT_DEF (tem))
    4682              :             return tem;
    4683              :           /* We create temporary SSA names for representatives that
    4684              :              do not have a definition (yet) but are not default defs either
    4685              :              assume they are fine to use.  */
    4686     17065892 :           basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
    4687     17065892 :           if (! def_bb
    4688     17065892 :               || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
    4689       137555 :             return tem;
    4690              :           /* ??? Now we could look for a leader.  Ideally we'd somehow
    4691              :              expose RPO VN leaders and get rid of AVAIL_OUT as well...  */
    4692              :         }
    4693              :     }
    4694              :   return name;
    4695              : }
    4696              : 
    4697              : unsigned int
    4698       987733 : pass_pre::execute (function *fun)
    4699              : {
    4700       987733 :   unsigned int todo = 0;
    4701              : 
    4702      1975466 :   do_partial_partial =
    4703       987733 :     flag_tree_partial_pre && optimize_function_for_speed_p (fun);
    4704              : 
    4705              :   /* This has to happen before VN runs because
    4706              :      loop_optimizer_init may create new phis, etc.  */
    4707       987733 :   loop_optimizer_init (LOOPS_NORMAL);
    4708       987733 :   split_edges_for_insertion ();
    4709       987733 :   scev_initialize ();
    4710       987733 :   calculate_dominance_info (CDI_DOMINATORS);
    4711              : 
    4712       987733 :   vn_driver *vn = new vn_driver (fun, true, false, true, VN_WALK);
    4713              : 
    4714       987733 :   init_pre ();
    4715              : 
    4716       987733 :   vn_valueize = pre_valueize;
    4717              : 
    4718              :   /* Insert can get quite slow on an incredibly large number of basic
    4719              :      blocks due to some quadratic behavior.  Until this behavior is
    4720              :      fixed, don't run it when he have an incredibly large number of
    4721              :      bb's.  If we aren't going to run insert, there is no point in
    4722              :      computing ANTIC, either, even though it's plenty fast nor do
    4723              :      we require AVAIL.  */
    4724       987733 :   if (n_basic_blocks_for_fn (fun) < 4000)
    4725              :     {
    4726       987726 :       compute_avail (fun);
    4727       987726 :       compute_antic ();
    4728       987726 :       insert ();
    4729              :     }
    4730              : 
    4731              :   /* Make sure to remove fake edges before committing our inserts.
    4732              :      This makes sure we don't end up with extra critical edges that
    4733              :      we would need to split.  */
    4734       987733 :   remove_fake_exit_edges ();
    4735       987733 :   gsi_commit_edge_inserts ();
    4736              : 
    4737              :   /* Eliminate folds statements which might (should not...) end up
    4738              :      not keeping virtual operands up-to-date.  */
    4739       987733 :   gcc_assert (!need_ssa_update_p (fun));
    4740              : 
    4741       987733 :   statistics_counter_event (fun, "Insertions", pre_stats.insertions);
    4742       987733 :   statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
    4743       987733 :   statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
    4744       987733 :   statistics_counter_event (fun, "New PHIs", pre_stats.phis);
    4745              : 
    4746       987733 :   todo |= vn->finalize (inserted_exprs);
    4747              : 
    4748       987733 :   vn_valueize = NULL;
    4749              : 
    4750       987733 :   fini_pre ();
    4751              : 
    4752       987733 :   scev_finalize ();
    4753       987733 :   loop_optimizer_finalize ();
    4754              : 
    4755              :   /* Perform a CFG cleanup before we run simple_dce_from_worklist since
    4756              :      unreachable code regions will have not up-to-date SSA form which
    4757              :      confuses it.  */
    4758       987733 :   bool need_crit_edge_split = false;
    4759       987733 :   if (todo & TODO_cleanup_cfg)
    4760              :     {
    4761       141520 :       cleanup_tree_cfg ();
    4762       141520 :       need_crit_edge_split = true;
    4763              :     }
    4764              : 
    4765              :   /* Because we don't follow exactly the standard PRE algorithm, and decide not
    4766              :      to insert PHI nodes sometimes, and because value numbering of casts isn't
    4767              :      perfect, we sometimes end up inserting dead code.   This simple DCE-like
    4768              :      pass removes any insertions we made that weren't actually used.  */
    4769       987733 :   simple_dce_from_worklist (inserted_exprs);
    4770       987733 :   BITMAP_FREE (inserted_exprs);
    4771              : 
    4772              :   /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
    4773              :      case we can merge the block with the remaining predecessor of the block.
    4774              :      It should either:
    4775              :      - call merge_blocks after each tail merge iteration
    4776              :      - call merge_blocks after all tail merge iterations
    4777              :      - mark TODO_cleanup_cfg when necessary.  */
    4778       987733 :   todo |= tail_merge_optimize (need_crit_edge_split);
    4779              : 
    4780       987733 :   delete vn;
    4781              : 
    4782              :   /* Tail merging invalidates the virtual SSA web, together with
    4783              :      cfg-cleanup opportunities exposed by PRE this will wreck the
    4784              :      SSA updating machinery.  So make sure to run update-ssa
    4785              :      manually, before eventually scheduling cfg-cleanup as part of
    4786              :      the todo.  */
    4787       987733 :   update_ssa (TODO_update_ssa_only_virtuals);
    4788              : 
    4789       987733 :   return todo;
    4790              : }
    4791              : 
    4792              : } // anon namespace
    4793              : 
    4794              : gimple_opt_pass *
    4795       302985 : make_pass_pre (gcc::context *ctxt)
    4796              : {
    4797       302985 :   return new pass_pre (ctxt);
    4798              : }
        

Generated by: LCOV version 2.6-beta

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