LCOV - code coverage report
Current view: top level - gcc - tree-ssa-sccvn.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.7 % 4632 4432
Test Date: 2026-06-20 15:32:29 Functions: 98.4 % 124 122
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* SCC value numbering for trees
       2              :    Copyright (C) 2006-2026 Free Software Foundation, Inc.
       3              :    Contributed by Daniel Berlin <dan@dberlin.org>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify
       8              : it under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful,
      13              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15              : GNU General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "backend.h"
      25              : #include "rtl.h"
      26              : #include "tree.h"
      27              : #include "gimple.h"
      28              : #include "ssa.h"
      29              : #include "expmed.h"
      30              : #include "insn-config.h"
      31              : #include "memmodel.h"
      32              : #include "emit-rtl.h"
      33              : #include "cgraph.h"
      34              : #include "gimple-pretty-print.h"
      35              : #include "splay-tree-utils.h"
      36              : #include "alias.h"
      37              : #include "fold-const.h"
      38              : #include "stor-layout.h"
      39              : #include "cfganal.h"
      40              : #include "tree-inline.h"
      41              : #include "internal-fn.h"
      42              : #include "gimple-iterator.h"
      43              : #include "gimple-fold.h"
      44              : #include "tree-eh.h"
      45              : #include "flags.h"
      46              : #include "dojump.h"
      47              : #include "explow.h"
      48              : #include "calls.h"
      49              : #include "varasm.h"
      50              : #include "stmt.h"
      51              : #include "expr.h"
      52              : #include "tree-dfa.h"
      53              : #include "tree-ssa.h"
      54              : #include "dumpfile.h"
      55              : #include "cfgloop.h"
      56              : #include "tree-ssa-propagate.h"
      57              : #include "tree-cfg.h"
      58              : #include "domwalk.h"
      59              : #include "gimple-match.h"
      60              : #include "stringpool.h"
      61              : #include "attribs.h"
      62              : #include "tree-pass.h"
      63              : #include "statistics.h"
      64              : #include "langhooks.h"
      65              : #include "ipa-utils.h"
      66              : #include "dbgcnt.h"
      67              : #include "tree-cfgcleanup.h"
      68              : #include "tree-ssa-loop.h"
      69              : #include "tree-scalar-evolution.h"
      70              : #include "tree-ssa-loop-niter.h"
      71              : #include "builtins.h"
      72              : #include "fold-const-call.h"
      73              : #include "ipa-modref-tree.h"
      74              : #include "ipa-modref.h"
      75              : #include "tree-ssa-sccvn.h"
      76              : #include "alloc-pool.h"
      77              : #include "symbol-summary.h"
      78              : #include "sreal.h"
      79              : #include "ipa-cp.h"
      80              : #include "ipa-prop.h"
      81              : #include "target.h"
      82              : 
      83              : /* This algorithm is based on the SCC algorithm presented by Keith
      84              :    Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
      85              :    (http://citeseer.ist.psu.edu/41805.html).  In
      86              :    straight line code, it is equivalent to a regular hash based value
      87              :    numbering that is performed in reverse postorder.
      88              : 
      89              :    For code with cycles, there are two alternatives, both of which
      90              :    require keeping the hashtables separate from the actual list of
      91              :    value numbers for SSA names.
      92              : 
      93              :    1. Iterate value numbering in an RPO walk of the blocks, removing
      94              :    all the entries from the hashtable after each iteration (but
      95              :    keeping the SSA name->value number mapping between iterations).
      96              :    Iterate until it does not change.
      97              : 
      98              :    2. Perform value numbering as part of an SCC walk on the SSA graph,
      99              :    iterating only the cycles in the SSA graph until they do not change
     100              :    (using a separate, optimistic hashtable for value numbering the SCC
     101              :    operands).
     102              : 
     103              :    The second is not just faster in practice (because most SSA graph
     104              :    cycles do not involve all the variables in the graph), it also has
     105              :    some nice properties.
     106              : 
     107              :    One of these nice properties is that when we pop an SCC off the
     108              :    stack, we are guaranteed to have processed all the operands coming from
     109              :    *outside of that SCC*, so we do not need to do anything special to
     110              :    ensure they have value numbers.
     111              : 
     112              :    Another nice property is that the SCC walk is done as part of a DFS
     113              :    of the SSA graph, which makes it easy to perform combining and
     114              :    simplifying operations at the same time.
     115              : 
     116              :    The code below is deliberately written in a way that makes it easy
     117              :    to separate the SCC walk from the other work it does.
     118              : 
     119              :    In order to propagate constants through the code, we track which
     120              :    expressions contain constants, and use those while folding.  In
     121              :    theory, we could also track expressions whose value numbers are
     122              :    replaced, in case we end up folding based on expression
     123              :    identities.
     124              : 
     125              :    In order to value number memory, we assign value numbers to vuses.
     126              :    This enables us to note that, for example, stores to the same
     127              :    address of the same value from the same starting memory states are
     128              :    equivalent.
     129              :    TODO:
     130              : 
     131              :    1. We can iterate only the changing portions of the SCC's, but
     132              :    I have not seen an SCC big enough for this to be a win.
     133              :    2. If you differentiate between phi nodes for loops and phi nodes
     134              :    for if-then-else, you can properly consider phi nodes in different
     135              :    blocks for equivalence.
     136              :    3. We could value number vuses in more cases, particularly, whole
     137              :    structure copies.
     138              : */
     139              : 
     140              : /* There's no BB_EXECUTABLE but we can use BB_VISITED.  */
     141              : #define BB_EXECUTABLE BB_VISITED
     142              : 
     143              : static vn_lookup_kind default_vn_walk_kind;
     144              : 
     145              : /* vn_nary_op hashtable helpers.  */
     146              : 
     147              : struct vn_nary_op_hasher : nofree_ptr_hash <vn_nary_op_s>
     148              : {
     149              :   typedef vn_nary_op_s *compare_type;
     150              :   static inline hashval_t hash (const vn_nary_op_s *);
     151              :   static inline bool equal (const vn_nary_op_s *, const vn_nary_op_s *);
     152              : };
     153              : 
     154              : /* Return the computed hashcode for nary operation P1.  */
     155              : 
     156              : inline hashval_t
     157    762145846 : vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
     158              : {
     159    762145846 :   return vno1->hashcode;
     160              : }
     161              : 
     162              : /* Compare nary operations P1 and P2 and return true if they are
     163              :    equivalent.  */
     164              : 
     165              : inline bool
     166    966087124 : vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
     167              : {
     168    966087124 :   return vno1 == vno2 || vn_nary_op_eq (vno1, vno2);
     169              : }
     170              : 
     171              : typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type;
     172              : typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
     173              : 
     174              : 
     175              : /* vn_phi hashtable helpers.  */
     176              : 
     177              : static int
     178              : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
     179              : 
     180              : struct vn_phi_hasher : nofree_ptr_hash <vn_phi_s>
     181              : {
     182              :   static inline hashval_t hash (const vn_phi_s *);
     183              :   static inline bool equal (const vn_phi_s *, const vn_phi_s *);
     184              : };
     185              : 
     186              : /* Return the computed hashcode for phi operation P1.  */
     187              : 
     188              : inline hashval_t
     189     25425378 : vn_phi_hasher::hash (const vn_phi_s *vp1)
     190              : {
     191     25425378 :   return vp1->hashcode;
     192              : }
     193              : 
     194              : /* Compare two phi entries for equality, ignoring VN_TOP arguments.  */
     195              : 
     196              : inline bool
     197     46163068 : vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
     198              : {
     199     46163068 :   return vp1 == vp2 || vn_phi_eq (vp1, vp2);
     200              : }
     201              : 
     202              : typedef hash_table<vn_phi_hasher> vn_phi_table_type;
     203              : typedef vn_phi_table_type::iterator vn_phi_iterator_type;
     204              : 
     205              : 
     206              : /* Compare two reference operands P1 and P2 for equality.  Return true if
     207              :    they are equal, and false otherwise.  */
     208              : 
     209              : static int
     210     25201163 : vn_reference_op_eq (const void *p1, const void *p2)
     211              : {
     212     25201163 :   const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
     213     25201163 :   const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
     214              : 
     215     25201163 :   return (vro1->opcode == vro2->opcode
     216              :           /* We do not care for differences in type qualification.  */
     217     25199062 :           && (vro1->type == vro2->type
     218      1157997 :               || (vro1->type && vro2->type
     219      1157997 :                   && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
     220      1157997 :                                          TYPE_MAIN_VARIANT (vro2->type))))
     221     24216076 :           && expressions_equal_p (vro1->op0, vro2->op0)
     222     24179478 :           && expressions_equal_p (vro1->op1, vro2->op1)
     223     24179478 :           && expressions_equal_p (vro1->op2, vro2->op2)
     224     49380641 :           && (vro1->opcode != CALL_EXPR || vro1->clique == vro2->clique));
     225              : }
     226              : 
     227              : /* Free a reference operation structure VP.  */
     228              : 
     229              : static inline void
     230            0 : free_reference (vn_reference_s *vr)
     231              : {
     232            0 :   vr->operands.release ();
     233              : }
     234              : 
     235              : 
     236              : /* vn_reference hashtable helpers.  */
     237              : 
     238              : struct vn_reference_hasher : nofree_ptr_hash <vn_reference_s>
     239              : {
     240              :   static inline hashval_t hash (const vn_reference_s *);
     241              :   static inline bool equal (const vn_reference_s *, const vn_reference_s *);
     242              : };
     243              : 
     244              : /* Return the hashcode for a given reference operation P1.  */
     245              : 
     246              : inline hashval_t
     247   3781138293 : vn_reference_hasher::hash (const vn_reference_s *vr1)
     248              : {
     249   3781138293 :   return vr1->hashcode;
     250              : }
     251              : 
     252              : inline bool
     253   4495753560 : vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
     254              : {
     255   4495753560 :   return v == c || vn_reference_eq (v, c);
     256              : }
     257              : 
     258              : typedef hash_table<vn_reference_hasher> vn_reference_table_type;
     259              : typedef vn_reference_table_type::iterator vn_reference_iterator_type;
     260              : 
     261              : /* Pretty-print OPS to OUTFILE.  */
     262              : 
     263              : void
     264          287 : print_vn_reference_ops (FILE *outfile, const vec<vn_reference_op_s> ops)
     265              : {
     266          287 :   vn_reference_op_t vro;
     267          287 :   unsigned int i;
     268          287 :   fprintf (outfile, "{");
     269         1304 :   for (i = 0; ops.iterate (i, &vro); i++)
     270              :     {
     271         1017 :       bool closebrace = false;
     272         1017 :       if (vro->opcode != SSA_NAME
     273          803 :           && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
     274              :         {
     275          803 :           fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
     276          803 :           if (vro->op0 || vro->opcode == CALL_EXPR)
     277              :             {
     278          803 :               fprintf (outfile, "<");
     279          803 :               closebrace = true;
     280              :             }
     281              :         }
     282         1017 :       if (vro->opcode == MEM_REF || vro->opcode == TARGET_MEM_REF)
     283          275 :         fprintf (outfile, "(A%d)", TYPE_ALIGN (vro->type));
     284         1017 :       if (vro->op0 || vro->opcode == CALL_EXPR)
     285              :         {
     286         1017 :           if (!vro->op0)
     287            0 :             fprintf (outfile, internal_fn_name ((internal_fn)vro->clique));
     288              :           else
     289              :             {
     290         1017 :               if (vro->opcode == MEM_REF || vro->opcode == TARGET_MEM_REF)
     291              :                 {
     292          275 :                   fprintf (outfile, "(");
     293          275 :                   print_generic_expr (outfile, TREE_TYPE (vro->op0));
     294          275 :                   fprintf (outfile, ")");
     295              :                 }
     296         1017 :               print_generic_expr (outfile, vro->op0);
     297              :             }
     298         1017 :           if (vro->op1)
     299              :             {
     300          185 :               fprintf (outfile, ",");
     301          185 :               print_generic_expr (outfile, vro->op1);
     302              :             }
     303         1017 :           if (vro->op2)
     304              :             {
     305          185 :               fprintf (outfile, ",");
     306          185 :               print_generic_expr (outfile, vro->op2);
     307              :             }
     308              :         }
     309         1017 :       if (closebrace)
     310          803 :         fprintf (outfile, ">");
     311         1017 :       if (i != ops.length () - 1)
     312          730 :         fprintf (outfile, ",");
     313              :     }
     314          287 :   fprintf (outfile, "}");
     315          287 : }
     316              : 
     317              : DEBUG_FUNCTION void
     318            0 : debug_vn_reference_ops (const vec<vn_reference_op_s> ops)
     319              : {
     320            0 :   print_vn_reference_ops (stderr, ops);
     321            0 :   fputc ('\n', stderr);
     322            0 : }
     323              : 
     324              : /* The set of VN hashtables.  */
     325              : 
     326              : typedef struct vn_tables_s
     327              : {
     328              :   vn_nary_op_table_type *nary;
     329              :   vn_phi_table_type *phis;
     330              :   vn_reference_table_type *references;
     331              : } *vn_tables_t;
     332              : 
     333              : 
     334              : /* vn_constant hashtable helpers.  */
     335              : 
     336              : struct vn_constant_hasher : free_ptr_hash <vn_constant_s>
     337              : {
     338              :   static inline hashval_t hash (const vn_constant_s *);
     339              :   static inline bool equal (const vn_constant_s *, const vn_constant_s *);
     340              : };
     341              : 
     342              : /* Hash table hash function for vn_constant_t.  */
     343              : 
     344              : inline hashval_t
     345     12129957 : vn_constant_hasher::hash (const vn_constant_s *vc1)
     346              : {
     347     12129957 :   return vc1->hashcode;
     348              : }
     349              : 
     350              : /* Hash table equality function for vn_constant_t.  */
     351              : 
     352              : inline bool
     353     14588231 : vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
     354              : {
     355     14588231 :   if (vc1->hashcode != vc2->hashcode)
     356              :     return false;
     357              : 
     358      2183391 :   return vn_constant_eq_with_type (vc1->constant, vc2->constant);
     359              : }
     360              : 
     361              : static hash_table<vn_constant_hasher> *constant_to_value_id;
     362              : 
     363              : 
     364              : /* Obstack we allocate the vn-tables elements from.  */
     365              : static obstack vn_tables_obstack;
     366              : /* Special obstack we never unwind.  */
     367              : static obstack vn_tables_insert_obstack;
     368              : 
     369              : static vn_reference_t last_inserted_ref;
     370              : static vn_phi_t last_inserted_phi;
     371              : static vn_nary_op_t last_inserted_nary;
     372              : static vn_ssa_aux_t last_pushed_avail;
     373              : 
     374              : /* Valid hashtables storing information we have proven to be
     375              :    correct.  */
     376              : static vn_tables_t valid_info;
     377              : 
     378              : /* Global RPO state for access from hooks.  */
     379              : static class eliminate_dom_walker *rpo_avail;
     380              : basic_block vn_context_bb;
     381              : int *vn_bb_to_rpo;
     382              : 
     383              : 
     384              : /* Valueization hook for simplify_replace_tree.  Valueize NAME if it is
     385              :    an SSA name, otherwise just return it.  */
     386              : tree (*vn_valueize) (tree);
     387              : static tree
     388        84493 : vn_valueize_for_srt (tree t, void* context ATTRIBUTE_UNUSED)
     389              : {
     390        84493 :   basic_block saved_vn_context_bb = vn_context_bb;
     391              :   /* Look for sth available at the definition block of the argument.
     392              :      This avoids inconsistencies between availability there which
     393              :      decides if the stmt can be removed and availability at the
     394              :      use site.  The SSA property ensures that things available
     395              :      at the definition are also available at uses.  */
     396        84493 :   if (!SSA_NAME_IS_DEFAULT_DEF (t))
     397        80732 :     vn_context_bb = gimple_bb (SSA_NAME_DEF_STMT (t));
     398        84493 :   tree res = vn_valueize (t);
     399        84493 :   vn_context_bb = saved_vn_context_bb;
     400        84493 :   return res;
     401              : }
     402              : 
     403              : 
     404              : /* This represents the top of the VN lattice, which is the universal
     405              :    value.  */
     406              : 
     407              : tree VN_TOP;
     408              : 
     409              : /* Unique counter for our value ids.  */
     410              : 
     411              : static unsigned int next_value_id;
     412              : static int next_constant_value_id;
     413              : 
     414              : 
     415              : /* Table of vn_ssa_aux_t's, one per ssa_name.  The vn_ssa_aux_t objects
     416              :    are allocated on an obstack for locality reasons, and to free them
     417              :    without looping over the vec.  */
     418              : 
     419              : struct vn_ssa_aux_hasher : typed_noop_remove <vn_ssa_aux_t>
     420              : {
     421              :   typedef vn_ssa_aux_t value_type;
     422              :   typedef tree compare_type;
     423              :   static inline hashval_t hash (const value_type &);
     424              :   static inline bool equal (const value_type &, const compare_type &);
     425              :   static inline void mark_deleted (value_type &) {}
     426              :   static const bool empty_zero_p = true;
     427            0 :   static inline void mark_empty (value_type &e) { e = NULL; }
     428              :   static inline bool is_deleted (value_type &) { return false; }
     429  >13218*10^7 :   static inline bool is_empty (value_type &e) { return e == NULL; }
     430              : };
     431              : 
     432              : hashval_t
     433  43480878550 : vn_ssa_aux_hasher::hash (const value_type &entry)
     434              : {
     435  43480878550 :   return SSA_NAME_VERSION (entry->name);
     436              : }
     437              : 
     438              : bool
     439  49751100321 : vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
     440              : {
     441  49751100321 :   return name == entry->name;
     442              : }
     443              : 
     444              : static hash_table<vn_ssa_aux_hasher> *vn_ssa_aux_hash;
     445              : typedef hash_table<vn_ssa_aux_hasher>::iterator vn_ssa_aux_iterator_type;
     446              : static struct obstack vn_ssa_aux_obstack;
     447              : 
     448              : static vn_nary_op_t vn_nary_op_insert_stmt (gimple *, tree);
     449              : static vn_nary_op_t vn_nary_op_insert_into (vn_nary_op_t,
     450              :                                             vn_nary_op_table_type *);
     451              : static void init_vn_nary_op_from_pieces (vn_nary_op_t, unsigned int,
     452              :                                          enum tree_code, tree, tree *);
     453              : static tree vn_lookup_simplify_result (gimple_match_op *);
     454              : static vn_reference_t vn_reference_lookup_or_insert_for_pieces
     455              :           (tree, alias_set_type, alias_set_type, poly_int64, poly_int64, tree,
     456              :            vec<vn_reference_op_s, va_heap>, tree);
     457              : 
     458              : /* Return whether there is value numbering information for a given SSA name.  */
     459              : 
     460              : bool
     461      5063758 : has_VN_INFO (tree name)
     462              : {
     463      5063758 :   return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
     464              : }
     465              : 
     466              : vn_ssa_aux_t
     467   3749634101 : VN_INFO (tree name)
     468              : {
     469   3749634101 :   vn_ssa_aux_t *res
     470   3749634101 :     = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
     471              :                                             INSERT);
     472   3749634101 :   if (*res != NULL)
     473              :     return *res;
     474              : 
     475    170245011 :   vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
     476    170245011 :   memset (newinfo, 0, sizeof (struct vn_ssa_aux));
     477    170245011 :   newinfo->name = name;
     478    170245011 :   newinfo->valnum = VN_TOP;
     479              :   /* We are using the visited flag to handle uses with defs not within the
     480              :      region being value-numbered.  */
     481    170245011 :   newinfo->visited = false;
     482              : 
     483              :   /* Given we create the VN_INFOs on-demand now we have to do initialization
     484              :      different than VN_TOP here.  */
     485    170245011 :   if (SSA_NAME_IS_DEFAULT_DEF (name))
     486      9174697 :     switch (TREE_CODE (SSA_NAME_VAR (name)))
     487              :       {
     488      1652824 :       case VAR_DECL:
     489              :         /* All undefined vars are VARYING.  */
     490      1652824 :         newinfo->valnum = name;
     491      1652824 :         newinfo->visited = true;
     492      1652824 :         break;
     493              : 
     494      7463859 :       case PARM_DECL:
     495              :         /* Parameters are VARYING but we can record a condition
     496              :            if we know it is a non-NULL pointer.  */
     497      7463859 :         newinfo->visited = true;
     498      7463859 :         newinfo->valnum = name;
     499     11485409 :         if (POINTER_TYPE_P (TREE_TYPE (name))
     500      8554870 :             && nonnull_arg_p (SSA_NAME_VAR (name)))
     501              :           {
     502      2235877 :             tree ops[2];
     503      2235877 :             ops[0] = name;
     504      2235877 :             ops[1] = build_int_cst (TREE_TYPE (name), 0);
     505      2235877 :             vn_nary_op_t nary;
     506              :             /* Allocate from non-unwinding stack.  */
     507      2235877 :             nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
     508      2235877 :             init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
     509              :                                          boolean_type_node, ops);
     510      2235877 :             nary->predicated_values = 0;
     511      2235877 :             nary->u.result = boolean_true_node;
     512      2235877 :             vn_nary_op_insert_into (nary, valid_info->nary);
     513      2235877 :             gcc_assert (nary->unwind_to == NULL);
     514              :             /* Also do not link it into the undo chain.  */
     515      2235877 :             last_inserted_nary = nary->next;
     516      2235877 :             nary->next = (vn_nary_op_t)(void *)-1;
     517      2235877 :             nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
     518      2235877 :             init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
     519              :                                          boolean_type_node, ops);
     520      2235877 :             nary->predicated_values = 0;
     521      2235877 :             nary->u.result = boolean_false_node;
     522      2235877 :             vn_nary_op_insert_into (nary, valid_info->nary);
     523      2235877 :             gcc_assert (nary->unwind_to == NULL);
     524      2235877 :             last_inserted_nary = nary->next;
     525      2235877 :             nary->next = (vn_nary_op_t)(void *)-1;
     526      2235877 :             if (dump_file && (dump_flags & TDF_DETAILS))
     527              :               {
     528           38 :                 fprintf (dump_file, "Recording ");
     529           38 :                 print_generic_expr (dump_file, name, TDF_SLIM);
     530           38 :                 fprintf (dump_file, " != 0\n");
     531              :               }
     532              :           }
     533              :         break;
     534              : 
     535        58014 :       case RESULT_DECL:
     536              :         /* If the result is passed by invisible reference the default
     537              :            def is initialized, otherwise it's uninitialized.  Still
     538              :            undefined is varying.  */
     539        58014 :         newinfo->visited = true;
     540        58014 :         newinfo->valnum = name;
     541        58014 :         break;
     542              : 
     543            0 :       default:
     544            0 :         gcc_unreachable ();
     545              :       }
     546              :   return newinfo;
     547              : }
     548              : 
     549              : /* Return the SSA value of X.  */
     550              : 
     551              : inline tree
     552   3415230900 : SSA_VAL (tree x, bool *visited = NULL)
     553              : {
     554   3415230900 :   vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
     555   3415230900 :   if (visited)
     556   1388207063 :     *visited = tem && tem->visited;
     557   3415230900 :   return tem && tem->visited ? tem->valnum : x;
     558              : }
     559              : 
     560              : /* Return the SSA value of the VUSE x, supporting released VDEFs
     561              :    during elimination which will value-number the VDEF to the
     562              :    associated VUSE (but not substitute in the whole lattice).  */
     563              : 
     564              : static inline tree
     565   1264353365 : vuse_ssa_val (tree x)
     566              : {
     567   1264353365 :   if (!x)
     568              :     return NULL_TREE;
     569              : 
     570   1261070353 :   do
     571              :     {
     572   1261070353 :       x = SSA_VAL (x);
     573   1261070353 :       gcc_assert (x != VN_TOP);
     574              :     }
     575   1261070353 :   while (SSA_NAME_IN_FREE_LIST (x));
     576              : 
     577              :   return x;
     578              : }
     579              : 
     580              : /* Similar to the above but used as callback for walk_non_aliased_vuses
     581              :    and thus should stop at unvisited VUSE to not walk across region
     582              :    boundaries.  */
     583              : 
     584              : static tree
     585   1070797053 : vuse_valueize (tree vuse)
     586              : {
     587   1070797053 :   do
     588              :     {
     589   1070797053 :       bool visited;
     590   1070797053 :       vuse = SSA_VAL (vuse, &visited);
     591   1070797053 :       if (!visited)
     592     15905738 :         return NULL_TREE;
     593   1054891315 :       gcc_assert (vuse != VN_TOP);
     594              :     }
     595   1054891315 :   while (SSA_NAME_IN_FREE_LIST (vuse));
     596              :   return vuse;
     597              : }
     598              : 
     599              : 
     600              : /* Return the vn_kind the expression computed by the stmt should be
     601              :    associated with.  */
     602              : 
     603              : enum vn_kind
     604    101267986 : vn_get_stmt_kind (gimple *stmt)
     605              : {
     606    101267986 :   switch (gimple_code (stmt))
     607              :     {
     608              :     case GIMPLE_CALL:
     609              :       return VN_REFERENCE;
     610              :     case GIMPLE_PHI:
     611              :       return VN_PHI;
     612    101267986 :     case GIMPLE_ASSIGN:
     613    101267986 :       {
     614    101267986 :         enum tree_code code = gimple_assign_rhs_code (stmt);
     615    101267986 :         tree rhs1 = gimple_assign_rhs1 (stmt);
     616    101267986 :         switch (get_gimple_rhs_class (code))
     617              :           {
     618              :           case GIMPLE_UNARY_RHS:
     619              :           case GIMPLE_BINARY_RHS:
     620              :           case GIMPLE_TERNARY_RHS:
     621              :             return VN_NARY;
     622     47236354 :           case GIMPLE_SINGLE_RHS:
     623     47236354 :             switch (TREE_CODE_CLASS (code))
     624              :               {
     625     35564727 :               case tcc_reference:
     626              :                 /* VOP-less references can go through unary case.  */
     627     35564727 :                 if ((code == REALPART_EXPR
     628              :                      || code == IMAGPART_EXPR
     629     35564727 :                      || code == VIEW_CONVERT_EXPR
     630     35564727 :                      || code == BIT_FIELD_REF)
     631     35564727 :                     && (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME
     632       636750 :                         || is_gimple_min_invariant (TREE_OPERAND (rhs1, 0))))
     633      2022434 :                   return VN_NARY;
     634              : 
     635              :                 /* Fallthrough.  */
     636              :               case tcc_declaration:
     637              :                 return VN_REFERENCE;
     638              : 
     639              :               case tcc_constant:
     640              :                 return VN_CONSTANT;
     641              : 
     642      5810199 :               default:
     643      5810199 :                 if (code == ADDR_EXPR)
     644      3140832 :                   return (is_gimple_min_invariant (rhs1)
     645      3140832 :                           ? VN_CONSTANT : VN_REFERENCE);
     646      2669367 :                 else if (code == CONSTRUCTOR)
     647              :                   return VN_NARY;
     648              :                 return VN_NONE;
     649              :               }
     650              :           default:
     651              :             return VN_NONE;
     652              :           }
     653              :       }
     654              :     default:
     655              :       return VN_NONE;
     656              :     }
     657              : }
     658              : 
     659              : /* Lookup a value id for CONSTANT and return it.  If it does not
     660              :    exist returns 0.  */
     661              : 
     662              : unsigned int
     663            0 : get_constant_value_id (tree constant)
     664              : {
     665            0 :   vn_constant_s **slot;
     666            0 :   struct vn_constant_s vc;
     667              : 
     668            0 :   vc.hashcode = vn_hash_constant_with_type (constant);
     669            0 :   vc.constant = constant;
     670            0 :   slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
     671            0 :   if (slot)
     672            0 :     return (*slot)->value_id;
     673              :   return 0;
     674              : }
     675              : 
     676              : /* Lookup a value id for CONSTANT, and if it does not exist, create a
     677              :    new one and return it.  If it does exist, return it.  */
     678              : 
     679              : unsigned int
     680     28357181 : get_or_alloc_constant_value_id (tree constant)
     681              : {
     682     28357181 :   vn_constant_s **slot;
     683     28357181 :   struct vn_constant_s vc;
     684     28357181 :   vn_constant_t vcp;
     685              : 
     686              :   /* If the hashtable isn't initialized we're not running from PRE and thus
     687              :      do not need value-ids.  */
     688     28357181 :   if (!constant_to_value_id)
     689              :     return 0;
     690              : 
     691      4666724 :   vc.hashcode = vn_hash_constant_with_type (constant);
     692      4666724 :   vc.constant = constant;
     693      4666724 :   slot = constant_to_value_id->find_slot (&vc, INSERT);
     694      4666724 :   if (*slot)
     695      2165985 :     return (*slot)->value_id;
     696              : 
     697      2500739 :   vcp = XNEW (struct vn_constant_s);
     698      2500739 :   vcp->hashcode = vc.hashcode;
     699      2500739 :   vcp->constant = constant;
     700      2500739 :   vcp->value_id = get_next_constant_value_id ();
     701      2500739 :   *slot = vcp;
     702      2500739 :   return vcp->value_id;
     703              : }
     704              : 
     705              : /* Compute the hash for a reference operand VRO1.  */
     706              : 
     707              : static void
     708    134243793 : vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
     709              : {
     710    134243793 :   hstate.add_int (vro1->opcode);
     711    134243793 :   if (vro1->opcode == CALL_EXPR && !vro1->op0)
     712       544331 :     hstate.add_int (vro1->clique);
     713    134243793 :   if (vro1->op0)
     714    127973368 :     inchash::add_expr (vro1->op0, hstate);
     715    134243793 :   if (vro1->op1)
     716     12120965 :     inchash::add_expr (vro1->op1, hstate);
     717    134243793 :   if (vro1->op2)
     718     13794880 :     inchash::add_expr (vro1->op2, hstate);
     719    134243793 : }
     720              : 
     721              : /* Compute a hash for the reference operation VR1 and return it.  */
     722              : 
     723              : hashval_t
     724    200210892 : vn_reference_compute_hash (const vn_reference_t vr1)
     725              : {
     726    200210892 :   inchash::hash hstate;
     727    200210892 :   hashval_t result;
     728    200210892 :   int i;
     729    200210892 :   vn_reference_op_t vro;
     730    200210892 :   poly_offset_int off = -1;
     731    200210892 :   bool deref = false;
     732              : 
     733    814084156 :   FOR_EACH_VEC_ELT (vr1->operands, i, vro)
     734              :     {
     735    613873264 :       if (vro->opcode == MEM_REF)
     736              :         deref = true;
     737    424160498 :       else if (vro->opcode != ADDR_EXPR)
     738    296888509 :         deref = false;
     739    613873264 :       if (maybe_ne (vro->off, -1))
     740              :         {
     741    360388136 :           if (known_eq (off, -1))
     742    192117541 :             off = 0;
     743    613873264 :           off += vro->off;
     744              :         }
     745              :       else
     746              :         {
     747    253485128 :           if (maybe_ne (off, -1)
     748    253485128 :               && maybe_ne (off, 0))
     749    101709473 :             hstate.add_poly_hwi (off.force_shwi ());
     750    253485128 :           off = -1;
     751    253485128 :           if (deref
     752    119454477 :               && vro->opcode == ADDR_EXPR)
     753              :             {
     754    119241335 :               if (vro->op0)
     755              :                 {
     756    119241335 :                   tree op = TREE_OPERAND (vro->op0, 0);
     757    119241335 :                   hstate.add_int (TREE_CODE (op));
     758    119241335 :                   inchash::add_expr (op, hstate);
     759              :                 }
     760              :             }
     761              :           else
     762    134243793 :             vn_reference_op_compute_hash (vro, hstate);
     763              :         }
     764              :     }
     765              :   /* Do not hash vr1->offset or vr1->max_size, we want to get collisions
     766              :      to be able to identify compatible results.  */
     767    200210892 :   result = hstate.end ();
     768              :   /* ??? We would ICE later if we hash instead of adding that in. */
     769    200210892 :   if (vr1->vuse)
     770    195373107 :     result += SSA_NAME_VERSION (vr1->vuse);
     771              : 
     772    200210892 :   return result;
     773              : }
     774              : 
     775              : /* Return true if reference operations VR1 and VR2 are equivalent.  This
     776              :    means they have the same set of operands and vuses.  If LEXICAL
     777              :    is true then the full access path has to be the same.  */
     778              : 
     779              : bool
     780   4491192198 : vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2,
     781              :                  bool lexical)
     782              : {
     783   4491192198 :   unsigned i, j;
     784              : 
     785              :   /* Early out if this is not a hash collision.  */
     786   4491192198 :   if (vr1->hashcode != vr2->hashcode)
     787              :     return false;
     788              : 
     789              :   /* The VOP needs to be the same.  */
     790     17378068 :   if (vr1->vuse != vr2->vuse)
     791              :     return false;
     792              : 
     793              :   /* The offset/max_size used for the ao_ref during lookup has to be
     794              :      the same.  */
     795     17377666 :   if (maybe_ne (vr1->offset, vr2->offset)
     796     17377666 :       || maybe_ne (vr1->max_size, vr2->max_size))
     797              :     {
     798              :       /* But nothing known in the prevailing entry is OK to be used.  */
     799      6722831 :       if (maybe_ne (vr1->offset, 0) || known_size_p (vr1->max_size))
     800              :         return false;
     801              :     }
     802              : 
     803              :   /* If the operands are the same we are done.  */
     804     34667148 :   if (vr1->operands == vr2->operands)
     805              :     return true;
     806              : 
     807     17333574 :   if (!vr1->type || !vr2->type)
     808              :     {
     809       528221 :       if (vr1->type != vr2->type)
     810              :         return false;
     811              :     }
     812     16805353 :   else if (vr1->type == vr2->type)
     813              :     ;
     814      2092997 :   else if (COMPLETE_TYPE_P (vr1->type) != COMPLETE_TYPE_P (vr2->type)
     815      2092997 :            || (COMPLETE_TYPE_P (vr1->type)
     816      2092997 :                && !expressions_equal_p (TYPE_SIZE (vr1->type),
     817      2092997 :                                         TYPE_SIZE (vr2->type))))
     818       755225 :     return false;
     819      1337772 :   else if (vr1->operands[0].opcode == CALL_EXPR
     820      1337772 :            && !types_compatible_p (vr1->type, vr2->type))
     821              :     return false;
     822      1337772 :   else if (INTEGRAL_TYPE_P (vr1->type)
     823       536921 :            && INTEGRAL_TYPE_P (vr2->type))
     824              :     {
     825       497018 :       if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
     826              :         return false;
     827              :     }
     828       840754 :   else if (INTEGRAL_TYPE_P (vr1->type)
     829       840754 :            && (TYPE_PRECISION (vr1->type)
     830        39903 :                != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
     831              :     return false;
     832       840708 :   else if (INTEGRAL_TYPE_P (vr2->type)
     833       840708 :            && (TYPE_PRECISION (vr2->type)
     834         9152 :                != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
     835              :     return false;
     836        13066 :   else if (VECTOR_BOOLEAN_TYPE_P (vr1->type)
     837       840113 :            && VECTOR_BOOLEAN_TYPE_P (vr2->type))
     838              :     {
     839              :       /* Vector boolean types can have padding, verify we are dealing with
     840              :          the same number of elements, aka the precision of the types.
     841              :          For example, In most architecture the precision_size of vbool*_t
     842              :          types are calculated like below:
     843              :          precision_size = type_size * 8
     844              : 
     845              :          Unfortunately, the RISC-V will adjust the precision_size for the
     846              :          vbool*_t in order to align the ISA as below:
     847              :          type_size      = [1, 1, 1, 1,  2,  4,  8]
     848              :          precision_size = [1, 2, 4, 8, 16, 32, 64]
     849              : 
     850              :          Then the precision_size of RISC-V vbool*_t will not be the multiple
     851              :          of the type_size.  We take care of this case consolidated here.  */
     852            0 :       if (maybe_ne (TYPE_VECTOR_SUBPARTS (vr1->type),
     853            0 :                     TYPE_VECTOR_SUBPARTS (vr2->type)))
     854              :         return false;
     855              :     }
     856       840113 :   else if (TYPE_MODE (vr1->type) != TYPE_MODE (vr2->type)
     857       840113 :            && (!mode_can_transfer_bits (TYPE_MODE (vr1->type))
     858        44776 :                || !mode_can_transfer_bits (TYPE_MODE (vr2->type))))
     859         1037 :     return false;
     860              : 
     861              :   i = 0;
     862              :   j = 0;
     863     21388739 :   do
     864              :     {
     865     21388739 :       poly_offset_int off1 = 0, off2 = 0;
     866     21388739 :       vn_reference_op_t vro1, vro2;
     867     21388739 :       vn_reference_op_s tem1, tem2;
     868     21388739 :       bool deref1 = false, deref2 = false;
     869     21388739 :       bool reverse1 = false, reverse2 = false;
     870     69458047 :       for (; vr1->operands.iterate (i, &vro1); i++)
     871              :         {
     872     48069308 :           if (vro1->opcode == MEM_REF)
     873              :             deref1 = true;
     874              :           /* Do not look through a storage order barrier.  */
     875     32892165 :           else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
     876        70214 :             return false;
     877     48069308 :           reverse1 |= vro1->reverse;
     878     48069308 :           if (lexical || known_eq (vro1->off, -1))
     879              :             break;
     880     26680569 :           off1 += vro1->off;
     881              :         }
     882     48192782 :       for (; vr2->operands.iterate (j, &vro2); j++)
     883              :         {
     884     48192782 :           if (vro2->opcode == MEM_REF)
     885              :             deref2 = true;
     886              :           /* Do not look through a storage order barrier.  */
     887     32992183 :           else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
     888              :             return false;
     889     48192782 :           reverse2 |= vro2->reverse;
     890     48192782 :           if (lexical || known_eq (vro2->off, -1))
     891              :             break;
     892     26804043 :           off2 += vro2->off;
     893              :         }
     894     21388739 :       if (maybe_ne (off1, off2) || reverse1 != reverse2)
     895              :         return false;
     896     21388601 :       if (deref1 && vro1->opcode == ADDR_EXPR)
     897              :         {
     898      8041569 :           memset (&tem1, 0, sizeof (tem1));
     899      8041569 :           tem1.op0 = TREE_OPERAND (vro1->op0, 0);
     900      8041569 :           tem1.type = TREE_TYPE (tem1.op0);
     901      8041569 :           tem1.opcode = TREE_CODE (tem1.op0);
     902      8041569 :           vro1 = &tem1;
     903      8041569 :           deref1 = false;
     904              :         }
     905     21388601 :       if (deref2 && vro2->opcode == ADDR_EXPR)
     906              :         {
     907      8041579 :           memset (&tem2, 0, sizeof (tem2));
     908      8041579 :           tem2.op0 = TREE_OPERAND (vro2->op0, 0);
     909      8041579 :           tem2.type = TREE_TYPE (tem2.op0);
     910      8041579 :           tem2.opcode = TREE_CODE (tem2.op0);
     911      8041579 :           vro2 = &tem2;
     912      8041579 :           deref2 = false;
     913              :         }
     914     21388601 :       if (deref1 != deref2)
     915              :         return false;
     916     21333849 :       if (!vn_reference_op_eq (vro1, vro2))
     917              :         return false;
     918              :       /* Both alignment and alias set are not relevant for the produced
     919              :          value but need to be included when doing lexical comparison.
     920              :          We also need to make sure that the access path ends in an
     921              :          access of the same size as otherwise we might assume an access
     922              :          may not trap while in fact it might.  */
     923     21321877 :       if (lexical
     924      2185319 :           && (vro1->opcode == MEM_REF
     925      2185319 :               || vro1->opcode == TARGET_MEM_REF)
     926     22039640 :           && (TYPE_ALIGN (vro1->type) != TYPE_ALIGN (vro2->type)
     927       717556 :               || (TYPE_SIZE (vro1->type) != TYPE_SIZE (vro2->type)
     928            6 :                   && (! TYPE_SIZE (vro1->type)
     929            6 :                       || ! TYPE_SIZE (vro2->type)
     930            6 :                       || ! operand_equal_p (TYPE_SIZE (vro1->type),
     931            6 :                                             TYPE_SIZE (vro2->type))))
     932      2152650 :               || (get_deref_alias_set (vro1->opcode == MEM_REF
     933       717550 :                                        ? TREE_TYPE (vro1->op0)
     934            0 :                                        : TREE_TYPE (vro1->op2))
     935      1435100 :                   != get_deref_alias_set (vro2->opcode == MEM_REF
     936       717550 :                                           ? TREE_TYPE (vro2->op0)
     937            0 :                                           : TREE_TYPE (vro2->op2)))))
     938         3352 :         return false;
     939     21318525 :       ++j;
     940     21318525 :       ++i;
     941              :     }
     942     42637050 :   while (vr1->operands.length () != i
     943     63955575 :          || vr2->operands.length () != j);
     944              : 
     945              :   return true;
     946              : }
     947              : 
     948              : /* Copy the operations present in load/store REF into RESULT, a vector of
     949              :    vn_reference_op_s's.  */
     950              : 
     951              : void
     952    218851908 : copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
     953              : {
     954              :   /* For non-calls, store the information that makes up the address.  */
     955    218851908 :   tree orig = ref;
     956    758589364 :   while (ref)
     957              :     {
     958    539737456 :       vn_reference_op_s temp;
     959              : 
     960    539737456 :       memset (&temp, 0, sizeof (temp));
     961    539737456 :       temp.type = TREE_TYPE (ref);
     962    539737456 :       temp.opcode = TREE_CODE (ref);
     963    539737456 :       temp.off = -1;
     964              : 
     965    539737456 :       switch (temp.opcode)
     966              :         {
     967     14801309 :         case MODIFY_EXPR:
     968     14801309 :           temp.op0 = TREE_OPERAND (ref, 1);
     969     14801309 :           break;
     970          137 :         case WITH_SIZE_EXPR:
     971          137 :           temp.op0 = TREE_OPERAND (ref, 1);
     972          137 :           temp.off = 0;
     973          137 :           break;
     974    114941462 :         case MEM_REF:
     975              :           /* The base address gets its own vn_reference_op_s structure.  */
     976    114941462 :           temp.op0 = TREE_OPERAND (ref, 1);
     977    114941462 :           if (!mem_ref_offset (ref).to_shwi (&temp.off))
     978            0 :             temp.off = -1;
     979    114941462 :           temp.clique = MR_DEPENDENCE_CLIQUE (ref);
     980    114941462 :           temp.base = MR_DEPENDENCE_BASE (ref);
     981    114941462 :           temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
     982    114941462 :           break;
     983      2523442 :         case TARGET_MEM_REF:
     984              :           /* The base address gets its own vn_reference_op_s structure.  */
     985      2523442 :           temp.op0 = TMR_INDEX (ref);
     986      2523442 :           temp.op1 = TMR_STEP (ref);
     987      2523442 :           temp.op2 = TMR_OFFSET (ref);
     988      2523442 :           temp.clique = MR_DEPENDENCE_CLIQUE (ref);
     989      2523442 :           temp.base = MR_DEPENDENCE_BASE (ref);
     990      2523442 :           result->safe_push (temp);
     991      2523442 :           memset (&temp, 0, sizeof (temp));
     992      2523442 :           temp.type = NULL_TREE;
     993      2523442 :           temp.opcode = ERROR_MARK;
     994      2523442 :           temp.op0 = TMR_INDEX2 (ref);
     995      2523442 :           temp.off = -1;
     996      2523442 :           break;
     997       728891 :         case BIT_FIELD_REF:
     998              :           /* Record bits, position and storage order.  */
     999       728891 :           temp.op0 = TREE_OPERAND (ref, 1);
    1000       728891 :           temp.op1 = TREE_OPERAND (ref, 2);
    1001      1457084 :           if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
    1002          698 :             temp.off = -1;
    1003       728891 :           temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
    1004       728891 :           break;
    1005    143250848 :         case COMPONENT_REF:
    1006              :           /* The field decl is enough to unambiguously specify the field,
    1007              :              so use its type here.  */
    1008    143250848 :           temp.type = TREE_TYPE (TREE_OPERAND (ref, 1));
    1009    143250848 :           temp.op0 = TREE_OPERAND (ref, 1);
    1010    143250848 :           temp.op1 = TREE_OPERAND (ref, 2);
    1011    286499264 :           temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
    1012    286498999 :                           && TYPE_REVERSE_STORAGE_ORDER
    1013              :                                (TREE_TYPE (TREE_OPERAND (ref, 0))));
    1014    143250848 :           {
    1015    143250848 :             tree this_offset = component_ref_field_offset (ref);
    1016    143250848 :             if (this_offset
    1017    143250848 :                 && poly_int_tree_p (this_offset))
    1018              :               {
    1019    143248712 :                 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
    1020    143248712 :                 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
    1021              :                   {
    1022    142784607 :                     poly_offset_int off
    1023    142784607 :                       = (wi::to_poly_offset (this_offset)
    1024    142784607 :                          + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
    1025              :                     /* Prohibit value-numbering zero offset components
    1026              :                        of addresses the same before the pass folding
    1027              :                        __builtin_object_size had a chance to run.  Likewise
    1028              :                        for components of zero size at arbitrary offset.  */
    1029    142784607 :                     if (TREE_CODE (orig) != ADDR_EXPR
    1030      4678552 :                         || (TYPE_SIZE (temp.type)
    1031      4665550 :                             && integer_nonzerop (TYPE_SIZE (temp.type))
    1032      6034363 :                             && maybe_ne (off, 0))
    1033    145668680 :                         || (cfun->curr_properties & PROP_objsz))
    1034    141413624 :                       off.to_shwi (&temp.off);
    1035              :                   }
    1036              :               }
    1037              :           }
    1038              :           break;
    1039     38189997 :         case ARRAY_RANGE_REF:
    1040     38189997 :         case ARRAY_REF:
    1041     38189997 :           {
    1042     38189997 :             tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
    1043              :             /* Record index as operand.  */
    1044     38189997 :             temp.op0 = TREE_OPERAND (ref, 1);
    1045              :             /* Always record lower bounds and element size.  */
    1046     38189997 :             temp.op1 = array_ref_low_bound (ref);
    1047              :             /* But record element size in units of the type alignment.  */
    1048     38189997 :             temp.op2 = TREE_OPERAND (ref, 3);
    1049     38189997 :             temp.align = eltype->type_common.align;
    1050     38189997 :             if (! temp.op2)
    1051     37979701 :               temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
    1052              :                                      size_int (TYPE_ALIGN_UNIT (eltype)));
    1053              :             /* Prohibit value-numbering addresses of one-after-the-last
    1054              :                element ARRAY_REFs the same as addresses of other components
    1055              :                before the pass folding __builtin_object_size had a chance
    1056              :                to run.  */
    1057     38189997 :             bool avoid_oob = true;
    1058     38189997 :             if (TREE_CODE (orig) != ADDR_EXPR
    1059       470880 :                 || cfun->curr_properties & PROP_objsz)
    1060              :               avoid_oob = false;
    1061       222545 :             else if (poly_int_tree_p (temp.op0))
    1062              :               {
    1063        74374 :                 tree ub = array_ref_up_bound (ref);
    1064        74374 :                 if (ub
    1065        72736 :                     && poly_int_tree_p (ub)
    1066              :                     /* ???  The C frontend for T[0] uses [0:] and the
    1067              :                        C++ frontend [0:-1U].  See layout_type for how
    1068              :                        awkward this is.  */
    1069        64521 :                     && !integer_minus_onep (ub)
    1070       147110 :                     && known_le (wi::to_poly_offset (temp.op0),
    1071              :                                  wi::to_poly_offset (ub)))
    1072        63674 :                   avoid_oob = false;
    1073              :               }
    1074     38189997 :             if (poly_int_tree_p (temp.op0)
    1075     21888629 :                 && poly_int_tree_p (temp.op1)
    1076     21888601 :                 && TREE_CODE (temp.op2) == INTEGER_CST
    1077     60017990 :                 && !avoid_oob)
    1078              :               {
    1079     43636296 :                 poly_offset_int off = ((wi::to_poly_offset (temp.op0)
    1080     65454444 :                                         - wi::to_poly_offset (temp.op1))
    1081     43636296 :                                        * wi::to_offset (temp.op2)
    1082     21818148 :                                        * vn_ref_op_align_unit (&temp));
    1083     21818148 :                 off.to_shwi (&temp.off);
    1084              :               }
    1085     38189997 :             temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
    1086     38189997 :                             && TYPE_REVERSE_STORAGE_ORDER
    1087              :                                  (TREE_TYPE (TREE_OPERAND (ref, 0))));
    1088              :           }
    1089     38189997 :           break;
    1090     81138102 :         case VAR_DECL:
    1091     81138102 :           if (DECL_HARD_REGISTER (ref))
    1092              :             {
    1093        20325 :               temp.op0 = ref;
    1094        20325 :               break;
    1095              :             }
    1096              :           /* Fallthru.  */
    1097     84477522 :         case PARM_DECL:
    1098     84477522 :         case CONST_DECL:
    1099     84477522 :         case RESULT_DECL:
    1100              :           /* Canonicalize decls to MEM[&decl] which is what we end up with
    1101              :              when valueizing MEM[ptr] with ptr = &decl.  */
    1102     84477522 :           temp.opcode = MEM_REF;
    1103     84477522 :           temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
    1104     84477522 :           temp.off = 0;
    1105     84477522 :           result->safe_push (temp);
    1106     84477522 :           temp.opcode = ADDR_EXPR;
    1107     84477522 :           temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
    1108     84477522 :           temp.type = TREE_TYPE (temp.op0);
    1109     84477522 :           temp.off = -1;
    1110     84477522 :           break;
    1111     94113505 :         case STRING_CST:
    1112     94113505 :         case INTEGER_CST:
    1113     94113505 :         case POLY_INT_CST:
    1114     94113505 :         case COMPLEX_CST:
    1115     94113505 :         case VECTOR_CST:
    1116     94113505 :         case REAL_CST:
    1117     94113505 :         case FIXED_CST:
    1118     94113505 :         case CONSTRUCTOR:
    1119     94113505 :         case SSA_NAME:
    1120     94113505 :           temp.op0 = ref;
    1121     94113505 :           break;
    1122     44282313 :         case ADDR_EXPR:
    1123     44282313 :           if (is_gimple_min_invariant (ref))
    1124              :             {
    1125     40240556 :               temp.op0 = ref;
    1126     40240556 :               break;
    1127              :             }
    1128              :           break;
    1129              :           /* These are only interesting for their operands, their
    1130              :              existence, and their type.  They will never be the last
    1131              :              ref in the chain of references (IE they require an
    1132              :              operand), so we don't have to put anything
    1133              :              for op* as it will be handled by the iteration  */
    1134       484414 :         case REALPART_EXPR:
    1135       484414 :           temp.off = 0;
    1136       484414 :           break;
    1137      1434956 :         case VIEW_CONVERT_EXPR:
    1138      1434956 :           temp.off = 0;
    1139      1434956 :           temp.reverse = storage_order_barrier_p (ref);
    1140      1434956 :           break;
    1141       488335 :         case IMAGPART_EXPR:
    1142              :           /* This is only interesting for its constant offset.  */
    1143       488335 :           temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
    1144       488335 :           break;
    1145            0 :         default:
    1146            0 :           gcc_unreachable ();
    1147              :         }
    1148    539737456 :       result->safe_push (temp);
    1149              : 
    1150    539737456 :       if (REFERENCE_CLASS_P (ref)
    1151    237695111 :           || TREE_CODE (ref) == MODIFY_EXPR
    1152    222893802 :           || TREE_CODE (ref) == WITH_SIZE_EXPR
    1153    762631121 :           || (TREE_CODE (ref) == ADDR_EXPR
    1154     44282313 :               && !is_gimple_min_invariant (ref)))
    1155    320885548 :         ref = TREE_OPERAND (ref, 0);
    1156              :       else
    1157              :         ref = NULL_TREE;
    1158              :     }
    1159    218851908 : }
    1160              : 
    1161              : /* Build a alias-oracle reference abstraction in *REF from the vn_reference
    1162              :    operands in *OPS, the reference alias set SET and the reference type TYPE.
    1163              :    Return true if something useful was produced.  */
    1164              : 
    1165              : bool
    1166     14312216 : ao_ref_init_from_vn_reference (ao_ref *ref,
    1167              :                                alias_set_type set, alias_set_type base_set,
    1168              :                                tree type, const vec<vn_reference_op_s> &ops)
    1169              : {
    1170     14312216 :   unsigned i;
    1171     14312216 :   tree base = NULL_TREE;
    1172     14312216 :   tree *op0_p = &base;
    1173     14312216 :   poly_offset_int offset = 0;
    1174     14312216 :   poly_offset_int max_size;
    1175     14312216 :   poly_offset_int size = -1;
    1176     14312216 :   tree size_tree = NULL_TREE;
    1177              : 
    1178              :   /* We don't handle calls.  */
    1179     14312216 :   if (!type)
    1180              :     return false;
    1181              : 
    1182     14312216 :   machine_mode mode = TYPE_MODE (type);
    1183     14312216 :   if (mode == BLKmode)
    1184        58503 :     size_tree = TYPE_SIZE (type);
    1185              :   else
    1186     28507426 :     size = GET_MODE_BITSIZE (mode);
    1187     14253713 :   if (size_tree != NULL_TREE
    1188        58503 :       && poly_int_tree_p (size_tree))
    1189        58503 :     size = wi::to_poly_offset (size_tree);
    1190              : 
    1191              :   /* Lower the final access size from the outermost expression.  */
    1192     14312216 :   const_vn_reference_op_t cst_op = &ops[0];
    1193              :   /* Cast away constness for the sake of the const-unsafe
    1194              :      FOR_EACH_VEC_ELT().  */
    1195     14312216 :   vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
    1196     14312216 :   size_tree = NULL_TREE;
    1197     14312216 :   if (op->opcode == COMPONENT_REF)
    1198      4911339 :     size_tree = DECL_SIZE (op->op0);
    1199      9400877 :   else if (op->opcode == BIT_FIELD_REF)
    1200        60296 :     size_tree = op->op0;
    1201      4971635 :   if (size_tree != NULL_TREE
    1202      4971635 :       && poly_int_tree_p (size_tree)
    1203      9943270 :       && (!known_size_p (size)
    1204     14312216 :           || known_lt (wi::to_poly_offset (size_tree), size)))
    1205        37743 :     size = wi::to_poly_offset (size_tree);
    1206              : 
    1207              :   /* Initially, maxsize is the same as the accessed element size.
    1208              :      In the following it will only grow (or become -1).  */
    1209     14312216 :   max_size = size;
    1210              : 
    1211              :   /* Compute cumulative bit-offset for nested component-refs and array-refs,
    1212              :      and find the ultimate containing object.  */
    1213     54964963 :   FOR_EACH_VEC_ELT (ops, i, op)
    1214              :     {
    1215     40796791 :       switch (op->opcode)
    1216              :         {
    1217              :         case CALL_EXPR:
    1218              :           return false;
    1219              : 
    1220              :         /* Record the base objects.  */
    1221     13863033 :         case MEM_REF:
    1222     13863033 :           *op0_p = build2 (MEM_REF, op->type,
    1223              :                            NULL_TREE, op->op0);
    1224     13863033 :           MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
    1225     13863033 :           MR_DEPENDENCE_BASE (*op0_p) = op->base;
    1226     13863033 :           op0_p = &TREE_OPERAND (*op0_p, 0);
    1227     13863033 :           break;
    1228              : 
    1229       304611 :         case TARGET_MEM_REF:
    1230       913833 :           *op0_p = build5 (TARGET_MEM_REF, op->type,
    1231              :                            NULL_TREE, op->op2, op->op0,
    1232       304611 :                            op->op1, ops[i+1].op0);
    1233       304611 :           MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
    1234       304611 :           MR_DEPENDENCE_BASE (*op0_p) = op->base;
    1235       304611 :           op0_p = &TREE_OPERAND (*op0_p, 0);
    1236       304611 :           ++i;
    1237       304611 :           break;
    1238              : 
    1239              :         /* Unwrap some of the wrapped decls.  */
    1240      6522969 :         case ADDR_EXPR:
    1241              :           /* Apart from ADDR_EXPR arguments to MEM_REF.  */
    1242      6522969 :           if (base != NULL_TREE
    1243      6522968 :               && TREE_CODE (base) == MEM_REF
    1244      6488074 :               && op->op0
    1245     13011043 :               && DECL_P (TREE_OPERAND (op->op0, 0)))
    1246              :             {
    1247      6481039 :               const_vn_reference_op_t pop = &ops[i-1];
    1248      6481039 :               base = TREE_OPERAND (op->op0, 0);
    1249      6481039 :               if (known_eq (pop->off, -1))
    1250              :                 {
    1251           25 :                   max_size = -1;
    1252           25 :                   offset = 0;
    1253              :                 }
    1254              :               else
    1255     19443042 :                 offset += poly_offset_int (pop->off) * BITS_PER_UNIT;
    1256              :               op0_p = NULL;
    1257              :               break;
    1258              :             }
    1259              :           /* Fallthru.  */
    1260      7687133 :         case PARM_DECL:
    1261      7687133 :         case CONST_DECL:
    1262      7687133 :         case RESULT_DECL:
    1263              :           /* ???  We shouldn't see these, but un-canonicalize what
    1264              :              copy_reference_ops_from_ref does when visiting MEM_REF.  */
    1265      7687133 :         case VAR_DECL:
    1266              :           /* ???  And for this only have DECL_HARD_REGISTER.  */
    1267      7687133 :         case STRING_CST:
    1268              :           /* This can show up in ARRAY_REF bases.  */
    1269      7687133 :         case INTEGER_CST:
    1270      7687133 :         case SSA_NAME:
    1271      7687133 :           *op0_p = op->op0;
    1272      7687133 :           op0_p = NULL;
    1273      7687133 :           break;
    1274              : 
    1275              :         /* And now the usual component-reference style ops.  */
    1276        60296 :         case BIT_FIELD_REF:
    1277        60296 :           offset += wi::to_poly_offset (op->op1);
    1278        60296 :           break;
    1279              : 
    1280      8045746 :         case COMPONENT_REF:
    1281      8045746 :           {
    1282      8045746 :             tree field = op->op0;
    1283              :             /* We do not have a complete COMPONENT_REF tree here so we
    1284              :                cannot use component_ref_field_offset.  Do the interesting
    1285              :                parts manually.  */
    1286      8045746 :             tree this_offset = DECL_FIELD_OFFSET (field);
    1287              : 
    1288      8045746 :             if (op->op1 || !poly_int_tree_p (this_offset))
    1289          234 :               max_size = -1;
    1290              :             else
    1291              :               {
    1292      8045512 :                 poly_offset_int woffset = (wi::to_poly_offset (this_offset)
    1293      8045512 :                                            << LOG2_BITS_PER_UNIT);
    1294      8045512 :                 woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
    1295      8045512 :                 offset += woffset;
    1296              :               }
    1297              :             break;
    1298              :           }
    1299              : 
    1300      3008069 :         case ARRAY_RANGE_REF:
    1301      3008069 :         case ARRAY_REF:
    1302              :           /* Use the recorded constant offset.  */
    1303      3008069 :           if (maybe_eq (op->off, -1))
    1304      1189118 :             max_size = -1;
    1305              :           else
    1306      5456853 :             offset += poly_offset_int (op->off) * BITS_PER_UNIT;
    1307              :           break;
    1308              : 
    1309              :         case REALPART_EXPR:
    1310              :           break;
    1311              : 
    1312              :         case IMAGPART_EXPR:
    1313     40652747 :           offset += size;
    1314              :           break;
    1315              : 
    1316              :         case VIEW_CONVERT_EXPR:
    1317              :           break;
    1318              : 
    1319              :         case POLY_INT_CST:
    1320              :         case COMPLEX_CST:
    1321              :         case VECTOR_CST:
    1322              :         case REAL_CST:
    1323              :         case FIXED_CST:
    1324              :         case CONSTRUCTOR:
    1325              :           return false;
    1326              : 
    1327              :         default:
    1328              :           return false;
    1329              :         }
    1330              :     }
    1331              : 
    1332     14168172 :   if (base == NULL_TREE)
    1333              :     return false;
    1334              : 
    1335     14168172 :   ref->ref = NULL_TREE;
    1336     14168172 :   ref->base = base;
    1337     14168172 :   ref->ref_alias_set = set;
    1338     14168172 :   ref->base_alias_set = base_set;
    1339              :   /* We discount volatiles from value-numbering elsewhere.  */
    1340     14168172 :   ref->volatile_p = false;
    1341              : 
    1342     14168172 :   if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
    1343              :     {
    1344            0 :       ref->offset = 0;
    1345            0 :       ref->size = -1;
    1346            0 :       ref->max_size = -1;
    1347            0 :       return true;
    1348              :     }
    1349              : 
    1350     14168172 :   if (!offset.to_shwi (&ref->offset))
    1351              :     {
    1352           26 :       ref->offset = 0;
    1353           26 :       ref->max_size = -1;
    1354           26 :       return true;
    1355              :     }
    1356              : 
    1357     14168146 :   if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
    1358      1038331 :     ref->max_size = -1;
    1359              : 
    1360              :   return true;
    1361              : }
    1362              : 
    1363              : /* Copy the operations present in load/store/call REF into RESULT, a vector of
    1364              :    vn_reference_op_s's.  */
    1365              : 
    1366              : static void
    1367      9065646 : copy_reference_ops_from_call (gcall *call,
    1368              :                               vec<vn_reference_op_s> *result)
    1369              : {
    1370      9065646 :   vn_reference_op_s temp;
    1371      9065646 :   unsigned i;
    1372      9065646 :   tree lhs = gimple_call_lhs (call);
    1373      9065646 :   int lr;
    1374              : 
    1375              :   /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
    1376              :      different.  By adding the lhs here in the vector, we ensure that the
    1377              :      hashcode is different, guaranteeing a different value number.  */
    1378      9065646 :   if (lhs && TREE_CODE (lhs) != SSA_NAME)
    1379              :     {
    1380       435299 :       memset (&temp, 0, sizeof (temp));
    1381       435299 :       temp.opcode = MODIFY_EXPR;
    1382       435299 :       temp.type = TREE_TYPE (lhs);
    1383       435299 :       temp.op0 = lhs;
    1384       435299 :       temp.off = -1;
    1385       435299 :       result->safe_push (temp);
    1386              :     }
    1387              : 
    1388              :   /* Copy the type, opcode, function, static chain and EH region, if any.  */
    1389      9065646 :   memset (&temp, 0, sizeof (temp));
    1390      9065646 :   temp.type = gimple_call_fntype (call);
    1391      9065646 :   temp.opcode = CALL_EXPR;
    1392      9065646 :   temp.op0 = gimple_call_fn (call);
    1393      9065646 :   if (gimple_call_internal_p (call))
    1394       529625 :     temp.clique = gimple_call_internal_fn (call);
    1395      9065646 :   temp.op1 = gimple_call_chain (call);
    1396      9065646 :   if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
    1397       589053 :     temp.op2 = size_int (lr);
    1398      9065646 :   temp.off = -1;
    1399      9065646 :   result->safe_push (temp);
    1400              : 
    1401              :   /* Copy the call arguments.  As they can be references as well,
    1402              :      just chain them together.  */
    1403     26741472 :   for (i = 0; i < gimple_call_num_args (call); ++i)
    1404              :     {
    1405     17675826 :       tree callarg = gimple_call_arg (call, i);
    1406     17675826 :       copy_reference_ops_from_ref (callarg, result);
    1407              :     }
    1408      9065646 : }
    1409              : 
    1410              : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS.  Updates
    1411              :    *I_P to point to the last element of the replacement.  */
    1412              : static bool
    1413    125236254 : vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
    1414              :                             unsigned int *i_p)
    1415              : {
    1416    125236254 :   unsigned int i = *i_p;
    1417    125236254 :   vn_reference_op_t op = &(*ops)[i];
    1418    125236254 :   vn_reference_op_t mem_op = &(*ops)[i - 1];
    1419    125236254 :   tree addr_base;
    1420    125236254 :   poly_int64 addr_offset = 0;
    1421              : 
    1422              :   /* The only thing we have to do is from &OBJ.foo.bar add the offset
    1423              :      from .foo.bar to the preceding MEM_REF offset and replace the
    1424              :      address with &OBJ.  */
    1425    125236254 :   addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
    1426              :                                                &addr_offset, vn_valueize);
    1427    125236254 :   gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
    1428    125236254 :   if (addr_base != TREE_OPERAND (op->op0, 0))
    1429              :     {
    1430       644982 :       poly_offset_int off
    1431       644982 :         = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
    1432              :                                   SIGNED)
    1433       644982 :            + addr_offset);
    1434       644982 :       mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
    1435       644982 :       op->op0 = build_fold_addr_expr (addr_base);
    1436       644982 :       if (tree_fits_shwi_p (mem_op->op0))
    1437       644915 :         mem_op->off = tree_to_shwi (mem_op->op0);
    1438              :       else
    1439           67 :         mem_op->off = -1;
    1440       644982 :       return true;
    1441              :     }
    1442              :   return false;
    1443              : }
    1444              : 
    1445              : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS.  Updates
    1446              :    *I_P to point to the last element of the replacement.  */
    1447              : static bool
    1448     83720169 : vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
    1449              :                                      unsigned int *i_p)
    1450              : {
    1451     83720169 :   bool changed = false;
    1452     91311991 :   vn_reference_op_t op;
    1453              : 
    1454     91311991 :   do
    1455              :     {
    1456     91311991 :       unsigned int i = *i_p;
    1457     91311991 :       op = &(*ops)[i];
    1458     91311991 :       vn_reference_op_t mem_op = &(*ops)[i - 1];
    1459     91311991 :       gimple *def_stmt;
    1460     91311991 :       enum tree_code code;
    1461     91311991 :       poly_offset_int off;
    1462              : 
    1463     91311991 :       def_stmt = SSA_NAME_DEF_STMT (op->op0);
    1464     91311991 :       if (!is_gimple_assign (def_stmt))
    1465     83718218 :         return changed;
    1466              : 
    1467     37360470 :       code = gimple_assign_rhs_code (def_stmt);
    1468     37360470 :       if (code != ADDR_EXPR
    1469     37360470 :           && code != POINTER_PLUS_EXPR)
    1470              :         return changed;
    1471              : 
    1472     20050720 :       off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
    1473              : 
    1474              :       /* The only thing we have to do is from &OBJ.foo.bar add the offset
    1475              :          from .foo.bar to the preceding MEM_REF offset and replace the
    1476              :          address with &OBJ.  */
    1477     20050720 :       if (code == ADDR_EXPR)
    1478              :         {
    1479       934916 :           tree addr, addr_base;
    1480       934916 :           poly_int64 addr_offset;
    1481              : 
    1482       934916 :           addr = gimple_assign_rhs1 (def_stmt);
    1483       934916 :           addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr, 0),
    1484              :                                                        &addr_offset,
    1485              :                                                        vn_valueize);
    1486              :           /* If that didn't work because the address isn't invariant propagate
    1487              :              the reference tree from the address operation in case the current
    1488              :              dereference isn't offsetted.  */
    1489       934916 :           if (!addr_base
    1490       280822 :               && *i_p == ops->length () - 1
    1491       140411 :               && known_eq (off, 0)
    1492              :               /* This makes us disable this transform for PRE where the
    1493              :                  reference ops might be also used for code insertion which
    1494              :                  is invalid.  */
    1495      1019883 :               && default_vn_walk_kind == VN_WALKREWRITE)
    1496              :             {
    1497        84877 :               auto_vec<vn_reference_op_s, 32> tem;
    1498        84877 :               copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
    1499              :               /* Make sure to preserve TBAA info.  The only objects not
    1500              :                  wrapped in MEM_REFs that can have their address taken are
    1501              :                  STRING_CSTs.  */
    1502        84877 :               if (tem.length () >= 2
    1503        84877 :                   && tem[tem.length () - 2].opcode == MEM_REF)
    1504              :                 {
    1505        84862 :                   vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
    1506        84862 :                   new_mem_op->op0
    1507        84862 :                       = wide_int_to_tree (TREE_TYPE (mem_op->op0),
    1508       169724 :                                           wi::to_poly_wide (new_mem_op->op0));
    1509              :                 }
    1510              :               else
    1511           15 :                 gcc_assert (tem.last ().opcode == STRING_CST);
    1512        84877 :               ops->pop ();
    1513        84877 :               ops->pop ();
    1514        84877 :               ops->safe_splice (tem);
    1515        84877 :               --*i_p;
    1516        84877 :               return true;
    1517        84877 :             }
    1518       850039 :           if (!addr_base
    1519       794505 :               || TREE_CODE (addr_base) != MEM_REF
    1520      1642707 :               || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
    1521       790807 :                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
    1522              :                                                                     0))))
    1523              :             return changed;
    1524              : 
    1525       792668 :           off += addr_offset;
    1526       792668 :           off += mem_ref_offset (addr_base);
    1527       792668 :           op->op0 = TREE_OPERAND (addr_base, 0);
    1528              :         }
    1529              :       else
    1530              :         {
    1531     19115804 :           tree ptr, ptroff;
    1532     19115804 :           ptr = gimple_assign_rhs1 (def_stmt);
    1533     19115804 :           ptroff = gimple_assign_rhs2 (def_stmt);
    1534     19115804 :           if (TREE_CODE (ptr) != SSA_NAME
    1535     17453023 :               || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
    1536              :               /* Make sure to not endlessly recurse.
    1537              :                  See gcc.dg/tree-ssa/20040408-1.c for an example.  Can easily
    1538              :                  happen when we value-number a PHI to its backedge value.  */
    1539     17451626 :               || SSA_VAL (ptr) == op->op0
    1540     36567430 :               || !poly_int_tree_p (ptroff))
    1541     12314699 :             return changed;
    1542              : 
    1543      6801105 :           off += wi::to_poly_offset (ptroff);
    1544      6801105 :           op->op0 = ptr;
    1545              :         }
    1546              : 
    1547      7593773 :       mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
    1548      7593773 :       if (tree_fits_shwi_p (mem_op->op0))
    1549      7290118 :         mem_op->off = tree_to_shwi (mem_op->op0);
    1550              :       else
    1551       303655 :         mem_op->off = -1;
    1552              :       /* ???  Can end up with endless recursion here!?
    1553              :          gcc.c-torture/execute/strcmp-1.c  */
    1554      7593773 :       if (TREE_CODE (op->op0) == SSA_NAME)
    1555      7591912 :         op->op0 = SSA_VAL (op->op0);
    1556      7593773 :       if (TREE_CODE (op->op0) != SSA_NAME)
    1557         1951 :         op->opcode = TREE_CODE (op->op0);
    1558              : 
    1559      7593773 :       changed = true;
    1560              :     }
    1561              :   /* Tail-recurse.  */
    1562      7593773 :   while (TREE_CODE (op->op0) == SSA_NAME);
    1563              : 
    1564              :   /* Fold a remaining *&.  */
    1565         1951 :   if (TREE_CODE (op->op0) == ADDR_EXPR)
    1566          261 :     vn_reference_fold_indirect (ops, i_p);
    1567              : 
    1568              :   return changed;
    1569              : }
    1570              : 
    1571              : /* Optimize the reference REF to a constant if possible or return
    1572              :    NULL_TREE if not.  */
    1573              : 
    1574              : tree
    1575    108100630 : fully_constant_vn_reference_p (vn_reference_t ref)
    1576              : {
    1577    108100630 :   vec<vn_reference_op_s> operands = ref->operands;
    1578    108100630 :   vn_reference_op_t op;
    1579              : 
    1580              :   /* Try to simplify the translated expression if it is
    1581              :      a call to a builtin function with at most two arguments.  */
    1582    108100630 :   op = &operands[0];
    1583    108100630 :   if (op->opcode == CALL_EXPR
    1584        85457 :       && (!op->op0
    1585        78078 :           || (TREE_CODE (op->op0) == ADDR_EXPR
    1586        78078 :               && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
    1587        78078 :               && fndecl_built_in_p (TREE_OPERAND (op->op0, 0),
    1588              :                                     BUILT_IN_NORMAL)))
    1589        69012 :       && operands.length () >= 2
    1590    108169610 :       && operands.length () <= 3)
    1591              :     {
    1592        32499 :       vn_reference_op_t arg0, arg1 = NULL;
    1593        32499 :       bool anyconst = false;
    1594        32499 :       arg0 = &operands[1];
    1595        32499 :       if (operands.length () > 2)
    1596         5558 :         arg1 = &operands[2];
    1597        32499 :       if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
    1598        32499 :           || (arg0->opcode == ADDR_EXPR
    1599        13585 :               && is_gimple_min_invariant (arg0->op0)))
    1600              :         anyconst = true;
    1601        32499 :       if (arg1
    1602        32499 :           && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
    1603         4038 :               || (arg1->opcode == ADDR_EXPR
    1604          581 :                   && is_gimple_min_invariant (arg1->op0))))
    1605              :         anyconst = true;
    1606        30398 :       if (anyconst)
    1607              :         {
    1608        21778 :           combined_fn fn;
    1609        21778 :           if (op->op0)
    1610        20826 :             fn = as_combined_fn (DECL_FUNCTION_CODE
    1611        20826 :                                         (TREE_OPERAND (op->op0, 0)));
    1612              :           else
    1613          952 :             fn = as_combined_fn ((internal_fn) op->clique);
    1614        21778 :           tree folded;
    1615        21778 :           if (arg1)
    1616         2711 :             folded = fold_const_call (fn, ref->type, arg0->op0, arg1->op0);
    1617              :           else
    1618        19067 :             folded = fold_const_call (fn, ref->type, arg0->op0);
    1619        21778 :           if (folded
    1620        21778 :               && is_gimple_min_invariant (folded))
    1621              :             return folded;
    1622              :         }
    1623              :     }
    1624              : 
    1625              :   /* Simplify reads from constants or constant initializers.  */
    1626    108068131 :   else if (BITS_PER_UNIT == 8
    1627    108068131 :            && ref->type
    1628    108068131 :            && COMPLETE_TYPE_P (ref->type)
    1629    216136220 :            && is_gimple_reg_type (ref->type))
    1630              :     {
    1631    103777223 :       poly_int64 off = 0;
    1632    103777223 :       HOST_WIDE_INT size;
    1633    103777223 :       if (INTEGRAL_TYPE_P (ref->type))
    1634     52755224 :         size = TYPE_PRECISION (ref->type);
    1635     51021999 :       else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
    1636     51021999 :         size = tree_to_shwi (TYPE_SIZE (ref->type));
    1637              :       else
    1638    108100630 :         return NULL_TREE;
    1639    103777223 :       if (size % BITS_PER_UNIT != 0
    1640    102024451 :           || size > MAX_BITSIZE_MODE_ANY_MODE)
    1641              :         return NULL_TREE;
    1642    102023124 :       size /= BITS_PER_UNIT;
    1643    102023124 :       unsigned i;
    1644    188455499 :       for (i = 0; i < operands.length (); ++i)
    1645              :         {
    1646    188455499 :           if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
    1647              :             {
    1648          309 :               ++i;
    1649          309 :               break;
    1650              :             }
    1651    188455190 :           if (operands[i].reverse)
    1652              :             return NULL_TREE;
    1653    188446832 :           if (known_eq (operands[i].off, -1))
    1654              :             return NULL_TREE;
    1655    174792956 :           off += operands[i].off;
    1656    174792956 :           if (operands[i].opcode == MEM_REF)
    1657              :             {
    1658     88360581 :               ++i;
    1659     88360581 :               break;
    1660              :             }
    1661              :         }
    1662     88360890 :       vn_reference_op_t base = &operands[--i];
    1663     88360890 :       tree ctor = error_mark_node;
    1664     88360890 :       tree decl = NULL_TREE;
    1665     88360890 :       if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
    1666          309 :         ctor = base->op0;
    1667     88360581 :       else if (base->opcode == MEM_REF
    1668     88360581 :                && base[1].opcode == ADDR_EXPR
    1669    145348960 :                && (VAR_P (TREE_OPERAND (base[1].op0, 0))
    1670      3529512 :                    || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
    1671      3529452 :                    || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
    1672              :         {
    1673     53464694 :           decl = TREE_OPERAND (base[1].op0, 0);
    1674     53464694 :           if (TREE_CODE (decl) == STRING_CST)
    1675              :             ctor = decl;
    1676              :           else
    1677     53458927 :             ctor = ctor_for_folding (decl);
    1678              :         }
    1679     88355123 :       if (ctor == NULL_TREE)
    1680          386 :         return build_zero_cst (ref->type);
    1681     88360504 :       else if (ctor != error_mark_node)
    1682              :         {
    1683        98993 :           HOST_WIDE_INT const_off;
    1684        98993 :           if (decl)
    1685              :             {
    1686       197368 :               tree res = fold_ctor_reference (ref->type, ctor,
    1687        98684 :                                               off * BITS_PER_UNIT,
    1688        98684 :                                               size * BITS_PER_UNIT, decl);
    1689        98684 :               if (res)
    1690              :                 {
    1691        58138 :                   STRIP_USELESS_TYPE_CONVERSION (res);
    1692        58138 :                   if (is_gimple_min_invariant (res))
    1693    108100630 :                     return res;
    1694              :                 }
    1695              :             }
    1696          309 :           else if (off.is_constant (&const_off))
    1697              :             {
    1698          309 :               unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
    1699          309 :               int len = native_encode_expr (ctor, buf, size, const_off);
    1700          309 :               if (len > 0)
    1701          139 :                 return native_interpret_expr (ref->type, buf, len);
    1702              :             }
    1703              :         }
    1704              :     }
    1705              : 
    1706              :   return NULL_TREE;
    1707              : }
    1708              : 
    1709              : /* Return true if OPS contain a storage order barrier.  */
    1710              : 
    1711              : static bool
    1712     58370904 : contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
    1713              : {
    1714     58370904 :   vn_reference_op_t op;
    1715     58370904 :   unsigned i;
    1716              : 
    1717    228499000 :   FOR_EACH_VEC_ELT (ops, i, op)
    1718    170128096 :     if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
    1719              :       return true;
    1720              : 
    1721              :   return false;
    1722              : }
    1723              : 
    1724              : /* Return true if OPS represent an access with reverse storage order.  */
    1725              : 
    1726              : static bool
    1727     58379172 : reverse_storage_order_for_component_p (vec<vn_reference_op_s> ops)
    1728              : {
    1729     58379172 :   unsigned i = 0;
    1730     58379172 :   if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
    1731              :     ++i;
    1732     58379172 :   switch (ops[i].opcode)
    1733              :     {
    1734     56311663 :     case ARRAY_REF:
    1735     56311663 :     case COMPONENT_REF:
    1736     56311663 :     case BIT_FIELD_REF:
    1737     56311663 :     case MEM_REF:
    1738     56311663 :       return ops[i].reverse;
    1739              :     default:
    1740              :       return false;
    1741              :     }
    1742              : }
    1743              : 
    1744              : /* Transform any SSA_NAME's in a vector of vn_reference_op_s
    1745              :    structures into their value numbers.  This is done in-place, and
    1746              :    the vector passed in is returned.  *VALUEIZED_ANYTHING will specify
    1747              :    whether any operands were valueized.  */
    1748              : 
    1749              : static void
    1750    216036063 : valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
    1751              :                  bool with_avail = false)
    1752              : {
    1753    216036063 :   *valueized_anything = false;
    1754              : 
    1755    871015124 :   for (unsigned i = 0; i < orig->length (); ++i)
    1756              :     {
    1757    654979061 : re_valueize:
    1758    658840771 :       vn_reference_op_t vro = &(*orig)[i];
    1759    658840771 :       if (vro->opcode == SSA_NAME
    1760    563510581 :           || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
    1761              :         {
    1762    119571728 :           tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
    1763    119571728 :           if (tem != vro->op0)
    1764              :             {
    1765     17616991 :               *valueized_anything = true;
    1766     17616991 :               vro->op0 = tem;
    1767              :             }
    1768              :           /* If it transforms from an SSA_NAME to a constant, update
    1769              :              the opcode.  */
    1770    119571728 :           if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
    1771      2031609 :             vro->opcode = TREE_CODE (vro->op0);
    1772              :         }
    1773    658840771 :       if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
    1774              :         {
    1775        26286 :           tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
    1776        26286 :           if (tem != vro->op1)
    1777              :             {
    1778          607 :               *valueized_anything = true;
    1779          607 :               vro->op1 = tem;
    1780              :             }
    1781              :         }
    1782    658840771 :       if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
    1783              :         {
    1784       205492 :           tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
    1785       205492 :           if (tem != vro->op2)
    1786              :             {
    1787       119592 :               *valueized_anything = true;
    1788       119592 :               vro->op2 = tem;
    1789              :             }
    1790              :         }
    1791              :       /* If it transforms from an SSA_NAME to an address, fold with
    1792              :          a preceding indirect reference.  */
    1793    658840771 :       if (i > 0
    1794    442726229 :           && vro->op0
    1795    439231617 :           && TREE_CODE (vro->op0) == ADDR_EXPR
    1796    789868975 :           && (*orig)[i - 1].opcode == MEM_REF)
    1797              :         {
    1798    125235993 :           if (vn_reference_fold_indirect (orig, &i))
    1799       644982 :             *valueized_anything = true;
    1800              :         }
    1801    533604778 :       else if (i > 0
    1802    317490236 :                && vro->opcode == SSA_NAME
    1803    626903359 :                && (*orig)[i - 1].opcode == MEM_REF)
    1804              :         {
    1805     83720169 :           if (vn_reference_maybe_forwprop_address (orig, &i))
    1806              :             {
    1807      3861710 :               *valueized_anything = true;
    1808              :               /* Re-valueize the current operand.  */
    1809      3861710 :               goto re_valueize;
    1810              :             }
    1811              :         }
    1812              :       /* If it transforms a non-constant ARRAY_REF into a constant
    1813              :          one, adjust the constant offset.  */
    1814    449884609 :       else if ((vro->opcode == ARRAY_REF
    1815    449884609 :                 || vro->opcode == ARRAY_RANGE_REF)
    1816     39278565 :                && known_eq (vro->off, -1)
    1817     17091260 :                && poly_int_tree_p (vro->op0)
    1818      4714965 :                && poly_int_tree_p (vro->op1)
    1819    454599574 :                && TREE_CODE (vro->op2) == INTEGER_CST)
    1820              :         {
    1821              :             /* Prohibit value-numbering addresses of one-after-the-last
    1822              :                element ARRAY_REFs the same as addresses of other components
    1823              :                before the pass folding __builtin_object_size had a chance
    1824              :                to run.  */
    1825      4581254 :           if (!(cfun->curr_properties & PROP_objsz)
    1826      5824126 :               && (*orig)[0].opcode == ADDR_EXPR)
    1827              :             {
    1828        35490 :               tree dom = TYPE_DOMAIN ((*orig)[i + 1].type);
    1829        53886 :               if (!dom
    1830        35340 :                   || !TYPE_MAX_VALUE (dom)
    1831        25448 :                   || !poly_int_tree_p (TYPE_MAX_VALUE (dom))
    1832        52670 :                   || integer_minus_onep (TYPE_MAX_VALUE (dom)))
    1833        19203 :                 continue;
    1834        17094 :               if (!known_le (wi::to_poly_offset (vro->op0),
    1835              :                              wi::to_poly_offset (TYPE_MAX_VALUE (dom))))
    1836          807 :                 continue;
    1837              :             }
    1838              : 
    1839      9124102 :           poly_offset_int off = ((wi::to_poly_offset (vro->op0)
    1840     13686153 :                                   - wi::to_poly_offset (vro->op1))
    1841      9124102 :                                  * wi::to_offset (vro->op2)
    1842      4562051 :                                  * vn_ref_op_align_unit (vro));
    1843      4562051 :           off.to_shwi (&vro->off);
    1844              :         }
    1845              :     }
    1846    216036063 : }
    1847              : 
    1848              : static void
    1849     12468241 : valueize_refs (vec<vn_reference_op_s> *orig)
    1850              : {
    1851     12468241 :   bool tem;
    1852            0 :   valueize_refs_1 (orig, &tem);
    1853            0 : }
    1854              : 
    1855              : static vec<vn_reference_op_s> shared_lookup_references;
    1856              : 
    1857              : /* Create a vector of vn_reference_op_s structures from REF, a
    1858              :    REFERENCE_CLASS_P tree.  The vector is shared among all callers of
    1859              :    this function.  *VALUEIZED_ANYTHING will specify whether any
    1860              :    operands were valueized.  */
    1861              : 
    1862              : static vec<vn_reference_op_s>
    1863    177879634 : valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
    1864              : {
    1865    177879634 :   if (!ref)
    1866            0 :     return vNULL;
    1867    177879634 :   shared_lookup_references.truncate (0);
    1868    177879634 :   copy_reference_ops_from_ref (ref, &shared_lookup_references);
    1869    177879634 :   valueize_refs_1 (&shared_lookup_references, valueized_anything);
    1870    177879634 :   return shared_lookup_references;
    1871              : }
    1872              : 
    1873              : /* Create a vector of vn_reference_op_s structures from CALL, a
    1874              :    call statement.  The vector is shared among all callers of
    1875              :    this function.  */
    1876              : 
    1877              : static vec<vn_reference_op_s>
    1878      9065646 : valueize_shared_reference_ops_from_call (gcall *call)
    1879              : {
    1880      9065646 :   if (!call)
    1881            0 :     return vNULL;
    1882      9065646 :   shared_lookup_references.truncate (0);
    1883      9065646 :   copy_reference_ops_from_call (call, &shared_lookup_references);
    1884      9065646 :   valueize_refs (&shared_lookup_references);
    1885      9065646 :   return shared_lookup_references;
    1886              : }
    1887              : 
    1888              : /* Lookup a SCCVN reference operation VR in the current hash table.
    1889              :    Returns the resulting value number if it exists in the hash table,
    1890              :    NULL_TREE otherwise.  VNRESULT will be filled in with the actual
    1891              :    vn_reference_t stored in the hashtable if something is found.  */
    1892              : 
    1893              : static tree
    1894     64481919 : vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
    1895              : {
    1896     64481919 :   vn_reference_s **slot;
    1897     64481919 :   hashval_t hash;
    1898              : 
    1899     64481919 :   hash = vr->hashcode;
    1900     64481919 :   slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
    1901     64481919 :   if (slot)
    1902              :     {
    1903      8010730 :       if (vnresult)
    1904      8010730 :         *vnresult = (vn_reference_t)*slot;
    1905      8010730 :       return ((vn_reference_t)*slot)->result;
    1906              :     }
    1907              : 
    1908              :   return NULL_TREE;
    1909              : }
    1910              : 
    1911              : 
    1912              : /* Partial definition tracking support.  */
    1913              : 
    1914              : struct pd_range
    1915              : {
    1916              :   HOST_WIDE_INT offset;
    1917              :   HOST_WIDE_INT size;
    1918              :   pd_range *m_children[2];
    1919              : };
    1920              : 
    1921              : struct pd_data
    1922              : {
    1923              :   tree rhs;
    1924              :   HOST_WIDE_INT rhs_off;
    1925              :   HOST_WIDE_INT offset;
    1926              :   HOST_WIDE_INT size;
    1927              : };
    1928              : 
    1929              : /* Context for alias walking.  */
    1930              : 
    1931              : struct vn_walk_cb_data
    1932              : {
    1933     60295660 :   vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
    1934              :                    vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
    1935              :                    bool redundant_store_removal_p_)
    1936     60295660 :     : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
    1937     60295660 :       mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
    1938     60295660 :       vn_walk_kind (vn_walk_kind_),
    1939     60295660 :       tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
    1940    120591320 :       saved_operands (vNULL), first_range (), first_set (-2),
    1941    120591320 :       first_base_set (-2)
    1942              :   {
    1943     60295660 :     if (!last_vuse_ptr)
    1944     28005467 :       last_vuse_ptr = &last_vuse;
    1945     60295660 :     ao_ref_init (&orig_ref, orig_ref_);
    1946     60295660 :     if (mask)
    1947              :       {
    1948       301588 :         wide_int w = wi::to_wide (mask);
    1949       301588 :         unsigned int pos = 0, prec = w.get_precision ();
    1950       301588 :         pd_data pd;
    1951       301588 :         pd.rhs = build_constructor (NULL_TREE, NULL);
    1952       301588 :         pd.rhs_off = 0;
    1953              :         /* When bitwise and with a constant is done on a memory load,
    1954              :            we don't really need all the bits to be defined or defined
    1955              :            to constants, we don't really care what is in the position
    1956              :            corresponding to 0 bits in the mask.
    1957              :            So, push the ranges of those 0 bits in the mask as artificial
    1958              :            zero stores and let the partial def handling code do the
    1959              :            rest.  */
    1960       646570 :         while (pos < prec)
    1961              :           {
    1962       626497 :             int tz = wi::ctz (w);
    1963       626497 :             if (pos + tz > prec)
    1964       281515 :               tz = prec - pos;
    1965       626497 :             if (tz)
    1966              :               {
    1967       475649 :                 if (BYTES_BIG_ENDIAN)
    1968              :                   pd.offset = prec - pos - tz;
    1969              :                 else
    1970       475649 :                   pd.offset = pos;
    1971       475649 :                 pd.size = tz;
    1972       475649 :                 void *r = push_partial_def (pd, 0, 0, 0, prec);
    1973       475649 :                 gcc_assert (r == NULL_TREE);
    1974              :               }
    1975       626497 :             pos += tz;
    1976       626497 :             if (pos == prec)
    1977              :               break;
    1978       344982 :             w = wi::lrshift (w, tz);
    1979       344982 :             tz = wi::ctz (wi::bit_not (w));
    1980       344982 :             if (pos + tz > prec)
    1981            0 :               tz = prec - pos;
    1982       344982 :             pos += tz;
    1983       344982 :             w = wi::lrshift (w, tz);
    1984              :           }
    1985       301588 :       }
    1986     60295660 :   }
    1987              :   ~vn_walk_cb_data ();
    1988              :   void *finish (alias_set_type, alias_set_type, tree);
    1989              :   void *push_partial_def (pd_data pd,
    1990              :                           alias_set_type, alias_set_type, HOST_WIDE_INT,
    1991              :                           HOST_WIDE_INT);
    1992              : 
    1993              :   vn_reference_t vr;
    1994              :   ao_ref orig_ref;
    1995              :   tree *last_vuse_ptr;
    1996              :   tree last_vuse;
    1997              :   tree mask;
    1998              :   tree masked_result;
    1999              :   tree same_val;
    2000              :   vn_lookup_kind vn_walk_kind;
    2001              :   bool tbaa_p;
    2002              :   bool redundant_store_removal_p;
    2003              :   vec<vn_reference_op_s> saved_operands;
    2004              : 
    2005              :   /* The VDEFs of partial defs we come along.  */
    2006              :   auto_vec<pd_data, 2> partial_defs;
    2007              :   /* The first defs range to avoid splay tree setup in most cases.  */
    2008              :   pd_range first_range;
    2009              :   alias_set_type first_set;
    2010              :   alias_set_type first_base_set;
    2011              :   default_splay_tree<pd_range *> known_ranges;
    2012              :   obstack ranges_obstack;
    2013              :   static constexpr HOST_WIDE_INT bufsize = 64;
    2014              : };
    2015              : 
    2016     60295660 : vn_walk_cb_data::~vn_walk_cb_data ()
    2017              : {
    2018     60295660 :   if (known_ranges)
    2019       169241 :     obstack_free (&ranges_obstack, NULL);
    2020     60295660 :   saved_operands.release ();
    2021     60295660 : }
    2022              : 
    2023              : void *
    2024      1525144 : vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
    2025              : {
    2026      1525144 :   if (first_set != -2)
    2027              :     {
    2028       420888 :       set = first_set;
    2029       420888 :       base_set = first_base_set;
    2030              :     }
    2031      1525144 :   if (mask)
    2032              :     {
    2033          449 :       masked_result = val;
    2034          449 :       return (void *) -1;
    2035              :     }
    2036      1524695 :   if (same_val && !operand_equal_p (val, same_val))
    2037              :     return (void *) -1;
    2038      1520916 :   vec<vn_reference_op_s> &operands
    2039      1520916 :     = saved_operands.exists () ? saved_operands : vr->operands;
    2040      1520916 :   return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
    2041              :                                                    vr->offset, vr->max_size,
    2042      1520916 :                                                    vr->type, operands, val);
    2043              : }
    2044              : 
    2045              : /* Push PD to the vector of partial definitions returning a
    2046              :    value when we are ready to combine things with VUSE, SET and MAXSIZEI,
    2047              :    NULL when we want to continue looking for partial defs or -1
    2048              :    on failure.  */
    2049              : 
    2050              : void *
    2051       554986 : vn_walk_cb_data::push_partial_def (pd_data pd,
    2052              :                                    alias_set_type set, alias_set_type base_set,
    2053              :                                    HOST_WIDE_INT offseti,
    2054              :                                    HOST_WIDE_INT maxsizei)
    2055              : {
    2056              :   /* We're using a fixed buffer for encoding so fail early if the object
    2057              :      we want to interpret is bigger.  */
    2058       554986 :   if (maxsizei > bufsize * BITS_PER_UNIT
    2059              :       || CHAR_BIT != 8
    2060              :       || BITS_PER_UNIT != 8
    2061              :       /* Not prepared to handle PDP endian.  */
    2062              :       || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
    2063              :     return (void *)-1;
    2064              : 
    2065              :   /* Turn too large constant stores into non-constant stores.  */
    2066       554915 :   if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
    2067            0 :     pd.rhs = error_mark_node;
    2068              : 
    2069              :   /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
    2070              :      most a partial byte before and/or after the region.  */
    2071       554915 :   if (!CONSTANT_CLASS_P (pd.rhs))
    2072              :     {
    2073       515619 :       if (pd.offset < offseti)
    2074              :         {
    2075         8256 :           HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
    2076         8256 :           gcc_assert (pd.size > o);
    2077         8256 :           pd.size -= o;
    2078         8256 :           pd.offset += o;
    2079              :         }
    2080       515619 :       if (pd.size > maxsizei)
    2081         7371 :         pd.size = maxsizei + ((pd.size - maxsizei) % BITS_PER_UNIT);
    2082              :     }
    2083              : 
    2084       554915 :   pd.offset -= offseti;
    2085              : 
    2086      1109830 :   bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
    2087       554915 :                         || CONSTANT_CLASS_P (pd.rhs));
    2088       554915 :   pd_range *r;
    2089       554915 :   if (partial_defs.is_empty ())
    2090              :     {
    2091              :       /* If we get a clobber upfront, fail.  */
    2092       354332 :       if (TREE_CLOBBER_P (pd.rhs))
    2093              :         return (void *)-1;
    2094       353979 :       if (!pd_constant_p)
    2095              :         return (void *)-1;
    2096       324715 :       partial_defs.safe_push (pd);
    2097       324715 :       first_range.offset = pd.offset;
    2098       324715 :       first_range.size = pd.size;
    2099       324715 :       first_set = set;
    2100       324715 :       first_base_set = base_set;
    2101       324715 :       last_vuse_ptr = NULL;
    2102       324715 :       r = &first_range;
    2103              :       /* Go check if the first partial definition was a full one in case
    2104              :          the caller didn't optimize for this.  */
    2105              :     }
    2106              :   else
    2107              :     {
    2108       200583 :       if (!known_ranges)
    2109              :         {
    2110              :           /* ???  Optimize the case where the 2nd partial def completes
    2111              :              things.  */
    2112       169241 :           gcc_obstack_init (&ranges_obstack);
    2113       169241 :           known_ranges.insert_max_node (&first_range);
    2114              :         }
    2115              :       /* Lookup the offset and see if we need to merge.  */
    2116       200583 :       int comparison = known_ranges.lookup_le
    2117       405446 :         ([&] (pd_range *r) { return pd.offset < r->offset; },
    2118       180319 :          [&] (pd_range *r) { return pd.offset > r->offset; });
    2119       200583 :       r = known_ranges.root ();
    2120       200583 :       if (comparison >= 0
    2121       200583 :           && ranges_known_overlap_p (r->offset, r->size + 1,
    2122              :                                      pd.offset, pd.size))
    2123              :         {
    2124              :           /* Ignore partial defs already covered.  Here we also drop shadowed
    2125              :              clobbers arriving here at the floor.  */
    2126         5828 :           if (known_subrange_p (pd.offset, pd.size, r->offset, r->size))
    2127              :             return NULL;
    2128         4993 :           r->size = MAX (r->offset + r->size, pd.offset + pd.size) - r->offset;
    2129              :         }
    2130              :       else
    2131              :         {
    2132              :           /* pd.offset wasn't covered yet, insert the range.  */
    2133       194755 :           void *addr = XOBNEW (&ranges_obstack, pd_range);
    2134       194755 :           r = new (addr) pd_range { pd.offset, pd.size, {} };
    2135       194755 :           known_ranges.insert_relative (comparison, r);
    2136              :         }
    2137              :       /* Merge r which now contains pd's range and is a member of the splay
    2138              :          tree with adjacent overlapping ranges.  */
    2139       199748 :       if (known_ranges.splay_next_node ())
    2140        22367 :         do
    2141              :           {
    2142        22367 :             pd_range *rafter = known_ranges.root ();
    2143        22367 :             if (!ranges_known_overlap_p (r->offset, r->size + 1,
    2144        22367 :                                          rafter->offset, rafter->size))
    2145              :               break;
    2146        22097 :             r->size = MAX (r->offset + r->size,
    2147        22097 :                            rafter->offset + rafter->size) - r->offset;
    2148              :           }
    2149        22097 :         while (known_ranges.remove_root_and_splay_next ());
    2150              :       /* If we get a clobber, fail.  */
    2151       199748 :       if (TREE_CLOBBER_P (pd.rhs))
    2152              :         return (void *)-1;
    2153              :       /* Non-constants are OK as long as they are shadowed by a constant.  */
    2154       197552 :       if (!pd_constant_p)
    2155              :         return (void *)-1;
    2156       191430 :       partial_defs.safe_push (pd);
    2157              :     }
    2158              : 
    2159              :   /* Now we have merged pd's range into the range tree.  When we have covered
    2160              :      [offseti, sizei] then the tree will contain exactly one node which has
    2161              :      the desired properties and it will be 'r'.  */
    2162       516145 :   if (!known_subrange_p (0, maxsizei, r->offset, r->size))
    2163              :     /* Continue looking for partial defs.  */
    2164              :     return NULL;
    2165              : 
    2166              :   /* Now simply native encode all partial defs in reverse order.  */
    2167         8701 :   unsigned ndefs = partial_defs.length ();
    2168              :   /* We support up to 512-bit values (for V8DFmode).  */
    2169         8701 :   unsigned char buffer[bufsize + 1];
    2170         8701 :   unsigned char this_buffer[bufsize + 1];
    2171         8701 :   int len;
    2172              : 
    2173         8701 :   memset (buffer, 0, bufsize + 1);
    2174         8701 :   unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
    2175        42983 :   while (!partial_defs.is_empty ())
    2176              :     {
    2177        25581 :       pd_data pd = partial_defs.pop ();
    2178        25581 :       unsigned int amnt;
    2179        25581 :       if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
    2180              :         {
    2181              :           /* Empty CONSTRUCTOR.  */
    2182         2069 :           if (pd.size >= needed_len * BITS_PER_UNIT)
    2183         2069 :             len = needed_len;
    2184              :           else
    2185         1782 :             len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
    2186         2069 :           memset (this_buffer, 0, len);
    2187              :         }
    2188        23512 :       else if (pd.rhs_off >= 0)
    2189              :         {
    2190        47024 :           len = native_encode_expr (pd.rhs, this_buffer, bufsize,
    2191        23512 :                                     (MAX (0, -pd.offset)
    2192        23512 :                                      + pd.rhs_off) / BITS_PER_UNIT);
    2193        23512 :           if (len <= 0
    2194        23512 :               || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
    2195        23512 :                         - MAX (0, -pd.offset) / BITS_PER_UNIT))
    2196              :             {
    2197            0 :               if (dump_file && (dump_flags & TDF_DETAILS))
    2198            0 :                 fprintf (dump_file, "Failed to encode %u "
    2199              :                          "partial definitions\n", ndefs);
    2200            0 :               return (void *)-1;
    2201              :             }
    2202              :         }
    2203              :       else /* negative pd.rhs_off indicates we want to chop off first bits */
    2204              :         {
    2205            0 :           if (-pd.rhs_off >= bufsize)
    2206              :             return (void *)-1;
    2207            0 :           len = native_encode_expr (pd.rhs,
    2208            0 :                                     this_buffer + -pd.rhs_off / BITS_PER_UNIT,
    2209            0 :                                     bufsize - -pd.rhs_off / BITS_PER_UNIT,
    2210            0 :                                     MAX (0, -pd.offset) / BITS_PER_UNIT);
    2211            0 :           if (len <= 0
    2212            0 :               || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
    2213            0 :                         - MAX (0, -pd.offset) / BITS_PER_UNIT))
    2214              :             {
    2215            0 :               if (dump_file && (dump_flags & TDF_DETAILS))
    2216            0 :                 fprintf (dump_file, "Failed to encode %u "
    2217              :                          "partial definitions\n", ndefs);
    2218            0 :               return (void *)-1;
    2219              :             }
    2220              :         }
    2221              : 
    2222        25581 :       unsigned char *p = buffer;
    2223        25581 :       HOST_WIDE_INT size = pd.size;
    2224        25581 :       if (pd.offset < 0)
    2225          263 :         size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
    2226        25581 :       this_buffer[len] = 0;
    2227        25581 :       if (BYTES_BIG_ENDIAN)
    2228              :         {
    2229              :           /* LSB of this_buffer[len - 1] byte should be at
    2230              :              pd.offset + pd.size - 1 bits in buffer.  */
    2231              :           amnt = ((unsigned HOST_WIDE_INT) pd.offset
    2232              :                   + pd.size) % BITS_PER_UNIT;
    2233              :           if (amnt)
    2234              :             shift_bytes_in_array_right (this_buffer, len + 1, amnt);
    2235              :           unsigned char *q = this_buffer;
    2236              :           unsigned int off = 0;
    2237              :           if (pd.offset >= 0)
    2238              :             {
    2239              :               unsigned int msk;
    2240              :               off = pd.offset / BITS_PER_UNIT;
    2241              :               gcc_assert (off < needed_len);
    2242              :               p = buffer + off;
    2243              :               if (size <= amnt)
    2244              :                 {
    2245              :                   msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
    2246              :                   *p = (*p & ~msk) | (this_buffer[len] & msk);
    2247              :                   size = 0;
    2248              :                 }
    2249              :               else
    2250              :                 {
    2251              :                   if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
    2252              :                     q = (this_buffer + len
    2253              :                          - (ROUND_UP (size - amnt, BITS_PER_UNIT)
    2254              :                             / BITS_PER_UNIT));
    2255              :                   if (pd.offset % BITS_PER_UNIT)
    2256              :                     {
    2257              :                       msk = -1U << (BITS_PER_UNIT
    2258              :                                     - (pd.offset % BITS_PER_UNIT));
    2259              :                       *p = (*p & msk) | (*q & ~msk);
    2260              :                       p++;
    2261              :                       q++;
    2262              :                       off++;
    2263              :                       size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
    2264              :                       gcc_assert (size >= 0);
    2265              :                     }
    2266              :                 }
    2267              :             }
    2268              :           else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
    2269              :             {
    2270              :               q = (this_buffer + len
    2271              :                    - (ROUND_UP (size - amnt, BITS_PER_UNIT)
    2272              :                       / BITS_PER_UNIT));
    2273              :               if (pd.offset % BITS_PER_UNIT)
    2274              :                 {
    2275              :                   q++;
    2276              :                   size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
    2277              :                                            % BITS_PER_UNIT);
    2278              :                   gcc_assert (size >= 0);
    2279              :                 }
    2280              :             }
    2281              :           if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
    2282              :               > needed_len)
    2283              :             size = (needed_len - off) * BITS_PER_UNIT;
    2284              :           memcpy (p, q, size / BITS_PER_UNIT);
    2285              :           if (size % BITS_PER_UNIT)
    2286              :             {
    2287              :               unsigned int msk
    2288              :                 = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
    2289              :               p += size / BITS_PER_UNIT;
    2290              :               q += size / BITS_PER_UNIT;
    2291              :               *p = (*q & msk) | (*p & ~msk);
    2292              :             }
    2293              :         }
    2294              :       else
    2295              :         {
    2296        25581 :           if (pd.offset >= 0)
    2297              :             {
    2298              :               /* LSB of this_buffer[0] byte should be at pd.offset bits
    2299              :                  in buffer.  */
    2300        25318 :               unsigned int msk;
    2301        25318 :               size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
    2302        25318 :               amnt = pd.offset % BITS_PER_UNIT;
    2303        25318 :               if (amnt)
    2304         1504 :                 shift_bytes_in_array_left (this_buffer, len + 1, amnt);
    2305        25318 :               unsigned int off = pd.offset / BITS_PER_UNIT;
    2306        25318 :               gcc_assert (off < needed_len);
    2307        25318 :               size = MIN (size,
    2308              :                           (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
    2309        25318 :               p = buffer + off;
    2310        25318 :               if (amnt + size < BITS_PER_UNIT)
    2311              :                 {
    2312              :                   /* Low amnt bits come from *p, then size bits
    2313              :                      from this_buffer[0] and the remaining again from
    2314              :                      *p.  */
    2315         1076 :                   msk = ((1 << size) - 1) << amnt;
    2316         1076 :                   *p = (*p & ~msk) | (this_buffer[0] & msk);
    2317         1076 :                   size = 0;
    2318              :                 }
    2319        24242 :               else if (amnt)
    2320              :                 {
    2321         1130 :                   msk = -1U << amnt;
    2322         1130 :                   *p = (*p & ~msk) | (this_buffer[0] & msk);
    2323         1130 :                   p++;
    2324         1130 :                   size -= (BITS_PER_UNIT - amnt);
    2325              :                 }
    2326              :             }
    2327              :           else
    2328              :             {
    2329          263 :               amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
    2330          263 :               if (amnt)
    2331           16 :                 size -= BITS_PER_UNIT - amnt;
    2332          263 :               size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
    2333          263 :               if (amnt)
    2334           16 :                 shift_bytes_in_array_left (this_buffer, len + 1, amnt);
    2335              :             }
    2336        25581 :           memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
    2337        25581 :           p += size / BITS_PER_UNIT;
    2338        25581 :           if (size % BITS_PER_UNIT)
    2339              :             {
    2340          626 :               unsigned int msk = -1U << (size % BITS_PER_UNIT);
    2341          626 :               *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
    2342          626 :                     & ~msk) | (*p & msk);
    2343              :             }
    2344              :         }
    2345              :     }
    2346              : 
    2347         8701 :   tree type = vr->type;
    2348              :   /* Make sure to interpret in a type that has a range covering the whole
    2349              :      access size.  */
    2350         8701 :   if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
    2351              :     {
    2352            0 :       if (BITINT_TYPE_P (vr->type)
    2353           26 :           && maxsizei > MAX_FIXED_MODE_SIZE)
    2354           13 :         type = build_bitint_type (maxsizei, TYPE_UNSIGNED (type));
    2355              :       else
    2356            0 :         type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
    2357              :     }
    2358         8701 :   tree val;
    2359         8701 :   if (BYTES_BIG_ENDIAN)
    2360              :     {
    2361              :       unsigned sz = needed_len;
    2362              :       if (maxsizei % BITS_PER_UNIT)
    2363              :         shift_bytes_in_array_right (buffer, needed_len,
    2364              :                                     BITS_PER_UNIT
    2365              :                                     - (maxsizei % BITS_PER_UNIT));
    2366              :       if (INTEGRAL_TYPE_P (type))
    2367              :         {
    2368              :           if (TYPE_MODE (type) != BLKmode)
    2369              :             sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
    2370              :           else
    2371              :             sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
    2372              :         }
    2373              :       if (sz > needed_len)
    2374              :         {
    2375              :           memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
    2376              :           val = native_interpret_expr (type, this_buffer, sz);
    2377              :         }
    2378              :       else
    2379              :         val = native_interpret_expr (type, buffer, needed_len);
    2380              :     }
    2381              :   else
    2382         8701 :     val = native_interpret_expr (type, buffer, bufsize);
    2383              :   /* If we chop off bits because the types precision doesn't match the memory
    2384              :      access size this is ok when optimizing reads but not when called from
    2385              :      the DSE code during elimination.  */
    2386         8701 :   if (val && type != vr->type)
    2387              :     {
    2388           13 :       if (! int_fits_type_p (val, vr->type))
    2389              :         val = NULL_TREE;
    2390              :       else
    2391           13 :         val = fold_convert (vr->type, val);
    2392              :     }
    2393              : 
    2394         8697 :   if (val)
    2395              :     {
    2396         8697 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2397            0 :         fprintf (dump_file,
    2398              :                  "Successfully combined %u partial definitions\n", ndefs);
    2399              :       /* We are using the alias-set of the first store we encounter which
    2400              :          should be appropriate here.  */
    2401         8697 :       return finish (first_set, first_base_set, val);
    2402              :     }
    2403              :   else
    2404              :     {
    2405            4 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2406            0 :         fprintf (dump_file,
    2407              :                  "Failed to interpret %u encoded partial definitions\n", ndefs);
    2408            4 :       return (void *)-1;
    2409              :     }
    2410              : }
    2411              : 
    2412              : /* Callback for walk_non_aliased_vuses.  Adjusts the vn_reference_t VR_
    2413              :    with the current VUSE and performs the expression lookup.  */
    2414              : 
    2415              : static void *
    2416   1078461877 : vn_reference_lookup_2 (ao_ref *op, tree vuse, void *data_)
    2417              : {
    2418   1078461877 :   vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
    2419   1078461877 :   vn_reference_t vr = data->vr;
    2420   1078461877 :   vn_reference_s **slot;
    2421   1078461877 :   hashval_t hash;
    2422              : 
    2423              :   /* If we have partial definitions recorded we have to go through
    2424              :      vn_reference_lookup_3.  */
    2425   2149258930 :   if (!data->partial_defs.is_empty ())
    2426              :     return NULL;
    2427              : 
    2428   1077695246 :   if (data->last_vuse_ptr)
    2429              :     {
    2430   1056976766 :       *data->last_vuse_ptr = vuse;
    2431   1056976766 :       data->last_vuse = vuse;
    2432              :     }
    2433              : 
    2434              :   /* Fixup vuse and hash.  */
    2435   1077695246 :   if (vr->vuse)
    2436   1077695246 :     vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
    2437   1077695246 :   vr->vuse = vuse_ssa_val (vuse);
    2438   1077695246 :   if (vr->vuse)
    2439   1077695246 :     vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
    2440              : 
    2441   1077695246 :   hash = vr->hashcode;
    2442   1077695246 :   slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
    2443   1077695246 :   if (slot)
    2444              :     {
    2445      7663618 :       if ((*slot)->result && data->saved_operands.exists ())
    2446       407038 :         return data->finish (vr->set, vr->base_set, (*slot)->result);
    2447              :       return *slot;
    2448              :     }
    2449              : 
    2450   1070031628 :   if (SSA_NAME_IS_DEFAULT_DEF (vuse))
    2451              :     {
    2452     17923868 :       HOST_WIDE_INT op_offset, op_size;
    2453     17923868 :       tree v = NULL_TREE;
    2454     17923868 :       tree base = ao_ref_base (op);
    2455              : 
    2456     17923868 :       if (base
    2457     17923868 :           && op->offset.is_constant (&op_offset)
    2458     17923868 :           && op->size.is_constant (&op_size)
    2459     17923868 :           && op->max_size_known_p ()
    2460     35395143 :           && known_eq (op->size, op->max_size))
    2461              :         {
    2462     17169142 :           if (TREE_CODE (base) == PARM_DECL)
    2463       656730 :             v = ipcp_get_aggregate_const (cfun, base, false, op_offset,
    2464              :                                           op_size);
    2465     16512412 :           else if (TREE_CODE (base) == MEM_REF
    2466      6772118 :                    && integer_zerop (TREE_OPERAND (base, 1))
    2467      5439629 :                    && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
    2468      5434430 :                    && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
    2469     20114654 :                    && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0)))
    2470              :                        == PARM_DECL))
    2471      3551740 :             v = ipcp_get_aggregate_const (cfun,
    2472      3551740 :                                           SSA_NAME_VAR (TREE_OPERAND (base, 0)),
    2473              :                                           true, op_offset, op_size);
    2474              :         }
    2475      4208470 :       if (v)
    2476         1206 :         return data->finish (vr->set, vr->base_set, v);
    2477              :     }
    2478              : 
    2479              :   return NULL;
    2480              : }
    2481              : 
    2482              : /* Lookup an existing or insert a new vn_reference entry into the
    2483              :    value table for the VUSE, SET, TYPE, OPERANDS reference which
    2484              :    has the value VALUE which is either a constant or an SSA name.  */
    2485              : 
    2486              : static vn_reference_t
    2487      1520916 : vn_reference_lookup_or_insert_for_pieces (tree vuse,
    2488              :                                           alias_set_type set,
    2489              :                                           alias_set_type base_set,
    2490              :                                           poly_int64 offset,
    2491              :                                           poly_int64 max_size,
    2492              :                                           tree type,
    2493              :                                           vec<vn_reference_op_s,
    2494              :                                                 va_heap> operands,
    2495              :                                           tree value)
    2496              : {
    2497      1520916 :   vn_reference_s vr1;
    2498      1520916 :   vn_reference_t result;
    2499      1520916 :   unsigned value_id;
    2500      1520916 :   vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
    2501      1520916 :   vr1.operands = operands;
    2502      1520916 :   vr1.type = type;
    2503      1520916 :   vr1.set = set;
    2504      1520916 :   vr1.base_set = base_set;
    2505      1520916 :   vr1.offset = offset;
    2506      1520916 :   vr1.max_size = max_size;
    2507      1520916 :   vr1.hashcode = vn_reference_compute_hash (&vr1);
    2508      1520916 :   if (vn_reference_lookup_1 (&vr1, &result))
    2509         7414 :     return result;
    2510              : 
    2511      1513502 :   if (TREE_CODE (value) == SSA_NAME)
    2512       340205 :     value_id = VN_INFO (value)->value_id;
    2513              :   else
    2514      1173297 :     value_id = get_or_alloc_constant_value_id (value);
    2515      1513502 :   return vn_reference_insert_pieces (vuse, set, base_set, offset, max_size,
    2516      1513502 :                                      type, operands.copy (), value, value_id);
    2517              : }
    2518              : 
    2519              : /* Return a value-number for RCODE OPS... either by looking up an existing
    2520              :    value-number for the possibly simplified result or by inserting the
    2521              :    operation if INSERT is true.  If SIMPLIFY is false, return a value
    2522              :    number for the unsimplified expression.  */
    2523              : 
    2524              : static tree
    2525     18488920 : vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
    2526              :                            bool simplify)
    2527              : {
    2528     18488920 :   tree result = NULL_TREE;
    2529              :   /* We will be creating a value number for
    2530              :        RCODE (OPS...).
    2531              :      So first simplify and lookup this expression to see if it
    2532              :      is already available.  */
    2533              :   /* For simplification valueize.  */
    2534     18488920 :   unsigned i = 0;
    2535     18488920 :   if (simplify)
    2536     42940891 :     for (i = 0; i < res_op->num_ops; ++i)
    2537     24457456 :       if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
    2538              :         {
    2539     15650418 :           tree tem = vn_valueize (res_op->ops[i]);
    2540     15650418 :           if (!tem)
    2541              :             break;
    2542     15650418 :           res_op->ops[i] = tem;
    2543              :         }
    2544              :   /* If valueization of an operand fails (it is not available), skip
    2545              :      simplification.  */
    2546     18488920 :   bool res = false;
    2547     18488920 :   if (i == res_op->num_ops)
    2548              :     {
    2549              :       /* Do not leak not available operands into the simplified expression
    2550              :          when called from PRE context.  */
    2551     18483435 :       if (rpo_avail)
    2552     11058820 :         mprts_hook = vn_lookup_simplify_result;
    2553     18483435 :       res = res_op->resimplify (NULL, vn_valueize);
    2554     18483435 :       mprts_hook = NULL;
    2555              :     }
    2556     31799450 :   gimple *new_stmt = NULL;
    2557     18483435 :   if (res
    2558     18483435 :       && gimple_simplified_result_is_gimple_val (res_op))
    2559              :     {
    2560              :       /* The expression is already available.  */
    2561      5172905 :       result = res_op->ops[0];
    2562              :       /* Valueize it, simplification returns sth in AVAIL only.  */
    2563      5172905 :       if (TREE_CODE (result) == SSA_NAME)
    2564       289752 :         result = SSA_VAL (result);
    2565              :     }
    2566              :   else
    2567              :     {
    2568     13316015 :       tree val = vn_lookup_simplify_result (res_op);
    2569              :       /* ???  In weird cases we can end up with internal-fn calls,
    2570              :          but this isn't expected so throw the result away.  See
    2571              :          PR123040 for an example.  */
    2572     13316015 :       if (!val && insert && res_op->code.is_tree_code ())
    2573              :         {
    2574       134239 :           gimple_seq stmts = NULL;
    2575       134239 :           result = maybe_push_res_to_seq (res_op, &stmts);
    2576       134239 :           if (result)
    2577              :             {
    2578       134233 :               gcc_assert (gimple_seq_singleton_p (stmts));
    2579       134233 :               new_stmt = gimple_seq_first_stmt (stmts);
    2580              :             }
    2581              :         }
    2582              :       else
    2583              :         /* The expression is already available.  */
    2584              :         result = val;
    2585              :     }
    2586       289758 :   if (new_stmt)
    2587              :     {
    2588              :       /* The expression is not yet available, value-number lhs to
    2589              :          the new SSA_NAME we created.  */
    2590              :       /* Initialize value-number information properly.  */
    2591       134233 :       vn_ssa_aux_t result_info = VN_INFO (result);
    2592       134233 :       result_info->valnum = result;
    2593       134233 :       result_info->value_id = get_next_value_id ();
    2594       134233 :       result_info->visited = 1;
    2595       134233 :       gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
    2596              :                                           new_stmt);
    2597       134233 :       result_info->needs_insertion = true;
    2598              :       /* ???  PRE phi-translation inserts NARYs without corresponding
    2599              :          SSA name result.  Re-use those but set their result according
    2600              :          to the stmt we just built.  */
    2601       134233 :       vn_nary_op_t nary = NULL;
    2602       134233 :       vn_nary_op_lookup_stmt (new_stmt, &nary);
    2603       134233 :       if (nary)
    2604              :         {
    2605            0 :           gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
    2606            0 :           nary->u.result = gimple_assign_lhs (new_stmt);
    2607              :         }
    2608              :       /* As all "inserted" statements are singleton SCCs, insert
    2609              :          to the valid table.  This is strictly needed to
    2610              :          avoid re-generating new value SSA_NAMEs for the same
    2611              :          expression during SCC iteration over and over (the
    2612              :          optimistic table gets cleared after each iteration).
    2613              :          We do not need to insert into the optimistic table, as
    2614              :          lookups there will fall back to the valid table.  */
    2615              :       else
    2616              :         {
    2617       134233 :           unsigned int length = vn_nary_length_from_stmt (new_stmt);
    2618       134233 :           vn_nary_op_t vno1
    2619       134233 :             = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
    2620       134233 :           vno1->value_id = result_info->value_id;
    2621       134233 :           vno1->length = length;
    2622       134233 :           vno1->predicated_values = 0;
    2623       134233 :           vno1->u.result = result;
    2624       134233 :           init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (new_stmt));
    2625       134233 :           vn_nary_op_insert_into (vno1, valid_info->nary);
    2626              :           /* Also do not link it into the undo chain.  */
    2627       134233 :           last_inserted_nary = vno1->next;
    2628       134233 :           vno1->next = (vn_nary_op_t)(void *)-1;
    2629              :         }
    2630       134233 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2631              :         {
    2632          591 :           fprintf (dump_file, "Inserting name ");
    2633          591 :           print_generic_expr (dump_file, result);
    2634          591 :           fprintf (dump_file, " for expression ");
    2635          591 :           print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
    2636          591 :           fprintf (dump_file, "\n");
    2637              :         }
    2638              :     }
    2639     18488920 :   return result;
    2640              : }
    2641              : 
    2642              : /* Return a value-number for RCODE OPS... either by looking up an existing
    2643              :    value-number for the simplified result or by inserting the operation.  */
    2644              : 
    2645              : static tree
    2646       179164 : vn_nary_build_or_lookup (gimple_match_op *res_op)
    2647              : {
    2648            0 :   return vn_nary_build_or_lookup_1 (res_op, true, true);
    2649              : }
    2650              : 
    2651              : /* Try to simplify the expression RCODE OPS... of type TYPE and return
    2652              :    its value if present.  Update NARY with a simplified expression if
    2653              :    it fits.  */
    2654              : 
    2655              : tree
    2656      7420132 : vn_nary_simplify (vn_nary_op_t nary)
    2657              : {
    2658      7420132 :   if (nary->length > gimple_match_op::MAX_NUM_OPS
    2659              :       /* For CONSTRUCTOR the vn_nary_op_t and gimple_match_op representation
    2660              :          does not match.  */
    2661      7419771 :       || nary->opcode == CONSTRUCTOR)
    2662              :     return NULL_TREE;
    2663      7418523 :   gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
    2664      7418523 :                       nary->type, nary->length);
    2665      7418523 :   memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
    2666      7418523 :   tree res = vn_nary_build_or_lookup_1 (&op, false, true);
    2667              :   /* Do not update *NARY with a simplified result that contains abnormals.
    2668              :      This matches what maybe_push_res_to_seq does when requesting insertion.  */
    2669     19473794 :   for (unsigned i = 0; i < op.num_ops; ++i)
    2670     12055352 :     if (TREE_CODE (op.ops[i]) == SSA_NAME
    2671     12055352 :         && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op.ops[i]))
    2672              :       return res;
    2673      7418442 :   if (op.code.is_tree_code ()
    2674      7418442 :       && op.num_ops <= nary->length
    2675     14836116 :       && (tree_code) op.code != CONSTRUCTOR)
    2676              :     {
    2677      7417673 :       nary->opcode = (tree_code) op.code;
    2678      7417673 :       nary->length = op.num_ops;
    2679     19471395 :       for (unsigned i = 0; i < op.num_ops; ++i)
    2680     12053722 :         nary->op[i] = op.ops[i];
    2681              :     }
    2682              :   return res;
    2683              : }
    2684              : 
    2685              : /* Elimination engine.  */
    2686              : 
    2687              : class eliminate_dom_walker : public dom_walker
    2688              : {
    2689              : public:
    2690              :   eliminate_dom_walker (cdi_direction, bitmap);
    2691              :   ~eliminate_dom_walker ();
    2692              : 
    2693              :   edge before_dom_children (basic_block) final override;
    2694              :   void after_dom_children (basic_block) final override;
    2695              : 
    2696              :   virtual tree eliminate_avail (basic_block, tree op);
    2697              :   virtual void eliminate_push_avail (basic_block, tree op);
    2698              :   tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
    2699              : 
    2700              :   void eliminate_stmt (basic_block, gimple_stmt_iterator *);
    2701              : 
    2702              :   unsigned eliminate_cleanup (bool region_p = false);
    2703              : 
    2704              :   bool do_pre;
    2705              :   unsigned int el_todo;
    2706              :   unsigned int eliminations;
    2707              :   unsigned int insertions;
    2708              : 
    2709              :   /* SSA names that had their defs inserted by PRE if do_pre.  */
    2710              :   bitmap inserted_exprs;
    2711              : 
    2712              :   /* Blocks with statements that have had their EH properties changed.  */
    2713              :   bitmap need_eh_cleanup;
    2714              : 
    2715              :   /* Blocks with statements that have had their AB properties changed.  */
    2716              :   bitmap need_ab_cleanup;
    2717              : 
    2718              :   /* Local state for the eliminate domwalk.  */
    2719              :   auto_vec<gimple *> to_remove;
    2720              :   auto_vec<gimple *> to_fixup;
    2721              :   auto_vec<tree> avail;
    2722              :   auto_vec<tree> avail_stack;
    2723              : };
    2724              : 
    2725              : /* Adaptor to the elimination engine using RPO availability.  */
    2726              : 
    2727     12200716 : class rpo_elim : public eliminate_dom_walker
    2728              : {
    2729              : public:
    2730      6100358 :   rpo_elim(basic_block entry_)
    2731     12200716 :     : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
    2732     12200716 :       m_avail_freelist (NULL) {}
    2733              : 
    2734              :   tree eliminate_avail (basic_block, tree op) final override;
    2735              : 
    2736              :   void eliminate_push_avail (basic_block, tree) final override;
    2737              : 
    2738              :   basic_block entry;
    2739              :   /* Freelist of avail entries which are allocated from the vn_ssa_aux
    2740              :      obstack.  */
    2741              :   vn_avail *m_avail_freelist;
    2742              : };
    2743              : 
    2744              : /* Return true if BASE1 and BASE2 can be adjusted so they have the
    2745              :    same address and adjust *OFFSET1 and *OFFSET2 accordingly.
    2746              :    Otherwise return false.  */
    2747              : 
    2748              : static bool
    2749      6740193 : adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
    2750              :                                        tree base2, poly_int64 *offset2)
    2751              : {
    2752      6740193 :   poly_int64 soff;
    2753      6740193 :   if (TREE_CODE (base1) == MEM_REF
    2754      3068180 :       && TREE_CODE (base2) == MEM_REF)
    2755              :     {
    2756      2461222 :       if (mem_ref_offset (base1).to_shwi (&soff))
    2757              :         {
    2758      2461222 :           base1 = TREE_OPERAND (base1, 0);
    2759      2461222 :           *offset1 += soff * BITS_PER_UNIT;
    2760              :         }
    2761      2461222 :       if (mem_ref_offset (base2).to_shwi (&soff))
    2762              :         {
    2763      2461222 :           base2 = TREE_OPERAND (base2, 0);
    2764      2461222 :           *offset2 += soff * BITS_PER_UNIT;
    2765              :         }
    2766      2461222 :       return operand_equal_p (base1, base2, 0);
    2767              :     }
    2768      4278971 :   return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
    2769              : }
    2770              : 
    2771              : /* Callback for walk_non_aliased_vuses.  Tries to perform a lookup
    2772              :    from the statement defining VUSE and if not successful tries to
    2773              :    translate *REFP and VR_ through an aggregate copy at the definition
    2774              :    of VUSE.  If *DISAMBIGUATE_ONLY is true then do not perform translation
    2775              :    of *REF and *VR.  If only disambiguation was performed then
    2776              :    *DISAMBIGUATE_ONLY is set to true.  */
    2777              : 
    2778              : static void *
    2779     41883662 : vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
    2780              :                        translate_flags *disambiguate_only)
    2781              : {
    2782     41883662 :   vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
    2783     41883662 :   vn_reference_t vr = data->vr;
    2784     41883662 :   gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
    2785     41883662 :   tree base = ao_ref_base (ref);
    2786     41883662 :   HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
    2787     41883662 :   static vec<vn_reference_op_s> lhs_ops;
    2788     41883662 :   ao_ref lhs_ref;
    2789     41883662 :   bool lhs_ref_ok = false;
    2790     41883662 :   poly_int64 copy_size;
    2791              : 
    2792              :   /* First try to disambiguate after value-replacing in the definitions LHS.  */
    2793     41883662 :   if (is_gimple_assign (def_stmt))
    2794              :     {
    2795     20672745 :       tree lhs = gimple_assign_lhs (def_stmt);
    2796     20672745 :       bool valueized_anything = false;
    2797              :       /* Avoid re-allocation overhead.  */
    2798     20672745 :       lhs_ops.truncate (0);
    2799     20672745 :       basic_block saved_rpo_bb = vn_context_bb;
    2800     20672745 :       vn_context_bb = gimple_bb (def_stmt);
    2801     20672745 :       if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
    2802              :         {
    2803     13433120 :           copy_reference_ops_from_ref (lhs, &lhs_ops);
    2804     13433120 :           valueize_refs_1 (&lhs_ops, &valueized_anything, true);
    2805              :         }
    2806     20672745 :       vn_context_bb = saved_rpo_bb;
    2807     20672745 :       ao_ref_init (&lhs_ref, lhs);
    2808     20672745 :       lhs_ref_ok = true;
    2809     20672745 :       if (valueized_anything
    2810      1984625 :           && ao_ref_init_from_vn_reference
    2811      1984625 :                (&lhs_ref, ao_ref_alias_set (&lhs_ref),
    2812      1984625 :                 ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
    2813     22657370 :           && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
    2814              :         {
    2815      1698069 :           *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
    2816      8235297 :           return NULL;
    2817              :         }
    2818              : 
    2819              :       /* When the def is a CLOBBER we can optimistically disambiguate
    2820              :          against it since any overlap it would be undefined behavior.
    2821              :          Avoid this for obvious must aliases to save compile-time though.
    2822              :          We also may not do this when the query is used for redundant
    2823              :          store removal.  */
    2824     18974676 :       if (!data->redundant_store_removal_p
    2825     10420718 :           && gimple_clobber_p (def_stmt)
    2826     19464192 :           && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
    2827              :         {
    2828       464740 :           *disambiguate_only = TR_DISAMBIGUATE;
    2829       464740 :           return NULL;
    2830              :         }
    2831              : 
    2832              :       /* Besides valueizing the LHS we can also use access-path based
    2833              :          disambiguation on the original non-valueized ref.  */
    2834     18509936 :       if (!ref->ref
    2835              :           && lhs_ref_ok
    2836      2620327 :           && data->orig_ref.ref)
    2837              :         {
    2838              :           /* We want to use the non-valueized LHS for this, but avoid redundant
    2839              :              work.  */
    2840      1819591 :           ao_ref *lref = &lhs_ref;
    2841      1819591 :           ao_ref lref_alt;
    2842      1819591 :           if (valueized_anything)
    2843              :             {
    2844       113592 :               ao_ref_init (&lref_alt, lhs);
    2845       113592 :               lref = &lref_alt;
    2846              :             }
    2847      1819591 :           if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
    2848              :             {
    2849       251424 :               *disambiguate_only = (valueized_anything
    2850       125712 :                                     ? TR_VALUEIZE_AND_DISAMBIGUATE
    2851              :                                     : TR_DISAMBIGUATE);
    2852       125712 :               return NULL;
    2853              :             }
    2854              :         }
    2855              : 
    2856              :       /* If we reach a clobbering statement try to skip it and see if
    2857              :          we find a VN result with exactly the same value as the
    2858              :          possible clobber.  In this case we can ignore the clobber
    2859              :          and return the found value.  */
    2860     18384224 :       if (!gimple_has_volatile_ops (def_stmt)
    2861     17053935 :           && ((is_gimple_reg_type (TREE_TYPE (lhs))
    2862     12523646 :                && types_compatible_p (TREE_TYPE (lhs), vr->type)
    2863      9782547 :                && !storage_order_barrier_p (lhs)
    2864      9782547 :                && !reverse_storage_order_for_component_p (lhs))
    2865      7271392 :               || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == CONSTRUCTOR)
    2866     10834358 :           && (ref->ref || data->orig_ref.ref)
    2867     10368330 :           && !data->mask
    2868     10346147 :           && data->partial_defs.is_empty ()
    2869     10343916 :           && multiple_p (get_object_alignment
    2870              :                            (ref->ref ? ref->ref : data->orig_ref.ref),
    2871              :                            ref->size)
    2872     41140238 :           && multiple_p (get_object_alignment (lhs), ref->size))
    2873              :         {
    2874      9950786 :           HOST_WIDE_INT offset2i, size2i;
    2875      9950786 :           poly_int64 offset = ref->offset;
    2876      9950786 :           poly_int64 maxsize = ref->max_size;
    2877              : 
    2878      9950786 :           gcc_assert (lhs_ref_ok);
    2879      9950786 :           tree base2 = ao_ref_base (&lhs_ref);
    2880      9950786 :           poly_int64 offset2 = lhs_ref.offset;
    2881      9950786 :           poly_int64 size2 = lhs_ref.size;
    2882      9950786 :           poly_int64 maxsize2 = lhs_ref.max_size;
    2883              : 
    2884      9950786 :           tree rhs = gimple_assign_rhs1 (def_stmt);
    2885      9950786 :           if (TREE_CODE (rhs) == CONSTRUCTOR)
    2886      1023422 :             rhs = integer_zero_node;
    2887              :           /* ???  We may not compare to ahead values which might be from
    2888              :              a different loop iteration but only to loop invariants.  Use
    2889              :              CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
    2890              :              The one-hop lookup below doesn't have this issue since there's
    2891              :              a virtual PHI before we ever reach a backedge to cross.
    2892              :              We can skip multiple defs as long as they are from the same
    2893              :              value though.  */
    2894      9950786 :           if (data->same_val
    2895      9950786 :               && !operand_equal_p (data->same_val, rhs))
    2896              :             ;
    2897              :           /* When this is a (partial) must-def, leave it to handling
    2898              :              below in case we are interested in the value.  */
    2899      9664971 :           else if (!(*disambiguate_only > TR_TRANSLATE)
    2900      3317228 :                    && base2
    2901      3317228 :                    && known_eq (maxsize2, size2)
    2902      2316130 :                    && adjust_offsets_for_equal_base_address (base, &offset,
    2903              :                                                              base2, &offset2)
    2904      1133871 :                    && offset2.is_constant (&offset2i)
    2905      1133871 :                    && size2.is_constant (&size2i)
    2906      1133871 :                    && maxsize.is_constant (&maxsizei)
    2907      1133871 :                    && offset.is_constant (&offseti)
    2908     10798842 :                    && ranges_known_overlap_p (offseti, maxsizei, offset2i,
    2909              :                                               size2i))
    2910              :             ;
    2911      8617278 :           else if (CONSTANT_CLASS_P (rhs))
    2912              :             {
    2913      4185088 :               if (dump_file && (dump_flags & TDF_DETAILS))
    2914              :                 {
    2915         2194 :                   fprintf (dump_file,
    2916              :                            "Skipping possible redundant definition ");
    2917         2194 :                   print_gimple_stmt (dump_file, def_stmt, 0);
    2918              :                 }
    2919              :               /* Delay the actual compare of the values to the end of the walk
    2920              :                  but do not update last_vuse from here.  */
    2921      4185088 :               data->last_vuse_ptr = NULL;
    2922      4185088 :               data->same_val = rhs;
    2923      4248707 :               return NULL;
    2924              :             }
    2925              :           else
    2926              :             {
    2927      4432190 :               tree saved_vuse = vr->vuse;
    2928      4432190 :               hashval_t saved_hashcode = vr->hashcode;
    2929      4432190 :               if (vr->vuse)
    2930      4432190 :                 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
    2931      8864380 :               vr->vuse = vuse_ssa_val (gimple_vuse (def_stmt));
    2932      4432190 :               if (vr->vuse)
    2933      4432190 :                 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
    2934      4432190 :               vn_reference_t vnresult = NULL;
    2935              :               /* Do not use vn_reference_lookup_2 since that might perform
    2936              :                  expression hashtable insertion but this lookup crosses
    2937              :                  a possible may-alias making such insertion conditionally
    2938              :                  invalid.  */
    2939      4432190 :               vn_reference_lookup_1 (vr, &vnresult);
    2940              :               /* Need to restore vr->vuse and vr->hashcode.  */
    2941      4432190 :               vr->vuse = saved_vuse;
    2942      4432190 :               vr->hashcode = saved_hashcode;
    2943      4432190 :               if (vnresult)
    2944              :                 {
    2945       241352 :                   if (TREE_CODE (rhs) == SSA_NAME)
    2946       239831 :                     rhs = SSA_VAL (rhs);
    2947       241352 :                   if (vnresult->result
    2948       241352 :                       && operand_equal_p (vnresult->result, rhs, 0))
    2949        63619 :                     return vnresult;
    2950              :                 }
    2951              :             }
    2952              :         }
    2953              :     }
    2954     21210917 :   else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
    2955     19046173 :            && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
    2956     23278859 :            && gimple_call_num_args (def_stmt) <= 4)
    2957              :     {
    2958              :       /* For builtin calls valueize its arguments and call the
    2959              :          alias oracle again.  Valueization may improve points-to
    2960              :          info of pointers and constify size and position arguments.
    2961              :          Originally this was motivated by PR61034 which has
    2962              :          conditional calls to free falsely clobbering ref because
    2963              :          of imprecise points-to info of the argument.  */
    2964              :       tree oldargs[4];
    2965              :       bool valueized_anything = false;
    2966      4820834 :       for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
    2967              :         {
    2968      3310331 :           oldargs[i] = gimple_call_arg (def_stmt, i);
    2969      3310331 :           tree val = vn_valueize (oldargs[i]);
    2970      3310331 :           if (val != oldargs[i])
    2971              :             {
    2972       119768 :               gimple_call_set_arg (def_stmt, i, val);
    2973       119768 :               valueized_anything = true;
    2974              :             }
    2975              :         }
    2976      1510503 :       if (valueized_anything)
    2977              :         {
    2978       186022 :           bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
    2979        93011 :                                                ref, data->tbaa_p);
    2980       337559 :           for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
    2981       244548 :             gimple_call_set_arg (def_stmt, i, oldargs[i]);
    2982        93011 :           if (!res)
    2983              :             {
    2984        27110 :               *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
    2985        27110 :               return NULL;
    2986              :             }
    2987              :         }
    2988              :     }
    2989              : 
    2990     35319324 :   if (*disambiguate_only > TR_TRANSLATE)
    2991              :     return (void *)-1;
    2992              : 
    2993              :   /* If we cannot constrain the size of the reference we cannot
    2994              :      test if anything kills it.  */
    2995     23558427 :   if (!ref->max_size_known_p ())
    2996              :     return (void *)-1;
    2997              : 
    2998     23136969 :   poly_int64 offset = ref->offset;
    2999     23136969 :   poly_int64 maxsize = ref->max_size;
    3000              : 
    3001              :   /* def_stmt may-defs *ref.  See if we can derive a value for *ref
    3002              :      from that definition.
    3003              :      1) Memset.  */
    3004     23136969 :   if (is_gimple_reg_type (vr->type)
    3005     23131163 :       && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
    3006     23041592 :           || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
    3007        90113 :       && (integer_zerop (gimple_call_arg (def_stmt, 1))
    3008        32487 :           || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
    3009         9049 :                || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
    3010              :               && CHAR_BIT == 8
    3011              :               && BITS_PER_UNIT == 8
    3012              :               && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
    3013        31230 :               && offset.is_constant (&offseti)
    3014        31230 :               && ref->size.is_constant (&sizei)
    3015        31230 :               && (offseti % BITS_PER_UNIT == 0
    3016           39 :                   || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
    3017        88856 :       && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
    3018        36304 :           || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
    3019        36304 :               && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
    3020     23190084 :       && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
    3021        30420 :           || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
    3022              :     {
    3023        53074 :       tree base2;
    3024        53074 :       poly_int64 offset2, size2, maxsize2;
    3025        53074 :       bool reverse;
    3026        53074 :       tree ref2 = gimple_call_arg (def_stmt, 0);
    3027        53074 :       if (TREE_CODE (ref2) == SSA_NAME)
    3028              :         {
    3029        30379 :           ref2 = SSA_VAL (ref2);
    3030        30379 :           if (TREE_CODE (ref2) == SSA_NAME
    3031        30379 :               && (TREE_CODE (base) != MEM_REF
    3032        19383 :                   || TREE_OPERAND (base, 0) != ref2))
    3033              :             {
    3034        24025 :               gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
    3035        24025 :               if (gimple_assign_single_p (def_stmt)
    3036        24025 :                   && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
    3037          802 :                 ref2 = gimple_assign_rhs1 (def_stmt);
    3038              :             }
    3039              :         }
    3040        53074 :       if (TREE_CODE (ref2) == ADDR_EXPR)
    3041              :         {
    3042        26482 :           ref2 = TREE_OPERAND (ref2, 0);
    3043        26482 :           base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
    3044              :                                            &reverse);
    3045        26482 :           if (!known_size_p (maxsize2)
    3046        26442 :               || !known_eq (maxsize2, size2)
    3047        52850 :               || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
    3048        56554 :             return (void *)-1;
    3049              :         }
    3050        26592 :       else if (TREE_CODE (ref2) == SSA_NAME)
    3051              :         {
    3052        26592 :           poly_int64 soff;
    3053        26592 :           if (TREE_CODE (base) != MEM_REF
    3054        45392 :               || !(mem_ref_offset (base)
    3055        37600 :                    << LOG2_BITS_PER_UNIT).to_shwi (&soff))
    3056        22628 :             return (void *)-1;
    3057        18800 :           offset += soff;
    3058        18800 :           offset2 = 0;
    3059        18800 :           if (TREE_OPERAND (base, 0) != ref2)
    3060              :             {
    3061        15431 :               gimple *def = SSA_NAME_DEF_STMT (ref2);
    3062        15431 :               if (is_gimple_assign (def)
    3063        14121 :                   && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
    3064        12141 :                   && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
    3065        16056 :                   && poly_int_tree_p (gimple_assign_rhs2 (def)))
    3066              :                 {
    3067          595 :                   tree rhs2 = gimple_assign_rhs2 (def);
    3068          595 :                   if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
    3069              :                                                SIGNED)
    3070          595 :                         << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
    3071              :                     return (void *)-1;
    3072          595 :                   ref2 = gimple_assign_rhs1 (def);
    3073          595 :                   if (TREE_CODE (ref2) == SSA_NAME)
    3074          595 :                     ref2 = SSA_VAL (ref2);
    3075              :                 }
    3076              :               else
    3077              :                 return (void *)-1;
    3078              :             }
    3079              :         }
    3080              :       else
    3081              :         return (void *)-1;
    3082        26578 :       tree len = gimple_call_arg (def_stmt, 2);
    3083        26578 :       HOST_WIDE_INT leni, offset2i;
    3084        26578 :       if (TREE_CODE (len) == SSA_NAME)
    3085          255 :         len = SSA_VAL (len);
    3086              :       /* Sometimes the above trickery is smarter than alias analysis.  Take
    3087              :          advantage of that.  */
    3088        26578 :       if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
    3089        53156 :                                    (wi::to_poly_offset (len)
    3090        26578 :                                     << LOG2_BITS_PER_UNIT)))
    3091              :         return NULL;
    3092        53099 :       if (data->partial_defs.is_empty ()
    3093        26521 :           && known_subrange_p (offset, maxsize, offset2,
    3094        26521 :                                wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
    3095              :         {
    3096        26027 :           tree val;
    3097        26027 :           if (integer_zerop (gimple_call_arg (def_stmt, 1)))
    3098        21191 :             val = build_zero_cst (vr->type);
    3099         4836 :           else if (INTEGRAL_TYPE_P (vr->type)
    3100         3696 :                    && known_eq (ref->size, 8)
    3101         7777 :                    && offseti % BITS_PER_UNIT == 0)
    3102              :             {
    3103         2941 :               gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
    3104         2941 :                                       vr->type, gimple_call_arg (def_stmt, 1));
    3105         2941 :               val = vn_nary_build_or_lookup (&res_op);
    3106         2941 :               if (!val
    3107         2941 :                   || (TREE_CODE (val) == SSA_NAME
    3108          626 :                       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
    3109            0 :                 return (void *)-1;
    3110              :             }
    3111              :           else
    3112              :             {
    3113         1895 :               unsigned buflen
    3114         1895 :                 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
    3115         1895 :               if (INTEGRAL_TYPE_P (vr->type)
    3116         1895 :                   && TYPE_MODE (vr->type) != BLKmode)
    3117         1508 :                 buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
    3118         1895 :               unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
    3119         1895 :               memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
    3120              :                       buflen);
    3121         1895 :               if (BYTES_BIG_ENDIAN)
    3122              :                 {
    3123              :                   unsigned int amnt
    3124              :                     = (((unsigned HOST_WIDE_INT) offseti + sizei)
    3125              :                        % BITS_PER_UNIT);
    3126              :                   if (amnt)
    3127              :                     {
    3128              :                       shift_bytes_in_array_right (buf, buflen,
    3129              :                                                   BITS_PER_UNIT - amnt);
    3130              :                       buf++;
    3131              :                       buflen--;
    3132              :                     }
    3133              :                 }
    3134         1895 :               else if (offseti % BITS_PER_UNIT != 0)
    3135              :                 {
    3136            7 :                   unsigned int amnt
    3137              :                     = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
    3138            7 :                                        % BITS_PER_UNIT);
    3139            7 :                   shift_bytes_in_array_left (buf, buflen, amnt);
    3140            7 :                   buf++;
    3141            7 :                   buflen--;
    3142              :                 }
    3143         1895 :               val = native_interpret_expr (vr->type, buf, buflen);
    3144         1895 :               if (!val)
    3145              :                 return (void *)-1;
    3146              :             }
    3147        26027 :           return data->finish (0, 0, val);
    3148              :         }
    3149              :       /* For now handle clearing memory with partial defs.  */
    3150          551 :       else if (known_eq (ref->size, maxsize)
    3151          480 :                && integer_zerop (gimple_call_arg (def_stmt, 1))
    3152          167 :                && tree_fits_poly_int64_p (len)
    3153          163 :                && tree_to_poly_int64 (len).is_constant (&leni)
    3154          163 :                && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
    3155          163 :                && offset.is_constant (&offseti)
    3156          163 :                && offset2.is_constant (&offset2i)
    3157          163 :                && maxsize.is_constant (&maxsizei)
    3158          551 :                && ranges_known_overlap_p (offseti, maxsizei, offset2i,
    3159          551 :                                           leni << LOG2_BITS_PER_UNIT))
    3160              :         {
    3161          163 :           pd_data pd;
    3162          163 :           pd.rhs = build_constructor (NULL_TREE, NULL);
    3163          163 :           pd.rhs_off = 0;
    3164          163 :           pd.offset = offset2i;
    3165          163 :           pd.size = leni << LOG2_BITS_PER_UNIT;
    3166          163 :           return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
    3167              :         }
    3168              :     }
    3169              : 
    3170              :   /* 2) Assignment from an empty CONSTRUCTOR.  */
    3171     23083895 :   else if (is_gimple_reg_type (vr->type)
    3172     23078089 :            && gimple_assign_single_p (def_stmt)
    3173      7661928 :            && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
    3174      1925125 :            && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0
    3175     25009020 :            && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt)))
    3176              :     {
    3177      1925093 :       tree base2;
    3178      1925093 :       poly_int64 offset2, size2, maxsize2;
    3179      1925093 :       HOST_WIDE_INT offset2i, size2i;
    3180      1925093 :       gcc_assert (lhs_ref_ok);
    3181      1925093 :       base2 = ao_ref_base (&lhs_ref);
    3182      1925093 :       offset2 = lhs_ref.offset;
    3183      1925093 :       size2 = lhs_ref.size;
    3184      1925093 :       maxsize2 = lhs_ref.max_size;
    3185      1925093 :       if (known_size_p (maxsize2)
    3186      1925055 :           && known_eq (maxsize2, size2)
    3187      3850102 :           && adjust_offsets_for_equal_base_address (base, &offset,
    3188              :                                                     base2, &offset2))
    3189              :         {
    3190      1898760 :           if (data->partial_defs.is_empty ()
    3191      1895230 :               && known_subrange_p (offset, maxsize, offset2, size2))
    3192              :             {
    3193              :               /* While technically undefined behavior do not optimize
    3194              :                  a full read from a clobber.  */
    3195      1894361 :               if (gimple_clobber_p (def_stmt))
    3196      1898710 :                 return (void *)-1;
    3197       973683 :               tree val = build_zero_cst (vr->type);
    3198       973683 :               return data->finish (ao_ref_alias_set (&lhs_ref),
    3199       973683 :                                    ao_ref_base_alias_set (&lhs_ref), val);
    3200              :             }
    3201         4399 :           else if (known_eq (ref->size, maxsize)
    3202         4349 :                    && maxsize.is_constant (&maxsizei)
    3203         4349 :                    && offset.is_constant (&offseti)
    3204         4349 :                    && offset2.is_constant (&offset2i)
    3205         4349 :                    && size2.is_constant (&size2i)
    3206         4399 :                    && ranges_known_overlap_p (offseti, maxsizei,
    3207              :                                               offset2i, size2i))
    3208              :             {
    3209              :               /* Let clobbers be consumed by the partial-def tracker
    3210              :                  which can choose to ignore them if they are shadowed
    3211              :                  by a later def.  */
    3212         4349 :               pd_data pd;
    3213         4349 :               pd.rhs = gimple_assign_rhs1 (def_stmt);
    3214         4349 :               pd.rhs_off = 0;
    3215         4349 :               pd.offset = offset2i;
    3216         4349 :               pd.size = size2i;
    3217         4349 :               return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
    3218              :                                              ao_ref_base_alias_set (&lhs_ref),
    3219              :                                              offseti, maxsizei);
    3220              :             }
    3221              :         }
    3222              :     }
    3223              : 
    3224              :   /* 3) Assignment from a constant.  We can use folds native encode/interpret
    3225              :      routines to extract the assigned bits.  */
    3226     21158802 :   else if (known_eq (ref->size, maxsize)
    3227     20636154 :            && is_gimple_reg_type (vr->type)
    3228     20630348 :            && !reverse_storage_order_for_component_p (vr->operands)
    3229     20627592 :            && !contains_storage_order_barrier_p (vr->operands)
    3230     20627592 :            && gimple_assign_single_p (def_stmt)
    3231      5412229 :            && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
    3232              :            && CHAR_BIT == 8
    3233              :            && BITS_PER_UNIT == 8
    3234              :            && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
    3235              :            /* native_encode and native_decode operate on arrays of bytes
    3236              :               and so fundamentally need a compile-time size and offset.  */
    3237      5409268 :            && maxsize.is_constant (&maxsizei)
    3238      5409268 :            && offset.is_constant (&offseti)
    3239     26568070 :            && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
    3240      4589919 :                || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
    3241      1856784 :                    && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
    3242              :     {
    3243       835739 :       tree lhs = gimple_assign_lhs (def_stmt);
    3244       835739 :       tree base2;
    3245       835739 :       poly_int64 offset2, size2, maxsize2;
    3246       835739 :       HOST_WIDE_INT offset2i, size2i;
    3247       835739 :       bool reverse;
    3248       835739 :       gcc_assert (lhs_ref_ok);
    3249       835739 :       base2 = ao_ref_base (&lhs_ref);
    3250       835739 :       offset2 = lhs_ref.offset;
    3251       835739 :       size2 = lhs_ref.size;
    3252       835739 :       maxsize2 = lhs_ref.max_size;
    3253       835739 :       reverse = reverse_storage_order_for_component_p (lhs);
    3254       835739 :       if (base2
    3255       835739 :           && !reverse
    3256       834911 :           && !storage_order_barrier_p (lhs)
    3257       834911 :           && known_eq (maxsize2, size2)
    3258       803189 :           && adjust_offsets_for_equal_base_address (base, &offset,
    3259              :                                                     base2, &offset2)
    3260        82154 :           && offset.is_constant (&offseti)
    3261        82154 :           && offset2.is_constant (&offset2i)
    3262       835739 :           && size2.is_constant (&size2i))
    3263              :         {
    3264        82154 :           if (data->partial_defs.is_empty ()
    3265        65322 :               && known_subrange_p (offseti, maxsizei, offset2, size2))
    3266              :             {
    3267              :               /* We support up to 512-bit values (for V8DFmode).  */
    3268        42747 :               unsigned char buffer[65];
    3269        42747 :               int len;
    3270              : 
    3271        42747 :               tree rhs = gimple_assign_rhs1 (def_stmt);
    3272        42747 :               if (TREE_CODE (rhs) == SSA_NAME)
    3273         1657 :                 rhs = SSA_VAL (rhs);
    3274        85494 :               len = native_encode_expr (rhs,
    3275              :                                         buffer, sizeof (buffer) - 1,
    3276        42747 :                                         (offseti - offset2i) / BITS_PER_UNIT);
    3277        42747 :               if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
    3278              :                 {
    3279        39725 :                   tree type = vr->type;
    3280        39725 :                   unsigned char *buf = buffer;
    3281        39725 :                   unsigned int amnt = 0;
    3282              :                   /* Make sure to interpret in a type that has a range
    3283              :                      covering the whole access size.  */
    3284        39725 :                   if (INTEGRAL_TYPE_P (vr->type)
    3285        39725 :                       && maxsizei != TYPE_PRECISION (vr->type))
    3286              :                     {
    3287          884 :                       bool uns = TYPE_UNSIGNED (type);
    3288          883 :                       if (BITINT_TYPE_P (vr->type)
    3289          885 :                           && maxsizei > MAX_FIXED_MODE_SIZE)
    3290            1 :                         type = build_bitint_type (maxsizei, uns);
    3291              :                       else
    3292          883 :                         type = build_nonstandard_integer_type (maxsizei, uns);
    3293              :                     }
    3294        39725 :                   if (BYTES_BIG_ENDIAN)
    3295              :                     {
    3296              :                       /* For big-endian native_encode_expr stored the rhs
    3297              :                          such that the LSB of it is the LSB of buffer[len - 1].
    3298              :                          That bit is stored into memory at position
    3299              :                          offset2 + size2 - 1, i.e. in byte
    3300              :                          base + (offset2 + size2 - 1) / BITS_PER_UNIT.
    3301              :                          E.g. for offset2 1 and size2 14, rhs -1 and memory
    3302              :                          previously cleared that is:
    3303              :                          0        1
    3304              :                          01111111|11111110
    3305              :                          Now, if we want to extract offset 2 and size 12 from
    3306              :                          it using native_interpret_expr (which actually works
    3307              :                          for integral bitfield types in terms of byte size of
    3308              :                          the mode), the native_encode_expr stored the value
    3309              :                          into buffer as
    3310              :                          XX111111|11111111
    3311              :                          and returned len 2 (the X bits are outside of
    3312              :                          precision).
    3313              :                          Let sz be maxsize / BITS_PER_UNIT if not extracting
    3314              :                          a bitfield, and GET_MODE_SIZE otherwise.
    3315              :                          We need to align the LSB of the value we want to
    3316              :                          extract as the LSB of buf[sz - 1].
    3317              :                          The LSB from memory we need to read is at position
    3318              :                          offset + maxsize - 1.  */
    3319              :                       HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
    3320              :                       if (INTEGRAL_TYPE_P (type))
    3321              :                         {
    3322              :                           if (TYPE_MODE (type) != BLKmode)
    3323              :                             sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
    3324              :                           else
    3325              :                             sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
    3326              :                         }
    3327              :                       amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
    3328              :                               - offseti - maxsizei) % BITS_PER_UNIT;
    3329              :                       if (amnt)
    3330              :                         shift_bytes_in_array_right (buffer, len, amnt);
    3331              :                       amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
    3332              :                               - offseti - maxsizei - amnt) / BITS_PER_UNIT;
    3333              :                       if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
    3334              :                         len = 0;
    3335              :                       else
    3336              :                         {
    3337              :                           buf = buffer + len - sz - amnt;
    3338              :                           len -= (buf - buffer);
    3339              :                         }
    3340              :                     }
    3341              :                   else
    3342              :                     {
    3343        39725 :                       amnt = ((unsigned HOST_WIDE_INT) offset2i
    3344        39725 :                               - offseti) % BITS_PER_UNIT;
    3345        39725 :                       if (amnt)
    3346              :                         {
    3347          315 :                           buffer[len] = 0;
    3348          315 :                           shift_bytes_in_array_left (buffer, len + 1, amnt);
    3349          315 :                           buf = buffer + 1;
    3350              :                         }
    3351              :                     }
    3352        39725 :                   tree val = native_interpret_expr (type, buf, len);
    3353              :                   /* If we chop off bits because the types precision doesn't
    3354              :                      match the memory access size this is ok when optimizing
    3355              :                      reads but not when called from the DSE code during
    3356              :                      elimination.  */
    3357        39725 :                   if (val
    3358        39723 :                       && type != vr->type)
    3359              :                     {
    3360          884 :                       if (! int_fits_type_p (val, vr->type))
    3361              :                         val = NULL_TREE;
    3362              :                       else
    3363          884 :                         val = fold_convert (vr->type, val);
    3364              :                     }
    3365              : 
    3366        39723 :                   if (val)
    3367        39723 :                     return data->finish (ao_ref_alias_set (&lhs_ref),
    3368        39723 :                                          ao_ref_base_alias_set (&lhs_ref), val);
    3369              :                 }
    3370              :             }
    3371        39407 :           else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
    3372              :                                            size2i))
    3373              :             {
    3374        39407 :               pd_data pd;
    3375        39407 :               tree rhs = gimple_assign_rhs1 (def_stmt);
    3376        39407 :               if (TREE_CODE (rhs) == SSA_NAME)
    3377         2182 :                 rhs = SSA_VAL (rhs);
    3378        39407 :               pd.rhs = rhs;
    3379        39407 :               pd.rhs_off = 0;
    3380        39407 :               pd.offset = offset2i;
    3381        39407 :               pd.size = size2i;
    3382        39407 :               return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
    3383              :                                              ao_ref_base_alias_set (&lhs_ref),
    3384              :                                              offseti, maxsizei);
    3385              :             }
    3386              :         }
    3387              :     }
    3388              : 
    3389              :   /* 4) Assignment from an SSA name which definition we may be able
    3390              :      to access pieces from or we can combine to a larger entity.  */
    3391     20323063 :   else if (known_eq (ref->size, maxsize)
    3392     19800415 :            && is_gimple_reg_type (vr->type)
    3393     19794609 :            && !reverse_storage_order_for_component_p (vr->operands)
    3394     19791853 :            && !contains_storage_order_barrier_p (vr->operands)
    3395     19791853 :            && gimple_assign_single_p (def_stmt)
    3396      4576490 :            && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
    3397     24896592 :            && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
    3398              :     {
    3399      1840394 :       tree lhs = gimple_assign_lhs (def_stmt);
    3400      1840394 :       tree base2;
    3401      1840394 :       poly_int64 offset2, size2, maxsize2;
    3402      1840394 :       HOST_WIDE_INT offset2i, size2i, offseti;
    3403      1840394 :       bool reverse;
    3404      1840394 :       gcc_assert (lhs_ref_ok);
    3405      1840394 :       base2 = ao_ref_base (&lhs_ref);
    3406      1840394 :       offset2 = lhs_ref.offset;
    3407      1840394 :       size2 = lhs_ref.size;
    3408      1840394 :       maxsize2 = lhs_ref.max_size;
    3409      1840394 :       reverse = reverse_storage_order_for_component_p (lhs);
    3410      1840394 :       tree def_rhs = gimple_assign_rhs1 (def_stmt);
    3411      1840394 :       if (!reverse
    3412      1840182 :           && !storage_order_barrier_p (lhs)
    3413      1840182 :           && known_size_p (maxsize2)
    3414      1815356 :           && known_eq (maxsize2, size2)
    3415      3536245 :           && adjust_offsets_for_equal_base_address (base, &offset,
    3416              :                                                     base2, &offset2))
    3417              :         {
    3418        79482 :           if (data->partial_defs.is_empty ()
    3419        73370 :               && known_subrange_p (offset, maxsize, offset2, size2)
    3420              :               /* ???  We can't handle bitfield precision extracts without
    3421              :                  either using an alternate type for the BIT_FIELD_REF and
    3422              :                  then doing a conversion or possibly adjusting the offset
    3423              :                  according to endianness.  */
    3424        50151 :               && (! INTEGRAL_TYPE_P (vr->type)
    3425        37039 :                   || known_eq (ref->size, TYPE_PRECISION (vr->type)))
    3426        91571 :               && multiple_p (ref->size, BITS_PER_UNIT))
    3427              :             {
    3428        44075 :               tree val = NULL_TREE;
    3429        88144 :               if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
    3430        48593 :                   || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
    3431              :                 {
    3432        43016 :                   gimple_match_op op (gimple_match_cond::UNCOND,
    3433        43016 :                                       BIT_FIELD_REF, vr->type,
    3434              :                                       SSA_VAL (def_rhs),
    3435              :                                       bitsize_int (ref->size),
    3436        43016 :                                       bitsize_int (offset - offset2));
    3437        43016 :                   val = vn_nary_build_or_lookup (&op);
    3438              :                 }
    3439         1059 :               else if (known_eq (ref->size, size2))
    3440              :                 {
    3441          985 :                   gimple_match_op op (gimple_match_cond::UNCOND,
    3442          985 :                                       VIEW_CONVERT_EXPR, vr->type,
    3443          985 :                                       SSA_VAL (def_rhs));
    3444          985 :                   val = vn_nary_build_or_lookup (&op);
    3445              :                 }
    3446        44001 :               if (val
    3447        44001 :                   && (TREE_CODE (val) != SSA_NAME
    3448        43251 :                       || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
    3449        43982 :                 return data->finish (ao_ref_alias_set (&lhs_ref),
    3450        79389 :                                      ao_ref_base_alias_set (&lhs_ref), val);
    3451              :             }
    3452        35407 :           else if (maxsize.is_constant (&maxsizei)
    3453        35407 :                    && offset.is_constant (&offseti)
    3454        35407 :                    && offset2.is_constant (&offset2i)
    3455        35407 :                    && size2.is_constant (&size2i)
    3456        35407 :                    && ranges_known_overlap_p (offset, maxsize, offset2, size2))
    3457              :             {
    3458        35407 :               pd_data pd;
    3459        35407 :               pd.rhs = SSA_VAL (def_rhs);
    3460        35407 :               pd.rhs_off = 0;
    3461        35407 :               pd.offset = offset2i;
    3462        35407 :               pd.size = size2i;
    3463        35407 :               return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
    3464              :                                              ao_ref_base_alias_set (&lhs_ref),
    3465              :                                              offseti, maxsizei);
    3466              :             }
    3467              :         }
    3468              :     }
    3469              : 
    3470              :   /* 4b) Assignment done via one of the vectorizer internal store
    3471              :      functions where we may be able to access pieces from or we can
    3472              :      combine to a larger entity.  */
    3473     18482669 :   else if (known_eq (ref->size, maxsize)
    3474     17960021 :            && is_gimple_reg_type (vr->type)
    3475     17954215 :            && !reverse_storage_order_for_component_p (vr->operands)
    3476     17951459 :            && !contains_storage_order_barrier_p (vr->operands)
    3477     17951459 :            && is_gimple_call (def_stmt)
    3478     14424600 :            && gimple_call_internal_p (def_stmt)
    3479     18741188 :            && internal_store_fn_p (gimple_call_internal_fn (def_stmt)))
    3480              :     {
    3481           36 :       gcall *call = as_a <gcall *> (def_stmt);
    3482           36 :       internal_fn fn = gimple_call_internal_fn (call);
    3483              : 
    3484           36 :       tree mask = NULL_TREE, len = NULL_TREE, bias = NULL_TREE;
    3485           36 :       switch (fn)
    3486              :         {
    3487           36 :         case IFN_MASK_STORE:
    3488           36 :           mask = gimple_call_arg (call, internal_fn_mask_index (fn));
    3489           36 :           mask = vn_valueize (mask);
    3490           36 :           if (TREE_CODE (mask) != VECTOR_CST)
    3491           28 :             return (void *)-1;
    3492              :           break;
    3493            0 :         case IFN_LEN_STORE:
    3494            0 :           {
    3495            0 :             int len_index = internal_fn_len_index (fn);
    3496            0 :             len = gimple_call_arg (call, len_index);
    3497            0 :             bias = gimple_call_arg (call, len_index + 1);
    3498            0 :             if (!tree_fits_uhwi_p (len) || !tree_fits_shwi_p (bias))
    3499              :               return (void *) -1;
    3500              :             break;
    3501              :           }
    3502              :         default:
    3503              :           return (void *)-1;
    3504              :         }
    3505           14 :       tree def_rhs = gimple_call_arg (call,
    3506           14 :                                       internal_fn_stored_value_index (fn));
    3507           14 :       def_rhs = vn_valueize (def_rhs);
    3508           14 :       if (TREE_CODE (def_rhs) != VECTOR_CST)
    3509              :         return (void *)-1;
    3510              : 
    3511           14 :       ao_ref_init_from_ptr_and_size (&lhs_ref,
    3512              :                                      vn_valueize (gimple_call_arg (call, 0)),
    3513           14 :                                      TYPE_SIZE_UNIT (TREE_TYPE (def_rhs)));
    3514           14 :       tree base2;
    3515           14 :       poly_int64 offset2, size2, maxsize2;
    3516           14 :       HOST_WIDE_INT offset2i, size2i, offseti;
    3517           14 :       base2 = ao_ref_base (&lhs_ref);
    3518           14 :       offset2 = lhs_ref.offset;
    3519           14 :       size2 = lhs_ref.size;
    3520           14 :       maxsize2 = lhs_ref.max_size;
    3521           14 :       if (known_size_p (maxsize2)
    3522           14 :           && known_eq (maxsize2, size2)
    3523           14 :           && adjust_offsets_for_equal_base_address (base, &offset,
    3524              :                                                     base2, &offset2)
    3525            6 :           && maxsize.is_constant (&maxsizei)
    3526            6 :           && offset.is_constant (&offseti)
    3527            6 :           && offset2.is_constant (&offset2i)
    3528           14 :           && size2.is_constant (&size2i))
    3529              :         {
    3530            6 :           if (!ranges_maybe_overlap_p (offset, maxsize, offset2, size2))
    3531              :             /* Poor-mans disambiguation.  */
    3532              :             return NULL;
    3533            6 :           else if (ranges_known_overlap_p (offset, maxsize, offset2, size2))
    3534              :             {
    3535            6 :               pd_data pd;
    3536            6 :               pd.rhs = def_rhs;
    3537            6 :               tree aa = gimple_call_arg (call, 1);
    3538            6 :               alias_set_type set = get_deref_alias_set (TREE_TYPE (aa));
    3539            6 :               tree vectype = TREE_TYPE (def_rhs);
    3540            6 :               unsigned HOST_WIDE_INT elsz
    3541            6 :                 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype)));
    3542            6 :               if (mask)
    3543              :                 {
    3544              :                   HOST_WIDE_INT start = 0, length = 0;
    3545              :                   unsigned mask_idx = 0;
    3546           48 :                   do
    3547              :                     {
    3548           48 :                       if (integer_zerop (VECTOR_CST_ELT (mask, mask_idx)))
    3549              :                         {
    3550           24 :                           if (length != 0)
    3551              :                             {
    3552           18 :                               pd.rhs_off = start;
    3553           18 :                               pd.offset = offset2i + start;
    3554           18 :                               pd.size = length;
    3555           18 :                               if (ranges_known_overlap_p
    3556           18 :                                     (offset, maxsize, pd.offset, pd.size))
    3557              :                                 {
    3558            0 :                                   void *res = data->push_partial_def
    3559            0 :                                               (pd, set, set, offseti, maxsizei);
    3560            0 :                                   if (res != NULL)
    3561            6 :                                     return res;
    3562              :                                 }
    3563              :                             }
    3564           24 :                           start = (mask_idx + 1) * elsz;
    3565           24 :                           length = 0;
    3566              :                         }
    3567              :                       else
    3568           24 :                         length += elsz;
    3569           48 :                       mask_idx++;
    3570              :                     }
    3571           48 :                   while (known_lt (mask_idx, TYPE_VECTOR_SUBPARTS (vectype)));
    3572            6 :                   if (length != 0)
    3573              :                     {
    3574            6 :                       pd.rhs_off = start;
    3575            6 :                       pd.offset = offset2i + start;
    3576            6 :                       pd.size = length;
    3577            6 :                       if (ranges_known_overlap_p (offset, maxsize,
    3578              :                                                   pd.offset, pd.size))
    3579            2 :                         return data->push_partial_def (pd, set, set,
    3580            2 :                                                        offseti, maxsizei);
    3581              :                     }
    3582              :                 }
    3583            0 :               else if (fn == IFN_LEN_STORE)
    3584              :                 {
    3585            0 :                   pd.offset = offset2i;
    3586            0 :                   pd.size = (tree_to_uhwi (len)
    3587            0 :                              + -tree_to_shwi (bias)) * BITS_PER_UNIT;
    3588            0 :                   if (BYTES_BIG_ENDIAN)
    3589              :                     pd.rhs_off = pd.size - tree_to_uhwi (TYPE_SIZE (vectype));
    3590              :                   else
    3591            0 :                     pd.rhs_off = 0;
    3592            0 :                   if (ranges_known_overlap_p (offset, maxsize,
    3593              :                                               pd.offset, pd.size))
    3594            0 :                     return data->push_partial_def (pd, set, set,
    3595            0 :                                                    offseti, maxsizei);
    3596              :                 }
    3597              :               else
    3598            0 :                 gcc_unreachable ();
    3599            4 :               return NULL;
    3600              :             }
    3601              :         }
    3602              :     }
    3603              : 
    3604              :   /* 5) For aggregate copies translate the reference through them if
    3605              :      the copy kills ref.  */
    3606     18482633 :   else if (data->vn_walk_kind == VN_WALKREWRITE
    3607     14935972 :            && gimple_assign_single_p (def_stmt)
    3608      2527352 :            && !gimple_has_volatile_ops (def_stmt)
    3609     21007687 :            && (DECL_P (gimple_assign_rhs1 (def_stmt))
    3610      1979903 :                || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
    3611      1574824 :                || handled_component_p (gimple_assign_rhs1 (def_stmt))))
    3612              :     {
    3613      2313977 :       tree base2;
    3614      2313977 :       int i, j, k;
    3615      2313977 :       auto_vec<vn_reference_op_s> rhs;
    3616      2313977 :       vn_reference_op_t vro;
    3617      2313977 :       ao_ref r;
    3618              : 
    3619      2313977 :       gcc_assert (lhs_ref_ok);
    3620              : 
    3621              :       /* See if the assignment kills REF.  */
    3622      2313977 :       base2 = ao_ref_base (&lhs_ref);
    3623      2313977 :       if (!lhs_ref.max_size_known_p ()
    3624      2313528 :           || (base != base2
    3625        86476 :               && (TREE_CODE (base) != MEM_REF
    3626        71132 :                   || TREE_CODE (base2) != MEM_REF
    3627        55594 :                   || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
    3628        19729 :                   || !tree_int_cst_equal (TREE_OPERAND (base, 1),
    3629        19729 :                                           TREE_OPERAND (base2, 1))))
    3630      4559239 :           || !stmt_kills_ref_p (def_stmt, ref))
    3631       393771 :         return (void *)-1;
    3632              : 
    3633              :       /* Find the common base of ref and the lhs.  lhs_ops already
    3634              :          contains valueized operands for the lhs.  */
    3635      1920206 :       poly_int64 extra_off = 0;
    3636      1920206 :       i = vr->operands.length () - 1;
    3637      1920206 :       j = lhs_ops.length () - 1;
    3638              : 
    3639              :       /* The base should be always equal due to the above check.  */
    3640      1920206 :       if (! vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
    3641              :         return (void *)-1;
    3642      1919946 :       i--, j--;
    3643              : 
    3644              :       /* The 2nd component should always exist and be a MEM_REF.  */
    3645      1919946 :       if (!(i >= 0 && j >= 0))
    3646              :         ;
    3647      1919946 :       else if (vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
    3648       912391 :         i--, j--;
    3649      1007555 :       else if (vr->operands[i].opcode == MEM_REF
    3650      1005834 :                && lhs_ops[j].opcode == MEM_REF
    3651      1005834 :                && known_ne (lhs_ops[j].off, -1)
    3652      2013389 :                && known_ne (vr->operands[i].off, -1))
    3653              :         {
    3654      1005834 :           bool found = false;
    3655              :           /* When we ge a mismatch at a MEM_REF that is not the sole component
    3656              :              try finding a match in one of the outer components and continue
    3657              :              stripping there.  This happens when addresses of components get
    3658              :              forwarded into dereferences.  */
    3659      1005834 :           if (i > 0)
    3660              :             {
    3661       105817 :               int temi = i - 1;
    3662       105817 :               poly_int64 tem_extra_off = extra_off + vr->operands[i].off;
    3663       105817 :               while (temi >= 0
    3664       227733 :                      && known_ne (vr->operands[temi].off, -1))
    3665              :                 {
    3666       123407 :                   if (vr->operands[temi].type
    3667       123407 :                       && lhs_ops[j].type
    3668       246814 :                       && (TYPE_MAIN_VARIANT (vr->operands[temi].type)
    3669       123407 :                           == TYPE_MAIN_VARIANT (lhs_ops[j].type)))
    3670              :                     {
    3671         1491 :                       i = temi;
    3672              :                       /* Strip the component that was type matched to
    3673              :                          the MEM_REF.  */
    3674         1491 :                       extra_off = (tem_extra_off
    3675         1491 :                                    + vr->operands[i].off - lhs_ops[j].off);
    3676         1491 :                       i--, j--;
    3677              :                       /* Strip further equal components.  */
    3678         1491 :                       found = true;
    3679         1491 :                       break;
    3680              :                     }
    3681       121916 :                   tem_extra_off += vr->operands[temi].off;
    3682       121916 :                   temi--;
    3683              :                 }
    3684              :             }
    3685      1005834 :           if (!found && j > 0)
    3686              :             {
    3687        29226 :               int temj = j - 1;
    3688        29226 :               poly_int64 tem_extra_off = extra_off - lhs_ops[j].off;
    3689        29226 :               while (temj >= 0
    3690        56568 :                      && known_ne (lhs_ops[temj].off, -1))
    3691              :                 {
    3692        31385 :                   if (vr->operands[i].type
    3693        31385 :                       && lhs_ops[temj].type
    3694        62770 :                       && (TYPE_MAIN_VARIANT (vr->operands[i].type)
    3695        31385 :                           == TYPE_MAIN_VARIANT (lhs_ops[temj].type)))
    3696              :                     {
    3697         4043 :                       j = temj;
    3698              :                       /* Strip the component that was type matched to
    3699              :                          the MEM_REF.  */
    3700         4043 :                       extra_off = (tem_extra_off
    3701         4043 :                                    + vr->operands[i].off - lhs_ops[j].off);
    3702         4043 :                       i--, j--;
    3703              :                       /* Strip further equal components.  */
    3704         4043 :                       found = true;
    3705         4043 :                       break;
    3706              :                     }
    3707        27342 :                   tem_extra_off += -lhs_ops[temj].off;
    3708        27342 :                   temj--;
    3709              :                 }
    3710              :             }
    3711              :           /* When we cannot find a common base to reconstruct the full
    3712              :              reference instead try to reduce the lookup to the new
    3713              :              base plus a constant offset.  */
    3714      1005834 :           if (!found)
    3715              :             {
    3716              :               while (j >= 0
    3717      2027568 :                      && known_ne (lhs_ops[j].off, -1))
    3718              :                 {
    3719      1027268 :                   extra_off += -lhs_ops[j].off;
    3720      1027268 :                   j--;
    3721              :                 }
    3722      1000300 :               if (j != -1)
    3723              :                 return (void *)-1;
    3724              :               while (i >= 0
    3725      2117037 :                      && known_ne (vr->operands[i].off, -1))
    3726              :                 {
    3727              :                   /* Punt if the additional ops contain a storage order
    3728              :                      barrier.  */
    3729      1116737 :                   if (vr->operands[i].opcode == VIEW_CONVERT_EXPR
    3730      1116737 :                       && vr->operands[i].reverse)
    3731              :                     break;
    3732      1116737 :                   extra_off += vr->operands[i].off;
    3733      1116737 :                   i--;
    3734              :                 }
    3735      1000300 :               if (i != -1)
    3736              :                 return (void *)-1;
    3737              :               found = true;
    3738              :             }
    3739              :           /* If we did find a match we'd eventually append a MEM_REF
    3740              :              as component.  Don't.  */
    3741              :           if (!found)
    3742              :             return (void *)-1;
    3743              :         }
    3744              :       else
    3745              :         return (void *)-1;
    3746              : 
    3747              :       /* Strip further common components, attempting to consume lhs_ops
    3748              :          in full.  */
    3749      1917052 :       while (j >= 0 && i >= 0
    3750      1917052 :              && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
    3751              :         {
    3752        25264 :           i--;
    3753        25264 :           j--;
    3754              :         }
    3755              : 
    3756              :       /* i now points to the first additional op.
    3757              :          ???  LHS may not be completely contained in VR, one or more
    3758              :          VIEW_CONVERT_EXPRs could be in its way.  We could at least
    3759              :          try handling outermost VIEW_CONVERT_EXPRs.  */
    3760      1891788 :       if (j != -1)
    3761              :         return (void *)-1;
    3762              : 
    3763              :       /* Punt if the additional ops contain a storage order barrier.  */
    3764      2957391 :       for (k = i; k >= 0; k--)
    3765              :         {
    3766      1068280 :           vro = &vr->operands[k];
    3767      1068280 :           if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
    3768              :             return (void *)-1;
    3769              :         }
    3770              : 
    3771              :       /* Now re-write REF to be based on the rhs of the assignment.  */
    3772      1889111 :       tree rhs1 = gimple_assign_rhs1 (def_stmt);
    3773      1889111 :       copy_reference_ops_from_ref (rhs1, &rhs);
    3774              : 
    3775              :       /* Apply an extra offset to the inner MEM_REF of the RHS.  */
    3776      1889111 :       bool force_no_tbaa = false;
    3777      1889111 :       if (maybe_ne (extra_off, 0))
    3778              :         {
    3779       723100 :           if (rhs.length () < 2)
    3780              :             return (void *)-1;
    3781       723100 :           int ix = rhs.length () - 2;
    3782       723100 :           if (rhs[ix].opcode != MEM_REF
    3783       723100 :               || known_eq (rhs[ix].off, -1))
    3784              :             return (void *)-1;
    3785       723082 :           rhs[ix].off += extra_off;
    3786       723082 :           rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
    3787       723082 :                                          build_int_cst (TREE_TYPE (rhs[ix].op0),
    3788              :                                                         extra_off));
    3789              :           /* When we have offsetted the RHS, reading only parts of it,
    3790              :              we can no longer use the original TBAA type, force alias-set
    3791              :              zero.  */
    3792       723082 :           force_no_tbaa = true;
    3793              :         }
    3794              : 
    3795              :       /* Save the operands since we need to use the original ones for
    3796              :          the hash entry we use.  */
    3797      1889093 :       if (!data->saved_operands.exists ())
    3798      1787169 :         data->saved_operands = vr->operands.copy ();
    3799              : 
    3800              :       /* We need to pre-pend vr->operands[0..i] to rhs.  */
    3801      1889093 :       vec<vn_reference_op_s> old = vr->operands;
    3802      5667279 :       if (i + 1 + rhs.length () > vr->operands.length ())
    3803      1150972 :         vr->operands.safe_grow (i + 1 + rhs.length (), true);
    3804              :       else
    3805       738121 :         vr->operands.truncate (i + 1 + rhs.length ());
    3806      6865310 :       FOR_EACH_VEC_ELT (rhs, j, vro)
    3807      4976217 :         vr->operands[i + 1 + j] = *vro;
    3808      1889093 :       valueize_refs (&vr->operands);
    3809      3778186 :       if (old == shared_lookup_references)
    3810      1889093 :         shared_lookup_references = vr->operands;
    3811      1889093 :       vr->hashcode = vn_reference_compute_hash (vr);
    3812              : 
    3813              :       /* Try folding the new reference to a constant.  */
    3814      1889093 :       tree val = fully_constant_vn_reference_p (vr);
    3815      1889093 :       if (val)
    3816              :         {
    3817        21926 :           if (data->partial_defs.is_empty ())
    3818        21917 :             return data->finish (ao_ref_alias_set (&lhs_ref),
    3819        21917 :                                  ao_ref_base_alias_set (&lhs_ref), val);
    3820              :           /* This is the only interesting case for partial-def handling
    3821              :              coming from targets that like to gimplify init-ctors as
    3822              :              aggregate copies from constant data like aarch64 for
    3823              :              PR83518.  */
    3824            9 :           if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
    3825              :             {
    3826            9 :               pd_data pd;
    3827            9 :               pd.rhs = val;
    3828            9 :               pd.rhs_off = 0;
    3829            9 :               pd.offset = 0;
    3830            9 :               pd.size = maxsizei;
    3831            9 :               return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
    3832              :                                              ao_ref_base_alias_set (&lhs_ref),
    3833              :                                              0, maxsizei);
    3834              :             }
    3835              :         }
    3836              : 
    3837              :       /* Continuing with partial defs isn't easily possible here, we
    3838              :          have to find a full def from further lookups from here.  Probably
    3839              :          not worth the special-casing everywhere.  */
    3840      2298232 :       if (!data->partial_defs.is_empty ())
    3841              :         return (void *)-1;
    3842              : 
    3843              :       /* Adjust *ref from the new operands.  */
    3844      1860986 :       ao_ref rhs1_ref;
    3845      1860986 :       ao_ref_init (&rhs1_ref, rhs1);
    3846      3013362 :       if (!ao_ref_init_from_vn_reference (&r,
    3847              :                                           force_no_tbaa ? 0
    3848      1152376 :                                           : ao_ref_alias_set (&rhs1_ref),
    3849              :                                           force_no_tbaa ? 0
    3850      1152376 :                                           : ao_ref_base_alias_set (&rhs1_ref),
    3851              :                                           vr->type, vr->operands))
    3852              :         return (void *)-1;
    3853              :       /* This can happen with bitfields.  */
    3854      1860986 :       if (maybe_ne (ref->size, r.size))
    3855              :         {
    3856              :           /* If the access lacks some subsetting simply apply that by
    3857              :              shortening it.  That in the end can only be successful
    3858              :              if we can pun the lookup result which in turn requires
    3859              :              exact offsets.  */
    3860         1553 :           if (known_eq (r.size, r.max_size)
    3861         1553 :               && known_lt (ref->size, r.size))
    3862         1553 :             r.size = r.max_size = ref->size;
    3863              :           else
    3864              :             return (void *)-1;
    3865              :         }
    3866      1860986 :       *ref = r;
    3867      1860986 :       vr->offset = r.offset;
    3868      1860986 :       vr->max_size = r.max_size;
    3869              : 
    3870              :       /* Do not update last seen VUSE after translating.  */
    3871      1860986 :       data->last_vuse_ptr = NULL;
    3872              :       /* Invalidate the original access path since it now contains
    3873              :          the wrong base.  */
    3874      1860986 :       data->orig_ref.ref = NULL_TREE;
    3875              :       /* Use the alias-set of this LHS for recording an eventual result.  */
    3876      1860986 :       if (data->first_set == -2)
    3877              :         {
    3878      1760582 :           data->first_set = ao_ref_alias_set (&lhs_ref);
    3879      1760582 :           data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
    3880              :         }
    3881              : 
    3882              :       /* Keep looking for the adjusted *REF / VR pair.  */
    3883      1860986 :       return NULL;
    3884      2313977 :     }
    3885              : 
    3886              :   /* 6) For memcpy copies translate the reference through them if the copy
    3887              :      kills ref.  But we cannot (easily) do this translation if the memcpy is
    3888              :      a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
    3889              :      can modify the storage order of objects (see storage_order_barrier_p).  */
    3890     16168656 :   else if (data->vn_walk_kind == VN_WALKREWRITE
    3891     12621995 :            && is_gimple_reg_type (vr->type)
    3892              :            /* ???  Handle BCOPY as well.  */
    3893     12616189 :            && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
    3894     12549815 :                || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
    3895     12549392 :                || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
    3896     12548216 :                || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
    3897     12547974 :                || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
    3898     12522851 :                || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
    3899        93666 :            && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
    3900        82434 :                || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
    3901        93630 :            && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
    3902        65941 :                || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
    3903        93615 :            && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), &copy_size)
    3904        52867 :                || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
    3905        52867 :                    && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
    3906              :                                        &copy_size)))
    3907              :            /* Handling this is more complicated, give up for now.  */
    3908     16211923 :            && data->partial_defs.is_empty ())
    3909              :     {
    3910        42656 :       tree lhs, rhs;
    3911        42656 :       ao_ref r;
    3912        42656 :       poly_int64 rhs_offset, lhs_offset;
    3913        42656 :       vn_reference_op_s op;
    3914        42656 :       poly_uint64 mem_offset;
    3915        42656 :       poly_int64 at, byte_maxsize;
    3916              : 
    3917              :       /* Only handle non-variable, addressable refs.  */
    3918        42656 :       if (maybe_ne (ref->size, maxsize)
    3919        41976 :           || !multiple_p (offset, BITS_PER_UNIT, &at)
    3920        42656 :           || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
    3921          680 :         return (void *)-1;
    3922              : 
    3923              :       /* Extract a pointer base and an offset for the destination.  */
    3924        41976 :       lhs = gimple_call_arg (def_stmt, 0);
    3925        41976 :       lhs_offset = 0;
    3926        41976 :       if (TREE_CODE (lhs) == SSA_NAME)
    3927              :         {
    3928        32600 :           lhs = vn_valueize (lhs);
    3929        32600 :           if (TREE_CODE (lhs) == SSA_NAME)
    3930              :             {
    3931        32280 :               gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
    3932        32280 :               if (gimple_assign_single_p (def_stmt)
    3933        32280 :                   && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
    3934         2381 :                 lhs = gimple_assign_rhs1 (def_stmt);
    3935              :             }
    3936              :         }
    3937        41976 :       if (TREE_CODE (lhs) == ADDR_EXPR)
    3938              :         {
    3939        17050 :           if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
    3940        16753 :               && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
    3941              :             return (void *)-1;
    3942        11937 :           tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
    3943              :                                                     &lhs_offset);
    3944        11937 :           if (!tem)
    3945              :             return (void *)-1;
    3946        11237 :           if (TREE_CODE (tem) == MEM_REF
    3947        11237 :               && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
    3948              :             {
    3949         1681 :               lhs = TREE_OPERAND (tem, 0);
    3950         1681 :               if (TREE_CODE (lhs) == SSA_NAME)
    3951         1681 :                 lhs = vn_valueize (lhs);
    3952         1681 :               lhs_offset += mem_offset;
    3953              :             }
    3954         9556 :           else if (DECL_P (tem))
    3955         9556 :             lhs = build_fold_addr_expr (tem);
    3956              :           else
    3957              :             return (void *)-1;
    3958              :         }
    3959        41136 :       if (TREE_CODE (lhs) != SSA_NAME
    3960         9557 :           && TREE_CODE (lhs) != ADDR_EXPR)
    3961              :         return (void *)-1;
    3962              : 
    3963              :       /* Extract a pointer base and an offset for the source.  */
    3964        41136 :       rhs = gimple_call_arg (def_stmt, 1);
    3965        41136 :       rhs_offset = 0;
    3966        41136 :       if (TREE_CODE (rhs) == SSA_NAME)
    3967        19145 :         rhs = vn_valueize (rhs);
    3968        41136 :       if (TREE_CODE (rhs) == ADDR_EXPR)
    3969              :         {
    3970        34641 :           if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
    3971        24168 :               && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
    3972              :             return (void *)-1;
    3973        23572 :           tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
    3974              :                                                     &rhs_offset);
    3975        23572 :           if (!tem)
    3976              :             return (void *)-1;
    3977        23572 :           if (TREE_CODE (tem) == MEM_REF
    3978        23572 :               && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
    3979              :             {
    3980            0 :               rhs = TREE_OPERAND (tem, 0);
    3981            0 :               rhs_offset += mem_offset;
    3982              :             }
    3983        23572 :           else if (DECL_P (tem)
    3984        17336 :                    || TREE_CODE (tem) == STRING_CST)
    3985        23572 :             rhs = build_fold_addr_expr (tem);
    3986              :           else
    3987              :             return (void *)-1;
    3988              :         }
    3989        41136 :       if (TREE_CODE (rhs) == SSA_NAME)
    3990        17564 :         rhs = SSA_VAL (rhs);
    3991        23572 :       else if (TREE_CODE (rhs) != ADDR_EXPR)
    3992              :         return (void *)-1;
    3993              : 
    3994              :       /* The bases of the destination and the references have to agree.  */
    3995        41136 :       if (TREE_CODE (base) == MEM_REF)
    3996              :         {
    3997        15714 :           if (TREE_OPERAND (base, 0) != lhs
    3998        15714 :               || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
    3999        11958 :             return (void *) -1;
    4000        12322 :           at += mem_offset;
    4001              :         }
    4002        25422 :       else if (!DECL_P (base)
    4003        24681 :                || TREE_CODE (lhs) != ADDR_EXPR
    4004        33989 :                || TREE_OPERAND (lhs, 0) != base)
    4005              :         return (void *)-1;
    4006              : 
    4007              :       /* If the access is completely outside of the memcpy destination
    4008              :          area there is no aliasing.  */
    4009        12322 :       if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
    4010              :         return NULL;
    4011              :       /* And the access has to be contained within the memcpy destination.  */
    4012        12289 :       if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
    4013              :         return (void *)-1;
    4014              : 
    4015              :       /* Save the operands since we need to use the original ones for
    4016              :          the hash entry we use.  */
    4017        11766 :       if (!data->saved_operands.exists ())
    4018        11267 :         data->saved_operands = vr->operands.copy ();
    4019              : 
    4020              :       /* Make room for 2 operands in the new reference.  */
    4021        11766 :       if (vr->operands.length () < 2)
    4022              :         {
    4023            0 :           vec<vn_reference_op_s> old = vr->operands;
    4024            0 :           vr->operands.safe_grow_cleared (2, true);
    4025            0 :           if (old == shared_lookup_references)
    4026            0 :             shared_lookup_references = vr->operands;
    4027              :         }
    4028              :       else
    4029        11766 :         vr->operands.truncate (2);
    4030              : 
    4031              :       /* The looked-through reference is a simple MEM_REF.  */
    4032        11766 :       memset (&op, 0, sizeof (op));
    4033        11766 :       op.type = vr->type;
    4034        11766 :       op.opcode = MEM_REF;
    4035        11766 :       op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
    4036        11766 :       op.off = at - lhs_offset + rhs_offset;
    4037        11766 :       vr->operands[0] = op;
    4038        11766 :       op.type = TREE_TYPE (rhs);
    4039        11766 :       op.opcode = TREE_CODE (rhs);
    4040        11766 :       op.op0 = rhs;
    4041        11766 :       op.off = -1;
    4042        11766 :       vr->operands[1] = op;
    4043        11766 :       vr->hashcode = vn_reference_compute_hash (vr);
    4044              : 
    4045              :       /* Try folding the new reference to a constant.  */
    4046        11766 :       tree val = fully_constant_vn_reference_p (vr);
    4047        11766 :       if (val)
    4048         2871 :         return data->finish (0, 0, val);
    4049              : 
    4050              :       /* Adjust *ref from the new operands.  */
    4051         8895 :       if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
    4052              :         return (void *)-1;
    4053              :       /* This can happen with bitfields.  */
    4054         8895 :       if (maybe_ne (ref->size, r.size))
    4055              :         return (void *)-1;
    4056         8895 :       *ref = r;
    4057         8895 :       vr->offset = r.offset;
    4058         8895 :       vr->max_size = r.max_size;
    4059              : 
    4060              :       /* Do not update last seen VUSE after translating.  */
    4061         8895 :       data->last_vuse_ptr = NULL;
    4062              :       /* Invalidate the original access path since it now contains
    4063              :          the wrong base.  */
    4064         8895 :       data->orig_ref.ref = NULL_TREE;
    4065              :       /* Use the alias-set of this stmt for recording an eventual result.  */
    4066         8895 :       if (data->first_set == -2)
    4067              :         {
    4068         8441 :           data->first_set = 0;
    4069         8441 :           data->first_base_set = 0;
    4070              :         }
    4071              : 
    4072              :       /* Keep looking for the adjusted *REF / VR pair.  */
    4073         8895 :       return NULL;
    4074              :     }
    4075              : 
    4076              :   /* Bail out and stop walking.  */
    4077              :   return (void *)-1;
    4078              : }
    4079              : 
    4080              : /* Return true if E is a backedge with respect to our CFG walk order.  */
    4081              : 
    4082              : static bool
    4083    119668901 : vn_is_backedge (edge e, void *)
    4084              : {
    4085              :   /* During PRE elimination we no longer have access to this info.  */
    4086    119668901 :   return (!vn_bb_to_rpo
    4087    119668901 :           || vn_bb_to_rpo[e->dest->index] <= vn_bb_to_rpo[e->src->index]);
    4088              : }
    4089              : 
    4090              : /* Return a reference op vector from OP that can be used for
    4091              :    vn_reference_lookup_pieces.  The caller is responsible for releasing
    4092              :    the vector.  */
    4093              : 
    4094              : vec<vn_reference_op_s>
    4095      4640189 : vn_reference_operands_for_lookup (tree op)
    4096              : {
    4097      4640189 :   bool valueized;
    4098      4640189 :   return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
    4099              : }
    4100              : 
    4101              : /* Lookup a reference operation by it's parts, in the current hash table.
    4102              :    Returns the resulting value number if it exists in the hash table,
    4103              :    NULL_TREE otherwise.  VNRESULT will be filled in with the actual
    4104              :    vn_reference_t stored in the hashtable if something is found.  */
    4105              : 
    4106              : tree
    4107      7667917 : vn_reference_lookup_pieces (tree vuse, alias_set_type set,
    4108              :                             alias_set_type base_set, tree type,
    4109              :                             vec<vn_reference_op_s> operands,
    4110              :                             vn_reference_t *vnresult, vn_lookup_kind kind)
    4111              : {
    4112      7667917 :   struct vn_reference_s vr1;
    4113      7667917 :   vn_reference_t tmp;
    4114      7667917 :   tree cst;
    4115              : 
    4116      7667917 :   if (!vnresult)
    4117            0 :     vnresult = &tmp;
    4118      7667917 :   *vnresult = NULL;
    4119              : 
    4120      7667917 :   vr1.vuse = vuse_ssa_val (vuse);
    4121      7667917 :   shared_lookup_references.truncate (0);
    4122     15335834 :   shared_lookup_references.safe_grow (operands.length (), true);
    4123      7667917 :   memcpy (shared_lookup_references.address (),
    4124      7667917 :           operands.address (),
    4125              :           sizeof (vn_reference_op_s)
    4126      7667917 :           * operands.length ());
    4127      7667917 :   bool valueized_p;
    4128      7667917 :   valueize_refs_1 (&shared_lookup_references, &valueized_p);
    4129      7667917 :   vr1.operands = shared_lookup_references;
    4130      7667917 :   vr1.type = type;
    4131      7667917 :   vr1.set = set;
    4132      7667917 :   vr1.base_set = base_set;
    4133              :   /* We can pretend there's no extra info fed in since the ao_refs offset
    4134              :      and max_size are computed only from the VN reference ops.  */
    4135      7667917 :   vr1.offset = 0;
    4136      7667917 :   vr1.max_size = -1;
    4137      7667917 :   vr1.hashcode = vn_reference_compute_hash (&vr1);
    4138      7667917 :   if ((cst = fully_constant_vn_reference_p (&vr1)))
    4139              :     return cst;
    4140              : 
    4141      7648917 :   vn_reference_lookup_1 (&vr1, vnresult);
    4142      7648917 :   if (!*vnresult
    4143      2983762 :       && kind != VN_NOWALK
    4144      2983762 :       && vr1.vuse)
    4145              :     {
    4146      2955536 :       ao_ref r;
    4147      2955536 :       unsigned limit = param_sccvn_max_alias_queries_per_access;
    4148      2955536 :       vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
    4149      2955536 :                             false);
    4150      2955536 :       vec<vn_reference_op_s> ops_for_ref;
    4151      2955536 :       if (!valueized_p)
    4152      2866069 :         ops_for_ref = vr1.operands;
    4153              :       else
    4154              :         {
    4155              :           /* For ao_ref_from_mem we have to ensure only available SSA names
    4156              :              end up in base and the only convenient way to make this work
    4157              :              for PRE is to re-valueize with that in mind.  */
    4158       178934 :           ops_for_ref.create (operands.length ());
    4159       178934 :           ops_for_ref.quick_grow (operands.length ());
    4160        89467 :           memcpy (ops_for_ref.address (),
    4161        89467 :                   operands.address (),
    4162              :                   sizeof (vn_reference_op_s)
    4163        89467 :                   * operands.length ());
    4164        89467 :           valueize_refs_1 (&ops_for_ref, &valueized_p, true);
    4165              :         }
    4166      2955536 :       if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
    4167              :                                          ops_for_ref))
    4168      2888540 :         *vnresult
    4169      2888540 :           = ((vn_reference_t)
    4170      2888540 :              walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
    4171              :                                      vn_reference_lookup_3, vn_is_backedge,
    4172              :                                      vuse_valueize, limit, &data));
    4173      5911072 :       if (ops_for_ref != shared_lookup_references)
    4174        89467 :         ops_for_ref.release ();
    4175      5911072 :       gcc_checking_assert (vr1.operands == shared_lookup_references);
    4176      2955536 :       if (*vnresult
    4177       417148 :           && data.same_val
    4178      2955536 :           && (!(*vnresult)->result
    4179            0 :               || !operand_equal_p ((*vnresult)->result, data.same_val)))
    4180              :         {
    4181            0 :           *vnresult = NULL;
    4182            0 :           return NULL_TREE;
    4183              :         }
    4184      2955536 :     }
    4185              : 
    4186      7648917 :   if (*vnresult)
    4187      5082303 :      return (*vnresult)->result;
    4188              : 
    4189              :   return NULL_TREE;
    4190              : }
    4191              : 
    4192              : /* When OPERANDS is an ADDR_EXPR that can be possibly expressed as a
    4193              :    POINTER_PLUS_EXPR return true and fill in its operands in OPS.  */
    4194              : 
    4195              : bool
    4196      2145680 : vn_pp_nary_for_addr (const vec<vn_reference_op_s>& operands, tree ops[2])
    4197              : {
    4198      4291360 :   gcc_assert (operands[0].opcode == ADDR_EXPR
    4199              :               && operands.last ().opcode == SSA_NAME);
    4200              :   poly_int64 off = 0;
    4201              :   vn_reference_op_t vro;
    4202              :   unsigned i;
    4203      6917930 :   for (i = 1; operands.iterate (i, &vro); ++i)
    4204              :     {
    4205      6917930 :       if (vro->opcode == SSA_NAME)
    4206              :         break;
    4207      4822133 :       else if (known_eq (vro->off, -1))
    4208              :         break;
    4209      4772250 :       off += vro->off;
    4210              :     }
    4211      2145680 :   if (i == operands.length () - 1
    4212      2095797 :       && maybe_ne (off, 0)
    4213              :       /* Make sure we the offset we accumulated in a 64bit int
    4214              :          fits the address computation carried out in target
    4215              :          offset precision.  */
    4216      3519999 :       && (off.coeffs[0]
    4217      1374319 :           == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
    4218              :     {
    4219      1373536 :       gcc_assert (operands[i-1].opcode == MEM_REF);
    4220      1373536 :       ops[0] = operands[i].op0;
    4221      1373536 :       ops[1] = wide_int_to_tree (sizetype, off);
    4222      1373536 :       return true;
    4223              :     }
    4224              :   return false;
    4225              : }
    4226              : 
    4227              : /* Lookup OP in the current hash table, and return the resulting value
    4228              :    number if it exists in the hash table.  Return NULL_TREE if it does
    4229              :    not exist in the hash table or if the result field of the structure
    4230              :    was NULL..  VNRESULT will be filled in with the vn_reference_t
    4231              :    stored in the hashtable if one exists.  When TBAA_P is false assume
    4232              :    we are looking up a store and treat it as having alias-set zero.
    4233              :    *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
    4234              :    MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
    4235              :    load is bitwise anded with MASK and so we are only interested in a subset
    4236              :    of the bits and can ignore if the other bits are uninitialized or
    4237              :    not initialized with constants.  When doing redundant store removal
    4238              :    the caller has to set REDUNDANT_STORE_REMOVAL_P.  */
    4239              : 
    4240              : tree
    4241     99503706 : vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
    4242              :                      vn_reference_t *vnresult, bool tbaa_p,
    4243              :                      tree *last_vuse_ptr, tree mask,
    4244              :                      bool redundant_store_removal_p)
    4245              : {
    4246     99503706 :   vec<vn_reference_op_s> operands;
    4247     99503706 :   struct vn_reference_s vr1;
    4248     99503706 :   bool valueized_anything;
    4249              : 
    4250     99503706 :   if (vnresult)
    4251     99119484 :     *vnresult = NULL;
    4252              : 
    4253     99503706 :   vr1.vuse = vuse_ssa_val (vuse);
    4254    199007412 :   vr1.operands = operands
    4255     99503706 :     = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
    4256              : 
    4257              :   /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR.  Avoid doing
    4258              :      this before the pass folding __builtin_object_size had a chance to run.  */
    4259     99503706 :   if ((cfun->curr_properties & PROP_objsz)
    4260     72154660 :       && operands[0].opcode == ADDR_EXPR
    4261    100583467 :       && operands.last ().opcode == SSA_NAME)
    4262              :     {
    4263      1045501 :       tree ops[2];
    4264      1045501 :       if (vn_pp_nary_for_addr (operands, ops))
    4265              :         {
    4266       670264 :           tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
    4267       670264 :                                                TREE_TYPE (op), ops, NULL);
    4268       670264 :           if (res)
    4269       670264 :             return res;
    4270       670264 :           return NULL_TREE;
    4271              :         }
    4272              :     }
    4273              : 
    4274     98833442 :   vr1.type = TREE_TYPE (op);
    4275     98833442 :   ao_ref op_ref;
    4276     98833442 :   ao_ref_init (&op_ref, op);
    4277     98833442 :   vr1.set = ao_ref_alias_set (&op_ref);
    4278     98833442 :   vr1.base_set = ao_ref_base_alias_set (&op_ref);
    4279     98833442 :   vr1.offset = 0;
    4280     98833442 :   vr1.max_size = -1;
    4281     98833442 :   vr1.hashcode = vn_reference_compute_hash (&vr1);
    4282     98833442 :   if (mask == NULL_TREE)
    4283     98531854 :     if (tree cst = fully_constant_vn_reference_p (&vr1))
    4284              :       return cst;
    4285              : 
    4286     98818058 :   if (kind != VN_NOWALK && vr1.vuse)
    4287              :     {
    4288     57340124 :       vn_reference_t wvnresult;
    4289     57340124 :       ao_ref r;
    4290     57340124 :       unsigned limit = param_sccvn_max_alias_queries_per_access;
    4291     57340124 :       auto_vec<vn_reference_op_s> ops_for_ref;
    4292     57340124 :       if (valueized_anything)
    4293              :         {
    4294      4497684 :           copy_reference_ops_from_ref (op, &ops_for_ref);
    4295      4497684 :           bool tem;
    4296      4497684 :           valueize_refs_1 (&ops_for_ref, &tem, true);
    4297              :         }
    4298              :       /* Make sure to use a valueized reference if we valueized anything.
    4299              :          Otherwise preserve the full reference for advanced TBAA.  */
    4300     57340124 :       if (!valueized_anything
    4301     57340124 :           || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
    4302              :                                              vr1.type, ops_for_ref))
    4303              :         {
    4304     52842440 :           ao_ref_init (&r, op);
    4305              :           /* Record the extra info we're getting from the full ref.  */
    4306     52842440 :           ao_ref_base (&r);
    4307     52842440 :           vr1.offset = r.offset;
    4308     52842440 :           vr1.max_size = r.max_size;
    4309              :         }
    4310     57340124 :       vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
    4311              :                             last_vuse_ptr, kind, tbaa_p, mask,
    4312    110182564 :                             redundant_store_removal_p);
    4313              : 
    4314     57340124 :       wvnresult
    4315              :         = ((vn_reference_t)
    4316     57340124 :            walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
    4317              :                                    vn_reference_lookup_3, vn_is_backedge,
    4318              :                                    vuse_valueize, limit, &data));
    4319    114680248 :       gcc_checking_assert (vr1.operands == shared_lookup_references);
    4320     57340124 :       if (wvnresult)
    4321              :         {
    4322      8419673 :           gcc_assert (mask == NULL_TREE);
    4323      8419673 :           if (data.same_val
    4324      8419673 :               && (!wvnresult->result
    4325        65023 :                   || !operand_equal_p (wvnresult->result, data.same_val)))
    4326        45315 :             return NULL_TREE;
    4327      8374358 :           if (vnresult)
    4328      8373182 :             *vnresult = wvnresult;
    4329      8374358 :           return wvnresult->result;
    4330              :         }
    4331     48920451 :       else if (mask)
    4332       301588 :         return data.masked_result;
    4333              : 
    4334              :       return NULL_TREE;
    4335     57340124 :     }
    4336              : 
    4337     41477934 :   if (last_vuse_ptr)
    4338      1408309 :     *last_vuse_ptr = vr1.vuse;
    4339     41477934 :   if (mask)
    4340              :     return NULL_TREE;
    4341     41477934 :   return vn_reference_lookup_1 (&vr1, vnresult);
    4342              : }
    4343              : 
    4344              : /* Lookup CALL in the current hash table and return the entry in
    4345              :    *VNRESULT if found.  Populates *VR for the hashtable lookup.  */
    4346              : 
    4347              : void
    4348      9065646 : vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
    4349              :                           vn_reference_t vr)
    4350              : {
    4351      9065646 :   if (vnresult)
    4352      9065646 :     *vnresult = NULL;
    4353              : 
    4354      9065646 :   tree vuse = gimple_vuse (call);
    4355              : 
    4356      9065646 :   vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
    4357      9065646 :   vr->operands = valueize_shared_reference_ops_from_call (call);
    4358      9065646 :   tree lhs = gimple_call_lhs (call);
    4359              :   /* For non-SSA return values the reference ops contain the LHS.  */
    4360      4974791 :   vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
    4361     13605138 :               ? TREE_TYPE (lhs) : NULL_TREE);
    4362      9065646 :   vr->punned = false;
    4363      9065646 :   vr->set = 0;
    4364      9065646 :   vr->base_set = 0;
    4365      9065646 :   vr->offset = 0;
    4366      9065646 :   vr->max_size = -1;
    4367      9065646 :   vr->hashcode = vn_reference_compute_hash (vr);
    4368      9065646 :   vn_reference_lookup_1 (vr, vnresult);
    4369      9065646 : }
    4370              : 
    4371              : /* Insert OP into the current hash table with a value number of RESULT.  */
    4372              : 
    4373              : static void
    4374     73735739 : vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
    4375              : {
    4376     73735739 :   vn_reference_s **slot;
    4377     73735739 :   vn_reference_t vr1;
    4378     73735739 :   bool tem;
    4379              : 
    4380     73735739 :   vec<vn_reference_op_s> operands
    4381     73735739 :     = valueize_shared_reference_ops_from_ref (op, &tem);
    4382              :   /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR.  Avoid doing this
    4383              :      before the pass folding __builtin_object_size had a chance to run.  */
    4384     73735739 :   if ((cfun->curr_properties & PROP_objsz)
    4385     55302637 :       && operands[0].opcode == ADDR_EXPR
    4386     74617161 :       && operands.last ().opcode == SSA_NAME)
    4387              :     {
    4388       849650 :       tree ops[2];
    4389       849650 :       if (vn_pp_nary_for_addr (operands, ops))
    4390              :         {
    4391       537524 :           vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR,
    4392       537524 :                                     TREE_TYPE (op), ops, result,
    4393       537524 :                                     VN_INFO (result)->value_id);
    4394       537524 :           return;
    4395              :         }
    4396              :     }
    4397              : 
    4398     73198215 :   vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
    4399     73198215 :   if (TREE_CODE (result) == SSA_NAME)
    4400     50523232 :     vr1->value_id = VN_INFO (result)->value_id;
    4401              :   else
    4402     22674983 :     vr1->value_id = get_or_alloc_constant_value_id (result);
    4403     73198215 :   vr1->vuse = vuse_ssa_val (vuse);
    4404     73198215 :   vr1->operands = operands.copy ();
    4405     73198215 :   vr1->type = TREE_TYPE (op);
    4406     73198215 :   vr1->punned = false;
    4407     73198215 :   ao_ref op_ref;
    4408     73198215 :   ao_ref_init (&op_ref, op);
    4409     73198215 :   vr1->set = ao_ref_alias_set (&op_ref);
    4410     73198215 :   vr1->base_set = ao_ref_base_alias_set (&op_ref);
    4411              :   /* Specifically use an unknown extent here, we're not doing any lookup
    4412              :      and assume the caller didn't either (or it went VARYING).  */
    4413     73198215 :   vr1->offset = 0;
    4414     73198215 :   vr1->max_size = -1;
    4415     73198215 :   vr1->hashcode = vn_reference_compute_hash (vr1);
    4416     73198215 :   vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
    4417     73198215 :   vr1->result_vdef = vdef;
    4418              : 
    4419     73198215 :   slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
    4420              :                                                       INSERT);
    4421              : 
    4422              :   /* Because IL walking on reference lookup can end up visiting
    4423              :      a def that is only to be visited later in iteration order
    4424              :      when we are about to make an irreducible region reducible
    4425              :      the def can be effectively processed and its ref being inserted
    4426              :      by vn_reference_lookup_3 already.  So we cannot assert (!*slot)
    4427              :      but save a lookup if we deal with already inserted refs here.  */
    4428     73198215 :   if (*slot)
    4429              :     {
    4430              :       /* We cannot assert that we have the same value either because
    4431              :          when disentangling an irreducible region we may end up visiting
    4432              :          a use before the corresponding def.  That's a missed optimization
    4433              :          only though.  See gcc.dg/tree-ssa/pr87126.c for example.  */
    4434            0 :       if (dump_file && (dump_flags & TDF_DETAILS)
    4435            0 :           && !operand_equal_p ((*slot)->result, vr1->result, 0))
    4436              :         {
    4437            0 :           fprintf (dump_file, "Keeping old value ");
    4438            0 :           print_generic_expr (dump_file, (*slot)->result);
    4439            0 :           fprintf (dump_file, " because of collision\n");
    4440              :         }
    4441            0 :       free_reference (vr1);
    4442            0 :       obstack_free (&vn_tables_obstack, vr1);
    4443            0 :       return;
    4444              :     }
    4445              : 
    4446     73198215 :   *slot = vr1;
    4447     73198215 :   vr1->next = last_inserted_ref;
    4448     73198215 :   last_inserted_ref = vr1;
    4449              : }
    4450              : 
    4451              : /* Insert a reference by it's pieces into the current hash table with
    4452              :    a value number of RESULT.  Return the resulting reference
    4453              :    structure we created.  */
    4454              : 
    4455              : vn_reference_t
    4456      1513502 : vn_reference_insert_pieces (tree vuse, alias_set_type set,
    4457              :                             alias_set_type base_set,
    4458              :                             poly_int64 offset, poly_int64 max_size, tree type,
    4459              :                             vec<vn_reference_op_s> operands,
    4460              :                             tree result, unsigned int value_id)
    4461              : 
    4462              : {
    4463      1513502 :   vn_reference_s **slot;
    4464      1513502 :   vn_reference_t vr1;
    4465              : 
    4466      1513502 :   vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
    4467      1513502 :   vr1->value_id = value_id;
    4468      1513502 :   vr1->vuse = vuse_ssa_val (vuse);
    4469      1513502 :   vr1->operands = operands;
    4470      1513502 :   valueize_refs (&vr1->operands);
    4471      1513502 :   vr1->type = type;
    4472      1513502 :   vr1->punned = false;
    4473      1513502 :   vr1->set = set;
    4474      1513502 :   vr1->base_set = base_set;
    4475      1513502 :   vr1->offset = offset;
    4476      1513502 :   vr1->max_size = max_size;
    4477      1513502 :   vr1->hashcode = vn_reference_compute_hash (vr1);
    4478      1513502 :   if (result && TREE_CODE (result) == SSA_NAME)
    4479       340205 :     result = SSA_VAL (result);
    4480      1513502 :   vr1->result = result;
    4481      1513502 :   vr1->result_vdef = NULL_TREE;
    4482              : 
    4483      1513502 :   slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
    4484              :                                                       INSERT);
    4485              : 
    4486              :   /* At this point we should have all the things inserted that we have
    4487              :      seen before, and we should never try inserting something that
    4488              :      already exists.  */
    4489      1513502 :   gcc_assert (!*slot);
    4490              : 
    4491      1513502 :   *slot = vr1;
    4492      1513502 :   vr1->next = last_inserted_ref;
    4493      1513502 :   last_inserted_ref = vr1;
    4494      1513502 :   return vr1;
    4495              : }
    4496              : 
    4497              : /* Compute and return the hash value for nary operation VBO1.  */
    4498              : 
    4499              : hashval_t
    4500    299931064 : vn_nary_op_compute_hash (const vn_nary_op_t vno1)
    4501              : {
    4502    299931064 :   inchash::hash hstate;
    4503    299931064 :   unsigned i;
    4504              : 
    4505    299931064 :   if (((vno1->length == 2
    4506    252555904 :         && commutative_tree_code (vno1->opcode))
    4507    137235301 :        || (vno1->length == 3
    4508      1544732 :            && commutative_ternary_tree_code (vno1->opcode)))
    4509    462629006 :       && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
    4510      2394571 :     std::swap (vno1->op[0], vno1->op[1]);
    4511    297536493 :   else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
    4512    297536493 :            && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
    4513              :     {
    4514       466246 :       std::swap (vno1->op[0], vno1->op[1]);
    4515       466246 :       vno1->opcode = swap_tree_comparison  (vno1->opcode);
    4516              :     }
    4517              : 
    4518    299931064 :   hstate.add_int (vno1->opcode);
    4519    856236465 :   for (i = 0; i < vno1->length; ++i)
    4520    556305401 :     inchash::add_expr (vno1->op[i], hstate);
    4521              : 
    4522    299931064 :   return hstate.end ();
    4523              : }
    4524              : 
    4525              : /* Compare nary operations VNO1 and VNO2 and return true if they are
    4526              :    equivalent.  */
    4527              : 
    4528              : bool
    4529    954882923 : vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
    4530              : {
    4531    954882923 :   unsigned i;
    4532              : 
    4533    954882923 :   if (vno1->hashcode != vno2->hashcode)
    4534              :     return false;
    4535              : 
    4536     49638399 :   if (vno1->length != vno2->length)
    4537              :     return false;
    4538              : 
    4539     49638399 :   if (vno1->opcode != vno2->opcode
    4540     49638399 :       || !types_compatible_p (vno1->type, vno2->type))
    4541      1144297 :     return false;
    4542              : 
    4543    140162388 :   for (i = 0; i < vno1->length; ++i)
    4544     91766659 :     if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
    4545              :       return false;
    4546              : 
    4547              :   /* BIT_INSERT_EXPR has an implicit operand as the type precision
    4548              :      of op1.  Need to check to make sure they are the same.  */
    4549     48395729 :   if (vno1->opcode == BIT_INSERT_EXPR
    4550          530 :       && TREE_CODE (vno1->op[1]) == INTEGER_CST
    4551     48395832 :       && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
    4552          103 :          != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
    4553              :     return false;
    4554              : 
    4555              :   return true;
    4556              : }
    4557              : 
    4558              : /* Initialize VNO from the pieces provided.  */
    4559              : 
    4560              : static void
    4561    185636429 : init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
    4562              :                              enum tree_code code, tree type, tree *ops)
    4563              : {
    4564    185636429 :   vno->opcode = code;
    4565    185636429 :   vno->length = length;
    4566    185636429 :   vno->type = type;
    4567      4471754 :   memcpy (&vno->op[0], ops, sizeof (tree) * length);
    4568            0 : }
    4569              : 
    4570              : /* Return the number of operands for a vn_nary ops structure from STMT.  */
    4571              : 
    4572              : unsigned int
    4573    108298016 : vn_nary_length_from_stmt (gimple *stmt)
    4574              : {
    4575    108298016 :   switch (gimple_assign_rhs_code (stmt))
    4576              :     {
    4577              :     case REALPART_EXPR:
    4578              :     case IMAGPART_EXPR:
    4579              :     case VIEW_CONVERT_EXPR:
    4580              :       return 1;
    4581              : 
    4582       598726 :     case BIT_FIELD_REF:
    4583       598726 :       return 3;
    4584              : 
    4585       511040 :     case CONSTRUCTOR:
    4586       511040 :       return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
    4587              : 
    4588    103849724 :     default:
    4589    103849724 :       return gimple_num_ops (stmt) - 1;
    4590              :     }
    4591              : }
    4592              : 
    4593              : /* Initialize VNO from STMT.  */
    4594              : 
    4595              : void
    4596    108298016 : init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
    4597              : {
    4598    108298016 :   unsigned i;
    4599              : 
    4600    108298016 :   vno->opcode = gimple_assign_rhs_code (stmt);
    4601    108298016 :   vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
    4602    108298016 :   switch (vno->opcode)
    4603              :     {
    4604      3338526 :     case REALPART_EXPR:
    4605      3338526 :     case IMAGPART_EXPR:
    4606      3338526 :     case VIEW_CONVERT_EXPR:
    4607      3338526 :       vno->length = 1;
    4608      3338526 :       vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
    4609      3338526 :       break;
    4610              : 
    4611       598726 :     case BIT_FIELD_REF:
    4612       598726 :       vno->length = 3;
    4613       598726 :       vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
    4614       598726 :       vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
    4615       598726 :       vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
    4616       598726 :       break;
    4617              : 
    4618       511040 :     case CONSTRUCTOR:
    4619       511040 :       vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
    4620      2017899 :       for (i = 0; i < vno->length; ++i)
    4621      1506859 :         vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
    4622              :       break;
    4623              : 
    4624    103849724 :     default:
    4625    103849724 :       gcc_checking_assert (!gimple_assign_single_p (stmt));
    4626    103849724 :       vno->length = gimple_num_ops (stmt) - 1;
    4627    284571725 :       for (i = 0; i < vno->length; ++i)
    4628    180722001 :         vno->op[i] = gimple_op (stmt, i + 1);
    4629              :     }
    4630    108298016 : }
    4631              : 
    4632              : /* Compute the hashcode for VNO and look for it in the hash table;
    4633              :    return the resulting value number if it exists in the hash table.
    4634              :    Return NULL_TREE if it does not exist in the hash table or if the
    4635              :    result field of the operation is NULL.  VNRESULT will contain the
    4636              :    vn_nary_op_t from the hashtable if it exists.  */
    4637              : 
    4638              : static tree
    4639    130238729 : vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
    4640              : {
    4641    130238729 :   vn_nary_op_s **slot;
    4642              : 
    4643    130238729 :   if (vnresult)
    4644    123083404 :     *vnresult = NULL;
    4645              : 
    4646    362152389 :   for (unsigned i = 0; i < vno->length; ++i)
    4647    231913660 :     if (TREE_CODE (vno->op[i]) == SSA_NAME)
    4648    164280312 :       vno->op[i] = SSA_VAL (vno->op[i]);
    4649              : 
    4650    130238729 :   vno->hashcode = vn_nary_op_compute_hash (vno);
    4651    130238729 :   slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
    4652    130238729 :   if (!slot)
    4653              :     return NULL_TREE;
    4654     17502715 :   if (vnresult)
    4655     17067805 :     *vnresult = *slot;
    4656     17502715 :   return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
    4657              : }
    4658              : 
    4659              : /* Lookup a n-ary operation by its pieces and return the resulting value
    4660              :    number if it exists in the hash table.  Return NULL_TREE if it does
    4661              :    not exist in the hash table or if the result field of the operation
    4662              :    is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
    4663              :    if it exists.  */
    4664              : 
    4665              : tree
    4666     74192683 : vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
    4667              :                           tree type, tree *ops, vn_nary_op_t *vnresult)
    4668              : {
    4669     74192683 :   vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
    4670              :                                   sizeof_vn_nary_op (length));
    4671     74192683 :   init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
    4672     74192683 :   return vn_nary_op_lookup_1 (vno1, vnresult);
    4673              : }
    4674              : 
    4675              : /* Lookup the rhs of STMT in the current hash table, and return the resulting
    4676              :    value number if it exists in the hash table.  Return NULL_TREE if
    4677              :    it does not exist in the hash table.  VNRESULT will contain the
    4678              :    vn_nary_op_t from the hashtable if it exists.  */
    4679              : 
    4680              : tree
    4681     56046046 : vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
    4682              : {
    4683     56046046 :   vn_nary_op_t vno1
    4684     56046046 :     = XALLOCAVAR (struct vn_nary_op_s,
    4685              :                   sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
    4686     56046046 :   init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
    4687     56046046 :   return vn_nary_op_lookup_1 (vno1, vnresult);
    4688              : }
    4689              : 
    4690              : /* Allocate a vn_nary_op_t with LENGTH operands on STACK.  */
    4691              : 
    4692              : vn_nary_op_t
    4693    168730255 : alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
    4694              : {
    4695    168730255 :   return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
    4696              : }
    4697              : 
    4698              : /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
    4699              :    obstack.  */
    4700              : 
    4701              : static vn_nary_op_t
    4702    151737577 : alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
    4703              : {
    4704            0 :   vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
    4705              : 
    4706    151737577 :   vno1->value_id = value_id;
    4707    151737577 :   vno1->length = length;
    4708    151737577 :   vno1->predicated_values = 0;
    4709    151737577 :   vno1->u.result = result;
    4710              : 
    4711    151737577 :   return vno1;
    4712              : }
    4713              : 
    4714              : /* Insert VNO into TABLE.  */
    4715              : 
    4716              : static vn_nary_op_t
    4717    156343564 : vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table)
    4718              : {
    4719    156343564 :   vn_nary_op_s **slot;
    4720              : 
    4721    156343564 :   gcc_assert (! vno->predicated_values
    4722              :               || (! vno->u.values->next
    4723              :                   && vno->u.values->n == 1));
    4724              : 
    4725    457478659 :   for (unsigned i = 0; i < vno->length; ++i)
    4726    301135095 :     if (TREE_CODE (vno->op[i]) == SSA_NAME)
    4727    196528330 :       vno->op[i] = SSA_VAL (vno->op[i]);
    4728              : 
    4729    156343564 :   vno->hashcode = vn_nary_op_compute_hash (vno);
    4730    156343564 :   slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
    4731    156343564 :   vno->unwind_to = *slot;
    4732    156343564 :   if (*slot)
    4733              :     {
    4734              :       /* Prefer non-predicated values.
    4735              :          ???  Only if those are constant, otherwise, with constant predicated
    4736              :          value, turn them into predicated values with entry-block validity
    4737              :          (???  but we always find the first valid result currently).  */
    4738     29930934 :       if ((*slot)->predicated_values
    4739     29192178 :           && ! vno->predicated_values)
    4740              :         {
    4741              :           /* ???  We cannot remove *slot from the unwind stack list.
    4742              :              For the moment we deal with this by skipping not found
    4743              :              entries but this isn't ideal ...  */
    4744        84047 :           *slot = vno;
    4745              :           /* ???  Maintain a stack of states we can unwind in
    4746              :              vn_nary_op_s?  But how far do we unwind?  In reality
    4747              :              we need to push change records somewhere...  Or not
    4748              :              unwind vn_nary_op_s and linking them but instead
    4749              :              unwind the results "list", linking that, which also
    4750              :              doesn't move on hashtable resize.  */
    4751              :           /* We can also have a ->unwind_to recording *slot there.
    4752              :              That way we can make u.values a fixed size array with
    4753              :              recording the number of entries but of course we then
    4754              :              have always N copies for each unwind_to-state.  Or we
    4755              :              make sure to only ever append and each unwinding will
    4756              :              pop off one entry (but how to deal with predicated
    4757              :              replaced with non-predicated here?)  */
    4758        84047 :           vno->next = last_inserted_nary;
    4759        84047 :           last_inserted_nary = vno;
    4760        84047 :           return vno;
    4761              :         }
    4762     29846887 :       else if (vno->predicated_values
    4763     29846535 :                && ! (*slot)->predicated_values)
    4764              :         return *slot;
    4765     29108483 :       else if (vno->predicated_values
    4766     29108131 :                && (*slot)->predicated_values)
    4767              :         {
    4768              :           /* ???  Factor this all into a insert_single_predicated_value
    4769              :              routine.  */
    4770     29108131 :           gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
    4771     29108131 :           basic_block vno_bb
    4772     29108131 :             = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
    4773     29108131 :           vn_pval *nval = vno->u.values;
    4774     29108131 :           vn_pval **next = &vno->u.values;
    4775     29108131 :           vn_pval *ins = NULL;
    4776     29108131 :           vn_pval *ins_at = NULL;
    4777              :           /* Find an existing value to append to.  */
    4778     54747725 :           for (vn_pval *val = (*slot)->u.values; val; val = val->next)
    4779              :             {
    4780     30093557 :               if (expressions_equal_p (val->result, nval->result))
    4781              :                 {
    4782              :                   /* Limit the number of places we register a predicate
    4783              :                      as valid.  */
    4784      4453963 :                   if (val->n > 8)
    4785       128402 :                     return *slot;
    4786     11074164 :                   for (unsigned i = 0; i < val->n; ++i)
    4787              :                     {
    4788      6983113 :                       basic_block val_bb
    4789      6983113 :                         = BASIC_BLOCK_FOR_FN (cfun,
    4790              :                                               val->valid_dominated_by_p[i]);
    4791      6983113 :                       if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
    4792              :                         /* Value registered with more generic predicate.  */
    4793       234510 :                         return *slot;
    4794      6748603 :                       else if (flag_checking)
    4795              :                         /* Shouldn't happen, we insert in RPO order.  */
    4796      6748603 :                         gcc_assert (!dominated_by_p (CDI_DOMINATORS,
    4797              :                                                      val_bb, vno_bb));
    4798              :                     }
    4799              :                   /* Append the location.  */
    4800      4091051 :                   ins_at = val;
    4801      4091051 :                   ins = (vn_pval *) obstack_alloc (&vn_tables_obstack,
    4802              :                                                    sizeof (vn_pval)
    4803              :                                                    + val->n * sizeof (int));
    4804      4091051 :                   ins->next = NULL;
    4805      4091051 :                   ins->result = val->result;
    4806      4091051 :                   ins->n = val->n + 1;
    4807      4091051 :                   memcpy (ins->valid_dominated_by_p,
    4808      4091051 :                           val->valid_dominated_by_p,
    4809      4091051 :                           val->n * sizeof (int));
    4810      4091051 :                   ins->valid_dominated_by_p[val->n] = vno_bb->index;
    4811      4091051 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    4812            4 :                     fprintf (dump_file, "Appending predicate to value.\n");
    4813              :                   break;
    4814              :                 }
    4815              :             }
    4816              :           /* Copy the rest of the value chain.  */
    4817     59333366 :           for (vn_pval *val = (*slot)->u.values; val; val = val->next)
    4818              :             {
    4819     30588147 :               if (val == ins_at)
    4820              :                 /* Replace the node we appended to.  */
    4821      4091051 :                 *next = ins;
    4822              :               else
    4823              :                 {
    4824              :                   /* Copy other predicated values.  */
    4825     26497096 :                   *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
    4826              :                                                      sizeof (vn_pval)
    4827              :                                                      + ((val->n-1)
    4828              :                                                         * sizeof (int)));
    4829     26497096 :                   memcpy (*next, val,
    4830     26497096 :                           sizeof (vn_pval) + (val->n-1) * sizeof (int));
    4831     26497096 :                   (*next)->next = NULL;
    4832              :                 }
    4833     30588147 :               next = &(*next)->next;
    4834              :             }
    4835              :           /* Append the value if we didn't find it.  */
    4836     28745219 :           if (!ins_at)
    4837     24654168 :             *next = nval;
    4838     28745219 :           *slot = vno;
    4839     28745219 :           vno->next = last_inserted_nary;
    4840     28745219 :           last_inserted_nary = vno;
    4841     28745219 :           return vno;
    4842              :         }
    4843              : 
    4844              :       /* While we do not want to insert things twice it's awkward to
    4845              :          avoid it in the case where visit_nary_op pattern-matches stuff
    4846              :          and ends up simplifying the replacement to itself.  We then
    4847              :          get two inserts, one from visit_nary_op and one from
    4848              :          vn_nary_build_or_lookup.
    4849              :          So allow inserts with the same value number.  */
    4850          352 :       if ((*slot)->u.result == vno->u.result)
    4851              :         return *slot;
    4852              :     }
    4853              : 
    4854              :   /* ???  There's also optimistic vs. previous committed state merging
    4855              :      that is problematic for the case of unwinding.  */
    4856              : 
    4857              :   /* ???  We should return NULL if we do not use 'vno' and have the
    4858              :      caller release it.  */
    4859    126412630 :   gcc_assert (!*slot);
    4860              : 
    4861    126412630 :   *slot = vno;
    4862    126412630 :   vno->next = last_inserted_nary;
    4863    126412630 :   last_inserted_nary = vno;
    4864    126412630 :   return vno;
    4865              : }
    4866              : 
    4867              : /* Insert a n-ary operation into the current hash table using it's
    4868              :    pieces.  Return the vn_nary_op_t structure we created and put in
    4869              :    the hashtable.  */
    4870              : 
    4871              : vn_nary_op_t
    4872       537524 : vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
    4873              :                           tree type, tree *ops,
    4874              :                           tree result, unsigned int value_id)
    4875              : {
    4876       537524 :   vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
    4877       537524 :   init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
    4878       537524 :   return vn_nary_op_insert_into (vno1, valid_info->nary);
    4879              : }
    4880              : 
    4881              : /* Return whether we can track a predicate valid when PRED_E is executed.  */
    4882              : 
    4883              : static bool
    4884    150118965 : can_track_predicate_on_edge (edge pred_e)
    4885              : {
    4886              :   /* ???  As we are currently recording the destination basic-block index in
    4887              :      vn_pval.valid_dominated_by_p and using dominance for the
    4888              :      validity check we cannot track predicates on all edges.  */
    4889    150118965 :   if (single_pred_p (pred_e->dest))
    4890              :     return true;
    4891              :   /* Never record for backedges.  */
    4892     11828004 :   if (pred_e->flags & EDGE_DFS_BACK)
    4893              :     return false;
    4894              :   /* When there's more than one predecessor we cannot track
    4895              :      predicate validity based on the destination block.  The
    4896              :      exception is when all other incoming edges sources are
    4897              :      dominated by the destination block.  */
    4898     11177929 :   edge_iterator ei;
    4899     11177929 :   edge e;
    4900     19168743 :   FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
    4901     17339919 :     if (e != pred_e && ! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
    4902              :       return false;
    4903              :   return true;
    4904              : }
    4905              : 
    4906              : static vn_nary_op_t
    4907    106434468 : vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
    4908              :                                      tree type, tree *ops,
    4909              :                                      tree result, unsigned int value_id,
    4910              :                                      edge pred_e)
    4911              : {
    4912    106434468 :   if (flag_checking)
    4913    106433632 :     gcc_assert (can_track_predicate_on_edge (pred_e));
    4914              : 
    4915        75234 :   if (dump_file && (dump_flags & TDF_DETAILS)
    4916              :       /* ???  Fix dumping, but currently we only get comparisons.  */
    4917    106505654 :       && TREE_CODE_CLASS (code) == tcc_comparison)
    4918              :     {
    4919        71186 :       fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
    4920        71186 :                pred_e->dest->index);
    4921        71186 :       print_generic_expr (dump_file, ops[0], TDF_SLIM);
    4922        71186 :       fprintf (dump_file, " %s ", get_tree_code_name (code));
    4923        71186 :       print_generic_expr (dump_file, ops[1], TDF_SLIM);
    4924       106408 :       fprintf (dump_file, " == %s\n",
    4925        71186 :                integer_zerop (result) ? "false" : "true");
    4926              :     }
    4927    106434468 :   vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
    4928    106434468 :   init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
    4929    106434468 :   vno1->predicated_values = 1;
    4930    106434468 :   vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
    4931              :                                               sizeof (vn_pval));
    4932    106434468 :   vno1->u.values->next = NULL;
    4933    106434468 :   vno1->u.values->result = result;
    4934    106434468 :   vno1->u.values->n = 1;
    4935    106434468 :   vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
    4936    106434468 :   return vn_nary_op_insert_into (vno1, valid_info->nary);
    4937              : }
    4938              : 
    4939              : static bool
    4940              : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool);
    4941              : 
    4942              : static tree
    4943      1720183 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb,
    4944              :                                  edge e = NULL)
    4945              : {
    4946      1720183 :   if (! vno->predicated_values)
    4947            0 :     return vno->u.result;
    4948      3574999 :   for (vn_pval *val = vno->u.values; val; val = val->next)
    4949      5488238 :     for (unsigned i = 0; i < val->n; ++i)
    4950              :       {
    4951      3633422 :         basic_block cand
    4952      3633422 :           = BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]);
    4953              :         /* Do not handle backedge executability optimistically since
    4954              :            when figuring out whether to iterate we do not consider
    4955              :            changed predication.
    4956              :            When asking for predicated values on an edge avoid looking
    4957              :            at edge executability for edges forward in our iteration
    4958              :            as well.  */
    4959      3633422 :         if (e && (e->flags & EDGE_DFS_BACK))
    4960              :           {
    4961        23362 :             if (dominated_by_p (CDI_DOMINATORS, bb, cand))
    4962         7769 :               return val->result;
    4963              :           }
    4964      3610060 :         else if (dominated_by_p_w_unex (bb, cand, false))
    4965       532083 :           return val->result;
    4966              :       }
    4967              :   return NULL_TREE;
    4968              : }
    4969              : 
    4970              : static tree
    4971       212560 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, edge e)
    4972              : {
    4973            0 :   return vn_nary_op_get_predicated_value (vno, e->src, e);
    4974              : }
    4975              : 
    4976              : /* Insert the rhs of STMT into the current hash table with a value number of
    4977              :    RESULT.  */
    4978              : 
    4979              : static vn_nary_op_t
    4980     44765585 : vn_nary_op_insert_stmt (gimple *stmt, tree result)
    4981              : {
    4982     44765585 :   vn_nary_op_t vno1
    4983     44765585 :     = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
    4984     44765585 :                         result, VN_INFO (result)->value_id);
    4985     44765585 :   init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
    4986     44765585 :   return vn_nary_op_insert_into (vno1, valid_info->nary);
    4987              : }
    4988              : 
    4989              : /* Compute a hashcode for PHI operation VP1 and return it.  */
    4990              : 
    4991              : static inline hashval_t
    4992     49593600 : vn_phi_compute_hash (vn_phi_t vp1)
    4993              : {
    4994     49593600 :   inchash::hash hstate;
    4995     49593600 :   tree phi1op;
    4996     49593600 :   tree type;
    4997     49593600 :   edge e;
    4998     49593600 :   edge_iterator ei;
    4999              : 
    5000     99187200 :   hstate.add_int (EDGE_COUNT (vp1->block->preds));
    5001     49593600 :   switch (EDGE_COUNT (vp1->block->preds))
    5002              :     {
    5003              :     case 1:
    5004              :       break;
    5005     42752819 :     case 2:
    5006              :       /* When this is a PHI node subject to CSE for different blocks
    5007              :          avoid hashing the block index.  */
    5008     42752819 :       if (vp1->cclhs)
    5009              :         break;
    5010              :       /* Fallthru.  */
    5011     33514405 :     default:
    5012     33514405 :       hstate.add_int (vp1->block->index);
    5013              :     }
    5014              : 
    5015              :   /* If all PHI arguments are constants we need to distinguish
    5016              :      the PHI node via its type.  */
    5017     49593600 :   type = vp1->type;
    5018     49593600 :   hstate.merge_hash (vn_hash_type (type));
    5019              : 
    5020    172077708 :   FOR_EACH_EDGE (e, ei, vp1->block->preds)
    5021              :     {
    5022              :       /* Don't hash backedge values they need to be handled as VN_TOP
    5023              :          for optimistic value-numbering.  */
    5024    122484108 :       if (e->flags & EDGE_DFS_BACK)
    5025     27669996 :         continue;
    5026              : 
    5027     94814112 :       phi1op = vp1->phiargs[e->dest_idx];
    5028     94814112 :       if (phi1op == VN_TOP)
    5029       245027 :         continue;
    5030     94569085 :       inchash::add_expr (phi1op, hstate);
    5031              :     }
    5032              : 
    5033     49593600 :   return hstate.end ();
    5034              : }
    5035              : 
    5036              : 
    5037              : /* Return true if COND1 and COND2 represent the same condition, set
    5038              :    *INVERTED_P if one needs to be inverted to make it the same as
    5039              :    the other.  */
    5040              : 
    5041              : static bool
    5042      3774922 : cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
    5043              :                     gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
    5044              : {
    5045      3774922 :   enum tree_code code1 = gimple_cond_code (cond1);
    5046      3774922 :   enum tree_code code2 = gimple_cond_code (cond2);
    5047              : 
    5048      3774922 :   *inverted_p = false;
    5049      3774922 :   if (code1 == code2)
    5050              :     ;
    5051       299629 :   else if (code1 == swap_tree_comparison (code2))
    5052              :     std::swap (lhs2, rhs2);
    5053       263555 :   else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
    5054       131302 :     *inverted_p = true;
    5055       132253 :   else if (code1 == invert_tree_comparison
    5056       132253 :                       (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
    5057              :     {
    5058        10388 :       std::swap (lhs2, rhs2);
    5059        10388 :       *inverted_p = true;
    5060              :     }
    5061              :   else
    5062              :     return false;
    5063              : 
    5064      3653057 :   return ((expressions_equal_p (lhs1, lhs2)
    5065       108198 :            && expressions_equal_p (rhs1, rhs2))
    5066      3677914 :           || (commutative_tree_code (code1)
    5067      1788196 :               && expressions_equal_p (lhs1, rhs2)
    5068         2300 :               && expressions_equal_p (rhs1, lhs2)));
    5069              : }
    5070              : 
    5071              : /* Compare two phi entries for equality, ignoring VN_TOP arguments.  */
    5072              : 
    5073              : static int
    5074     40602484 : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
    5075              : {
    5076     40602484 :   if (vp1->hashcode != vp2->hashcode)
    5077              :     return false;
    5078              : 
    5079     12673770 :   if (vp1->block != vp2->block)
    5080              :     {
    5081     11347767 :       if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
    5082              :         return false;
    5083              : 
    5084     36478324 :       switch (EDGE_COUNT (vp1->block->preds))
    5085              :         {
    5086              :         case 1:
    5087              :           /* Single-arg PHIs are just copies.  */
    5088              :           break;
    5089              : 
    5090      3782589 :         case 2:
    5091      3782589 :           {
    5092              :             /* Make sure both PHIs are classified as CSEable.  */
    5093      3782589 :             if (! vp1->cclhs || ! vp2->cclhs)
    5094              :               return false;
    5095              : 
    5096              :             /* Rule out backedges into the PHI.  */
    5097      3782589 :             gcc_checking_assert
    5098              :               (vp1->block->loop_father->header != vp1->block
    5099              :                && vp2->block->loop_father->header != vp2->block);
    5100              : 
    5101              :             /* If the PHI nodes do not have compatible types
    5102              :                they are not the same.  */
    5103      3782589 :             if (!types_compatible_p (vp1->type, vp2->type))
    5104              :               return false;
    5105              : 
    5106              :             /* If the immediate dominator end in switch stmts multiple
    5107              :                values may end up in the same PHI arg via intermediate
    5108              :                CFG merges.  */
    5109      3774922 :             basic_block idom1
    5110      3774922 :               = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
    5111      3774922 :             basic_block idom2
    5112      3774922 :               = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
    5113      3774922 :             gcc_checking_assert (EDGE_COUNT (idom1->succs) == 2
    5114              :                                  && EDGE_COUNT (idom2->succs) == 2);
    5115              : 
    5116              :             /* Verify the controlling stmt is the same.  */
    5117      7549844 :             gcond *last1 = as_a <gcond *> (*gsi_last_bb (idom1));
    5118      7549844 :             gcond *last2 = as_a <gcond *> (*gsi_last_bb (idom2));
    5119      3774922 :             bool inverted_p;
    5120      3774922 :             if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
    5121      3774922 :                                       last2, vp2->cclhs, vp2->ccrhs,
    5122              :                                       &inverted_p))
    5123              :               return false;
    5124              : 
    5125              :             /* Get at true/false controlled edges into the PHI.  */
    5126        83426 :             edge te1, te2, fe1, fe2;
    5127        83426 :             if (! extract_true_false_controlled_edges (idom1, vp1->block,
    5128              :                                                        &te1, &fe1)
    5129        83426 :                 || ! extract_true_false_controlled_edges (idom2, vp2->block,
    5130              :                                                           &te2, &fe2))
    5131        36241 :               return false;
    5132              : 
    5133              :             /* Swap edges if the second condition is the inverted of the
    5134              :                first.  */
    5135        47185 :             if (inverted_p)
    5136         2038 :               std::swap (te2, fe2);
    5137              : 
    5138              :             /* Since we do not know which edge will be executed we have
    5139              :                to be careful when matching VN_TOP.  Be conservative and
    5140              :                only match VN_TOP == VN_TOP for now, we could allow
    5141              :                VN_TOP on the not prevailing PHI though.  See for example
    5142              :                PR102920.  */
    5143        47185 :             if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
    5144        47185 :                                        vp2->phiargs[te2->dest_idx], false)
    5145        92557 :                 || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
    5146        45372 :                                           vp2->phiargs[fe2->dest_idx], false))
    5147         1813 :               return false;
    5148              : 
    5149              :             return true;
    5150              :           }
    5151              : 
    5152              :         default:
    5153              :           return false;
    5154              :         }
    5155              :     }
    5156              : 
    5157              :   /* If the PHI nodes do not have compatible types
    5158              :      they are not the same.  */
    5159      8891181 :   if (!types_compatible_p (vp1->type, vp2->type))
    5160              :     return false;
    5161              : 
    5162              :   /* Any phi in the same block will have it's arguments in the
    5163              :      same edge order, because of how we store phi nodes.  */
    5164      8890067 :   unsigned nargs = EDGE_COUNT (vp1->block->preds);
    5165     20603247 :   for (unsigned i = 0; i < nargs; ++i)
    5166              :     {
    5167     16479087 :       tree phi1op = vp1->phiargs[i];
    5168     16479087 :       tree phi2op = vp2->phiargs[i];
    5169     16479087 :       if (phi1op == phi2op)
    5170     11617791 :         continue;
    5171      4861296 :       if (!expressions_equal_p (phi1op, phi2op, false))
    5172              :         return false;
    5173              :     }
    5174              : 
    5175              :   return true;
    5176              : }
    5177              : 
    5178              : /* Lookup PHI in the current hash table, and return the resulting
    5179              :    value number if it exists in the hash table.  Return NULL_TREE if
    5180              :    it does not exist in the hash table. */
    5181              : 
    5182              : static tree
    5183     27184360 : vn_phi_lookup (gimple *phi, bool backedges_varying_p)
    5184              : {
    5185     27184360 :   vn_phi_s **slot;
    5186     27184360 :   struct vn_phi_s *vp1;
    5187     27184360 :   edge e;
    5188     27184360 :   edge_iterator ei;
    5189              : 
    5190     27184360 :   vp1 = XALLOCAVAR (struct vn_phi_s,
    5191              :                     sizeof (struct vn_phi_s)
    5192              :                     + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
    5193              : 
    5194              :   /* Canonicalize the SSA_NAME's to their value number.  */
    5195     93646192 :   FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
    5196              :     {
    5197     66461832 :       tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
    5198     66461832 :       if (TREE_CODE (def) == SSA_NAME
    5199     55308107 :           && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
    5200              :         {
    5201     52773065 :           if (!virtual_operand_p (def)
    5202     52773065 :               && ssa_undefined_value_p (def, false))
    5203       135987 :             def = VN_TOP;
    5204              :           else
    5205     52637078 :             def = SSA_VAL (def);
    5206              :         }
    5207     66461832 :       vp1->phiargs[e->dest_idx] = def;
    5208              :     }
    5209     27184360 :   vp1->type = TREE_TYPE (gimple_phi_result (phi));
    5210     27184360 :   vp1->block = gimple_bb (phi);
    5211              :   /* Extract values of the controlling condition.  */
    5212     27184360 :   vp1->cclhs = NULL_TREE;
    5213     27184360 :   vp1->ccrhs = NULL_TREE;
    5214     27184360 :   if (EDGE_COUNT (vp1->block->preds) == 2
    5215     27184360 :       && vp1->block->loop_father->header != vp1->block)
    5216              :     {
    5217      8480334 :       basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
    5218      8480334 :       if (EDGE_COUNT (idom1->succs) == 2)
    5219     16872216 :         if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
    5220              :           {
    5221              :             /* ???  We want to use SSA_VAL here.  But possibly not
    5222              :                allow VN_TOP.  */
    5223      8218027 :             vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
    5224      8218027 :             vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
    5225              :           }
    5226              :     }
    5227     27184360 :   vp1->hashcode = vn_phi_compute_hash (vp1);
    5228     27184360 :   slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
    5229     27184360 :   if (!slot)
    5230              :     return NULL_TREE;
    5231      4169532 :   return (*slot)->result;
    5232              : }
    5233              : 
    5234              : /* Insert PHI into the current hash table with a value number of
    5235              :    RESULT.  */
    5236              : 
    5237              : static vn_phi_t
    5238     22409240 : vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
    5239              : {
    5240     22409240 :   vn_phi_s **slot;
    5241     22409240 :   vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
    5242              :                                            sizeof (vn_phi_s)
    5243              :                                            + ((gimple_phi_num_args (phi) - 1)
    5244              :                                               * sizeof (tree)));
    5245     22409240 :   edge e;
    5246     22409240 :   edge_iterator ei;
    5247              : 
    5248              :   /* Canonicalize the SSA_NAME's to their value number.  */
    5249     78431516 :   FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
    5250              :     {
    5251     56022276 :       tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
    5252     56022276 :       if (TREE_CODE (def) == SSA_NAME
    5253     45945972 :           && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
    5254              :         {
    5255     43411351 :           if (!virtual_operand_p (def)
    5256     43411351 :               && ssa_undefined_value_p (def, false))
    5257       109284 :             def = VN_TOP;
    5258              :           else
    5259     43302067 :             def = SSA_VAL (def);
    5260              :         }
    5261     56022276 :       vp1->phiargs[e->dest_idx] = def;
    5262              :     }
    5263     22409240 :   vp1->value_id = VN_INFO (result)->value_id;
    5264     22409240 :   vp1->type = TREE_TYPE (gimple_phi_result (phi));
    5265     22409240 :   vp1->block = gimple_bb (phi);
    5266              :   /* Extract values of the controlling condition.  */
    5267     22409240 :   vp1->cclhs = NULL_TREE;
    5268     22409240 :   vp1->ccrhs = NULL_TREE;
    5269     22409240 :   if (EDGE_COUNT (vp1->block->preds) == 2
    5270     22409240 :       && vp1->block->loop_father->header != vp1->block)
    5271              :     {
    5272      8119579 :       basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
    5273      8119579 :       if (EDGE_COUNT (idom1->succs) == 2)
    5274     16153788 :         if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
    5275              :           {
    5276              :             /* ???  We want to use SSA_VAL here.  But possibly not
    5277              :                allow VN_TOP.  */
    5278      7861168 :             vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
    5279      7861168 :             vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
    5280              :           }
    5281              :     }
    5282     22409240 :   vp1->result = result;
    5283     22409240 :   vp1->hashcode = vn_phi_compute_hash (vp1);
    5284              : 
    5285     22409240 :   slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
    5286     22409240 :   gcc_assert (!*slot);
    5287              : 
    5288     22409240 :   *slot = vp1;
    5289     22409240 :   vp1->next = last_inserted_phi;
    5290     22409240 :   last_inserted_phi = vp1;
    5291     22409240 :   return vp1;
    5292              : }
    5293              : 
    5294              : 
    5295              : /* Return true if BB1 is dominated by BB2 taking into account edges
    5296              :    that are not executable.  When ALLOW_BACK is false consider not
    5297              :    executable backedges as executable.  */
    5298              : 
    5299              : static bool
    5300     71775194 : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool allow_back)
    5301              : {
    5302     71775194 :   edge_iterator ei;
    5303     71775194 :   edge e;
    5304              : 
    5305     71775194 :   if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
    5306              :     return true;
    5307              : 
    5308              :   /* Before iterating we'd like to know if there exists a
    5309              :      (executable) path from bb2 to bb1 at all, if not we can
    5310              :      directly return false.  For now simply iterate once.  */
    5311              : 
    5312              :   /* Iterate to the single executable bb1 predecessor.  */
    5313     21313755 :   if (EDGE_COUNT (bb1->preds) > 1)
    5314              :     {
    5315      2948110 :       edge prede = NULL;
    5316      6440652 :       FOR_EACH_EDGE (e, ei, bb1->preds)
    5317      6006662 :         if ((e->flags & EDGE_EXECUTABLE)
    5318       622697 :             || (!allow_back && (e->flags & EDGE_DFS_BACK)))
    5319              :           {
    5320      5462230 :             if (prede)
    5321              :               {
    5322              :                 prede = NULL;
    5323              :                 break;
    5324              :               }
    5325              :             prede = e;
    5326              :           }
    5327      2948110 :       if (prede)
    5328              :         {
    5329       433990 :           bb1 = prede->src;
    5330              : 
    5331              :           /* Re-do the dominance check with changed bb1.  */
    5332       433990 :           if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
    5333              :             return true;
    5334              :         }
    5335              :     }
    5336              : 
    5337              :   /* Iterate to the single executable bb2 successor.  */
    5338     21067932 :   if (EDGE_COUNT (bb2->succs) > 1)
    5339              :     {
    5340      6356114 :       edge succe = NULL;
    5341     12876930 :       FOR_EACH_EDGE (e, ei, bb2->succs)
    5342     12712489 :         if ((e->flags & EDGE_EXECUTABLE)
    5343       203101 :             || (!allow_back && (e->flags & EDGE_DFS_BACK)))
    5344              :           {
    5345     12509429 :             if (succe)
    5346              :               {
    5347              :                 succe = NULL;
    5348              :                 break;
    5349              :               }
    5350              :             succe = e;
    5351              :           }
    5352      6356114 :       if (succe
    5353              :           /* Limit the number of edges we check, we should bring in
    5354              :              context from the iteration and compute the single
    5355              :              executable incoming edge when visiting a block.  */
    5356      6356114 :           && EDGE_COUNT (succe->dest->preds) < 8)
    5357              :         {
    5358              :           /* Verify the reached block is only reached through succe.
    5359              :              If there is only one edge we can spare us the dominator
    5360              :              check and iterate directly.  */
    5361       125539 :           if (EDGE_COUNT (succe->dest->preds) > 1)
    5362              :             {
    5363        53828 :               FOR_EACH_EDGE (e, ei, succe->dest->preds)
    5364        41663 :                 if (e != succe
    5365        26996 :                     && ((e->flags & EDGE_EXECUTABLE)
    5366        17942 :                         || (!allow_back && (e->flags & EDGE_DFS_BACK))))
    5367              :                   {
    5368              :                     succe = NULL;
    5369              :                     break;
    5370              :                   }
    5371              :             }
    5372       125539 :           if (succe)
    5373              :             {
    5374       116476 :               bb2 = succe->dest;
    5375              : 
    5376              :               /* Re-do the dominance check with changed bb2.  */
    5377       116476 :               if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
    5378              :                 return true;
    5379              :             }
    5380              :         }
    5381              :     }
    5382              :   /* Iterate to the single successor of bb2 with only a single executable
    5383              :      incoming edge.  */
    5384     14711818 :   else if (EDGE_COUNT (bb2->succs) == 1
    5385     14158474 :            && EDGE_COUNT (single_succ (bb2)->preds) > 1
    5386              :            /* Limit the number of edges we check, we should bring in
    5387              :               context from the iteration and compute the single
    5388              :               executable incoming edge when visiting a block.  */
    5389     28619799 :            && EDGE_COUNT (single_succ (bb2)->preds) < 8)
    5390              :     {
    5391      4958805 :       edge prede = NULL;
    5392     11209000 :       FOR_EACH_EDGE (e, ei, single_succ (bb2)->preds)
    5393     10646096 :         if ((e->flags & EDGE_EXECUTABLE)
    5394      1341734 :             || (!allow_back && (e->flags & EDGE_DFS_BACK)))
    5395              :           {
    5396      9308651 :             if (prede)
    5397              :               {
    5398              :                 prede = NULL;
    5399              :                 break;
    5400              :               }
    5401              :             prede = e;
    5402              :           }
    5403              :       /* We might actually get to a query with BB2 not visited yet when
    5404              :          we're querying for a predicated value.  */
    5405      4958805 :       if (prede && prede->src == bb2)
    5406              :         {
    5407       502796 :           bb2 = prede->dest;
    5408              : 
    5409              :           /* Re-do the dominance check with changed bb2.  */
    5410       502796 :           if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
    5411              :             return true;
    5412              :         }
    5413              :     }
    5414              : 
    5415              :   /* We could now iterate updating bb1 / bb2.  */
    5416              :   return false;
    5417              : }
    5418              : 
    5419              : /* Set the value number of FROM to TO, return true if it has changed
    5420              :    as a result.  */
    5421              : 
    5422              : static inline bool
    5423    202756683 : set_ssa_val_to (tree from, tree to)
    5424              : {
    5425    202756683 :   vn_ssa_aux_t from_info = VN_INFO (from);
    5426    202756683 :   tree currval = from_info->valnum; // SSA_VAL (from)
    5427    202756683 :   poly_int64 toff, coff;
    5428    202756683 :   bool curr_undefined = false;
    5429    202756683 :   bool curr_invariant = false;
    5430              : 
    5431              :   /* The only thing we allow as value numbers are ssa_names
    5432              :      and invariants.  So assert that here.  We don't allow VN_TOP
    5433              :      as visiting a stmt should produce a value-number other than
    5434              :      that.
    5435              :      ???  Still VN_TOP can happen for unreachable code, so force
    5436              :      it to varying in that case.  Not all code is prepared to
    5437              :      get VN_TOP on valueization.  */
    5438    202756683 :   if (to == VN_TOP)
    5439              :     {
    5440              :       /* ???  When iterating and visiting PHI <undef, backedge-value>
    5441              :          for the first time we rightfully get VN_TOP and we need to
    5442              :          preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
    5443              :          With SCCVN we were simply lucky we iterated the other PHI
    5444              :          cycles first and thus visited the backedge-value DEF.  */
    5445            0 :       if (currval == VN_TOP)
    5446            0 :         goto set_and_exit;
    5447            0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5448            0 :         fprintf (dump_file, "Forcing value number to varying on "
    5449              :                  "receiving VN_TOP\n");
    5450              :       to = from;
    5451              :     }
    5452              : 
    5453    202756683 :   gcc_checking_assert (to != NULL_TREE
    5454              :                        && ((TREE_CODE (to) == SSA_NAME
    5455              :                             && (to == from || SSA_VAL (to) == to))
    5456              :                            || is_gimple_min_invariant (to)));
    5457              : 
    5458    202756683 :   if (from != to)
    5459              :     {
    5460     32337705 :       if (currval == from)
    5461              :         {
    5462        13824 :           if (dump_file && (dump_flags & TDF_DETAILS))
    5463              :             {
    5464            0 :               fprintf (dump_file, "Not changing value number of ");
    5465            0 :               print_generic_expr (dump_file, from);
    5466            0 :               fprintf (dump_file, " from VARYING to ");
    5467            0 :               print_generic_expr (dump_file, to);
    5468            0 :               fprintf (dump_file, "\n");
    5469              :             }
    5470        13824 :           return false;
    5471              :         }
    5472     32323881 :       curr_invariant = is_gimple_min_invariant (currval);
    5473     64647762 :       curr_undefined = (TREE_CODE (currval) == SSA_NAME
    5474      3852624 :                         && !virtual_operand_p (currval)
    5475     35952656 :                         && ssa_undefined_value_p (currval, false));
    5476     32323881 :       if (currval != VN_TOP
    5477              :           && !curr_invariant
    5478      5346596 :           && !curr_undefined
    5479     36163349 :           && is_gimple_min_invariant (to))
    5480              :         {
    5481          220 :           if (dump_file && (dump_flags & TDF_DETAILS))
    5482              :             {
    5483            0 :               fprintf (dump_file, "Forcing VARYING instead of changing "
    5484              :                        "value number of ");
    5485            0 :               print_generic_expr (dump_file, from);
    5486            0 :               fprintf (dump_file, " from ");
    5487            0 :               print_generic_expr (dump_file, currval);
    5488            0 :               fprintf (dump_file, " (non-constant) to ");
    5489            0 :               print_generic_expr (dump_file, to);
    5490            0 :               fprintf (dump_file, " (constant)\n");
    5491              :             }
    5492              :           to = from;
    5493              :         }
    5494     32323661 :       else if (currval != VN_TOP
    5495      5346376 :                && !curr_undefined
    5496      5333220 :                && TREE_CODE (to) == SSA_NAME
    5497      4500480 :                && !virtual_operand_p (to)
    5498     36600292 :                && ssa_undefined_value_p (to, false))
    5499              :         {
    5500            6 :           if (dump_file && (dump_flags & TDF_DETAILS))
    5501              :             {
    5502            0 :               fprintf (dump_file, "Forcing VARYING instead of changing "
    5503              :                        "value number of ");
    5504            0 :               print_generic_expr (dump_file, from);
    5505            0 :               fprintf (dump_file, " from ");
    5506            0 :               print_generic_expr (dump_file, currval);
    5507            0 :               fprintf (dump_file, " (non-undefined) to ");
    5508            0 :               print_generic_expr (dump_file, to);
    5509            0 :               fprintf (dump_file, " (undefined)\n");
    5510              :             }
    5511              :           to = from;
    5512              :         }
    5513     32323655 :       else if (TREE_CODE (to) == SSA_NAME
    5514     32323655 :                && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
    5515              :         to = from;
    5516              :     }
    5517              : 
    5518    170418978 : set_and_exit:
    5519    202742859 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5520              :     {
    5521       398558 :       fprintf (dump_file, "Setting value number of ");
    5522       398558 :       print_generic_expr (dump_file, from);
    5523       398558 :       fprintf (dump_file, " to ");
    5524       398558 :       print_generic_expr (dump_file, to);
    5525              :     }
    5526              : 
    5527    202742859 :   if (currval != to
    5528    165111721 :       && !operand_equal_p (currval, to, 0)
    5529              :       /* Different undefined SSA names are not actually different.  See
    5530              :          PR82320 for a testcase were we'd otherwise not terminate iteration.  */
    5531    165042825 :       && !(curr_undefined
    5532         3389 :            && TREE_CODE (to) == SSA_NAME
    5533          578 :            && !virtual_operand_p (to)
    5534          578 :            && ssa_undefined_value_p (to, false))
    5535              :       /* ???  For addresses involving volatile objects or types operand_equal_p
    5536              :          does not reliably detect ADDR_EXPRs as equal.  We know we are only
    5537              :          getting invariant gimple addresses here, so can use
    5538              :          get_addr_base_and_unit_offset to do this comparison.  */
    5539    367785074 :       && !(TREE_CODE (currval) == ADDR_EXPR
    5540       456649 :            && TREE_CODE (to) == ADDR_EXPR
    5541           12 :            && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
    5542            6 :                == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
    5543            6 :            && known_eq (coff, toff)))
    5544              :     {
    5545    165042209 :       if (to != from
    5546     27974265 :           && currval != VN_TOP
    5547      1000569 :           && !curr_undefined
    5548              :           /* We do not want to allow lattice transitions from one value
    5549              :              to another since that may lead to not terminating iteration
    5550              :              (see PR95049).  Since there's no convenient way to check
    5551              :              for the allowed transition of VAL -> PHI (loop entry value,
    5552              :              same on two PHIs, to same PHI result) we restrict the check
    5553              :              to invariants.  */
    5554      1000569 :           && curr_invariant
    5555    165703435 :           && is_gimple_min_invariant (to))
    5556              :         {
    5557            0 :           if (dump_file && (dump_flags & TDF_DETAILS))
    5558            0 :             fprintf (dump_file, " forced VARYING");
    5559              :           to = from;
    5560              :         }
    5561    165042209 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5562       398242 :         fprintf (dump_file, " (changed)\n");
    5563    165042209 :       from_info->valnum = to;
    5564    165042209 :       return true;
    5565              :     }
    5566     37700650 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5567          316 :     fprintf (dump_file, "\n");
    5568              :   return false;
    5569              : }
    5570              : 
    5571              : /* Set all definitions in STMT to value number to themselves.
    5572              :    Return true if a value number changed. */
    5573              : 
    5574              : static bool
    5575    280932863 : defs_to_varying (gimple *stmt)
    5576              : {
    5577    280932863 :   bool changed = false;
    5578    280932863 :   ssa_op_iter iter;
    5579    280932863 :   def_operand_p defp;
    5580              : 
    5581    310049530 :   FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
    5582              :     {
    5583     29116667 :       tree def = DEF_FROM_PTR (defp);
    5584     29116667 :       changed |= set_ssa_val_to (def, def);
    5585              :     }
    5586    280932863 :   return changed;
    5587              : }
    5588              : 
    5589              : /* Visit a copy between LHS and RHS, return true if the value number
    5590              :    changed.  */
    5591              : 
    5592              : static bool
    5593      7941906 : visit_copy (tree lhs, tree rhs)
    5594              : {
    5595              :   /* Valueize.  */
    5596      7941906 :   rhs = SSA_VAL (rhs);
    5597              : 
    5598      7941906 :   return set_ssa_val_to (lhs, rhs);
    5599              : }
    5600              : 
    5601              : /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
    5602              :    is the same.  */
    5603              : 
    5604              : static tree
    5605      2449702 : valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
    5606              : {
    5607      2449702 :   if (TREE_CODE (op) == SSA_NAME)
    5608      2152374 :     op = vn_valueize (op);
    5609              : 
    5610              :   /* Either we have the op widened available.  */
    5611      2449702 :   tree ops[3] = {};
    5612      2449702 :   ops[0] = op;
    5613      2449702 :   tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
    5614              :                                        wide_type, ops, NULL);
    5615      2449702 :   if (tem)
    5616              :     return tem;
    5617              : 
    5618              :   /* Or the op is truncated from some existing value.  */
    5619      2162995 :   if (allow_truncate && TREE_CODE (op) == SSA_NAME)
    5620              :     {
    5621       542543 :       gimple *def = SSA_NAME_DEF_STMT (op);
    5622       542543 :       if (is_gimple_assign (def)
    5623       542543 :           && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
    5624              :         {
    5625       291667 :           tem = gimple_assign_rhs1 (def);
    5626       291667 :           if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
    5627              :             {
    5628       198530 :               if (TREE_CODE (tem) == SSA_NAME)
    5629       198530 :                 tem = vn_valueize (tem);
    5630       198530 :               return tem;
    5631              :             }
    5632              :         }
    5633              :     }
    5634              : 
    5635              :   /* For constants simply extend it.  */
    5636      1964465 :   if (TREE_CODE (op) == INTEGER_CST)
    5637       330466 :     return wide_int_to_tree (wide_type, wi::to_widest (op));
    5638              : 
    5639              :   return NULL_TREE;
    5640              : }
    5641              : 
    5642              : /* Visit a nary operator RHS, value number it, and return true if the
    5643              :    value number of LHS has changed as a result.  */
    5644              : 
    5645              : static bool
    5646     48453440 : visit_nary_op (tree lhs, gassign *stmt)
    5647              : {
    5648     48453440 :   vn_nary_op_t vnresult;
    5649     48453440 :   tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
    5650     48453440 :   if (! result && vnresult)
    5651       153875 :     result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
    5652     44836754 :   if (result)
    5653      3686514 :     return set_ssa_val_to (lhs, result);
    5654              : 
    5655              :   /* Do some special pattern matching for redundancies of operations
    5656              :      in different types.  */
    5657     44766926 :   enum tree_code code = gimple_assign_rhs_code (stmt);
    5658     44766926 :   tree type = TREE_TYPE (lhs);
    5659     44766926 :   tree rhs1 = gimple_assign_rhs1 (stmt);
    5660     44766926 :   switch (code)
    5661              :     {
    5662      9938647 :     CASE_CONVERT:
    5663              :       /* Match arithmetic done in a different type where we can easily
    5664              :          substitute the result from some earlier sign-changed or widened
    5665              :          operation.  */
    5666      9938647 :       if (INTEGRAL_TYPE_P (type)
    5667      8900161 :           && TREE_CODE (rhs1) == SSA_NAME
    5668              :           /* We only handle sign-changes, zero-extension -> & mask or
    5669              :              sign-extension if we know the inner operation doesn't
    5670              :              overflow.  */
    5671     18603602 :           && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
    5672      5238795 :                 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
    5673      5237997 :                     && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
    5674      7936456 :                && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
    5675      5879938 :               || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
    5676              :         {
    5677      7599339 :           gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
    5678      5465829 :           if (def
    5679      5465829 :               && (gimple_assign_rhs_code (def) == PLUS_EXPR
    5680      4243346 :                   || gimple_assign_rhs_code (def) == MINUS_EXPR
    5681      4101303 :                   || gimple_assign_rhs_code (def) == MULT_EXPR))
    5682              :             {
    5683      1974378 :               tree ops[3] = {};
    5684              :               /* When requiring a sign-extension we cannot model a
    5685              :                  previous truncation with a single op so don't bother.  */
    5686      1974378 :               bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
    5687              :               /* Either we have the op widened available.  */
    5688      1974378 :               ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
    5689              :                                            allow_truncate);
    5690      1974378 :               if (ops[0])
    5691       950648 :                 ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
    5692              :                                              allow_truncate);
    5693      1974378 :               if (ops[0] && ops[1])
    5694              :                 {
    5695       340379 :                   ops[0] = vn_nary_op_lookup_pieces
    5696       340379 :                       (2, gimple_assign_rhs_code (def), type, ops, NULL);
    5697              :                   /* We have wider operation available.  */
    5698       340379 :                   if (ops[0]
    5699              :                       /* If the leader is a wrapping operation we can
    5700              :                          insert it for code hoisting w/o introducing
    5701              :                          undefined overflow.  If it is not it has to
    5702              :                          be available.  See PR86554.  */
    5703       340379 :                       && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops[0]))
    5704         1759 :                           || (rpo_avail && vn_context_bb
    5705         1759 :                               && rpo_avail->eliminate_avail (vn_context_bb,
    5706              :                                                              ops[0]))))
    5707              :                     {
    5708         9608 :                       unsigned lhs_prec = TYPE_PRECISION (type);
    5709         9608 :                       unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
    5710         9608 :                       if (lhs_prec == rhs_prec
    5711         9608 :                           || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
    5712         1777 :                               && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
    5713              :                         {
    5714         9009 :                           gimple_match_op match_op (gimple_match_cond::UNCOND,
    5715         9009 :                                                     NOP_EXPR, type, ops[0]);
    5716         9009 :                           result = vn_nary_build_or_lookup (&match_op);
    5717         9009 :                           if (result)
    5718              :                             {
    5719         9009 :                               bool changed = set_ssa_val_to (lhs, result);
    5720         9009 :                               if (TREE_CODE (result) == SSA_NAME)
    5721         9009 :                                 vn_nary_op_insert_stmt (stmt, result);
    5722         9009 :                               return changed;
    5723              :                             }
    5724              :                         }
    5725              :                       else
    5726              :                         {
    5727          599 :                           tree mask = wide_int_to_tree
    5728          599 :                             (type, wi::mask (rhs_prec, false, lhs_prec));
    5729          599 :                           gimple_match_op match_op (gimple_match_cond::UNCOND,
    5730          599 :                                                     BIT_AND_EXPR,
    5731          599 :                                                     TREE_TYPE (lhs),
    5732          599 :                                                     ops[0], mask);
    5733          599 :                           result = vn_nary_build_or_lookup (&match_op);
    5734          599 :                           if (result)
    5735              :                             {
    5736          599 :                               bool changed = set_ssa_val_to (lhs, result);
    5737          599 :                               if (TREE_CODE (result) == SSA_NAME)
    5738          599 :                                 vn_nary_op_insert_stmt (stmt, result);
    5739          599 :                               return changed;
    5740              :                             }
    5741              :                         }
    5742              :                     }
    5743              :                 }
    5744              :             }
    5745              :         }
    5746              :       break;
    5747      1482811 :     case BIT_AND_EXPR:
    5748      1482811 :       if (INTEGRAL_TYPE_P (type)
    5749      1444651 :           && TREE_CODE (rhs1) == SSA_NAME
    5750      1444651 :           && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
    5751       893267 :           && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
    5752       893149 :           && default_vn_walk_kind != VN_NOWALK
    5753              :           && CHAR_BIT == 8
    5754              :           && BITS_PER_UNIT == 8
    5755              :           && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
    5756       892940 :           && TYPE_PRECISION (type) <= vn_walk_cb_data::bufsize * BITS_PER_UNIT
    5757       892938 :           && !integer_all_onesp (gimple_assign_rhs2 (stmt))
    5758      2375749 :           && !integer_zerop (gimple_assign_rhs2 (stmt)))
    5759              :         {
    5760       892938 :           gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
    5761       656412 :           if (ass
    5762       656412 :               && !gimple_has_volatile_ops (ass)
    5763       654950 :               && vn_get_stmt_kind (ass) == VN_REFERENCE)
    5764              :             {
    5765       301588 :               tree last_vuse = gimple_vuse (ass);
    5766       301588 :               tree op = gimple_assign_rhs1 (ass);
    5767       904764 :               tree result = vn_reference_lookup (op, gimple_vuse (ass),
    5768              :                                                  default_vn_walk_kind,
    5769              :                                                  NULL, true, &last_vuse,
    5770              :                                                  gimple_assign_rhs2 (stmt));
    5771       301588 :               if (result
    5772       302037 :                   && useless_type_conversion_p (TREE_TYPE (result),
    5773          449 :                                                 TREE_TYPE (op)))
    5774          449 :                 return set_ssa_val_to (lhs, result);
    5775              :             }
    5776              :         }
    5777              :       break;
    5778       245386 :     case BIT_FIELD_REF:
    5779       245386 :       if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
    5780              :         {
    5781       245358 :           tree op0 = vn_valueize (TREE_OPERAND (rhs1, 0));
    5782       245358 :           gassign *ass;
    5783       245358 :           if (TREE_CODE (op0) == SSA_NAME
    5784       245358 :               && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
    5785       202767 :               && !gimple_has_volatile_ops (ass)
    5786       448042 :               && vn_get_stmt_kind (ass) == VN_REFERENCE)
    5787              :             {
    5788        89882 :               tree last_vuse = gimple_vuse (ass);
    5789        89882 :               tree op = gimple_assign_rhs1 (ass);
    5790              :               /* Avoid building invalid and unexpected refs.  */
    5791        89882 :               if (TREE_CODE (op) != TARGET_MEM_REF
    5792              :                   && TREE_CODE (op) != BIT_FIELD_REF
    5793              :                   && TREE_CODE (op) != REALPART_EXPR
    5794              :                   && TREE_CODE (op) != IMAGPART_EXPR)
    5795              :                 {
    5796        82634 :                   tree op = build3 (BIT_FIELD_REF, TREE_TYPE (rhs1),
    5797              :                                     gimple_assign_rhs1 (ass),
    5798        82634 :                                     TREE_OPERAND (rhs1, 1),
    5799        82634 :                                     TREE_OPERAND (rhs1, 2));
    5800       165268 :                   tree result = vn_reference_lookup (op, gimple_vuse (ass),
    5801              :                                                      default_vn_walk_kind,
    5802              :                                                      NULL, true, &last_vuse);
    5803        82634 :                   if (result
    5804        82634 :                       && useless_type_conversion_p (type, TREE_TYPE (result)))
    5805         1176 :                     return set_ssa_val_to (lhs, result);
    5806        81755 :                   else if (result
    5807          297 :                            && TYPE_SIZE (type)
    5808          297 :                            && TYPE_SIZE (TREE_TYPE (result))
    5809        82052 :                            && operand_equal_p (TYPE_SIZE (type),
    5810          297 :                                                TYPE_SIZE (TREE_TYPE (result))))
    5811              :                     {
    5812          297 :                       gimple_match_op match_op (gimple_match_cond::UNCOND,
    5813          297 :                                                 VIEW_CONVERT_EXPR,
    5814          297 :                                                 type, result);
    5815          297 :                       result = vn_nary_build_or_lookup (&match_op);
    5816          297 :                       if (result)
    5817              :                         {
    5818          297 :                           bool changed = set_ssa_val_to (lhs, result);
    5819          297 :                           if (TREE_CODE (result) == SSA_NAME)
    5820          285 :                             vn_nary_op_insert_stmt (stmt, result);
    5821          297 :                           return changed;
    5822              :                         }
    5823              :                     }
    5824              :                 }
    5825              :             }
    5826              :         }
    5827              :       break;
    5828       321709 :     case TRUNC_DIV_EXPR:
    5829       321709 :       if (TYPE_UNSIGNED (type))
    5830              :         break;
    5831              :       /* Fallthru.  */
    5832      5444359 :     case RDIV_EXPR:
    5833      5444359 :     case MULT_EXPR:
    5834              :       /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v.  */
    5835      5444359 :       if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
    5836              :         {
    5837      5443449 :           tree rhs[2];
    5838      5443449 :           rhs[0] = rhs1;
    5839      5443449 :           rhs[1] = gimple_assign_rhs2 (stmt);
    5840     16323712 :           for (unsigned i = 0; i <= 1; ++i)
    5841              :             {
    5842     10885748 :               unsigned j = i == 0 ? 1 : 0;
    5843     10885748 :               tree ops[2];
    5844     10885748 :               gimple_match_op match_op (gimple_match_cond::UNCOND,
    5845     10885748 :                                         NEGATE_EXPR, type, rhs[i]);
    5846     10885748 :               ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
    5847     10885748 :               ops[j] = rhs[j];
    5848     10885748 :               if (ops[i]
    5849     10885748 :                   && (ops[0] = vn_nary_op_lookup_pieces (2, code,
    5850              :                                                          type, ops, NULL)))
    5851              :                 {
    5852         5485 :                   gimple_match_op match_op (gimple_match_cond::UNCOND,
    5853         5485 :                                             NEGATE_EXPR, type, ops[0]);
    5854         5485 :                   result = vn_nary_build_or_lookup_1 (&match_op, true, false);
    5855         5485 :                   if (result)
    5856              :                     {
    5857         5485 :                       bool changed = set_ssa_val_to (lhs, result);
    5858         5485 :                       if (TREE_CODE (result) == SSA_NAME)
    5859         5485 :                         vn_nary_op_insert_stmt (stmt, result);
    5860         5485 :                       return changed;
    5861              :                     }
    5862              :                 }
    5863              :             }
    5864              :         }
    5865              :       break;
    5866       365370 :     case LSHIFT_EXPR:
    5867              :       /* For X << C, use the value number of X * (1 << C).  */
    5868       365370 :       if (INTEGRAL_TYPE_P (type)
    5869       350375 :           && TYPE_OVERFLOW_WRAPS (type)
    5870       552647 :           && !TYPE_SATURATING (type))
    5871              :         {
    5872       187277 :           tree rhs2 = gimple_assign_rhs2 (stmt);
    5873       187277 :           if (TREE_CODE (rhs2) == INTEGER_CST
    5874       108458 :               && tree_fits_uhwi_p (rhs2)
    5875       295735 :               && tree_to_uhwi (rhs2) < TYPE_PRECISION (type))
    5876              :             {
    5877       108458 :               wide_int w = wi::set_bit_in_zero (tree_to_uhwi (rhs2),
    5878       108458 :                                                 TYPE_PRECISION (type));
    5879       216916 :               gimple_match_op match_op (gimple_match_cond::UNCOND,
    5880       108458 :                                         MULT_EXPR, type, rhs1,
    5881       108458 :                                         wide_int_to_tree (type, w));
    5882       108458 :               result = vn_nary_build_or_lookup (&match_op);
    5883       108458 :               if (result)
    5884              :                 {
    5885       108458 :                   bool changed = set_ssa_val_to (lhs, result);
    5886       108458 :                   if (TREE_CODE (result) == SSA_NAME)
    5887       108457 :                     vn_nary_op_insert_stmt (stmt, result);
    5888       108458 :                   return changed;
    5889              :                 }
    5890       108458 :             }
    5891              :         }
    5892              :       break;
    5893              :     default:
    5894              :       break;
    5895              :     }
    5896              : 
    5897     44641750 :   bool changed = set_ssa_val_to (lhs, lhs);
    5898     44641750 :   vn_nary_op_insert_stmt (stmt, lhs);
    5899     44641750 :   return changed;
    5900              : }
    5901              : 
    5902              : /* Visit a call STMT storing into LHS.  Return true if the value number
    5903              :    of the LHS has changed as a result.  */
    5904              : 
    5905              : static bool
    5906      8515454 : visit_reference_op_call (tree lhs, gcall *stmt)
    5907              : {
    5908      8515454 :   bool changed = false;
    5909      8515454 :   struct vn_reference_s vr1;
    5910      8515454 :   vn_reference_t vnresult = NULL;
    5911      8515454 :   tree vdef = gimple_vdef (stmt);
    5912      8515454 :   modref_summary *summary;
    5913              : 
    5914              :   /* Non-ssa lhs is handled in copy_reference_ops_from_call.  */
    5915      8515454 :   if (lhs && TREE_CODE (lhs) != SSA_NAME)
    5916      4502311 :     lhs = NULL_TREE;
    5917              : 
    5918      8515454 :   vn_reference_lookup_call (stmt, &vnresult, &vr1);
    5919              : 
    5920              :   /* If the lookup did not succeed for pure functions try to use
    5921              :      modref info to find a candidate to CSE to.  */
    5922      8515454 :   const unsigned accesses_limit = 8;
    5923      8515454 :   if (!vnresult
    5924      7888226 :       && !vdef
    5925      7888226 :       && lhs
    5926      2793941 :       && gimple_vuse (stmt)
    5927     10049339 :       && (((summary = get_modref_function_summary (stmt, NULL))
    5928       203860 :            && !summary->global_memory_read
    5929        77554 :            && summary->load_accesses < accesses_limit)
    5930      1456675 :           || gimple_call_flags (stmt) & ECF_CONST))
    5931              :     {
    5932              :       /* First search if we can do something useful and build a
    5933              :          vector of all loads we have to check.  */
    5934        77943 :       bool unknown_memory_access = false;
    5935        77943 :       auto_vec<ao_ref, accesses_limit> accesses;
    5936        77943 :       unsigned load_accesses = summary ? summary->load_accesses : 0;
    5937        77943 :       if (!unknown_memory_access)
    5938              :         /* Add loads done as part of setting up the call arguments.
    5939              :            That's also necessary for CONST functions which will
    5940              :            not have a modref summary.  */
    5941       229471 :         for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
    5942              :           {
    5943       151536 :             tree arg = gimple_call_arg (stmt, i);
    5944       151536 :             if (TREE_CODE (arg) != SSA_NAME
    5945       151536 :                 && !is_gimple_min_invariant (arg))
    5946              :               {
    5947        61300 :                 if (accesses.length () >= accesses_limit - load_accesses)
    5948              :                   {
    5949              :                     unknown_memory_access = true;
    5950              :                     break;
    5951              :                   }
    5952        30642 :                 accesses.quick_grow (accesses.length () + 1);
    5953        30642 :                 ao_ref_init (&accesses.last (), arg);
    5954              :               }
    5955              :           }
    5956        77943 :       if (summary && !unknown_memory_access)
    5957              :         {
    5958              :           /* Add loads as analyzed by IPA modref.  */
    5959       274143 :           for (auto base_node : summary->loads->bases)
    5960        69445 :             if (unknown_memory_access)
    5961              :               break;
    5962       283066 :             else for (auto ref_node : base_node->refs)
    5963        75583 :               if (unknown_memory_access)
    5964              :                 break;
    5965       313770 :               else for (auto access_node : ref_node->accesses)
    5966              :                 {
    5967       206554 :                   accesses.quick_grow (accesses.length () + 1);
    5968       103277 :                   ao_ref *r = &accesses.last ();
    5969       103277 :                   if (!access_node.get_ao_ref (stmt, r))
    5970              :                     {
    5971              :                       /* Initialize a ref based on the argument and
    5972              :                          unknown offset if possible.  */
    5973        16220 :                       tree arg = access_node.get_call_arg (stmt);
    5974        16220 :                       if (arg && TREE_CODE (arg) == SSA_NAME)
    5975         2754 :                         arg = SSA_VAL (arg);
    5976         2754 :                       if (arg
    5977        16210 :                           && TREE_CODE (arg) == ADDR_EXPR
    5978        13462 :                           && (arg = get_base_address (arg))
    5979        16216 :                           && DECL_P (arg))
    5980              :                         {
    5981            0 :                           ao_ref_init (r, arg);
    5982            0 :                           r->ref = NULL_TREE;
    5983            0 :                           r->base = arg;
    5984              :                         }
    5985              :                       else
    5986              :                         {
    5987              :                           unknown_memory_access = true;
    5988              :                           break;
    5989              :                         }
    5990              :                     }
    5991        87057 :                   r->base_alias_set = base_node->base;
    5992        87057 :                   r->ref_alias_set = ref_node->ref;
    5993              :                 }
    5994              :         }
    5995              : 
    5996              :       /* Walk the VUSE->VDEF chain optimistically trying to find an entry
    5997              :          for the call in the hashtable.  */
    5998        77943 :       unsigned limit = (unknown_memory_access
    5999        77943 :                         ? 0
    6000        61715 :                         : (param_sccvn_max_alias_queries_per_access
    6001        61715 :                            / (accesses.length () + 1)));
    6002        77943 :       tree saved_vuse = vr1.vuse;
    6003        77943 :       hashval_t saved_hashcode = vr1.hashcode;
    6004       414259 :       while (limit > 0 && !vnresult && !SSA_NAME_IS_DEFAULT_DEF (vr1.vuse))
    6005              :         {
    6006       358447 :           vr1.hashcode = vr1.hashcode - SSA_NAME_VERSION (vr1.vuse);
    6007       358447 :           gimple *def = SSA_NAME_DEF_STMT (vr1.vuse);
    6008              :           /* ???  We could use fancy stuff like in walk_non_aliased_vuses, but
    6009              :              do not bother for now.  */
    6010       358447 :           if (is_a <gphi *> (def))
    6011              :             break;
    6012       672632 :           vr1.vuse = vuse_ssa_val (gimple_vuse (def));
    6013       336316 :           vr1.hashcode = vr1.hashcode + SSA_NAME_VERSION (vr1.vuse);
    6014       336316 :           vn_reference_lookup_1 (&vr1, &vnresult);
    6015       336316 :           limit--;
    6016              :         }
    6017              : 
    6018              :       /* If we found a candidate to CSE to verify it is valid.  */
    6019        77943 :       if (vnresult && !accesses.is_empty ())
    6020              :         {
    6021         1919 :           tree vuse = vuse_ssa_val (gimple_vuse (stmt));
    6022         7134 :           while (vnresult && vuse != vr1.vuse)
    6023              :             {
    6024         3296 :               gimple *def = SSA_NAME_DEF_STMT (vuse);
    6025        17311 :               for (auto &ref : accesses)
    6026              :                 {
    6027              :                   /* ???  stmt_may_clobber_ref_p_1 does per stmt constant
    6028              :                      analysis overhead that we might be able to cache.  */
    6029         9164 :                   if (stmt_may_clobber_ref_p_1 (def, &ref, true))
    6030              :                     {
    6031         1741 :                       vnresult = NULL;
    6032         1741 :                       break;
    6033              :                     }
    6034              :                 }
    6035         6592 :               vuse = vuse_ssa_val (gimple_vuse (def));
    6036              :             }
    6037              :         }
    6038        77943 :       vr1.vuse = saved_vuse;
    6039        77943 :       vr1.hashcode = saved_hashcode;
    6040        77943 :     }
    6041              : 
    6042      8515454 :   if (vnresult)
    6043              :     {
    6044       627434 :       if (vdef)
    6045              :         {
    6046       172368 :           if (vnresult->result_vdef)
    6047       172368 :             changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
    6048            0 :           else if (!lhs && gimple_call_lhs (stmt))
    6049              :             /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
    6050              :                as the call still acts as a lhs store.  */
    6051            0 :             changed |= set_ssa_val_to (vdef, vdef);
    6052              :           else
    6053              :             /* If the call was discovered to be pure or const reflect
    6054              :                that as far as possible.  */
    6055            0 :             changed |= set_ssa_val_to (vdef,
    6056              :                                        vuse_ssa_val (gimple_vuse (stmt)));
    6057              :         }
    6058              : 
    6059       627434 :       if (!vnresult->result && lhs)
    6060            0 :         vnresult->result = lhs;
    6061              : 
    6062       627434 :       if (vnresult->result && lhs)
    6063       107967 :         changed |= set_ssa_val_to (lhs, vnresult->result);
    6064              :     }
    6065              :   else
    6066              :     {
    6067      7888020 :       vn_reference_t vr2;
    6068      7888020 :       vn_reference_s **slot;
    6069      7888020 :       tree vdef_val = vdef;
    6070      7888020 :       if (vdef)
    6071              :         {
    6072              :           /* If we value numbered an indirect functions function to
    6073              :              one not clobbering memory value number its VDEF to its
    6074              :              VUSE.  */
    6075      4781287 :           tree fn = gimple_call_fn (stmt);
    6076      4781287 :           if (fn && TREE_CODE (fn) == SSA_NAME)
    6077              :             {
    6078       128372 :               fn = SSA_VAL (fn);
    6079       128372 :               if (TREE_CODE (fn) == ADDR_EXPR
    6080         1720 :                   && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
    6081         1720 :                   && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
    6082         1720 :                       & (ECF_CONST | ECF_PURE))
    6083              :                   /* If stmt has non-SSA_NAME lhs, value number the
    6084              :                      vdef to itself, as the call still acts as a lhs
    6085              :                      store.  */
    6086       129496 :                   && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
    6087         2116 :                 vdef_val = vuse_ssa_val (gimple_vuse (stmt));
    6088              :             }
    6089      4781287 :           changed |= set_ssa_val_to (vdef, vdef_val);
    6090              :         }
    6091      7888020 :       if (lhs)
    6092      3905176 :         changed |= set_ssa_val_to (lhs, lhs);
    6093      7888020 :       vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
    6094      7888020 :       vr2->vuse = vr1.vuse;
    6095              :       /* As we are not walking the virtual operand chain we know the
    6096              :          shared_lookup_references are still original so we can re-use
    6097              :          them here.  */
    6098      7888020 :       vr2->operands = vr1.operands.copy ();
    6099      7888020 :       vr2->type = vr1.type;
    6100      7888020 :       vr2->punned = vr1.punned;
    6101      7888020 :       vr2->set = vr1.set;
    6102      7888020 :       vr2->offset = vr1.offset;
    6103      7888020 :       vr2->max_size = vr1.max_size;
    6104      7888020 :       vr2->base_set = vr1.base_set;
    6105      7888020 :       vr2->hashcode = vr1.hashcode;
    6106      7888020 :       vr2->result = lhs;
    6107      7888020 :       vr2->result_vdef = vdef_val;
    6108      7888020 :       vr2->value_id = 0;
    6109      7888020 :       slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
    6110              :                                                           INSERT);
    6111      7888020 :       gcc_assert (!*slot);
    6112      7888020 :       *slot = vr2;
    6113      7888020 :       vr2->next = last_inserted_ref;
    6114      7888020 :       last_inserted_ref = vr2;
    6115              :     }
    6116              : 
    6117      8515454 :   return changed;
    6118              : }
    6119              : 
    6120              : /* Visit a load from a reference operator RHS, part of STMT, value number it,
    6121              :    and return true if the value number of the LHS has changed as a result.  */
    6122              : 
    6123              : static bool
    6124     33999603 : visit_reference_op_load (tree lhs, tree op, gimple *stmt)
    6125              : {
    6126     33999603 :   bool changed = false;
    6127     33999603 :   tree result;
    6128     33999603 :   vn_reference_t res;
    6129              : 
    6130     33999603 :   tree vuse = gimple_vuse (stmt);
    6131     33999603 :   tree last_vuse = vuse;
    6132     33999603 :   result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
    6133              : 
    6134              :   /* We handle type-punning through unions by value-numbering based
    6135              :      on offset and size of the access.  Be prepared to handle a
    6136              :      type-mismatch here via creating a VIEW_CONVERT_EXPR.  */
    6137     33999603 :   if (result
    6138     33999603 :       && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
    6139              :     {
    6140        18001 :       if (CONSTANT_CLASS_P (result))
    6141         4142 :         result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
    6142              :       else
    6143              :         {
    6144              :           /* We will be setting the value number of lhs to the value number
    6145              :              of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
    6146              :              So first simplify and lookup this expression to see if it
    6147              :              is already available.  */
    6148        13859 :           gimple_match_op res_op (gimple_match_cond::UNCOND,
    6149        13859 :                                   VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
    6150        13859 :           result = vn_nary_build_or_lookup (&res_op);
    6151        13859 :           if (result
    6152        13853 :               && TREE_CODE (result) == SSA_NAME
    6153        26026 :               && VN_INFO (result)->needs_insertion)
    6154              :             /* Track whether this is the canonical expression for different
    6155              :                typed loads.  We use that as a stopgap measure for code
    6156              :                hoisting when dealing with floating point loads.  */
    6157        10925 :             res->punned = true;
    6158              :         }
    6159              : 
    6160              :       /* When building the conversion fails avoid inserting the reference
    6161              :          again.  */
    6162        18001 :       if (!result)
    6163            6 :         return set_ssa_val_to (lhs, lhs);
    6164              :     }
    6165              : 
    6166     33981602 :   if (result)
    6167      5447679 :     changed = set_ssa_val_to (lhs, result);
    6168              :   else
    6169              :     {
    6170     28551918 :       changed = set_ssa_val_to (lhs, lhs);
    6171     28551918 :       vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
    6172     28551918 :       if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
    6173              :         {
    6174      8777775 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6175              :             {
    6176        23137 :               fprintf (dump_file, "Using extra use virtual operand ");
    6177        23137 :               print_generic_expr (dump_file, last_vuse);
    6178        23137 :               fprintf (dump_file, "\n");
    6179              :             }
    6180      8777775 :           vn_reference_insert (op, lhs, vuse, NULL_TREE);
    6181              :         }
    6182              :     }
    6183              : 
    6184              :   return changed;
    6185              : }
    6186              : 
    6187              : 
    6188              : /* Visit a store to a reference operator LHS, part of STMT, value number it,
    6189              :    and return true if the value number of the LHS has changed as a result.  */
    6190              : 
    6191              : static bool
    6192     32649137 : visit_reference_op_store (tree lhs, tree op, gimple *stmt)
    6193              : {
    6194     32649137 :   bool changed = false;
    6195     32649137 :   vn_reference_t vnresult = NULL;
    6196     32649137 :   tree assign;
    6197     32649137 :   bool resultsame = false;
    6198     32649137 :   tree vuse = gimple_vuse (stmt);
    6199     32649137 :   tree vdef = gimple_vdef (stmt);
    6200              : 
    6201     32649137 :   if (TREE_CODE (op) == SSA_NAME)
    6202     14832163 :     op = SSA_VAL (op);
    6203              : 
    6204              :   /* First we want to lookup using the *vuses* from the store and see
    6205              :      if there the last store to this location with the same address
    6206              :      had the same value.
    6207              : 
    6208              :      The vuses represent the memory state before the store.  If the
    6209              :      memory state, address, and value of the store is the same as the
    6210              :      last store to this location, then this store will produce the
    6211              :      same memory state as that store.
    6212              : 
    6213              :      In this case the vdef versions for this store are value numbered to those
    6214              :      vuse versions, since they represent the same memory state after
    6215              :      this store.
    6216              : 
    6217              :      Otherwise, the vdefs for the store are used when inserting into
    6218              :      the table, since the store generates a new memory state.  */
    6219              : 
    6220     32649137 :   vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
    6221     32649137 :   if (vnresult
    6222      1665246 :       && vnresult->result)
    6223              :     {
    6224      1665246 :       tree result = vnresult->result;
    6225      1665246 :       gcc_checking_assert (TREE_CODE (result) != SSA_NAME
    6226              :                            || result == SSA_VAL (result));
    6227      1665246 :       resultsame = expressions_equal_p (result, op);
    6228      1665246 :       if (resultsame)
    6229              :         {
    6230              :           /* If the TBAA state isn't compatible for downstream reads
    6231              :              we cannot value-number the VDEFs the same.  */
    6232        51813 :           ao_ref lhs_ref;
    6233        51813 :           ao_ref_init (&lhs_ref, lhs);
    6234        51813 :           alias_set_type set = ao_ref_alias_set (&lhs_ref);
    6235        51813 :           alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
    6236        51813 :           if ((vnresult->set != set
    6237          940 :                && ! alias_set_subset_of (set, vnresult->set))
    6238        52413 :               || (vnresult->base_set != base_set
    6239         6350 :                   && ! alias_set_subset_of (base_set, vnresult->base_set)))
    6240          868 :             resultsame = false;
    6241              :         }
    6242              :     }
    6243              : 
    6244          868 :   if (!resultsame)
    6245              :     {
    6246     32598192 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6247              :         {
    6248        20449 :           fprintf (dump_file, "No store match\n");
    6249        20449 :           fprintf (dump_file, "Value numbering store ");
    6250        20449 :           print_generic_expr (dump_file, lhs);
    6251        20449 :           fprintf (dump_file, " to ");
    6252        20449 :           print_generic_expr (dump_file, op);
    6253        20449 :           fprintf (dump_file, "\n");
    6254              :         }
    6255              :       /* Have to set value numbers before insert, since insert is
    6256              :          going to valueize the references in-place.  */
    6257     32598192 :       if (vdef)
    6258     32598192 :         changed |= set_ssa_val_to (vdef, vdef);
    6259              : 
    6260              :       /* Do not insert structure copies into the tables.  */
    6261     32598192 :       if (is_gimple_min_invariant (op)
    6262     32598192 :           || is_gimple_reg (op))
    6263     29025398 :         vn_reference_insert (lhs, op, vdef, NULL);
    6264              : 
    6265              :       /* Only perform the following when being called from PRE
    6266              :          which embeds tail merging.  */
    6267     32598192 :       if (default_vn_walk_kind == VN_WALK)
    6268              :         {
    6269      7420661 :           assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
    6270      7420661 :           vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
    6271      7420661 :           if (!vnresult)
    6272      7380648 :             vn_reference_insert (assign, lhs, vuse, vdef);
    6273              :         }
    6274              :     }
    6275              :   else
    6276              :     {
    6277              :       /* We had a match, so value number the vdef to have the value
    6278              :          number of the vuse it came from.  */
    6279              : 
    6280        50945 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6281            9 :         fprintf (dump_file, "Store matched earlier value, "
    6282              :                  "value numbering store vdefs to matching vuses.\n");
    6283              : 
    6284        50945 :       changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
    6285              :     }
    6286              : 
    6287     32649137 :   return changed;
    6288              : }
    6289              : 
    6290              : /* Visit and value number PHI, return true if the value number
    6291              :    changed.  When BACKEDGES_VARYING_P is true then assume all
    6292              :    backedge values are varying.  When INSERTED is not NULL then
    6293              :    this is just a ahead query for a possible iteration, set INSERTED
    6294              :    to true if we'd insert into the hashtable.  */
    6295              : 
    6296              : static bool
    6297     34025407 : visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
    6298              : {
    6299     34025407 :   tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
    6300     34025407 :   bool seen_undef_visited = false;
    6301     34025407 :   tree backedge_val = NULL_TREE;
    6302     34025407 :   bool seen_non_backedge = false;
    6303     34025407 :   tree sameval_base = NULL_TREE;
    6304     34025407 :   poly_int64 soff, doff;
    6305     34025407 :   unsigned n_executable = 0;
    6306     34025407 :   edge sameval_e = NULL;
    6307              : 
    6308              :   /* TODO: We could check for this in initialization, and replace this
    6309              :      with a gcc_assert.  */
    6310     34025407 :   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
    6311        31043 :     return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
    6312              : 
    6313              :   /* We track whether a PHI was CSEd to avoid excessive iterations
    6314              :      that would be necessary only because the PHI changed arguments
    6315              :      but not value.  */
    6316     33994364 :   if (!inserted)
    6317     26475455 :     gimple_set_plf (phi, GF_PLF_1, false);
    6318              : 
    6319     33994364 :   basic_block bb = gimple_bb (phi);
    6320              : 
    6321              :   /* For the equivalence handling below make sure to first process an
    6322              :      edge with a non-constant.  */
    6323     33994364 :   auto_vec<edge, 2> preds;
    6324     67988728 :   preds.reserve_exact (EDGE_COUNT (bb->preds));
    6325     33994364 :   bool seen_nonconstant = false;
    6326    112181035 :   for (unsigned i = 0; i < EDGE_COUNT (bb->preds); ++i)
    6327              :     {
    6328     78186671 :       edge e = EDGE_PRED (bb, i);
    6329     78186671 :       preds.quick_push (e);
    6330     78186671 :       if (!seen_nonconstant)
    6331              :         {
    6332     41648897 :           tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
    6333     41648897 :           if (TREE_CODE (def) == SSA_NAME)
    6334              :             {
    6335     32302751 :               seen_nonconstant = true;
    6336     32302751 :               if (i != 0)
    6337      5712143 :                 std::swap (preds[0], preds[i]);
    6338              :             }
    6339              :         }
    6340              :     }
    6341              : 
    6342              :   /* See if all non-TOP arguments have the same value.  TOP is
    6343              :      equivalent to everything, so we can ignore it.  */
    6344    143106810 :   for (edge e : preds)
    6345     67561426 :     if (e->flags & EDGE_EXECUTABLE)
    6346              :       {
    6347     62562685 :         tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
    6348              : 
    6349     62562685 :         if (def == PHI_RESULT (phi))
    6350       328162 :           continue;
    6351     62259128 :         ++n_executable;
    6352     62259128 :         bool visited = true;
    6353     62259128 :         if (TREE_CODE (def) == SSA_NAME)
    6354              :           {
    6355     50206134 :             tree val = SSA_VAL (def, &visited);
    6356     50206134 :             if (SSA_NAME_IS_DEFAULT_DEF (def))
    6357      2653277 :               visited = true;
    6358     50206134 :             if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
    6359     47675942 :               def = val;
    6360     50206134 :             if (e->flags & EDGE_DFS_BACK)
    6361     15333294 :               backedge_val = def;
    6362              :           }
    6363     62259128 :         if (!(e->flags & EDGE_DFS_BACK))
    6364     46759010 :           seen_non_backedge = true;
    6365     62259128 :         if (def == VN_TOP)
    6366              :           ;
    6367              :         /* Ignore undefined defs for sameval but record one.  */
    6368     62259128 :         else if (TREE_CODE (def) == SSA_NAME
    6369     46821173 :                  && ! virtual_operand_p (def)
    6370     86167033 :                  && ssa_undefined_value_p (def, false))
    6371              :           {
    6372       233436 :             if (!seen_undef
    6373              :                 /* Avoid having not visited undefined defs if we also have
    6374              :                    a visited one.  */
    6375        35060 :                 || (!seen_undef_visited && visited))
    6376              :               {
    6377       198379 :                 seen_undef = def;
    6378       198379 :                 seen_undef_visited = visited;
    6379              :               }
    6380              :           }
    6381     62025692 :         else if (sameval == VN_TOP)
    6382              :           {
    6383              :             sameval = def;
    6384              :             sameval_e = e;
    6385              :           }
    6386     28078521 :         else if (expressions_equal_p (def, sameval))
    6387              :           sameval_e = NULL;
    6388     44173454 :         else if (virtual_operand_p (def))
    6389              :           {
    6390              :             sameval = NULL_TREE;
    6391     26437708 :             break;
    6392              :           }
    6393              :         else
    6394              :           {
    6395              :             /* We know we're arriving only with invariant addresses here,
    6396              :                try harder comparing them.  We can do some caching here
    6397              :                which we cannot do in expressions_equal_p.  */
    6398     16564593 :             if (TREE_CODE (def) == ADDR_EXPR
    6399       372024 :                 && TREE_CODE (sameval) == ADDR_EXPR
    6400       103274 :                 && sameval_base != (void *)-1)
    6401              :               {
    6402       103274 :                 if (!sameval_base)
    6403       103272 :                   sameval_base = get_addr_base_and_unit_offset
    6404       103272 :                                    (TREE_OPERAND (sameval, 0), &soff);
    6405       103272 :                 if (!sameval_base)
    6406              :                   sameval_base = (tree)(void *)-1;
    6407       103279 :                 else if ((get_addr_base_and_unit_offset
    6408       103274 :                             (TREE_OPERAND (def, 0), &doff) == sameval_base)
    6409       103274 :                          && known_eq (soff, doff))
    6410            5 :                   continue;
    6411              :               }
    6412              :             /* There's also the possibility to use equivalences.  */
    6413     32041033 :             if (!FLOAT_TYPE_P (TREE_TYPE (def))
    6414              :                 /* But only do this if we didn't force any of sameval or
    6415              :                    val to VARYING because of backedge processing rules.  */
    6416     15371933 :                 && (TREE_CODE (sameval) != SSA_NAME
    6417     12112787 :                     || SSA_VAL (sameval) == sameval)
    6418     31936458 :                 && (TREE_CODE (def) != SSA_NAME || SSA_VAL (def) == def))
    6419              :               {
    6420     15371856 :                 vn_nary_op_t vnresult;
    6421     15371856 :                 tree ops[2];
    6422     15371856 :                 ops[0] = def;
    6423     15371856 :                 ops[1] = sameval;
    6424              :                 /* Canonicalize the operands order for eq below. */
    6425     15371856 :                 if (tree_swap_operands_p (ops[0], ops[1]))
    6426      9260692 :                   std::swap (ops[0], ops[1]);
    6427     15371856 :                 tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR,
    6428              :                                                      boolean_type_node,
    6429              :                                                      ops, &vnresult);
    6430     15371856 :                 if (! val && vnresult && vnresult->predicated_values)
    6431              :                   {
    6432       212560 :                     val = vn_nary_op_get_predicated_value (vnresult, e);
    6433       119628 :                     if (val && integer_truep (val)
    6434       237282 :                         && !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
    6435              :                       {
    6436        24600 :                         if (dump_file && (dump_flags & TDF_DETAILS))
    6437              :                           {
    6438            2 :                             fprintf (dump_file, "Predication says ");
    6439            2 :                             print_generic_expr (dump_file, def, TDF_NONE);
    6440            2 :                             fprintf (dump_file, " and ");
    6441            2 :                             print_generic_expr (dump_file, sameval, TDF_NONE);
    6442            2 :                             fprintf (dump_file, " are equal on edge %d -> %d\n",
    6443            2 :                                      e->src->index, e->dest->index);
    6444              :                           }
    6445        24600 :                         continue;
    6446              :                       }
    6447              :                   }
    6448              :               }
    6449              :             sameval = NULL_TREE;
    6450              :             break;
    6451              :           }
    6452              :       }
    6453              : 
    6454              :   /* If the value we want to use is flowing over the backedge and we
    6455              :      should take it as VARYING but it has a non-VARYING value drop to
    6456              :      VARYING.
    6457              :      If we value-number a virtual operand never value-number to the
    6458              :      value from the backedge as that confuses the alias-walking code.
    6459              :      See gcc.dg/torture/pr87176.c.  If the value is the same on a
    6460              :      non-backedge everything is OK though.  */
    6461     33994364 :   bool visited_p;
    6462     33994364 :   if ((backedge_val
    6463     33994364 :        && !seen_non_backedge
    6464         2009 :        && TREE_CODE (backedge_val) == SSA_NAME
    6465         1742 :        && sameval == backedge_val
    6466          311 :        && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
    6467           40 :            || SSA_VAL (backedge_val) != backedge_val))
    6468              :       /* Do not value-number a virtual operand to sth not visited though
    6469              :          given that allows us to escape a region in alias walking.  */
    6470     33996102 :       || (sameval
    6471      7556385 :           && TREE_CODE (sameval) == SSA_NAME
    6472      4466246 :           && !SSA_NAME_IS_DEFAULT_DEF (sameval)
    6473      3766439 :           && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
    6474      1882084 :           && (SSA_VAL (sameval, &visited_p), !visited_p)))
    6475              :     /* Note this just drops to VARYING without inserting the PHI into
    6476              :        the hashes.  */
    6477       294186 :     result = PHI_RESULT (phi);
    6478              :   /* If none of the edges was executable keep the value-number at VN_TOP,
    6479              :      if only a single edge is executable use its value.  */
    6480     33700178 :   else if (n_executable <= 1)
    6481      6510305 :     result = seen_undef ? seen_undef : sameval;
    6482              :   /* If we saw only undefined values and VN_TOP use one of the
    6483              :      undefined values.  */
    6484     27189873 :   else if (sameval == VN_TOP)
    6485      7079726 :     result = (seen_undef && seen_undef_visited) ? seen_undef : sameval;
    6486              :   /* First see if it is equivalent to a phi node in this block.  We prefer
    6487              :      this as it allows IV elimination - see PRs 66502 and 67167.  */
    6488     27184360 :   else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
    6489              :     {
    6490      4169532 :       if (!inserted
    6491        69625 :           && TREE_CODE (result) == SSA_NAME
    6492      4239157 :           && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
    6493              :         {
    6494        69625 :           gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
    6495        69625 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6496              :             {
    6497            6 :               fprintf (dump_file, "Marking CSEd to PHI node ");
    6498            6 :               print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
    6499              :                                  0, TDF_SLIM);
    6500            6 :               fprintf (dump_file, "\n");
    6501              :             }
    6502              :         }
    6503              :     }
    6504              :   /* If all values are the same use that, unless we've seen undefined
    6505              :      values as well and the value isn't constant.
    6506              :      CCP/copyprop have the same restriction to not remove uninit warnings.  */
    6507     23014828 :   else if (sameval
    6508     23014828 :            && (! seen_undef || is_gimple_min_invariant (sameval)))
    6509              :     result = sameval;
    6510              :   else
    6511              :     {
    6512     22409240 :       result = PHI_RESULT (phi);
    6513              :       /* Only insert PHIs that are varying, for constant value numbers
    6514              :          we mess up equivalences otherwise as we are only comparing
    6515              :          the immediate controlling predicates.  */
    6516     22409240 :       vn_phi_insert (phi, result, backedges_varying_p);
    6517     22409240 :       if (inserted)
    6518      3265380 :         *inserted = true;
    6519              :     }
    6520              : 
    6521     33994364 :   return set_ssa_val_to (PHI_RESULT (phi), result);
    6522     33994364 : }
    6523              : 
    6524              : /* Try to simplify RHS using equivalences and constant folding.  */
    6525              : 
    6526              : static tree
    6527    125701843 : try_to_simplify (gassign *stmt)
    6528              : {
    6529    125701843 :   enum tree_code code = gimple_assign_rhs_code (stmt);
    6530    125701843 :   tree tem;
    6531              : 
    6532              :   /* For stores we can end up simplifying a SSA_NAME rhs.  Just return
    6533              :      in this case, there is no point in doing extra work.  */
    6534    125701843 :   if (code == SSA_NAME)
    6535              :     return NULL_TREE;
    6536              : 
    6537              :   /* First try constant folding based on our current lattice.  */
    6538    110869385 :   mprts_hook = vn_lookup_simplify_result;
    6539    110869385 :   tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
    6540    110869385 :   mprts_hook = NULL;
    6541    110869385 :   if (tem
    6542    110869385 :       && (TREE_CODE (tem) == SSA_NAME
    6543     24617026 :           || is_gimple_min_invariant (tem)))
    6544     24751020 :     return tem;
    6545              : 
    6546              :   return NULL_TREE;
    6547              : }
    6548              : 
    6549              : /* Visit and value number STMT, return true if the value number
    6550              :    changed.  */
    6551              : 
    6552              : static bool
    6553    446592107 : visit_stmt (gimple *stmt, bool backedges_varying_p = false)
    6554              : {
    6555    446592107 :   bool changed = false;
    6556              : 
    6557    446592107 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6558              :     {
    6559       411012 :       fprintf (dump_file, "Value numbering stmt = ");
    6560       411012 :       print_gimple_stmt (dump_file, stmt, 0);
    6561              :     }
    6562              : 
    6563    446592107 :   if (gimple_code (stmt) == GIMPLE_PHI)
    6564     26496719 :     changed = visit_phi (stmt, NULL, backedges_varying_p);
    6565    589541576 :   else if (gimple_has_volatile_ops (stmt))
    6566      8636831 :     changed = defs_to_varying (stmt);
    6567    411458557 :   else if (gassign *ass = dyn_cast <gassign *> (stmt))
    6568              :     {
    6569    130687237 :       enum tree_code code = gimple_assign_rhs_code (ass);
    6570    130687237 :       tree lhs = gimple_assign_lhs (ass);
    6571    130687237 :       tree rhs1 = gimple_assign_rhs1 (ass);
    6572    130687237 :       tree simplified;
    6573              : 
    6574              :       /* Shortcut for copies. Simplifying copies is pointless,
    6575              :          since we copy the expression and value they represent.  */
    6576    130687237 :       if (code == SSA_NAME
    6577     19817852 :           && TREE_CODE (lhs) == SSA_NAME)
    6578              :         {
    6579      4985394 :           changed = visit_copy (lhs, rhs1);
    6580      4985394 :           goto done;
    6581              :         }
    6582    125701843 :       simplified = try_to_simplify (ass);
    6583    125701843 :       if (simplified)
    6584              :         {
    6585     24751020 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6586              :             {
    6587        14306 :               fprintf (dump_file, "RHS ");
    6588        14306 :               print_gimple_expr (dump_file, ass, 0);
    6589        14306 :               fprintf (dump_file, " simplified to ");
    6590        14306 :               print_generic_expr (dump_file, simplified);
    6591        14306 :               fprintf (dump_file, "\n");
    6592              :             }
    6593              :         }
    6594              :       /* Setting value numbers to constants will occasionally
    6595              :          screw up phi congruence because constants are not
    6596              :          uniquely associated with a single ssa name that can be
    6597              :          looked up.  */
    6598     24751020 :       if (simplified
    6599     24751020 :           && is_gimple_min_invariant (simplified)
    6600     21794801 :           && TREE_CODE (lhs) == SSA_NAME)
    6601              :         {
    6602      7545158 :           changed = set_ssa_val_to (lhs, simplified);
    6603      7545158 :           goto done;
    6604              :         }
    6605    118156685 :       else if (simplified
    6606     17205862 :                && TREE_CODE (simplified) == SSA_NAME
    6607      2956219 :                && TREE_CODE (lhs) == SSA_NAME)
    6608              :         {
    6609      2956219 :           changed = visit_copy (lhs, simplified);
    6610      2956219 :           goto done;
    6611              :         }
    6612              : 
    6613    115200466 :       if ((TREE_CODE (lhs) == SSA_NAME
    6614              :            /* We can substitute SSA_NAMEs that are live over
    6615              :               abnormal edges with their constant value.  */
    6616     82551060 :            && !(gimple_assign_copy_p (ass)
    6617           26 :                 && is_gimple_min_invariant (rhs1))
    6618     82551034 :            && !(simplified
    6619            0 :                 && is_gimple_min_invariant (simplified))
    6620     82551034 :            && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
    6621              :           /* Stores or copies from SSA_NAMEs that are live over
    6622              :              abnormal edges are a problem.  */
    6623    197750197 :           || (code == SSA_NAME
    6624     14832458 :               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
    6625         1598 :         changed = defs_to_varying (ass);
    6626    115198868 :       else if (REFERENCE_CLASS_P (lhs)
    6627    115198868 :                || DECL_P (lhs))
    6628     32649137 :         changed = visit_reference_op_store (lhs, rhs1, ass);
    6629     82549731 :       else if (TREE_CODE (lhs) == SSA_NAME)
    6630              :         {
    6631     82549731 :           if ((gimple_assign_copy_p (ass)
    6632           26 :                && is_gimple_min_invariant (rhs1))
    6633     82549757 :               || (simplified
    6634            0 :                   && is_gimple_min_invariant (simplified)))
    6635              :             {
    6636            0 :               if (simplified)
    6637            0 :                 changed = set_ssa_val_to (lhs, simplified);
    6638              :               else
    6639            0 :                 changed = set_ssa_val_to (lhs, rhs1);
    6640              :             }
    6641              :           else
    6642              :             {
    6643              :               /* Visit the original statement.  */
    6644     82549731 :               switch (vn_get_stmt_kind (ass))
    6645              :                 {
    6646     48453440 :                 case VN_NARY:
    6647     48453440 :                   changed = visit_nary_op (lhs, ass);
    6648     48453440 :                   break;
    6649     33999603 :                 case VN_REFERENCE:
    6650     33999603 :                   changed = visit_reference_op_load (lhs, rhs1, ass);
    6651     33999603 :                   break;
    6652        96688 :                 default:
    6653        96688 :                   changed = defs_to_varying (ass);
    6654        96688 :                   break;
    6655              :                 }
    6656              :             }
    6657              :         }
    6658              :       else
    6659            0 :         changed = defs_to_varying (ass);
    6660              :     }
    6661    280771320 :   else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
    6662              :     {
    6663     24475924 :       tree lhs = gimple_call_lhs (call_stmt);
    6664     24475924 :       if (lhs && TREE_CODE (lhs) == SSA_NAME)
    6665              :         {
    6666              :           /* Try constant folding based on our current lattice.  */
    6667      8272497 :           tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
    6668              :                                                             vn_valueize);
    6669      8272497 :           if (simplified)
    6670              :             {
    6671        64241 :               if (dump_file && (dump_flags & TDF_DETAILS))
    6672              :                 {
    6673            1 :                   fprintf (dump_file, "call ");
    6674            1 :                   print_gimple_expr (dump_file, call_stmt, 0);
    6675            1 :                   fprintf (dump_file, " simplified to ");
    6676            1 :                   print_generic_expr (dump_file, simplified);
    6677            1 :                   fprintf (dump_file, "\n");
    6678              :                 }
    6679              :             }
    6680              :           /* Setting value numbers to constants will occasionally
    6681              :              screw up phi congruence because constants are not
    6682              :              uniquely associated with a single ssa name that can be
    6683              :              looked up.  */
    6684        64241 :           if (simplified
    6685        64241 :               && is_gimple_min_invariant (simplified))
    6686              :             {
    6687        57827 :               changed = set_ssa_val_to (lhs, simplified);
    6688       115654 :               if (gimple_vdef (call_stmt))
    6689          740 :                 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
    6690              :                                            SSA_VAL (gimple_vuse (call_stmt)));
    6691        57827 :               goto done;
    6692              :             }
    6693      8214670 :           else if (simplified
    6694         6414 :                    && TREE_CODE (simplified) == SSA_NAME)
    6695              :             {
    6696          293 :               changed = visit_copy (lhs, simplified);
    6697          586 :               if (gimple_vdef (call_stmt))
    6698            0 :                 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
    6699              :                                            SSA_VAL (gimple_vuse (call_stmt)));
    6700          293 :               goto done;
    6701              :             }
    6702      8214377 :           else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
    6703              :             {
    6704          381 :               changed = defs_to_varying (call_stmt);
    6705          381 :               goto done;
    6706              :             }
    6707              :         }
    6708              : 
    6709              :       /* Pick up flags from a devirtualization target.  */
    6710     24417423 :       tree fn = gimple_call_fn (stmt);
    6711     24417423 :       int extra_fnflags = 0;
    6712     24417423 :       if (fn && TREE_CODE (fn) == SSA_NAME)
    6713              :         {
    6714       533459 :           fn = SSA_VAL (fn);
    6715       533459 :           if (TREE_CODE (fn) == ADDR_EXPR
    6716       533459 :               && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
    6717         4854 :             extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
    6718              :         }
    6719     24417423 :       if ((/* Calls to the same function with the same vuse
    6720              :               and the same operands do not necessarily return the same
    6721              :               value, unless they're pure or const.  */
    6722     24417423 :            ((gimple_call_flags (call_stmt) | extra_fnflags)
    6723     24417423 :             & (ECF_PURE | ECF_CONST))
    6724              :            /* If calls have a vdef, subsequent calls won't have
    6725              :               the same incoming vuse.  So, if 2 calls with vdef have the
    6726              :               same vuse, we know they're not subsequent.
    6727              :               We can value number 2 calls to the same function with the
    6728              :               same vuse and the same operands which are not subsequent
    6729              :               the same, because there is no code in the program that can
    6730              :               compare the 2 values...  */
    6731     20585945 :            || (gimple_vdef (call_stmt)
    6732              :                /* ... unless the call returns a pointer which does
    6733              :                   not alias with anything else.  In which case the
    6734              :                   information that the values are distinct are encoded
    6735              :                   in the IL.  */
    6736     20551073 :                && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
    6737              :                /* Only perform the following when being called from PRE
    6738              :                   which embeds tail merging.  */
    6739     20016385 :                && default_vn_walk_kind == VN_WALK))
    6740              :           /* Do not process .DEFERRED_INIT since that confuses uninit
    6741              :              analysis.  */
    6742     29308146 :           && !gimple_call_internal_p (call_stmt, IFN_DEFERRED_INIT))
    6743      8515454 :         changed = visit_reference_op_call (lhs, call_stmt);
    6744              :       else
    6745     15901969 :         changed = defs_to_varying (call_stmt);
    6746              :     }
    6747              :   else
    6748    256295396 :     changed = defs_to_varying (stmt);
    6749    446592107 :  done:
    6750    446592107 :   return changed;
    6751              : }
    6752              : 
    6753              : 
    6754              : /* Allocate a value number table.  */
    6755              : 
    6756              : static void
    6757      6100358 : allocate_vn_table (vn_tables_t table, unsigned size)
    6758              : {
    6759      6100358 :   table->phis = new vn_phi_table_type (size);
    6760      6100358 :   table->nary = new vn_nary_op_table_type (size);
    6761      6100358 :   table->references = new vn_reference_table_type (size);
    6762      6100358 : }
    6763              : 
    6764              : /* Free a value number table.  */
    6765              : 
    6766              : static void
    6767      6100358 : free_vn_table (vn_tables_t table)
    6768              : {
    6769              :   /* Walk over elements and release vectors.  */
    6770      6100358 :   vn_reference_iterator_type hir;
    6771      6100358 :   vn_reference_t vr;
    6772    144847846 :   FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
    6773     69373744 :     vr->operands.release ();
    6774      6100358 :   delete table->phis;
    6775      6100358 :   table->phis = NULL;
    6776      6100358 :   delete table->nary;
    6777      6100358 :   table->nary = NULL;
    6778      6100358 :   delete table->references;
    6779      6100358 :   table->references = NULL;
    6780      6100358 : }
    6781              : 
    6782              : /* Set *ID according to RESULT.  */
    6783              : 
    6784              : static void
    6785     34094692 : set_value_id_for_result (tree result, unsigned int *id)
    6786              : {
    6787     34094692 :   if (result && TREE_CODE (result) == SSA_NAME)
    6788     21224204 :     *id = VN_INFO (result)->value_id;
    6789      9647440 :   else if (result && is_gimple_min_invariant (result))
    6790      3636464 :     *id = get_or_alloc_constant_value_id (result);
    6791              :   else
    6792      9234024 :     *id = get_next_value_id ();
    6793     34094692 : }
    6794              : 
    6795              : /* Set the value ids in the valid hash tables.  */
    6796              : 
    6797              : static void
    6798       961530 : set_hashtable_value_ids (void)
    6799              : {
    6800       961530 :   vn_nary_op_iterator_type hin;
    6801       961530 :   vn_phi_iterator_type hip;
    6802       961530 :   vn_reference_iterator_type hir;
    6803       961530 :   vn_nary_op_t vno;
    6804       961530 :   vn_reference_t vr;
    6805       961530 :   vn_phi_t vp;
    6806              : 
    6807              :   /* Now set the value ids of the things we had put in the hash
    6808              :      table.  */
    6809              : 
    6810     48118818 :   FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
    6811     23578644 :     if (! vno->predicated_values)
    6812      7694857 :       set_value_id_for_result (vno->u.result, &vno->value_id);
    6813              : 
    6814      8830042 :   FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
    6815      3934256 :     set_value_id_for_result (vp->result, &vp->value_id);
    6816              : 
    6817     45892688 :   FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
    6818              :                                hir)
    6819     22465579 :     set_value_id_for_result (vr->result, &vr->value_id);
    6820       961530 : }
    6821              : 
    6822              : /* Return the maximum value id we have ever seen.  */
    6823              : 
    6824              : unsigned int
    6825      1923060 : get_max_value_id (void)
    6826              : {
    6827      1923060 :   return next_value_id;
    6828              : }
    6829              : 
    6830              : /* Return the maximum constant value id we have ever seen.  */
    6831              : 
    6832              : unsigned int
    6833      1923060 : get_max_constant_value_id (void)
    6834              : {
    6835      1923060 :   return -next_constant_value_id;
    6836              : }
    6837              : 
    6838              : /* Return the next unique value id.  */
    6839              : 
    6840              : unsigned int
    6841     48542656 : get_next_value_id (void)
    6842              : {
    6843     48542656 :   gcc_checking_assert ((int)next_value_id > 0);
    6844     48542656 :   return next_value_id++;
    6845              : }
    6846              : 
    6847              : /* Return the next unique value id for constants.  */
    6848              : 
    6849              : unsigned int
    6850      2500739 : get_next_constant_value_id (void)
    6851              : {
    6852      2500739 :   gcc_checking_assert (next_constant_value_id < 0);
    6853      2500739 :   return next_constant_value_id--;
    6854              : }
    6855              : 
    6856              : 
    6857              : /* Compare two expressions E1 and E2 and return true if they are equal.
    6858              :    If match_vn_top_optimistically is true then VN_TOP is equal to anything,
    6859              :    otherwise VN_TOP only matches VN_TOP.  */
    6860              : 
    6861              : bool
    6862    243667169 : expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically)
    6863              : {
    6864              :   /* The obvious case.  */
    6865    243667169 :   if (e1 == e2)
    6866              :     return true;
    6867              : 
    6868              :   /* If either one is VN_TOP consider them equal.  */
    6869     69959733 :   if (match_vn_top_optimistically
    6870     65094961 :       && (e1 == VN_TOP || e2 == VN_TOP))
    6871              :     return true;
    6872              : 
    6873              :   /* If only one of them is null, they cannot be equal.  While in general
    6874              :      this should not happen for operations like TARGET_MEM_REF some
    6875              :      operands are optional and an identity value we could substitute
    6876              :      has differing semantics.  */
    6877     69959733 :   if (!e1 || !e2)
    6878              :     return false;
    6879              : 
    6880              :   /* SSA_NAME compare pointer equal.  */
    6881     69959733 :   if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
    6882              :     return false;
    6883              : 
    6884              :   /* Now perform the actual comparison.  */
    6885     34718887 :   if (TREE_CODE (e1) == TREE_CODE (e2)
    6886     34718887 :       && operand_equal_p (e1, e2, OEP_PURE_SAME))
    6887              :     return true;
    6888              : 
    6889              :   return false;
    6890              : }
    6891              : 
    6892              : 
    6893              : /* Return true if the nary operation NARY may trap.  This is a copy
    6894              :    of stmt_could_throw_1_p adjusted to the SCCVN IL.  */
    6895              : 
    6896              : bool
    6897      5624922 : vn_nary_may_trap (vn_nary_op_t nary)
    6898              : {
    6899      5624922 :   tree type;
    6900      5624922 :   tree rhs2 = NULL_TREE;
    6901      5624922 :   bool honor_nans = false;
    6902      5624922 :   bool honor_snans = false;
    6903      5624922 :   bool fp_operation = false;
    6904      5624922 :   bool honor_trapv = false;
    6905      5624922 :   bool handled, ret;
    6906      5624922 :   unsigned i;
    6907              : 
    6908      5624922 :   if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
    6909              :       || TREE_CODE_CLASS (nary->opcode) == tcc_unary
    6910      5624922 :       || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
    6911              :     {
    6912      5524350 :       type = nary->type;
    6913      5524350 :       fp_operation = FLOAT_TYPE_P (type);
    6914      5524350 :       if (fp_operation)
    6915              :         {
    6916       117997 :           honor_nans = flag_trapping_math && !flag_finite_math_only;
    6917       117997 :           honor_snans = flag_signaling_nans != 0;
    6918              :         }
    6919      5406353 :       else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
    6920              :         honor_trapv = true;
    6921              :     }
    6922      5624922 :   if (nary->length >= 2)
    6923      2237637 :     rhs2 = nary->op[1];
    6924      5624922 :   ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
    6925              :                                        honor_trapv, honor_nans, honor_snans,
    6926              :                                        rhs2, &handled);
    6927      5624922 :   if (handled && ret)
    6928              :     return true;
    6929              : 
    6930     13176802 :   for (i = 0; i < nary->length; ++i)
    6931      7668708 :     if (tree_could_trap_p (nary->op[i]))
    6932              :       return true;
    6933              : 
    6934              :   return false;
    6935              : }
    6936              : 
    6937              : /* Return true if the reference operation REF may trap.  */
    6938              : 
    6939              : bool
    6940       914032 : vn_reference_may_trap (vn_reference_t ref)
    6941              : {
    6942       914032 :   switch (ref->operands[0].opcode)
    6943              :     {
    6944              :     case MODIFY_EXPR:
    6945              :     case CALL_EXPR:
    6946              :       /* We do not handle calls.  */
    6947              :       return true;
    6948              :     case ADDR_EXPR:
    6949              :       /* And toplevel address computations never trap.  */
    6950              :       return false;
    6951              :     default:;
    6952              :     }
    6953              : 
    6954              :   vn_reference_op_t op;
    6955              :   unsigned i;
    6956      2531839 :   FOR_EACH_VEC_ELT (ref->operands, i, op)
    6957              :     {
    6958      2531604 :       switch (op->opcode)
    6959              :         {
    6960              :         case WITH_SIZE_EXPR:
    6961              :         case TARGET_MEM_REF:
    6962              :           /* Always variable.  */
    6963              :           return true;
    6964       711946 :         case COMPONENT_REF:
    6965       711946 :           if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
    6966              :             return true;
    6967              :           break;
    6968            0 :         case ARRAY_RANGE_REF:
    6969            0 :           if (TREE_CODE (op->op0) == SSA_NAME)
    6970              :             return true;
    6971              :           break;
    6972       203856 :         case ARRAY_REF:
    6973       203856 :           {
    6974       203856 :             if (TREE_CODE (op->op0) != INTEGER_CST)
    6975              :               return true;
    6976              : 
    6977              :             /* !in_array_bounds   */
    6978       183911 :             tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
    6979       183911 :             if (!domain_type)
    6980              :               return true;
    6981              : 
    6982       183865 :             tree min = op->op1;
    6983       183865 :             tree max = TYPE_MAX_VALUE (domain_type);
    6984       183865 :             if (!min
    6985       183865 :                 || !max
    6986       170964 :                 || TREE_CODE (min) != INTEGER_CST
    6987       170964 :                 || TREE_CODE (max) != INTEGER_CST)
    6988              :               return true;
    6989              : 
    6990       168335 :             if (tree_int_cst_lt (op->op0, min)
    6991       168335 :                 || tree_int_cst_lt (max, op->op0))
    6992          325 :               return true;
    6993              : 
    6994              :             break;
    6995              :           }
    6996              :         case MEM_REF:
    6997              :           /* Nothing interesting in itself, the base is separate.  */
    6998              :           break;
    6999              :         /* The following are the address bases.  */
    7000              :         case SSA_NAME:
    7001              :           return true;
    7002       525226 :         case ADDR_EXPR:
    7003       525226 :           if (op->op0)
    7004       525226 :             return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
    7005              :           return false;
    7006      1699584 :         default:;
    7007              :         }
    7008              :     }
    7009              :   return false;
    7010              : }
    7011              : 
    7012     10319395 : eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
    7013     10319395 :                                             bitmap inserted_exprs_)
    7014     10319395 :   : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
    7015     10319395 :     el_todo (0), eliminations (0), insertions (0),
    7016     10319395 :     inserted_exprs (inserted_exprs_)
    7017              : {
    7018     10319395 :   need_eh_cleanup = BITMAP_ALLOC (NULL);
    7019     10319395 :   need_ab_cleanup = BITMAP_ALLOC (NULL);
    7020     10319395 : }
    7021              : 
    7022     10319395 : eliminate_dom_walker::~eliminate_dom_walker ()
    7023              : {
    7024     10319395 :   BITMAP_FREE (need_eh_cleanup);
    7025     10319395 :   BITMAP_FREE (need_ab_cleanup);
    7026     10319395 : }
    7027              : 
    7028              : /* Return a leader for OP that is available at the current point of the
    7029              :    eliminate domwalk.  */
    7030              : 
    7031              : tree
    7032    178848140 : eliminate_dom_walker::eliminate_avail (basic_block, tree op)
    7033              : {
    7034    178848140 :   tree valnum = VN_INFO (op)->valnum;
    7035    178848140 :   if (TREE_CODE (valnum) == SSA_NAME)
    7036              :     {
    7037    173890262 :       if (SSA_NAME_IS_DEFAULT_DEF (valnum))
    7038              :         return valnum;
    7039    302895944 :       if (avail.length () > SSA_NAME_VERSION (valnum))
    7040              :         {
    7041    136389978 :           tree av = avail[SSA_NAME_VERSION (valnum)];
    7042              :           /* When PRE discovers a new redundancy there's no way to unite
    7043              :              the value classes so it instead inserts a copy old-val = new-val.
    7044              :              Look through such copies here, providing one more level of
    7045              :              simplification at elimination time.  */
    7046    136389978 :           gassign *ass;
    7047    239697794 :           if (av && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (av))))
    7048     73405402 :             if (gimple_assign_rhs_class (ass) == GIMPLE_SINGLE_RHS)
    7049              :               {
    7050     38698969 :                 tree rhs1 = gimple_assign_rhs1 (ass);
    7051     38698969 :                 if (CONSTANT_CLASS_P (rhs1)
    7052     38698969 :                     || (TREE_CODE (rhs1) == SSA_NAME
    7053        10057 :                         && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
    7054              :                   av = rhs1;
    7055              :               }
    7056    136389978 :           return av;
    7057              :         }
    7058              :     }
    7059      4957878 :   else if (is_gimple_min_invariant (valnum))
    7060              :     return valnum;
    7061              :   return NULL_TREE;
    7062              : }
    7063              : 
    7064              : /* At the current point of the eliminate domwalk make OP available.  */
    7065              : 
    7066              : void
    7067     49541287 : eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
    7068              : {
    7069     49541287 :   tree valnum = VN_INFO (op)->valnum;
    7070     49541287 :   if (TREE_CODE (valnum) == SSA_NAME)
    7071              :     {
    7072     95747681 :       if (avail.length () <= SSA_NAME_VERSION (valnum))
    7073     16777179 :         avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
    7074     49541287 :       tree pushop = op;
    7075     49541287 :       if (avail[SSA_NAME_VERSION (valnum)])
    7076        44478 :         pushop = avail[SSA_NAME_VERSION (valnum)];
    7077     49541287 :       avail_stack.safe_push (pushop);
    7078     49541287 :       avail[SSA_NAME_VERSION (valnum)] = op;
    7079              :     }
    7080     49541287 : }
    7081              : 
    7082              : /* Insert the expression recorded by SCCVN for VAL at *GSI.  Returns
    7083              :    the leader for the expression if insertion was successful.  */
    7084              : 
    7085              : tree
    7086       122661 : eliminate_dom_walker::eliminate_insert (basic_block bb,
    7087              :                                         gimple_stmt_iterator *gsi, tree val)
    7088              : {
    7089              :   /* We can insert a sequence with a single assignment only.  */
    7090       122661 :   gimple_seq stmts = VN_INFO (val)->expr;
    7091       122661 :   if (!gimple_seq_singleton_p (stmts))
    7092              :     return NULL_TREE;
    7093       223323 :   gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
    7094       122661 :   if (!stmt
    7095       122661 :       || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
    7096              :           && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
    7097              :           && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
    7098              :           && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
    7099              :           && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
    7100           88 :               || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
    7101              :     return NULL_TREE;
    7102              : 
    7103        31478 :   tree op = gimple_assign_rhs1 (stmt);
    7104        31478 :   if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
    7105        31478 :       || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
    7106        19345 :     op = TREE_OPERAND (op, 0);
    7107        31478 :   tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
    7108        31432 :   if (!leader)
    7109              :     return NULL_TREE;
    7110              : 
    7111        22003 :   tree res;
    7112        22003 :   stmts = NULL;
    7113        40336 :   if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
    7114        32732 :     res = gimple_build (&stmts, BIT_FIELD_REF,
    7115        16366 :                         TREE_TYPE (val), leader,
    7116        16366 :                         TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
    7117        16366 :                         TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
    7118         5637 :   else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
    7119          160 :     res = gimple_build (&stmts, BIT_AND_EXPR,
    7120           80 :                         TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
    7121              :   else
    7122         5557 :     res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
    7123         5557 :                         TREE_TYPE (val), leader);
    7124        22003 :   if (TREE_CODE (res) != SSA_NAME
    7125        22002 :       || SSA_NAME_IS_DEFAULT_DEF (res)
    7126        44005 :       || gimple_bb (SSA_NAME_DEF_STMT (res)))
    7127              :     {
    7128            4 :       gimple_seq_discard (stmts);
    7129              : 
    7130              :       /* During propagation we have to treat SSA info conservatively
    7131              :          and thus we can end up simplifying the inserted expression
    7132              :          at elimination time to sth not defined in stmts.  */
    7133              :       /* But then this is a redundancy we failed to detect.  Which means
    7134              :          res now has two values.  That doesn't play well with how
    7135              :          we track availability here, so give up.  */
    7136            4 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7137              :         {
    7138            0 :           if (TREE_CODE (res) == SSA_NAME)
    7139            0 :             res = eliminate_avail (bb, res);
    7140            0 :           if (res)
    7141              :             {
    7142            0 :               fprintf (dump_file, "Failed to insert expression for value ");
    7143            0 :               print_generic_expr (dump_file, val);
    7144            0 :               fprintf (dump_file, " which is really fully redundant to ");
    7145            0 :               print_generic_expr (dump_file, res);
    7146            0 :               fprintf (dump_file, "\n");
    7147              :             }
    7148              :         }
    7149              : 
    7150            4 :       return NULL_TREE;
    7151              :     }
    7152              :   else
    7153              :     {
    7154        21999 :       gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    7155        21999 :       vn_ssa_aux_t vn_info = VN_INFO (res);
    7156        21999 :       vn_info->valnum = val;
    7157        21999 :       vn_info->visited = true;
    7158              :     }
    7159              : 
    7160        21999 :   insertions++;
    7161        21999 :   if (dump_file && (dump_flags & TDF_DETAILS))
    7162              :     {
    7163          497 :       fprintf (dump_file, "Inserted ");
    7164          497 :       print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
    7165              :     }
    7166              : 
    7167              :   return res;
    7168              : }
    7169              : 
    7170              : void
    7171    344682651 : eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
    7172              : {
    7173    344682651 :   tree sprime = NULL_TREE;
    7174    344682651 :   gimple *stmt = gsi_stmt (*gsi);
    7175    344682651 :   tree lhs = gimple_get_lhs (stmt);
    7176    118763362 :   if (lhs && TREE_CODE (lhs) == SSA_NAME
    7177    164307744 :       && !gimple_has_volatile_ops (stmt)
    7178              :       /* See PR43491.  Do not replace a global register variable when
    7179              :          it is a the RHS of an assignment.  Do replace local register
    7180              :          variables since gcc does not guarantee a local variable will
    7181              :          be allocated in register.
    7182              :          ???  The fix isn't effective here.  This should instead
    7183              :          be ensured by not value-numbering them the same but treating
    7184              :          them like volatiles?  */
    7185    425773507 :       && !(gimple_assign_single_p (stmt)
    7186     34868697 :            && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
    7187      2439682 :                && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
    7188         4184 :                && is_global_var (gimple_assign_rhs1 (stmt)))))
    7189              :     {
    7190     81090612 :       sprime = eliminate_avail (b, lhs);
    7191     81090612 :       if (!sprime)
    7192              :         {
    7193              :           /* If there is no existing usable leader but SCCVN thinks
    7194              :              it has an expression it wants to use as replacement,
    7195              :              insert that.  */
    7196     68242708 :           tree val = VN_INFO (lhs)->valnum;
    7197     68242708 :           vn_ssa_aux_t vn_info;
    7198     68242708 :           if (val != VN_TOP
    7199     68242708 :               && TREE_CODE (val) == SSA_NAME
    7200     68242708 :               && (vn_info = VN_INFO (val), true)
    7201     68242708 :               && vn_info->needs_insertion
    7202       317090 :               && vn_info->expr != NULL
    7203     68365369 :               && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
    7204        21999 :             eliminate_push_avail (b, sprime);
    7205              :         }
    7206              : 
    7207              :       /* If this now constitutes a copy duplicate points-to
    7208              :          and range info appropriately.  This is especially
    7209              :          important for inserted code.  */
    7210     68242708 :       if (sprime
    7211     12869903 :           && TREE_CODE (sprime) == SSA_NAME)
    7212      8854437 :         maybe_duplicate_ssa_info_at_copy (lhs, sprime);
    7213              : 
    7214              :       /* Inhibit the use of an inserted PHI on a loop header when
    7215              :          the address of the memory reference is a simple induction
    7216              :          variable.  In other cases the vectorizer won't do anything
    7217              :          anyway (either it's loop invariant or a complicated
    7218              :          expression).  */
    7219      8854437 :       if (sprime
    7220     12869903 :           && TREE_CODE (sprime) == SSA_NAME
    7221      8854437 :           && do_pre
    7222       905940 :           && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
    7223       887255 :           && loop_outer (b->loop_father)
    7224       384172 :           && has_zero_uses (sprime)
    7225       190940 :           && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
    7226       190759 :           && gimple_assign_load_p (stmt))
    7227              :         {
    7228       103063 :           gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
    7229       103063 :           basic_block def_bb = gimple_bb (def_stmt);
    7230       103063 :           if (gimple_code (def_stmt) == GIMPLE_PHI
    7231       103063 :               && def_bb->loop_father->header == def_bb)
    7232              :             {
    7233        65471 :               loop_p loop = def_bb->loop_father;
    7234        65471 :               ssa_op_iter iter;
    7235        65471 :               tree op;
    7236        65471 :               bool found = false;
    7237        82995 :               FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
    7238              :                 {
    7239        61960 :                   affine_iv iv;
    7240        61960 :                   def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
    7241        61960 :                   if (def_bb
    7242        56118 :                       && flow_bb_inside_loop_p (loop, def_bb)
    7243       112973 :                       && simple_iv (loop, loop, op, &iv, true))
    7244              :                     {
    7245        44436 :                       found = true;
    7246        44436 :                       break;
    7247              :                     }
    7248              :                 }
    7249        21035 :               if (found)
    7250              :                 {
    7251        44436 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    7252              :                     {
    7253            3 :                       fprintf (dump_file, "Not replacing ");
    7254            3 :                       print_gimple_expr (dump_file, stmt, 0);
    7255            3 :                       fprintf (dump_file, " with ");
    7256            3 :                       print_generic_expr (dump_file, sprime);
    7257            3 :                       fprintf (dump_file, " which would add a loop"
    7258              :                                " carried dependence to loop %d\n",
    7259              :                                loop->num);
    7260              :                     }
    7261              :                   /* Don't keep sprime available.  */
    7262        44436 :                   sprime = NULL_TREE;
    7263              :                 }
    7264              :             }
    7265              :         }
    7266              : 
    7267     81090612 :       if (sprime)
    7268              :         {
    7269              :           /* If we can propagate the value computed for LHS into
    7270              :              all uses don't bother doing anything with this stmt.  */
    7271     12825467 :           if (may_propagate_copy (lhs, sprime))
    7272              :             {
    7273              :               /* Mark it for removal.  */
    7274     12823504 :               to_remove.safe_push (stmt);
    7275              : 
    7276              :               /* ???  Don't count copy/constant propagations.  */
    7277     12823504 :               if (gimple_assign_single_p (stmt)
    7278     12823504 :                   && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
    7279      4477006 :                       || gimple_assign_rhs1 (stmt) == sprime))
    7280     13640480 :                 return;
    7281              : 
    7282      7759005 :               if (dump_file && (dump_flags & TDF_DETAILS))
    7283              :                 {
    7284        18630 :                   fprintf (dump_file, "Replaced ");
    7285        18630 :                   print_gimple_expr (dump_file, stmt, 0);
    7286        18630 :                   fprintf (dump_file, " with ");
    7287        18630 :                   print_generic_expr (dump_file, sprime);
    7288        18630 :                   fprintf (dump_file, " in all uses of ");
    7289        18630 :                   print_gimple_stmt (dump_file, stmt, 0);
    7290              :                 }
    7291              : 
    7292      7759005 :               eliminations++;
    7293      7759005 :               return;
    7294              :             }
    7295              : 
    7296              :           /* If this is an assignment from our leader (which
    7297              :              happens in the case the value-number is a constant)
    7298              :              then there is nothing to do.  Likewise if we run into
    7299              :              inserted code that needed a conversion because of
    7300              :              our type-agnostic value-numbering of loads.  */
    7301         1963 :           if ((gimple_assign_single_p (stmt)
    7302            1 :                || (is_gimple_assign (stmt)
    7303            1 :                    && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
    7304            0 :                        || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
    7305         1964 :               && sprime == gimple_assign_rhs1 (stmt))
    7306              :             return;
    7307              : 
    7308              :           /* Else replace its RHS.  */
    7309          719 :           if (dump_file && (dump_flags & TDF_DETAILS))
    7310              :             {
    7311            0 :               fprintf (dump_file, "Replaced ");
    7312            0 :               print_gimple_expr (dump_file, stmt, 0);
    7313            0 :               fprintf (dump_file, " with ");
    7314            0 :               print_generic_expr (dump_file, sprime);
    7315            0 :               fprintf (dump_file, " in ");
    7316            0 :               print_gimple_stmt (dump_file, stmt, 0);
    7317              :             }
    7318          719 :           eliminations++;
    7319              : 
    7320          719 :           bool can_make_abnormal_goto = (is_gimple_call (stmt)
    7321          719 :                                          && stmt_can_make_abnormal_goto (stmt));
    7322          719 :           gimple *orig_stmt = stmt;
    7323          719 :           if (!useless_type_conversion_p (TREE_TYPE (lhs),
    7324          719 :                                           TREE_TYPE (sprime)))
    7325              :             {
    7326              :               /* We preserve conversions to but not from function or method
    7327              :                  types.  This asymmetry makes it necessary to re-instantiate
    7328              :                  conversions here.  */
    7329          717 :               if (POINTER_TYPE_P (TREE_TYPE (lhs))
    7330          717 :                   && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
    7331          717 :                 sprime = fold_convert (TREE_TYPE (lhs), sprime);
    7332              :               else
    7333            0 :                 gcc_unreachable ();
    7334              :             }
    7335          719 :           tree vdef = gimple_vdef (stmt);
    7336          719 :           tree vuse = gimple_vuse (stmt);
    7337          719 :           propagate_tree_value_into_stmt (gsi, sprime);
    7338          719 :           stmt = gsi_stmt (*gsi);
    7339          719 :           update_stmt (stmt);
    7340              :           /* In case the VDEF on the original stmt was released, value-number
    7341              :              it to the VUSE.  This is to make vuse_ssa_val able to skip
    7342              :              released virtual operands.  */
    7343         1438 :           if (vdef != gimple_vdef (stmt))
    7344              :             {
    7345            0 :               gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
    7346            0 :               VN_INFO (vdef)->valnum = vuse;
    7347              :             }
    7348              : 
    7349              :           /* If we removed EH side-effects from the statement, clean
    7350              :              its EH information.  */
    7351          719 :           if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
    7352              :             {
    7353            0 :               bitmap_set_bit (need_eh_cleanup,
    7354            0 :                               gimple_bb (stmt)->index);
    7355            0 :               if (dump_file && (dump_flags & TDF_DETAILS))
    7356            0 :                 fprintf (dump_file, "  Removed EH side-effects.\n");
    7357              :             }
    7358              : 
    7359              :           /* Likewise for AB side-effects.  */
    7360          719 :           if (can_make_abnormal_goto
    7361          719 :               && !stmt_can_make_abnormal_goto (stmt))
    7362              :             {
    7363            0 :               bitmap_set_bit (need_ab_cleanup,
    7364            0 :                               gimple_bb (stmt)->index);
    7365            0 :               if (dump_file && (dump_flags & TDF_DETAILS))
    7366            0 :                 fprintf (dump_file, "  Removed AB side-effects.\n");
    7367              :             }
    7368              : 
    7369          719 :           return;
    7370              :         }
    7371              :     }
    7372              : 
    7373              :   /* If the statement is a scalar store, see if the expression
    7374              :      has the same value number as its rhs.  If so, the store is
    7375              :      dead.  */
    7376    331857184 :   if (gimple_assign_single_p (stmt)
    7377    125260842 :       && !gimple_has_volatile_ops (stmt)
    7378     54609264 :       && !is_gimple_reg (gimple_assign_lhs (stmt))
    7379     28236542 :       && (TREE_CODE (gimple_assign_lhs (stmt)) != VAR_DECL
    7380      2750231 :           || !DECL_HARD_REGISTER (gimple_assign_lhs (stmt)))
    7381    360089717 :       && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
    7382     16128733 :           || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
    7383              :     {
    7384     25179451 :       tree rhs = gimple_assign_rhs1 (stmt);
    7385     25179451 :       vn_reference_t vnresult;
    7386              :       /* ???  gcc.dg/torture/pr91445.c shows that we lookup a boolean
    7387              :          typed load of a byte known to be 0x11 as 1 so a store of
    7388              :          a boolean 1 is detected as redundant.  Because of this we
    7389              :          have to make sure to lookup with a ref where its size
    7390              :          matches the precision.  */
    7391     25179451 :       tree lookup_lhs = lhs;
    7392     50104814 :       if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
    7393     13138405 :           && (TREE_CODE (lhs) != COMPONENT_REF
    7394      7992553 :               || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
    7395     38123785 :           && !type_has_mode_precision_p (TREE_TYPE (lhs)))
    7396              :         {
    7397       819020 :           if (BITINT_TYPE_P (TREE_TYPE (lhs))
    7398       425761 :               && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
    7399              :             lookup_lhs = NULL_TREE;
    7400       407716 :           else if (TREE_CODE (lhs) == COMPONENT_REF
    7401       407716 :                    || TREE_CODE (lhs) == MEM_REF)
    7402              :             {
    7403       285555 :               tree ltype = build_nonstandard_integer_type
    7404       285555 :                                 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
    7405       285555 :                                  TYPE_UNSIGNED (TREE_TYPE (lhs)));
    7406       285555 :               if (TREE_CODE (lhs) == COMPONENT_REF)
    7407              :                 {
    7408       217802 :                   tree foff = component_ref_field_offset (lhs);
    7409       217802 :                   tree f = TREE_OPERAND (lhs, 1);
    7410       217802 :                   if (!poly_int_tree_p (foff))
    7411              :                     lookup_lhs = NULL_TREE;
    7412              :                   else
    7413       435604 :                     lookup_lhs = build3 (BIT_FIELD_REF, ltype,
    7414       217802 :                                          TREE_OPERAND (lhs, 0),
    7415       217802 :                                          TYPE_SIZE (TREE_TYPE (lhs)),
    7416              :                                          bit_from_pos
    7417       217802 :                                            (foff, DECL_FIELD_BIT_OFFSET (f)));
    7418              :                 }
    7419              :               else
    7420        67753 :                 lookup_lhs = build2 (MEM_REF, ltype,
    7421        67753 :                                      TREE_OPERAND (lhs, 0),
    7422        67753 :                                      TREE_OPERAND (lhs, 1));
    7423              :             }
    7424              :           else
    7425              :             lookup_lhs = NULL_TREE;
    7426              :         }
    7427     25050083 :       tree val = NULL_TREE, tem;
    7428     25050083 :       if (lookup_lhs)
    7429     50100166 :         val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
    7430              :                                    VN_WALKREWRITE, &vnresult, false,
    7431              :                                    NULL, NULL_TREE, true);
    7432     25179451 :       if (TREE_CODE (rhs) == SSA_NAME)
    7433     12103800 :         rhs = VN_INFO (rhs)->valnum;
    7434     25179451 :       gassign *ass;
    7435     25179451 :       if (val
    7436     25179451 :           && (operand_equal_p (val, rhs, 0)
    7437              :               /* Due to the bitfield lookups above we can get bit
    7438              :                  interpretations of the same RHS as values here.  Those
    7439              :                  are redundant as well.  */
    7440      3064105 :               || (TREE_CODE (val) == SSA_NAME
    7441      1868800 :                   && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
    7442      1700197 :                   && (tem = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
    7443      1700197 :                   && TREE_CODE (tem) == VIEW_CONVERT_EXPR
    7444         3492 :                   && TREE_OPERAND (tem, 0) == rhs)
    7445      3064103 :               || (TREE_CODE (rhs) == SSA_NAME
    7446     25635946 :                   && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs)))
    7447      1456669 :                   && gimple_assign_rhs1 (ass) == val
    7448       678148 :                   && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass))
    7449            9 :                   && tree_nop_conversion_p (TREE_TYPE (rhs), TREE_TYPE (val)))))
    7450              :         {
    7451              :           /* We can only remove the later store if the former aliases
    7452              :              at least all accesses the later one does or if the store
    7453              :              was to readonly memory storing the same value.  */
    7454       239144 :           ao_ref lhs_ref;
    7455       239144 :           ao_ref_init (&lhs_ref, lhs);
    7456       239144 :           alias_set_type set = ao_ref_alias_set (&lhs_ref);
    7457       239144 :           alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
    7458       239144 :           if (! vnresult
    7459       239144 :               || ((vnresult->set == set
    7460        49176 :                    || alias_set_subset_of (set, vnresult->set))
    7461       223792 :                   && (vnresult->base_set == base_set
    7462        21112 :                       || alias_set_subset_of (base_set, vnresult->base_set))))
    7463              :             {
    7464       221653 :               if (dump_file && (dump_flags & TDF_DETAILS))
    7465              :                 {
    7466           17 :                   fprintf (dump_file, "Deleted redundant store ");
    7467           17 :                   print_gimple_stmt (dump_file, stmt, 0);
    7468              :                 }
    7469              : 
    7470              :               /* Queue stmt for removal.  */
    7471       221653 :               to_remove.safe_push (stmt);
    7472       221653 :               return;
    7473              :             }
    7474              :         }
    7475              :     }
    7476              : 
    7477              :   /* If this is a control statement value numbering left edges
    7478              :      unexecuted on force the condition in a way consistent with
    7479              :      that.  */
    7480    331635531 :   if (gcond *cond = dyn_cast <gcond *> (stmt))
    7481              :     {
    7482     18751799 :       if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
    7483     18751799 :           ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
    7484              :         {
    7485       593360 :           if (dump_file && (dump_flags & TDF_DETAILS))
    7486              :             {
    7487           15 :               fprintf (dump_file, "Removing unexecutable edge from ");
    7488           15 :               print_gimple_stmt (dump_file, stmt, 0);
    7489              :             }
    7490       593360 :           if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
    7491       593360 :               == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
    7492       232808 :             gimple_cond_make_true (cond);
    7493              :           else
    7494       360552 :             gimple_cond_make_false (cond);
    7495       593360 :           update_stmt (cond);
    7496       593360 :           el_todo |= TODO_cleanup_cfg;
    7497       593360 :           return;
    7498              :         }
    7499              :     }
    7500              : 
    7501    331042171 :   bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
    7502    331042171 :   bool was_noreturn = (is_gimple_call (stmt)
    7503    331042171 :                        && gimple_call_noreturn_p (stmt));
    7504    331042171 :   tree vdef = gimple_vdef (stmt);
    7505    331042171 :   tree vuse = gimple_vuse (stmt);
    7506              : 
    7507              :   /* If we didn't replace the whole stmt (or propagate the result
    7508              :      into all uses), replace all uses on this stmt with their
    7509              :      leaders.  */
    7510    331042171 :   bool modified = false;
    7511    331042171 :   use_operand_p use_p;
    7512    331042171 :   ssa_op_iter iter;
    7513    493395777 :   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
    7514              :     {
    7515    162353606 :       tree use = USE_FROM_PTR (use_p);
    7516              :       /* ???  The call code above leaves stmt operands un-updated.  */
    7517    162353606 :       if (TREE_CODE (use) != SSA_NAME)
    7518            0 :         continue;
    7519    162353606 :       tree sprime;
    7520    162353606 :       if (SSA_NAME_IS_DEFAULT_DEF (use))
    7521              :         /* ???  For default defs BB shouldn't matter, but we have to
    7522              :            solve the inconsistency between rpo eliminate and
    7523              :            dom eliminate avail valueization first.  */
    7524     26040903 :         sprime = eliminate_avail (b, use);
    7525              :       else
    7526              :         /* Look for sth available at the definition block of the argument.
    7527              :            This avoids inconsistencies between availability there which
    7528              :            decides if the stmt can be removed and availability at the
    7529              :            use site.  The SSA property ensures that things available
    7530              :            at the definition are also available at uses.  */
    7531    136312703 :         sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
    7532    162353606 :       if (sprime && sprime != use
    7533     12716816 :           && may_propagate_copy (use, sprime, true)
    7534              :           /* We substitute into debug stmts to avoid excessive
    7535              :              debug temporaries created by removed stmts, but we need
    7536              :              to avoid doing so for inserted sprimes as we never want
    7537              :              to create debug temporaries for them.  */
    7538    175069705 :           && (!inserted_exprs
    7539      1186135 :               || TREE_CODE (sprime) != SSA_NAME
    7540      1171479 :               || !is_gimple_debug (stmt)
    7541       374524 :               || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
    7542              :         {
    7543     12370628 :           propagate_value (use_p, sprime);
    7544     12370628 :           modified = true;
    7545              :         }
    7546              :     }
    7547              : 
    7548              :   /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
    7549              :      into which is a requirement for the IPA devirt machinery.  */
    7550    331042171 :   gimple *old_stmt = stmt;
    7551    331042171 :   if (modified)
    7552              :     {
    7553              :       /* If a formerly non-invariant ADDR_EXPR is turned into an
    7554              :          invariant one it was on a separate stmt.  */
    7555     11488185 :       if (gimple_assign_single_p (stmt)
    7556     11488185 :           && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
    7557       232049 :         recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
    7558     11488185 :       gimple_stmt_iterator prev = *gsi;
    7559     11488185 :       gsi_prev (&prev);
    7560     11488185 :       if (fold_stmt (gsi, follow_all_ssa_edges))
    7561              :         {
    7562              :           /* fold_stmt may have created new stmts in between
    7563              :              the previous stmt and the folded stmt.  Mark
    7564              :              all defs created there as varying to not confuse
    7565              :              the SCCVN machinery as we're using that even during
    7566              :              elimination.  */
    7567       965873 :           if (gsi_end_p (prev))
    7568       218344 :             prev = gsi_start_bb (b);
    7569              :           else
    7570       856701 :             gsi_next (&prev);
    7571       965873 :           if (gsi_stmt (prev) != gsi_stmt (*gsi))
    7572        89224 :             do
    7573              :               {
    7574        55541 :                 tree def;
    7575        55541 :                 ssa_op_iter dit;
    7576       107259 :                 FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
    7577              :                                            dit, SSA_OP_ALL_DEFS)
    7578              :                     /* As existing DEFs may move between stmts
    7579              :                        only process new ones.  */
    7580        51718 :                     if (! has_VN_INFO (def))
    7581              :                       {
    7582        33581 :                         vn_ssa_aux_t vn_info = VN_INFO (def);
    7583        33581 :                         vn_info->valnum = def;
    7584        33581 :                         vn_info->visited = true;
    7585              :                       }
    7586        55541 :                 if (gsi_stmt (prev) == gsi_stmt (*gsi))
    7587              :                   break;
    7588        33683 :                 gsi_next (&prev);
    7589        33683 :               }
    7590              :             while (1);
    7591              :         }
    7592     11488185 :       stmt = gsi_stmt (*gsi);
    7593              :       /* In case we folded the stmt away schedule the NOP for removal.  */
    7594     11488185 :       if (gimple_nop_p (stmt))
    7595          826 :         to_remove.safe_push (stmt);
    7596              :     }
    7597              : 
    7598              :   /* Visit indirect calls and turn them into direct calls if
    7599              :      possible using the devirtualization machinery.  Do this before
    7600              :      checking for required EH/abnormal/noreturn cleanup as devird
    7601              :      may expose more of those.  */
    7602    331042171 :   if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
    7603              :     {
    7604     22041876 :       tree fn = gimple_call_fn (call_stmt);
    7605     22041876 :       if (fn
    7606     21295756 :           && flag_devirtualize
    7607     42593946 :           && virtual_method_call_p (fn))
    7608              :         {
    7609       176480 :           tree otr_type = obj_type_ref_class (fn);
    7610       176480 :           unsigned HOST_WIDE_INT otr_tok
    7611       176480 :               = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
    7612       176480 :           tree instance;
    7613       176480 :           ipa_polymorphic_call_context context (current_function_decl,
    7614       176480 :                                                 fn, stmt, &instance);
    7615       176480 :           context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
    7616              :                                     otr_type, stmt, NULL);
    7617       176480 :           bool final;
    7618       176480 :           vec <cgraph_node *> targets
    7619       176480 :               = possible_polymorphic_call_targets (obj_type_ref_class (fn),
    7620              :                                                    otr_tok, context, &final);
    7621       176480 :           if (dump_file)
    7622           22 :             dump_possible_polymorphic_call_targets (dump_file,
    7623              :                                                     obj_type_ref_class (fn),
    7624              :                                                     otr_tok, context);
    7625       176768 :           if (final && targets.length () <= 1 && dbg_cnt (devirt))
    7626              :             {
    7627           66 :               tree fn;
    7628           66 :               if (targets.length () == 1)
    7629           66 :                 fn = targets[0]->decl;
    7630              :               else
    7631            0 :                 fn = builtin_decl_unreachable ();
    7632           66 :               if (dump_enabled_p ())
    7633              :                 {
    7634            9 :                   dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
    7635              :                                    "converting indirect call to "
    7636              :                                    "function %s\n",
    7637            9 :                                    lang_hooks.decl_printable_name (fn, 2));
    7638              :                 }
    7639           66 :               gimple_call_set_fndecl (call_stmt, fn);
    7640              :               /* If changing the call to __builtin_unreachable
    7641              :                  or similar noreturn function, adjust gimple_call_fntype
    7642              :                  too.  */
    7643           66 :               if (gimple_call_noreturn_p (call_stmt)
    7644            0 :                   && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
    7645            0 :                   && TYPE_ARG_TYPES (TREE_TYPE (fn))
    7646           66 :                   && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
    7647            0 :                       == void_type_node))
    7648            0 :                 gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
    7649           66 :               maybe_remove_unused_call_args (cfun, call_stmt);
    7650           66 :               modified = true;
    7651              :             }
    7652              :         }
    7653              :     }
    7654              : 
    7655    331042171 :   if (modified)
    7656              :     {
    7657              :       /* When changing a call into a noreturn call, cfg cleanup
    7658              :          is needed to fix up the noreturn call.  */
    7659     11488206 :       if (!was_noreturn
    7660     11488206 :           && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
    7661           56 :         to_fixup.safe_push  (stmt);
    7662              :       /* When changing a condition or switch into one we know what
    7663              :          edge will be executed, schedule a cfg cleanup.  */
    7664     11488206 :       if ((gimple_code (stmt) == GIMPLE_COND
    7665      1485030 :            && (gimple_cond_true_p (as_a <gcond *> (stmt))
    7666      1479810 :                || gimple_cond_false_p (as_a <gcond *> (stmt))))
    7667     12965705 :           || (gimple_code (stmt) == GIMPLE_SWITCH
    7668         7383 :               && TREE_CODE (gimple_switch_index
    7669              :                             (as_a <gswitch *> (stmt))) == INTEGER_CST))
    7670         9318 :         el_todo |= TODO_cleanup_cfg;
    7671              :       /* If we removed EH side-effects from the statement, clean
    7672              :          its EH information.  */
    7673     11488206 :       if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
    7674              :         {
    7675         1616 :           bitmap_set_bit (need_eh_cleanup,
    7676         1616 :                           gimple_bb (stmt)->index);
    7677         1616 :           if (dump_file && (dump_flags & TDF_DETAILS))
    7678            0 :             fprintf (dump_file, "  Removed EH side-effects.\n");
    7679              :         }
    7680              :       /* Likewise for AB side-effects.  */
    7681     11488206 :       if (can_make_abnormal_goto
    7682     11488206 :           && !stmt_can_make_abnormal_goto (stmt))
    7683              :         {
    7684            0 :           bitmap_set_bit (need_ab_cleanup,
    7685            0 :                           gimple_bb (stmt)->index);
    7686            0 :           if (dump_file && (dump_flags & TDF_DETAILS))
    7687            0 :             fprintf (dump_file, "  Removed AB side-effects.\n");
    7688              :         }
    7689     11488206 :       update_stmt (stmt);
    7690              :       /* In case the VDEF on the original stmt was released, value-number
    7691              :          it to the VUSE.  This is to make vuse_ssa_val able to skip
    7692              :          released virtual operands.  */
    7693     14667775 :       if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
    7694         1872 :         VN_INFO (vdef)->valnum = vuse;
    7695              :     }
    7696              : 
    7697              :   /* Make new values available - for fully redundant LHS we
    7698              :      continue with the next stmt above and skip this.
    7699              :      But avoid picking up dead defs.  */
    7700    331042171 :   tree def;
    7701    400629406 :   FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
    7702     69587235 :     if (! has_zero_uses (def)
    7703     69587235 :         || (inserted_exprs
    7704       206836 :             && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (def))))
    7705     68216457 :       eliminate_push_avail (b, def);
    7706              : }
    7707              : 
    7708              : /* Perform elimination for the basic-block B during the domwalk.  */
    7709              : 
    7710              : edge
    7711     40642519 : eliminate_dom_walker::before_dom_children (basic_block b)
    7712              : {
    7713              :   /* Mark new bb.  */
    7714     40642519 :   avail_stack.safe_push (NULL_TREE);
    7715              : 
    7716              :   /* Skip unreachable blocks marked unreachable during the SCCVN domwalk.  */
    7717     40642519 :   if (!(b->flags & BB_EXECUTABLE))
    7718              :     return NULL;
    7719              : 
    7720     35904523 :   vn_context_bb = b;
    7721              : 
    7722     47214373 :   for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
    7723              :     {
    7724     11309850 :       gphi *phi = gsi.phi ();
    7725     11309850 :       tree res = PHI_RESULT (phi);
    7726              : 
    7727     22619700 :       if (virtual_operand_p (res))
    7728              :         {
    7729      5188819 :           gsi_next (&gsi);
    7730      5188819 :           continue;
    7731              :         }
    7732              : 
    7733      6121031 :       tree sprime = eliminate_avail (b, res);
    7734      6121031 :       if (sprime
    7735      6121031 :           && sprime != res)
    7736              :         {
    7737       428387 :           if (dump_file && (dump_flags & TDF_DETAILS))
    7738              :             {
    7739           20 :               fprintf (dump_file, "Replaced redundant PHI node defining ");
    7740           20 :               print_generic_expr (dump_file, res);
    7741           20 :               fprintf (dump_file, " with ");
    7742           20 :               print_generic_expr (dump_file, sprime);
    7743           20 :               fprintf (dump_file, "\n");
    7744              :             }
    7745              : 
    7746              :           /* If we inserted this PHI node ourself, it's not an elimination.  */
    7747       428387 :           if (! inserted_exprs
    7748       543504 :               || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
    7749       402702 :             eliminations++;
    7750              : 
    7751              :           /* If we will propagate into all uses don't bother to do
    7752              :              anything.  */
    7753       428387 :           if (may_propagate_copy (res, sprime))
    7754              :             {
    7755              :               /* Mark the PHI for removal.  */
    7756       428387 :               to_remove.safe_push (phi);
    7757       428387 :               gsi_next (&gsi);
    7758       428387 :               continue;
    7759              :             }
    7760              : 
    7761            0 :           remove_phi_node (&gsi, false);
    7762              : 
    7763            0 :           if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
    7764            0 :             sprime = fold_convert (TREE_TYPE (res), sprime);
    7765            0 :           gimple *stmt = gimple_build_assign (res, sprime);
    7766            0 :           gimple_stmt_iterator gsi2 = gsi_after_labels (b);
    7767            0 :           gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
    7768            0 :           continue;
    7769            0 :         }
    7770              : 
    7771      5692644 :       eliminate_push_avail (b, res);
    7772      5692644 :       gsi_next (&gsi);
    7773              :     }
    7774              : 
    7775     71809046 :   for (gimple_stmt_iterator gsi = gsi_start_bb (b);
    7776    275630439 :        !gsi_end_p (gsi);
    7777    239725916 :        gsi_next (&gsi))
    7778    239725916 :     eliminate_stmt (b, &gsi);
    7779              : 
    7780              :   /* Replace destination PHI arguments.  */
    7781     35904523 :   edge_iterator ei;
    7782     35904523 :   edge e;
    7783     84750027 :   FOR_EACH_EDGE (e, ei, b->succs)
    7784     48845504 :     if (e->flags & EDGE_EXECUTABLE)
    7785     48316053 :       for (gphi_iterator gsi = gsi_start_phis (e->dest);
    7786     77333862 :            !gsi_end_p (gsi);
    7787     29017809 :            gsi_next (&gsi))
    7788              :         {
    7789     29017809 :           gphi *phi = gsi.phi ();
    7790     29017809 :           use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
    7791     29017809 :           tree arg = USE_FROM_PTR (use_p);
    7792     48032863 :           if (TREE_CODE (arg) != SSA_NAME
    7793     29017809 :               || virtual_operand_p (arg))
    7794     19015054 :             continue;
    7795     10002755 :           tree sprime = eliminate_avail (b, arg);
    7796     20005510 :           if (sprime && may_propagate_copy (arg, sprime,
    7797     10002755 :                                             !(e->flags & EDGE_ABNORMAL)))
    7798      9990465 :             propagate_value (use_p, sprime);
    7799              :         }
    7800              : 
    7801     35904523 :   vn_context_bb = NULL;
    7802              : 
    7803     35904523 :   return NULL;
    7804              : }
    7805              : 
    7806              : /* Make no longer available leaders no longer available.  */
    7807              : 
    7808              : void
    7809     40642519 : eliminate_dom_walker::after_dom_children (basic_block)
    7810              : {
    7811     40642519 :   tree entry;
    7812     90183806 :   while ((entry = avail_stack.pop ()) != NULL_TREE)
    7813              :     {
    7814     49541287 :       tree valnum = VN_INFO (entry)->valnum;
    7815     49541287 :       tree old = avail[SSA_NAME_VERSION (valnum)];
    7816     49541287 :       if (old == entry)
    7817     49496809 :         avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
    7818              :       else
    7819        44478 :         avail[SSA_NAME_VERSION (valnum)] = entry;
    7820              :     }
    7821     40642519 : }
    7822              : 
    7823              : /* Remove queued stmts and perform delayed cleanups.  */
    7824              : 
    7825              : unsigned
    7826      6080766 : eliminate_dom_walker::eliminate_cleanup (bool region_p)
    7827              : {
    7828      6080766 :   statistics_counter_event (cfun, "Eliminated", eliminations);
    7829      6080766 :   statistics_counter_event (cfun, "Insertions", insertions);
    7830              : 
    7831              :   /* We cannot remove stmts during BB walk, especially not release SSA
    7832              :      names there as this confuses the VN machinery.  The stmts ending
    7833              :      up in to_remove are either stores or simple copies.
    7834              :      Remove stmts in reverse order to make debug stmt creation possible.  */
    7835     32982547 :   while (!to_remove.is_empty ())
    7836              :     {
    7837     14740193 :       bool do_release_defs = true;
    7838     14740193 :       gimple *stmt = to_remove.pop ();
    7839              : 
    7840              :       /* When we are value-numbering a region we do not require exit PHIs to
    7841              :          be present so we have to make sure to deal with uses outside of the
    7842              :          region of stmts that we thought are eliminated.
    7843              :          ??? Note we may be confused by uses in dead regions we didn't run
    7844              :          elimination on.  Rather than checking individual uses we accept
    7845              :          dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
    7846              :          contains such example).  */
    7847     14740193 :       if (region_p)
    7848              :         {
    7849      1705685 :           if (gphi *phi = dyn_cast <gphi *> (stmt))
    7850              :             {
    7851      1095179 :               tree lhs = gimple_phi_result (phi);
    7852      1095179 :               if (!has_zero_uses (lhs))
    7853              :                 {
    7854        22851 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    7855            3 :                     fprintf (dump_file, "Keeping eliminated stmt live "
    7856              :                              "as copy because of out-of-region uses\n");
    7857        22851 :                   tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
    7858        22851 :                   gimple *copy = gimple_build_assign (lhs, sprime);
    7859        22851 :                   gimple_stmt_iterator gsi
    7860        22851 :                     = gsi_after_labels (gimple_bb (stmt));
    7861        22851 :                   gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
    7862        22851 :                   do_release_defs = false;
    7863              :                 }
    7864              :             }
    7865       610506 :           else if (tree lhs = gimple_get_lhs (stmt))
    7866       610506 :             if (TREE_CODE (lhs) == SSA_NAME
    7867       610506 :                 && !has_zero_uses (lhs))
    7868              :               {
    7869         1655 :                 if (dump_file && (dump_flags & TDF_DETAILS))
    7870            0 :                   fprintf (dump_file, "Keeping eliminated stmt live "
    7871              :                            "as copy because of out-of-region uses\n");
    7872         1655 :                 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
    7873         1655 :                 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    7874         1655 :                 if (is_gimple_assign (stmt))
    7875              :                   {
    7876         1655 :                     gimple_assign_set_rhs_from_tree (&gsi, sprime);
    7877         1655 :                     stmt = gsi_stmt (gsi);
    7878         1655 :                     update_stmt (stmt);
    7879         1655 :                     if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
    7880            0 :                       bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
    7881         1655 :                     continue;
    7882              :                   }
    7883              :                 else
    7884              :                   {
    7885            0 :                     gimple *copy = gimple_build_assign (lhs, sprime);
    7886            0 :                     gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
    7887            0 :                     do_release_defs = false;
    7888              :                   }
    7889              :               }
    7890              :         }
    7891              : 
    7892     14738538 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7893              :         {
    7894        21671 :           fprintf (dump_file, "Removing dead stmt ");
    7895        21671 :           print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
    7896              :         }
    7897              : 
    7898     14738538 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    7899     14738538 :       if (gimple_code (stmt) == GIMPLE_PHI)
    7900      1694210 :         remove_phi_node (&gsi, do_release_defs);
    7901              :       else
    7902              :         {
    7903     13044328 :           basic_block bb = gimple_bb (stmt);
    7904     13044328 :           unlink_stmt_vdef (stmt);
    7905     13044328 :           if (gsi_remove (&gsi, true))
    7906        26543 :             bitmap_set_bit (need_eh_cleanup, bb->index);
    7907     13044328 :           if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
    7908            2 :             bitmap_set_bit (need_ab_cleanup, bb->index);
    7909     13044328 :           if (do_release_defs)
    7910     13044328 :             release_defs (stmt);
    7911              :         }
    7912              : 
    7913              :       /* Removing a stmt may expose a forwarder block.  */
    7914     14738538 :       el_todo |= TODO_cleanup_cfg;
    7915              :     }
    7916              : 
    7917              :   /* Fixup stmts that became noreturn calls.  This may require splitting
    7918              :      blocks and thus isn't possible during the dominator walk.  Do this
    7919              :      in reverse order so we don't inadvertently remove a stmt we want to
    7920              :      fixup by visiting a dominating now noreturn call first.  */
    7921      6080822 :   while (!to_fixup.is_empty ())
    7922              :     {
    7923           56 :       gimple *stmt = to_fixup.pop ();
    7924              : 
    7925           56 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7926              :         {
    7927            0 :           fprintf (dump_file, "Fixing up noreturn call ");
    7928            0 :           print_gimple_stmt (dump_file, stmt, 0);
    7929              :         }
    7930              : 
    7931           56 :       if (fixup_noreturn_call (stmt))
    7932           56 :         el_todo |= TODO_cleanup_cfg;
    7933              :     }
    7934              : 
    7935      6080766 :   bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
    7936      6080766 :   bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
    7937              : 
    7938      6080766 :   if (do_eh_cleanup)
    7939        10607 :     gimple_purge_all_dead_eh_edges (need_eh_cleanup);
    7940              : 
    7941      6080766 :   if (do_ab_cleanup)
    7942            2 :     gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
    7943              : 
    7944      6080766 :   if (do_eh_cleanup || do_ab_cleanup)
    7945        10609 :     el_todo |= TODO_cleanup_cfg;
    7946              : 
    7947      6080766 :   return el_todo;
    7948              : }
    7949              : 
    7950              : /* Eliminate fully redundant computations.  */
    7951              : 
    7952              : unsigned
    7953      4219037 : eliminate_with_rpo_vn (bitmap inserted_exprs)
    7954              : {
    7955      4219037 :   eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
    7956              : 
    7957      4219037 :   eliminate_dom_walker *saved_rpo_avail = rpo_avail;
    7958      4219037 :   rpo_avail = &walker;
    7959      4219037 :   walker.walk (cfun->cfg->x_entry_block_ptr);
    7960      4219037 :   rpo_avail = saved_rpo_avail;
    7961              : 
    7962      4219037 :   return walker.eliminate_cleanup ();
    7963      4219037 : }
    7964              : 
    7965              : static unsigned
    7966              : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
    7967              :              bool iterate, bool eliminate, bool skip_entry_phis,
    7968              :              vn_lookup_kind kind);
    7969              : 
    7970              : void
    7971       961530 : run_rpo_vn (vn_lookup_kind kind)
    7972              : {
    7973       961530 :   do_rpo_vn_1 (cfun, NULL, NULL, true, false, false, kind);
    7974              : 
    7975              :   /* ???  Prune requirement of these.  */
    7976       961530 :   constant_to_value_id = new hash_table<vn_constant_hasher> (23);
    7977              : 
    7978              :   /* Initialize the value ids and prune out remaining VN_TOPs
    7979              :      from dead code.  */
    7980       961530 :   tree name;
    7981       961530 :   unsigned i;
    7982     46364735 :   FOR_EACH_SSA_NAME (i, name, cfun)
    7983              :     {
    7984     33176775 :       vn_ssa_aux_t info = VN_INFO (name);
    7985     33176775 :       if (!info->visited
    7986     33103119 :           || info->valnum == VN_TOP)
    7987        73656 :         info->valnum = name;
    7988     33176775 :       if (info->valnum == name)
    7989     32051659 :         info->value_id = get_next_value_id ();
    7990      1125116 :       else if (is_gimple_min_invariant (info->valnum))
    7991        38578 :         info->value_id = get_or_alloc_constant_value_id (info->valnum);
    7992              :     }
    7993              : 
    7994              :   /* Propagate.  */
    7995     46364735 :   FOR_EACH_SSA_NAME (i, name, cfun)
    7996              :     {
    7997     33176775 :       vn_ssa_aux_t info = VN_INFO (name);
    7998     33176775 :       if (TREE_CODE (info->valnum) == SSA_NAME
    7999     33138197 :           && info->valnum != name
    8000     34263313 :           && info->value_id != VN_INFO (info->valnum)->value_id)
    8001      1086538 :         info->value_id = VN_INFO (info->valnum)->value_id;
    8002              :     }
    8003              : 
    8004       961530 :   set_hashtable_value_ids ();
    8005              : 
    8006       961530 :   if (dump_file && (dump_flags & TDF_DETAILS))
    8007              :     {
    8008           14 :       fprintf (dump_file, "Value numbers:\n");
    8009          406 :       FOR_EACH_SSA_NAME (i, name, cfun)
    8010              :         {
    8011          307 :           if (VN_INFO (name)->visited
    8012          307 :               && SSA_VAL (name) != name)
    8013              :             {
    8014           33 :               print_generic_expr (dump_file, name);
    8015           33 :               fprintf (dump_file, " = ");
    8016           33 :               print_generic_expr (dump_file, SSA_VAL (name));
    8017           33 :               fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
    8018              :             }
    8019              :         }
    8020              :     }
    8021       961530 : }
    8022              : 
    8023              : /* Free VN associated data structures.  */
    8024              : 
    8025              : void
    8026      6100358 : free_rpo_vn (void)
    8027              : {
    8028      6100358 :   free_vn_table (valid_info);
    8029      6100358 :   XDELETE (valid_info);
    8030      6100358 :   obstack_free (&vn_tables_obstack, NULL);
    8031      6100358 :   obstack_free (&vn_tables_insert_obstack, NULL);
    8032              : 
    8033      6100358 :   vn_ssa_aux_iterator_type it;
    8034      6100358 :   vn_ssa_aux_t info;
    8035    346590380 :   FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
    8036    170245011 :     if (info->needs_insertion)
    8037      4114709 :       release_ssa_name (info->name);
    8038      6100358 :   obstack_free (&vn_ssa_aux_obstack, NULL);
    8039      6100358 :   delete vn_ssa_aux_hash;
    8040              : 
    8041      6100358 :   delete constant_to_value_id;
    8042      6100358 :   constant_to_value_id = NULL;
    8043      6100358 : }
    8044              : 
    8045              : /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables.  */
    8046              : 
    8047              : static tree
    8048     22972784 : vn_lookup_simplify_result (gimple_match_op *res_op)
    8049              : {
    8050     22972784 :   if (!res_op->code.is_tree_code ())
    8051              :     return NULL_TREE;
    8052     22969616 :   tree *ops = res_op->ops;
    8053     22969616 :   unsigned int length = res_op->num_ops;
    8054     22969616 :   if (res_op->code == CONSTRUCTOR
    8055              :       /* ???  We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
    8056              :          and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree.  */
    8057     22969616 :       && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
    8058              :     {
    8059         1018 :       length = CONSTRUCTOR_NELTS (res_op->ops[0]);
    8060         1018 :       ops = XALLOCAVEC (tree, length);
    8061         4590 :       for (unsigned i = 0; i < length; ++i)
    8062         3572 :         ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
    8063              :     }
    8064     22969616 :   vn_nary_op_t vnresult = NULL;
    8065     22969616 :   tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
    8066              :                                        res_op->type, ops, &vnresult);
    8067              :   /* If this is used from expression simplification make sure to
    8068              :      return an available expression.  */
    8069     22969616 :   if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
    8070      2226008 :     res = rpo_avail->eliminate_avail (vn_context_bb, res);
    8071              :   return res;
    8072              : }
    8073              : 
    8074              : /* Return a leader for OPs value that is valid at BB.  */
    8075              : 
    8076              : tree
    8077    265321792 : rpo_elim::eliminate_avail (basic_block bb, tree op)
    8078              : {
    8079    265321792 :   bool visited;
    8080    265321792 :   tree valnum = SSA_VAL (op, &visited);
    8081              :   /* If we didn't visit OP then it must be defined outside of the
    8082              :      region we process and also dominate it.  So it is available.  */
    8083    265321792 :   if (!visited)
    8084              :     return op;
    8085    263181316 :   if (TREE_CODE (valnum) == SSA_NAME)
    8086              :     {
    8087    249204151 :       if (SSA_NAME_IS_DEFAULT_DEF (valnum))
    8088              :         return valnum;
    8089    242571692 :       vn_ssa_aux_t valnum_info = VN_INFO (valnum);
    8090    242571692 :       vn_avail *av = valnum_info->avail;
    8091    242571692 :       if (!av)
    8092              :         {
    8093              :           /* See above.  But when there's availability info prefer
    8094              :              what we recorded there for example to preserve LC SSA.  */
    8095     82858401 :           if (!valnum_info->visited)
    8096              :             return valnum;
    8097              :           return NULL_TREE;
    8098              :         }
    8099    159713291 :       if (av->location == bb->index)
    8100              :         /* On tramp3d 90% of the cases are here.  */
    8101    105499394 :         return ssa_name (av->leader);
    8102     68165134 :       do
    8103              :         {
    8104     68165134 :           basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
    8105              :           /* ???  During elimination we have to use availability at the
    8106              :              definition site of a use we try to replace.  This
    8107              :              is required to not run into inconsistencies because
    8108              :              of dominated_by_p_w_unex behavior and removing a definition
    8109              :              while not replacing all uses.
    8110              :              ???  We could try to consistently walk dominators
    8111              :              ignoring non-executable regions.  The nearest common
    8112              :              dominator of bb and abb is where we can stop walking.  We
    8113              :              may also be able to "pre-compute" (bits of) the next immediate
    8114              :              (non-)dominator during the RPO walk when marking edges as
    8115              :              executable.  */
    8116     68165134 :           if (dominated_by_p_w_unex (bb, abb, true))
    8117              :             {
    8118     50378088 :               tree leader = ssa_name (av->leader);
    8119              :               /* Prevent eliminations that break loop-closed SSA.  */
    8120     50378088 :               if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
    8121      3138458 :                   && ! SSA_NAME_IS_DEFAULT_DEF (leader)
    8122     53516546 :                   && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
    8123      3138458 :                                                          (leader))->loop_father,
    8124              :                                               bb))
    8125              :                 return NULL_TREE;
    8126     50299064 :               if (dump_file && (dump_flags & TDF_DETAILS))
    8127              :                 {
    8128         3543 :                   print_generic_expr (dump_file, leader);
    8129         3543 :                   fprintf (dump_file, " is available for ");
    8130         3543 :                   print_generic_expr (dump_file, valnum);
    8131         3543 :                   fprintf (dump_file, "\n");
    8132              :                 }
    8133              :               /* On tramp3d 99% of the _remaining_ cases succeed at
    8134              :                  the first enty.  */
    8135     50299064 :               return leader;
    8136              :             }
    8137              :           /* ???  Can we somehow skip to the immediate dominator
    8138              :              RPO index (bb_to_rpo)?  Again, maybe not worth, on
    8139              :              tramp3d the worst number of elements in the vector is 9.  */
    8140     17787046 :           av = av->next;
    8141              :         }
    8142     17787046 :       while (av);
    8143              :       /* While we prefer avail we have to fallback to using the value
    8144              :          directly if defined outside of the region when none of the
    8145              :          available defs suit.  */
    8146      3835809 :       if (!valnum_info->visited)
    8147              :         return valnum;
    8148              :     }
    8149     13977165 :   else if (valnum != VN_TOP)
    8150              :     /* valnum is is_gimple_min_invariant.  */
    8151              :     return valnum;
    8152              :   return NULL_TREE;
    8153              : }
    8154              : 
    8155              : /* Make LEADER a leader for its value at BB.  */
    8156              : 
    8157              : void
    8158     96104149 : rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
    8159              : {
    8160     96104149 :   tree valnum = VN_INFO (leader)->valnum;
    8161     96104149 :   if (valnum == VN_TOP
    8162     96104149 :       || is_gimple_min_invariant (valnum))
    8163            0 :     return;
    8164     96104149 :   if (dump_file && (dump_flags & TDF_DETAILS))
    8165              :     {
    8166       324987 :       fprintf (dump_file, "Making available beyond BB%d ", bb->index);
    8167       324987 :       print_generic_expr (dump_file, leader);
    8168       324987 :       fprintf (dump_file, " for value ");
    8169       324987 :       print_generic_expr (dump_file, valnum);
    8170       324987 :       fprintf (dump_file, "\n");
    8171              :     }
    8172     96104149 :   vn_ssa_aux_t value = VN_INFO (valnum);
    8173     96104149 :   vn_avail *av;
    8174     96104149 :   if (m_avail_freelist)
    8175              :     {
    8176     18699374 :       av = m_avail_freelist;
    8177     18699374 :       m_avail_freelist = m_avail_freelist->next;
    8178              :     }
    8179              :   else
    8180     77404775 :     av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
    8181     96104149 :   av->location = bb->index;
    8182     96104149 :   av->leader = SSA_NAME_VERSION (leader);
    8183     96104149 :   av->next = value->avail;
    8184     96104149 :   av->next_undo = last_pushed_avail;
    8185     96104149 :   last_pushed_avail = value;
    8186     96104149 :   value->avail = av;
    8187              : }
    8188              : 
    8189              : /* Valueization hook for RPO VN plus required state.  */
    8190              : 
    8191              : tree
    8192   2049807970 : rpo_vn_valueize (tree name)
    8193              : {
    8194   2049807970 :   if (TREE_CODE (name) == SSA_NAME)
    8195              :     {
    8196   2004405225 :       vn_ssa_aux_t val = VN_INFO (name);
    8197   2004405225 :       if (val)
    8198              :         {
    8199   2004405225 :           tree tem = val->valnum;
    8200   2004405225 :           if (tem != VN_TOP && tem != name)
    8201              :             {
    8202    107674160 :               if (TREE_CODE (tem) != SSA_NAME)
    8203              :                 return tem;
    8204              :               /* For all values we only valueize to an available leader
    8205              :                  which means we can use SSA name info without restriction.  */
    8206     90787827 :               tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
    8207     90787827 :               if (tem)
    8208              :                 return tem;
    8209              :             }
    8210              :         }
    8211              :     }
    8212              :   return name;
    8213              : }
    8214              : 
    8215              : /* Insert on PRED_E predicates derived from CODE OPS being true besides the
    8216              :    inverted condition.  */
    8217              : 
    8218              : static void
    8219     27139756 : insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
    8220              : {
    8221     27139756 :   switch (code)
    8222              :     {
    8223      1366941 :     case LT_EXPR:
    8224              :       /* a < b -> a {!,<}= b */
    8225      1366941 :       vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
    8226              :                                            ops, boolean_true_node, 0, pred_e);
    8227      1366941 :       vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
    8228              :                                            ops, boolean_true_node, 0, pred_e);
    8229              :       /* a < b -> ! a {>,=} b */
    8230      1366941 :       vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
    8231              :                                            ops, boolean_false_node, 0, pred_e);
    8232      1366941 :       vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
    8233              :                                            ops, boolean_false_node, 0, pred_e);
    8234      1366941 :       break;
    8235      3380285 :     case GT_EXPR:
    8236              :       /* a > b -> a {!,>}= b */
    8237      3380285 :       vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
    8238              :                                            ops, boolean_true_node, 0, pred_e);
    8239      3380285 :       vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
    8240              :                                            ops, boolean_true_node, 0, pred_e);
    8241              :       /* a > b -> ! a {<,=} b */
    8242      3380285 :       vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
    8243              :                                            ops, boolean_false_node, 0, pred_e);
    8244      3380285 :       vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
    8245              :                                            ops, boolean_false_node, 0, pred_e);
    8246      3380285 :       break;
    8247      9307827 :     case EQ_EXPR:
    8248              :       /* a == b -> ! a {<,>} b */
    8249      9307827 :       vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
    8250              :                                            ops, boolean_false_node, 0, pred_e);
    8251      9307827 :       vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
    8252              :                                            ops, boolean_false_node, 0, pred_e);
    8253      9307827 :       break;
    8254              :     case LE_EXPR:
    8255              :     case GE_EXPR:
    8256              :     case NE_EXPR:
    8257              :       /* Nothing besides inverted condition.  */
    8258              :       break;
    8259     27139756 :     default:;
    8260              :     }
    8261     27139756 : }
    8262              : 
    8263              : /* Insert on the TRUE_E true and FALSE_E false predicates
    8264              :    derived from LHS CODE RHS.  */
    8265              : 
    8266              : static void
    8267     23109193 : insert_predicates_for_cond (tree_code code, tree lhs, tree rhs,
    8268              :                             edge true_e, edge false_e)
    8269              : {
    8270              :   /* If both edges are null, then there is nothing to be done. */
    8271     23109193 :   if (!true_e && !false_e)
    8272      1299102 :     return;
    8273              : 
    8274              :   /* Canonicalize the comparison if needed, putting
    8275              :      the constant in the rhs.  */
    8276     21813582 :   if (tree_swap_operands_p (lhs, rhs))
    8277              :     {
    8278        16629 :       std::swap (lhs, rhs);
    8279        16629 :       code = swap_tree_comparison (code);
    8280              :     }
    8281              : 
    8282              :   /* If the lhs is not a ssa name, don't record anything. */
    8283     21813582 :   if (TREE_CODE (lhs) != SSA_NAME)
    8284              :     return;
    8285              : 
    8286     21810091 :   tree_code icode = invert_tree_comparison (code, HONOR_NANS (lhs));
    8287     21810091 :   tree ops[2];
    8288     21810091 :   ops[0] = lhs;
    8289     21810091 :   ops[1] = rhs;
    8290     21810091 :   if (true_e)
    8291     17800209 :     vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
    8292              :                                          boolean_true_node, 0, true_e);
    8293     21810091 :   if (false_e)
    8294     16791623 :     vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
    8295              :                                          boolean_false_node, 0, false_e);
    8296     21810091 :   if (icode != ERROR_MARK)
    8297              :     {
    8298     21563065 :       if (true_e)
    8299     17646055 :         vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
    8300              :                                              boolean_false_node, 0, true_e);
    8301     21563065 :       if (false_e)
    8302     16592023 :         vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
    8303              :                                              boolean_true_node, 0, false_e);
    8304              :     }
    8305              :   /* Relax for non-integers, inverted condition handled
    8306              :      above.  */
    8307     21810091 :   if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
    8308              :     {
    8309     17095483 :       if (true_e)
    8310     14029476 :         insert_related_predicates_on_edge (code, ops, true_e);
    8311     17095483 :       if (false_e)
    8312     13110280 :         insert_related_predicates_on_edge (icode, ops, false_e);
    8313              :   }
    8314     21810091 :   if (integer_zerop (rhs)
    8315     21810091 :       && (code == NE_EXPR || code == EQ_EXPR))
    8316              :     {
    8317      9108706 :       gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
    8318              :       /* (A CMP B) != 0 is the same as (A CMP B).
    8319              :          (A CMP B) == 0 is just (A CMP B) with the edges swapped.  */
    8320      9108706 :       if (is_gimple_assign (def_stmt)
    8321      9108706 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_comparison)
    8322              :           {
    8323       432236 :             tree_code nc = gimple_assign_rhs_code (def_stmt);
    8324       432236 :             tree nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
    8325       432236 :             tree nrhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
    8326       432236 :             edge nt = true_e;
    8327       432236 :             edge nf = false_e;
    8328       432236 :             if (code == EQ_EXPR)
    8329       306245 :               std::swap (nt, nf);
    8330       432236 :             if (lhs != nlhs)
    8331       432236 :               insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
    8332              :           }
    8333              :       /* (a | b) == 0 ->
    8334              :             on true edge assert: a == 0 & b == 0. */
    8335              :       /* (a | b) != 0 ->
    8336              :             on false edge assert: a == 0 & b == 0. */
    8337      9108706 :       if (is_gimple_assign (def_stmt)
    8338      9108706 :           && gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
    8339              :         {
    8340       255743 :           edge e = code == EQ_EXPR ? true_e : false_e;
    8341       255743 :           tree nlhs;
    8342              : 
    8343       255743 :           nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
    8344              :           /* A valueization of the `a` might return the old lhs
    8345              :              which is already handled above. */
    8346       255743 :           if (nlhs != lhs)
    8347       255743 :             insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
    8348              : 
    8349              :           /* A valueization of the `b` might return the old lhs
    8350              :              which is already handled above. */
    8351       255743 :           nlhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
    8352       255743 :           if (nlhs != lhs)
    8353       255743 :             insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
    8354              :         }
    8355              :     }
    8356              : }
    8357              : 
    8358              : /* Main stmt worker for RPO VN, process BB.  */
    8359              : 
    8360              : static unsigned
    8361     60715359 : process_bb (rpo_elim &avail, basic_block bb,
    8362              :             bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
    8363              :             bool do_region, bitmap exit_bbs, bool skip_phis)
    8364              : {
    8365     60715359 :   unsigned todo = 0;
    8366     60715359 :   edge_iterator ei;
    8367     60715359 :   edge e;
    8368              : 
    8369     60715359 :   vn_context_bb = bb;
    8370              : 
    8371              :   /* If we are in loop-closed SSA preserve this state.  This is
    8372              :      relevant when called on regions from outside of FRE/PRE.  */
    8373     60715359 :   bool lc_phi_nodes = false;
    8374     60715359 :   if (!skip_phis
    8375     60715359 :       && loops_state_satisfies_p (LOOP_CLOSED_SSA))
    8376      3699118 :     FOR_EACH_EDGE (e, ei, bb->preds)
    8377      2234007 :       if (e->src->loop_father != e->dest->loop_father
    8378      2234007 :           && flow_loop_nested_p (e->dest->loop_father,
    8379              :                                  e->src->loop_father))
    8380              :         {
    8381              :           lc_phi_nodes = true;
    8382              :           break;
    8383              :         }
    8384              : 
    8385              :   /* When we visit a loop header substitute into loop info.  */
    8386     60715359 :   if (!iterate && eliminate && bb->loop_father->header == bb)
    8387              :     {
    8388              :       /* Keep fields in sync with substitute_in_loop_info.  */
    8389       943179 :       if (bb->loop_father->nb_iterations)
    8390       156824 :         bb->loop_father->nb_iterations
    8391       156824 :           = simplify_replace_tree (bb->loop_father->nb_iterations,
    8392              :                                    NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
    8393              :     }
    8394              : 
    8395              :   /* Value-number all defs in the basic-block.  */
    8396     60715359 :   if (!skip_phis)
    8397     87182825 :     for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
    8398     26496719 :          gsi_next (&gsi))
    8399              :       {
    8400     26496719 :         gphi *phi = gsi.phi ();
    8401     26496719 :         tree res = PHI_RESULT (phi);
    8402     26496719 :         vn_ssa_aux_t res_info = VN_INFO (res);
    8403     26496719 :         if (!bb_visited)
    8404              :           {
    8405     18652350 :             gcc_assert (!res_info->visited);
    8406     18652350 :             res_info->valnum = VN_TOP;
    8407     18652350 :             res_info->visited = true;
    8408              :           }
    8409              : 
    8410              :         /* When not iterating force backedge values to varying.  */
    8411     26496719 :         visit_stmt (phi, !iterate_phis);
    8412     52993438 :         if (virtual_operand_p (res))
    8413     10475332 :           continue;
    8414              : 
    8415              :         /* Eliminate */
    8416              :         /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
    8417              :            how we handle backedges and availability.
    8418              :            And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization.  */
    8419     16021387 :         tree val = res_info->valnum;
    8420     16021387 :         if (res != val && !iterate && eliminate)
    8421              :           {
    8422      1389302 :             if (tree leader = avail.eliminate_avail (bb, res))
    8423              :               {
    8424      1266394 :                 if (leader != res
    8425              :                     /* Preserve loop-closed SSA form.  */
    8426      1266394 :                     && (! lc_phi_nodes
    8427         6404 :                         || is_gimple_min_invariant (leader)))
    8428              :                   {
    8429      1265823 :                     if (dump_file && (dump_flags & TDF_DETAILS))
    8430              :                       {
    8431          209 :                         fprintf (dump_file, "Replaced redundant PHI node "
    8432              :                                  "defining ");
    8433          209 :                         print_generic_expr (dump_file, res);
    8434          209 :                         fprintf (dump_file, " with ");
    8435          209 :                         print_generic_expr (dump_file, leader);
    8436          209 :                         fprintf (dump_file, "\n");
    8437              :                       }
    8438      1265823 :                     avail.eliminations++;
    8439              : 
    8440      1265823 :                     if (may_propagate_copy (res, leader))
    8441              :                       {
    8442              :                         /* Schedule for removal.  */
    8443      1265823 :                         avail.to_remove.safe_push (phi);
    8444      1265823 :                         continue;
    8445              :                       }
    8446              :                     /* ???  Else generate a copy stmt.  */
    8447              :                   }
    8448              :               }
    8449              :           }
    8450              :         /* Only make defs available that not already are.  But make
    8451              :            sure loop-closed SSA PHI node defs are picked up for
    8452              :            downstream uses.  */
    8453     14755564 :         if (lc_phi_nodes
    8454     14755564 :             || res == val
    8455     14755564 :             || ! avail.eliminate_avail (bb, res))
    8456     11268054 :           avail.eliminate_push_avail (bb, res);
    8457              :       }
    8458              : 
    8459              :   /* For empty BBs mark outgoing edges executable.  For non-empty BBs
    8460              :      we do this when processing the last stmt as we have to do this
    8461              :      before elimination which otherwise forces GIMPLE_CONDs to
    8462              :      if (1 != 0) style when seeing non-executable edges.  */
    8463    121430718 :   if (gsi_end_p (gsi_start_bb (bb)))
    8464              :     {
    8465     13912958 :       FOR_EACH_EDGE (e, ei, bb->succs)
    8466              :         {
    8467      6956479 :           if (!(e->flags & EDGE_EXECUTABLE))
    8468              :             {
    8469      4721619 :               if (dump_file && (dump_flags & TDF_DETAILS))
    8470         6194 :                 fprintf (dump_file,
    8471              :                          "marking outgoing edge %d -> %d executable\n",
    8472         6194 :                          e->src->index, e->dest->index);
    8473      4721619 :               e->flags |= EDGE_EXECUTABLE;
    8474      4721619 :               e->dest->flags |= BB_EXECUTABLE;
    8475              :             }
    8476      2234860 :           else if (!(e->dest->flags & BB_EXECUTABLE))
    8477              :             {
    8478            0 :               if (dump_file && (dump_flags & TDF_DETAILS))
    8479            0 :                 fprintf (dump_file,
    8480              :                          "marking destination block %d reachable\n",
    8481              :                          e->dest->index);
    8482            0 :               e->dest->flags |= BB_EXECUTABLE;
    8483              :             }
    8484              :         }
    8485              :     }
    8486    121430718 :   for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
    8487    480810747 :        !gsi_end_p (gsi); gsi_next (&gsi))
    8488              :     {
    8489    420095388 :       ssa_op_iter i;
    8490    420095388 :       tree op;
    8491    420095388 :       if (!bb_visited)
    8492              :         {
    8493    480465286 :           FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
    8494              :             {
    8495    136100653 :               vn_ssa_aux_t op_info = VN_INFO (op);
    8496    136100653 :               gcc_assert (!op_info->visited);
    8497    136100653 :               op_info->valnum = VN_TOP;
    8498    136100653 :               op_info->visited = true;
    8499              :             }
    8500              : 
    8501              :           /* We somehow have to deal with uses that are not defined
    8502              :              in the processed region.  Forcing unvisited uses to
    8503              :              varying here doesn't play well with def-use following during
    8504              :              expression simplification, so we deal with this by checking
    8505              :              the visited flag in SSA_VAL.  */
    8506              :         }
    8507              : 
    8508    420095388 :       visit_stmt (gsi_stmt (gsi));
    8509              : 
    8510    420095388 :       gimple *last = gsi_stmt (gsi);
    8511    420095388 :       e = NULL;
    8512    420095388 :       switch (gimple_code (last))
    8513              :         {
    8514       113471 :         case GIMPLE_SWITCH:
    8515       113471 :           e = find_taken_edge (bb, vn_valueize (gimple_switch_index
    8516       113471 :                                                 (as_a <gswitch *> (last))));
    8517       113471 :           break;
    8518     24359798 :         case GIMPLE_COND:
    8519     24359798 :           {
    8520     24359798 :             tree lhs = vn_valueize (gimple_cond_lhs (last));
    8521     24359798 :             tree rhs = vn_valueize (gimple_cond_rhs (last));
    8522     24359798 :             tree_code cmpcode = gimple_cond_code (last);
    8523              :             /* Canonicalize the comparison if needed, putting
    8524              :                the constant in the rhs.  */
    8525     24359798 :             if (tree_swap_operands_p (lhs, rhs))
    8526              :               {
    8527       827863 :                 std::swap (lhs, rhs);
    8528       827863 :                 cmpcode = swap_tree_comparison (cmpcode);
    8529              :                }
    8530     24359798 :             tree val = gimple_simplify (cmpcode,
    8531              :                                         boolean_type_node, lhs, rhs,
    8532              :                                         NULL, vn_valueize);
    8533              :             /* If the condition didn't simplify see if we have recorded
    8534              :                an expression from sofar taken edges.  */
    8535     24359798 :             if (! val || TREE_CODE (val) != INTEGER_CST)
    8536              :               {
    8537     22515883 :                 vn_nary_op_t vnresult;
    8538     22515883 :                 tree ops[2];
    8539     22515883 :                 ops[0] = lhs;
    8540     22515883 :                 ops[1] = rhs;
    8541     22515883 :                 val = vn_nary_op_lookup_pieces (2, cmpcode,
    8542              :                                                 boolean_type_node, ops,
    8543              :                                                 &vnresult);
    8544              :                 /* Got back a ssa name, then try looking up `val != 0`
    8545              :                    as it might have been recorded that way.  */
    8546     22515883 :                 if (val && TREE_CODE (val) == SSA_NAME)
    8547              :                   {
    8548       154465 :                     ops[0] = val;
    8549       154465 :                     ops[1] = build_zero_cst (TREE_TYPE (val));
    8550       154465 :                     val = vn_nary_op_lookup_pieces (2, NE_EXPR,
    8551              :                                                     boolean_type_node, ops,
    8552              :                                                     &vnresult);
    8553              :                   }
    8554              :                 /* Did we get a predicated value?  */
    8555     22515867 :                 if (! val && vnresult && vnresult->predicated_values)
    8556              :                   {
    8557      1353748 :                     val = vn_nary_op_get_predicated_value (vnresult, bb);
    8558      1353748 :                     if (val && dump_file && (dump_flags & TDF_DETAILS))
    8559              :                       {
    8560            2 :                         fprintf (dump_file, "Got predicated value ");
    8561            2 :                         print_generic_expr (dump_file, val, TDF_NONE);
    8562            2 :                         fprintf (dump_file, " for ");
    8563            2 :                         print_gimple_stmt (dump_file, last, TDF_SLIM);
    8564              :                       }
    8565              :                   }
    8566              :               }
    8567     22515883 :             if (val)
    8568      2194327 :               e = find_taken_edge (bb, val);
    8569     24359798 :             if (! e)
    8570              :               {
    8571              :                 /* If we didn't manage to compute the taken edge then
    8572              :                    push predicated expressions for the condition itself
    8573              :                    and related conditions to the hashtables.  This allows
    8574              :                    simplification of redundant conditions which is
    8575              :                    important as early cleanup.  */
    8576     22165471 :                 edge true_e, false_e;
    8577     22165471 :                 extract_true_false_edges_from_block (bb, &true_e, &false_e);
    8578       539611 :                 if ((do_region && bitmap_bit_p (exit_bbs, true_e->dest->index))
    8579     22395574 :                     || !can_track_predicate_on_edge (true_e))
    8580      4876482 :                   true_e = NULL;
    8581       539611 :                 if ((do_region && bitmap_bit_p (exit_bbs, false_e->dest->index))
    8582     22368981 :                     || !can_track_predicate_on_edge (false_e))
    8583      5768307 :                   false_e = NULL;
    8584     22165471 :                 insert_predicates_for_cond (cmpcode, lhs, rhs, true_e, false_e);
    8585              :               }
    8586              :             break;
    8587              :           }
    8588         1436 :         case GIMPLE_GOTO:
    8589         1436 :           e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
    8590         1436 :           break;
    8591              :         default:
    8592              :           e = NULL;
    8593              :         }
    8594    420095388 :       if (e)
    8595              :         {
    8596      2197937 :           todo = TODO_cleanup_cfg;
    8597      2197937 :           if (!(e->flags & EDGE_EXECUTABLE))
    8598              :             {
    8599      1734957 :               if (dump_file && (dump_flags & TDF_DETAILS))
    8600           35 :                 fprintf (dump_file,
    8601              :                          "marking known outgoing %sedge %d -> %d executable\n",
    8602           35 :                          e->flags & EDGE_DFS_BACK ? "back-" : "",
    8603           35 :                          e->src->index, e->dest->index);
    8604      1734957 :               e->flags |= EDGE_EXECUTABLE;
    8605      1734957 :               e->dest->flags |= BB_EXECUTABLE;
    8606              :             }
    8607       462980 :           else if (!(e->dest->flags & BB_EXECUTABLE))
    8608              :             {
    8609        27286 :               if (dump_file && (dump_flags & TDF_DETAILS))
    8610            1 :                 fprintf (dump_file,
    8611              :                          "marking destination block %d reachable\n",
    8612              :                          e->dest->index);
    8613        27286 :               e->dest->flags |= BB_EXECUTABLE;
    8614              :             }
    8615              :         }
    8616    835794902 :       else if (gsi_one_before_end_p (gsi))
    8617              :         {
    8618    126508112 :           FOR_EACH_EDGE (e, ei, bb->succs)
    8619              :             {
    8620     74947169 :               if (!(e->flags & EDGE_EXECUTABLE))
    8621              :                 {
    8622     54886833 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    8623        18580 :                     fprintf (dump_file,
    8624              :                              "marking outgoing edge %d -> %d executable\n",
    8625        18580 :                              e->src->index, e->dest->index);
    8626     54886833 :                   e->flags |= EDGE_EXECUTABLE;
    8627     54886833 :                   e->dest->flags |= BB_EXECUTABLE;
    8628              :                 }
    8629     20060336 :               else if (!(e->dest->flags & BB_EXECUTABLE))
    8630              :                 {
    8631      2491552 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    8632         5995 :                     fprintf (dump_file,
    8633              :                              "marking destination block %d reachable\n",
    8634              :                              e->dest->index);
    8635      2491552 :                   e->dest->flags |= BB_EXECUTABLE;
    8636              :                 }
    8637              :             }
    8638              :         }
    8639              : 
    8640              :       /* Eliminate.  That also pushes to avail.  */
    8641    420095388 :       if (eliminate && ! iterate)
    8642    104956735 :         avail.eliminate_stmt (bb, &gsi);
    8643              :       else
    8644              :         /* If not eliminating, make all not already available defs
    8645              :            available.  But avoid picking up dead defs.  */
    8646    394521867 :         FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
    8647     79383214 :           if (! has_zero_uses (op)
    8648     79383214 :               && ! avail.eliminate_avail (bb, op))
    8649     60446282 :             avail.eliminate_push_avail (bb, op);
    8650              :     }
    8651              : 
    8652              :   /* Eliminate in destination PHI arguments.  Always substitute in dest
    8653              :      PHIs, even for non-executable edges.  This handles region
    8654              :      exits PHIs.  */
    8655     60715359 :   if (!iterate && eliminate)
    8656     32602739 :     FOR_EACH_EDGE (e, ei, bb->succs)
    8657     19407845 :       for (gphi_iterator gsi = gsi_start_phis (e->dest);
    8658     37554162 :            !gsi_end_p (gsi); gsi_next (&gsi))
    8659              :         {
    8660     18146317 :           gphi *phi = gsi.phi ();
    8661     18146317 :           use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
    8662     18146317 :           tree arg = USE_FROM_PTR (use_p);
    8663     27617192 :           if (TREE_CODE (arg) != SSA_NAME
    8664     18146317 :               || virtual_operand_p (arg))
    8665      9470875 :             continue;
    8666      8675442 :           tree sprime;
    8667      8675442 :           if (SSA_NAME_IS_DEFAULT_DEF (arg))
    8668              :             {
    8669       117566 :               sprime = SSA_VAL (arg);
    8670       117566 :               gcc_assert (TREE_CODE (sprime) != SSA_NAME
    8671              :                           || SSA_NAME_IS_DEFAULT_DEF (sprime));
    8672              :             }
    8673              :           else
    8674              :             /* Look for sth available at the definition block of the argument.
    8675              :                This avoids inconsistencies between availability there which
    8676              :                decides if the stmt can be removed and availability at the
    8677              :                use site.  The SSA property ensures that things available
    8678              :                at the definition are also available at uses.  */
    8679      8557876 :             sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
    8680              :                                             arg);
    8681      8675442 :           if (sprime
    8682      8675442 :               && sprime != arg
    8683      8675442 :               && may_propagate_copy (arg, sprime, !(e->flags & EDGE_ABNORMAL)))
    8684      1499819 :             propagate_value (use_p, sprime);
    8685              :         }
    8686              : 
    8687     60715359 :   vn_context_bb = NULL;
    8688     60715359 :   return todo;
    8689              : }
    8690              : 
    8691              : /* Unwind state per basic-block.  */
    8692              : 
    8693              : struct unwind_state
    8694              : {
    8695              :   /* Times this block has been visited.  */
    8696              :   unsigned visited;
    8697              :   /* Whether to handle this as iteration point or whether to treat
    8698              :      incoming backedge PHI values as varying.  */
    8699              :   bool iterate;
    8700              :   /* Maximum RPO index this block is reachable from.  */
    8701              :   int max_rpo;
    8702              :   /* Unwind state.  */
    8703              :   void *ob_top;
    8704              :   vn_reference_t ref_top;
    8705              :   vn_phi_t phi_top;
    8706              :   vn_nary_op_t nary_top;
    8707              :   vn_avail *avail_top;
    8708              : };
    8709              : 
    8710              : /* Unwind the RPO VN state for iteration.  */
    8711              : 
    8712              : static void
    8713      1891745 : do_unwind (unwind_state *to, rpo_elim &avail)
    8714              : {
    8715      1891745 :   gcc_assert (to->iterate);
    8716     34615811 :   for (; last_inserted_nary != to->nary_top;
    8717     32724066 :        last_inserted_nary = last_inserted_nary->next)
    8718              :     {
    8719     32724066 :       vn_nary_op_t *slot;
    8720     32724066 :       slot = valid_info->nary->find_slot_with_hash
    8721     32724066 :         (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
    8722              :       /* Predication causes the need to restore previous state.  */
    8723     32724066 :       if ((*slot)->unwind_to)
    8724      6611435 :         *slot = (*slot)->unwind_to;
    8725              :       else
    8726     26112631 :         valid_info->nary->clear_slot (slot);
    8727              :     }
    8728      7452329 :   for (; last_inserted_phi != to->phi_top;
    8729      5560584 :        last_inserted_phi = last_inserted_phi->next)
    8730              :     {
    8731      5560584 :       vn_phi_t *slot;
    8732      5560584 :       slot = valid_info->phis->find_slot_with_hash
    8733      5560584 :         (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
    8734      5560584 :       valid_info->phis->clear_slot (slot);
    8735              :     }
    8736     15117738 :   for (; last_inserted_ref != to->ref_top;
    8737     13225993 :        last_inserted_ref = last_inserted_ref->next)
    8738              :     {
    8739     13225993 :       vn_reference_t *slot;
    8740     13225993 :       slot = valid_info->references->find_slot_with_hash
    8741     13225993 :         (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
    8742     13225993 :       (*slot)->operands.release ();
    8743     13225993 :       valid_info->references->clear_slot (slot);
    8744              :     }
    8745      1891745 :   obstack_free (&vn_tables_obstack, to->ob_top);
    8746              : 
    8747              :   /* Prune [rpo_idx, ] from avail.  */
    8748     20591119 :   for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
    8749              :     {
    8750     18699374 :       vn_ssa_aux_t val = last_pushed_avail;
    8751     18699374 :       vn_avail *av = val->avail;
    8752     18699374 :       val->avail = av->next;
    8753     18699374 :       last_pushed_avail = av->next_undo;
    8754     18699374 :       av->next = avail.m_avail_freelist;
    8755     18699374 :       avail.m_avail_freelist = av;
    8756              :     }
    8757      1891745 : }
    8758              : 
    8759              : /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
    8760              :    If ITERATE is true then treat backedges optimistically as not
    8761              :    executed and iterate.  If ELIMINATE is true then perform
    8762              :    elimination, otherwise leave that to the caller.  If SKIP_ENTRY_PHIS
    8763              :    is true then force PHI nodes in ENTRY->dest to VARYING.  */
    8764              : 
    8765              : static unsigned
    8766      6100358 : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
    8767              :              bool iterate, bool eliminate, bool skip_entry_phis,
    8768              :              vn_lookup_kind kind)
    8769              : {
    8770      6100358 :   unsigned todo = 0;
    8771      6100358 :   default_vn_walk_kind = kind;
    8772              : 
    8773              :   /* We currently do not support region-based iteration when
    8774              :      elimination is requested.  */
    8775      6100358 :   gcc_assert (!entry || !iterate || !eliminate);
    8776              :   /* When iterating we need loop info up-to-date.  */
    8777      6100358 :   gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
    8778              : 
    8779      6100358 :   bool do_region = entry != NULL;
    8780      6100358 :   if (!do_region)
    8781              :     {
    8782      5417483 :       entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
    8783      5417483 :       exit_bbs = BITMAP_ALLOC (NULL);
    8784      5417483 :       bitmap_set_bit (exit_bbs, EXIT_BLOCK);
    8785              :     }
    8786              : 
    8787              :   /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
    8788              :      re-mark those that are contained in the region.  */
    8789      6100358 :   edge_iterator ei;
    8790      6100358 :   edge e;
    8791     12260945 :   FOR_EACH_EDGE (e, ei, entry->dest->preds)
    8792      6160587 :     e->flags &= ~EDGE_DFS_BACK;
    8793              : 
    8794      6100358 :   int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
    8795      6100358 :   auto_vec<std::pair<int, int> > toplevel_scc_extents;
    8796      6100358 :   int n = rev_post_order_and_mark_dfs_back_seme
    8797      7981679 :     (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
    8798              : 
    8799      6100358 :   if (!do_region)
    8800      5417483 :     BITMAP_FREE (exit_bbs);
    8801              : 
    8802              :   /* If there are any non-DFS_BACK edges into entry->dest skip
    8803              :      processing PHI nodes for that block.  This supports
    8804              :      value-numbering loop bodies w/o the actual loop.  */
    8805     12260944 :   FOR_EACH_EDGE (e, ei, entry->dest->preds)
    8806      6160587 :     if (e != entry
    8807        60229 :         && !(e->flags & EDGE_DFS_BACK))
    8808              :       break;
    8809      6100358 :   if (e != NULL && dump_file && (dump_flags & TDF_DETAILS))
    8810            0 :     fprintf (dump_file, "Region does not contain all edges into "
    8811              :              "the entry block, skipping its PHIs.\n");
    8812      6100358 :   skip_entry_phis |= e != NULL;
    8813              : 
    8814      6100358 :   int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
    8815     55881860 :   for (int i = 0; i < n; ++i)
    8816     49781502 :     bb_to_rpo[rpo[i]] = i;
    8817      6100358 :   vn_bb_to_rpo = bb_to_rpo;
    8818              : 
    8819      6100358 :   unwind_state *rpo_state = XNEWVEC (unwind_state, n);
    8820              : 
    8821      6100358 :   rpo_elim avail (entry->dest);
    8822      6100358 :   rpo_avail = &avail;
    8823              : 
    8824              :   /* Verify we have no extra entries into the region.  */
    8825      6100358 :   if (flag_checking && do_region)
    8826              :     {
    8827       682869 :       auto_bb_flag bb_in_region (fn);
    8828      2077613 :       for (int i = 0; i < n; ++i)
    8829              :         {
    8830      1394744 :           basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
    8831      1394744 :           bb->flags |= bb_in_region;
    8832              :         }
    8833              :       /* We can't merge the first two loops because we cannot rely
    8834              :          on EDGE_DFS_BACK for edges not within the region.  But if
    8835              :          we decide to always have the bb_in_region flag we can
    8836              :          do the checking during the RPO walk itself (but then it's
    8837              :          also easy to handle MEME conservatively).  */
    8838      2077613 :       for (int i = 0; i < n; ++i)
    8839              :         {
    8840      1394744 :           basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
    8841      1394744 :           edge e;
    8842      1394744 :           edge_iterator ei;
    8843      3052044 :           FOR_EACH_EDGE (e, ei, bb->preds)
    8844      1657300 :             gcc_assert (e == entry
    8845              :                         || (skip_entry_phis && bb == entry->dest)
    8846              :                         || (e->src->flags & bb_in_region));
    8847              :         }
    8848      2077613 :       for (int i = 0; i < n; ++i)
    8849              :         {
    8850      1394744 :           basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
    8851      1394744 :           bb->flags &= ~bb_in_region;
    8852              :         }
    8853       682869 :     }
    8854              : 
    8855              :   /* Create the VN state.  For the initial size of the various hashtables
    8856              :      use a heuristic based on region size and number of SSA names.  */
    8857      6100358 :   unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
    8858      6100358 :                           / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
    8859      6100358 :   VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
    8860      6100358 :   next_value_id = 1;
    8861      6100358 :   next_constant_value_id = -1;
    8862              : 
    8863      6100358 :   vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
    8864      6100358 :   gcc_obstack_init (&vn_ssa_aux_obstack);
    8865              : 
    8866      6100358 :   gcc_obstack_init (&vn_tables_obstack);
    8867      6100358 :   gcc_obstack_init (&vn_tables_insert_obstack);
    8868      6100358 :   valid_info = XCNEW (struct vn_tables_s);
    8869      6100358 :   allocate_vn_table (valid_info, region_size);
    8870      6100358 :   last_inserted_ref = NULL;
    8871      6100358 :   last_inserted_phi = NULL;
    8872      6100358 :   last_inserted_nary = NULL;
    8873      6100358 :   last_pushed_avail = NULL;
    8874              : 
    8875      6100358 :   vn_valueize = rpo_vn_valueize;
    8876              : 
    8877              :   /* Initialize the unwind state and edge/BB executable state.  */
    8878      6100358 :   unsigned curr_scc = 0;
    8879     55881860 :   for (int i = 0; i < n; ++i)
    8880              :     {
    8881     49781502 :       basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
    8882     49781502 :       rpo_state[i].visited = 0;
    8883     49781502 :       rpo_state[i].max_rpo = i;
    8884     58167078 :       if (!iterate && curr_scc < toplevel_scc_extents.length ())
    8885              :         {
    8886      7005804 :           if (i >= toplevel_scc_extents[curr_scc].first
    8887      7005804 :               && i <= toplevel_scc_extents[curr_scc].second)
    8888      3883323 :             rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
    8889      7005804 :           if (i == toplevel_scc_extents[curr_scc].second)
    8890       729819 :             curr_scc++;
    8891              :         }
    8892     49781502 :       bb->flags &= ~BB_EXECUTABLE;
    8893     49781502 :       bool has_backedges = false;
    8894     49781502 :       edge e;
    8895     49781502 :       edge_iterator ei;
    8896    118097904 :       FOR_EACH_EDGE (e, ei, bb->preds)
    8897              :         {
    8898     68316402 :           if (e->flags & EDGE_DFS_BACK)
    8899      2831986 :             has_backedges = true;
    8900     68316402 :           e->flags &= ~EDGE_EXECUTABLE;
    8901     68316402 :           if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
    8902     68316402 :             continue;
    8903              :         }
    8904     49781502 :       rpo_state[i].iterate = iterate && has_backedges;
    8905              :     }
    8906      6100358 :   entry->flags |= EDGE_EXECUTABLE;
    8907      6100358 :   entry->dest->flags |= BB_EXECUTABLE;
    8908              : 
    8909              :   /* As heuristic to improve compile-time we handle only the N innermost
    8910              :      loops and the outermost one optimistically.  */
    8911      6100358 :   if (iterate)
    8912              :     {
    8913      4219037 :       unsigned max_depth = param_rpo_vn_max_loop_depth;
    8914     14191509 :       for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
    8915      1536801 :         if (loop_depth (loop) > max_depth)
    8916         2108 :           for (unsigned i = 2;
    8917         9022 :                i < loop_depth (loop) - max_depth; ++i)
    8918              :             {
    8919         2108 :               basic_block header = superloop_at_depth (loop, i)->header;
    8920         2108 :               bool non_latch_backedge = false;
    8921         2108 :               edge e;
    8922         2108 :               edge_iterator ei;
    8923         6355 :               FOR_EACH_EDGE (e, ei, header->preds)
    8924         4247 :                 if (e->flags & EDGE_DFS_BACK)
    8925              :                   {
    8926              :                     /* There can be a non-latch backedge into the header
    8927              :                        which is part of an outer irreducible region.  We
    8928              :                        cannot avoid iterating this block then.  */
    8929         2139 :                     if (!dominated_by_p (CDI_DOMINATORS,
    8930         2139 :                                          e->src, e->dest))
    8931              :                       {
    8932           12 :                         if (dump_file && (dump_flags & TDF_DETAILS))
    8933            0 :                           fprintf (dump_file, "non-latch backedge %d -> %d "
    8934              :                                    "forces iteration of loop %d\n",
    8935            0 :                                    e->src->index, e->dest->index, loop->num);
    8936              :                         non_latch_backedge = true;
    8937              :                       }
    8938              :                     else
    8939         2127 :                       e->flags |= EDGE_EXECUTABLE;
    8940              :                   }
    8941         2108 :               rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
    8942      4219037 :             }
    8943              :     }
    8944              : 
    8945      6100358 :   uint64_t nblk = 0;
    8946      6100358 :   int idx = 0;
    8947      4219037 :   if (iterate)
    8948              :     /* Go and process all blocks, iterating as necessary.  */
    8949     48339589 :     do
    8950              :       {
    8951     48339589 :         basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
    8952              : 
    8953              :         /* If the block has incoming backedges remember unwind state.  This
    8954              :            is required even for non-executable blocks since in irreducible
    8955              :            regions we might reach them via the backedge and re-start iterating
    8956              :            from there.
    8957              :            Note we can individually mark blocks with incoming backedges to
    8958              :            not iterate where we then handle PHIs conservatively.  We do that
    8959              :            heuristically to reduce compile-time for degenerate cases.  */
    8960     48339589 :         if (rpo_state[idx].iterate)
    8961              :           {
    8962      4363563 :             rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
    8963      4363563 :             rpo_state[idx].ref_top = last_inserted_ref;
    8964      4363563 :             rpo_state[idx].phi_top = last_inserted_phi;
    8965      4363563 :             rpo_state[idx].nary_top = last_inserted_nary;
    8966      4363563 :             rpo_state[idx].avail_top
    8967      4363563 :               = last_pushed_avail ? last_pushed_avail->avail : NULL;
    8968              :           }
    8969              : 
    8970     48339589 :         if (!(bb->flags & BB_EXECUTABLE))
    8971              :           {
    8972       928169 :             if (dump_file && (dump_flags & TDF_DETAILS))
    8973            2 :               fprintf (dump_file, "Block %d: BB%d found not executable\n",
    8974              :                        idx, bb->index);
    8975       928169 :             idx++;
    8976      2819914 :             continue;
    8977              :           }
    8978              : 
    8979     47411420 :         if (dump_file && (dump_flags & TDF_DETAILS))
    8980          334 :           fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
    8981     47411420 :         nblk++;
    8982     94822840 :         todo |= process_bb (avail, bb,
    8983     47411420 :                             rpo_state[idx].visited != 0,
    8984              :                             rpo_state[idx].iterate,
    8985              :                             iterate, eliminate, do_region, exit_bbs, false);
    8986     47411420 :         rpo_state[idx].visited++;
    8987              : 
    8988              :         /* Verify if changed values flow over executable outgoing backedges
    8989              :            and those change destination PHI values (that's the thing we
    8990              :            can easily verify).  Reduce over all such edges to the farthest
    8991              :            away PHI.  */
    8992     47411420 :         int iterate_to = -1;
    8993     47411420 :         edge_iterator ei;
    8994     47411420 :         edge e;
    8995    114185425 :         FOR_EACH_EDGE (e, ei, bb->succs)
    8996     66774005 :           if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
    8997              :               == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
    8998      4373456 :               && rpo_state[bb_to_rpo[e->dest->index]].iterate)
    8999              :             {
    9000      4370690 :               int destidx = bb_to_rpo[e->dest->index];
    9001      4370690 :               if (!rpo_state[destidx].visited)
    9002              :                 {
    9003          134 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    9004            0 :                     fprintf (dump_file, "Unvisited destination %d\n",
    9005              :                              e->dest->index);
    9006          134 :                   if (iterate_to == -1 || destidx < iterate_to)
    9007          134 :                     iterate_to = destidx;
    9008          134 :                   continue;
    9009              :                 }
    9010      4370556 :               if (dump_file && (dump_flags & TDF_DETAILS))
    9011           53 :                 fprintf (dump_file, "Looking for changed values of backedge"
    9012              :                          " %d->%d destination PHIs\n",
    9013           53 :                          e->src->index, e->dest->index);
    9014      4370556 :               vn_context_bb = e->dest;
    9015      4370556 :               gphi_iterator gsi;
    9016      4370556 :               for (gsi = gsi_start_phis (e->dest);
    9017     10007341 :                    !gsi_end_p (gsi); gsi_next (&gsi))
    9018              :                 {
    9019      7528688 :                   bool inserted = false;
    9020              :                   /* While we'd ideally just iterate on value changes
    9021              :                      we CSE PHIs and do that even across basic-block
    9022              :                      boundaries.  So even hashtable state changes can
    9023              :                      be important (which is roughly equivalent to
    9024              :                      PHI argument value changes).  To not excessively
    9025              :                      iterate because of that we track whether a PHI
    9026              :                      was CSEd to with GF_PLF_1.  */
    9027      7528688 :                   bool phival_changed;
    9028      7528688 :                   if ((phival_changed = visit_phi (gsi.phi (),
    9029              :                                                    &inserted, false))
    9030      8906789 :                       || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
    9031              :                     {
    9032      1891903 :                       if (!phival_changed
    9033      1891903 :                           && dump_file && (dump_flags & TDF_DETAILS))
    9034            0 :                         fprintf (dump_file, "PHI was CSEd and hashtable "
    9035              :                                  "state (changed)\n");
    9036      1891903 :                       if (iterate_to == -1 || destidx < iterate_to)
    9037      1891818 :                         iterate_to = destidx;
    9038      1891903 :                       break;
    9039              :                     }
    9040              :                 }
    9041      4370556 :               vn_context_bb = NULL;
    9042              :             }
    9043     47411420 :         if (iterate_to != -1)
    9044              :           {
    9045      1891745 :             do_unwind (&rpo_state[iterate_to], avail);
    9046      1891745 :             idx = iterate_to;
    9047      1891745 :             if (dump_file && (dump_flags & TDF_DETAILS))
    9048           20 :               fprintf (dump_file, "Iterating to %d BB%d\n",
    9049           20 :                        iterate_to, rpo[iterate_to]);
    9050      1891745 :             continue;
    9051              :           }
    9052              : 
    9053     45519675 :         idx++;
    9054              :       }
    9055     48339589 :     while (idx < n);
    9056              : 
    9057              :   else /* !iterate */
    9058              :     {
    9059              :       /* Process all blocks greedily with a worklist that enforces RPO
    9060              :          processing of reachable blocks.  */
    9061      1881321 :       auto_bitmap worklist;
    9062      1881321 :       bitmap_set_bit (worklist, 0);
    9063     17066581 :       while (!bitmap_empty_p (worklist))
    9064              :         {
    9065     13303939 :           int idx = bitmap_clear_first_set_bit (worklist);
    9066     13303939 :           basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
    9067     13303939 :           gcc_assert ((bb->flags & BB_EXECUTABLE)
    9068              :                       && !rpo_state[idx].visited);
    9069              : 
    9070     13303939 :           if (dump_file && (dump_flags & TDF_DETAILS))
    9071        35234 :             fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
    9072              : 
    9073              :           /* When we run into predecessor edges where we cannot trust its
    9074              :              executable state mark them executable so PHI processing will
    9075              :              be conservative.
    9076              :              ???  Do we need to force arguments flowing over that edge
    9077              :              to be varying or will they even always be?  */
    9078     13303939 :           edge_iterator ei;
    9079     13303939 :           edge e;
    9080     32214491 :           FOR_EACH_EDGE (e, ei, bb->preds)
    9081     18910552 :             if (!(e->flags & EDGE_EXECUTABLE)
    9082      1019445 :                 && (bb == entry->dest
    9083       962521 :                     || (!rpo_state[bb_to_rpo[e->src->index]].visited
    9084       925870 :                         && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
    9085              :                             >= (int)idx))))
    9086              :               {
    9087       959398 :                 if (dump_file && (dump_flags & TDF_DETAILS))
    9088        11305 :                   fprintf (dump_file, "Cannot trust state of predecessor "
    9089              :                            "edge %d -> %d, marking executable\n",
    9090        11305 :                            e->src->index, e->dest->index);
    9091       959398 :                 e->flags |= EDGE_EXECUTABLE;
    9092              :               }
    9093              : 
    9094     13303939 :           nblk++;
    9095     13303939 :           todo |= process_bb (avail, bb, false, false, false, eliminate,
    9096              :                               do_region, exit_bbs,
    9097     13303939 :                               skip_entry_phis && bb == entry->dest);
    9098     13303939 :           rpo_state[idx].visited++;
    9099              : 
    9100     32844425 :           FOR_EACH_EDGE (e, ei, bb->succs)
    9101     19540486 :             if ((e->flags & EDGE_EXECUTABLE)
    9102     19463710 :                 && e->dest->index != EXIT_BLOCK
    9103     18294843 :                 && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
    9104     36504387 :                 && !rpo_state[bb_to_rpo[e->dest->index]].visited)
    9105     16009786 :               bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
    9106              :         }
    9107      1881321 :     }
    9108              : 
    9109              :   /* If statistics or dump file active.  */
    9110      6100358 :   int nex = 0;
    9111      6100358 :   unsigned max_visited = 1;
    9112     55881860 :   for (int i = 0; i < n; ++i)
    9113              :     {
    9114     49781502 :       basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
    9115     49781502 :       if (bb->flags & BB_EXECUTABLE)
    9116     49196908 :         nex++;
    9117     49781502 :       statistics_histogram_event (cfun, "RPO block visited times",
    9118     49781502 :                                   rpo_state[i].visited);
    9119     49781502 :       if (rpo_state[i].visited > max_visited)
    9120              :         max_visited = rpo_state[i].visited;
    9121              :     }
    9122      6100358 :   unsigned nvalues = 0, navail = 0;
    9123    167679463 :   for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
    9124    329258568 :        i != vn_ssa_aux_hash->end (); ++i)
    9125              :     {
    9126    161579105 :       nvalues++;
    9127    161579105 :       vn_avail *av = (*i)->avail;
    9128    238983880 :       while (av)
    9129              :         {
    9130     77404775 :           navail++;
    9131     77404775 :           av = av->next;
    9132              :         }
    9133              :     }
    9134      6100358 :   statistics_counter_event (cfun, "RPO blocks", n);
    9135      6100358 :   statistics_counter_event (cfun, "RPO blocks visited", nblk);
    9136      6100358 :   statistics_counter_event (cfun, "RPO blocks executable", nex);
    9137      6100358 :   statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
    9138      6100358 :   statistics_histogram_event (cfun, "RPO num values", nvalues);
    9139      6100358 :   statistics_histogram_event (cfun, "RPO num avail", navail);
    9140      6100358 :   statistics_histogram_event (cfun, "RPO num lattice",
    9141      6100358 :                               vn_ssa_aux_hash->elements ());
    9142      6100358 :   if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
    9143              :     {
    9144        11210 :       fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
    9145              :                " blocks in total discovering %d executable blocks iterating "
    9146              :                "%d.%d times, a block was visited max. %u times\n",
    9147              :                n, nblk, nex,
    9148        11210 :                (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
    9149              :                max_visited);
    9150        11210 :       fprintf (dump_file, "RPO tracked %d values available at %d locations "
    9151              :                "and %" PRIu64 " lattice elements\n",
    9152        11210 :                nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
    9153              :     }
    9154              : 
    9155      6100358 :   if (eliminate)
    9156              :     {
    9157              :       /* When !iterate we already performed elimination during the RPO
    9158              :          walk.  */
    9159      5119236 :       if (iterate)
    9160              :         {
    9161              :           /* Elimination for region-based VN needs to be done within the
    9162              :              RPO walk.  */
    9163      3257507 :           gcc_assert (! do_region);
    9164              :           /* Note we can't use avail.walk here because that gets confused
    9165              :              by the existing availability and it will be less efficient
    9166              :              as well.  */
    9167      3257507 :           todo |= eliminate_with_rpo_vn (NULL);
    9168              :         }
    9169              :       else
    9170      1861729 :         todo |= avail.eliminate_cleanup (do_region);
    9171              :     }
    9172              : 
    9173      6100358 :   vn_valueize = NULL;
    9174      6100358 :   rpo_avail = NULL;
    9175      6100358 :   vn_bb_to_rpo = NULL;
    9176              : 
    9177      6100358 :   XDELETEVEC (bb_to_rpo);
    9178      6100358 :   XDELETEVEC (rpo);
    9179      6100358 :   XDELETEVEC (rpo_state);
    9180              : 
    9181      6100358 :   return todo;
    9182      6100358 : }
    9183              : 
    9184              : /* Region-based entry for RPO VN.  Performs value-numbering and elimination
    9185              :    on the SEME region specified by ENTRY and EXIT_BBS.  If ENTRY is not
    9186              :    the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
    9187              :    are not considered.
    9188              :    If ITERATE is true then treat backedges optimistically as not
    9189              :    executed and iterate.  If ELIMINATE is true then perform
    9190              :    elimination, otherwise leave that to the caller.
    9191              :    If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
    9192              :    KIND specifies the amount of work done for handling memory operations.  */
    9193              : 
    9194              : unsigned
    9195       702467 : do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
    9196              :            bool iterate, bool eliminate, bool skip_entry_phis,
    9197              :            vn_lookup_kind kind)
    9198              : {
    9199       702467 :   auto_timevar tv (TV_TREE_RPO_VN);
    9200       702467 :   unsigned todo = do_rpo_vn_1 (fn, entry, exit_bbs, iterate, eliminate,
    9201              :                                skip_entry_phis, kind);
    9202       702467 :   free_rpo_vn ();
    9203      1404934 :   return todo;
    9204       702467 : }
    9205              : 
    9206              : 
    9207              : namespace {
    9208              : 
    9209              : const pass_data pass_data_fre =
    9210              : {
    9211              :   GIMPLE_PASS, /* type */
    9212              :   "fre", /* name */
    9213              :   OPTGROUP_NONE, /* optinfo_flags */
    9214              :   TV_TREE_FRE, /* tv_id */
    9215              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    9216              :   0, /* properties_provided */
    9217              :   0, /* properties_destroyed */
    9218              :   0, /* todo_flags_start */
    9219              :   0, /* todo_flags_finish */
    9220              : };
    9221              : 
    9222              : class pass_fre : public gimple_opt_pass
    9223              : {
    9224              : public:
    9225      1494140 :   pass_fre (gcc::context *ctxt)
    9226      2988280 :     : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
    9227              :   {}
    9228              : 
    9229              :   /* opt_pass methods: */
    9230      1195312 :   opt_pass * clone () final override { return new pass_fre (m_ctxt); }
    9231      1494140 :   void set_pass_param (unsigned int n, bool param) final override
    9232              :     {
    9233      1494140 :       gcc_assert (n == 0);
    9234      1494140 :       may_iterate = param;
    9235      1494140 :     }
    9236      4516114 :   bool gate (function *) final override
    9237              :     {
    9238      4516114 :       return flag_tree_fre != 0 && (may_iterate || optimize > 1);
    9239              :     }
    9240              :   unsigned int execute (function *) final override;
    9241              : 
    9242              : private:
    9243              :   bool may_iterate;
    9244              : }; // class pass_fre
    9245              : 
    9246              : unsigned int
    9247      4436361 : pass_fre::execute (function *fun)
    9248              : {
    9249      4436361 :   unsigned todo = 0;
    9250              : 
    9251              :   /* At -O[1g] use the cheap non-iterating mode.  */
    9252      4436361 :   bool iterate_p = may_iterate && (optimize > 1);
    9253      4436361 :   calculate_dominance_info (CDI_DOMINATORS);
    9254      4436361 :   if (iterate_p)
    9255      3257507 :     loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
    9256              : 
    9257      4436361 :   todo = do_rpo_vn_1 (fun, NULL, NULL, iterate_p, true, false, VN_WALKREWRITE);
    9258      4436361 :   free_rpo_vn ();
    9259              : 
    9260      4436361 :   if (iterate_p)
    9261      3257507 :     loop_optimizer_finalize ();
    9262              : 
    9263      4436361 :   if (scev_initialized_p ())
    9264        32106 :     scev_reset_htab ();
    9265              : 
    9266              :   /* For late FRE after IVOPTs and unrolling, see if we can
    9267              :      remove some TREE_ADDRESSABLE and rewrite stuff into SSA.  */
    9268      4436361 :   if (!may_iterate)
    9269       993408 :     todo |= TODO_update_address_taken;
    9270              : 
    9271      4436361 :   return todo;
    9272              : }
    9273              : 
    9274              : } // anon namespace
    9275              : 
    9276              : gimple_opt_pass *
    9277       298828 : make_pass_fre (gcc::context *ctxt)
    9278              : {
    9279       298828 :   return new pass_fre (ctxt);
    9280              : }
    9281              : 
    9282              : #undef BB_EXECUTABLE
        

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