LCOV - code coverage report
Current view: top level - gcc - tree-ssa-forwprop.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.7 % 3720 3560
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 96 96
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Forward propagation of expressions for single use variables.
       2              :    Copyright (C) 2004-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify
       7              : it under the terms of the GNU General Public License as published by
       8              : the Free Software Foundation; either version 3, or (at your option)
       9              : any later version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful,
      12              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14              : GNU General Public License for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : #include "config.h"
      21              : #include "system.h"
      22              : #include "coretypes.h"
      23              : #include "backend.h"
      24              : #include "rtl.h"
      25              : #include "tree.h"
      26              : #include "gimple.h"
      27              : #include "cfghooks.h"
      28              : #include "tree-pass.h"
      29              : #include "ssa.h"
      30              : #include "expmed.h"
      31              : #include "optabs-query.h"
      32              : #include "gimple-pretty-print.h"
      33              : #include "fold-const.h"
      34              : #include "stor-layout.h"
      35              : #include "gimple-iterator.h"
      36              : #include "gimple-fold.h"
      37              : #include "tree-eh.h"
      38              : #include "gimplify.h"
      39              : #include "gimplify-me.h"
      40              : #include "tree-cfg.h"
      41              : #include "expr.h"
      42              : #include "tree-dfa.h"
      43              : #include "tree-ssa-propagate.h"
      44              : #include "tree-ssa-dom.h"
      45              : #include "tree-ssa-strlen.h"
      46              : #include "builtins.h"
      47              : #include "tree-cfgcleanup.h"
      48              : #include "cfganal.h"
      49              : #include "optabs-tree.h"
      50              : #include "insn-config.h"
      51              : #include "recog.h"
      52              : #include "cfgloop.h"
      53              : #include "tree-vectorizer.h"
      54              : #include "tree-vector-builder.h"
      55              : #include "vec-perm-indices.h"
      56              : #include "internal-fn.h"
      57              : #include "cgraph.h"
      58              : #include "tree-ssa.h"
      59              : #include "gimple-range.h"
      60              : #include "tree-ssa-dce.h"
      61              : #include "tree-ssa-math-opts.h"
      62              : 
      63              : /* This pass propagates the RHS of assignment statements into use
      64              :    sites of the LHS of the assignment.  It's basically a specialized
      65              :    form of tree combination.   It is hoped all of this can disappear
      66              :    when we have a generalized tree combiner.
      67              : 
      68              :    One class of common cases we handle is forward propagating a single use
      69              :    variable into a COND_EXPR.
      70              : 
      71              :      bb0:
      72              :        x = a COND b;
      73              :        if (x) goto ... else goto ...
      74              : 
      75              :    Will be transformed into:
      76              : 
      77              :      bb0:
      78              :        if (a COND b) goto ... else goto ...
      79              : 
      80              :    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
      81              : 
      82              :    Or (assuming c1 and c2 are constants):
      83              : 
      84              :      bb0:
      85              :        x = a + c1;
      86              :        if (x EQ/NEQ c2) goto ... else goto ...
      87              : 
      88              :    Will be transformed into:
      89              : 
      90              :      bb0:
      91              :         if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
      92              : 
      93              :    Similarly for x = a - c1.
      94              : 
      95              :    Or
      96              : 
      97              :      bb0:
      98              :        x = !a
      99              :        if (x) goto ... else goto ...
     100              : 
     101              :    Will be transformed into:
     102              : 
     103              :      bb0:
     104              :         if (a == 0) goto ... else goto ...
     105              : 
     106              :    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
     107              :    For these cases, we propagate A into all, possibly more than one,
     108              :    COND_EXPRs that use X.
     109              : 
     110              :    Or
     111              : 
     112              :      bb0:
     113              :        x = (typecast) a
     114              :        if (x) goto ... else goto ...
     115              : 
     116              :    Will be transformed into:
     117              : 
     118              :      bb0:
     119              :         if (a != 0) goto ... else goto ...
     120              : 
     121              :    (Assuming a is an integral type and x is a boolean or x is an
     122              :     integral and a is a boolean.)
     123              : 
     124              :    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
     125              :    For these cases, we propagate A into all, possibly more than one,
     126              :    COND_EXPRs that use X.
     127              : 
     128              :    In addition to eliminating the variable and the statement which assigns
     129              :    a value to the variable, we may be able to later thread the jump without
     130              :    adding insane complexity in the dominator optimizer.
     131              : 
     132              :    Also note these transformations can cascade.  We handle this by having
     133              :    a worklist of COND_EXPR statements to examine.  As we make a change to
     134              :    a statement, we put it back on the worklist to examine on the next
     135              :    iteration of the main loop.
     136              : 
     137              :    A second class of propagation opportunities arises for ADDR_EXPR
     138              :    nodes.
     139              : 
     140              :      ptr = &x->y->z;
     141              :      res = *ptr;
     142              : 
     143              :    Will get turned into
     144              : 
     145              :      res = x->y->z;
     146              : 
     147              :    Or
     148              :      ptr = (type1*)&type2var;
     149              :      res = *ptr
     150              : 
     151              :    Will get turned into (if type1 and type2 are the same size
     152              :    and neither have volatile on them):
     153              :      res = VIEW_CONVERT_EXPR<type1>(type2var)
     154              : 
     155              :    Or
     156              : 
     157              :      ptr = &x[0];
     158              :      ptr2 = ptr + <constant>;
     159              : 
     160              :    Will get turned into
     161              : 
     162              :      ptr2 = &x[constant/elementsize];
     163              : 
     164              :   Or
     165              : 
     166              :      ptr = &x[0];
     167              :      offset = index * element_size;
     168              :      offset_p = (pointer) offset;
     169              :      ptr2 = ptr + offset_p
     170              : 
     171              :   Will get turned into:
     172              : 
     173              :      ptr2 = &x[index];
     174              : 
     175              :   Or
     176              :     ssa = (int) decl
     177              :     res = ssa & 1
     178              : 
     179              :   Provided that decl has known alignment >= 2, will get turned into
     180              : 
     181              :     res = 0
     182              : 
     183              :   We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
     184              :   allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
     185              :   {NOT_EXPR,NEG_EXPR}.
     186              : 
     187              :    This will (of course) be extended as other needs arise.  */
     188              : 
     189              : /* Data structure that contains simplifiable vectorized permute sequences.
     190              :    See recognise_vec_perm_simplify_seq () for a description of the sequence.  */
     191              : 
     192              : struct _vec_perm_simplify_seq
     193              : {
     194              :   /* Defining stmts of vectors in the sequence.  */
     195              :   gassign *v_1_stmt;
     196              :   gassign *v_2_stmt;
     197              :   gassign *v_x_stmt;
     198              :   gassign *v_y_stmt;
     199              :   /* Final permute statement.  */
     200              :   gassign *stmt;
     201              :   /* New selector indices for stmt.  */
     202              :   tree new_sel;
     203              :   /* Elements of each vector and selector.  */
     204              :   unsigned int nelts;
     205              : };
     206              : typedef struct _vec_perm_simplify_seq *vec_perm_simplify_seq;
     207              : 
     208              : static bool forward_propagate_addr_expr (tree, tree, bool);
     209              : 
     210              : /* Set to true if we delete dead edges during the optimization.  */
     211              : static bool cfg_changed;
     212              : 
     213              : static tree rhs_to_tree (tree type, gimple *stmt);
     214              : 
     215              : static bitmap to_purge;
     216              : 
     217              : /* Const-and-copy lattice.  */
     218              : static vec<tree> lattice;
     219              : 
     220              : /* Set the lattice entry for NAME to VAL.  */
     221              : static void
     222     33658087 : fwprop_set_lattice_val (tree name, tree val)
     223              : {
     224     33658087 :   if (TREE_CODE (name) == SSA_NAME)
     225              :     {
     226     33658087 :       if (SSA_NAME_VERSION (name) >= lattice.length ())
     227              :         {
     228        31596 :           lattice.reserve (num_ssa_names - lattice.length ());
     229        21064 :           lattice.quick_grow_cleared (num_ssa_names);
     230              :         }
     231     33658087 :       lattice[SSA_NAME_VERSION (name)] = val;
     232              :       /* As this now constitutes a copy duplicate points-to
     233              :          and range info appropriately.  */
     234     33658087 :       if (TREE_CODE (val) == SSA_NAME)
     235     33162434 :         maybe_duplicate_ssa_info_at_copy (name, val);
     236              :     }
     237     33658087 : }
     238              : 
     239              : /* Invalidate the lattice entry for NAME, done when releasing SSA names.  */
     240              : static void
     241       946230 : fwprop_invalidate_lattice (tree name)
     242              : {
     243       946230 :   if (name
     244       943677 :       && TREE_CODE (name) == SSA_NAME
     245      1889779 :       && SSA_NAME_VERSION (name) < lattice.length ())
     246       943518 :     lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
     247       946230 : }
     248              : 
     249              : /* Get the statement we can propagate from into NAME skipping
     250              :    trivial copies.  Returns the statement which defines the
     251              :    propagation source or NULL_TREE if there is no such one.
     252              :    If SINGLE_USE_ONLY is set considers only sources which have
     253              :    a single use chain up to NAME.  If SINGLE_USE_P is non-null,
     254              :    it is set to whether the chain to NAME is a single use chain
     255              :    or not.  SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set.  */
     256              : 
     257              : static gimple *
     258     28640254 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
     259              : {
     260     28640254 :   bool single_use = true;
     261              : 
     262     28641272 :   do {
     263     28640763 :     gimple *def_stmt = SSA_NAME_DEF_STMT (name);
     264              : 
     265     28640763 :     if (!has_single_use (name))
     266              :       {
     267     15628403 :         single_use = false;
     268     15628403 :         if (single_use_only)
     269              :           return NULL;
     270              :       }
     271              : 
     272              :     /* If name is defined by a PHI node or is the default def, bail out.  */
     273     28639201 :     if (!is_gimple_assign (def_stmt))
     274              :       return NULL;
     275              : 
     276              :     /* If def_stmt is a simple copy, continue looking.  */
     277     20168290 :     if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
     278          509 :       name = gimple_assign_rhs1 (def_stmt);
     279              :     else
     280              :       {
     281     20167781 :         if (!single_use_only && single_use_p)
     282     19839963 :           *single_use_p = single_use;
     283              : 
     284              :         return def_stmt;
     285              :       }
     286          509 :   } while (1);
     287              : }
     288              : 
     289              : /* Checks if the destination ssa name in DEF_STMT can be used as
     290              :    propagation source.  Returns true if so, otherwise false.  */
     291              : 
     292              : static bool
     293     28391414 : can_propagate_from (gimple *def_stmt)
     294              : {
     295     28391414 :   gcc_assert (is_gimple_assign (def_stmt));
     296              : 
     297              :   /* If the rhs has side-effects we cannot propagate from it.  */
     298     28391414 :   if (gimple_has_volatile_ops (def_stmt))
     299              :     return false;
     300              : 
     301              :   /* If the rhs is a load we cannot propagate from it.  */
     302     27795877 :   if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
     303     27795877 :       || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
     304              :     return false;
     305              : 
     306              :   /* Constants can be always propagated.  */
     307     13736476 :   if (gimple_assign_single_p (def_stmt)
     308     13736476 :       && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
     309              :     return true;
     310              : 
     311              :   /* We cannot propagate ssa names that occur in abnormal phi nodes.  */
     312     13736476 :   if (stmt_references_abnormal_ssa_name (def_stmt))
     313              :     return false;
     314              : 
     315              :   /* If the definition is a conversion of a pointer to a function type,
     316              :      then we cannot apply optimizations as some targets require
     317              :      function pointers to be canonicalized and in this case this
     318              :      optimization could eliminate a necessary canonicalization.  */
     319     13735785 :   if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
     320              :     {
     321      3319201 :       tree rhs = gimple_assign_rhs1 (def_stmt);
     322      3319201 :       if (FUNCTION_POINTER_TYPE_P (TREE_TYPE (rhs)))
     323          818 :         return false;
     324              :     }
     325              : 
     326              :   return true;
     327              : }
     328              : 
     329              : /* Remove a chain of dead statements starting at the definition of
     330              :    NAME.  The chain is linked via the first operand of the defining statements.
     331              :    If NAME was replaced in its only use then this function can be used
     332              :    to clean up dead stmts.  The function handles already released SSA
     333              :    names gracefully.  */
     334              : 
     335              : static void
     336       235676 : remove_prop_source_from_use (tree name)
     337              : {
     338       299735 :   gimple_stmt_iterator gsi;
     339       299735 :   gimple *stmt;
     340              : 
     341       299735 :   do {
     342       299735 :     basic_block bb;
     343              : 
     344       299735 :     if (SSA_NAME_IN_FREE_LIST (name)
     345       299692 :         || SSA_NAME_IS_DEFAULT_DEF (name)
     346       598115 :         || !has_zero_uses (name))
     347              :       break;
     348              : 
     349        64522 :     stmt = SSA_NAME_DEF_STMT (name);
     350        64522 :     if (gimple_code (stmt) == GIMPLE_PHI
     351        64522 :         || gimple_has_side_effects (stmt))
     352              :       break;
     353              : 
     354        64522 :     bb = gimple_bb (stmt);
     355        64522 :     gsi = gsi_for_stmt (stmt);
     356        64522 :     unlink_stmt_vdef (stmt);
     357        64522 :     if (gsi_remove (&gsi, true))
     358            6 :       bitmap_set_bit (to_purge, bb->index);
     359        64522 :     fwprop_invalidate_lattice (gimple_get_lhs (stmt));
     360        64522 :     release_defs (stmt);
     361              : 
     362        64522 :     name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
     363        64522 :   } while (name && TREE_CODE (name) == SSA_NAME);
     364              : 
     365       235676 : }
     366              : 
     367              : /* Return the rhs of a gassign *STMT in a form of a single tree,
     368              :    converted to type TYPE.
     369              : 
     370              :    This should disappear, but is needed so we can combine expressions and use
     371              :    the fold() interfaces. Long term, we need to develop folding and combine
     372              :    routines that deal with gimple exclusively . */
     373              : 
     374              : static tree
     375      7469555 : rhs_to_tree (tree type, gimple *stmt)
     376              : {
     377      7469555 :   location_t loc = gimple_location (stmt);
     378      7469555 :   enum tree_code code = gimple_assign_rhs_code (stmt);
     379      7469555 :   switch (get_gimple_rhs_class (code))
     380              :     {
     381        13641 :     case GIMPLE_TERNARY_RHS:
     382        13641 :       return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
     383              :                               gimple_assign_rhs2 (stmt),
     384        13641 :                               gimple_assign_rhs3 (stmt));
     385      5099082 :     case GIMPLE_BINARY_RHS:
     386      5099082 :       return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
     387      5099082 :                               gimple_assign_rhs2 (stmt));
     388      2093184 :     case GIMPLE_UNARY_RHS:
     389      2093184 :       return build1 (code, type, gimple_assign_rhs1 (stmt));
     390       263648 :     case GIMPLE_SINGLE_RHS:
     391       263648 :       return gimple_assign_rhs1 (stmt);
     392            0 :     default:
     393            0 :       gcc_unreachable ();
     394              :     }
     395              : }
     396              : 
     397              : /* Combine OP0 CODE OP1 in the context of a COND_EXPR.  Returns
     398              :    the folded result in a form suitable for COND_EXPR_COND or
     399              :    NULL_TREE, if there is no suitable simplified form.  If
     400              :    INVARIANT_ONLY is true only gimple_min_invariant results are
     401              :    considered simplified.  */
     402              : 
     403              : static tree
     404      8423376 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
     405              :                         tree op0, tree op1, bool invariant_only)
     406              : {
     407      8423376 :   tree t;
     408              : 
     409      8423376 :   gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
     410              : 
     411      8423376 :   t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
     412      8423376 :   if (!t)
     413              :     return NULL_TREE;
     414              : 
     415              :   /* Require that we got a boolean type out if we put one in.  */
     416      3697147 :   gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
     417              : 
     418              :   /* Canonicalize the combined condition for use in a COND_EXPR.  */
     419      3697147 :   t = canonicalize_cond_expr_cond (t);
     420              : 
     421              :   /* Bail out if we required an invariant but didn't get one.  */
     422      3697147 :   if (!t || (invariant_only && !is_gimple_min_invariant (t)))
     423      3464277 :     return NULL_TREE;
     424              : 
     425              :   return t;
     426              : }
     427              : 
     428              : /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
     429              :    of its operand.  Return a new comparison tree or NULL_TREE if there
     430              :    were no simplifying combines.  */
     431              : 
     432              : static tree
     433     22545922 : forward_propagate_into_comparison_1 (gimple *stmt,
     434              :                                      enum tree_code code, tree type,
     435              :                                      tree op0, tree op1)
     436              : {
     437     22545922 :   tree tmp = NULL_TREE;
     438     22545922 :   tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
     439     22545922 :   bool single_use0_p = false, single_use1_p = false;
     440              : 
     441              :   /* For comparisons use the first operand, that is likely to
     442              :      simplify comparisons against constants.  */
     443     22545922 :   if (TREE_CODE (op0) == SSA_NAME)
     444              :     {
     445     22475545 :       gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
     446     22475545 :       if (def_stmt && can_propagate_from (def_stmt))
     447              :         {
     448      5655472 :           enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
     449      5655472 :           bool invariant_only_p = !single_use0_p;
     450              : 
     451      5655472 :           rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
     452              : 
     453              :           /* Always combine comparisons or conversions from booleans.  */
     454      5655472 :           if (TREE_CODE (op1) == INTEGER_CST
     455      5655472 :               && ((CONVERT_EXPR_CODE_P (def_code)
     456       904153 :                    && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
     457              :                       == BOOLEAN_TYPE)
     458      3614453 :                   || TREE_CODE_CLASS (def_code) == tcc_comparison))
     459              :             invariant_only_p = false;
     460              : 
     461      5655472 :           tmp = combine_cond_expr_cond (stmt, code, type,
     462              :                                         rhs0, op1, invariant_only_p);
     463      5655472 :           if (tmp)
     464              :             return tmp;
     465              :         }
     466              :     }
     467              : 
     468              :   /* If that wasn't successful, try the second operand.  */
     469     22322145 :   if (TREE_CODE (op1) == SSA_NAME)
     470              :     {
     471      5560868 :       gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
     472      5560868 :       if (def_stmt && can_propagate_from (def_stmt))
     473              :         {
     474      1814083 :           rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
     475      1814083 :           tmp = combine_cond_expr_cond (stmt, code, type,
     476              :                                         op0, rhs1, !single_use1_p);
     477      1814083 :           if (tmp)
     478              :             return tmp;
     479              :         }
     480              :     }
     481              : 
     482              :   /* If that wasn't successful either, try both operands.  */
     483     22314885 :   if (rhs0 != NULL_TREE
     484     22314885 :       && rhs1 != NULL_TREE)
     485       953821 :     tmp = combine_cond_expr_cond (stmt, code, type,
     486              :                                   rhs0, rhs1,
     487       953821 :                                   !(single_use0_p && single_use1_p));
     488              : 
     489              :   return tmp;
     490              : }
     491              : 
     492              : /* Propagate from the ssa name definition statements of the assignment
     493              :    from a comparison at *GSI into the conditional if that simplifies it.
     494              :    Returns true if the stmt was modified.  */
     495              : 
     496              : static bool
     497      2664298 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
     498              : {
     499      2664298 :   gimple *stmt = gsi_stmt (*gsi);
     500      2664298 :   tree tmp;
     501      2664298 :   tree type = TREE_TYPE (gimple_assign_lhs (stmt));
     502      2664298 :   tree rhs1 = gimple_assign_rhs1 (stmt);
     503      2664298 :   tree rhs2 = gimple_assign_rhs2 (stmt);
     504              : 
     505              :   /* Combine the comparison with defining statements.  */
     506      2664298 :   tmp = forward_propagate_into_comparison_1 (stmt,
     507              :                                              gimple_assign_rhs_code (stmt),
     508              :                                              type, rhs1, rhs2);
     509      2664298 :   if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
     510              :     {
     511         7692 :       if (dump_file)
     512              :         {
     513            0 :           fprintf (dump_file, "  Replaced '");
     514            0 :           print_gimple_expr (dump_file, stmt, 0);
     515            0 :           fprintf (dump_file, "' with '");
     516            0 :           print_generic_expr (dump_file, tmp);
     517            0 :           fprintf (dump_file, "'\n");
     518              :         }
     519         7692 :       gimple_assign_set_rhs_from_tree (gsi, tmp);
     520         7692 :       fold_stmt (gsi);
     521         7692 :       update_stmt (gsi_stmt (*gsi));
     522              : 
     523         7692 :       if (TREE_CODE (rhs1) == SSA_NAME)
     524         7692 :         remove_prop_source_from_use (rhs1);
     525         7692 :       if (TREE_CODE (rhs2) == SSA_NAME)
     526         3423 :         remove_prop_source_from_use (rhs2);
     527              :       return true;
     528              :     }
     529              : 
     530              :   return false;
     531              : }
     532              : 
     533              : /* Propagate from the ssa name definition statements of COND_EXPR
     534              :    in GIMPLE_COND statement STMT into the conditional if that simplifies it.
     535              :    Returns zero if no statement was changed, one if there were
     536              :    changes and two if cfg_cleanup needs to run.  */
     537              : 
     538              : static int
     539     19881624 : forward_propagate_into_gimple_cond (gcond *stmt)
     540              : {
     541     19881624 :   tree tmp;
     542     19881624 :   enum tree_code code = gimple_cond_code (stmt);
     543     19881624 :   tree rhs1 = gimple_cond_lhs (stmt);
     544     19881624 :   tree rhs2 = gimple_cond_rhs (stmt);
     545              : 
     546              :   /* GIMPLE_COND will always be a comparison.  */
     547     19881624 :   gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison);
     548              : 
     549     19881624 :   tmp = forward_propagate_into_comparison_1 (stmt, code,
     550              :                                              boolean_type_node,
     551              :                                              rhs1, rhs2);
     552     19881624 :   if (tmp
     553     19881624 :       && is_gimple_condexpr_for_cond (tmp))
     554              :     {
     555       218605 :       if (dump_file)
     556              :         {
     557            9 :           fprintf (dump_file, "  Replaced '");
     558            9 :           print_gimple_expr (dump_file, stmt, 0);
     559            9 :           fprintf (dump_file, "' with '");
     560            9 :           print_generic_expr (dump_file, tmp);
     561            9 :           fprintf (dump_file, "'\n");
     562              :         }
     563              : 
     564       218605 :       gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
     565       218605 :       update_stmt (stmt);
     566              : 
     567       218605 :       if (TREE_CODE (rhs1) == SSA_NAME)
     568       218605 :         remove_prop_source_from_use (rhs1);
     569       218605 :       if (TREE_CODE (rhs2) == SSA_NAME)
     570         5955 :         remove_prop_source_from_use (rhs2);
     571       218605 :       return is_gimple_min_invariant (tmp) ? 2 : 1;
     572              :     }
     573              : 
     574     19663019 :   if (canonicalize_bool_cond (stmt, gimple_bb (stmt)))
     575              :     return 1;
     576              : 
     577              :   return 0;
     578              : }
     579              : 
     580              : /* We've just substituted an ADDR_EXPR into stmt.  Update all the
     581              :    relevant data structures to match.  */
     582              : 
     583              : static void
     584      2005221 : tidy_after_forward_propagate_addr (gimple *stmt)
     585              : {
     586              :   /* We may have turned a trapping insn into a non-trapping insn.  */
     587      2005221 :   if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
     588           39 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
     589              : 
     590      2005221 :   if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
     591       249979 :      recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
     592      2005221 : }
     593              : 
     594              : /* NAME is a SSA_NAME representing DEF_RHS which is of the form
     595              :    ADDR_EXPR <whatever>.
     596              : 
     597              :    Try to forward propagate the ADDR_EXPR into the use USE_STMT.
     598              :    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
     599              :    node or for recovery of array indexing from pointer arithmetic.
     600              : 
     601              :    Return true if the propagation was successful (the propagation can
     602              :    be not totally successful, yet things may have been changed).  */
     603              : 
     604              : static bool
     605      2862515 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
     606              :                                gimple_stmt_iterator *use_stmt_gsi,
     607              :                                bool single_use_p)
     608              : {
     609      2862515 :   tree lhs, rhs, rhs2, array_ref;
     610      2862515 :   gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
     611      2862515 :   enum tree_code rhs_code;
     612      2862515 :   bool res = true;
     613              : 
     614      2862515 :   gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
     615              : 
     616      2862515 :   lhs = gimple_assign_lhs (use_stmt);
     617      2862515 :   rhs_code = gimple_assign_rhs_code (use_stmt);
     618      2862515 :   rhs = gimple_assign_rhs1 (use_stmt);
     619              : 
     620              :   /* Do not perform copy-propagation but recurse through copy chains.  */
     621      2862515 :   if (TREE_CODE (lhs) == SSA_NAME
     622      1424499 :       && rhs_code == SSA_NAME)
     623         8813 :     return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
     624              : 
     625              :   /* The use statement could be a conversion.  Recurse to the uses of the
     626              :      lhs as copyprop does not copy through pointer to integer to pointer
     627              :      conversions and FRE does not catch all cases either.
     628              :      Treat the case of a single-use name and
     629              :      a conversion to def_rhs type separate, though.  */
     630      2853702 :   if (TREE_CODE (lhs) == SSA_NAME
     631      1415686 :       && CONVERT_EXPR_CODE_P (rhs_code))
     632              :     {
     633              :       /* If there is a point in a conversion chain where the types match
     634              :          so we can remove a conversion re-materialize the address here
     635              :          and stop.  */
     636        25060 :       if (single_use_p
     637        25060 :           && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
     638              :         {
     639            2 :           gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
     640            2 :           gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
     641            2 :           return true;
     642              :         }
     643              : 
     644              :       /* Else recurse if the conversion preserves the address value.  */
     645        50116 :       if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
     646            2 :            || POINTER_TYPE_P (TREE_TYPE (lhs)))
     647        50116 :           && (TYPE_PRECISION (TREE_TYPE (lhs))
     648        25058 :               >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
     649        24991 :         return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
     650              : 
     651              :       return false;
     652              :     }
     653              : 
     654              :   /* If this isn't a conversion chain from this on we only can propagate
     655              :      into compatible pointer contexts.  */
     656      2828642 :   if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
     657              :     return false;
     658              : 
     659              :   /* Propagate through constant pointer adjustments.  */
     660      2807264 :   if (TREE_CODE (lhs) == SSA_NAME
     661      1371034 :       && rhs_code == POINTER_PLUS_EXPR
     662      1371034 :       && rhs == name
     663      2978625 :       && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
     664              :     {
     665       120544 :       tree new_def_rhs;
     666              :       /* As we come here with non-invariant addresses in def_rhs we need
     667              :          to make sure we can build a valid constant offsetted address
     668              :          for further propagation.  Simply rely on fold building that
     669              :          and check after the fact.  */
     670       120544 :       new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
     671              :                                  def_rhs,
     672              :                                  fold_convert (ptr_type_node,
     673              :                                                gimple_assign_rhs2 (use_stmt)));
     674       120544 :       if (TREE_CODE (new_def_rhs) == MEM_REF
     675       120544 :           && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
     676              :         return false;
     677       116429 :       new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs);
     678              : 
     679              :       /* Recurse.  If we could propagate into all uses of lhs do not
     680              :          bother to replace into the current use but just pretend we did.  */
     681       116429 :       if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
     682              :         return true;
     683              : 
     684        40236 :       if (useless_type_conversion_p (TREE_TYPE (lhs),
     685        40236 :                                      TREE_TYPE (new_def_rhs)))
     686        40236 :         gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
     687              :                                         new_def_rhs);
     688            0 :       else if (is_gimple_min_invariant (new_def_rhs))
     689            0 :         gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs);
     690              :       else
     691              :         return false;
     692        40236 :       gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
     693        40236 :       update_stmt (use_stmt);
     694        40236 :       return true;
     695              :     }
     696              : 
     697              :   /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
     698              :      ADDR_EXPR will not appear on the LHS.  */
     699      2686720 :   tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
     700      4077061 :   while (handled_component_p (*lhsp))
     701      1390341 :     lhsp = &TREE_OPERAND (*lhsp, 0);
     702      2686720 :   lhs = *lhsp;
     703              : 
     704              :   /* Now see if the LHS node is a MEM_REF using NAME.  If so,
     705              :      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
     706      2686720 :   if (TREE_CODE (lhs) == MEM_REF
     707      2686720 :       && TREE_OPERAND (lhs, 0) == name)
     708              :     {
     709       902202 :       tree def_rhs_base;
     710       902202 :       poly_int64 def_rhs_offset;
     711              :       /* If the address is invariant we can always fold it.  */
     712       902202 :       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
     713              :                                                          &def_rhs_offset)))
     714              :         {
     715       853992 :           poly_offset_int off = mem_ref_offset (lhs);
     716       853992 :           tree new_ptr;
     717       853992 :           off += def_rhs_offset;
     718       853992 :           if (TREE_CODE (def_rhs_base) == MEM_REF)
     719              :             {
     720       836536 :               off += mem_ref_offset (def_rhs_base);
     721       836536 :               new_ptr = TREE_OPERAND (def_rhs_base, 0);
     722              :             }
     723              :           else
     724        17456 :             new_ptr = build_fold_addr_expr (def_rhs_base);
     725       853992 :           TREE_OPERAND (lhs, 0) = new_ptr;
     726       853992 :           TREE_OPERAND (lhs, 1)
     727       853992 :             = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
     728       853992 :           tidy_after_forward_propagate_addr (use_stmt);
     729              :           /* Continue propagating into the RHS if this was not the only use.  */
     730       853992 :           if (single_use_p)
     731       230668 :             return true;
     732              :         }
     733              :       /* If the LHS is a plain dereference and the value type is the same as
     734              :          that of the pointed-to type of the address we can put the
     735              :          dereferenced address on the LHS preserving the original alias-type.  */
     736        48210 :       else if (integer_zerop (TREE_OPERAND (lhs, 1))
     737        18736 :                && ((gimple_assign_lhs (use_stmt) == lhs
     738        15112 :                     && useless_type_conversion_p
     739        15112 :                          (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
     740        15112 :                           TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
     741        13729 :                    || types_compatible_p (TREE_TYPE (lhs),
     742        13729 :                                           TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
     743              :                /* Don't forward anything into clobber stmts if it would result
     744              :                   in the lhs no longer being a MEM_REF.  */
     745        56471 :                && (!gimple_clobber_p (use_stmt)
     746          166 :                    || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
     747              :         {
     748         8095 :           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
     749         8095 :           tree new_offset, new_base, saved, new_lhs;
     750        29329 :           while (handled_component_p (*def_rhs_basep))
     751        13139 :             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
     752         8095 :           saved = *def_rhs_basep;
     753         8095 :           if (TREE_CODE (*def_rhs_basep) == MEM_REF)
     754              :             {
     755         3947 :               new_base = TREE_OPERAND (*def_rhs_basep, 0);
     756         3947 :               new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
     757              :                                          TREE_OPERAND (*def_rhs_basep, 1));
     758              :             }
     759              :           else
     760              :             {
     761         4148 :               new_base = build_fold_addr_expr (*def_rhs_basep);
     762         4148 :               new_offset = TREE_OPERAND (lhs, 1);
     763              :             }
     764         8095 :           tree atype = TREE_TYPE (*def_rhs_basep);
     765         8095 :           if (TYPE_ALIGN (TREE_TYPE (lhs)) < TYPE_ALIGN (atype))
     766          312 :             atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (lhs)));
     767         8095 :           *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
     768         8095 :           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
     769         8095 :           TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
     770         8095 :           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
     771         8095 :           new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
     772         8095 :           *lhsp = new_lhs;
     773         8095 :           TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
     774         8095 :           TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
     775         8095 :           *def_rhs_basep = saved;
     776         8095 :           tidy_after_forward_propagate_addr (use_stmt);
     777              :           /* Continue propagating into the RHS if this was not the
     778              :              only use.  */
     779         8095 :           if (single_use_p)
     780              :             return true;
     781              :         }
     782              :       else
     783              :         /* We can have a struct assignment dereferencing our name twice.
     784              :            Note that we didn't propagate into the lhs to not falsely
     785              :            claim we did when propagating into the rhs.  */
     786              :         res = false;
     787              :     }
     788              : 
     789              :   /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
     790              :      nodes from the RHS.  */
     791      2451784 :   tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
     792      2451784 :   if (TREE_CODE (*rhsp) == ADDR_EXPR)
     793       238365 :     rhsp = &TREE_OPERAND (*rhsp, 0);
     794      3468978 :   while (handled_component_p (*rhsp))
     795      1017194 :     rhsp = &TREE_OPERAND (*rhsp, 0);
     796      2451784 :   rhs = *rhsp;
     797              : 
     798              :   /* Now see if the RHS node is a MEM_REF using NAME.  If so,
     799              :      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
     800      2451784 :   if (TREE_CODE (rhs) == MEM_REF
     801      2451784 :       && TREE_OPERAND (rhs, 0) == name)
     802              :     {
     803      1165689 :       tree def_rhs_base;
     804      1165689 :       poly_int64 def_rhs_offset;
     805      1165689 :       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
     806              :                                                          &def_rhs_offset)))
     807              :         {
     808      1127378 :           poly_offset_int off = mem_ref_offset (rhs);
     809      1127378 :           tree new_ptr;
     810      1127378 :           off += def_rhs_offset;
     811      1127378 :           if (TREE_CODE (def_rhs_base) == MEM_REF)
     812              :             {
     813      1101130 :               off += mem_ref_offset (def_rhs_base);
     814      1101130 :               new_ptr = TREE_OPERAND (def_rhs_base, 0);
     815              :             }
     816              :           else
     817        26248 :             new_ptr = build_fold_addr_expr (def_rhs_base);
     818      1127378 :           TREE_OPERAND (rhs, 0) = new_ptr;
     819      1127378 :           TREE_OPERAND (rhs, 1)
     820      1127378 :             = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
     821      1127378 :           fold_stmt_inplace (use_stmt_gsi);
     822      1127378 :           tidy_after_forward_propagate_addr (use_stmt);
     823      1127378 :           return res;
     824              :         }
     825              :       /* If the RHS is a plain dereference and the value type is the same as
     826              :          that of the pointed-to type of the address we can put the
     827              :          dereferenced address on the RHS preserving the original alias-type.  */
     828        38311 :       else if (integer_zerop (TREE_OPERAND (rhs, 1))
     829        38311 :                && ((gimple_assign_rhs1 (use_stmt) == rhs
     830        20848 :                     && useless_type_conversion_p
     831        20848 :                          (TREE_TYPE (gimple_assign_lhs (use_stmt)),
     832        20848 :                           TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
     833        22942 :                    || types_compatible_p (TREE_TYPE (rhs),
     834        22942 :                                           TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
     835              :         {
     836        15756 :           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
     837        15756 :           tree new_offset, new_base, saved, new_rhs;
     838        55949 :           while (handled_component_p (*def_rhs_basep))
     839        24437 :             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
     840        15756 :           saved = *def_rhs_basep;
     841        15756 :           if (TREE_CODE (*def_rhs_basep) == MEM_REF)
     842              :             {
     843         7498 :               new_base = TREE_OPERAND (*def_rhs_basep, 0);
     844         7498 :               new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
     845              :                                          TREE_OPERAND (*def_rhs_basep, 1));
     846              :             }
     847              :           else
     848              :             {
     849         8258 :               new_base = build_fold_addr_expr (*def_rhs_basep);
     850         8258 :               new_offset = TREE_OPERAND (rhs, 1);
     851              :             }
     852        15756 :           tree atype = TREE_TYPE (*def_rhs_basep);
     853        15756 :           if (TYPE_ALIGN (TREE_TYPE (rhs)) < TYPE_ALIGN (atype))
     854          574 :             atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (rhs)));
     855        15756 :           *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
     856        15756 :           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
     857        15756 :           TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
     858        15756 :           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
     859        15756 :           new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
     860        15756 :           *rhsp = new_rhs;
     861        15756 :           TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
     862        15756 :           TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
     863        15756 :           *def_rhs_basep = saved;
     864        15756 :           fold_stmt_inplace (use_stmt_gsi);
     865        15756 :           tidy_after_forward_propagate_addr (use_stmt);
     866        15756 :           return res;
     867              :         }
     868              :     }
     869              : 
     870              :   /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
     871              :      is nothing to do. */
     872      1308650 :   if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
     873      1308650 :       || gimple_assign_rhs1 (use_stmt) != name)
     874              :     return false;
     875              : 
     876              :   /* The remaining cases are all for turning pointer arithmetic into
     877              :      array indexing.  They only apply when we have the address of
     878              :      element zero in an array.  If that is not the case then there
     879              :      is nothing to do.  */
     880        50817 :   array_ref = TREE_OPERAND (def_rhs, 0);
     881        50817 :   if ((TREE_CODE (array_ref) != ARRAY_REF
     882         4665 :        || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
     883         4665 :        || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
     884        52335 :       && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
     885              :     return false;
     886              : 
     887        27171 :   rhs2 = gimple_assign_rhs2 (use_stmt);
     888              :   /* Optimize &x[C1] p+ C2 to  &x p+ C3 with C3 = C1 * element_size + C2.  */
     889        27171 :   if (TREE_CODE (rhs2) == INTEGER_CST)
     890              :     {
     891            0 :       tree new_rhs = build1_loc (gimple_location (use_stmt),
     892            0 :                                  ADDR_EXPR, TREE_TYPE (def_rhs),
     893            0 :                                  fold_build2 (MEM_REF,
     894              :                                               TREE_TYPE (TREE_TYPE (def_rhs)),
     895              :                                               unshare_expr (def_rhs),
     896              :                                               fold_convert (ptr_type_node,
     897              :                                                             rhs2)));
     898            0 :       gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
     899            0 :       use_stmt = gsi_stmt (*use_stmt_gsi);
     900            0 :       update_stmt (use_stmt);
     901            0 :       tidy_after_forward_propagate_addr (use_stmt);
     902            0 :       return true;
     903              :     }
     904              : 
     905              :   return false;
     906              : }
     907              : 
     908              : /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
     909              : 
     910              :    Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
     911              :    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
     912              :    node or for recovery of array indexing from pointer arithmetic.
     913              : 
     914              :    PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
     915              :    the single use in the previous invocation.  Pass true when calling
     916              :    this as toplevel.
     917              : 
     918              :    Returns true, if all uses have been propagated into.  */
     919              : 
     920              : static bool
     921      3320137 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
     922              : {
     923      3320137 :   bool all = true;
     924      3320137 :   bool single_use_p = parent_single_use_p && has_single_use (name);
     925              : 
     926     17506107 :   for (gimple *use_stmt : gather_imm_use_stmts (name))
     927              :     {
     928      7545696 :       bool result;
     929      7545696 :       tree use_rhs;
     930              : 
     931              :       /* If the use is not in a simple assignment statement, then
     932              :          there is nothing we can do.  */
     933      7545696 :       auto assign = dyn_cast<gassign *> (use_stmt);
     934     12228877 :       if (!assign)
     935              :         {
     936      4683181 :           if (!is_gimple_debug (use_stmt))
     937      1930163 :             all = false;
     938      4683181 :           continue;
     939              :         }
     940              : 
     941      2862515 :       gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
     942      2862515 :       result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
     943              :                                               single_use_p);
     944              :       /* If the use has moved to a different statement adjust
     945              :          the update machinery for the old statement too.  */
     946      2862515 :       if (use_stmt != gsi_stmt (gsi))
     947              :         {
     948            0 :           update_stmt (assign);
     949            0 :           use_stmt = gsi_stmt (gsi);
     950              :         }
     951      2862515 :       update_stmt (use_stmt);
     952      2862515 :       all &= result;
     953              : 
     954              :       /* Remove intermediate now unused copy and conversion chains.  */
     955      2862515 :       use_rhs = gimple_assign_rhs1 (assign);
     956      2862515 :       if (result
     957      1499769 :           && TREE_CODE (gimple_assign_lhs (assign)) == SSA_NAME
     958      1251775 :           && TREE_CODE (use_rhs) == SSA_NAME
     959      2943976 :           && has_zero_uses (gimple_assign_lhs (assign)))
     960              :         {
     961        81461 :           gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
     962        81461 :           fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
     963        81461 :           release_defs (use_stmt);
     964        81461 :           gsi_remove (&gsi, true);
     965              :         }
     966      3320137 :     }
     967              : 
     968      3320137 :   return all && has_zero_uses (name);
     969              : }
     970              : 
     971              : 
     972              : /* Helper function for simplify_gimple_switch.  Remove case labels that
     973              :    have values outside the range of the new type.  */
     974              : 
     975              : static void
     976        11792 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
     977              :                                   vec<std::pair<int, int> > &edges_to_remove)
     978              : {
     979        11792 :   unsigned int branch_num = gimple_switch_num_labels (stmt);
     980        11792 :   auto_vec<tree> labels (branch_num);
     981        11792 :   unsigned int i, len;
     982              : 
     983              :   /* Collect the existing case labels in a VEC, and preprocess it as if
     984              :      we are gimplifying a GENERIC SWITCH_EXPR.  */
     985        75152 :   for (i = 1; i < branch_num; i++)
     986        51568 :     labels.quick_push (gimple_switch_label (stmt, i));
     987        11792 :   preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
     988              : 
     989              :   /* If any labels were removed, replace the existing case labels
     990              :      in the GIMPLE_SWITCH statement with the correct ones.
     991              :      Note that the type updates were done in-place on the case labels,
     992              :      so we only have to replace the case labels in the GIMPLE_SWITCH
     993              :      if the number of labels changed.  */
     994        11792 :   len = labels.length ();
     995        11792 :   if (len < branch_num - 1)
     996              :     {
     997            0 :       bitmap target_blocks;
     998            0 :       edge_iterator ei;
     999            0 :       edge e;
    1000              : 
    1001              :       /* Corner case: *all* case labels have been removed as being
    1002              :          out-of-range for INDEX_TYPE.  Push one label and let the
    1003              :          CFG cleanups deal with this further.  */
    1004            0 :       if (len == 0)
    1005              :         {
    1006            0 :           tree label, elt;
    1007              : 
    1008            0 :           label = CASE_LABEL (gimple_switch_default_label (stmt));
    1009            0 :           elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
    1010            0 :           labels.quick_push (elt);
    1011            0 :           len = 1;
    1012              :         }
    1013              : 
    1014            0 :       for (i = 0; i < labels.length (); i++)
    1015            0 :         gimple_switch_set_label (stmt, i + 1, labels[i]);
    1016            0 :       for (i++ ; i < branch_num; i++)
    1017            0 :         gimple_switch_set_label (stmt, i, NULL_TREE);
    1018            0 :       gimple_switch_set_num_labels (stmt, len + 1);
    1019              : 
    1020              :       /* Cleanup any edges that are now dead.  */
    1021            0 :       target_blocks = BITMAP_ALLOC (NULL);
    1022            0 :       for (i = 0; i < gimple_switch_num_labels (stmt); i++)
    1023              :         {
    1024            0 :           tree elt = gimple_switch_label (stmt, i);
    1025            0 :           basic_block target = label_to_block (cfun, CASE_LABEL (elt));
    1026            0 :           bitmap_set_bit (target_blocks, target->index);
    1027              :         }
    1028            0 :       for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
    1029              :         {
    1030            0 :           if (! bitmap_bit_p (target_blocks, e->dest->index))
    1031            0 :             edges_to_remove.safe_push (std::make_pair (e->src->index,
    1032            0 :                                                        e->dest->index));
    1033              :           else
    1034            0 :             ei_next (&ei);
    1035              :         }
    1036            0 :       BITMAP_FREE (target_blocks);
    1037              :     }
    1038        11792 : }
    1039              : 
    1040              : /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
    1041              :    the condition which we may be able to optimize better.  */
    1042              : 
    1043              : static bool
    1044       111627 : simplify_gimple_switch (gswitch *stmt,
    1045              :                         vec<std::pair<int, int> > &edges_to_remove,
    1046              :                         bitmap simple_dce_worklist)
    1047              : {
    1048              :   /* The optimization that we really care about is removing unnecessary
    1049              :      casts.  That will let us do much better in propagating the inferred
    1050              :      constant at the switch target.  */
    1051       111627 :   tree cond = gimple_switch_index (stmt);
    1052       111627 :   if (TREE_CODE (cond) == SSA_NAME)
    1053              :     {
    1054       111626 :       gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
    1055       111626 :       if (gimple_assign_cast_p (def_stmt))
    1056              :         {
    1057        12773 :           tree def = gimple_assign_rhs1 (def_stmt);
    1058        12773 :           if (TREE_CODE (def) != SSA_NAME)
    1059              :             return false;
    1060              : 
    1061              :           /* If we have an extension or sign-change that preserves the
    1062              :              values we check against then we can copy the source value into
    1063              :              the switch.  */
    1064        12773 :           tree ti = TREE_TYPE (def);
    1065        12773 :           if (INTEGRAL_TYPE_P (ti)
    1066        12773 :               && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
    1067              :             {
    1068        12503 :               size_t n = gimple_switch_num_labels (stmt);
    1069        12503 :               tree min = NULL_TREE, max = NULL_TREE;
    1070        12503 :               if (n > 1)
    1071              :                 {
    1072        12503 :                   min = CASE_LOW (gimple_switch_label (stmt, 1));
    1073        12503 :                   if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
    1074          186 :                     max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
    1075              :                   else
    1076        12317 :                     max = CASE_LOW (gimple_switch_label (stmt, n - 1));
    1077              :                 }
    1078        12503 :               if ((!min || int_fits_type_p (min, ti))
    1079        12495 :                   && (!max || int_fits_type_p (max, ti)))
    1080              :                 {
    1081        11792 :                   bitmap_set_bit (simple_dce_worklist,
    1082        11792 :                                   SSA_NAME_VERSION (cond));
    1083        11792 :                   gimple_switch_set_index (stmt, def);
    1084        11792 :                   simplify_gimple_switch_label_vec (stmt, ti,
    1085              :                                                     edges_to_remove);
    1086        11792 :                   update_stmt (stmt);
    1087        11792 :                   return true;
    1088              :                 }
    1089              :             }
    1090              :         }
    1091              :     }
    1092              : 
    1093              :   return false;
    1094              : }
    1095              : 
    1096              : /* For pointers p2 and p1 return p2 - p1 if the
    1097              :    difference is known and constant, otherwise return NULL.  */
    1098              : 
    1099              : static tree
    1100         5521 : constant_pointer_difference (tree p1, tree p2)
    1101              : {
    1102         5521 :   int i, j;
    1103              : #define CPD_ITERATIONS 5
    1104         5521 :   tree exps[2][CPD_ITERATIONS];
    1105         5521 :   tree offs[2][CPD_ITERATIONS];
    1106         5521 :   int cnt[2];
    1107              : 
    1108        16563 :   for (i = 0; i < 2; i++)
    1109              :     {
    1110        11042 :       tree p = i ? p1 : p2;
    1111        11042 :       tree off = size_zero_node;
    1112        11042 :       gimple *stmt;
    1113        11042 :       enum tree_code code;
    1114              : 
    1115              :       /* For each of p1 and p2 we need to iterate at least
    1116              :          twice, to handle ADDR_EXPR directly in p1/p2,
    1117              :          SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
    1118              :          on definition's stmt RHS.  Iterate a few extra times.  */
    1119        11042 :       j = 0;
    1120        12848 :       do
    1121              :         {
    1122        12848 :           if (!POINTER_TYPE_P (TREE_TYPE (p)))
    1123              :             break;
    1124        12842 :           if (TREE_CODE (p) == ADDR_EXPR)
    1125              :             {
    1126         9671 :               tree q = TREE_OPERAND (p, 0);
    1127         9671 :               poly_int64 offset;
    1128         9671 :               tree base = get_addr_base_and_unit_offset (q, &offset);
    1129         9671 :               if (base)
    1130              :                 {
    1131         8871 :                   q = base;
    1132         8871 :                   if (maybe_ne (offset, 0))
    1133         3786 :                     off = size_binop (PLUS_EXPR, off, size_int (offset));
    1134              :                 }
    1135         9671 :               if (TREE_CODE (q) == MEM_REF
    1136         9671 :                   && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
    1137              :                 {
    1138          155 :                   p = TREE_OPERAND (q, 0);
    1139          155 :                   off = size_binop (PLUS_EXPR, off,
    1140              :                                     wide_int_to_tree (sizetype,
    1141              :                                                       mem_ref_offset (q)));
    1142              :                 }
    1143              :               else
    1144              :                 {
    1145         9516 :                   exps[i][j] = q;
    1146         9516 :                   offs[i][j++] = off;
    1147         9516 :                   break;
    1148              :                 }
    1149              :             }
    1150         3326 :           if (TREE_CODE (p) != SSA_NAME)
    1151              :             break;
    1152         3326 :           exps[i][j] = p;
    1153         3326 :           offs[i][j++] = off;
    1154         3326 :           if (j == CPD_ITERATIONS)
    1155              :             break;
    1156         3326 :           stmt = SSA_NAME_DEF_STMT (p);
    1157         3326 :           auto assign = dyn_cast<gassign *> (stmt);
    1158         2659 :           if (!assign || gimple_assign_lhs (assign) != p)
    1159              :             break;
    1160         2659 :           code = gimple_assign_rhs_code (assign);
    1161         2659 :           if (code == POINTER_PLUS_EXPR)
    1162              :             {
    1163         2796 :               if (TREE_CODE (gimple_assign_rhs2 (assign)) != INTEGER_CST)
    1164              :                 break;
    1165          871 :               off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (assign));
    1166          871 :               p = gimple_assign_rhs1 (assign);
    1167              :             }
    1168              :           else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code))
    1169          935 :             p = gimple_assign_rhs1 (assign);
    1170              :           else
    1171              :             break;
    1172              :         }
    1173              :       while (1);
    1174        11042 :       cnt[i] = j;
    1175              :     }
    1176              : 
    1177         7513 :   for (i = 0; i < cnt[0]; i++)
    1178         9780 :     for (j = 0; j < cnt[1]; j++)
    1179         7788 :       if (exps[0][i] == exps[1][j])
    1180         4636 :         return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
    1181              : 
    1182              :   return NULL_TREE;
    1183              : }
    1184              : 
    1185              : /* Helper function for optimize_aggr_zeroprop.
    1186              :    Props the zeroing (memset, VAL) that was done in DEST+OFFSET:LEN
    1187              :    (DEFSTMT) into the STMT.  Returns true if the STMT was updated.  */
    1188              : static void
    1189     22713997 : optimize_aggr_zeroprop_1 (gimple *defstmt, gimple *stmt,
    1190              :                           tree dest, poly_int64 offset, tree val,
    1191              :                           poly_offset_int len)
    1192              : {
    1193     22713997 :   tree src2;
    1194     22713997 :   tree len2 = NULL_TREE;
    1195     22713997 :   poly_int64 offset2;
    1196              : 
    1197     22713997 :   if (gimple_call_builtin_p (stmt, BUILT_IN_MEMCPY)
    1198        22383 :       && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR
    1199     22728222 :       && poly_int_tree_p (gimple_call_arg (stmt, 2)))
    1200              :     {
    1201        13041 :       src2 = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
    1202        13041 :       len2 = gimple_call_arg (stmt, 2);
    1203              :     }
    1204     22700956 :    else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
    1205              :      {
    1206      1922673 :         src2 = gimple_assign_rhs1 (stmt);
    1207      1922673 :         len2 = (TREE_CODE (src2) == COMPONENT_REF
    1208      1922673 :                 ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
    1209      1754258 :                 : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
    1210              :         /* Can only handle zero memsets. */
    1211      1922673 :         if (!integer_zerop (val))
    1212     22692970 :           return;
    1213              :      }
    1214              :    else
    1215              :      return;
    1216              : 
    1217      1934589 :   if (len2 == NULL_TREE
    1218      1934589 :       || !poly_int_tree_p (len2))
    1219              :     return;
    1220              : 
    1221      1934589 :   src2 = get_addr_base_and_unit_offset (src2, &offset2);
    1222      1934589 :   if (src2 == NULL_TREE
    1223      1934589 :       || maybe_lt (offset2, offset))
    1224              :     return;
    1225              : 
    1226       880309 :   if (!operand_equal_p (dest, src2, 0))
    1227              :     return;
    1228              : 
    1229              :   /* [ dest + offset, dest + offset + len - 1 ] is set to val.
    1230              :      Make sure that
    1231              :      [ dest + offset2, dest + offset2 + len2 - 1 ] is a subset of that.  */
    1232       131164 :   if (maybe_gt (wi::to_poly_offset (len2) + (offset2 - offset),
    1233              :                 len))
    1234              :     return;
    1235              : 
    1236        21027 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1237              :     {
    1238           32 :       fprintf (dump_file, "Simplified\n  ");
    1239           32 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1240           32 :       fprintf (dump_file, "after previous\n  ");
    1241           32 :       print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1242              :     }
    1243        21027 :   gimple *orig_stmt = stmt;
    1244              :   /* For simplicity, don't change the kind of the stmt,
    1245              :      turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
    1246              :      into memset (&dest, val, len);
    1247              :      In theory we could change dest = src into memset if dest
    1248              :      is addressable (maybe beneficial if val is not 0), or
    1249              :      memcpy (&dest, &src, len) into dest = {} if len is the size
    1250              :      of dest, dest isn't volatile.  */
    1251        21027 :   if (auto assign = dyn_cast<gassign *> (stmt))
    1252              :     {
    1253        21022 :       tree ctor_type = TREE_TYPE (gimple_assign_lhs (assign));
    1254        21022 :       tree ctor = build_constructor (ctor_type, NULL);
    1255        21022 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    1256        21022 :       gimple_assign_set_rhs_from_tree (&gsi, ctor);
    1257        21022 :       update_stmt (stmt);
    1258        21022 :       statistics_counter_event (cfun, "copy zeroing propagation of aggregate", 1);
    1259              :     }
    1260              :   else /* If stmt is memcpy, transform it into memset.  */
    1261              :     {
    1262            5 :       gcall *call = as_a <gcall *> (stmt);
    1263            5 :       tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET);
    1264            5 :       gimple_call_set_fndecl (call, fndecl);
    1265            5 :       gimple_call_set_fntype (call, TREE_TYPE (fndecl));
    1266            5 :       gimple_call_set_arg (call, 1, val);
    1267            5 :       update_stmt (stmt);
    1268            5 :       statistics_counter_event (cfun, "memcpy to memset changed", 1);
    1269              :     }
    1270              : 
    1271        21027 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1272              :     {
    1273           32 :       fprintf (dump_file, "into\n  ");
    1274           32 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1275              :     }
    1276              : 
    1277              :   /* Mark the bb for eh cleanup if needed.  */
    1278        21027 :   if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
    1279            6 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
    1280              : }
    1281              : 
    1282              : /* Optimize
    1283              :    a = {}; // DEST = value ;; LEN(nullptr)
    1284              :    b = a;
    1285              :    into
    1286              :    a = {};
    1287              :    b = {};
    1288              :    Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
    1289              :    and/or memcpy (&b, &a, sizeof (a)); instead of b = a;  */
    1290              : 
    1291              : static void
    1292     32179609 : optimize_aggr_zeroprop (gimple *stmt, bool full_walk)
    1293              : {
    1294     32179609 :   ao_ref read;
    1295     64359218 :   if (gimple_has_volatile_ops (stmt))
    1296     28088152 :     return;
    1297              : 
    1298     31021205 :   tree dest = NULL_TREE;
    1299     31021205 :   tree val = integer_zero_node;
    1300     31021205 :   tree len = NULL_TREE;
    1301     31021205 :   bool can_use_tbba = true;
    1302              : 
    1303     31021205 :   if (gimple_call_builtin_p (stmt, BUILT_IN_MEMSET)
    1304       117330 :       && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR
    1305        61171 :       && TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
    1306     31080073 :       && poly_int_tree_p (gimple_call_arg (stmt, 2)))
    1307              :     {
    1308        56023 :       dest = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
    1309        56023 :       len = gimple_call_arg (stmt, 2);
    1310        56023 :       val = gimple_call_arg (stmt, 1);
    1311        56023 :       ao_ref_init_from_ptr_and_size (&read, gimple_call_arg (stmt, 0), len);
    1312        56023 :       can_use_tbba = false;
    1313              :     }
    1314     30965182 :   else if (gimple_store_p (stmt)
    1315     30903715 :            && gimple_assign_single_p (stmt)
    1316     61868897 :            && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST)
    1317              :     {
    1318        31030 :       tree str = gimple_assign_rhs1 (stmt);
    1319        31030 :       dest = gimple_assign_lhs (stmt);
    1320        31030 :       ao_ref_init (&read, dest);
    1321              :       /* The string must contain all null char's for now.  */
    1322        67168 :       for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
    1323              :         {
    1324        33751 :           if (TREE_STRING_POINTER (str)[i] != 0)
    1325              :             {
    1326              :               dest = NULL_TREE;
    1327              :               break;
    1328              :             }
    1329              :         }
    1330              :     }
    1331              :   /* A store of integer (scalar, vector or complex) zeros is
    1332              :      a zero store. */
    1333     30934152 :   else if (gimple_store_p (stmt)
    1334     30872685 :            && gimple_assign_single_p (stmt)
    1335     61806837 :            && integer_zerop (gimple_assign_rhs1 (stmt)))
    1336              :     {
    1337      3587009 :       tree rhs = gimple_assign_rhs1 (stmt);
    1338      3587009 :       tree type = TREE_TYPE (rhs);
    1339      3587009 :       dest = gimple_assign_lhs (stmt);
    1340      3587009 :       ao_ref_init (&read, dest);
    1341              :       /* For integral types, the type precision needs to be a multiply of BITS_PER_UNIT. */
    1342      3587009 :       if (INTEGRAL_TYPE_P (type)
    1343      3587009 :           && (TYPE_PRECISION (type) % BITS_PER_UNIT) != 0)
    1344              :         dest = NULL_TREE;
    1345              :     }
    1346     27347143 :   else if (gimple_store_p (stmt)
    1347     27285676 :            && gimple_assign_single_p (stmt)
    1348     27285676 :            && TREE_CODE (gimple_assign_rhs1 (stmt)) == CONSTRUCTOR
    1349     28092894 :            && !gimple_clobber_p (stmt))
    1350              :     {
    1351       745751 :       dest = gimple_assign_lhs (stmt);
    1352       745751 :       ao_ref_init (&read, dest);
    1353              :     }
    1354              : 
    1355      4202850 :   if (dest == NULL_TREE)
    1356              :     return;
    1357              : 
    1358      4174207 :   if (len == NULL_TREE)
    1359      4118184 :     len = (TREE_CODE (dest) == COMPONENT_REF
    1360      4118184 :            ? DECL_SIZE_UNIT (TREE_OPERAND (dest, 1))
    1361      1796049 :            : TYPE_SIZE_UNIT (TREE_TYPE (dest)));
    1362      4118184 :   if (len == NULL_TREE
    1363      4174207 :       || !poly_int_tree_p (len))
    1364              :     return;
    1365              : 
    1366              :   /* Sometimes memset can have no vdef due to invalid declaration of memset (const, etc.).  */
    1367     36436542 :   if (!gimple_vdef (stmt))
    1368              :     return;
    1369              : 
    1370              :   /* This store needs to be on the byte boundary and pointing to an object.  */
    1371      4174183 :   poly_int64 offset;
    1372      4174183 :   tree dest_base = get_addr_base_and_unit_offset (dest, &offset);
    1373      4174183 :   if (dest_base == NULL_TREE)
    1374              :     return;
    1375              : 
    1376              :   /* Setup the worklist.  */
    1377      4091457 :   auto_vec<std::pair<tree, unsigned>> worklist;
    1378      4091457 :   unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 0;
    1379      8182914 :   worklist.safe_push (std::make_pair (gimple_vdef (stmt), limit));
    1380              : 
    1381     28362986 :   while (!worklist.is_empty ())
    1382              :     {
    1383     20180072 :       std::pair<tree, unsigned> top = worklist.pop ();
    1384     20180072 :       tree vdef = top.first;
    1385     20180072 :       limit = top.second;
    1386     20180072 :       gimple *use_stmt;
    1387     20180072 :       imm_use_iterator iter;
    1388     44950095 :       FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
    1389              :         {
    1390              :           /* Handling PHI nodes might not be worth it so don't.  */
    1391     24770023 :           if (is_a <gphi*> (use_stmt))
    1392      2056026 :             continue;
    1393              : 
    1394              :           /* If this statement does not clobber add the vdef stmt to the
    1395              :              worklist.
    1396              :              After hitting the limit, allow clobbers to able to pass through.  */
    1397      2067892 :           if ((limit != 0 || gimple_clobber_p (use_stmt))
    1398     20682579 :               && gimple_vdef (use_stmt)
    1399     40258184 :               && !stmt_may_clobber_ref_p_1 (use_stmt, &read,
    1400              :                                            /* tbaa_p = */ can_use_tbba))
    1401              :               {
    1402     16088615 :                 unsigned new_limit = limit == 0 ? 0 : limit - 1;
    1403     32177230 :                 worklist.safe_push (std::make_pair (gimple_vdef (use_stmt),
    1404              :                                                     new_limit));
    1405              :               }
    1406              : 
    1407     22713997 :            optimize_aggr_zeroprop_1 (stmt, use_stmt, dest_base, offset,
    1408     22713997 :                                      val, wi::to_poly_offset (len));
    1409     20180072 :         }
    1410              :     }
    1411              : 
    1412      4091457 : }
    1413              : 
    1414              : /* Returns the pointer to the base of the object of the
    1415              :    reference EXPR and extracts the information about
    1416              :    the offset of the access, storing it to PBYTESIZE,
    1417              :    PBYTEPOS and PREVERSEP.
    1418              :    If the access is not a byte sized or position is not
    1419              :    on the byte, return NULL.  */
    1420              : static tree
    1421      5371518 : split_core_and_offset_size (tree expr,
    1422              :                             poly_int64 *pbytesize, poly_int64 *pbytepos,
    1423              :                             tree *poffset, int *preversep)
    1424              : {
    1425      5371518 :   tree core;
    1426      5371518 :   machine_mode mode;
    1427      5371518 :   int unsignedp, volatilep;
    1428      5371518 :   poly_int64 bitsize;
    1429      5371518 :   poly_int64 bitpos;
    1430      5371518 :   location_t loc = EXPR_LOCATION (expr);
    1431              : 
    1432      5371518 :   core = get_inner_reference (expr, &bitsize, &bitpos,
    1433              :                               poffset, &mode, &unsignedp, preversep,
    1434              :                               &volatilep);
    1435     10743036 :   if (!multiple_p (bitsize, BITS_PER_UNIT, pbytesize))
    1436              :     return NULL_TREE;
    1437      5371518 :   if (!multiple_p (bitpos, BITS_PER_UNIT, pbytepos))
    1438              :     return NULL_TREE;
    1439              :   /* If we are left with MEM[a + CST] strip that and add it to the
    1440              :      pbytepos and return a. */
    1441      5371518 :   if (TREE_CODE (core) == MEM_REF)
    1442              :     {
    1443      1264200 :       poly_offset_int tem;
    1444      1264200 :       tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
    1445      1264200 :       tem += *pbytepos;
    1446      1264200 :       if (tem.to_shwi (pbytepos))
    1447      1261958 :         return TREE_OPERAND (core, 0);
    1448              :     }
    1449      4109560 :   core = build_fold_addr_expr_loc (loc, core);
    1450      4109560 :   STRIP_NOPS (core);
    1451      4109560 :   return core;
    1452              : }
    1453              : 
    1454              : /* Returns a new src based on the
    1455              :    copy `DEST = SRC` and for the old SRC2.
    1456              :    Returns null if SRC2 is not related to DEST.  */
    1457              : 
    1458              : static tree
    1459      1281441 : new_src_based_on_copy (tree src2, tree dest, tree src)
    1460              : {
    1461              :   /* If the second src is not exactly the same as dest,
    1462              :      try to handle it separately; see it is address/size equivalent.
    1463              :      Handles `a` and `a.b` and `MEM<char[N]>(&a)` which all have
    1464              :      the same size and offsets as address/size equivalent.
    1465              :      This allows copying over a memcpy and also one for copying
    1466              :      where one field is the same size as the whole struct.  */
    1467      1281441 :   if (operand_equal_p (dest, src2))
    1468              :     return src;
    1469              :   /* if both dest and src2 are decls, then we know these 2
    1470              :      accesses can't be the same.  */
    1471       731189 :   if (DECL_P (dest) && DECL_P (src2))
    1472              :     return NULL_TREE;
    1473              :   /* A VCE can't be used with imag/real or BFR so reject them early. */
    1474       383298 :   if (TREE_CODE (src) == IMAGPART_EXPR
    1475       383298 :       || TREE_CODE (src) == REALPART_EXPR
    1476       383298 :       || TREE_CODE (src) == BIT_FIELD_REF)
    1477              :     return NULL_TREE;
    1478       383298 :   tree core1, core2;
    1479       383298 :   poly_int64 bytepos1, bytepos2;
    1480       383298 :   poly_int64 bytesize1, bytesize2;
    1481       383298 :   tree toffset1, toffset2;
    1482       383298 :   int reversep1 = 0;
    1483       383298 :   int reversep2 = 0;
    1484       383298 :   poly_int64 diff = 0;
    1485       383298 :   core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
    1486              :                                           &toffset1, &reversep1);
    1487       383298 :   core2 = split_core_and_offset_size (src2, &bytesize2, &bytepos2,
    1488              :                                           &toffset2, &reversep2);
    1489       383298 :   if (!core1 || !core2)
    1490              :     return NULL_TREE;
    1491       383298 :   if (reversep1 != reversep2)
    1492              :     return NULL_TREE;
    1493              :   /* The sizes of the 2 accesses need to be the same. */
    1494       383298 :   if (!known_eq (bytesize1, bytesize2))
    1495              :     return NULL_TREE;
    1496       175914 :   if (!operand_equal_p (core1, core2, 0))
    1497              :     return NULL_TREE;
    1498              : 
    1499        23622 :   if (toffset1 && toffset2)
    1500              :     {
    1501            2 :       tree type = TREE_TYPE (toffset1);
    1502            2 :       if (type != TREE_TYPE (toffset2))
    1503            0 :         toffset2 = fold_convert (type, toffset2);
    1504              : 
    1505            2 :       tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
    1506            2 :       if (!cst_and_fits_in_hwi (tdiff))
    1507              :         return NULL_TREE;
    1508              : 
    1509            0 :       diff = int_cst_value (tdiff);
    1510            0 :     }
    1511        23620 :   else if (toffset1 || toffset2)
    1512              :     {
    1513              :       /* If only one of the offsets is non-constant, the difference cannot
    1514              :          be a constant.  */
    1515              :       return NULL_TREE;
    1516              :     }
    1517        23588 :   diff += bytepos1 - bytepos2;
    1518              :   /* The offset between the 2 need to be 0. */
    1519        23588 :   if (!known_eq (diff, 0))
    1520              :     return NULL_TREE;
    1521        22747 :   return fold_build1 (VIEW_CONVERT_EXPR,TREE_TYPE (src2), src);
    1522              : }
    1523              : 
    1524              : /* Returns true if SRC and DEST are the same address such that
    1525              :    `SRC == DEST;` is considered a nop. This is more than an
    1526              :    operand_equal_p check as it needs to be similar to
    1527              :    new_src_based_on_copy.  */
    1528              : 
    1529              : static bool
    1530      4557011 : same_for_assignment (tree src, tree dest)
    1531              : {
    1532      4557011 :   if (operand_equal_p (dest, src, 0))
    1533              :     return true;
    1534              :   /* if both dest and src2 are decls, then we know these 2
    1535              :      accesses can't be the same.  */
    1536      4554128 :   if (DECL_P (dest) && DECL_P (src))
    1537              :     return false;
    1538              : 
    1539      2302461 :   tree core1, core2;
    1540      2302461 :   poly_int64 bytepos1, bytepos2;
    1541      2302461 :   poly_int64 bytesize1, bytesize2;
    1542      2302461 :   tree toffset1, toffset2;
    1543      2302461 :   int reversep1 = 0;
    1544      2302461 :   int reversep2 = 0;
    1545      2302461 :   poly_int64 diff = 0;
    1546      2302461 :   core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
    1547              :                                       &toffset1, &reversep1);
    1548      2302461 :   core2 = split_core_and_offset_size (src, &bytesize2, &bytepos2,
    1549              :                                       &toffset2, &reversep2);
    1550      2302461 :   if (!core1 || !core2)
    1551              :     return false;
    1552      2302461 :   if (reversep1 != reversep2)
    1553              :     return false;
    1554              :   /* The sizes of the 2 accesses need to be the same. */
    1555      2302461 :   if (!known_eq (bytesize1, bytesize2))
    1556              :     return false;
    1557      2301574 :   if (!operand_equal_p (core1, core2, 0))
    1558              :     return false;
    1559         6194 :   if (toffset1 && toffset2)
    1560              :     {
    1561          313 :       tree type = TREE_TYPE (toffset1);
    1562          313 :       if (type != TREE_TYPE (toffset2))
    1563            0 :         toffset2 = fold_convert (type, toffset2);
    1564              : 
    1565          313 :       tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
    1566          313 :       if (!cst_and_fits_in_hwi (tdiff))
    1567              :         return false;
    1568              : 
    1569            0 :       diff = int_cst_value (tdiff);
    1570            0 :     }
    1571         5881 :   else if (toffset1 || toffset2)
    1572              :     {
    1573              :       /* If only one of the offsets is non-constant, the difference cannot
    1574              :          be a constant.  */
    1575              :       return false;
    1576              :     }
    1577         5881 :   diff += bytepos1 - bytepos2;
    1578              :   /* The offset between the 2 need to be 0. */
    1579         5881 :   if (!known_eq (diff, 0))
    1580         5635 :     return false;
    1581              :   return true;
    1582              : }
    1583              : 
    1584              : /* Helper function for optimize_agr_copyprop.
    1585              :    For aggregate copies in USE_STMT, see if DEST
    1586              :    is on the lhs of USE_STMT and replace it with SRC. */
    1587              : static void
    1588      1036584 : optimize_agr_copyprop_1 (gimple *stmt, gimple *use_stmt,
    1589              :                          tree dest, tree src)
    1590              : {
    1591      1036584 :   gcc_assert (gimple_assign_load_p (use_stmt)
    1592              :               && gimple_store_p (use_stmt));
    1593      2073168 :   if (gimple_has_volatile_ops (use_stmt))
    1594       621417 :     return;
    1595      1036583 :   tree dest2 = gimple_assign_lhs (use_stmt);
    1596      1036583 :   tree src2 = gimple_assign_rhs1 (use_stmt);
    1597              :   /* If the new store is `src2 = src2;` skip over it. */
    1598      1036583 :   if (same_for_assignment (src2, dest2))
    1599              :     return;
    1600      1036020 :   src = new_src_based_on_copy (src2, dest, src);
    1601      1036020 :   if (!src)
    1602              :     return;
    1603              :   /* For 2 memory references and using a temporary to do the copy,
    1604              :      don't remove the temporary as the 2 memory references might overlap.
    1605              :      Note t does not need to be decl as it could be field.
    1606              :      See PR 22237 for full details.
    1607              :      E.g.
    1608              :      t = *a; #DEST = SRC;
    1609              :      *b = t; #DEST2 = SRC2;
    1610              :      Cannot be convert into
    1611              :      t = *a;
    1612              :      *b = *a;
    1613              :      Though the following is allowed to be done:
    1614              :      t = *a;
    1615              :      *a = t;
    1616              :      And convert it into:
    1617              :      t = *a;
    1618              :      *a = *a;
    1619              :      */
    1620       444330 :   if (!operand_equal_p (dest2, src, 0)
    1621       444330 :       && !DECL_P (dest2) && !DECL_P (src))
    1622              :     {
    1623              :       /* If *a and *b have the same base see if
    1624              :          the offset between the two is greater than
    1625              :          or equal to the size of the type. */
    1626        32557 :       poly_int64 offset1, offset2;
    1627        32557 :       tree len = TYPE_SIZE_UNIT (TREE_TYPE (src));
    1628        32557 :       if (len == NULL_TREE
    1629        32557 :           || !tree_fits_poly_int64_p (len))
    1630        29163 :         return;
    1631        32557 :       tree base1 = get_addr_base_and_unit_offset (dest2, &offset1);
    1632        32557 :       tree base2 = get_addr_base_and_unit_offset (src, &offset2);
    1633        32557 :       poly_int64 size = tree_to_poly_int64 (len);
    1634              :       /* If the bases are 2 different decls,
    1635              :          then there can be no overlapping.  */
    1636        32557 :       if (base1 && base2
    1637        31619 :           && DECL_P (base1) && DECL_P (base2)
    1638         1874 :           && base1 != base2)
    1639              :         ;
    1640              :       /* If we can't figure out the base or the bases are
    1641              :          not equal then fall back to an alignment check.  */
    1642        30912 :       else if (!base1
    1643        30912 :                || !base2
    1644        30912 :                || !operand_equal_p (base1, base2))
    1645              :         {
    1646        30540 :           unsigned int align1 = get_object_alignment (src);
    1647        30540 :           unsigned int align2 = get_object_alignment (dest2);
    1648        30540 :           align1 /= BITS_PER_UNIT;
    1649        30540 :           align2 /= BITS_PER_UNIT;
    1650              :           /* If the alignment of either object is less
    1651              :              than the size then there is a possibility
    1652              :              of overlapping.  */
    1653        30540 :           if (maybe_lt (align1, size)
    1654        30540 :               || maybe_lt (align2, size))
    1655        29163 :             return;
    1656              :         }
    1657              :       /* Make sure [offset1, offset1 + len - 1] does
    1658              :          not overlap with [offset2, offset2 + len - 1],
    1659              :          it is ok if they are at the same location though.  */
    1660          372 :       else if (ranges_maybe_overlap_p (offset1, size, offset2, size)
    1661          372 :           && !known_eq (offset2, offset1))
    1662              :         return;
    1663              :     }
    1664              : 
    1665       415167 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1666              :     {
    1667           11 :       fprintf (dump_file, "Simplified\n  ");
    1668           11 :       print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
    1669           11 :       fprintf (dump_file, "after previous\n  ");
    1670           11 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1671              :     }
    1672       415167 :   gimple *orig_stmt = use_stmt;
    1673       415167 :   gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
    1674       415167 :   gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (src));
    1675       415167 :   update_stmt (use_stmt);
    1676              : 
    1677       415167 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1678              :     {
    1679           11 :       fprintf (dump_file, "into\n  ");
    1680           11 :       print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
    1681              :     }
    1682       415167 :   if (maybe_clean_or_replace_eh_stmt (orig_stmt, use_stmt))
    1683            0 :     bitmap_set_bit (to_purge, gimple_bb (use_stmt)->index);
    1684       415167 :   statistics_counter_event (cfun, "copy prop for aggregate", 1);
    1685              : }
    1686              : 
    1687              : /* Helper function for optimize_agr_copyprop_1, propagate aggregates
    1688              :    into the arguments of USE_STMT if the argument matches with DEST;
    1689              :    replacing it with SRC.  */
    1690              : static void
    1691       717276 : optimize_agr_copyprop_arg (gimple *defstmt, gcall *call,
    1692              :                            tree dest, tree src)
    1693              : {
    1694       717276 :   bool changed = false;
    1695      2402023 :   for (unsigned arg = 0; arg < gimple_call_num_args (call); arg++)
    1696              :     {
    1697      1684747 :       tree *argptr = gimple_call_arg_ptr (call, arg);
    1698      3176570 :       if (TREE_CODE (*argptr) == SSA_NAME
    1699       961992 :           || is_gimple_min_invariant (*argptr)
    1700      1877671 :           || TYPE_VOLATILE (TREE_TYPE (*argptr)))
    1701      1491823 :         continue;
    1702       192924 :       tree newsrc = new_src_based_on_copy (*argptr, dest, src);
    1703       192924 :       if (!newsrc)
    1704       116626 :         continue;
    1705              : 
    1706        76298 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1707              :         {
    1708            9 :           fprintf (dump_file, "Simplified\n  ");
    1709            9 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    1710            9 :           fprintf (dump_file, "after previous\n  ");
    1711            9 :           print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1712              :         }
    1713        76298 :       *argptr = unshare_expr (newsrc);
    1714        76298 :       changed = true;
    1715        76298 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1716              :         {
    1717            9 :           fprintf (dump_file, "into\n  ");
    1718            9 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    1719              :         }
    1720              :     }
    1721       717276 :   if (changed)
    1722        76120 :     update_stmt (call);
    1723       717276 : }
    1724              : 
    1725              : /* Helper function for optimize_agr_copyprop, propagate aggregates
    1726              :    into the return stmt USE if the operand of the return matches DEST;
    1727              :    replacing it with SRC.  */
    1728              : static void
    1729       129617 : optimize_agr_copyprop_return (gimple *defstmt, greturn *use,
    1730              :                               tree dest, tree src)
    1731              : {
    1732       129617 :   tree rvalue = gimple_return_retval (use);
    1733       129617 :   if (!rvalue
    1734        83344 :       || TREE_CODE (rvalue) == SSA_NAME
    1735        74392 :       || is_gimple_min_invariant (rvalue)
    1736       203585 :       || TYPE_VOLATILE (TREE_TYPE (rvalue)))
    1737              :     return;
    1738              : 
    1739              :   /* `return <retval>;` is already the best it could be.
    1740              :      Likewise `return *<retval>_N(D)`.  */
    1741        73967 :   if (TREE_CODE (rvalue) == RESULT_DECL
    1742        73967 :       || (TREE_CODE (rvalue) == MEM_REF
    1743            0 :           && TREE_CODE (TREE_OPERAND (rvalue, 0)) == SSA_NAME
    1744            0 :           && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (rvalue, 0)))
    1745              :                == RESULT_DECL))
    1746              :     return;
    1747        52497 :   tree newsrc = new_src_based_on_copy (rvalue, dest, src);
    1748        52497 :   if (!newsrc)
    1749              :     return;
    1750              :   /* Currently only support non-global vars.
    1751              :      See PR 124099 on enumtls not supporting expanding for GIMPLE_RETURN.
    1752              :      FIXME: could support VCEs too?  */
    1753        52371 :   if (!VAR_P (newsrc) || is_global_var (newsrc))
    1754              :     return;
    1755        26667 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1756              :     {
    1757            1 :       fprintf (dump_file, "Simplified\n  ");
    1758            1 :       print_gimple_stmt (dump_file, use, 0, dump_flags);
    1759            1 :       fprintf (dump_file, "after previous\n  ");
    1760            1 :       print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1761              :     }
    1762        26667 :   gimple_return_set_retval (use, newsrc);
    1763        26667 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1764              :     {
    1765            1 :       fprintf (dump_file, "into\n  ");
    1766            1 :       print_gimple_stmt (dump_file, use, 0, dump_flags);
    1767              :     }
    1768        26667 :   update_stmt (use);
    1769              : }
    1770              : 
    1771              : /* Optimizes
    1772              :    DEST = SRC;
    1773              :    DEST2 = DEST; # DEST2 = SRC2;
    1774              :    into
    1775              :    DEST = SRC;
    1776              :    DEST2 = SRC;
    1777              :    STMT is the first statement and SRC is the common
    1778              :    between the statements.
    1779              : 
    1780              :    Also optimizes:
    1781              :    DEST = SRC;
    1782              :    call_func(..., DEST, ...);
    1783              :    into:
    1784              :    DEST = SRC;
    1785              :    call_func(..., SRC, ...);
    1786              : 
    1787              : */
    1788              : static void
    1789      3930044 : optimize_agr_copyprop (gimple *stmt)
    1790              : {
    1791      7860088 :   if (gimple_has_volatile_ops (stmt))
    1792       412182 :     return;
    1793              : 
    1794              :   /* Can't prop if the statement could throw.  */
    1795      3928829 :   if (stmt_could_throw_p (cfun, stmt))
    1796              :     return;
    1797              : 
    1798      3520428 :   tree dest = gimple_assign_lhs (stmt);
    1799      3520428 :   tree src = gimple_assign_rhs1 (stmt);
    1800              :   /* If the statement is `src = src;` then ignore it. */
    1801      3520428 :   if (same_for_assignment (dest, src))
    1802              :     return;
    1803              : 
    1804      3517862 :   tree vdef = gimple_vdef (stmt);
    1805      3517862 :   imm_use_iterator iter;
    1806      3517862 :   gimple *use_stmt;
    1807     10143661 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
    1808              :     {
    1809      6625799 :       if (gimple_assign_load_p (use_stmt)
    1810      6625799 :           && gimple_store_p (use_stmt))
    1811      1036584 :         optimize_agr_copyprop_1 (stmt, use_stmt, dest, src);
    1812      5589215 :       else if (is_gimple_call (use_stmt))
    1813       717276 :         optimize_agr_copyprop_arg (stmt, as_a<gcall*>(use_stmt), dest, src);
    1814      4871939 :       else if (is_a<greturn*> (use_stmt))
    1815       129617 :         optimize_agr_copyprop_return (stmt, as_a<greturn*>(use_stmt), dest, src);
    1816      3517862 :     }
    1817              : }
    1818              : 
    1819              : /* Simple DSE of the lhs from a clobber STMT.
    1820              :    This is used mostly to clean up from optimize_agr_copyprop and
    1821              :    to remove (exactly one) extra copy that might later on confuse SRA.
    1822              :    An example is:
    1823              :    ;; write to a and such.
    1824              :    b = a; // This statement is to be removed
    1825              :    b = {CLOBBER};
    1826              :    SRA will totally scalarize b (which means also a) here for the extra copy
    1827              :    which is not something welcomed. So removing the copy will
    1828              :    allow SRA to move the scalarization of a further down or not at all.
    1829              :    */
    1830              : static void
    1831      7522876 : do_simple_agr_dse (gassign *stmt, bool full_walk)
    1832              : {
    1833              :   /* Don't do this while in -Og as we want to keep around the copy
    1834              :      for debuggability.  */
    1835      7522876 :   if (optimize_debug)
    1836      5202753 :     return;
    1837      7519331 :   ao_ref read;
    1838      7519331 :   basic_block bb = gimple_bb (stmt);
    1839      7519331 :   tree lhs = gimple_assign_lhs (stmt);
    1840              :   /* Only handle clobbers of a full decl.  */
    1841      7519331 :   if (!DECL_P (lhs))
    1842              :     return;
    1843      6795906 :   ao_ref_init (&read, lhs);
    1844      6795906 :   tree vuse = gimple_vuse (stmt);
    1845      6795906 :   unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 4;
    1846     17593871 :   while (limit)
    1847              :     {
    1848     17580628 :       gimple *ostmt = SSA_NAME_DEF_STMT (vuse);
    1849              :       /* Don't handle phis, just declare to be done. */
    1850     17580628 :       if (is_a<gphi*>(ostmt) || gimple_nop_p (ostmt))
    1851              :         break;
    1852     15273748 :       basic_block obb = gimple_bb (ostmt);
    1853              :       /* If the clobber is not fully dominating the statement define,
    1854              :          then it is not "simple" to detect if the define is fully clobbered.  */
    1855     15273748 :       if (obb != bb && !dominated_by_p (CDI_DOMINATORS, bb, obb))
    1856      4475783 :         return;
    1857     15273748 :       gimple *use_stmt;
    1858     15273748 :       imm_use_iterator iter;
    1859     46634198 :       FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_vdef (ostmt))
    1860              :         {
    1861     18101257 :           basic_block ubb = gimple_bb (use_stmt);
    1862     18101257 :           if (stmt == use_stmt)
    1863      5230939 :             continue;
    1864              :           /* If the use is a clobber for lhs,
    1865              :              then it can be safely skipped; this happens with eh
    1866              :              and sometimes jump threading.  */
    1867     12870318 :           if (gimple_clobber_p (use_stmt)
    1868     12870318 :               && lhs == gimple_assign_lhs (use_stmt))
    1869       176126 :             continue;
    1870              :           /* If the use is a phi and it is single use then check if that single use
    1871              :              is a clobber and lhs is the same.  */
    1872     12694192 :           if (gphi *use_phi = dyn_cast<gphi*>(use_stmt))
    1873              :             {
    1874       381355 :               use_operand_p ou;
    1875       381355 :               gimple *ostmt;
    1876       381355 :               if (single_imm_use (gimple_phi_result (use_phi), &ou, &ostmt)
    1877       319185 :                   && gimple_clobber_p (ostmt)
    1878       635874 :                   && lhs == gimple_assign_lhs (ostmt))
    1879        69434 :                 continue;
    1880              :               /* A phi node will never be dominating the clobber.  */
    1881       311921 :               return;
    1882              :             }
    1883              :           /* The use needs to be dominating the clobber. */
    1884      1505375 :           if ((ubb != bb && !dominated_by_p (CDI_DOMINATORS, bb, ubb))
    1885     13069297 :               || ref_maybe_used_by_stmt_p (use_stmt, &read, false))
    1886              :             return;
    1887              :           /* Count the above alias lookup towards the limit. */
    1888     11085875 :           limit--;
    1889     11085875 :           if (limit == 0)
    1890              :             return;
    1891      2014555 :         }
    1892     13259193 :       vuse = gimple_vuse (ostmt);
    1893              :       /* This is a call with an assignment to the clobber decl,
    1894              :          remove the lhs or the whole stmt if it was pure/const. */
    1895     13259193 :       if (is_a <gcall*>(ostmt)
    1896     13259193 :           && lhs == gimple_call_lhs (ostmt))
    1897              :         {
    1898              :           /* Don't remove stores/statements that are needed for non-call
    1899              :               eh to work.  */
    1900         4031 :           if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
    1901              :             return;
    1902              :           /* If we delete a stmt that could throw, mark the block
    1903              :              in to_purge to cleanup afterwards.  */
    1904         4025 :           if (stmt_could_throw_p (cfun, ostmt))
    1905         1035 :             bitmap_set_bit (to_purge, obb->index);
    1906         4025 :           int flags = gimple_call_flags (ostmt);
    1907         4025 :           if ((flags & (ECF_PURE|ECF_CONST|ECF_NOVOPS))
    1908          205 :               && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
    1909              :             {
    1910          121 :                gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
    1911          121 :                if (dump_file && (dump_flags & TDF_DETAILS))
    1912              :                 {
    1913           14 :                   fprintf (dump_file, "Removing dead call store stmt ");
    1914           14 :                   print_gimple_stmt (dump_file, ostmt, 0);
    1915           14 :                   fprintf (dump_file, "\n");
    1916              :                 }
    1917          121 :               unlink_stmt_vdef (ostmt);
    1918          121 :               release_defs (ostmt);
    1919          121 :               gsi_remove (&gsi, true);
    1920          121 :               statistics_counter_event (cfun, "delete call dead store", 1);
    1921              :               /* Only remove the first store previous statement. */
    1922          121 :               return;
    1923              :             }
    1924              :           /* Make sure we do not remove a return slot we cannot reconstruct
    1925              :              later.  */
    1926         3904 :           if (gimple_call_return_slot_opt_p (as_a <gcall *>(ostmt))
    1927         3904 :               && (TREE_ADDRESSABLE (TREE_TYPE (gimple_call_fntype (ostmt)))
    1928          534 :                   || !poly_int_tree_p
    1929          534 :                       (TYPE_SIZE (TREE_TYPE (gimple_call_fntype (ostmt))))))
    1930              :             return;
    1931          671 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1932              :             {
    1933            6 :               fprintf (dump_file, "Removing lhs of call stmt ");
    1934            6 :               print_gimple_stmt (dump_file, ostmt, 0);
    1935            6 :               fprintf (dump_file, "\n");
    1936              :             }
    1937          671 :           gimple_call_set_lhs (ostmt, NULL_TREE);
    1938          671 :           update_stmt (ostmt);
    1939          671 :           statistics_counter_event (cfun, "removed lhs call", 1);
    1940          671 :           return;
    1941              :         }
    1942              :       /* This an assignment store to the clobbered decl,
    1943              :          then maybe remove it. */
    1944     13255162 :       if (is_a <gassign*>(ostmt)
    1945     11152204 :           && gimple_store_p (ostmt)
    1946     11152204 :           && !gimple_clobber_p (ostmt)
    1947     16564748 :           && lhs == gimple_assign_lhs (ostmt))
    1948              :         {
    1949              :           /* Don't remove stores/statements that are needed for non-call
    1950              :               eh to work.  */
    1951       168315 :           if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
    1952              :             return;
    1953              :           /* If we delete a stmt that could throw, mark the block
    1954              :              in to_purge to cleanup afterwards.  */
    1955       163041 :           if (stmt_could_throw_p (cfun, ostmt))
    1956            0 :             bitmap_set_bit (to_purge, obb->index);
    1957       163041 :           gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
    1958       163041 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1959              :             {
    1960           12 :               fprintf (dump_file, "Removing dead store stmt ");
    1961           12 :               print_gimple_stmt (dump_file, ostmt, 0);
    1962           12 :               fprintf (dump_file, "\n");
    1963              :             }
    1964       163041 :           unlink_stmt_vdef (ostmt);
    1965       163041 :           release_defs (ostmt);
    1966       163041 :           gsi_remove (&gsi, true);
    1967       163041 :           statistics_counter_event (cfun, "delete dead store", 1);
    1968              :           /* Only remove the first store previous statement. */
    1969       163041 :           return;
    1970              :         }
    1971              :       /* If the statement uses or maybe writes to the decl,
    1972              :          then nothing is to be removed. Don't know if the write
    1973              :          to the decl is partial write or a full one so the need
    1974              :          to stop.
    1975              :          e.g.
    1976              :          b.c = a;
    1977              :            Easier to stop here rather than do a full partial
    1978              :            dse of this statement.
    1979              :          b = {CLOBBER}; */
    1980     13086847 :       if (stmt_may_clobber_ref_p_1 (ostmt, &read, false)
    1981     13086847 :           || ref_maybe_used_by_stmt_p (ostmt, &read, false))
    1982              :         return;
    1983     10797965 :       limit--;
    1984              :     }
    1985              : }
    1986              : 
    1987              : /* Optimizes builtin memcmps for small constant sizes.
    1988              :    GSI_P is the GSI for the call. STMT is the call itself.
    1989              :    */
    1990              : 
    1991              : static bool
    1992       468666 : simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
    1993              : {
    1994              :   /* Make sure memcmp arguments are the correct type.  */
    1995       468666 :   if (gimple_call_num_args (stmt) != 3)
    1996              :     return false;
    1997       468666 :   tree arg1 = gimple_call_arg (stmt, 0);
    1998       468666 :   tree arg2 = gimple_call_arg (stmt, 1);
    1999       468666 :   tree len = gimple_call_arg (stmt, 2);
    2000              : 
    2001       468666 :   if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
    2002              :     return false;
    2003       468666 :   if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
    2004              :     return false;
    2005       468666 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (len)))
    2006              :     return false;
    2007              : 
    2008              :   /* The return value of the memcmp has to be used
    2009              :      equality comparison to zero. */
    2010       468666 :   tree res = gimple_call_lhs (stmt);
    2011              : 
    2012       468666 :   if (!res || !use_in_zero_equality (res))
    2013              :     return false;
    2014              : 
    2015       452948 :   unsigned HOST_WIDE_INT leni;
    2016              : 
    2017       452948 :   if (tree_fits_uhwi_p (len)
    2018       632246 :       && (leni = tree_to_uhwi (len)) <= GET_MODE_SIZE (word_mode)
    2019       533378 :       && pow2p_hwi (leni))
    2020              :     {
    2021        19091 :       leni *= CHAR_TYPE_SIZE;
    2022        19091 :       unsigned align1 = get_pointer_alignment (arg1);
    2023        19091 :       unsigned align2 = get_pointer_alignment (arg2);
    2024        19091 :       unsigned align = MIN (align1, align2);
    2025        19091 :       scalar_int_mode mode;
    2026        38182 :       if (int_mode_for_size (leni, 1).exists (&mode)
    2027        19091 :           && (align >= leni || !targetm.slow_unaligned_access (mode, align)))
    2028              :         {
    2029        19091 :           location_t loc = gimple_location (stmt);
    2030        19091 :           tree type, off;
    2031        19091 :           type = build_nonstandard_integer_type (leni, 1);
    2032        38182 :           gcc_assert (known_eq (GET_MODE_BITSIZE (TYPE_MODE (type)), leni));
    2033        19091 :           tree ptrtype = build_pointer_type_for_mode (char_type_node,
    2034              :                                                       ptr_mode, true);
    2035        19091 :           off = build_int_cst (ptrtype, 0);
    2036              : 
    2037              :           /* Create unaligned types if needed. */
    2038        19091 :           tree type1 = type, type2 = type;
    2039        19091 :           if (TYPE_ALIGN (type1) > align1)
    2040         7758 :             type1 = build_aligned_type (type1, align1);
    2041        19091 :           if (TYPE_ALIGN (type2) > align2)
    2042         8265 :             type2 = build_aligned_type (type2, align2);
    2043              : 
    2044        19091 :           arg1 = build2_loc (loc, MEM_REF, type1, arg1, off);
    2045        19091 :           arg2 = build2_loc (loc, MEM_REF, type2, arg2, off);
    2046        19091 :           tree tem1 = fold_const_aggregate_ref (arg1);
    2047        19091 :           if (tem1)
    2048          242 :             arg1 = tem1;
    2049        19091 :           tree tem2 = fold_const_aggregate_ref (arg2);
    2050        19091 :           if (tem2)
    2051         7394 :             arg2 = tem2;
    2052        19091 :           res = fold_convert_loc (loc, TREE_TYPE (res),
    2053              :                                   fold_build2_loc (loc, NE_EXPR,
    2054              :                                                    boolean_type_node,
    2055              :                                                    arg1, arg2));
    2056        19091 :           gimplify_and_update_call_from_tree (gsi_p, res);
    2057        19091 :           return true;
    2058              :         }
    2059              :     }
    2060              : 
    2061              :   /* Replace memcmp with memcmp_eq if the above fails. */
    2062       433857 :   if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
    2063              :     return false;
    2064       343545 :   if (!fold_before_rtl_expansion_p ())
    2065              :     return false;
    2066        90312 :   gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
    2067        90312 :   update_stmt (stmt);
    2068        90312 :   return true;
    2069              : }
    2070              : 
    2071              : /* Optimizes builtin memchrs for small constant sizes with a const string.
    2072              :    GSI_P is the GSI for the call. STMT is the call itself.
    2073              :    */
    2074              : 
    2075              : static bool
    2076        11197 : simplify_builtin_memchr (gimple_stmt_iterator *gsi_p, gcall *stmt)
    2077              : {
    2078        11197 :   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
    2079              :     return false;
    2080              : 
    2081        11197 :   if (gimple_call_num_args (stmt) != 3)
    2082              :     return false;
    2083              : 
    2084        11197 :   tree res = gimple_call_lhs (stmt);
    2085        11197 :   if (!res || !use_in_zero_equality (res))
    2086              :     return false;
    2087              : 
    2088         1399 :   tree ptr = gimple_call_arg (stmt, 0);
    2089         1399 :   if (TREE_CODE (ptr) != ADDR_EXPR
    2090         1399 :       || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
    2091              :     return false;
    2092              : 
    2093          392 :   unsigned HOST_WIDE_INT slen
    2094          392 :     = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
    2095              :   /* It must be a non-empty string constant.  */
    2096          392 :   if (slen < 2)
    2097              :     return false;
    2098              : 
    2099              :   /* For -Os, only simplify strings with a single character.  */
    2100          388 :   if (!optimize_bb_for_speed_p (gimple_bb (stmt))
    2101          388 :       && slen > 2)
    2102              :     return false;
    2103              : 
    2104          372 :   tree size = gimple_call_arg (stmt, 2);
    2105              :   /* Size must be a constant which is <= UNITS_PER_WORD and
    2106              :      <= the string length.  */
    2107          372 :   if (!tree_fits_uhwi_p (size))
    2108              :     return false;
    2109              : 
    2110          372 :   unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
    2111          373 :   if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
    2112              :     return false;
    2113              : 
    2114          320 :   tree ch = gimple_call_arg (stmt, 1);
    2115          320 :   location_t loc = gimple_location (stmt);
    2116          320 :   if (!useless_type_conversion_p (char_type_node,
    2117          320 :                                   TREE_TYPE (ch)))
    2118          320 :     ch = fold_convert_loc (loc, char_type_node, ch);
    2119          320 :   const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
    2120          320 :   unsigned int isize = sz;
    2121          320 :   tree *op = XALLOCAVEC (tree, isize);
    2122         1129 :   for (unsigned int i = 0; i < isize; i++)
    2123              :     {
    2124          809 :       op[i] = build_int_cst (char_type_node, p[i]);
    2125          809 :       op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
    2126              :                                op[i], ch);
    2127              :     }
    2128          809 :   for (unsigned int i = isize - 1; i >= 1; i--)
    2129          489 :     op[i - 1] = fold_convert_loc (loc, boolean_type_node,
    2130              :                                   fold_build2_loc (loc,
    2131              :                                                    BIT_IOR_EXPR,
    2132              :                                                    boolean_type_node,
    2133          489 :                                                    op[i - 1],
    2134          489 :                                                    op[i]));
    2135          320 :   res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
    2136          320 :   gimplify_and_update_call_from_tree (gsi_p, res);
    2137          320 :   return true;
    2138              : }
    2139              : 
    2140              : /* *GSI_P is a GIMPLE_CALL to a builtin function.
    2141              :    Optimize
    2142              :    memcpy (p, "abcd", 4); // STMT1
    2143              :    memset (p + 4, ' ', 3); // STMT2
    2144              :    into
    2145              :    memcpy (p, "abcd   ", 7);
    2146              :    call if the latter can be stored by pieces during expansion.
    2147              : */
    2148              : 
    2149              : static bool
    2150       117490 : simplify_builtin_memcpy_memset (gimple_stmt_iterator *gsi_p, gcall *stmt2)
    2151              : {
    2152       117490 :   if (gimple_call_num_args (stmt2) != 3
    2153       117490 :       || gimple_call_lhs (stmt2)
    2154              :       || CHAR_BIT != 8
    2155       117490 :       || BITS_PER_UNIT != 8)
    2156              :     return false;
    2157              : 
    2158       111313 :   tree vuse = gimple_vuse (stmt2);
    2159       111313 :   if (vuse == NULL)
    2160              :     return false;
    2161       111297 :   gimple *stmt1 = SSA_NAME_DEF_STMT (vuse);
    2162              : 
    2163       111297 :   tree callee1;
    2164       111297 :   tree ptr1, src1, str1, off1, len1, lhs1;
    2165       111297 :   tree ptr2 = gimple_call_arg (stmt2, 0);
    2166       111297 :   tree val2 = gimple_call_arg (stmt2, 1);
    2167       111297 :   tree len2 = gimple_call_arg (stmt2, 2);
    2168       111297 :   tree diff, vdef, new_str_cst;
    2169       111297 :   gimple *use_stmt;
    2170       111297 :   unsigned int ptr1_align;
    2171       111297 :   unsigned HOST_WIDE_INT src_len;
    2172       111297 :   char *src_buf;
    2173       111297 :   use_operand_p use_p;
    2174              : 
    2175       111297 :   if (!tree_fits_shwi_p (val2)
    2176       106804 :       || !tree_fits_uhwi_p (len2)
    2177       179854 :       || compare_tree_int (len2, 1024) == 1)
    2178              :     return false;
    2179              : 
    2180        60260 :   if (is_gimple_call (stmt1))
    2181              :     {
    2182              :       /* If first stmt is a call, it needs to be memcpy
    2183              :          or mempcpy, with string literal as second argument and
    2184              :          constant length.  */
    2185        31124 :       callee1 = gimple_call_fndecl (stmt1);
    2186        31124 :       if (callee1 == NULL_TREE
    2187        31008 :           || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
    2188        58302 :           || gimple_call_num_args (stmt1) != 3)
    2189              :         return false;
    2190        25774 :       if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
    2191        25774 :           && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
    2192              :         return false;
    2193        11216 :       ptr1 = gimple_call_arg (stmt1, 0);
    2194        11216 :       src1 = gimple_call_arg (stmt1, 1);
    2195        11216 :       len1 = gimple_call_arg (stmt1, 2);
    2196        11216 :       lhs1 = gimple_call_lhs (stmt1);
    2197        11216 :       if (!tree_fits_uhwi_p (len1))
    2198              :         return false;
    2199        11129 :       str1 = string_constant (src1, &off1, NULL, NULL);
    2200        11129 :       if (str1 == NULL_TREE)
    2201              :         return false;
    2202         5234 :       if (!tree_fits_uhwi_p (off1)
    2203         5234 :           || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
    2204         5234 :           || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
    2205         5234 :                                      - tree_to_uhwi (off1)) > 0
    2206         5167 :           || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
    2207        15568 :           || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
    2208         5167 :              != TYPE_MODE (char_type_node))
    2209              :         return false;
    2210              :     }
    2211        29136 :   else if (gimple_assign_single_p (stmt1))
    2212              :     {
    2213              :       /* Otherwise look for length 1 memcpy optimized into
    2214              :          assignment.  */
    2215        17645 :       ptr1 = gimple_assign_lhs (stmt1);
    2216        17645 :       src1 = gimple_assign_rhs1 (stmt1);
    2217        17645 :       if (TREE_CODE (ptr1) != MEM_REF
    2218         3501 :           || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
    2219        18638 :           || !tree_fits_shwi_p (src1))
    2220              :         return false;
    2221          347 :       ptr1 = build_fold_addr_expr (ptr1);
    2222          347 :       STRIP_USELESS_TYPE_CONVERSION (ptr1);
    2223          347 :       callee1 = NULL_TREE;
    2224          347 :       len1 = size_one_node;
    2225          347 :       lhs1 = NULL_TREE;
    2226          347 :       off1 = size_zero_node;
    2227          347 :       str1 = NULL_TREE;
    2228              :     }
    2229              :   else
    2230              :     return false;
    2231              : 
    2232         5514 :   diff = constant_pointer_difference (ptr1, ptr2);
    2233         5514 :   if (diff == NULL && lhs1 != NULL)
    2234              :     {
    2235            7 :       diff = constant_pointer_difference (lhs1, ptr2);
    2236            7 :       if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
    2237            7 :           && diff != NULL)
    2238            7 :         diff = size_binop (PLUS_EXPR, diff,
    2239              :                            fold_convert (sizetype, len1));
    2240              :     }
    2241              :   /* If the difference between the second and first destination pointer
    2242              :      is not constant, or is bigger than memcpy length, bail out.  */
    2243         5514 :   if (diff == NULL
    2244         4636 :       || !tree_fits_uhwi_p (diff)
    2245         4636 :       || tree_int_cst_lt (len1, diff)
    2246         9890 :       || compare_tree_int (diff, 1024) == 1)
    2247              :     return false;
    2248              : 
    2249              :   /* Use maximum of difference plus memset length and memcpy length
    2250              :      as the new memcpy length, if it is too big, bail out.  */
    2251         4376 :   src_len = tree_to_uhwi (diff);
    2252         4376 :   src_len += tree_to_uhwi (len2);
    2253         4376 :   if (src_len < tree_to_uhwi (len1))
    2254              :     src_len = tree_to_uhwi (len1);
    2255         4376 :   if (src_len > 1024)
    2256              :     return false;
    2257              : 
    2258              :   /* If mempcpy value is used elsewhere, bail out, as mempcpy
    2259              :      with bigger length will return different result.  */
    2260         4376 :   if (lhs1 != NULL_TREE
    2261           64 :       && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
    2262         4383 :       && (TREE_CODE (lhs1) != SSA_NAME
    2263            7 :           || !single_imm_use (lhs1, &use_p, &use_stmt)
    2264            7 :           || use_stmt != stmt2))
    2265              :     return false;
    2266              : 
    2267              :   /* If anything reads memory in between memcpy and memset
    2268              :      call, the modified memcpy call might change it.  */
    2269         4376 :   vdef = gimple_vdef (stmt1);
    2270         4376 :   if (vdef != NULL
    2271         4376 :       && (!single_imm_use (vdef, &use_p, &use_stmt)
    2272         3657 :           || use_stmt != stmt2))
    2273              :     return false;
    2274              : 
    2275         3657 :   ptr1_align = get_pointer_alignment (ptr1);
    2276              :   /* Construct the new source string literal.  */
    2277         3657 :   src_buf = XALLOCAVEC (char, src_len + 1);
    2278         3657 :   if (callee1)
    2279         3491 :     memcpy (src_buf,
    2280         3491 :             TREE_STRING_POINTER (str1) + tree_to_uhwi (off1),
    2281              :             tree_to_uhwi (len1));
    2282              :   else
    2283          166 :     src_buf[0] = tree_to_shwi (src1);
    2284         3657 :   memset (src_buf + tree_to_uhwi (diff),
    2285         3657 :           tree_to_shwi (val2), tree_to_uhwi (len2));
    2286         3657 :   src_buf[src_len] = '\0';
    2287              :   /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
    2288              :      handle embedded '\0's.  */
    2289         3657 :   if (strlen (src_buf) != src_len)
    2290              :     return false;
    2291         3563 :   rtl_profile_for_bb (gimple_bb (stmt2));
    2292              :   /* If the new memcpy wouldn't be emitted by storing the literal
    2293              :      by pieces, this optimization might enlarge .rodata too much,
    2294              :      as commonly used string literals couldn't be shared any
    2295              :      longer.  */
    2296         3563 :   if (!can_store_by_pieces (src_len,
    2297              :                             builtin_strncpy_read_str,
    2298              :                             src_buf, ptr1_align, false))
    2299              :     return false;
    2300              : 
    2301         2681 :   new_str_cst = build_string_literal (src_len, src_buf);
    2302         2681 :   if (callee1)
    2303              :     {
    2304              :       /* If STMT1 is a mem{,p}cpy call, adjust it and remove
    2305              :          memset call.  */
    2306         2553 :       if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
    2307            7 :         gimple_call_set_lhs (stmt1, NULL_TREE);
    2308         2553 :       gimple_call_set_arg (stmt1, 1, new_str_cst);
    2309         2553 :       gimple_call_set_arg (stmt1, 2,
    2310         2553 :                            build_int_cst (TREE_TYPE (len1), src_len));
    2311         2553 :       update_stmt (stmt1);
    2312         2553 :       unlink_stmt_vdef (stmt2);
    2313         2553 :       gsi_replace (gsi_p, gimple_build_nop (), false);
    2314         2553 :       fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
    2315         2553 :       release_defs (stmt2);
    2316         2553 :       if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
    2317              :         {
    2318            7 :           fwprop_invalidate_lattice (lhs1);
    2319            7 :           release_ssa_name (lhs1);
    2320              :         }
    2321              :       return true;
    2322              :     }
    2323              :   else
    2324              :     {
    2325              :       /* Otherwise, if STMT1 is length 1 memcpy optimized into
    2326              :          assignment, remove STMT1 and change memset call into
    2327              :          memcpy call.  */
    2328          128 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
    2329              : 
    2330          128 :       if (!is_gimple_val (ptr1))
    2331           12 :         ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
    2332              :                                          true, GSI_SAME_STMT);
    2333          128 :       tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCPY);
    2334          128 :       gimple_call_set_fndecl (stmt2, fndecl);
    2335          128 :       gimple_call_set_fntype (stmt2,
    2336          128 :                               TREE_TYPE (fndecl));
    2337          128 :       gimple_call_set_arg (stmt2, 0, ptr1);
    2338          128 :       gimple_call_set_arg (stmt2, 1, new_str_cst);
    2339          128 :       gimple_call_set_arg (stmt2, 2,
    2340          128 :                            build_int_cst (TREE_TYPE (len2), src_len));
    2341          128 :       unlink_stmt_vdef (stmt1);
    2342          128 :       gsi_remove (&gsi, true);
    2343          128 :       fwprop_invalidate_lattice (gimple_get_lhs (stmt1));
    2344          128 :       release_defs (stmt1);
    2345          128 :       update_stmt (stmt2);
    2346          128 :       return false;
    2347              :     }
    2348              : }
    2349              : 
    2350              : 
    2351              : /* Try to optimize out __builtin_stack_restore.  Optimize it out
    2352              :    if there is another __builtin_stack_restore in the same basic
    2353              :    block and no calls or ASM_EXPRs are in between, or if this block's
    2354              :    only outgoing edge is to EXIT_BLOCK and there are no calls or
    2355              :    ASM_EXPRs after this __builtin_stack_restore.
    2356              :    Note restore right before a noreturn function is not needed.
    2357              :    And skip some cheap calls that will most likely become an instruction.
    2358              :    Restoring the stack before a call is important to be able to keep
    2359              :    stack usage down so that call does not run out of stack.  */
    2360              : 
    2361              : 
    2362              : static bool
    2363        10753 : optimize_stack_restore (gimple_stmt_iterator *gsi, gimple *call)
    2364              : {
    2365        10753 :   if (!fold_before_rtl_expansion_p ())
    2366              :     return false;
    2367         2629 :   tree callee;
    2368         2629 :   gimple *stmt;
    2369              : 
    2370         2629 :   basic_block bb = gsi_bb (*gsi);
    2371              : 
    2372         2629 :   if (gimple_call_num_args (call) != 1
    2373         2629 :       || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
    2374         5258 :       || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
    2375              :     return false;
    2376              : 
    2377         2629 :   gimple_stmt_iterator i = *gsi;
    2378         6680 :   for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
    2379              :     {
    2380         4545 :       stmt = gsi_stmt (i);
    2381         4545 :       if (is_a<gasm*> (stmt))
    2382              :         return false;
    2383         4544 :       gcall *call = dyn_cast<gcall*>(stmt);
    2384         4544 :       if (!call)
    2385         3814 :         continue;
    2386              : 
    2387              :       /* We can remove the restore in front of noreturn
    2388              :          calls.  Since the restore will happen either
    2389              :          via an unwind/longjmp or not at all. */
    2390          730 :       if (gimple_call_noreturn_p (call))
    2391              :         break;
    2392              : 
    2393              :       /* Internal calls are ok, to bypass
    2394              :          check first since fndecl will be null. */
    2395          714 :       if (gimple_call_internal_p (call))
    2396            1 :         continue;
    2397              : 
    2398          713 :       callee = gimple_call_fndecl (call);
    2399              :       /* Non-builtin calls are not ok. */
    2400          713 :       if (!callee
    2401          713 :           || !fndecl_built_in_p (callee))
    2402              :         return false;
    2403              : 
    2404              :       /* Do not remove stack updates before strub leave.  */
    2405          632 :       if (fndecl_built_in_p (callee, BUILT_IN___STRUB_LEAVE)
    2406              :           /* Alloca calls are not ok either. */
    2407          632 :           || fndecl_builtin_alloc_p (callee))
    2408              :         return false;
    2409              : 
    2410          417 :       if (fndecl_built_in_p (callee, BUILT_IN_STACK_RESTORE))
    2411           78 :         goto second_stack_restore;
    2412              : 
    2413              :       /* If not a simple or inexpensive builtin, then it is not ok either. */
    2414          339 :       if (!is_simple_builtin (callee)
    2415          339 :           && !is_inexpensive_builtin (callee))
    2416              :         return false;
    2417              :     }
    2418              : 
    2419              :   /* Allow one successor of the exit block, or zero successors.  */
    2420         2151 :   switch (EDGE_COUNT (bb->succs))
    2421              :     {
    2422              :     case 0:
    2423              :       break;
    2424         2058 :     case 1:
    2425         2058 :       if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
    2426              :         return false;
    2427              :       break;
    2428              :     default:
    2429              :       return false;
    2430              :     }
    2431         1810 :  second_stack_restore:
    2432              : 
    2433              :   /* If there's exactly one use, then zap the call to __builtin_stack_save.
    2434              :      If there are multiple uses, then the last one should remove the call.
    2435              :      In any case, whether the call to __builtin_stack_save can be removed
    2436              :      or not is irrelevant to removing the call to __builtin_stack_restore.  */
    2437         1810 :   if (has_single_use (gimple_call_arg (call, 0)))
    2438              :     {
    2439         1638 :       gimple *stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
    2440         1638 :       if (is_gimple_call (stack_save))
    2441              :         {
    2442         1636 :           callee = gimple_call_fndecl (stack_save);
    2443         1636 :           if (callee && fndecl_built_in_p (callee, BUILT_IN_STACK_SAVE))
    2444              :             {
    2445         1636 :               gimple_stmt_iterator stack_save_gsi;
    2446         1636 :               tree rhs;
    2447              : 
    2448         1636 :               stack_save_gsi = gsi_for_stmt (stack_save);
    2449         1636 :               rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
    2450         1636 :               replace_call_with_value (&stack_save_gsi, rhs);
    2451              :             }
    2452              :         }
    2453              :     }
    2454              : 
    2455              :   /* No effect, so the statement will be deleted.  */
    2456         1810 :   replace_call_with_value (gsi, NULL_TREE);
    2457         1810 :   return true;
    2458              : }
    2459              : 
    2460              : /* Optimizes strlen (s) ==/!= 0 to *s ==/!= 0. */
    2461              : static bool
    2462        60596 : optimize_strlen_comp (gimple_stmt_iterator *gsi, gimple *call)
    2463              : {
    2464        60596 :   if (!fold_before_rtl_expansion_p ())
    2465              :     return false;
    2466              : 
    2467        13008 :   tree lhs = gimple_call_lhs (call);
    2468        13008 :   if (lhs == NULL_TREE || use_in_zero_equality (lhs, true) == NULL)
    2469              :     return false;
    2470              : 
    2471              :   /* The string passed to strlen. */
    2472          102 :   tree ptr = gimple_call_arg (call, 0);
    2473              : 
    2474              :   /* Dereference the string. */
    2475          102 :   tree deref = fold_build2 (MEM_REF, char_type_node, ptr,
    2476              :                             build_zero_cst (ptr_type_node));
    2477              : 
    2478              :   /* Perform a type conversion. */
    2479          102 :   deref = fold_convert_loc (gimple_location (call),
    2480          102 :                             TREE_TYPE (lhs),
    2481              :                             deref);
    2482              : 
    2483              :   /* Replace the original call to strlen with the dereference we just built. */
    2484          102 :   gimplify_and_update_call_from_tree (gsi, deref);
    2485              : 
    2486          102 :   return true;
    2487              : }
    2488              : 
    2489              : /* *GSI_P is a GIMPLE_CALL to a builtin function.
    2490              :    Optimize
    2491              :    memcpy (p, "abcd", 4);
    2492              :    memset (p + 4, ' ', 3);
    2493              :    into
    2494              :    memcpy (p, "abcd   ", 7);
    2495              :    call if the latter can be stored by pieces during expansion.
    2496              : 
    2497              :    Optimize
    2498              :    memchr ("abcd", a, 4) == 0;
    2499              :    or
    2500              :    memchr ("abcd", a, 4) != 0;
    2501              :    to
    2502              :    (a == 'a' || a == 'b' || a == 'c' || a == 'd') == 0
    2503              :    or
    2504              :    (a == 'a' || a == 'b' || a == 'c' || a == 'd') != 0
    2505              : 
    2506              :    Also canonicalize __atomic_fetch_op (p, x, y) op x
    2507              :    to __atomic_op_fetch (p, x, y) or
    2508              :    __atomic_op_fetch (p, x, y) iop x
    2509              :    to __atomic_fetch_op (p, x, y) when possible (also __sync).  */
    2510              : 
    2511              : static bool
    2512      6366002 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2, bool full_walk)
    2513              : {
    2514      6366002 :   gimple *stmt2 = gsi_stmt (*gsi_p);
    2515      6366002 :   enum built_in_function other_atomic = END_BUILTINS;
    2516      6366002 :   enum tree_code atomic_op = ERROR_MARK;
    2517              : 
    2518      6366002 :   switch (DECL_FUNCTION_CODE (callee2))
    2519              :     {
    2520        60596 :     case BUILT_IN_STRLEN:
    2521        60596 :       return optimize_strlen_comp (gsi_p, as_a<gcall*>(stmt2));
    2522        10753 :     case BUILT_IN_STACK_RESTORE:
    2523        10753 :       return optimize_stack_restore (gsi_p, as_a<gcall*>(stmt2));
    2524       468666 :     case BUILT_IN_MEMCMP:
    2525       468666 :     case BUILT_IN_MEMCMP_EQ:
    2526       468666 :       return simplify_builtin_memcmp (gsi_p, as_a<gcall*>(stmt2));
    2527        11197 :     case BUILT_IN_MEMCHR:
    2528        11197 :       return simplify_builtin_memchr (gsi_p, as_a<gcall*>(stmt2));
    2529              : 
    2530       117490 :     case BUILT_IN_MEMSET:
    2531       117490 :       if (gimple_call_num_args (stmt2) == 3)
    2532              :         {
    2533              :           /* Try to prop the zeroing/value of the memset to memcpy
    2534              :              if the dest is an address and the value is a constant. */
    2535       117490 :           optimize_aggr_zeroprop (stmt2, full_walk);
    2536              :         }
    2537       117490 :       return simplify_builtin_memcpy_memset (gsi_p, as_a<gcall*>(stmt2));
    2538              : 
    2539              :  #define CASE_ATOMIC(NAME, OTHER, OP) \
    2540              :     case BUILT_IN_##NAME##_1:                                           \
    2541              :     case BUILT_IN_##NAME##_2:                                           \
    2542              :     case BUILT_IN_##NAME##_4:                                           \
    2543              :     case BUILT_IN_##NAME##_8:                                           \
    2544              :     case BUILT_IN_##NAME##_16:                                          \
    2545              :       atomic_op = OP;                                                   \
    2546              :       other_atomic                                                      \
    2547              :         = (enum built_in_function) (BUILT_IN_##OTHER##_1                \
    2548              :                                     + (DECL_FUNCTION_CODE (callee2)     \
    2549              :                                        - BUILT_IN_##NAME##_1));         \
    2550              :       goto handle_atomic_fetch_op;
    2551              : 
    2552        49613 :     CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
    2553         7234 :     CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
    2554         3180 :     CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
    2555         3159 :     CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
    2556         4298 :     CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR)
    2557              : 
    2558         2373 :     CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR)
    2559         2012 :     CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR)
    2560         1876 :     CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR)
    2561         2144 :     CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR)
    2562         1987 :     CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR)
    2563              : 
    2564        14459 :     CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
    2565         8580 :     CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
    2566         2428 :     CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR)
    2567              : 
    2568          854 :     CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR)
    2569          740 :     CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR)
    2570          800 :     CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR)
    2571              : 
    2572              : #undef CASE_ATOMIC
    2573              : 
    2574       105737 :     handle_atomic_fetch_op:
    2575       105737 :       if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
    2576              :         {
    2577        61037 :           tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
    2578        61037 :           tree arg = gimple_call_arg (stmt2, 1);
    2579        61037 :           gimple *use_stmt, *cast_stmt = NULL;
    2580        61037 :           use_operand_p use_p;
    2581        61037 :           tree ndecl = builtin_decl_explicit (other_atomic);
    2582              : 
    2583        61037 :           if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
    2584              :             break;
    2585              : 
    2586        59920 :           if (gimple_assign_cast_p (use_stmt))
    2587              :             {
    2588        31540 :               cast_stmt = use_stmt;
    2589        31540 :               lhsc = gimple_assign_lhs (cast_stmt);
    2590        31540 :               if (lhsc == NULL_TREE
    2591        31540 :                   || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
    2592        30989 :                   || (TYPE_PRECISION (TREE_TYPE (lhsc))
    2593        30989 :                       != TYPE_PRECISION (TREE_TYPE (lhs2)))
    2594        60927 :                   || !single_imm_use (lhsc, &use_p, &use_stmt))
    2595              :                 {
    2596         2681 :                   use_stmt = cast_stmt;
    2597         2681 :                   cast_stmt = NULL;
    2598         2681 :                   lhsc = lhs2;
    2599              :                 }
    2600              :             }
    2601              : 
    2602        59920 :           bool ok = false;
    2603        59920 :           tree oarg = NULL_TREE;
    2604        59920 :           enum tree_code ccode = ERROR_MARK;
    2605        59920 :           tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
    2606        59920 :           auto assign = dyn_cast<gassign *> (use_stmt);
    2607        19591 :           if (assign && gimple_assign_rhs_code (assign) == atomic_op)
    2608              :             {
    2609         1416 :               if (gimple_assign_rhs1 (assign) == lhsc)
    2610         1016 :                 oarg = gimple_assign_rhs2 (assign);
    2611          400 :               else if (atomic_op != MINUS_EXPR)
    2612              :                 oarg = gimple_assign_rhs1 (assign);
    2613              :             }
    2614        58504 :           else if (atomic_op == MINUS_EXPR
    2615        58504 :                    && assign
    2616         3736 :                    && gimple_assign_rhs_code (assign) == PLUS_EXPR
    2617          199 :                    && TREE_CODE (arg) == INTEGER_CST
    2618        58703 :                    && (TREE_CODE (gimple_assign_rhs2 (assign))
    2619              :                        == INTEGER_CST))
    2620              :             {
    2621          183 :               tree a = fold_convert (TREE_TYPE (lhs2), arg);
    2622          366 :               tree o = fold_convert (TREE_TYPE (lhs2),
    2623              :                                      gimple_assign_rhs2 (assign));
    2624          183 :               if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
    2625              :                 ok = true;
    2626              :             }
    2627        58321 :           else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
    2628              :             ;
    2629        52671 :           else if (gimple_code (use_stmt) == GIMPLE_COND)
    2630              :             {
    2631        19724 :               ccode = gimple_cond_code (use_stmt);
    2632        19724 :               crhs1 = gimple_cond_lhs (use_stmt);
    2633        19724 :               crhs2 = gimple_cond_rhs (use_stmt);
    2634              :             }
    2635        32947 :           else if (auto assign = dyn_cast<gassign *> (use_stmt))
    2636              :             {
    2637         9837 :               if (gimple_assign_rhs_class (assign) == GIMPLE_BINARY_RHS)
    2638              :                 {
    2639         4185 :                   ccode = gimple_assign_rhs_code (assign);
    2640         4185 :                   crhs1 = gimple_assign_rhs1 (assign);
    2641         4185 :                   crhs2 = gimple_assign_rhs2 (assign);
    2642              :                 }
    2643              :             }
    2644        24925 :           if (ccode == EQ_EXPR || ccode == NE_EXPR)
    2645              :             {
    2646              :               /* Deal with x - y == 0 or x ^ y == 0
    2647              :                  being optimized into x == y and x + cst == 0
    2648              :                  into x == -cst.  */
    2649        22711 :               tree o = NULL_TREE;
    2650        22711 :               if (crhs1 == lhsc)
    2651              :                 o = crhs2;
    2652          133 :               else if (crhs2 == lhsc)
    2653          133 :                 o = crhs1;
    2654        22711 :               if (o && atomic_op != PLUS_EXPR)
    2655              :                 oarg = o;
    2656        10267 :               else if (o
    2657        10267 :                        && TREE_CODE (o) == INTEGER_CST
    2658        10267 :                        && TREE_CODE (arg) == INTEGER_CST)
    2659              :                 {
    2660         9557 :                   tree a = fold_convert (TREE_TYPE (lhs2), arg);
    2661         9557 :                   o = fold_convert (TREE_TYPE (lhs2), o);
    2662         9557 :                   if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
    2663        59920 :                     ok = true;
    2664              :                 }
    2665              :             }
    2666        59920 :           if (oarg && !ok)
    2667              :             {
    2668        13860 :               if (operand_equal_p (arg, oarg, 0))
    2669              :                 ok = true;
    2670        12519 :               else if (TREE_CODE (arg) == SSA_NAME
    2671         2215 :                        && TREE_CODE (oarg) == SSA_NAME)
    2672              :                 {
    2673          745 :                   tree oarg2 = oarg;
    2674          745 :                   if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg)))
    2675              :                     {
    2676          104 :                       gimple *g = SSA_NAME_DEF_STMT (oarg);
    2677          104 :                       oarg2 = gimple_assign_rhs1 (g);
    2678          104 :                       if (TREE_CODE (oarg2) != SSA_NAME
    2679          104 :                           || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2))
    2680          208 :                           || (TYPE_PRECISION (TREE_TYPE (oarg2))
    2681          104 :                               != TYPE_PRECISION (TREE_TYPE (oarg))))
    2682              :                         oarg2 = oarg;
    2683              :                     }
    2684          745 :                   if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg)))
    2685              :                     {
    2686          544 :                       gimple *g = SSA_NAME_DEF_STMT (arg);
    2687          544 :                       tree rhs1 = gimple_assign_rhs1 (g);
    2688              :                       /* Handle e.g.
    2689              :                          x.0_1 = (long unsigned int) x_4(D);
    2690              :                          _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0);
    2691              :                          _3 = (long int) _2;
    2692              :                          _7 = x_4(D) + _3;  */
    2693          544 :                       if (rhs1 == oarg || rhs1 == oarg2)
    2694              :                         ok = true;
    2695              :                       /* Handle e.g.
    2696              :                          x.18_1 = (short unsigned int) x_5(D);
    2697              :                          _2 = (int) x.18_1;
    2698              :                          _3 = __atomic_fetch_xor_2 (&vshort, _2, 0);
    2699              :                          _4 = (short int) _3;
    2700              :                          _8 = x_5(D) ^ _4;
    2701              :                          This happens only for char/short.  */
    2702          160 :                       else if (TREE_CODE (rhs1) == SSA_NAME
    2703          160 :                                && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
    2704          320 :                                && (TYPE_PRECISION (TREE_TYPE (rhs1))
    2705          160 :                                    == TYPE_PRECISION (TREE_TYPE (lhs2))))
    2706              :                         {
    2707          160 :                           g = SSA_NAME_DEF_STMT (rhs1);
    2708          160 :                           if (gimple_assign_cast_p (g)
    2709          160 :                               && (gimple_assign_rhs1 (g) == oarg
    2710            0 :                                   || gimple_assign_rhs1 (g) == oarg2))
    2711              :                             ok = true;
    2712              :                         }
    2713              :                     }
    2714          745 :                   if (!ok && arg == oarg2)
    2715              :                     /* Handle e.g.
    2716              :                        _1 = __sync_fetch_and_add_4 (&v, x_5(D));
    2717              :                        _2 = (int) _1;
    2718              :                        x.0_3 = (int) x_5(D);
    2719              :                        _7 = _2 + x.0_3;  */
    2720              :                     ok = true;
    2721              :                 }
    2722              :             }
    2723              : 
    2724        58579 :           if (ok)
    2725              :             {
    2726         2618 :               tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
    2727         2618 :               gimple_call_set_lhs (stmt2, new_lhs);
    2728         2618 :               gimple_call_set_fndecl (stmt2, ndecl);
    2729         2618 :               gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
    2730         2618 :               if (ccode == ERROR_MARK)
    2731         2000 :                 gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
    2732              :                                                 ? NOP_EXPR : SSA_NAME,
    2733              :                                                 new_lhs);
    2734              :               else
    2735              :                 {
    2736         1395 :                   crhs1 = new_lhs;
    2737         1395 :                   crhs2 = build_zero_cst (TREE_TYPE (lhs2));
    2738         1395 :                   if (gimple_code (use_stmt) == GIMPLE_COND)
    2739              :                     {
    2740         1050 :                       gcond *cond_stmt = as_a <gcond *> (use_stmt);
    2741         1050 :                       gimple_cond_set_lhs (cond_stmt, crhs1);
    2742         1050 :                       gimple_cond_set_rhs (cond_stmt, crhs2);
    2743              :                     }
    2744          345 :                   else if (gimple_assign_rhs_class (use_stmt)
    2745              :                            == GIMPLE_BINARY_RHS)
    2746              :                     {
    2747          345 :                       gimple_assign_set_rhs1 (use_stmt, crhs1);
    2748          345 :                       gimple_assign_set_rhs2 (use_stmt, crhs2);
    2749              :                     }
    2750              :                 }
    2751         2618 :               update_stmt (use_stmt);
    2752         2618 :               if (atomic_op != BIT_AND_EXPR
    2753         2618 :                   && atomic_op != BIT_IOR_EXPR
    2754         2618 :                   && !stmt_ends_bb_p (stmt2))
    2755              :                 {
    2756              :                   /* For the benefit of debug stmts, emit stmt(s) to set
    2757              :                      lhs2 to the value it had from the new builtin.
    2758              :                      E.g. if it was previously:
    2759              :                      lhs2 = __atomic_fetch_add_8 (ptr, arg, 0);
    2760              :                      emit:
    2761              :                      new_lhs = __atomic_add_fetch_8 (ptr, arg, 0);
    2762              :                      lhs2 = new_lhs - arg;
    2763              :                      We also keep cast_stmt if any in the IL for
    2764              :                      the same reasons.
    2765              :                      These stmts will be DCEd later and proper debug info
    2766              :                      will be emitted.
    2767              :                      This is only possible for reversible operations
    2768              :                      (+/-/^) and without -fnon-call-exceptions.  */
    2769         2277 :                   gsi = gsi_for_stmt (stmt2);
    2770         2277 :                   tree type = TREE_TYPE (lhs2);
    2771         2277 :                   if (TREE_CODE (arg) == INTEGER_CST)
    2772         1695 :                     arg = fold_convert (type, arg);
    2773          582 :                   else if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
    2774              :                     {
    2775            0 :                       tree narg = make_ssa_name (type);
    2776            0 :                       gimple *g = gimple_build_assign (narg, NOP_EXPR, arg);
    2777            0 :                       gsi_insert_after (&gsi, g, GSI_NEW_STMT);
    2778            0 :                       arg = narg;
    2779              :                     }
    2780         2277 :                   enum tree_code rcode;
    2781         2277 :                   switch (atomic_op)
    2782              :                     {
    2783              :                     case PLUS_EXPR: rcode = MINUS_EXPR; break;
    2784          733 :                     case MINUS_EXPR: rcode = PLUS_EXPR; break;
    2785          498 :                     case BIT_XOR_EXPR: rcode = atomic_op; break;
    2786              :                     default: gcc_unreachable ();
    2787              :                     }
    2788         2277 :                   gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
    2789         2277 :                   gsi_insert_after (&gsi, g, GSI_NEW_STMT);
    2790         2277 :                   update_stmt (stmt2);
    2791              :                 }
    2792              :               else
    2793              :                 {
    2794              :                   /* For e.g.
    2795              :                      lhs2 = __atomic_fetch_or_8 (ptr, arg, 0);
    2796              :                      after we change it to
    2797              :                      new_lhs = __atomic_or_fetch_8 (ptr, arg, 0);
    2798              :                      there is no way to find out the lhs2 value (i.e.
    2799              :                      what the atomic memory contained before the operation),
    2800              :                      values of some bits are lost.  We have checked earlier
    2801              :                      that we don't have any non-debug users except for what
    2802              :                      we are already changing, so we need to reset the
    2803              :                      debug stmts and remove the cast_stmt if any.  */
    2804          341 :                   imm_use_iterator iter;
    2805          676 :                   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2)
    2806          335 :                     if (use_stmt != cast_stmt)
    2807              :                       {
    2808          168 :                         gcc_assert (is_gimple_debug (use_stmt));
    2809          168 :                         gimple_debug_bind_reset_value (use_stmt);
    2810          168 :                         update_stmt (use_stmt);
    2811          341 :                       }
    2812          341 :                   if (cast_stmt)
    2813              :                     {
    2814          167 :                       gsi = gsi_for_stmt (cast_stmt);
    2815          167 :                       gsi_remove (&gsi, true);
    2816              :                     }
    2817          341 :                   update_stmt (stmt2);
    2818          341 :                   release_ssa_name (lhs2);
    2819              :                 }
    2820              :             }
    2821              :         }
    2822              :       break;
    2823              : 
    2824              :     default:
    2825              :       break;
    2826              :     }
    2827              :   return false;
    2828              : }
    2829              : 
    2830              : /* Given a ssa_name in NAME see if it was defined by an assignment and
    2831              :    set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
    2832              :    to the second operand on the rhs. */
    2833              : 
    2834              : static inline void
    2835     17975347 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
    2836              : {
    2837     17975347 :   enum tree_code code1;
    2838     17975347 :   tree arg11;
    2839     17975347 :   tree arg21;
    2840     17975347 :   tree arg31;
    2841     17975347 :   enum gimple_rhs_class grhs_class;
    2842              : 
    2843     17975347 :   code1 = TREE_CODE (name);
    2844     17975347 :   arg11 = name;
    2845     17975347 :   arg21 = NULL_TREE;
    2846     17975347 :   arg31 = NULL_TREE;
    2847     17975347 :   grhs_class = get_gimple_rhs_class (code1);
    2848              : 
    2849     17975347 :   if (code1 == SSA_NAME)
    2850              :     {
    2851     11962206 :       auto def = safe_dyn_cast<gassign *> (SSA_NAME_DEF_STMT (name));
    2852              : 
    2853      7410778 :       if (def && can_propagate_from (def))
    2854              :         {
    2855      5098968 :           code1 = gimple_assign_rhs_code (def);
    2856      5098968 :           arg11 = gimple_assign_rhs1 (def);
    2857      5098968 :           arg21 = gimple_assign_rhs2 (def);
    2858      5098968 :           arg31 = gimple_assign_rhs3 (def);
    2859              :         }
    2860              :     }
    2861      6013141 :   else if (grhs_class != GIMPLE_SINGLE_RHS)
    2862            0 :     code1 = ERROR_MARK;
    2863              : 
    2864     17975347 :   *code = code1;
    2865     17975347 :   *arg1 = arg11;
    2866     17975347 :   if (arg2)
    2867     17958146 :     *arg2 = arg21;
    2868     17975347 :   if (arg31)
    2869         2839 :     *code = ERROR_MARK;
    2870     17975347 : }
    2871              : 
    2872              : 
    2873              : /* Recognize rotation patterns.  Return true if a transformation
    2874              :    applied, otherwise return false.
    2875              : 
    2876              :    We are looking for X with unsigned type T with bitsize B, OP being
    2877              :    +, | or ^, some type T2 wider than T.  For:
    2878              :    (X << CNT1) OP (X >> CNT2)                               iff CNT1 + CNT2 == B
    2879              :    ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
    2880              : 
    2881              :    transform these into:
    2882              :    X r<< CNT1
    2883              : 
    2884              :    Or for:
    2885              :    (X << Y) OP (X >> (B - Y))
    2886              :    (X << (int) Y) OP (X >> (int) (B - Y))
    2887              :    ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
    2888              :    ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
    2889              :    (X << Y) | (X >> ((-Y) & (B - 1)))
    2890              :    (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
    2891              :    ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
    2892              :    ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
    2893              : 
    2894              :    transform these into (last 2 only if ranger can prove Y < B
    2895              :    or Y = N * B):
    2896              :    X r<< Y
    2897              :    or
    2898              :    X r<< (& & (B - 1))
    2899              :    The latter for the forms with T2 wider than T if ranger can't prove Y < B.
    2900              : 
    2901              :    Or for:
    2902              :    (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1)))
    2903              :    (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1)))
    2904              :    ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1))))
    2905              :    ((T) ((T2) X << (int) (Y & (B - 1)))) \
    2906              :      | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
    2907              : 
    2908              :    transform these into:
    2909              :    X r<< (Y & (B - 1))
    2910              : 
    2911              :    Note, in the patterns with T2 type, the type of OP operands
    2912              :    might be even a signed type, but should have precision B.
    2913              :    Expressions with & (B - 1) should be recognized only if B is
    2914              :    a power of 2.  */
    2915              : 
    2916              : static bool
    2917     10516252 : simplify_rotate (gimple_stmt_iterator *gsi)
    2918              : {
    2919     10516252 :   gimple *stmt = gsi_stmt (*gsi);
    2920     10516252 :   tree arg[2], rtype, rotcnt = NULL_TREE;
    2921     10516252 :   tree def_arg1[2], def_arg2[2];
    2922     10516252 :   enum tree_code def_code[2];
    2923     10516252 :   tree lhs;
    2924     10516252 :   int i;
    2925     10516252 :   bool swapped_p = false;
    2926     10516252 :   gimple *g;
    2927     10516252 :   gimple *def_arg_stmt[2] = { NULL, NULL };
    2928     10516252 :   int wider_prec = 0;
    2929     10516252 :   bool add_masking = false;
    2930              : 
    2931     10516252 :   arg[0] = gimple_assign_rhs1 (stmt);
    2932     10516252 :   arg[1] = gimple_assign_rhs2 (stmt);
    2933     10516252 :   rtype = TREE_TYPE (arg[0]);
    2934              : 
    2935              :   /* Only create rotates in complete modes.  Other cases are not
    2936              :      expanded properly.  */
    2937     10516252 :   if (!INTEGRAL_TYPE_P (rtype)
    2938     10516252 :       || !type_has_mode_precision_p (rtype))
    2939              :     return false;
    2940              : 
    2941     26818791 :   for (i = 0; i < 2; i++)
    2942              :     {
    2943     17879194 :       defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2944     17879194 :       if (TREE_CODE (arg[i]) == SSA_NAME)
    2945     11866053 :         def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
    2946              :     }
    2947              : 
    2948              :   /* Look through narrowing (or same precision) conversions.  */
    2949      7965383 :   if (CONVERT_EXPR_CODE_P (def_code[0])
    2950       974214 :       && CONVERT_EXPR_CODE_P (def_code[1])
    2951       142561 :       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
    2952       118285 :       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
    2953       110635 :       && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
    2954       110635 :          == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
    2955        64197 :       && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
    2956        44747 :       && has_single_use (arg[0])
    2957      8971798 :       && has_single_use (arg[1]))
    2958              :     {
    2959        26813 :       wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
    2960        80439 :       for (i = 0; i < 2; i++)
    2961              :         {
    2962        53626 :           arg[i] = def_arg1[i];
    2963        53626 :           defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2964        53626 :           if (TREE_CODE (arg[i]) == SSA_NAME)
    2965        53626 :             def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
    2966              :         }
    2967              :     }
    2968              :   else
    2969              :     {
    2970              :       /* Handle signed rotate; the RSHIFT_EXPR has to be done
    2971              :          in unsigned type but LSHIFT_EXPR could be signed.  */
    2972      8912784 :       i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
    2973      7947649 :       if (CONVERT_EXPR_CODE_P (def_code[i])
    2974       965135 :           && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
    2975        32350 :           && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
    2976        31069 :           && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
    2977      8917692 :           && has_single_use (arg[i]))
    2978              :         {
    2979         2061 :           arg[i] = def_arg1[i];
    2980         2061 :           defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2981         2061 :           if (TREE_CODE (arg[i]) == SSA_NAME)
    2982         2061 :             def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
    2983              :         }
    2984              :     }
    2985              : 
    2986              :   /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR.  */
    2987      9130074 :   for (i = 0; i < 2; i++)
    2988      9104470 :     if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
    2989              :       return false;
    2990       231984 :     else if (!has_single_use (arg[i]))
    2991              :       return false;
    2992        25604 :   if (def_code[0] == def_code[1])
    2993              :     return false;
    2994              : 
    2995              :   /* If we've looked through narrowing conversions before, look through
    2996              :      widening conversions from unsigned type with the same precision
    2997              :      as rtype here.  */
    2998        21247 :   if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype))
    2999        19345 :     for (i = 0; i < 2; i++)
    3000              :       {
    3001        12897 :         tree tem;
    3002        12897 :         enum tree_code code;
    3003        12897 :         defcodefor_name (def_arg1[i], &code, &tem, NULL);
    3004            1 :         if (!CONVERT_EXPR_CODE_P (code)
    3005        12896 :             || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
    3006        25793 :             || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
    3007            1 :           return false;
    3008        12896 :         def_arg1[i] = tem;
    3009              :       }
    3010              :   /* Both shifts have to use the same first operand.  */
    3011        21246 :   if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
    3012        33542 :       || !types_compatible_p (TREE_TYPE (def_arg1[0]),
    3013        12296 :                               TREE_TYPE (def_arg1[1])))
    3014              :     {
    3015         8950 :       if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
    3016         8950 :            != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
    3017         8950 :           || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
    3018         8950 :               == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
    3019         8926 :         return false;
    3020              : 
    3021              :       /* Handle signed rotate; the RSHIFT_EXPR has to be done
    3022              :          in unsigned type but LSHIFT_EXPR could be signed.  */
    3023          485 :       i = def_code[0] != RSHIFT_EXPR;
    3024          485 :       if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
    3025              :         return false;
    3026              : 
    3027          448 :       tree tem;
    3028          448 :       enum tree_code code;
    3029          448 :       defcodefor_name (def_arg1[i], &code, &tem, NULL);
    3030          305 :       if (!CONVERT_EXPR_CODE_P (code)
    3031          143 :           || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
    3032          591 :           || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
    3033              :         return false;
    3034          134 :       def_arg1[i] = tem;
    3035          134 :       if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
    3036          158 :           || !types_compatible_p (TREE_TYPE (def_arg1[0]),
    3037           24 :                                   TREE_TYPE (def_arg1[1])))
    3038              :         return false;
    3039              :     }
    3040        12296 :   else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
    3041              :     return false;
    3042              : 
    3043              :   /* CNT1 + CNT2 == B case above.  */
    3044        11065 :   if (tree_fits_uhwi_p (def_arg2[0])
    3045         1568 :       && tree_fits_uhwi_p (def_arg2[1])
    3046        11065 :       && tree_to_uhwi (def_arg2[0])
    3047         1568 :          + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype))
    3048              :     rotcnt = def_arg2[0];
    3049        10142 :   else if (TREE_CODE (def_arg2[0]) != SSA_NAME
    3050         9497 :            || TREE_CODE (def_arg2[1]) != SSA_NAME)
    3051              :     return false;
    3052              :   else
    3053              :     {
    3054         9497 :       tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2];
    3055         9497 :       enum tree_code cdef_code[2];
    3056         9497 :       gimple *def_arg_alt_stmt[2] = { NULL, NULL };
    3057         9497 :       int check_range = 0;
    3058         9497 :       gimple *check_range_stmt = NULL;
    3059              :       /* Look through conversion of the shift count argument.
    3060              :          The C/C++ FE cast any shift count argument to integer_type_node.
    3061              :          The only problem might be if the shift count type maximum value
    3062              :          is equal or smaller than number of bits in rtype.  */
    3063        28491 :       for (i = 0; i < 2; i++)
    3064              :         {
    3065        18994 :           def_arg2_alt[i] = def_arg2[i];
    3066        18994 :           defcodefor_name (def_arg2[i], &cdef_code[i],
    3067              :                            &cdef_arg1[i], &cdef_arg2[i]);
    3068        14723 :           if (CONVERT_EXPR_CODE_P (cdef_code[i])
    3069         4271 :               && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i]))
    3070         4271 :               && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i]))
    3071         4271 :                  > floor_log2 (TYPE_PRECISION (rtype))
    3072        23265 :               && type_has_mode_precision_p (TREE_TYPE (cdef_arg1[i])))
    3073              :             {
    3074         4271 :               def_arg2_alt[i] = cdef_arg1[i];
    3075         4271 :               if (TREE_CODE (def_arg2[i]) == SSA_NAME)
    3076         4271 :                 def_arg_alt_stmt[i] = SSA_NAME_DEF_STMT (def_arg2[i]);
    3077         4271 :               defcodefor_name (def_arg2_alt[i], &cdef_code[i],
    3078              :                                &cdef_arg1[i], &cdef_arg2[i]);
    3079              :             }
    3080              :           else
    3081        14723 :             def_arg_alt_stmt[i] = def_arg_stmt[i];
    3082              :         }
    3083        25816 :       for (i = 0; i < 2; i++)
    3084              :         /* Check for one shift count being Y and the other B - Y,
    3085              :            with optional casts.  */
    3086        18643 :         if (cdef_code[i] == MINUS_EXPR
    3087          862 :             && tree_fits_shwi_p (cdef_arg1[i])
    3088          862 :             && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype)
    3089        19465 :             && TREE_CODE (cdef_arg2[i]) == SSA_NAME)
    3090              :           {
    3091          822 :             tree tem;
    3092          822 :             enum tree_code code;
    3093              : 
    3094          822 :             if (cdef_arg2[i] == def_arg2[1 - i]
    3095          472 :                 || cdef_arg2[i] == def_arg2_alt[1 - i])
    3096              :               {
    3097          350 :                 rotcnt = cdef_arg2[i];
    3098          350 :                 check_range = -1;
    3099          350 :                 if (cdef_arg2[i] == def_arg2[1 - i])
    3100          350 :                   check_range_stmt = def_arg_stmt[1 - i];
    3101              :                 else
    3102            0 :                   check_range_stmt = def_arg_alt_stmt[1 - i];
    3103          806 :                 break;
    3104              :               }
    3105          472 :             defcodefor_name (cdef_arg2[i], &code, &tem, NULL);
    3106           16 :             if (CONVERT_EXPR_CODE_P (code)
    3107          456 :                 && INTEGRAL_TYPE_P (TREE_TYPE (tem))
    3108          456 :                 && TYPE_PRECISION (TREE_TYPE (tem))
    3109          456 :                    > floor_log2 (TYPE_PRECISION (rtype))
    3110          456 :                 && type_has_mode_precision_p (TREE_TYPE (tem))
    3111          928 :                 && (tem == def_arg2[1 - i]
    3112          288 :                     || tem == def_arg2_alt[1 - i]))
    3113              :               {
    3114          456 :                 rotcnt = tem;
    3115          456 :                 check_range = -1;
    3116          456 :                 if (tem == def_arg2[1 - i])
    3117          168 :                   check_range_stmt = def_arg_stmt[1 - i];
    3118              :                 else
    3119          288 :                   check_range_stmt = def_arg_alt_stmt[1 - i];
    3120              :                 break;
    3121              :               }
    3122              :           }
    3123              :         /* The above sequence isn't safe for Y being 0,
    3124              :            because then one of the shifts triggers undefined behavior.
    3125              :            This alternative is safe even for rotation count of 0.
    3126              :            One shift count is Y and the other (-Y) & (B - 1).
    3127              :            Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1).  */
    3128        17821 :         else if (cdef_code[i] == BIT_AND_EXPR
    3129        28752 :                  && pow2p_hwi (TYPE_PRECISION (rtype))
    3130        12433 :                  && tree_fits_shwi_p (cdef_arg2[i])
    3131        24866 :                  && tree_to_shwi (cdef_arg2[i])
    3132        12433 :                     == TYPE_PRECISION (rtype) - 1
    3133        12373 :                  && TREE_CODE (cdef_arg1[i]) == SSA_NAME
    3134        30194 :                  && gimple_assign_rhs_code (stmt) == BIT_IOR_EXPR)
    3135              :           {
    3136         2313 :             tree tem;
    3137         2313 :             enum tree_code code;
    3138              : 
    3139         2313 :             defcodefor_name (cdef_arg1[i], &code, &tem, NULL);
    3140         2114 :             if (CONVERT_EXPR_CODE_P (code)
    3141          199 :                 && INTEGRAL_TYPE_P (TREE_TYPE (tem))
    3142          199 :                 && TYPE_PRECISION (TREE_TYPE (tem))
    3143          199 :                    > floor_log2 (TYPE_PRECISION (rtype))
    3144         2512 :                 && type_has_mode_precision_p (TREE_TYPE (tem)))
    3145          199 :               defcodefor_name (tem, &code, &tem, NULL);
    3146              : 
    3147         2313 :             if (code == NEGATE_EXPR)
    3148              :               {
    3149         1532 :                 if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i])
    3150              :                   {
    3151          853 :                     rotcnt = tem;
    3152          853 :                     check_range = 1;
    3153          853 :                     if (tem == def_arg2[1 - i])
    3154          845 :                       check_range_stmt = def_arg_stmt[1 - i];
    3155              :                     else
    3156            8 :                       check_range_stmt = def_arg_alt_stmt[1 - i];
    3157         1518 :                     break;
    3158              :                   }
    3159          679 :                 tree tem2;
    3160          679 :                 defcodefor_name (tem, &code, &tem2, NULL);
    3161          237 :                 if (CONVERT_EXPR_CODE_P (code)
    3162          442 :                     && INTEGRAL_TYPE_P (TREE_TYPE (tem2))
    3163          442 :                     && TYPE_PRECISION (TREE_TYPE (tem2))
    3164          442 :                        > floor_log2 (TYPE_PRECISION (rtype))
    3165         1121 :                     && type_has_mode_precision_p (TREE_TYPE (tem2)))
    3166              :                   {
    3167          442 :                     if (tem2 == def_arg2[1 - i]
    3168          442 :                         || tem2 == def_arg2_alt[1 - i])
    3169              :                       {
    3170          228 :                         rotcnt = tem2;
    3171          228 :                         check_range = 1;
    3172          228 :                         if (tem2 == def_arg2[1 - i])
    3173            0 :                           check_range_stmt = def_arg_stmt[1 - i];
    3174              :                         else
    3175          228 :                           check_range_stmt = def_arg_alt_stmt[1 - i];
    3176              :                         break;
    3177              :                       }
    3178              :                   }
    3179              :                 else
    3180          237 :                   tem2 = NULL_TREE;
    3181              : 
    3182          451 :                 if (cdef_code[1 - i] == BIT_AND_EXPR
    3183          438 :                     && tree_fits_shwi_p (cdef_arg2[1 - i])
    3184          876 :                     && tree_to_shwi (cdef_arg2[1 - i])
    3185          438 :                        == TYPE_PRECISION (rtype) - 1
    3186          889 :                     && TREE_CODE (cdef_arg1[1 - i]) == SSA_NAME)
    3187              :                   {
    3188          438 :                     if (tem == cdef_arg1[1 - i]
    3189          213 :                         || tem2 == cdef_arg1[1 - i])
    3190              :                       {
    3191              :                         rotcnt = def_arg2[1 - i];
    3192          437 :                         break;
    3193              :                       }
    3194          193 :                     tree tem3;
    3195          193 :                     defcodefor_name (cdef_arg1[1 - i], &code, &tem3, NULL);
    3196            0 :                     if (CONVERT_EXPR_CODE_P (code)
    3197          193 :                         && INTEGRAL_TYPE_P (TREE_TYPE (tem3))
    3198          193 :                         && TYPE_PRECISION (TREE_TYPE (tem3))
    3199          193 :                            > floor_log2 (TYPE_PRECISION (rtype))
    3200          386 :                         && type_has_mode_precision_p (TREE_TYPE (tem3)))
    3201              :                       {
    3202          193 :                         if (tem == tem3 || tem2 == tem3)
    3203              :                           {
    3204              :                             rotcnt = def_arg2[1 - i];
    3205              :                             break;
    3206              :                           }
    3207              :                       }
    3208              :                   }
    3209              :               }
    3210              :           }
    3211         2324 :       if (check_range && wider_prec > TYPE_PRECISION (rtype))
    3212              :         {
    3213         1533 :           if (TREE_CODE (rotcnt) != SSA_NAME)
    3214          573 :             return false;
    3215         1533 :           int_range_max r;
    3216         1533 :           range_query *q = get_range_query (cfun);
    3217         1533 :           if (q == get_global_range_query ())
    3218         1522 :             q = enable_ranger (cfun);
    3219         1533 :           if (!q->range_of_expr (r, rotcnt, check_range_stmt))
    3220              :             {
    3221            0 :               if (check_range > 0)
    3222              :                 return false;
    3223            0 :               r.set_varying (TREE_TYPE (rotcnt));
    3224              :             }
    3225         1533 :           int prec = TYPE_PRECISION (TREE_TYPE (rotcnt));
    3226         1533 :           signop sign = TYPE_SIGN (TREE_TYPE (rotcnt));
    3227         1533 :           wide_int min = wide_int::from (TYPE_PRECISION (rtype), prec, sign);
    3228         1533 :           wide_int max = wide_int::from (wider_prec - 1, prec, sign);
    3229         1533 :           if (check_range < 0)
    3230          616 :             max = min;
    3231         1533 :           int_range<1> r2 (TREE_TYPE (rotcnt), min, max);
    3232         1533 :           r.intersect (r2);
    3233         1533 :           if (!r.undefined_p ())
    3234              :             {
    3235         1181 :               if (check_range > 0)
    3236              :                 {
    3237          589 :                   int_range_max r3;
    3238         1844 :                   for (int i = TYPE_PRECISION (rtype) + 1; i < wider_prec;
    3239         1255 :                        i += TYPE_PRECISION (rtype))
    3240              :                     {
    3241         1255 :                       int j = i + TYPE_PRECISION (rtype) - 2;
    3242         1255 :                       min = wide_int::from (i, prec, sign);
    3243         1255 :                       max = wide_int::from (MIN (j, wider_prec - 1),
    3244         1255 :                                             prec, sign);
    3245         1255 :                       int_range<1> r4 (TREE_TYPE (rotcnt), min, max);
    3246         1255 :                       r3.union_ (r4);
    3247         1255 :                     }
    3248          589 :                   r.intersect (r3);
    3249          589 :                   if (!r.undefined_p ())
    3250          573 :                     return false;
    3251          589 :                 }
    3252              :               add_masking = true;
    3253              :             }
    3254         1533 :         }
    3255         8924 :       if (rotcnt == NULL_TREE)
    3256              :         return false;
    3257         1751 :       swapped_p = i != 1;
    3258              :     }
    3259              : 
    3260         2674 :   if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]),
    3261         2674 :                                   TREE_TYPE (rotcnt)))
    3262              :     {
    3263          496 :       g = gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2[0])),
    3264              :                                NOP_EXPR, rotcnt);
    3265          496 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    3266          496 :       rotcnt = gimple_assign_lhs (g);
    3267              :     }
    3268         2674 :   if (add_masking)
    3269              :     {
    3270          608 :       g = gimple_build_assign (make_ssa_name (TREE_TYPE (rotcnt)),
    3271              :                                BIT_AND_EXPR, rotcnt,
    3272          608 :                                build_int_cst (TREE_TYPE (rotcnt),
    3273          608 :                                               TYPE_PRECISION (rtype) - 1));
    3274          608 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    3275          608 :       rotcnt = gimple_assign_lhs (g);
    3276              :     }
    3277         2674 :   lhs = gimple_assign_lhs (stmt);
    3278         2674 :   if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
    3279         1010 :     lhs = make_ssa_name (TREE_TYPE (def_arg1[0]));
    3280         2674 :   g = gimple_build_assign (lhs,
    3281         2674 :                            ((def_code[0] == LSHIFT_EXPR) ^ swapped_p)
    3282              :                            ? LROTATE_EXPR : RROTATE_EXPR, def_arg1[0], rotcnt);
    3283         2674 :   if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
    3284              :     {
    3285         1010 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    3286         1010 :       g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, lhs);
    3287              :     }
    3288         2674 :   gsi_replace (gsi, g, false);
    3289         2674 :   return true;
    3290              : }
    3291              : 
    3292              : 
    3293              : /* Check whether an array contains a valid table according to VALIDATE_FN.  */
    3294              : template<typename ValidateFn>
    3295              : static bool
    3296           55 : check_table_array (tree ctor, HOST_WIDE_INT &zero_val, unsigned bits,
    3297              :                   ValidateFn validate_fn)
    3298              : {
    3299              :   tree elt, idx;
    3300           55 :   unsigned HOST_WIDE_INT i, raw_idx = 0;
    3301           55 :   unsigned matched = 0;
    3302              : 
    3303           55 :   zero_val = 0;
    3304              : 
    3305         2608 :   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
    3306              :     {
    3307         2608 :       if (!tree_fits_shwi_p (idx))
    3308              :         return false;
    3309         2608 :       if (!tree_fits_shwi_p (elt) && TREE_CODE (elt) != RAW_DATA_CST)
    3310              :         return false;
    3311              : 
    3312         2608 :       unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
    3313              :       HOST_WIDE_INT val;
    3314              : 
    3315         2608 :       if (TREE_CODE (elt) == INTEGER_CST)
    3316         2544 :         val = tree_to_shwi (elt);
    3317              :       else
    3318              :         {
    3319           64 :           if (raw_idx == (unsigned) RAW_DATA_LENGTH (elt))
    3320              :             {
    3321            0 :               raw_idx = 0;
    3322            0 :               continue;
    3323              :             }
    3324           64 :           if (TYPE_UNSIGNED (TREE_TYPE (elt)))
    3325            0 :             val = RAW_DATA_UCHAR_ELT (elt, raw_idx);
    3326              :           else
    3327           64 :             val = RAW_DATA_SCHAR_ELT (elt, raw_idx);
    3328           64 :           index += raw_idx;
    3329           64 :           raw_idx++;
    3330           64 :           i--;
    3331              :         }
    3332              : 
    3333         2608 :       if (index > bits * 2)
    3334              :         return false;
    3335              : 
    3336         2608 :       if (index == 0)
    3337              :         {
    3338           55 :           zero_val = val;
    3339           55 :           matched++;
    3340              :         }
    3341              : 
    3342         2608 :       if (val >= 0 && val < bits && validate_fn (val, index))
    3343         1888 :         matched++;
    3344              : 
    3345         2608 :       if (matched > bits)
    3346              :         return true;
    3347              :     }
    3348              : 
    3349              :   return false;
    3350              : }
    3351              : 
    3352              : /* Check whether a string contains a valid table according to VALIDATE_FN.  */
    3353              : template<typename ValidateFn>
    3354              : static bool
    3355           39 : check_table_string (tree string, HOST_WIDE_INT &zero_val,unsigned bits,
    3356              :                     ValidateFn validate_fn)
    3357              : {
    3358           39 :   unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
    3359           39 :   unsigned matched = 0;
    3360           39 :   const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
    3361              : 
    3362           39 :   if (len < bits || len > bits * 2)
    3363              :     return false;
    3364              : 
    3365           39 :   zero_val = p[0];
    3366              : 
    3367         1703 :   for (unsigned i = 0; i < len; i++)
    3368         1664 :     if (p[i] < bits && validate_fn (p[i], i))
    3369         1664 :       matched++;
    3370              : 
    3371           39 :   return matched == bits;
    3372              : }
    3373              : 
    3374              : /* Check whether CTOR contains a valid table according to VALIDATE_FN.  */
    3375              : template<typename ValidateFn>
    3376              : static bool
    3377          102 : check_table (tree ctor, tree type, HOST_WIDE_INT &zero_val, unsigned bits,
    3378              :              ValidateFn validate_fn)
    3379              : {
    3380          102 :   if (TREE_CODE (ctor) == CONSTRUCTOR)
    3381           55 :     return check_table_array (ctor, zero_val, bits, validate_fn);
    3382              :   else if (TREE_CODE (ctor) == STRING_CST
    3383           47 :            && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
    3384           39 :     return check_table_string (ctor, zero_val, bits, validate_fn);
    3385              :   return false;
    3386              : }
    3387              : 
    3388              : /* Match.pd function to match the ctz expression.  */
    3389              : extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree));
    3390              : extern bool gimple_clz_table_index (tree, tree *, tree (*)(tree));
    3391              : extern bool gimple_clz_msb_iso_table_index (tree, tree *, tree (*)(tree));
    3392              : 
    3393              : /* Recognize count leading and trailing zeroes idioms.
    3394              :    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
    3395              :    constant which when multiplied by a power of 2 creates a unique value
    3396              :    in the top 5 or 6 bits.  This is then indexed into a table which maps it
    3397              :    to the number of trailing zeroes.  Array[0] is returned so the caller can
    3398              :    emit an appropriate sequence depending on whether ctz (0) is defined on
    3399              :    the target.  */
    3400              : 
    3401              : static bool
    3402      2046494 : simplify_count_zeroes (gimple_stmt_iterator *gsi)
    3403              : {
    3404      2046494 :   gimple *stmt = gsi_stmt (*gsi);
    3405      2046494 :   tree array_ref = gimple_assign_rhs1 (stmt);
    3406      2046494 :   tree res_ops[3];
    3407              : 
    3408      2046494 :   gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
    3409              : 
    3410      2046494 :   internal_fn fn = IFN_LAST;
    3411              :   /* When true, the matched idiom is a CLZ using DeBruijn CTZ on the
    3412              :      isolated MSB -- see clz_msb_iso_table_index in match.pd.  The
    3413              :      table stores MSB positions and must satisfy the direct CTZ
    3414              :      DeBruijn property, so we validate it with the CTZ checkfn even
    3415              :      though we emit IFN_CLZ code.  */
    3416      2046494 :   bool clz_via_ctz = false;
    3417              :   /* For CTZ we recognize ((x & -x) * C) >> SHIFT where the array data
    3418              :      represents the number of trailing zeros.  */
    3419      2046494 :   if (gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL))
    3420              :     fn = IFN_CTZ;
    3421              :   /* For CLZ we recognize
    3422              :        x |= x >> 1;
    3423              :        x |= x >> 2;
    3424              :        x |= x >> 4;
    3425              :        x |= x >> 8;
    3426              :        x |= x >> 16;
    3427              :        (x * C) >> SHIFT
    3428              :      where 31 minus the array data represents the number of leading zeros.  */
    3429      2046428 :   else if (gimple_clz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0],
    3430              :                                    NULL))
    3431              :     fn = IFN_CLZ;
    3432              :   /* Variant CLZ idiom: after the OR-cascade sets all bits from 0 to
    3433              :      the original MSB, (value - (value >> 1)) isolates the MSB as a
    3434              :      power of two (2^k), and the subsequent DeBruijn multiply-and-shift
    3435              :      is a CTZ-style lookup on 2^k.  The table stores MSB positions
    3436              :      directly.  */
    3437      2046388 :   else if (gimple_clz_msb_iso_table_index (TREE_OPERAND (array_ref, 1),
    3438              :                                            &res_ops[0], NULL))
    3439              :     {
    3440              :       fn = IFN_CLZ;
    3441              :       clz_via_ctz = true;
    3442              :     }
    3443              :   else
    3444              :     return false;
    3445              : 
    3446          107 :   HOST_WIDE_INT zero_val;
    3447          107 :   tree type = TREE_TYPE (array_ref);
    3448          107 :   tree array = TREE_OPERAND (array_ref, 0);
    3449          107 :   tree input_type = TREE_TYPE (res_ops[0]);
    3450          107 :   unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type));
    3451              : 
    3452              :   /* Check the array element type is integral and not wider than 64 bits,
    3453              :      and the input is an unsigned 32-bit or 64-bit type.  The table values
    3454              :      are bit positions in [0, input_bits - 1], so any integer element type
    3455              :      with at least 6 bits of precision suffices; the cap is just to keep
    3456              :      the transformation simple.  */
    3457          107 :   if (!INTEGRAL_TYPE_P (type) || TYPE_PRECISION (type) > 64
    3458          214 :       || !TYPE_UNSIGNED (input_type))
    3459              :     return false;
    3460          103 :   if (input_bits != 32 && input_bits != 64)
    3461              :     return false;
    3462              : 
    3463          103 :   if (!direct_internal_fn_supported_p (fn, input_type, OPTIMIZE_FOR_BOTH))
    3464              :     return false;
    3465              : 
    3466              :   /* Check the lower bound of the array is zero.  */
    3467          103 :   tree low = array_ref_low_bound (array_ref);
    3468          103 :   if (!low || !integer_zerop (low))
    3469              :     return false;
    3470              : 
    3471              :   /* Check the shift extracts the top 5..7 bits.  */
    3472          103 :   unsigned shiftval = tree_to_shwi (res_ops[2]);
    3473          103 :   if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
    3474              :     return false;
    3475              : 
    3476          102 :   tree ctor = ctor_for_folding (array);
    3477          102 :   if (!ctor)
    3478              :     return false;
    3479          102 :   unsigned HOST_WIDE_INT mulval = tree_to_uhwi (res_ops[1]);
    3480              :   /* CTZ and the MSB-isolation CLZ variant both use the direct CTZ
    3481              :      DeBruijn check (table[(magic << data) >> shift] == data).  */
    3482          102 :   if (fn == IFN_CTZ || clz_via_ctz)
    3483              :     {
    3484         2782 :       auto checkfn = [&](unsigned data, unsigned i) -> bool
    3485              :         {
    3486         2720 :           unsigned HOST_WIDE_INT mask
    3487         2720 :             = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
    3488         2720 :           return (((mulval << data) & mask) >> shiftval) == i;
    3489           62 :         };
    3490           62 :       if (!check_table (ctor, type, zero_val, input_bits, checkfn))
    3491            8 :         return false;
    3492              :     }
    3493           40 :   else if (fn == IFN_CLZ)
    3494              :     {
    3495         1569 :       auto checkfn = [&](unsigned data, unsigned i) -> bool
    3496              :         {
    3497         1529 :           unsigned HOST_WIDE_INT mask
    3498         1529 :             = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
    3499              :           /* The OR-cascade produces a value with all bits from 0 to the
    3500              :              original MSB set.  Compute (1 << (data + 1)) - 1 to simulate
    3501              :              that value.  When data + 1 equals HOST_BITS_PER_WIDE_INT
    3502              :              (i.e. data is the MSB position of a 64-bit input) the shift
    3503              :              is undefined behavior, so handle that case explicitly using
    3504              :              all-ones.  Without this, any well-formed 64-bit DeBruijn CLZ
    3505              :              table is rejected because its entry for the all-ones input
    3506              :              correctly maps to the MSB (e.g. table[...] == 63).
    3507              :              PR tree-optimization/122569.  */
    3508         3057 :           unsigned HOST_WIDE_INT all_bits_below
    3509              :             = (data + 1 == HOST_BITS_PER_WIDE_INT)
    3510         1529 :               ? HOST_WIDE_INT_M1U
    3511         1528 :               : ((HOST_WIDE_INT_1U << (data + 1)) - 1);
    3512         1529 :           return (((all_bits_below * mulval) & mask) >> shiftval) == i;
    3513           40 :         };
    3514           40 :     if (!check_table (ctor, type, zero_val, input_bits, checkfn))
    3515            0 :       return false;
    3516              :     }
    3517              : 
    3518              :   /* The sequence below computes the result in int, for a CLZ as
    3519              :      INPUT_BITS - 1 minus the count.  If the table entry for zero does not
    3520              :      survive that, select it in TYPE after the conversion instead.  */
    3521           94 :   HOST_WIDE_INT table_zero = zero_val;
    3522           94 :   bool late_select
    3523           94 :     = (!wi::fits_to_tree_p (zero_val, integer_type_node)
    3524           94 :        || (fn == IFN_CLZ
    3525           34 :            && !wi::fits_to_tree_p (input_bits - 1 - zero_val,
    3526           34 :                                  integer_type_node)));
    3527              : 
    3528           94 :   HOST_WIDE_INT ctz_val = -1;
    3529           94 :   bool zero_ok;
    3530           94 :   if (fn == IFN_CTZ)
    3531              :     {
    3532           53 :       ctz_val = 0;
    3533          106 :       zero_ok = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
    3534              :                                            ctz_val) == 2;
    3535              :     }
    3536           41 :   else if (fn == IFN_CLZ)
    3537              :     {
    3538           41 :       ctz_val = 32;
    3539           41 :       zero_ok = CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
    3540              :                                            ctz_val) == 2;
    3541           41 :       if (!late_select)
    3542           34 :         zero_val = input_bits - 1 - zero_val;
    3543              :     }
    3544              :   /* Pass the value at zero only if the target defines it; otherwise the
    3545              :      COND_EXPR below supplies the result for zero.  */
    3546           94 :   int nargs = zero_ok ? 2 : 1;
    3547              : 
    3548              :   /* If the input value can't be zero, don't special case ctz (0).  */
    3549           94 :   range_query *q = get_range_query (cfun);
    3550           94 :   if (q == get_global_range_query ())
    3551           94 :     q = enable_ranger (cfun);
    3552           94 :   int_range_max vr;
    3553           94 :   if (q->range_of_expr (vr, res_ops[0], stmt)
    3554           94 :       && !range_includes_zero_p (vr))
    3555              :     {
    3556           24 :       zero_ok = true;
    3557           24 :       zero_val = 0;
    3558           24 :       ctz_val = 0;
    3559           24 :       nargs = 1;
    3560           24 :       late_select = false;
    3561              :     }
    3562              : 
    3563           94 :   gimple_seq seq = NULL;
    3564           94 :   gimple *g;
    3565          102 :   gcall *call = gimple_build_call_internal (fn, nargs, res_ops[0],
    3566              :                                             nargs == 1 ? NULL_TREE
    3567          102 :                                             : build_int_cst (integer_type_node,
    3568            8 :                                                              ctz_val));
    3569           94 :   gimple_set_location (call, gimple_location (stmt));
    3570           94 :   gimple_set_lhs (call, make_ssa_name (integer_type_node));
    3571           94 :   gimple_seq_add_stmt (&seq, call);
    3572              : 
    3573           94 :   tree prev_lhs = gimple_call_lhs (call);
    3574              : 
    3575           94 :   if (late_select || (zero_ok && zero_val == ctz_val))
    3576              :     ;
    3577              :   /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0.  */
    3578            6 :   else if (zero_ok && zero_val == 0 && ctz_val == input_bits)
    3579              :     {
    3580            5 :       g = gimple_build_assign (make_ssa_name (integer_type_node),
    3581              :                                BIT_AND_EXPR, prev_lhs,
    3582              :                                build_int_cst (integer_type_node,
    3583            5 :                                               input_bits - 1));
    3584            5 :       gimple_set_location (g, gimple_location (stmt));
    3585            5 :       gimple_seq_add_stmt (&seq, g);
    3586            5 :       prev_lhs = gimple_assign_lhs (g);
    3587              :     }
    3588              :   /* As fallback emit a conditional move.  */
    3589              :   else
    3590              :     {
    3591           42 :       g = gimple_build_assign (make_ssa_name (boolean_type_node), EQ_EXPR,
    3592              :                                res_ops[0], build_zero_cst (input_type));
    3593           42 :       gimple_set_location (g, gimple_location (stmt));
    3594           42 :       gimple_seq_add_stmt (&seq, g);
    3595           42 :       tree cond = gimple_assign_lhs (g);
    3596           42 :       g = gimple_build_assign (make_ssa_name (integer_type_node),
    3597              :                                COND_EXPR, cond,
    3598           42 :                                build_int_cst (integer_type_node, zero_val),
    3599              :                                prev_lhs);
    3600           42 :       gimple_set_location (g, gimple_location (stmt));
    3601           42 :       gimple_seq_add_stmt (&seq, g);
    3602           42 :       prev_lhs = gimple_assign_lhs (g);
    3603              :     }
    3604              : 
    3605           94 :   if (fn == IFN_CLZ)
    3606              :     {
    3607           41 :       g = gimple_build_assign (make_ssa_name (integer_type_node),
    3608              :                                MINUS_EXPR,
    3609              :                                build_int_cst (integer_type_node,
    3610           41 :                                               input_bits - 1),
    3611              :                                prev_lhs);
    3612           41 :       gimple_set_location (g, gimple_location (stmt));
    3613           41 :       gimple_seq_add_stmt (&seq, g);
    3614           41 :       prev_lhs = gimple_assign_lhs (g);
    3615              :     }
    3616              : 
    3617           94 :   if (late_select)
    3618              :     {
    3619           21 :       g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, prev_lhs);
    3620           21 :       gimple_set_location (g, gimple_location (stmt));
    3621           21 :       gimple_seq_add_stmt (&seq, g);
    3622           21 :       prev_lhs = gimple_assign_lhs (g);
    3623           21 :       g = gimple_build_assign (make_ssa_name (boolean_type_node), EQ_EXPR,
    3624              :                                res_ops[0], build_zero_cst (input_type));
    3625           21 :       gimple_set_location (g, gimple_location (stmt));
    3626           21 :       gimple_seq_add_stmt (&seq, g);
    3627           21 :       g = gimple_build_assign (gimple_assign_lhs (stmt), COND_EXPR,
    3628              :                                gimple_assign_lhs (g),
    3629           21 :                                build_int_cst (type, table_zero), prev_lhs);
    3630              :     }
    3631              :   else
    3632           73 :     g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
    3633           94 :   gimple_seq_add_stmt (&seq, g);
    3634           94 :   gsi_replace_with_seq (gsi, seq, true);
    3635           94 :   return true;
    3636           94 : }
    3637              : 
    3638              : /* Long-multiply fold framework.
    3639              : 
    3640              :    Walks the outer addition or bit_ior chain on a candidate statement,
    3641              :    classifies each summand against the atom match patterns from
    3642              :    match.pd, and looks the resulting multiset of (kind, extract) tuples
    3643              :    up in a table.  On a hit, three cross-summand consistency checks
    3644              :    decide whether the wide multiply is emitted.  */
    3645              : 
    3646              : /* Match.pd recognizers for the conditional carry-add pattern. */
    3647              : 
    3648              : extern bool gimple_cond_carry_add (tree, tree *, tree (*)(tree));
    3649              : 
    3650              : /* Match.pd functions to match long multiplication.  */
    3651              : 
    3652              : extern bool gimple_mul_hi (tree, tree *, tree (*)(tree));
    3653              : extern bool gimple_mul_lo (tree, tree *, tree (*)(tree));
    3654              : extern bool gimple_mul_hilo (tree, tree *, tree (*)(tree));
    3655              : extern bool gimple_mul_lolo (tree, tree *, tree (*)(tree));
    3656              : extern bool gimple_mul_hihi (tree, tree *, tree (*)(tree));
    3657              : extern bool gimple_mul_cross_sum (tree, tree *, tree (*)(tree));
    3658              : extern bool gimple_mul_low_sum (tree, tree *, tree (*)(tree));
    3659              : extern bool gimple_mul_low_accum (tree, tree *, tree (*)(tree));
    3660              : extern bool gimple_mul_carry_cross_sum (tree, tree *, tree (*)(tree));
    3661              : extern bool gimple_mul_carry_low_sum (tree, tree *, tree (*)(tree));
    3662              : extern bool gimple_mul_carry_low (tree, tree *, tree (*)(tree));
    3663              : extern bool gimple_mul_ladder_sum1 (tree, tree *, tree (*)(tree));
    3664              : extern bool gimple_mul_ladder_sum2 (tree, tree *, tree (*)(tree));
    3665              : extern bool gimple_mul_ladder_sum3 (tree, tree *, tree (*)(tree));
    3666              : extern bool gimple_mul_ladder_part_sum (tree, tree *, tree (*)(tree));
    3667              : 
    3668              : /* Append to SEQ statements assigning DEST the high-part multiply of
    3669              :    OP1 and OP2, emitted as
    3670              :      (N)(((2N) op1 * (2N) op2) >> N).
    3671              :    pass_optimize_widening_mul's convert_mult_to_widen and
    3672              :    convert_mult_to_highpart later rewrite this to a single
    3673              :    WIDEN_MULT_EXPR or MULT_HIGHPART_EXPR when the target supports it,
    3674              :    otherwise the 2N multiply expands directly.  Emitting the canonical
    3675              :    widening shape keeps target-capability decisions in the layer that
    3676              :    already owns them.  */
    3677              : 
    3678              : static void
    3679         1931 : build_mul_high_seq (tree op1, tree op2, tree dest, location_t loc,
    3680              :                     gimple_seq *seq)
    3681              : {
    3682         1931 :   tree op_type = TREE_TYPE (op1);
    3683         1931 :   unsigned int width = TYPE_PRECISION (op_type);
    3684         1931 :   tree wide_type = build_nonstandard_integer_type (width * 2, 1);
    3685              : 
    3686         1931 :   tree wide_a = gimple_convert (seq, loc, wide_type, op1);
    3687         1931 :   tree wide_b = gimple_convert (seq, loc, wide_type, op2);
    3688         1931 :   tree wide_prod = gimple_build (seq, loc, MULT_EXPR, wide_type,
    3689              :                                  wide_a, wide_b);
    3690         1931 :   tree hi = gimple_build (seq, loc, RSHIFT_EXPR, wide_type, wide_prod,
    3691         1931 :                           build_int_cst (integer_type_node, width));
    3692              : 
    3693         1931 :   gimple *prod = gimple_build_assign (dest, NOP_EXPR, hi);
    3694         1931 :   gimple_set_location (prod, loc);
    3695         1931 :   gimple_seq_add_stmt (seq, prod);
    3696         1931 : }
    3697              : 
    3698              : /* Append to SEQ statements combining ACC with each of EXTRAS under
    3699              :    OUTER, the last one assigning to STMT's lhs.  EXTRAS are leaves of
    3700              :    STMT's own chain, so any combining order is valid.  */
    3701              : 
    3702              : static void
    3703          361 : long_mul_apply_extras (tree acc, const vec<tree> &extras, tree_code outer,
    3704              :                        gassign *stmt, gimple_seq *seq)
    3705              : {
    3706          361 :   location_t loc = gimple_location (stmt);
    3707          361 :   tree lhs = gimple_assign_lhs (stmt);
    3708          388 :   for (unsigned i = 0; i + 1 < extras.length (); i++)
    3709           27 :     acc = gimple_build (seq, loc, outer, TREE_TYPE (lhs), acc, extras[i]);
    3710          361 :   gimple *last = gimple_build_assign (lhs, outer, acc, extras.last ());
    3711          361 :   gimple_set_location (last, loc);
    3712          361 :   gimple_seq_add_stmt (seq, last);
    3713          361 : }
    3714              : 
    3715              : /* Replace STMT with a high-part multiply of OP1 and OP2, combining any
    3716              :    EXTRAS back on top under OUTER.  */
    3717              : 
    3718              : static void
    3719         1916 : create_mul_high_seq (tree op1, tree op2, gassign *stmt,
    3720              :                      const vec<tree> &extras, tree_code outer)
    3721              : {
    3722         1916 :   gimple_seq seq = NULL;
    3723         1916 :   tree lhs = gimple_assign_lhs (stmt);
    3724         2277 :   tree dest = extras.is_empty () ? lhs : make_ssa_name (TREE_TYPE (lhs));
    3725         1916 :   build_mul_high_seq (op1, op2, dest, gimple_location (stmt), &seq);
    3726         1916 :   if (!extras.is_empty ())
    3727          361 :     long_mul_apply_extras (dest, extras, outer, stmt, &seq);
    3728         1916 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    3729         1916 :   gsi_replace_with_seq (&gsi, seq, true);
    3730         1916 : }
    3731              : 
    3732              : /* Replace STMT with a low-part multiply of OP1 and OP2, combining any
    3733              :    EXTRAS back on top under OUTER.  */
    3734              : 
    3735              : static void
    3736           28 : create_mul_low_seq (tree op1, tree op2, gassign *stmt,
    3737              :                     const vec<tree> &extras, tree_code outer)
    3738              : {
    3739           28 :   gimple_seq seq = NULL;
    3740           28 :   tree lhs = gimple_assign_lhs (stmt);
    3741           28 :   tree dest = extras.is_empty () ? lhs : make_ssa_name (TREE_TYPE (lhs));
    3742           28 :   gimple *prod = gimple_build_assign (dest, MULT_EXPR, op1, op2);
    3743           28 :   gimple_set_location (prod, gimple_location (stmt));
    3744           28 :   gimple_seq_add_stmt (&seq, prod);
    3745           28 :   if (!extras.is_empty ())
    3746            0 :     long_mul_apply_extras (dest, extras, outer, stmt, &seq);
    3747           28 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    3748           28 :   gsi_replace_with_seq (&gsi, seq, true);
    3749           28 : }
    3750              : 
    3751              : /* Widest match.pd atom (mul_carry_low_sum) takes 7 captures; round up
    3752              :    to 8 for the scratch buffers below.  */
    3753              : static constexpr unsigned LONG_MUL_MAX_CAPTURES = 8;
    3754              : 
    3755              : /* Longest variant in long_mul_table has 4 summands.  */
    3756              : static constexpr unsigned LONG_MUL_MAX_SUMMANDS = 4;
    3757              : 
    3758              : /* Cap on the leaves set aside as not part of the idiom, so an
    3759              :    arbitrarily long unrelated chain still bails early.  */
    3760              : static constexpr unsigned LONG_MUL_MAX_EXTRAS = 4;
    3761              : 
    3762              : namespace {
    3763              : 
    3764              : enum long_mul_kind : unsigned char {
    3765              :   LMK_INVALID,
    3766              :   LMK_MUL_HIHI,
    3767              :   LMK_MUL_LOLO,
    3768              :   LMK_MUL_HILO,
    3769              :   LMK_CROSS_SUM,
    3770              :   LMK_LOW_ACCUM,
    3771              :   LMK_LOW_SUM,
    3772              :   LMK_LADDER_SUM1,
    3773              :   LMK_LADDER_SUM2,
    3774              :   LMK_LADDER_SUM3,
    3775              :   LMK_LADDER_PART_SUM,
    3776              :   LMK_CARRY_LOW,
    3777              :   LMK_CARRY_CROSS_SUM,
    3778              :   LMK_CARRY_LOW_SUM,
    3779              : };
    3780              : 
    3781              : /* How the leaf wraps its inner kind.  Carry kinds use LMX_NONE: their
    3782              :    match.pd pattern bakes the lshift in, so the leaf is already the
    3783              :    complete carry expression.  */
    3784              : 
    3785              : enum long_mul_extract : unsigned char  {
    3786              :   LMX_NONE,
    3787              :   LMX_HI,
    3788              :   LMX_LO,
    3789              :   LMX_SHL_N,
    3790              : };
    3791              : 
    3792              : struct long_mul_summand {
    3793      7104907 :   long_mul_summand () {}
    3794              :   long_mul_summand (tree);
    3795              :   long_mul_summand (const long_mul_summand &) = default;
    3796              :   long_mul_kind kind = LMK_INVALID;
    3797              :   long_mul_extract extract = LMX_NONE;
    3798              :   tree op0 = NULL_TREE, op1 = NULL_TREE;
    3799              :   tree hilo[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
    3800              :   tree carry_a = NULL_TREE, carry_b = NULL_TREE;
    3801              :   unsigned HOST_WIDE_INT shift = 0;
    3802      7081751 :   operator bool () { return kind != LMK_INVALID; }
    3803              : private:
    3804              :   bool classify_carry (tree leaf);
    3805              :   bool classify_hi_extract (tree, unsigned HOST_WIDE_INT);
    3806              :   bool classify_lo_extract (tree);
    3807              :   bool classify_shl_extract (tree, unsigned HOST_WIDE_INT);
    3808              :   bool classify_plus_kinds (tree);
    3809              :   bool classify_bare (tree);
    3810              :   void set (long_mul_kind kind, const tree *res_ops);
    3811              : };
    3812              : 
    3813              : }
    3814              : 
    3815              : /* Walk the OUTER addition or BIT_IOR chain rooted at STMT and collect
    3816              :    the leaf operands into LEAVES.  Descends through single-use
    3817              :    intermediate stmts of the same code.  Returns false once the leaf
    3818              :    count exceeds LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS, so an
    3819              :    overlong chain bails mid-walk instead of after a full traversal.
    3820              : 
    3821              :    If SHARED_DEF_OUT is non-NULL, record there the first inner stmt that
    3822              :    shares the outer code but has more than one use -- descending into it
    3823              :    would change semantics, so it stays as a leaf.  Such a leaf often
    3824              :    classifies as something no row matches, silently disabling the fold;
    3825              :    the caller surfaces this as a dump-file hint.  */
    3826              : 
    3827              : static bool
    3828      3460203 : long_mul_linearize_chain (gassign *stmt, tree_code outer, vec<tree> &leaves,
    3829              :                           gimple **shared_def_out = NULL)
    3830              : {
    3831      3460203 :   auto_vec<tree, 8> stack;
    3832      6920406 :   stack.safe_push (gimple_assign_rhs2 (stmt));
    3833      3460203 :   stack.safe_push (gimple_assign_rhs1 (stmt));
    3834              : 
    3835     10732260 :   while (!stack.is_empty ())
    3836              :     {
    3837      7272852 :       tree t = stack.pop ();
    3838      7272852 :       if (TREE_CODE (t) == SSA_NAME)
    3839              :         {
    3840      4794354 :           auto *def = safe_dyn_cast<gassign *> (SSA_NAME_DEF_STMT (t));
    3841      4209876 :           if (def && gimple_assign_rhs_code (def) == outer)
    3842              :             {
    3843       356052 :               if (has_single_use (t))
    3844              :                 {
    3845       357136 :                   stack.safe_push (gimple_assign_rhs2 (def));
    3846       178568 :                   stack.safe_push (gimple_assign_rhs1 (def));
    3847       178568 :                   continue;
    3848              :                 }
    3849       177484 :               if (shared_def_out && !*shared_def_out)
    3850       162404 :                 *shared_def_out = def;
    3851              :             }
    3852              :         }
    3853      7094284 :       leaves.safe_push (t);
    3854      7094284 :       if (leaves.length () > LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS)
    3855          795 :         return false;
    3856              :     }
    3857      6918816 :   return !leaves.is_empty ();
    3858      3460203 : }
    3859              : 
    3860              : /* If EXPR is defined by LSHIFT_EXPR with a uhwi-valued amount, return
    3861              :    the shifted input via *INNER_OUT and the amount via *SHIFT_OUT.  */
    3862              : 
    3863              : static bool
    3864      7021606 : long_mul_is_lshift_def (tree expr, tree *inner_out,
    3865              :                         unsigned HOST_WIDE_INT *shift_out)
    3866              : {
    3867      7021606 :   if (TREE_CODE (expr) != SSA_NAME)
    3868              :     return false;
    3869      4543594 :   auto *def = safe_dyn_cast<gassign *> (SSA_NAME_DEF_STMT (expr));
    3870      3958109 :   if (!def || gimple_assign_rhs_code (def) != LSHIFT_EXPR)
    3871              :     return false;
    3872        85797 :   tree amount = gimple_assign_rhs2 (def);
    3873        85797 :   if (!tree_fits_uhwi_p (amount))
    3874              :     return false;
    3875        59431 :   *inner_out = gimple_assign_rhs1 (def);
    3876        59431 :   *shift_out = tree_to_uhwi (amount);
    3877        59431 :   return true;
    3878              : }
    3879              : 
    3880              : /* Fill THIS's kind plus the captures from RES_OPS that the kind requires.
    3881              :    The kind itself determines how many (op0, op1) and hilo captures to
    3882              :    pick up from RES_OPS, and whether a baked-in shift is present.  */
    3883              : 
    3884              : void
    3885        55753 : long_mul_summand::set (long_mul_kind kind, const tree *res_ops)
    3886              : {
    3887        55753 :   this->kind = kind;
    3888        55753 :   unsigned n_ops = 0;
    3889        55753 :   unsigned n_hilos = 0;
    3890        55753 :   int shift_idx = -1;
    3891        53284 :   switch (kind)
    3892              :     {
    3893            0 :     default:
    3894            0 :       gcc_unreachable ();
    3895              :     case LMK_MUL_HIHI:
    3896              :     case LMK_MUL_LOLO:
    3897              :     case LMK_MUL_HILO:
    3898              :       n_ops = 2;
    3899              :       break;
    3900         5797 :     case LMK_CROSS_SUM:
    3901         5797 :       n_hilos = 2;
    3902         5797 :       break;
    3903        18624 :     case LMK_LOW_ACCUM:
    3904        18624 :     case LMK_LOW_SUM:
    3905        18624 :     case LMK_LADDER_SUM1:
    3906        18624 :     case LMK_LADDER_SUM2:
    3907        18624 :     case LMK_LADDER_SUM3:
    3908        18624 :       n_ops = 2;
    3909        18624 :       n_hilos = 2;
    3910        18624 :       break;
    3911          148 :     case LMK_LADDER_PART_SUM:
    3912          148 :       n_ops = 2;
    3913          148 :       n_hilos = 1;
    3914          148 :       break;
    3915              :     case LMK_CARRY_CROSS_SUM:
    3916              :       n_hilos = 3;
    3917              :       break;
    3918          106 :     case LMK_CARRY_LOW_SUM:
    3919          106 :       n_ops = 2;
    3920          106 :       n_hilos = 3;
    3921          106 :       shift_idx = 5;
    3922          106 :       break;
    3923         2469 :     case LMK_CARRY_LOW:
    3924         2469 :       this->carry_a = res_ops[0];
    3925         2469 :       this->carry_b = res_ops[1];
    3926            0 :       return;
    3927              :     }
    3928        24675 :   if (n_ops >= 1)
    3929        47307 :     this->op0 = res_ops[0];
    3930        47307 :   if (n_ops >= 2)
    3931        47307 :     this->op1 = res_ops[1];
    3932        53284 :   gcc_checking_assert (n_hilos <= 3);
    3933       103132 :   for (unsigned i = 0; i < n_hilos; i++)
    3934        49848 :     this->hilo[i] = res_ops[n_ops + i];
    3935        53284 :   if (shift_idx >= 0)
    3936              :     /* The carry atoms (mul_carry_cross_sum, mul_carry_low_sum) capture the
    3937              :        shift since it is always less than TYPE_PRECISION, using tree_to_uhwi is safe.  */
    3938          106 :     this->shift = tree_to_uhwi (res_ops[shift_idx]);
    3939              : }
    3940              : 
    3941              : /* Classify LEAF as a carry-kind summand.  The lshift amount is baked
    3942              :    into mul_carry_cross_sum / mul_carry_low_sum, so they're tried before
    3943              :    any branch that looks for a generic (X >> N) or (X << N) wrapper.  */
    3944              : 
    3945              : bool
    3946      7081751 : long_mul_summand::classify_carry (tree leaf)
    3947              : {
    3948      7081751 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    3949              :   /* mul_carry_low_sum's inner is constrained to mul_low_sum (cross_sum
    3950              :      + mul_hi(mul_lolo)); mul_carry_cross_sum's inner is just
    3951              :      mul_cross_sum (any plus); mul_carry_low matches gt:c (@0, plus(@0,
    3952              :      @1)) without a baked-in shift.  Most specific first, so the
    3953              :      less-constrained pattern doesn't shadow the more-constrained one.  */
    3954      7081751 :   if (gimple_mul_carry_low_sum (leaf, res_ops, NULL))
    3955              :     {
    3956          106 :       set (LMK_CARRY_LOW_SUM, res_ops);
    3957          106 :       return true;
    3958              :     }
    3959      7081645 :   if (gimple_mul_carry_cross_sum (leaf, res_ops, NULL))
    3960              :     {
    3961          180 :       set (LMK_CARRY_CROSS_SUM, res_ops);
    3962          180 :       return true;
    3963              :     }
    3964      7081465 :   if (gimple_mul_carry_low (leaf, res_ops, NULL))
    3965              :     {
    3966         2469 :       set (LMK_CARRY_LOW, res_ops);
    3967         2469 :       return true;
    3968              :     }
    3969              :   return false;
    3970              : }
    3971              : 
    3972              : /* Plus-based summand kinds shared by the (X >> SHIFT) and (X << SHIFT)
    3973              :    classifiers.  Order is by specificity: mul_low_sum's first arm is any
    3974              :    plus, so mul_ladder_sum1/3 (which constrain that arm to a plus
    3975              :    containing a mul_lo) and mul_low_accum (which constrains both arms)
    3976              :    shadow it and must come first.  */
    3977              : 
    3978              : bool
    3979        85823 : long_mul_summand::classify_plus_kinds (tree inner)
    3980              : {
    3981        85823 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    3982        85823 :   if (gimple_mul_low_accum (inner, res_ops, NULL))
    3983              :     {
    3984           28 :       set (LMK_LOW_ACCUM, res_ops);
    3985           28 :       return true;
    3986              :     }
    3987        85795 :   if (gimple_mul_ladder_sum3 (inner, res_ops, NULL))
    3988              :     {
    3989           15 :       set (LMK_LADDER_SUM3, res_ops);
    3990           15 :       return true;
    3991              :     }
    3992        85780 :   if (gimple_mul_ladder_sum1 (inner, res_ops, NULL))
    3993              :     {
    3994        18248 :       set (LMK_LADDER_SUM1, res_ops);
    3995        18248 :       return true;
    3996              :     }
    3997        67532 :   if (gimple_mul_low_sum (inner, res_ops, NULL))
    3998              :     {
    3999          230 :       set (LMK_LOW_SUM, res_ops);
    4000          230 :       return true;
    4001              :     }
    4002        67302 :   if (gimple_mul_ladder_sum2 (inner, res_ops, NULL))
    4003              :     {
    4004          103 :       set (LMK_LADDER_SUM2, res_ops);
    4005          103 :       return true;
    4006              :     }
    4007              :   return false;
    4008              : }
    4009              : 
    4010              : /* Classify INNER -- already unwrapped from an outer (X >> SHIFT) -- as
    4011              :    a high-half-extracted summand.  mul_hilo (mult-shape) is orthogonal
    4012              :    to the plus-based kinds and is tried first; ladder_part_sum (one arm
    4013              :    unconstrained) and mul_cross_sum (any plus) are the fallbacks after
    4014              :    the shared plus-based ladder.  */
    4015              : 
    4016              : bool
    4017        35667 : long_mul_summand::classify_hi_extract (tree inner, unsigned HOST_WIDE_INT shift)
    4018              : {
    4019        35667 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4020        35667 :   this->extract = LMX_HI;
    4021        35667 :   this->shift = shift;
    4022        35667 :   if (gimple_mul_hilo (inner, res_ops, NULL))
    4023              :     {
    4024         9258 :       set (LMK_MUL_HILO, res_ops);
    4025         9258 :       return true;
    4026              :     }
    4027        26409 :   if (classify_plus_kinds (inner))
    4028              :     return true;
    4029        16974 :   if (gimple_mul_ladder_part_sum (inner, res_ops, NULL))
    4030              :     {
    4031          148 :       set (LMK_LADDER_PART_SUM, res_ops);
    4032          148 :       return true;
    4033              :     }
    4034        16826 :   if (gimple_mul_cross_sum (inner, res_ops, NULL))
    4035              :     {
    4036         2855 :       set (LMK_CROSS_SUM, res_ops);
    4037         2855 :       return true;
    4038              :     }
    4039              :   return false;
    4040              : }
    4041              : 
    4042              : /* Classify INNER -- already unwrapped from an outer (X & MASK) -- as
    4043              :    a low-half-masked summand.  */
    4044              : 
    4045              : bool
    4046        21743 : long_mul_summand::classify_lo_extract (tree inner)
    4047              : {
    4048        21743 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4049        21743 :   this->extract = LMX_LO;
    4050        21743 :   if (gimple_mul_lolo (inner, res_ops, NULL))
    4051              :     {
    4052         9349 :       set (LMK_MUL_LOLO, res_ops);
    4053         9349 :       return true;
    4054              :     }
    4055              :   return false;
    4056              : }
    4057              : 
    4058              : /* Classify INNER -- already unwrapped from an outer (X << SHIFT) -- as
    4059              :    a left-shifted summand.  No mul_hilo / ladder_part_sum here -- those
    4060              :    shapes appear only under (X >> SHIFT).  */
    4061              : 
    4062              : bool
    4063        59414 : long_mul_summand::classify_shl_extract (tree inner,
    4064              :                                         unsigned HOST_WIDE_INT shift)
    4065              : {
    4066        59414 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4067        59414 :   this->extract = LMX_SHL_N;
    4068        59414 :   this->shift = shift;
    4069        59414 :   if (classify_plus_kinds (inner))
    4070              :     return true;
    4071        50225 :   if (gimple_mul_cross_sum (inner, res_ops, NULL))
    4072              :     {
    4073         2942 :       set (LMK_CROSS_SUM, res_ops);
    4074         2942 :       return true;
    4075              :     }
    4076              :   return false;
    4077              : }
    4078              : 
    4079              : /* Classify LEAF as one of the bare-kind summands (no extraction
    4080              :    wrapper): mul_hihi or mul_lolo standing on their own.  */
    4081              : 
    4082              : bool
    4083      6962172 : long_mul_summand::classify_bare (tree leaf)
    4084              : {
    4085      6962172 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4086      6962172 :   if (gimple_mul_hihi (leaf, res_ops, NULL))
    4087              :     {
    4088         9750 :       set (LMK_MUL_HIHI, res_ops);
    4089         9750 :       return true;
    4090              :     }
    4091      6952422 :   if (gimple_mul_lolo (leaf, res_ops, NULL))
    4092              :     {
    4093           72 :       set (LMK_MUL_LOLO, res_ops);
    4094           72 :       return true;
    4095              :     }
    4096              :   return false;
    4097              : }
    4098              : 
    4099              : /* Classify LEAF as one of the long-multiply summand shapes.  On success,
    4100              :    fill *INFO with the kind, extract, captured operands and shift.
    4101              :    Dispatches to per-extract helpers; the order matters because the
    4102              :    carry kinds bake an lshift into the pattern and would otherwise be
    4103              :    misread by the (X << N) branch.  */
    4104              : 
    4105      7081751 : long_mul_summand::long_mul_summand (tree leaf)
    4106      7081751 :  : long_mul_summand()
    4107              : {
    4108      7081751 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4109              : 
    4110      7081751 :   if (classify_carry (leaf))
    4111       119579 :     return;
    4112              : 
    4113      7078996 :   if (gimple_mul_hi (leaf, res_ops, NULL))
    4114              :     {
    4115        35667 :       classify_hi_extract (res_ops[0], tree_to_uhwi (res_ops[1]));
    4116        35667 :       return;
    4117              :     }
    4118              : 
    4119      7043329 :   if (gimple_mul_lo (leaf, res_ops, NULL))
    4120              :     {
    4121        21743 :       classify_lo_extract (res_ops[0]);
    4122        21743 :       return;
    4123              :     }
    4124              : 
    4125      7021586 :   tree inner;
    4126      7021586 :   unsigned HOST_WIDE_INT shift;
    4127      7021586 :   if (long_mul_is_lshift_def (leaf, &inner, &shift))
    4128              :     {
    4129        59414 :       classify_shl_extract (inner, shift);
    4130        59414 :       return;
    4131              :     }
    4132              : 
    4133      6962172 :   classify_bare (leaf);
    4134              : }
    4135              : 
    4136              : /* qsort comparator: sort summands by (kind, extract) to put a multiset
    4137              :    into canonical order for table lookup.  Unstable sort within a tie is
    4138              :    harmless: no row in long_mul_table pairs distinct subterms under the
    4139              :    same (kind, extract), and long_mul_check_consistency cross-validates
    4140              :    that matching summands share one canonical (op0, op1).  */
    4141              : 
    4142              : static int
    4143       140372 : long_mul_summand_compare (const void *a, const void *b)
    4144              : {
    4145       140372 :   const long_mul_summand *sa = (const long_mul_summand *) a;
    4146       140372 :   const long_mul_summand *sb = (const long_mul_summand *) b;
    4147       140372 :   gcc_checking_assert (sa->kind != LMK_INVALID);
    4148       140372 :   gcc_checking_assert (sb->kind != LMK_INVALID);
    4149       140372 :   if (sa->kind != sb->kind)
    4150       139509 :     return (int) sa->kind - (int) sb->kind;
    4151          863 :   return (int) sa->extract - (int) sb->extract;
    4152              : }
    4153              : 
    4154              : /* One row of the long-multiply variant table.  COUNT is how many entries
    4155              :    of SIG carry the row's signature (2 to LONG_MUL_MAX_SUMMANDS); a row
    4156              :    with fewer summands leaves the remaining SIG entries zero-initialized.
    4157              :    Those zeros are not a terminator -- {LMK_MUL_HIHI, LMX_NONE} is itself a
    4158              :    valid signature -- so long_mul_signature_matches is bounded by COUNT,
    4159              :    never by a sentinel entry.  */
    4160              : 
    4161              : struct long_mul_row {
    4162              :   enum long_mul_row_part { HIGH_PART, LOW_PART } part;
    4163              :   tree_code outer;
    4164              :   unsigned char count;
    4165              :   struct {
    4166              :     long_mul_kind kind;
    4167              :     long_mul_extract extract;
    4168              :   } sig[LONG_MUL_MAX_SUMMANDS];
    4169              :   bool (*extra_check) (const vec<long_mul_summand> &, gimple *);
    4170              : };
    4171              : 
    4172              : /* True if (A, B) is the same pair as (OP0, OP1) in either order.  */
    4173              : 
    4174              : static inline bool
    4175        23409 : long_mul_same_ops (tree a, tree b, tree op0, tree op1)
    4176              : {
    4177        32838 :   return (a == op0 && b == op1) || (a == op1 && b == op0);
    4178              : }
    4179              : 
    4180              : /* True if H is a cross-half product of (OP0, OP1) -- gimple_mul_hilo
    4181              :    recognizes it and its captured operands match the pair.  */
    4182              : 
    4183              : static bool
    4184         4526 : long_mul_is_cross_half (tree h, tree op0, tree op1)
    4185              : {
    4186         4526 :   tree scratch[LONG_MUL_MAX_CAPTURES];
    4187         4526 :   return gimple_mul_hilo (h, scratch, NULL)
    4188         4526 :          && long_mul_same_ops (scratch[0], scratch[1], op0, op1);
    4189              : }
    4190              : 
    4191              : /* Orientation of the mul_hilo capture H relative to (OP0, OP1):
    4192              :    returns 0 for high(OP0)*low(OP1), 1 for high(OP1)*low(OP0), or -1
    4193              :    if H does not decompose that way.  A cross-sum of two mul_hilos must
    4194              :    see one of each orientation -- otherwise a doubled factor would fold
    4195              :    to the wrong value.  (In a self-multiply the two orientations
    4196              :    coincide; see the OP0 == OP1 bypass in long_mul_check_consistency.)  */
    4197              : 
    4198              : static int
    4199         6123 : long_mul_hilo_orientation (tree h, tree op0, tree op1)
    4200              : {
    4201         6123 :   tree scratch[LONG_MUL_MAX_CAPTURES];
    4202         6123 :   if (!gimple_mul_hilo (h, scratch, NULL))
    4203              :     return -1;
    4204         6123 :   if (scratch[0] == op0 && scratch[1] == op1)
    4205              :     return 0;
    4206         2154 :   if (scratch[0] == op1 && scratch[1] == op0)
    4207         2154 :     return 1;
    4208              :   return -1;
    4209              : }
    4210              : 
    4211              : /* Find the first summand that carries operand captures, and return its
    4212              :    (op0, op1) pair in *OP0_OUT / *OP1_OUT.  Returns false if no summand
    4213              :    provides them.  */
    4214              : 
    4215              : static bool
    4216         8155 : long_mul_canonical_ops (const vec<long_mul_summand> &summands,
    4217              :                         tree *op0_out, tree *op1_out)
    4218              : {
    4219        24465 :   for (const long_mul_summand &s : summands)
    4220         8155 :     if (s.op0)
    4221              :       {
    4222         8155 :         *op0_out = s.op0;
    4223         8155 :         *op1_out = s.op1;
    4224         8155 :         return true;
    4225              :       }
    4226              :   return false;
    4227              : }
    4228              : 
    4229              : /* Return the first summand in SUMMANDS whose kind matches KIND, or NULL.  */
    4230              : 
    4231              : static const long_mul_summand *
    4232         3743 : long_mul_find_summand (const vec<long_mul_summand> &summands,
    4233              :                        long_mul_kind kind)
    4234              : {
    4235         3743 :   gcc_checking_assert (kind != LMK_INVALID);
    4236        16852 :   for (const long_mul_summand &s : summands)
    4237         9366 :     if (s.kind == kind)
    4238              :       return &s;
    4239              :   return NULL;
    4240              : }
    4241              : 
    4242              : /* Run the cross-summand validation invariants and return the canonical
    4243              :    (op0, op1).  Returns false unless all summands that carry operands use
    4244              :    the same (op0, op1) pair (in either order), every LMX_HI/LMX_SHL_N shift
    4245              :    equals halfwidth, every captured hilo is a true cross-half product of
    4246              :    (op0, op1), and every cross-half pair (both those inside a single
    4247              :    mul_cross_sum-bearing summand and those spread across separate
    4248              :    LMK_MUL_HILO summands) contains one of each orientation.  */
    4249              : 
    4250              : static bool
    4251         8138 : long_mul_check_consistency (const vec<long_mul_summand> &summands,
    4252              :                    tree *op0_out, tree *op1_out)
    4253              : {
    4254         8138 :   tree op0, op1;
    4255         8138 :   if (!long_mul_canonical_ops (summands, &op0, &op1))
    4256              :     return false;
    4257              : 
    4258         8138 :   tree op_type = TREE_TYPE (op0);
    4259         8138 :   if (!INTEGRAL_TYPE_P (op_type)
    4260         8138 :       || TYPE_PRECISION (op_type) % 2 != 0)
    4261              :     return false;
    4262         8138 :   unsigned int halfwidth = TYPE_PRECISION (op_type) / 2;
    4263              : 
    4264              :   /* Self-multiply (x*x) collapses the two cross-halves onto one value,
    4265              :      so the complementarity constraint is a trivial no-op there.  */
    4266         8138 :   bool need_orient = op0 != op1;
    4267         8138 :   int mul_hilo_orient[2] = { 0, 0 };
    4268              : 
    4269        37329 :   for (const long_mul_summand &s : summands)
    4270              :     {
    4271        19019 :       if (s.op0 && !long_mul_same_ops (s.op0, s.op1, op0, op1))
    4272              :         return false;
    4273        12941 :       if ((s.extract == LMX_HI || s.extract == LMX_SHL_N)
    4274         4704 :           && s.shift != halfwidth)
    4275              :         return false;
    4276        51710 :       for (tree h : s.hilo)
    4277        38795 :         if (h && !long_mul_is_cross_half (h, op0, op1))
    4278              :           return false;
    4279              : 
    4280        12915 :       if (!need_orient)
    4281            8 :         continue;
    4282              : 
    4283              :       /* The two cross-sum operands are the last two non-null hilos:
    4284              :          (hilo[1], hilo[2]) for the CARRY_*_SUM kinds, (hilo[0], hilo[1]) for
    4285              :          the CROSS_SUM / SUM / ACCUM / LADDER_SUM kinds, none for the
    4286              :          rest.  */
    4287        12907 :       tree a = NULL_TREE;
    4288        12907 :       tree b = NULL_TREE;
    4289        12907 :       if (s.hilo[2])
    4290              :         {
    4291           82 :           a = s.hilo[1];
    4292           82 :           b = s.hilo[2];
    4293              :         }
    4294        12825 :       else if (s.hilo[1])
    4295              :         {
    4296         2048 :           a = s.hilo[0];
    4297         2048 :           b = s.hilo[1];
    4298              :         }
    4299        12907 :       if (a && b
    4300        15037 :           && (long_mul_hilo_orientation (a, op0, op1)
    4301         2130 :               == long_mul_hilo_orientation (b, op0, op1)))
    4302              :         return false;
    4303              : 
    4304              :       /* Two LMK_MUL_HILO summands (the two-hilos ladder form) stand for
    4305              :          the two cross-halves separately; count orientations and require
    4306              :          the pair to be complementary.  s.op0/op1 is already validated to
    4307              :          match (op0, op1) in some order above.  */
    4308        12907 :       if (s.kind == LMK_MUL_HILO && s.op0)
    4309         2532 :         mul_hilo_orient[s.op0 == op1]++;
    4310              :     }
    4311              : 
    4312         2034 :   if (mul_hilo_orient[0] + mul_hilo_orient[1] >= 2
    4313           10 :       && (mul_hilo_orient[0] == 0 || mul_hilo_orient[1] == 0))
    4314              :     return false;
    4315              : 
    4316         2034 :   *op0_out = op0;
    4317         2034 :   *op1_out = op1;
    4318         2034 :   return true;
    4319              : }
    4320              : 
    4321              : /* Compare the (already-sorted) SUMMANDS multiset against ROW.sig.  */
    4322              : 
    4323              : static bool
    4324       125014 : long_mul_signature_matches (const vec<long_mul_summand> &summands,
    4325              :                    const long_mul_row &row)
    4326              : {
    4327       250028 :   if (row.count != summands.length ())
    4328              :     return false;
    4329        75886 :   for (unsigned i = 0; i < row.count; i++)
    4330        67748 :     if (summands[i].kind != row.sig[i].kind
    4331        67748 :         || summands[i].extract != row.sig[i].extract)
    4332              :       return false;
    4333              :   return true;
    4334              : }
    4335              : 
    4336              : /* Extra check for the two-carries high-part row: the LMK_CARRY_LOW summand's
    4337              :    two operands (carry_a, carry_b) must be a (cross_shifted, mul_lolo) pair
    4338              :    consistent with the multiset's canonical (op0, op1).  */
    4339              : 
    4340              : static bool
    4341           17 : long_mul_check_two_carries (const vec<long_mul_summand> &summands,
    4342              :                             gimple *)
    4343              : {
    4344           17 :   tree op0, op1;
    4345           17 :   if (!long_mul_canonical_ops (summands, &op0, &op1))
    4346              :     return false;
    4347           17 :   unsigned int halfwidth = TYPE_PRECISION (TREE_TYPE (op0)) / 2;
    4348              : 
    4349           17 :   const long_mul_summand *cl = long_mul_find_summand (summands, LMK_CARRY_LOW);
    4350           17 :   if (!cl)
    4351              :     return false;
    4352              : 
    4353              :   /* The two carry_low operands must be (cross_shifted, mul_lolo) in either
    4354              :      order.  cross_shifted = LSHIFT_EXPR (mul_cross_sum, halfwidth).  */
    4355           17 :   tree cs = cl->carry_a, lolo = cl->carry_b;
    4356           17 :   tree inner;
    4357           17 :   unsigned HOST_WIDE_INT shift;
    4358           17 :   if (!long_mul_is_lshift_def (cs, &inner, &shift))
    4359              :     {
    4360            3 :       std::swap (cs, lolo);
    4361            3 :       if (!long_mul_is_lshift_def (cs, &inner, &shift))
    4362              :         return false;
    4363              :     }
    4364           17 :   if (shift != halfwidth)
    4365              :     return false;
    4366              : 
    4367           17 :   tree scratch[LONG_MUL_MAX_CAPTURES];
    4368           17 :   if (!gimple_mul_cross_sum (inner, scratch, NULL))
    4369              :     return false;
    4370           51 :   for (int i = 0; i < 2; i++)
    4371           34 :     if (!long_mul_is_cross_half (scratch[i], op0, op1))
    4372              :       return false;
    4373           17 :   if (!gimple_mul_lolo (lolo, scratch, NULL)
    4374           17 :       || !long_mul_same_ops (scratch[0], scratch[1], op0, op1))
    4375              :     return false;
    4376              : 
    4377              :   return true;
    4378              : }
    4379              : 
    4380              : /* Extra check for xh*yh + (hilo >> N) + (ladder_sum1 >> N).  hilo must be
    4381              :    the product that ladder_sum1 masks, since the high half of the unmasked
    4382              :    one is already in the sum.  */
    4383              : 
    4384              : static bool
    4385         1769 : long_mul_check_ladder_sum1 (const vec<long_mul_summand> &summands,
    4386              :                             gimple *)
    4387              : {
    4388         1769 :   const long_mul_summand *hilo
    4389         1769 :     = long_mul_find_summand (summands, LMK_MUL_HILO);
    4390         1769 :   const long_mul_summand *sum
    4391         1769 :     = long_mul_find_summand (summands, LMK_LADDER_SUM1);
    4392         1769 :   return long_mul_hilo_orientation (sum->hilo[0], hilo->op0, hilo->op1) == 0;
    4393              : }
    4394              : 
    4395              : /* Likewise for xh*yh + (ladder_sum2 >> N) + (ladder_part_sum >> N):
    4396              :    ladder_part_sum must hold the product that ladder_sum2 masks.  */
    4397              : 
    4398              : static bool
    4399           94 : long_mul_check_ladder_sum2 (const vec<long_mul_summand> &summands,
    4400              :                             gimple *)
    4401              : {
    4402           94 :   const long_mul_summand *part
    4403           94 :     = long_mul_find_summand (summands, LMK_LADDER_PART_SUM);
    4404           94 :   const long_mul_summand *sum
    4405           94 :     = long_mul_find_summand (summands, LMK_LADDER_SUM2);
    4406           94 :   tree masked[LONG_MUL_MAX_CAPTURES];
    4407           94 :   return (gimple_mul_hilo (sum->hilo[0], masked, NULL)
    4408           94 :           && (long_mul_hilo_orientation (part->hilo[0], masked[0], masked[1])
    4409           94 :               == 0));
    4410              : }
    4411              : 
    4412              : /* The lolo + cross_shifted shape is also the low half of a two-carry
    4413              :    long-multiply, where an unsigned overflow compare against one of
    4414              :    the PLUS operands is the low-carry term consumed by the matching
    4415              :    high-part fold.  Folding to mul_lo here destroys cross_shifted,
    4416              :    which both the compare and the high-part match still need; defer
    4417              :    so the high-part fold runs first.  After it does, the compare is
    4418              :    dead and the surviving lolo + cross_shifted is picked up by this
    4419              :    row in the next forwprop instance.  Returns false to defer.  */
    4420              : 
    4421              : static bool
    4422           60 : long_mul_check_low_plus_defer (const vec<long_mul_summand> &, gimple *stmt)
    4423              : {
    4424              :   /* The PHI entry passes its gphi as the candidate but commits only to
    4425              :      HIGH_PART rows, so a LOW_PART row never folds from there.  Guard the
    4426              :      gimple_assign accessors regardless, so this stays correct if a future
    4427              :      PLUS-shaped row reachable from the PHI path uses it.  */
    4428           60 :   gassign *assign = dyn_cast<gassign *> (stmt);
    4429           60 :   if (!assign)
    4430              :     return false;
    4431              : 
    4432           60 :   tree lhs = gimple_assign_lhs (assign);
    4433           60 :   tree rhs1 = gimple_assign_rhs1 (assign);
    4434           60 :   tree rhs2 = gimple_assign_rhs2 (assign);
    4435              : 
    4436           60 :   imm_use_iterator iter;
    4437           60 :   gimple *use_stmt;
    4438           75 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    4439              :     {
    4440           66 :       tree cmp_op1 = NULL_TREE, cmp_op2 = NULL_TREE;
    4441           66 :       enum tree_code use_code = ERROR_MARK;
    4442           66 :       if (auto assign = dyn_cast<gassign *> (use_stmt))
    4443              :         {
    4444           48 :           use_code = gimple_assign_rhs_code (assign);
    4445           48 :           cmp_op1 = gimple_assign_rhs1 (assign);
    4446           48 :           cmp_op2 = gimple_assign_rhs2 (assign);
    4447              :         }
    4448           18 :       else if (gcond *cond = dyn_cast<gcond *> (use_stmt))
    4449              :         {
    4450           12 :           use_code = gimple_cond_code (cond);
    4451           12 :           cmp_op1 = gimple_cond_lhs (cond);
    4452           12 :           cmp_op2 = gimple_cond_rhs (cond);
    4453              :         }
    4454           66 :       if (use_code == GT_EXPR || use_code == LT_EXPR
    4455           15 :           || use_code == GE_EXPR || use_code == LE_EXPR)
    4456              :         {
    4457           51 :           tree other = (cmp_op1 == lhs) ? cmp_op2
    4458           32 :                      : (cmp_op2 == lhs) ? cmp_op1 : NULL_TREE;
    4459           51 :           if (other && (other == rhs1 || other == rhs2))
    4460           51 :             return false;
    4461              :         }
    4462           51 :     }
    4463            9 :   return true;
    4464              : }
    4465              : 
    4466              : /* Long-multiply variant table.  Each row enumerates the multiset of
    4467              :    (kind, extract) summands that compose one long-multiply form.  Rows
    4468              :    are sorted by long_mul_summand_compare, matching the input summands'
    4469              :    sort order, so a plain element-wise compare suffices.  Rows describe
    4470              :    unsigned schoolbook expansions on an even-width 2N-bit type split at
    4471              :    half-width N; EXTRA_CHECK carries invariants the (kind, extract)
    4472              :    signature cannot express.
    4473              : 
    4474              :    The formula on each row uses xh, xl, yh, yl for the half-width pieces
    4475              :    of x and y, cross_sum for xh*yl + xl*yh, and hilo for either cross-half
    4476              :    product (consumers validate the operand shape).  */
    4477              : 
    4478              : static const long_mul_row long_mul_table[] = {
    4479              :   /* HIGH-PART folds.  */
    4480              :   /* xh*yh + (low_sum >> N) + ((hilo > low_sum) << N),
    4481              :      low_sum = cross_sum + (xl*yl >> N).  */
    4482              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
    4483              :     { { LMK_MUL_HIHI, LMX_NONE },
    4484              :       { LMK_LOW_SUM, LMX_HI },
    4485              :       { LMK_CARRY_LOW_SUM, LMX_NONE } },
    4486              :     NULL },
    4487              :   /* xh*yh + (low_accum >> N) + (cross_sum >> N) + ((hilo > cross_sum) << N),
    4488              :      low_accum = (xl*yl >> N) + (cross_sum & mask).  */
    4489              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
    4490              :     { { LMK_MUL_HIHI, LMX_NONE },
    4491              :       { LMK_CROSS_SUM, LMX_HI },
    4492              :       { LMK_LOW_ACCUM, LMX_HI },
    4493              :       { LMK_CARRY_CROSS_SUM, LMX_NONE } },
    4494              :     NULL },
    4495              :   /* xh*yh + (cross_sum >> N) + carry_low + ((hilo > cross_sum) << N),
    4496              :      carry_low = (xl*yl + (cross_sum << N)) < (cross_sum << N).  */
    4497              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
    4498              :     { { LMK_MUL_HIHI, LMX_NONE },
    4499              :       { LMK_CROSS_SUM, LMX_HI },
    4500              :       { LMK_CARRY_LOW, LMX_NONE },
    4501              :       { LMK_CARRY_CROSS_SUM, LMX_NONE } },
    4502              :     long_mul_check_two_carries },
    4503              :   /* xh*yh + (hilo >> N) + (ladder_sum1 >> N),
    4504              :      ladder_sum1 = (hilo & mask) + hilo' + (xl*yl >> N),
    4505              :      hilo, hilo' the two cross-half products.  */
    4506              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
    4507              :     { { LMK_MUL_HIHI, LMX_NONE },
    4508              :       { LMK_MUL_HILO, LMX_HI },
    4509              :       { LMK_LADDER_SUM1, LMX_HI } },
    4510              :     long_mul_check_ladder_sum1 },
    4511              :   /* xh*yh + (ladder_sum2 >> N) + (ladder_part_sum >> N),
    4512              :      ladder_part_sum = (xl*yl >> N) + hilo,
    4513              :      ladder_sum2 = (ladder_part_sum & mask) + hilo'.  */
    4514              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
    4515              :     { { LMK_MUL_HIHI, LMX_NONE },
    4516              :       { LMK_LADDER_SUM2, LMX_HI },
    4517              :       { LMK_LADDER_PART_SUM, LMX_HI } },
    4518              :     long_mul_check_ladder_sum2 },
    4519              :   /* xh*yh + (hilo >> N) + (hilo' >> N) + (ladder_sum3 >> N),
    4520              :      ladder_sum3 = (hilo & mask) + (hilo' & mask) + (xl*yl >> N).  */
    4521              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
    4522              :     { { LMK_MUL_HIHI, LMX_NONE },
    4523              :       { LMK_MUL_HILO, LMX_HI },
    4524              :       { LMK_MUL_HILO, LMX_HI },
    4525              :       { LMK_LADDER_SUM3, LMX_HI } },
    4526              :     NULL },
    4527              :   /* LOW-PART folds.  Recover the lower 2N bits from xl*yl plus a
    4528              :      shifted cross-half term.  */
    4529              :   /* xl*yl + (cross_sum << N).  */
    4530              :   { long_mul_row::LOW_PART, PLUS_EXPR, 2,
    4531              :     { { LMK_MUL_LOLO, LMX_NONE },
    4532              :       { LMK_CROSS_SUM, LMX_SHL_N } },
    4533              :     long_mul_check_low_plus_defer },
    4534              :   /* (xl*yl & mask) | (low_accum << N),
    4535              :      low_accum = (xl*yl >> N) + (cross_sum & mask).  */
    4536              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4537              :     { { LMK_MUL_LOLO, LMX_LO },
    4538              :       { LMK_LOW_ACCUM, LMX_SHL_N } },
    4539              :     NULL },
    4540              :   /* (xl*yl & mask) | (low_sum << N),
    4541              :      low_sum = cross_sum + (xl*yl >> N).  */
    4542              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4543              :     { { LMK_MUL_LOLO, LMX_LO },
    4544              :       { LMK_LOW_SUM, LMX_SHL_N } },
    4545              :     NULL },
    4546              :   /* (xl*yl & mask) | (ladder_sum1 << N),
    4547              :      ladder_sum1 as in the high ladder row above.  */
    4548              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4549              :     { { LMK_MUL_LOLO, LMX_LO },
    4550              :       { LMK_LADDER_SUM1, LMX_SHL_N } },
    4551              :     NULL },
    4552              :   /* (xl*yl & mask) | (ladder_sum2 << N),
    4553              :      ladder_sum2 as in the high ladder row above.  */
    4554              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4555              :     { { LMK_MUL_LOLO, LMX_LO },
    4556              :       { LMK_LADDER_SUM2, LMX_SHL_N } },
    4557              :     NULL },
    4558              :   /* (xl*yl & mask) | (ladder_sum3 << N),
    4559              :      ladder_sum3 as in the high ladder-long row above.  */
    4560              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4561              :     { { LMK_MUL_LOLO, LMX_LO },
    4562              :       { LMK_LADDER_SUM3, LMX_SHL_N } },
    4563              :     NULL },
    4564              : };
    4565              : 
    4566              : /* If a multi-used inner addition (sharing the chain's outer code) blocked
    4567              :    linearization of a long-mul candidate, emit a dump-file hint pointing
    4568              :    at it.  */
    4569              : 
    4570              : static void
    4571        21446 : long_mul_hint_shared_intermediate (gimple *shared_def)
    4572              : {
    4573        21446 :   if (!shared_def || !dump_file || !(dump_flags & TDF_DETAILS))
    4574              :     return;
    4575            0 :   fprintf (dump_file, "long-mul fold rejected: shared intermediate at ");
    4576            0 :   print_gimple_stmt (dump_file, shared_def, 0, TDF_SLIM);
    4577              : }
    4578              : 
    4579              : /* Search long_mul_table for a row whose multiset matches SUMMANDS for
    4580              :    outer kind OUTER on a result of type LHS_TYPE.  CANDIDATE_STMT is
    4581              :    passed to per-row extra_check predicates.  On a hit, returns the
    4582              :    matching row and writes the half-width operands via OUT_OP0/OUT_OP1.
    4583              :    No IR mutation.  */
    4584              : 
    4585              : static const long_mul_row *
    4586        18810 : long_mul_classify_match (const vec<long_mul_summand> &summands,
    4587              :                          tree lhs_type, tree_code outer,
    4588              :                          gimple *candidate_stmt,
    4589              :                          tree *out_op0, tree *out_op1)
    4590              : {
    4591              :   /* HIGH_PART rows emit a 2N-bit multiply that pass_optimize_widening_mul
    4592              :      consumes -- either via WIDEN_MULT_EXPR / MULT_HIGHPART conversion when
    4593              :      the target has a native 2N multiply, or via lower_long_mul_high_chain
    4594              :      when it does not.  LOW_PART rows emit a plain MULT_EXPR.  Emission
    4595              :      needs a 2N mode to exist in the mode table AND the widening_mul pass
    4596              :      to be active: without the pass, the emit could reach RTL expand as an
    4597              :      unexpandable 2N multiply (e.g. OImode).  BITINT_TYPE is
    4598              :      refused -- the long_mul_high_chain atom excludes it.  */
    4599        18810 :   scalar_int_mode mode, wide_mode;
    4600        18810 :   bool can_emit_high
    4601        18810 :     = optimize_widening_mul_active_p ()
    4602        18719 :       && TREE_CODE (lhs_type) != BITINT_TYPE
    4603        37438 :       && is_a <scalar_int_mode> (TYPE_MODE (lhs_type), &mode)
    4604        37529 :       && GET_MODE_2XWIDER_MODE (mode).exists (&wide_mode);
    4605              : 
    4606       226947 :   for (const long_mul_row &row : long_mul_table)
    4607              :     {
    4608       412054 :       if (row.outer != outer
    4609       125560 :           || (row.part == long_mul_row::HIGH_PART && !can_emit_high)
    4610       335110 :           || !long_mul_signature_matches (summands, row))
    4611       208137 :         continue;
    4612              : 
    4613         8138 :       tree op0, op1;
    4614         8138 :       if (!long_mul_check_consistency (summands, &op0, &op1))
    4615         6104 :         continue;
    4616              : 
    4617              :       /* Do not emit the wide chain when an operand is subject to
    4618              :          abnormal coalescing: the widening_mul-side consumers refuse
    4619              :          such operands (see convert_mult_to_widen), which would leave
    4620              :          the chain without a consumer.  */
    4621         2034 :       if (row.part == long_mul_row::HIGH_PART
    4622         2034 :           && ((TREE_CODE (op0) == SSA_NAME
    4623         1955 :                && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
    4624         1955 :               || (TREE_CODE (op1) == SSA_NAME
    4625         1955 :                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
    4626            0 :         continue;
    4627              : 
    4628         2034 :       if (row.extra_check && !row.extra_check (summands, candidate_stmt))
    4629           75 :         continue;
    4630              : 
    4631         1959 :       *out_op0 = op0;
    4632         1959 :       *out_op1 = op1;
    4633         1959 :       return &row;
    4634              :     }
    4635              :   return NULL;
    4636              : }
    4637              : 
    4638              : /* Walk STMT's outer chain (kind OUTER), classify each leaf as a
    4639              :    long-multiply summand, optionally add the already-classified EXTRA,
    4640              :    and look the multiset up in long_mul_table for a result of type
    4641              :    LHS_TYPE.  CANDIDATE is passed to per-row extra_check predicates.
    4642              : 
    4643              :    If EXTRAS_OUT is non-NULL, leaves matching no summand are set aside
    4644              :    there instead of failing the match, and the caller must re-apply
    4645              :    them on top of the folded multiply.  A leaf that does match is
    4646              :    always consumed: if that makes the signature miss every row the
    4647              :    match fails, rather than retrying with the leaf demoted to an extra
    4648              :    (subset search would be exponential).
    4649              : 
    4650              :    Returns the matched row and the half-width operands via
    4651              :    OUT_OP0/OUT_OP1, or NULL on a miss.  No IR mutation.  */
    4652              : 
    4653              : static const long_mul_row *
    4654      3460203 : long_mul_classify_chain (gassign *stmt, tree_code outer, tree lhs_type,
    4655              :                          gimple *candidate, const long_mul_summand *extra,
    4656              :                          vec<tree> *extras_out,
    4657              :                          tree *out_op0, tree *out_op1)
    4658              : {
    4659      3460203 :   auto_vec<tree, LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS> leaves;
    4660      3460203 :   gimple *shared_def = NULL;
    4661      3460203 :   if (!long_mul_linearize_chain (stmt, outer, leaves, &shared_def))
    4662              :     return NULL;
    4663              : 
    4664      3459408 :   auto_vec<long_mul_summand,
    4665      3459408 :            LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS + 1> summands;
    4666     17455380 :   for (tree leaf : leaves)
    4667              :     {
    4668      7081751 :       if (long_mul_summand s{leaf})
    4669              :         {
    4670        55753 :           gcc_checking_assert (s.kind != LMK_INVALID);
    4671        55753 :           summands.quick_push (s);
    4672              :         }
    4673     14051718 :       else if (extras_out && extras_out->length () < LONG_MUL_MAX_EXTRAS)
    4674      7021403 :         extras_out->safe_push (leaf);
    4675              :       else
    4676              :         {
    4677         4595 :           long_mul_hint_shared_intermediate (shared_def);
    4678         4595 :           return NULL;
    4679              :         }
    4680              :     }
    4681      3454813 :   if (extra)
    4682              :     {
    4683          355 :       gcc_checking_assert (extra->kind != LMK_INVALID);
    4684          355 :       summands.quick_push (*extra);
    4685              :     }
    4686      6914221 :   if (summands.length () < 2
    4687      3454813 :       || summands.length () > LONG_MUL_MAX_SUMMANDS)
    4688              :     return NULL;
    4689        18810 :   summands.qsort (long_mul_summand_compare);
    4690              : 
    4691        18810 :   const long_mul_row *row
    4692        18810 :     = long_mul_classify_match (summands, lhs_type, outer, candidate,
    4693              :                                out_op0, out_op1);
    4694        18810 :   if (!row)
    4695        16851 :     long_mul_hint_shared_intermediate (shared_def);
    4696              :   return row;
    4697      3459408 : }
    4698              : 
    4699              : /* Top-level entry for long-multiply folding.  Walks STMT's outer
    4700              :    addition or BIT_IOR chain, classifies the summands, and dispatches
    4701              :    to create_mul_high_seq / create_mul_low_seq if the multiset matches
    4702              :    a known long-multiply form.  Returns true on success.  */
    4703              : 
    4704              : static bool
    4705     10367115 : match_long_mul (gassign *stmt)
    4706              : {
    4707     10367115 :   tree_code outer = gimple_assign_rhs_code (stmt);
    4708     10367115 :   if (outer != PLUS_EXPR && outer != BIT_IOR_EXPR)
    4709              :     return false;
    4710              : 
    4711              :   /* Skip non-candidate adds (signed, pointer, odd-width) before walking the
    4712              :      chain.  No legitimate long-mul leaf has a type the atoms would reject.
    4713              :      This just avoids the linearize/classify work on every other PLUS/IOR.  */
    4714     10367115 :   tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
    4715     10367115 :   if (!INTEGRAL_TYPE_P (lhs_type)
    4716      9069708 :       || !TYPE_UNSIGNED (lhs_type)
    4717     14234225 :       || TYPE_PRECISION (lhs_type) % 2 != 0)
    4718              :     return false;
    4719              : 
    4720              :   /* Only start at the end of a chain: a consumer with the same code
    4721              :      linearizes through this statement anyway, so starting here is
    4722              :      redundant.  A consumer in another block does not count -- folding
    4723              :      at the later use could sink a loop-invariant multiply into a
    4724              :      loop.  */
    4725      3628597 :   use_operand_p use_p;
    4726      3628597 :   gimple *use_stmt;
    4727      3628597 :   if (single_imm_use (gimple_assign_lhs (stmt), &use_p, &use_stmt)
    4728      2551148 :       && is_gimple_assign (use_stmt)
    4729      1394294 :       && gimple_assign_rhs_code (use_stmt) == outer
    4730      3804457 :       && gimple_bb (use_stmt) == gimple_bb (stmt))
    4731              :     return false;
    4732              : 
    4733      3459570 :   auto_vec<tree, LONG_MUL_MAX_EXTRAS> extras;
    4734      3459570 :   tree op0, op1;
    4735      3459570 :   const long_mul_row *row
    4736      3459570 :     = long_mul_classify_chain (stmt, outer, lhs_type, stmt, NULL, &extras,
    4737              :                                &op0, &op1);
    4738      3459570 :   if (!row)
    4739              :     return false;
    4740              : 
    4741         1944 :   if (row->part == long_mul_row::HIGH_PART)
    4742              :     {
    4743         1916 :       create_mul_high_seq (op0, op1, stmt, extras, outer);
    4744         1916 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4745           39 :         fprintf (dump_file, "Long multiplication high part folded.\n");
    4746              :       return true;
    4747              :     }
    4748           28 :   create_mul_low_seq (op0, op1, stmt, extras, outer);
    4749           28 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4750           11 :     fprintf (dump_file, "Long multiplication low part folded.\n");
    4751              :   return true;
    4752      3459570 : }
    4753              : 
    4754              : /* PHI-driven entry for long-multiply folding.  When PHI's value
    4755              :    flattens to base + (carry << N), probe sum to classify the carry
    4756              :    kind, linearize base for the remaining high-part summands, and run
    4757              :    the long-multiply table.  On a hit, emit a 2N-bit multiply at the
    4758              :    top of the join block with PHI_RES as its LHS and remove the PHI.
    4759              :    Otherwise leave the IR untouched.  Only HIGH_PART rows are
    4760              :    reachable.  LOW_PART rows are BIT_IOR-shaped and never produce a
    4761              :    carry PHI.  */
    4762              : 
    4763              : static bool
    4764      8944578 : match_long_mul_phi (gphi *phi)
    4765              : {
    4766      8944578 :   tree phi_res = gimple_phi_result (phi);
    4767      8944578 :   tree lhs_type = TREE_TYPE (phi_res);
    4768      6357317 :   if (!INTEGRAL_TYPE_P (lhs_type) || !TYPE_UNSIGNED (lhs_type)
    4769     11771597 :       || TYPE_PRECISION (lhs_type) % 2 != 0)
    4770              :     return false;
    4771              : 
    4772      2093375 :   tree cca_ops[4];
    4773      2093375 :   if (!gimple_cond_carry_add (phi_res, cca_ops, NULL))
    4774              :     return false;
    4775        23156 :   tree cmp_lhs = cca_ops[0];
    4776        23156 :   tree sum = cca_ops[1];
    4777        23156 :   tree base = cca_ops[2];
    4778              : 
    4779              :   /* Classify sum and populate the carry summand directly.  Most
    4780              :      specific first, mirroring long_mul_classify_carry's order.  */
    4781        23156 :   long_mul_summand carry{};
    4782        23156 :   tree sum_ops[LONG_MUL_MAX_CAPTURES];
    4783        23156 :   unsigned HOST_WIDE_INT shift_amt
    4784        23156 :     = wi::exact_log2 (wi::to_wide (cca_ops[3]));
    4785        23156 :   unsigned HOST_WIDE_INT halfwidth = TYPE_PRECISION (lhs_type) / 2;
    4786        23156 :   carry.shift = shift_amt;
    4787              : 
    4788        23156 :   if (shift_amt == halfwidth
    4789        23156 :       && gimple_mul_low_sum (sum, sum_ops, NULL))
    4790              :     {
    4791              :       /* mul_carry_low_sum's flat form ties the outer lshift amount to
    4792              :          the inner mul_hi's INTEGER_CST@0 via match.pd capture re-use;
    4793              :          the PHI form has no such tie, so gate on shift_amt explicitly.  */
    4794           14 :       carry.kind = LMK_CARRY_LOW_SUM;
    4795           14 :       carry.op0 = sum_ops[0];
    4796           14 :       carry.op1 = sum_ops[1];
    4797           14 :       carry.hilo[0] = cmp_lhs;
    4798           14 :       carry.hilo[1] = sum_ops[2];
    4799           14 :       carry.hilo[2] = sum_ops[3];
    4800              :     }
    4801        23142 :   else if (shift_amt == halfwidth
    4802        23142 :            && gimple_mul_cross_sum (sum, sum_ops, NULL))
    4803              :     {
    4804              :       /* mul_cross_sum is just (plus:c @0 @1) with no half-width
    4805              :          constraint.  Gate here to mirror mul_carry_cross_sum;
    4806              :          a mismatch falls through to the LMK_CARRY_LOW branch.  */
    4807            1 :       carry.kind = LMK_CARRY_CROSS_SUM;
    4808            1 :       carry.hilo[0] = cmp_lhs;
    4809            1 :       carry.hilo[1] = sum_ops[0];
    4810            1 :       carry.hilo[2] = sum_ops[1];
    4811              :     }
    4812        23141 :   else if (shift_amt == 0 && TREE_CODE (sum) == SSA_NAME)
    4813              :     {
    4814        21963 :       auto *def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (sum));
    4815        22111 :       if (!def || gimple_assign_rhs_code (def) != PLUS_EXPR)
    4816              :         return false;
    4817         4747 :       tree p1 = gimple_assign_rhs1 (def);
    4818         4747 :       tree p2 = gimple_assign_rhs2 (def);
    4819         4747 :       if (p1 != cmp_lhs && p2 != cmp_lhs)
    4820              :         return false;
    4821         4008 :       carry.kind = LMK_CARRY_LOW;
    4822         4008 :       carry.carry_a = cmp_lhs;
    4823         4008 :       carry.carry_b = p1 == cmp_lhs ? p2 : p1;
    4824         4008 :     }
    4825              :   else
    4826              :     return false;
    4827              : 
    4828              :   /* Linearize base, the rest of the high-part chain.  */
    4829         4023 :   if (TREE_CODE (base) != SSA_NAME)
    4830              :     return false;
    4831         4023 :   auto *base_def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (base));
    4832         1237 :   if (!base_def || gimple_assign_rhs_code (base_def) != PLUS_EXPR)
    4833              :     return false;
    4834              : 
    4835          633 :   tree op0, op1;
    4836          633 :   const long_mul_row *row
    4837          633 :     = long_mul_classify_chain (base_def, PLUS_EXPR, lhs_type, phi, &carry,
    4838              :                                NULL, &op0, &op1);
    4839          633 :   if (!row || row->part != long_mul_row::HIGH_PART)
    4840              :     return false;
    4841              : 
    4842           15 :   gimple_seq seq = NULL;
    4843           15 :   build_mul_high_seq (op0, op1, phi_res, gimple_location (phi), &seq);
    4844           15 :   gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (phi));
    4845           15 :   gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
    4846           15 :   gimple_stmt_iterator psi = gsi_for_stmt (phi);
    4847           15 :   remove_phi_node (&psi, false);
    4848           15 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4849            5 :     fprintf (dump_file,
    4850              :              "Long multiplication high part folded (carry PHI).\n");
    4851              :   return true;
    4852              : }
    4853              : 
    4854              : /* Verify if we have the following structure:
    4855              : 
    4856              :    iftmp1 = PHI <pow2a, pow2b, pow2c, ...>
    4857              :    _ssa1 = _ssa2 MOD|DIV iftmp1;
    4858              :    _ssa3 = _ssa1 EQ|NE 0;
    4859              : 
    4860              :    And, if the right conditions are met, change the PHI args
    4861              :    and "_ssa1" stmt to a cheaper alternative.
    4862              : 
    4863              :    - for MOD, if either "_ssa2" is known to be positive or
    4864              :    "_ssa1" is used just in zero comparisons:
    4865              : 
    4866              :    iftmp1 = PHI <(pow2a - 1), (pow2b - 1), (pow2c - 1), ...>
    4867              :    _ssa1 = _ssa2 & iftmp1;
    4868              : 
    4869              :    - for DIV, if "_ssa2" is known to be positive:
    4870              : 
    4871              :    iftmp1 = PHI <log2 (pow2a), log2 (pow2b), log2 (pow2c), ...>
    4872              :    _ssa1 = _ssa2 >> iftmp1;  */
    4873              : static bool
    4874       139879 : simplify_phi_result_movdiv (gimple *stmt, tree_code code)
    4875              : {
    4876       139879 :   tree rhs1 = gimple_assign_rhs1 (stmt);
    4877       139879 :   tree_code new_code;
    4878       139879 :   bool needs_reset = false;
    4879              : 
    4880              :   /* Skip complex types (PR127163) */
    4881       139879 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    4882              :     return false;
    4883              : 
    4884        73265 :   switch (code)
    4885              :     {
    4886          228 :       case TRUNC_MOD_EXPR:
    4887          228 :       case FLOOR_MOD_EXPR:
    4888          228 :         if (!tree_expr_nonnegative_p (rhs1))
    4889              :           {
    4890              :             /* For a negative X, the truncating X % 2^k and X & (2^k - 1)
    4891              :                differ (e.g. -3 % 4 is -3 but -3 & 3 is 1), though both are
    4892              :                zero for the same X.  That is enough when the result is only
    4893              :                compared with zero, but the range recorded for the MOD result
    4894              :                no longer holds for the AND, so reset it.  */
    4895          143 :             needs_reset = true;
    4896          143 :             if (!use_in_zero_equality (gimple_assign_lhs (stmt), true))
    4897              :               return false;
    4898              :           }
    4899              : 
    4900              :         new_code = BIT_AND_EXPR;
    4901              :         break;
    4902              : 
    4903          770 :       case TRUNC_DIV_EXPR:
    4904          770 :       case FLOOR_DIV_EXPR:
    4905          770 :       case EXACT_DIV_EXPR:
    4906          770 :         if (!tree_expr_nonnegative_p (rhs1))
    4907              :           return false;
    4908              : 
    4909              :         new_code = RSHIFT_EXPR;
    4910              :         break;
    4911              : 
    4912              :      default:
    4913              :         return false;
    4914              :     }
    4915              : 
    4916          619 :   gphi *phi = as_a<gphi *> (SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt)));
    4917              : 
    4918         1295 :   for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
    4919          663 :     if (!integer_pow2p (gimple_phi_arg_def (phi, i))
    4920          663 :         || tree_int_cst_sgn (gimple_phi_arg_def (phi, i)) < 0)
    4921              :       return false;
    4922              : 
    4923           13 :   tree type = TREE_TYPE (gimple_phi_result (phi));
    4924           13 :   tree new_phires = make_ssa_name (type);
    4925           13 :   gphi *new_phi = create_phi_node (new_phires, phi->bb);
    4926              : 
    4927           54 :   for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
    4928              :     {
    4929           28 :       tree phi_arg = gimple_phi_arg_def (phi, i);
    4930           28 :       edge e = gimple_phi_arg_edge (phi, i);
    4931           28 :       tree arg;
    4932              : 
    4933           28 :       if (new_code == RSHIFT_EXPR)
    4934            4 :         arg = wide_int_to_tree (type, wi::exact_log2 (wi::to_wide (phi_arg)));
    4935              :       else
    4936           24 :         arg = wide_int_to_tree (type, wi::to_wide (phi_arg) - 1);
    4937              : 
    4938           28 :       add_phi_arg (new_phi, arg, e,
    4939           28 :                    gimple_phi_arg_location (phi, e->dest_idx));
    4940              :     }
    4941              : 
    4942              :   /* Add a gimple_convert to integer_type_node for new_phires
    4943              :      since it might be a long long which we want to convert
    4944              :      into an integer or a bit_int that we want to convert into
    4945              :      an integer.  */
    4946           13 :   gimple_stmt_iterator gsi;
    4947           13 :   if (new_code == RSHIFT_EXPR)
    4948              :     {
    4949            2 :       gsi = gsi_for_stmt (stmt);
    4950            2 :       new_phires = gimple_convert (&gsi, true, GSI_SAME_STMT,
    4951              :                                    gimple_location (stmt),
    4952              :                                    integer_type_node, new_phires);
    4953              :     }
    4954              : 
    4955           13 :   gimple_assign_set_rhs2 (stmt, new_phires);
    4956           13 :   gimple_assign_set_rhs_code (stmt, new_code);
    4957           13 :   if (needs_reset)
    4958            8 :     reset_flow_sensitive_info (gimple_assign_lhs (stmt));
    4959           13 :   update_stmt (stmt);
    4960              : 
    4961           13 :   gsi = gsi_for_phi (phi);
    4962           13 :   remove_phi_node (&gsi, true);
    4963              : 
    4964           13 :   return true;
    4965              : }
    4966              : 
    4967              : /* Determine whether applying the 2 permutations (mask1 then mask2)
    4968              :    gives back one of the input.  */
    4969              : 
    4970              : static int
    4971           46 : is_combined_permutation_identity (tree mask1, tree mask2)
    4972              : {
    4973           46 :   tree mask;
    4974           46 :   unsigned HOST_WIDE_INT nelts, i, j;
    4975           46 :   bool maybe_identity1 = true;
    4976           46 :   bool maybe_identity2 = true;
    4977              : 
    4978           46 :   gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
    4979              :                        && TREE_CODE (mask2) == VECTOR_CST);
    4980              : 
    4981              :   /* For VLA masks, check for the following pattern:
    4982              :      v1 = VEC_PERM_EXPR (v0, ..., mask1)
    4983              :      v2 = VEC_PERM_EXPR (v1, ..., mask2)
    4984              :      -->
    4985              :      v2 = v0
    4986              :      if mask1 == mask2 == {nelts - 1, nelts - 2, ...}.  */
    4987              : 
    4988           46 :   if (operand_equal_p (mask1, mask2, 0)
    4989           46 :       && !VECTOR_CST_NELTS (mask1).is_constant ())
    4990              :     {
    4991              :       vec_perm_builder builder;
    4992              :       if (tree_to_vec_perm_builder (&builder, mask1))
    4993              :         {
    4994              :           poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1));
    4995              :           vec_perm_indices sel (builder, 1, nelts);
    4996              :           if (sel.series_p (0, 1, nelts - 1, -1))
    4997              :             return 1;
    4998              :         }
    4999              :     }
    5000              : 
    5001           46 :   mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
    5002           46 :   if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
    5003              :     return 0;
    5004              : 
    5005           46 :   if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
    5006              :     return 0;
    5007           80 :   for (i = 0; i < nelts; i++)
    5008              :     {
    5009           80 :       tree val = VECTOR_CST_ELT (mask, i);
    5010           80 :       gcc_assert (TREE_CODE (val) == INTEGER_CST);
    5011           80 :       j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
    5012           80 :       if (j == i)
    5013              :         maybe_identity2 = false;
    5014           63 :       else if (j == i + nelts)
    5015              :         maybe_identity1 = false;
    5016              :       else
    5017              :         return 0;
    5018              :     }
    5019            0 :   return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
    5020              : }
    5021              : 
    5022              : /* Combine a shuffle with its arguments.  Returns true if there were any
    5023              :    changes made.  */
    5024              : 
    5025              : static bool
    5026       194057 : simplify_permutation (gimple_stmt_iterator *gsi)
    5027              : {
    5028       194057 :   gimple *stmt = gsi_stmt (*gsi);
    5029       194057 :   gimple *def_stmt = NULL;
    5030       194057 :   tree op0, op1, op2, op3, arg0, arg1;
    5031       194057 :   enum tree_code code, code2 = ERROR_MARK;
    5032       194057 :   bool single_use_op0 = false;
    5033              : 
    5034       194057 :   gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
    5035              : 
    5036       194057 :   op0 = gimple_assign_rhs1 (stmt);
    5037       194057 :   op1 = gimple_assign_rhs2 (stmt);
    5038       194057 :   op2 = gimple_assign_rhs3 (stmt);
    5039              : 
    5040       194057 :   if (TREE_CODE (op2) != VECTOR_CST)
    5041              :     return false;
    5042              : 
    5043       191265 :   if (TREE_CODE (op0) == VECTOR_CST)
    5044              :     {
    5045              :       code = VECTOR_CST;
    5046              :       arg0 = op0;
    5047              :     }
    5048       189092 :   else if (TREE_CODE (op0) == SSA_NAME)
    5049              :     {
    5050       189092 :       def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
    5051       189092 :       if (!def_stmt)
    5052              :         return false;
    5053       180587 :       code = gimple_assign_rhs_code (def_stmt);
    5054       180587 :       if (code == VIEW_CONVERT_EXPR)
    5055              :         {
    5056         1687 :           tree rhs = gimple_assign_rhs1 (def_stmt);
    5057         1687 :           tree name = TREE_OPERAND (rhs, 0);
    5058         1687 :           if (TREE_CODE (name) != SSA_NAME)
    5059              :             return false;
    5060         1687 :           if (!has_single_use (name))
    5061          254 :             single_use_op0 = false;
    5062              :           /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
    5063              :              but still keep the code to indicate it comes from
    5064              :              VIEW_CONVERT_EXPR.  */
    5065         1687 :           def_stmt = safe_dyn_cast<gassign *> (SSA_NAME_DEF_STMT (name));
    5066          900 :           if (!def_stmt)
    5067              :             return false;
    5068          900 :           if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
    5069              :             return false;
    5070              :         }
    5071       179166 :       if (!can_propagate_from (def_stmt))
    5072              :         return false;
    5073        24701 :       arg0 = gimple_assign_rhs1 (def_stmt);
    5074              :     }
    5075              :   else
    5076              :     return false;
    5077              : 
    5078              :   /* Two consecutive shuffles.  */
    5079        24701 :   if (code == VEC_PERM_EXPR)
    5080              :     {
    5081         6765 :       tree orig;
    5082         6765 :       int ident;
    5083              : 
    5084         6765 :       if (op0 != op1)
    5085              :         return false;
    5086           46 :       op3 = gimple_assign_rhs3 (def_stmt);
    5087           46 :       if (TREE_CODE (op3) != VECTOR_CST)
    5088              :         return false;
    5089              :       /* is_combined_permutation_identity assumes that both permutes and
    5090              :          their inputs have the same number of elements.  Mixed widths are
    5091              :          left to the match.pd VEC_PERM merge patterns.  */
    5092           46 :       if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)),
    5093           46 :                     TYPE_VECTOR_SUBPARTS (TREE_TYPE (op3)))
    5094           92 :           || maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op0)),
    5095              :                        TYPE_VECTOR_SUBPARTS
    5096           46 :                          (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
    5097              :         return false;
    5098           46 :       ident = is_combined_permutation_identity (op3, op2);
    5099           46 :       if (!ident)
    5100              :         return false;
    5101            0 :       orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
    5102            0 :                           : gimple_assign_rhs2 (def_stmt);
    5103            0 :       gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
    5104            0 :       gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
    5105            0 :       gimple_set_num_ops (stmt, 2);
    5106            0 :       update_stmt (stmt);
    5107            0 :       remove_prop_source_from_use (op0);
    5108            0 :       return true;
    5109              :     }
    5110        20109 :   else if (code == CONSTRUCTOR
    5111        20109 :            || code == VECTOR_CST
    5112              :            || code == VIEW_CONVERT_EXPR)
    5113              :     {
    5114         5011 :       if (op0 != op1)
    5115              :         {
    5116         4813 :           if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
    5117              :             return false;
    5118              : 
    5119         4134 :           if (TREE_CODE (op1) == VECTOR_CST)
    5120              :             arg1 = op1;
    5121         3501 :           else if (TREE_CODE (op1) == SSA_NAME)
    5122              :             {
    5123         3501 :               gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
    5124         3501 :               if (!def_stmt2)
    5125              :                 return false;
    5126         1698 :               code2 = gimple_assign_rhs_code (def_stmt2);
    5127         1698 :               if (code2 == VIEW_CONVERT_EXPR)
    5128              :                 {
    5129            0 :                   tree rhs = gimple_assign_rhs1 (def_stmt2);
    5130            0 :                   tree name = TREE_OPERAND (rhs, 0);
    5131            0 :                   if (TREE_CODE (name) != SSA_NAME)
    5132              :                     return false;
    5133            0 :                   if (!has_single_use (name))
    5134              :                     return false;
    5135            0 :                   def_stmt2
    5136            0 :                     = safe_dyn_cast<gassign *> (SSA_NAME_DEF_STMT (name));
    5137            0 :                   if (!def_stmt2)
    5138              :                     return false;
    5139            0 :                   if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
    5140              :                     return false;
    5141              :                 }
    5142         1698 :               else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
    5143              :                 return false;
    5144         1485 :               if (!can_propagate_from (def_stmt2))
    5145              :                 return false;
    5146         1485 :               arg1 = gimple_assign_rhs1 (def_stmt2);
    5147              :             }
    5148              :           else
    5149              :             return false;
    5150              :         }
    5151              :       else
    5152              :         {
    5153              :           /* Already used twice in this statement.  */
    5154          198 :           if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
    5155              :             return false;
    5156              :           arg1 = arg0;
    5157              :         }
    5158              : 
    5159              :       /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
    5160              :          operands source, check whether it's valid to transform and prepare
    5161              :          the required new operands.  */
    5162         2247 :       if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
    5163              :         {
    5164              :           /* Figure out the target vector type to which operands should be
    5165              :              converted.  If both are CONSTRUCTOR, the types should be the
    5166              :              same, otherwise, use the one of CONSTRUCTOR.  */
    5167           24 :           tree tgt_type = NULL_TREE;
    5168           24 :           if (code == VIEW_CONVERT_EXPR)
    5169              :             {
    5170           24 :               gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
    5171           24 :               code = CONSTRUCTOR;
    5172           24 :               tgt_type = TREE_TYPE (arg0);
    5173              :             }
    5174           24 :           if (code2 == VIEW_CONVERT_EXPR)
    5175              :             {
    5176            0 :               tree arg1_type = TREE_TYPE (arg1);
    5177            0 :               if (tgt_type == NULL_TREE)
    5178              :                 tgt_type = arg1_type;
    5179            0 :               else if (tgt_type != arg1_type)
    5180           23 :                 return false;
    5181              :             }
    5182              : 
    5183           24 :           if (!VECTOR_TYPE_P (tgt_type))
    5184              :             return false;
    5185           24 :           tree op2_type = TREE_TYPE (op2);
    5186              : 
    5187              :           /* Figure out the shrunk factor.  */
    5188           24 :           poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
    5189           24 :           poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
    5190           24 :           if (maybe_gt (tgt_units, op2_units))
    5191              :             return false;
    5192           24 :           unsigned int factor;
    5193           47 :           if (!constant_multiple_p (op2_units, tgt_units, &factor))
    5194              :             return false;
    5195              : 
    5196              :           /* Build the new permutation control vector as target vector.  */
    5197           24 :           vec_perm_builder builder;
    5198           24 :           if (!tree_to_vec_perm_builder (&builder, op2))
    5199              :             return false;
    5200           24 :           vec_perm_indices indices (builder, 2, op2_units);
    5201           24 :           vec_perm_indices new_indices;
    5202           24 :           if (new_indices.new_shrunk_vector (indices, factor))
    5203              :             {
    5204            1 :               tree mask_type = tgt_type;
    5205            1 :               if (!VECTOR_INTEGER_TYPE_P (mask_type))
    5206              :                 {
    5207            0 :                   tree elem_type = TREE_TYPE (mask_type);
    5208            0 :                   unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
    5209            0 :                   tree int_type = build_nonstandard_integer_type (elem_size, 0);
    5210            0 :                   mask_type = build_vector_type (int_type, tgt_units);
    5211              :                 }
    5212            1 :               op2 = vec_perm_indices_to_tree (mask_type, new_indices);
    5213              :             }
    5214              :           else
    5215           23 :             return false;
    5216              : 
    5217              :           /* Convert the VECTOR_CST to the appropriate vector type.  */
    5218            1 :           if (tgt_type != TREE_TYPE (arg0))
    5219            0 :             arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
    5220            1 :           else if (tgt_type != TREE_TYPE (arg1))
    5221            0 :             arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
    5222           47 :         }
    5223              : 
    5224              :       /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before.  */
    5225         2224 :       gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
    5226              : 
    5227              :       /* Shuffle of a constructor.  */
    5228         2224 :       tree res_type
    5229         2224 :         = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)),
    5230         2224 :                              TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)));
    5231         2224 :       tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
    5232         2224 :       if (!opt
    5233          280 :           || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
    5234              :         return false;
    5235              :       /* Found VIEW_CONVERT_EXPR before, need one explicit conversion.  */
    5236          280 :       if (res_type != TREE_TYPE (op0))
    5237              :         {
    5238            1 :           tree name = make_ssa_name (TREE_TYPE (opt));
    5239            1 :           gimple *ass_stmt = gimple_build_assign (name, opt);
    5240            1 :           gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
    5241            1 :           opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
    5242              :         }
    5243          280 :       gimple_assign_set_rhs_from_tree (gsi, opt);
    5244          280 :       update_stmt (gsi_stmt (*gsi));
    5245          280 :       if (TREE_CODE (op0) == SSA_NAME)
    5246            1 :         remove_prop_source_from_use (op0);
    5247          280 :       if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
    5248            0 :         remove_prop_source_from_use (op1);
    5249              :       return true;
    5250              :     }
    5251              : 
    5252              :   return false;
    5253              : }
    5254              : 
    5255              : /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
    5256              :    conversions with code CONV_CODE or update it if still ERROR_MARK.
    5257              :    Return NULL_TREE if no such matching def was found.  */
    5258              : 
    5259              : static tree
    5260       441914 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
    5261              : {
    5262       441914 :   if (TREE_CODE (val) != SSA_NAME)
    5263              :     return NULL_TREE ;
    5264       411248 :   gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
    5265       411248 :   if (!def_stmt)
    5266              :     return NULL_TREE;
    5267       326120 :   enum tree_code code = gimple_assign_rhs_code (def_stmt);
    5268       326120 :   if (code == FLOAT_EXPR
    5269       326120 :       || code == FIX_TRUNC_EXPR
    5270              :       || CONVERT_EXPR_CODE_P (code))
    5271              :     {
    5272       188647 :       tree op1 = gimple_assign_rhs1 (def_stmt);
    5273       188647 :       if (conv_code == ERROR_MARK)
    5274        89639 :         conv_code = code;
    5275        99008 :       else if (conv_code != code)
    5276              :         return NULL_TREE;
    5277       188647 :       if (TREE_CODE (op1) != SSA_NAME)
    5278              :         return NULL_TREE;
    5279        79439 :       def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (op1));
    5280        60935 :       if (!def_stmt)
    5281              :         return NULL_TREE;
    5282        60935 :       code = gimple_assign_rhs_code (def_stmt);
    5283              :     }
    5284       198408 :   if (code != BIT_FIELD_REF)
    5285              :     return NULL_TREE;
    5286        23614 :   return gimple_assign_rhs1 (def_stmt);
    5287              : }
    5288              : 
    5289              : /* Rewrite a chain of BIT_INSERT_EXPRs that each insert one element into
    5290              :    a vector, like
    5291              :      v_1 = BIT_INSERT_EXPR <v_0, a_2, 0>;
    5292              :      v_3 = BIT_INSERT_EXPR <v_1, b_4, 32>;
    5293              :      ...
    5294              :    into a single CONSTRUCTOR {a_2, b_4, ...} (or a VECTOR_CST) when the
    5295              :    inserts overwrite every element of the vector, or when the vector at
    5296              :    the start of the chain is a VECTOR_CST or a CONSTRUCTOR that provides
    5297              :    the remaining elements.  A CONSTRUCTOR is expanded with vec_init, which
    5298              :    for instance turns a broadcast written element by element into a single
    5299              :    splat instruction.  The statements of the chain that become dead are
    5300              :    queued in SIMPLE_DCE_WORKLIST.  Returns true if there were any
    5301              :    changes.  */
    5302              : 
    5303              : static bool
    5304         2758 : simplify_bit_insert_chain (gimple_stmt_iterator *gsi,
    5305              :                            bitmap simple_dce_worklist)
    5306              : {
    5307         2758 :   gassign *stmt = as_a <gassign *> (gsi_stmt (*gsi));
    5308         2758 :   tree lhs = gimple_assign_lhs (stmt);
    5309         2758 :   tree type = TREE_TYPE (lhs);
    5310         2758 :   unsigned HOST_WIDE_INT nelts;
    5311              : 
    5312         2758 :   if (TREE_CODE (type) != VECTOR_TYPE
    5313         2628 :       || VECTOR_BOOLEAN_TYPE_P (type)
    5314         5386 :       || !TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
    5315              :     return false;
    5316              : 
    5317              :   /* Handle the chain once, from its last statement.  */
    5318         2628 :   use_operand_p use_p;
    5319         2628 :   gimple *use_stmt;
    5320         2628 :   if (single_imm_use (lhs, &use_p, &use_stmt)
    5321         2550 :       && is_gimple_assign (use_stmt)
    5322         1129 :       && gimple_assign_rhs_code (use_stmt) == BIT_INSERT_EXPR
    5323         3017 :       && gimple_assign_rhs1 (use_stmt) == lhs)
    5324              :     return false;
    5325              : 
    5326         2239 :   tree elt_type = TREE_TYPE (type);
    5327         2239 :   unsigned HOST_WIDE_INT elt_bits = vector_element_bits (type);
    5328         2239 :   auto_vec<tree, 16> elts;
    5329         2239 :   elts.safe_grow_cleared (nelts, true);
    5330         2239 :   unsigned HOST_WIDE_INT filled = 0;
    5331         2239 :   unsigned HOST_WIDE_INT ninserts = 0;
    5332         2239 :   gassign *def = stmt;
    5333         2239 :   tree base = NULL_TREE;
    5334         2636 :   do
    5335              :     {
    5336         2636 :       tree val = gimple_assign_rhs2 (def);
    5337         2636 :       tree pos = gimple_assign_rhs3 (def);
    5338         2636 :       if (!types_compatible_p (TREE_TYPE (val), elt_type)
    5339         2516 :           || !tree_fits_uhwi_p (pos)
    5340         5152 :           || tree_to_uhwi (pos) % elt_bits != 0)
    5341              :         break;
    5342         2516 :       unsigned HOST_WIDE_INT lane = tree_to_uhwi (pos) / elt_bits;
    5343         2516 :       if (lane >= nelts)
    5344              :         break;
    5345              :       /* A later insert into the same lane wins.  */
    5346         2516 :       if (!elts[lane])
    5347              :         {
    5348         2486 :           elts[lane] = val;
    5349         2486 :           filled++;
    5350              :         }
    5351         2516 :       ninserts++;
    5352         2516 :       base = gimple_assign_rhs1 (def);
    5353              :       /* Continue with the previous insert of the chain while some lanes
    5354              :          are still unknown.  Only look through inserts used by the chain
    5355              :          alone, the others have to stay anyway.  */
    5356         2516 :       def = NULL;
    5357         2516 :       if (filled < nelts && TREE_CODE (base) == SSA_NAME)
    5358              :         {
    5359              :           /* Bound the walk if the same lanes are written many times.  */
    5360         2342 :           if (ninserts >= 2 * nelts)
    5361              :             return false;
    5362         2342 :           gassign *prev = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (base));
    5363         1072 :           if (prev
    5364         1072 :               && gimple_assign_rhs_code (prev) == BIT_INSERT_EXPR
    5365          398 :               && has_single_use (base))
    5366              :             def = prev;
    5367              :         }
    5368              :     }
    5369         2516 :   while (def);
    5370              :   /* A single insert is already a single operation, and other
    5371              :      simplifications (like extracting part of the inserted value with a
    5372              :      BIT_FIELD_REF) only handle BIT_INSERT_EXPR.  Requiring two inserts
    5373              :      also means that the first operand of STMT is defined by an insert of
    5374              :      the chain, which is what is queued for DCE below.  */
    5375         2239 :   if (ninserts < 2)
    5376              :     return false;
    5377              : 
    5378              :   /* Take the elements that are not overwritten from the start of the
    5379              :      chain.  */
    5380          170 :   if (filled < nelts)
    5381              :     {
    5382          105 :       if (TREE_CODE (base) == VECTOR_CST)
    5383              :         {
    5384            5 :           for (unsigned HOST_WIDE_INT i = 0; i < nelts; i++)
    5385            4 :             if (!elts[i])
    5386            2 :               elts[i] = VECTOR_CST_ELT (base, i);
    5387              :         }
    5388          104 :       else if (TREE_CODE (base) == SSA_NAME)
    5389              :         {
    5390         2246 :           gassign *bdef = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (base));
    5391           11 :           if (!bdef || gimple_assign_rhs_code (bdef) != CONSTRUCTOR)
    5392              :             return false;
    5393            1 :           tree ctor = gimple_assign_rhs1 (bdef);
    5394            1 :           if (!types_compatible_p (TREE_TYPE (ctor), type))
    5395              :             return false;
    5396              :           /* Elements that are vectors themselves are not handled.  */
    5397            1 :           if (CONSTRUCTOR_NELTS (ctor) != 0
    5398            1 :               && TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value))
    5399              :                  == VECTOR_TYPE)
    5400              :             return false;
    5401            5 :           for (unsigned HOST_WIDE_INT i = 0; i < nelts; i++)
    5402            4 :             if (!elts[i])
    5403              :               {
    5404            2 :                 if (i < CONSTRUCTOR_NELTS (ctor))
    5405              :                   {
    5406            2 :                     constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
    5407            2 :                     if (ce->index
    5408            2 :                         && (!tree_fits_uhwi_p (ce->index)
    5409            2 :                             || tree_to_uhwi (ce->index) != i))
    5410              :                       return false;
    5411            2 :                     elts[i] = ce->value;
    5412              :                   }
    5413              :                 else
    5414              :                   /* Trailing elements of a CONSTRUCTOR are zero.  */
    5415            0 :                   elts[i] = build_zero_cst (elt_type);
    5416              :               }
    5417              :         }
    5418              :       else
    5419              :         return false;
    5420              :     }
    5421              : 
    5422              :   /* The CONSTRUCTOR uses the elements at the place of the last insert,
    5423              :      which can extend their live range.  Do not do that for SSA names
    5424              :      that occur in abnormal PHIs.  */
    5425           67 :   bool all_constant = true;
    5426          539 :   for (tree elt : elts)
    5427          338 :     if (TREE_CODE (elt) == SSA_NAME
    5428          338 :         && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (elt))
    5429              :       return false;
    5430          338 :     else if (!CONSTANT_CLASS_P (elt))
    5431          264 :       all_constant = false;
    5432              : 
    5433           67 :   tree new_rhs;
    5434           67 :   if (all_constant)
    5435              :     {
    5436           16 :       tree_vector_builder builder (type, nelts, 1);
    5437          120 :       for (tree elt : elts)
    5438           72 :         builder.quick_push (elt);
    5439           16 :       new_rhs = builder.build ();
    5440           16 :     }
    5441              :   else
    5442              :     {
    5443           51 :       vec<constructor_elt, va_gc> *ce;
    5444           51 :       vec_alloc (ce, nelts);
    5445          419 :       for (tree elt : elts)
    5446          266 :         CONSTRUCTOR_APPEND_ELT (ce, NULL_TREE, elt);
    5447           51 :       new_rhs = build_constructor (type, ce);
    5448              :     }
    5449              : 
    5450           67 :   tree old_vec = gimple_assign_rhs1 (stmt);
    5451           67 :   if (TREE_CODE (old_vec) == SSA_NAME)
    5452           67 :     bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (old_vec));
    5453           67 :   gimple_assign_set_rhs_from_tree (gsi, new_rhs);
    5454           67 :   update_stmt (gsi_stmt (*gsi));
    5455           67 :   return true;
    5456         2239 : }
    5457              : 
    5458              : /* Recognize a VEC_PERM_EXPR.  Returns true if there were any changes.  */
    5459              : 
    5460              : static bool
    5461       168476 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
    5462              : {
    5463       168476 :   gimple *stmt = gsi_stmt (*gsi);
    5464       168476 :   tree op, orig[2], type;
    5465       168476 :   unsigned i;
    5466       168476 :   unsigned HOST_WIDE_INT nelts;
    5467       168476 :   unsigned HOST_WIDE_INT refnelts;
    5468       168476 :   enum tree_code conv_code;
    5469       168476 :   constructor_elt *elt;
    5470              : 
    5471       168476 :   op = gimple_assign_rhs1 (stmt);
    5472       168476 :   type = TREE_TYPE (op);
    5473       168476 :   gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
    5474              :                        && TREE_CODE (type) == VECTOR_TYPE);
    5475              : 
    5476       168476 :   if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
    5477              :     return false;
    5478              : 
    5479       168476 :   orig[0] = NULL;
    5480       168476 :   orig[1] = NULL;
    5481       168476 :   tree orig_elem_type[2] = {};
    5482       168476 :   conv_code = ERROR_MARK;
    5483       168476 :   bool maybe_ident = true;
    5484       168476 :   bool maybe_blend[2] = { true, true };
    5485       168476 :   tree one_constant = NULL_TREE;
    5486       168476 :   tree one_nonconstant = NULL_TREE;
    5487       168476 :   tree subelt;
    5488       168476 :   auto_vec<tree> constants;
    5489       168476 :   constants.safe_grow_cleared (nelts, true);
    5490       168476 :   auto_vec<std::pair<unsigned, unsigned>, 64> elts;
    5491       168476 :   unsigned int tsubelts = 0;
    5492       477216 :   FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
    5493              :     {
    5494       441914 :       tree ref, op1;
    5495       441914 :       unsigned int elem, src_elem_size;
    5496       441914 :       unsigned HOST_WIDE_INT nsubelts = 1;
    5497              : 
    5498       441914 :       if (i >= nelts)
    5499       168476 :         return false;
    5500              : 
    5501              :       /* Look for elements extracted and possibly converted from
    5502              :          another vector.  */
    5503       441914 :       op1 = get_bit_field_ref_def (elt->value, conv_code);
    5504       446097 :       if (op1
    5505        23614 :           && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
    5506              :           /* Do not extend the live range of an SSA name that occurs in an
    5507              :              abnormal PHI.  */
    5508         4914 :           && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ref)
    5509         4864 :           && VECTOR_TYPE_P (TREE_TYPE (ref))
    5510         4849 :           && (tree_nop_conversion_p (TREE_TYPE (op1),
    5511         4849 :                                      TREE_TYPE (TREE_TYPE (ref)))
    5512          801 :               || (VECTOR_TYPE_P (TREE_TYPE (op1))
    5513          135 :                   && tree_nop_conversion_p (TREE_TYPE (TREE_TYPE (op1)),
    5514          135 :                                             TREE_TYPE (TREE_TYPE (ref)))
    5515          135 :                   && TYPE_VECTOR_SUBPARTS (TREE_TYPE (op1))
    5516          135 :                         .is_constant (&nsubelts)))
    5517         4183 :           && constant_multiple_p (bit_field_size (op1), nsubelts,
    5518              :                                   &src_elem_size)
    5519       446097 :           && constant_multiple_p (bit_field_offset (op1), src_elem_size, &elem)
    5520       446097 :           && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
    5521              :         {
    5522              :           unsigned int j;
    5523         4525 :           for (j = 0; j < 2; ++j)
    5524              :             {
    5525         4500 :               if (!orig[j])
    5526              :                 {
    5527         1802 :                   if (j == 0
    5528         1980 :                       || useless_type_conversion_p (TREE_TYPE (orig[0]),
    5529          178 :                                                     TREE_TYPE (ref)))
    5530              :                     break;
    5531              :                 }
    5532         2698 :               else if (ref == orig[j])
    5533              :                 break;
    5534              :             }
    5535              :           /* Found a suitable vector element.  */
    5536         4183 :           if (j < 2)
    5537              :             {
    5538         4158 :               orig[j] = ref;
    5539              :               /* Track what element type was actually extracted (which may
    5540              :                  differ in signedness from the vector's element type due to
    5541              :                  tree_nop_conversion_p).  */
    5542         4158 :               if (!orig_elem_type[j])
    5543         1798 :                 orig_elem_type[j] = TREE_TYPE (op1);
    5544         4158 :               if (elem != i || j != 0)
    5545         2153 :                 maybe_ident = false;
    5546         4158 :               if (elem != i)
    5547         2106 :                 maybe_blend[j] = false;
    5548         8577 :               for (unsigned int k = 0; k < nsubelts; ++k)
    5549         4419 :                 elts.safe_push (std::make_pair (j, elem + k));
    5550         4158 :               tsubelts += nsubelts;
    5551         4158 :               continue;
    5552         4158 :             }
    5553              :           /* Else fallthru.  */
    5554              :         }
    5555              :       /* Handle elements not extracted from a vector.
    5556              :           1. constants by permuting with constant vector
    5557              :           2. a unique non-constant element by permuting with a splat vector  */
    5558       437756 :       if (orig[1]
    5559       270087 :           && orig[1] != error_mark_node)
    5560              :         return false;
    5561       437731 :       orig[1] = error_mark_node;
    5562       437731 :       if (VECTOR_TYPE_P (TREE_TYPE (elt->value))
    5563       437731 :           && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (elt->value))
    5564         5798 :                         .is_constant (&nsubelts))
    5565              :         return false;
    5566       437731 :       if (CONSTANT_CLASS_P (elt->value))
    5567              :         {
    5568        30662 :           if (one_nonconstant)
    5569              :             return false;
    5570        21727 :           if (!one_constant)
    5571         9660 :             one_constant = TREE_CODE (elt->value) == VECTOR_CST
    5572         9660 :                            ? VECTOR_CST_ELT (elt->value, 0)
    5573              :                            : elt->value;
    5574        21727 :           if (TREE_CODE (elt->value) == VECTOR_CST)
    5575              :             {
    5576          777 :               for (unsigned int k = 0; k < nsubelts; k++)
    5577          587 :                 constants[tsubelts + k] = VECTOR_CST_ELT (elt->value, k);
    5578              :             }
    5579              :           else
    5580        21537 :             constants[tsubelts] = elt->value;
    5581              :         }
    5582              :       else
    5583              :         {
    5584       407069 :           if (one_constant)
    5585              :             return false;
    5586       397930 :           subelt = VECTOR_TYPE_P (TREE_TYPE (elt->value))
    5587       397930 :                    ? ssa_uniform_vector_p (elt->value)
    5588              :                    : elt->value;
    5589       397930 :           if (!subelt
    5590              :               /* The splat of the element of a uniform vector is a new use
    5591              :                  of that element.  Do not extend the live range of an SSA
    5592              :                  name that occurs in an abnormal PHI.  */
    5593       397930 :               || (VECTOR_TYPE_P (TREE_TYPE (elt->value))
    5594          123 :                   && TREE_CODE (subelt) == SSA_NAME
    5595          123 :                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (subelt)))
    5596              :             return false;
    5597       392574 :           if (!one_nonconstant)
    5598              :             one_nonconstant = subelt;
    5599       239921 :           else if (!operand_equal_p (one_nonconstant, subelt, 0))
    5600              :             return false;
    5601              :         }
    5602       609563 :       for (unsigned int k = 0; k < nsubelts; ++k)
    5603       304981 :         elts.safe_push (std::make_pair (1, tsubelts + k));
    5604       304582 :       tsubelts += nsubelts;
    5605       304582 :       maybe_ident = false;
    5606              :     }
    5607              : 
    5608        70604 :   if (elts.length () < nelts)
    5609              :     return false;
    5610              : 
    5611        34130 :   if (! orig[0]
    5612        34130 :       || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
    5613              :     return false;
    5614         1139 :   refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
    5615              :   /* We currently do not handle larger destination vectors.  */
    5616         1139 :   if (refnelts < nelts)
    5617              :     return false;
    5618              : 
    5619              :   /* Determine the element type for the conversion source.
    5620              :      As orig_elem_type keeps track of the original type, check
    5621              :      if we need to perform a sign swap after permuting.
    5622              :      We need to be able to construct a vector type from the element
    5623              :      type which is not possible for e.g. BitInt or pointers
    5624              :      so pun with an integer type if needed.   */
    5625          925 :   tree perm_eltype = TREE_TYPE (TREE_TYPE (orig[0]));
    5626          925 :   bool sign_change_p = false;
    5627          925 :   if (conv_code != ERROR_MARK
    5628          201 :       && orig_elem_type[0]
    5629         1126 :       && TYPE_SIGN (orig_elem_type[0]) != TYPE_SIGN (perm_eltype))
    5630              :     {
    5631           35 :       perm_eltype = signed_or_unsigned_type_for
    5632           35 :         (TYPE_UNSIGNED (orig_elem_type[0]), perm_eltype);
    5633           35 :       sign_change_p = true;
    5634              :     }
    5635          925 :   tree conv_src_type = build_vector_type (perm_eltype, nelts);
    5636              : 
    5637          925 :   if (maybe_ident)
    5638              :     {
    5639              :       /* When there is no conversion, use the target type directly.  */
    5640          190 :       if (conv_code == ERROR_MARK && nelts != refnelts)
    5641          190 :         conv_src_type = type;
    5642          190 :       if (conv_code != ERROR_MARK
    5643          190 :           && !supportable_convert_operation (conv_code, type, conv_src_type))
    5644              :         {
    5645              :           /* Only few targets implement direct conversion patterns so try
    5646              :              some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR.  */
    5647          103 :           optab optab;
    5648          103 :           insn_code icode;
    5649          103 :           tree halfvectype, dblvectype;
    5650          103 :           enum tree_code unpack_op;
    5651              : 
    5652          103 :           if (!BYTES_BIG_ENDIAN)
    5653          183 :             unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
    5654          103 :                          ? VEC_UNPACK_FLOAT_LO_EXPR
    5655              :                          : VEC_UNPACK_LO_EXPR);
    5656              :           else
    5657              :             unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
    5658              :                          ? VEC_UNPACK_FLOAT_HI_EXPR
    5659              :                          : VEC_UNPACK_HI_EXPR);
    5660              : 
    5661              :           /* Conversions between DFP and FP have no special tree code
    5662              :              but we cannot handle those since all relevant vector conversion
    5663              :              optabs only have a single mode.  */
    5664           15 :           if (CONVERT_EXPR_CODE_P (conv_code)
    5665           88 :               && FLOAT_TYPE_P (TREE_TYPE (type))
    5666          119 :               && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
    5667            8 :                   != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
    5668              :             return false;
    5669              : 
    5670           15 :           if (CONVERT_EXPR_CODE_P (conv_code)
    5671           87 :               && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
    5672           87 :                   == TYPE_PRECISION (TREE_TYPE (type)))
    5673            6 :               && orig_elem_type[0]
    5674            6 :               && useless_type_conversion_p (orig_elem_type[0],
    5675            6 :                                             TREE_TYPE (TREE_TYPE (orig[0])))
    5676            6 :               && mode_for_vector (as_a <scalar_mode>
    5677            6 :                                   (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
    5678           12 :                                   nelts * 2).exists ()
    5679            6 :               && (dblvectype
    5680            6 :                   = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
    5681            6 :                                        nelts * 2))
    5682              :               /* Only use it for vector modes or for vector booleans
    5683              :                  represented as scalar bitmasks.  See PR95528.  */
    5684            6 :               && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
    5685            0 :                   || VECTOR_BOOLEAN_TYPE_P (dblvectype))
    5686            6 :               && (optab = optab_for_tree_code (unpack_op,
    5687              :                                                dblvectype,
    5688              :                                                optab_default))
    5689            6 :               && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
    5690              :                   != CODE_FOR_nothing)
    5691          102 :               && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
    5692              :             {
    5693            0 :               gimple_seq stmts = NULL;
    5694            0 :               tree dbl;
    5695            0 :               if (refnelts == nelts)
    5696              :                 {
    5697              :                   /* ???  Paradoxical subregs don't exist, so insert into
    5698              :                      the lower half of a wider zero vector.  */
    5699            0 :                   dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
    5700              :                                       build_zero_cst (dblvectype), orig[0],
    5701            0 :                                       bitsize_zero_node);
    5702              :                 }
    5703            0 :               else if (refnelts == 2 * nelts)
    5704              :                 dbl = orig[0];
    5705              :               else
    5706            0 :                 dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
    5707            0 :                                     orig[0], TYPE_SIZE (dblvectype),
    5708            0 :                                     bitsize_zero_node);
    5709            0 :               gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    5710            0 :               gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
    5711              :             }
    5712           15 :           else if (CONVERT_EXPR_CODE_P (conv_code)
    5713           87 :                    && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
    5714           87 :                        == 2 * TYPE_PRECISION (TREE_TYPE (type)))
    5715            5 :                    && orig_elem_type[0]
    5716            5 :                    && useless_type_conversion_p (orig_elem_type[0],
    5717            5 :                                                  TREE_TYPE (TREE_TYPE (orig[0])))
    5718            1 :                    && mode_for_vector (as_a <scalar_mode>
    5719            1 :                                          (TYPE_MODE
    5720              :                                            (TREE_TYPE (TREE_TYPE (orig[0])))),
    5721            2 :                                        nelts / 2).exists ()
    5722            1 :                    && (halfvectype
    5723            1 :                          = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
    5724            1 :                                               nelts / 2))
    5725              :                    /* Only use it for vector modes or for vector booleans
    5726              :                       represented as scalar bitmasks.  See PR95528.  */
    5727            1 :                    && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
    5728            0 :                        || VECTOR_BOOLEAN_TYPE_P (halfvectype))
    5729            1 :                    && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
    5730              :                                                     halfvectype,
    5731              :                                                     optab_default))
    5732            1 :                    && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
    5733              :                        != CODE_FOR_nothing)
    5734          103 :                    && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
    5735              :             {
    5736            0 :               gimple_seq stmts = NULL;
    5737            0 :               tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
    5738            0 :                                        orig[0], TYPE_SIZE (halfvectype),
    5739            0 :                                        bitsize_zero_node);
    5740            0 :               tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
    5741            0 :                                        orig[0], TYPE_SIZE (halfvectype),
    5742            0 :                                        TYPE_SIZE (halfvectype));
    5743            0 :               gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    5744            0 :               gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
    5745              :                                               low, hig);
    5746              :             }
    5747              :           else
    5748          102 :             return false;
    5749            0 :           update_stmt (gsi_stmt (*gsi));
    5750            0 :           return true;
    5751              :         }
    5752           87 :       if (nelts != refnelts)
    5753              :         {
    5754           15 :           gassign *lowpart
    5755           15 :             = gimple_build_assign (make_ssa_name (conv_src_type),
    5756              :                                    build3 (BIT_FIELD_REF, conv_src_type,
    5757           15 :                                            orig[0], TYPE_SIZE (conv_src_type),
    5758              :                                            bitsize_zero_node));
    5759           15 :           gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
    5760           15 :           orig[0] = gimple_assign_lhs (lowpart);
    5761              :         }
    5762           72 :       else if (sign_change_p)
    5763              :         {
    5764            0 :           gassign *conv
    5765            0 :             = gimple_build_assign (make_ssa_name (conv_src_type),
    5766              :                                    build1 (VIEW_CONVERT_EXPR, conv_src_type,
    5767              :                                            orig[0]));
    5768            0 :           gsi_insert_before (gsi, conv, GSI_SAME_STMT);
    5769            0 :           orig[0] = gimple_assign_lhs (conv);
    5770              :         }
    5771           87 :       if (conv_code == ERROR_MARK)
    5772              :         {
    5773           70 :           tree src_type = TREE_TYPE (orig[0]);
    5774           70 :           if (!useless_type_conversion_p (type, src_type))
    5775              :             {
    5776            0 :               gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
    5777              :                                     TYPE_VECTOR_SUBPARTS (src_type))
    5778              :                           && tree_nop_conversion_p (TREE_TYPE (type),
    5779              :                                                     TREE_TYPE (src_type)));
    5780            0 :               tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
    5781            0 :               orig[0] = make_ssa_name (type);
    5782            0 :               gassign *assign = gimple_build_assign (orig[0], rhs);
    5783            0 :               gsi_insert_before (gsi, assign, GSI_SAME_STMT);
    5784              :             }
    5785           70 :           gimple_assign_set_rhs_from_tree (gsi, orig[0]);
    5786              :         }
    5787              :       else
    5788           17 :         gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
    5789              :                                         NULL_TREE, NULL_TREE);
    5790              :     }
    5791              :   else
    5792              :     {
    5793              :       /* If we combine a vector with a non-vector avoid cases where
    5794              :          we'll obviously end up with more GIMPLE stmts which is when
    5795              :          we'll later not fold this to a single insert into the vector
    5796              :          and we had a single extract originally.  See PR92819.  */
    5797          735 :       if (nelts == 2
    5798          303 :           && refnelts > 2
    5799          111 :           && orig[1] == error_mark_node
    5800           31 :           && !maybe_blend[0])
    5801           92 :         return false;
    5802          710 :       tree mask_type, perm_type;
    5803          710 :       perm_type = TREE_TYPE (orig[0]);
    5804          710 :       if (conv_code != ERROR_MARK
    5805          710 :           && !supportable_convert_operation (conv_code, type, conv_src_type))
    5806              :         return false;
    5807              : 
    5808              :       /* Now that we know the number of elements of the source build the
    5809              :          permute vector.
    5810              :          ???  When the second vector has constant values we can shuffle
    5811              :          it and its source indexes to make the permutation supported.
    5812              :          For now it mimics a blend.  */
    5813          700 :       vec_perm_builder sel (refnelts, refnelts, 1);
    5814          700 :       bool all_same_p = true;
    5815         8004 :       for (i = 0; i < elts.length (); ++i)
    5816              :         {
    5817         3302 :           sel.quick_push (elts[i].second + elts[i].first * refnelts);
    5818         3302 :           all_same_p &= known_eq (sel[i], sel[0]);
    5819              :         }
    5820              :       /* And fill the tail with "something".  It's really don't care,
    5821              :          and ideally we'd allow VEC_PERM to have a smaller destination
    5822              :          vector.  As a heuristic:
    5823              : 
    5824              :          (a) if what we have so far duplicates a single element, make the
    5825              :              tail do the same
    5826              : 
    5827              :          (b) otherwise preserve a uniform orig[0].  This facilitates
    5828              :              later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR.  */
    5829         1282 :       for (; i < refnelts; ++i)
    5830         1164 :         sel.quick_push (all_same_p
    5831         1746 :                         ? sel[0]
    5832          156 :                         : (elts[0].second == 0 && elts[0].first == 0
    5833          856 :                            ? 0 : refnelts) + i);
    5834          881 :       vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
    5835          700 :       machine_mode vmode = TYPE_MODE (perm_type);
    5836          700 :       if ((cfun->curr_properties & PROP_gimple_lvec)
    5837          700 :           && !can_vec_perm_const_p (vmode, vmode, indices))
    5838              :         return false;
    5839          643 :       mask_type = build_vector_type (ssizetype, refnelts);
    5840          643 :       tree op2 = vec_perm_indices_to_tree (mask_type, indices);
    5841          643 :       bool converted_orig1 = false;
    5842          643 :       gimple_seq stmts = NULL;
    5843          643 :       if (!orig[1])
    5844          156 :         orig[1] = orig[0];
    5845          487 :       else if (orig[1] == error_mark_node
    5846          387 :                && one_nonconstant)
    5847              :         {
    5848              :           /* ???  We can see if we can safely convert to the original
    5849              :              element type.  */
    5850           77 :           converted_orig1 = conv_code != ERROR_MARK;
    5851           77 :           tree target_type = converted_orig1 ? type : perm_type;
    5852           77 :           tree nonconstant_for_splat = one_nonconstant;
    5853              :           /* If there's a nop conversion between the target element type and
    5854              :              the nonconstant's type, convert it.  */
    5855           77 :           if (!useless_type_conversion_p (TREE_TYPE (target_type),
    5856           77 :                                           TREE_TYPE (one_nonconstant)))
    5857            0 :             nonconstant_for_splat
    5858            0 :               = gimple_build (&stmts, NOP_EXPR, TREE_TYPE (target_type),
    5859              :                               one_nonconstant);
    5860           77 :           orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
    5861              :                                                   target_type,
    5862              :                                                   nonconstant_for_splat);
    5863           77 :         }
    5864          410 :       else if (orig[1] == error_mark_node)
    5865              :         {
    5866              :           /* ???  See if we can convert the vector to the original type.  */
    5867          310 :           converted_orig1 = conv_code != ERROR_MARK;
    5868          310 :           unsigned n = converted_orig1 ? nelts : refnelts;
    5869          293 :           tree target_type = converted_orig1 ? type : perm_type;
    5870          310 :           tree_vector_builder vec (target_type, n, 1);
    5871         2508 :           for (unsigned i = 0; i < n; ++i)
    5872         3652 :             if (i < nelts && constants[i])
    5873              :               {
    5874         1056 :                 tree constant = constants[i];
    5875              :                 /* If there's a nop conversion, convert the constant.  */
    5876         1056 :                 if (!useless_type_conversion_p (TREE_TYPE (target_type),
    5877         1056 :                                                 TREE_TYPE (constant)))
    5878            2 :                   constant = fold_convert (TREE_TYPE (target_type), constant);
    5879         1056 :                 vec.quick_push (constant);
    5880              :               }
    5881              :             else
    5882              :               {
    5883              :                 /* ??? Push a don't-care value.  */
    5884          832 :                 tree constant = one_constant;
    5885          832 :                 if (!useless_type_conversion_p (TREE_TYPE (target_type),
    5886          832 :                                                 TREE_TYPE (constant)))
    5887            2 :                   constant = fold_convert (TREE_TYPE (target_type), constant);
    5888          832 :                 vec.quick_push (constant);
    5889              :               }
    5890          310 :           orig[1] = vec.build ();
    5891          310 :         }
    5892          543 :       tree blend_op2 = NULL_TREE;
    5893          543 :       if (converted_orig1)
    5894              :         {
    5895              :           /* Make sure we can do a blend in the target type.  */
    5896           22 :           vec_perm_builder sel (nelts, nelts, 1);
    5897          100 :           for (i = 0; i < elts.length (); ++i)
    5898           78 :             sel.quick_push (elts[i].first
    5899           78 :                             ? elts[i].second + nelts : i);
    5900           22 :           vec_perm_indices indices (sel, 2, nelts);
    5901           22 :           machine_mode vmode = TYPE_MODE (type);
    5902           22 :           if ((cfun->curr_properties & PROP_gimple_lvec)
    5903           22 :               && !can_vec_perm_const_p (vmode, vmode, indices))
    5904            0 :             return false;
    5905           22 :           mask_type = build_vector_type (ssizetype, nelts);
    5906           22 :           blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
    5907           22 :         }
    5908              : 
    5909              :       /* For a real orig[1] (no splat, constant etc.) we might need to
    5910              :          nop-convert it.  Do so here.  */
    5911          643 :       if (orig[1] && orig[1] != error_mark_node
    5912          643 :           && !converted_orig1
    5913          621 :           && !useless_type_conversion_p (perm_type, TREE_TYPE (orig[1]))
    5914          643 :           && tree_nop_conversion_p (TREE_TYPE (perm_type),
    5915            0 :                                     TREE_TYPE (TREE_TYPE (orig[1]))))
    5916            0 :         orig[1] = gimple_build (&stmts, VIEW_CONVERT_EXPR, perm_type,
    5917              :                                 orig[1]);
    5918              : 
    5919          643 :       tree orig1_for_perm
    5920          643 :         = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
    5921          643 :       tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
    5922              :                                orig[0], orig1_for_perm, op2);
    5923              :       /* If we're building a smaller vector, extract the element
    5924              :          with the proper type.  */
    5925          643 :       if (nelts != refnelts)
    5926          222 :         res = gimple_build (&stmts, BIT_FIELD_REF,
    5927              :                             conv_code != ERROR_MARK ? conv_src_type : type,
    5928              :                             res,
    5929          111 :                             TYPE_SIZE (conv_code != ERROR_MARK ? conv_src_type
    5930              :                                                                : type),
    5931          111 :                             bitsize_zero_node);
    5932              :       /* Otherwise, we can still have an intermediate sign change.
    5933              :          ??? In that case we have two subsequent conversions.
    5934              :          We should be able to merge them.  */
    5935          532 :       else if (sign_change_p)
    5936           15 :         res = gimple_build (&stmts, VIEW_CONVERT_EXPR, conv_src_type, res);
    5937              :       /* Finally, apply the conversion.  */
    5938          643 :       if (conv_code != ERROR_MARK)
    5939           56 :         res = gimple_build (&stmts, conv_code, type, res);
    5940          587 :       else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
    5941              :         {
    5942            3 :           gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
    5943              :                                 TYPE_VECTOR_SUBPARTS (perm_type))
    5944              :                       && tree_nop_conversion_p (TREE_TYPE (type),
    5945              :                                                 TREE_TYPE (perm_type)));
    5946            3 :           res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
    5947              :         }
    5948              :       /* Blend in the actual constant.  */
    5949          643 :       if (converted_orig1)
    5950           22 :         res = gimple_build (&stmts, VEC_PERM_EXPR, type,
    5951           22 :                             res, orig[1], blend_op2);
    5952          643 :       gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    5953          643 :       gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
    5954          700 :     }
    5955          730 :   update_stmt (gsi_stmt (*gsi));
    5956          730 :   return true;
    5957       168476 : }
    5958              : 
    5959              : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
    5960              :    lvalue.  This splits out an address computation stmt before *GSI
    5961              :    and returns a MEM_REF wrapping the address.  */
    5962              : 
    5963              : static tree
    5964         1142 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
    5965              : {
    5966         1142 :   if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
    5967          189 :     mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
    5968         1142 :   tree ptrtype = build_pointer_type (TREE_TYPE (ref));
    5969         1142 :   tree tem = make_ssa_name (ptrtype);
    5970         1142 :   gimple *new_stmt
    5971         1142 :     = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
    5972              :                                         unshare_expr (ref)));
    5973         1142 :   gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5974         2284 :   ref = build2_loc (EXPR_LOCATION (ref),
    5975         1142 :                     MEM_REF, TREE_TYPE (ref), tem,
    5976         1142 :                     build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
    5977         1142 :   return ref;
    5978              : }
    5979              : 
    5980              : /* Rewrite the vector load at *GSI to component-wise loads if the load
    5981              :    is only used in BIT_FIELD_REF extractions with eventual intermediate
    5982              :    widening.  */
    5983              : 
    5984              : static void
    5985       286162 : optimize_vector_load (gimple_stmt_iterator *gsi)
    5986              : {
    5987       286162 :   gimple *stmt = gsi_stmt (*gsi);
    5988       286162 :   tree lhs = gimple_assign_lhs (stmt);
    5989       286162 :   tree rhs = gimple_assign_rhs1 (stmt);
    5990       286162 :   tree vuse = gimple_vuse (stmt);
    5991              : 
    5992              :   /* Gather BIT_FIELD_REFs to rewrite, looking through
    5993              :      VEC_UNPACK_{LO,HI}_EXPR.  */
    5994       286162 :   use_operand_p use_p;
    5995       286162 :   imm_use_iterator iter;
    5996       286162 :   bool rewrite = true;
    5997       286162 :   bool scalar_use = false;
    5998       286162 :   bool unpack_use = false;
    5999       286162 :   auto_vec<gassign *, 8> bf_stmts;
    6000       286162 :   auto_vec<tree, 8> worklist;
    6001       286162 :   worklist.quick_push (lhs);
    6002       288034 :   do
    6003              :     {
    6004       288034 :       tree def = worklist.pop ();
    6005       288034 :       unsigned HOST_WIDE_INT def_eltsize
    6006       288034 :         = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
    6007       367446 :       FOR_EACH_IMM_USE_FAST (use_p, iter, def)
    6008              :         {
    6009       346206 :           gimple *use_stmt = USE_STMT (use_p);
    6010       346206 :           if (is_gimple_debug (use_stmt))
    6011        79412 :             continue;
    6012       344534 :           tree use_lhs;
    6013       344534 :           gassign *use_a = dyn_cast<gassign *> (use_stmt);
    6014       344534 :           if (!use_a
    6015              :               /* For alias reasons we move the use to the place of the
    6016              :                  load.  Avoid this when abnormals are involved.  */
    6017       344534 :               || ((TREE_CODE ((use_lhs = gimple_assign_lhs (use_a)))
    6018              :                    == SSA_NAME)
    6019       243585 :                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs)))
    6020              :             {
    6021              :               rewrite = false;
    6022       266794 :               break;
    6023              :             }
    6024       314207 :           enum tree_code use_code = gimple_assign_rhs_code (use_a);
    6025       314207 :           tree use_rhs = gimple_assign_rhs1 (use_a);
    6026       388220 :           if (use_code == BIT_FIELD_REF
    6027        74014 :               && TREE_OPERAND (use_rhs, 0) == def
    6028              :               /* If its on the VEC_UNPACK_{HI,LO}_EXPR
    6029              :                  def need to verify it is element aligned.  */
    6030       388221 :               && (def == lhs
    6031          153 :                   || (known_eq (bit_field_size (use_rhs), def_eltsize)
    6032        74166 :                       && constant_multiple_p (bit_field_offset (use_rhs),
    6033              :                                               def_eltsize)
    6034              :                       /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
    6035              :                          via a NOP_EXPR only for integral types.
    6036              :                          ???  Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR.  */
    6037          153 :                       && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
    6038              :             {
    6039        74013 :               if (!VECTOR_TYPE_P (TREE_TYPE (gimple_assign_lhs (use_a))))
    6040        71616 :                 scalar_use = true;
    6041        74013 :               bf_stmts.safe_push (use_a);
    6042        74013 :               continue;
    6043              :             }
    6044              :           /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR.  */
    6045       240194 :           if (def == lhs
    6046       238403 :               && (use_code == VEC_UNPACK_HI_EXPR
    6047       238403 :                   || use_code == VEC_UNPACK_LO_EXPR)
    6048         3727 :               && use_rhs == lhs)
    6049              :             {
    6050         3727 :               unpack_use = true;
    6051         3727 :               worklist.safe_push (gimple_assign_lhs (use_a));
    6052         3727 :               continue;
    6053              :             }
    6054              :           rewrite = false;
    6055              :           break;
    6056       288034 :         }
    6057       288034 :       if (!rewrite)
    6058              :         break;
    6059              :     }
    6060        42480 :   while (!worklist.is_empty ());
    6061              : 
    6062       286162 :   rewrite = rewrite && (scalar_use
    6063        19368 :                         || unpack_use
    6064          674 :                         || !can_implement_p (mov_optab,
    6065          674 :                                              TYPE_MODE (TREE_TYPE (lhs))));
    6066       267008 :   if (!rewrite)
    6067              :     {
    6068       267008 :       gsi_next (gsi);
    6069       267008 :       return;
    6070              :     }
    6071              :   /* We now have all ultimate uses of the load to rewrite in bf_stmts.  */
    6072              : 
    6073              :   /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
    6074              :      For TARGET_MEM_REFs we have to separate the LEA from the reference.  */
    6075        19154 :   tree load_rhs = rhs;
    6076        19154 :   if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
    6077         1141 :     load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
    6078              : 
    6079              :   /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
    6080              :      the place of the original load.  */
    6081       127211 :   for (gassign *use_stmt : bf_stmts)
    6082              :     {
    6083        69749 :       tree bfr = gimple_assign_rhs1 (use_stmt);
    6084        69749 :       tree new_rhs = unshare_expr (load_rhs);
    6085        69749 :       if (TREE_OPERAND (bfr, 0) != lhs)
    6086              :         {
    6087              :           /* When the BIT_FIELD_REF is on the promoted vector we have to
    6088              :              adjust it and emit a conversion afterwards.  */
    6089          152 :           gimple *def_stmt
    6090          152 :               = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
    6091          152 :           enum tree_code def_code
    6092          152 :               = gimple_assign_rhs_code (def_stmt);
    6093              : 
    6094              :           /* The adjusted BIT_FIELD_REF is of the promotion source
    6095              :              vector size and at half of the offset...  */
    6096          152 :           new_rhs = fold_build3 (BIT_FIELD_REF,
    6097              :                                  TREE_TYPE (TREE_TYPE (lhs)),
    6098              :                                  new_rhs,
    6099              :                                  TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
    6100              :                                  size_binop (EXACT_DIV_EXPR,
    6101              :                                              TREE_OPERAND (bfr, 2),
    6102              :                                              bitsize_int (2)));
    6103              :           /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR.  */
    6104          152 :           if (def_code == (!BYTES_BIG_ENDIAN
    6105              :                            ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
    6106           76 :             TREE_OPERAND (new_rhs, 2)
    6107          152 :               = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
    6108              :                             size_binop (EXACT_DIV_EXPR,
    6109              :                                         TYPE_SIZE (TREE_TYPE (lhs)),
    6110              :                                         bitsize_int (2)));
    6111          152 :           tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
    6112          152 :           gimple *new_stmt = gimple_build_assign (tem, new_rhs);
    6113          152 :           location_t loc = gimple_location (use_stmt);
    6114          152 :           gimple_set_location (new_stmt, loc);
    6115          152 :           gimple_set_vuse (new_stmt, vuse);
    6116          152 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    6117              :           /* Perform scalar promotion.  */
    6118          152 :           new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
    6119              :                                           NOP_EXPR, tem);
    6120          152 :           gimple_set_location (new_stmt, loc);
    6121          152 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    6122              :         }
    6123              :       else
    6124              :         {
    6125              :           /* When the BIT_FIELD_REF is on the original load result
    6126              :              we can just wrap that.  */
    6127        69597 :           tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
    6128              :                                       unshare_expr (load_rhs),
    6129              :                                       TREE_OPERAND (bfr, 1),
    6130              :                                       TREE_OPERAND (bfr, 2));
    6131        69597 :           gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
    6132              :                                                   new_rhs);
    6133        69597 :           location_t loc = gimple_location (use_stmt);
    6134        69597 :           gimple_set_location (new_stmt, loc);
    6135        69597 :           gimple_set_vuse (new_stmt, vuse);
    6136        69597 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    6137              :         }
    6138        69749 :       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    6139        69749 :       unlink_stmt_vdef (use_stmt);
    6140        69749 :       gsi_remove (&gsi2, true);
    6141              :     }
    6142              : 
    6143              :   /* Finally get rid of the intermediate stmts.  */
    6144        19154 :   gimple *use_stmt;
    6145        19826 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    6146              :     {
    6147          672 :       if (is_gimple_debug (use_stmt))
    6148              :         {
    6149          610 :           if (gimple_debug_bind_p (use_stmt))
    6150              :             {
    6151          610 :               gimple_debug_bind_reset_value (use_stmt);
    6152          610 :               update_stmt (use_stmt);
    6153              :             }
    6154          610 :           continue;
    6155              :         }
    6156           62 :       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    6157           62 :       unlink_stmt_vdef (use_stmt);
    6158           62 :       release_defs (use_stmt);
    6159           62 :       gsi_remove (&gsi2, true);
    6160        19154 :     }
    6161              :   /* And the original load.  */
    6162        19154 :   release_defs (stmt);
    6163        19154 :   gsi_remove (gsi, true);
    6164       286162 : }
    6165              : 
    6166              : 
    6167              : /* Primitive "lattice" function for gimple_simplify.  */
    6168              : 
    6169              : static tree
    6170   1824867817 : fwprop_ssa_val (tree name)
    6171              : {
    6172              :   /* First valueize NAME.  */
    6173   1824867817 :   if (TREE_CODE (name) == SSA_NAME
    6174   1824867817 :       && SSA_NAME_VERSION (name) < lattice.length ())
    6175              :     {
    6176   1823913350 :       tree val = lattice[SSA_NAME_VERSION (name)];
    6177   1823913350 :       if (val)
    6178   1824867817 :         name = val;
    6179              :     }
    6180              :   /* We continue matching along SSA use-def edges for SSA names
    6181              :      that are not single-use.  Currently there are no patterns
    6182              :      that would cause any issues with that.  */
    6183   1824867817 :   return name;
    6184              : }
    6185              : 
    6186              : /* Search for opportunities to free half of the lanes in the following pattern:
    6187              : 
    6188              :      v_in = {e0, e1, e2, e3}
    6189              :      v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
    6190              :      // v_1 = {e0, e2, e0, e2}
    6191              :      v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
    6192              :      // v_2 = {e1, e3, e1, e3}
    6193              : 
    6194              :      v_x = v_1 + v_2
    6195              :      // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
    6196              :      v_y = v_1 - v_2
    6197              :      // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
    6198              : 
    6199              :      v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
    6200              :      // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
    6201              : 
    6202              :    The last statement could be simplified to:
    6203              :      v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
    6204              :      // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
    6205              : 
    6206              :    Characteristic properties:
    6207              :    - v_1 and v_2 are created from the same input vector v_in and introduce the
    6208              :      lane duplication (in the selection operand) that we can eliminate.
    6209              :    - v_x and v_y are results from lane-preserving operations that use v_1 and
    6210              :      v_2 as inputs.
    6211              :    - v_out is created by selecting from duplicated lanes.  */
    6212              : 
    6213              : static bool
    6214       191322 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
    6215              : {
    6216       191322 :   unsigned HOST_WIDE_INT nelts;
    6217              : 
    6218       191322 :   gcc_checking_assert (stmt);
    6219       191322 :   gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
    6220       191322 :   basic_block bb = gimple_bb (stmt);
    6221              : 
    6222              :   /* Decompose the final vec permute statement.  */
    6223       191322 :   tree v_x = gimple_assign_rhs1 (stmt);
    6224       191322 :   tree v_y = gimple_assign_rhs2 (stmt);
    6225       191322 :   tree sel = gimple_assign_rhs3 (stmt);
    6226              : 
    6227       191322 :   if (TREE_CODE (sel) != VECTOR_CST
    6228       266183 :       || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
    6229       188530 :       || TREE_CODE (v_x) != SSA_NAME
    6230       186436 :       || TREE_CODE (v_y) != SSA_NAME
    6231       180943 :       || !has_single_use (v_x)
    6232       304754 :       || !has_single_use (v_y))
    6233              :     return false;
    6234              : 
    6235              :   /* Don't analyse sequences with many lanes.  */
    6236       111575 :   if (nelts > 4)
    6237              :     return false;
    6238              : 
    6239              :   /* Lookup the definition of v_x and v_y.  */
    6240       107349 :   gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
    6241       107349 :   gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
    6242       106960 :   if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
    6243       214309 :       || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
    6244              :     return false;
    6245              : 
    6246              :   /* Check the operations that define v_x and v_y.  */
    6247       106953 :   if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
    6248       109074 :       || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
    6249              :     return false;
    6250              : 
    6251         2121 :   tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
    6252         2121 :   tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
    6253         2121 :   tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
    6254         2121 :   tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
    6255              : 
    6256         2121 :   if (v_x_stmt == v_y_stmt
    6257         2121 :       || TREE_CODE (v_x_1) != SSA_NAME
    6258         2118 :       || TREE_CODE (v_x_2) != SSA_NAME
    6259         2094 :       || num_imm_uses (v_x_1) != 2
    6260         4056 :       || num_imm_uses (v_x_2) != 2)
    6261              :     return false;
    6262              : 
    6263         1900 :   if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
    6264              :     {
    6265              :       /* Allow operands of commutative operators to swap.  */
    6266          659 :       if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
    6267              :         {
    6268              :           /* Keep v_x_1 the first operand for non-commutative operators.  */
    6269          254 :           std::swap (v_x_1, v_x_2);
    6270          254 :           if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
    6271              :             return false;
    6272              :         }
    6273          405 :       else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
    6274              :         {
    6275          405 :           if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
    6276              :             return false;
    6277              :         }
    6278              :       else
    6279              :         return false;
    6280              :     }
    6281         1900 :   gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
    6282         1900 :   gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
    6283         1836 :   if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
    6284         3736 :       || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
    6285              :     return false;
    6286              : 
    6287         1830 :   if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
    6288         1959 :       || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
    6289              :     return false;
    6290              : 
    6291              :   /* Decompose initial VEC_PERM_EXPRs.  */
    6292          112 :   tree v_in = gimple_assign_rhs1 (v_1_stmt);
    6293          112 :   tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
    6294          112 :   tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
    6295          112 :   if (v_in != gimple_assign_rhs2 (v_1_stmt)
    6296          107 :       || v_in != gimple_assign_rhs1 (v_2_stmt)
    6297          217 :       || v_in != gimple_assign_rhs2 (v_2_stmt))
    6298              :     return false;
    6299              : 
    6300          105 :   unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
    6301          105 :   if (TREE_CODE (v_1_sel) != VECTOR_CST
    6302          105 :       || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
    6303          105 :       || TREE_CODE (v_2_sel) != VECTOR_CST
    6304          210 :       || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
    6305              :     return false;
    6306              : 
    6307          105 :   if (nelts != v_1_nelts || nelts != v_2_nelts)
    6308              :     return false;
    6309              : 
    6310              :   /* Create the new selector.  */
    6311          105 :   vec_perm_builder new_sel_perm (nelts, nelts, 1);
    6312          105 :   auto_vec<bool> lanes (nelts);
    6313          105 :   lanes.quick_grow_cleared (nelts);
    6314          630 :   for (unsigned int i = 0; i < nelts; i++)
    6315              :     {
    6316              :       /* Extract the i-th value from the selector.  */
    6317          420 :       unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
    6318          420 :       unsigned int lane = sel_cst % nelts;
    6319          420 :       unsigned int offs = sel_cst / nelts;
    6320              : 
    6321              :       /* Check what's in the lane.  */
    6322          420 :       unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
    6323          420 :       unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
    6324              : 
    6325              :       /* Reuse previous lane (if any).  */
    6326          420 :       unsigned int l = 0;
    6327          711 :       for (; l < lane; l++)
    6328              :         {
    6329          489 :           if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
    6330          489 :               && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
    6331              :             break;
    6332              :         }
    6333              : 
    6334              :       /* Add to narrowed selector.  */
    6335          420 :       new_sel_perm.quick_push (l + offs * nelts);
    6336              : 
    6337              :       /* Mark lane as used.  */
    6338          420 :       lanes[l] = true;
    6339              :     }
    6340              : 
    6341              :   /* Count how many lanes are need.  */
    6342              :   unsigned int cnt = 0;
    6343          525 :   for (unsigned int i = 0; i < nelts; i++)
    6344          420 :     cnt += lanes[i];
    6345              : 
    6346              :   /* If more than (nelts/2) lanes are needed, skip the sequence.  */
    6347          105 :   if (cnt > nelts / 2)
    6348              :     return false;
    6349              : 
    6350              :   /* Check if the resulting permutation is cheap.  */
    6351          105 :   vec_perm_indices new_indices (new_sel_perm, 2, nelts);
    6352          105 :   tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
    6353          105 :   machine_mode vmode = TYPE_MODE (vectype);
    6354          105 :   if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
    6355              :     return false;
    6356              : 
    6357          105 :   *seq = XNEW (struct _vec_perm_simplify_seq);
    6358          105 :   (*seq)->stmt = stmt;
    6359          105 :   (*seq)->v_1_stmt = v_1_stmt;
    6360          105 :   (*seq)->v_2_stmt = v_2_stmt;
    6361          105 :   (*seq)->v_x_stmt = v_x_stmt;
    6362          105 :   (*seq)->v_y_stmt = v_y_stmt;
    6363          105 :   (*seq)->nelts = nelts;
    6364          105 :   (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
    6365              : 
    6366          105 :   if (dump_file)
    6367              :     {
    6368           32 :       fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
    6369           32 :       print_gimple_stmt (dump_file, stmt, 0);
    6370              : 
    6371           32 :       if (dump_flags & TDF_DETAILS)
    6372              :         {
    6373           32 :           fprintf (dump_file, "\tNarrowed vec_perm selector: ");
    6374           32 :           print_generic_expr (dump_file, (*seq)->new_sel);
    6375           32 :           fprintf (dump_file, "\n");
    6376              :         }
    6377              :     }
    6378              : 
    6379              :   return true;
    6380          210 : }
    6381              : 
    6382              : /* Reduce the lane consumption of a simplifiable vec perm sequence.  */
    6383              : 
    6384              : static void
    6385           78 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
    6386              : {
    6387           78 :   gassign *stmt = seq->stmt;
    6388           78 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6389              :     {
    6390           26 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6391           26 :       fprintf (dump_file, "Old stmt: ");
    6392           26 :       print_gimple_stmt (dump_file, stmt, 0);
    6393              :     }
    6394              : 
    6395              :   /* Update the last VEC_PERM statement.  */
    6396           78 :   gimple_assign_set_rhs3 (stmt, seq->new_sel);
    6397           78 :   update_stmt (stmt);
    6398              : 
    6399           78 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6400              :     {
    6401           26 :       fprintf (dump_file, "New stmt: ");
    6402           26 :       print_gimple_stmt (dump_file, stmt, 0);
    6403              :     }
    6404           78 : }
    6405              : 
    6406              : /* Test if we can blend two simplifiable vec permute sequences.
    6407              :    NEED_SWAP will be set, if sequences must be swapped for blending.  */
    6408              : 
    6409              : static bool
    6410           49 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
    6411              :                                     vec_perm_simplify_seq seq2,
    6412              :                                     bool *need_swap)
    6413              : {
    6414           49 :   unsigned int nelts = seq1->nelts;
    6415           49 :   basic_block bb = gimple_bb (seq1->stmt);
    6416              : 
    6417           49 :   gcc_assert (gimple_bb (seq2->stmt) == bb);
    6418              : 
    6419              :   /* BBs and number of elements must be equal.  */
    6420           49 :   if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
    6421              :     return false;
    6422              : 
    6423              :   /* We need vectors of the same type.  */
    6424           49 :   if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
    6425           49 :       != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
    6426              :     return false;
    6427              : 
    6428              :   /* We require isomorphic operators.  */
    6429           43 :   if (((gimple_assign_rhs_code (seq1->v_x_stmt)
    6430           43 :         != gimple_assign_rhs_code (seq2->v_x_stmt))
    6431           43 :        || (gimple_assign_rhs_code (seq1->v_y_stmt)
    6432           43 :            != gimple_assign_rhs_code (seq2->v_y_stmt))))
    6433              :     return false;
    6434              : 
    6435              :   /* We cannot have any dependencies between the sequences.
    6436              : 
    6437              :      For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
    6438              :      seq1's v_in is defined before these statements, but we need
    6439              :      to check if seq2's v_in is defined before them as well.
    6440              : 
    6441              :      Further, we will reuse seq2->stmt.  We need to ensure that
    6442              :      seq1->v_x_stmt and seq1->v_y_stmt are before it.
    6443              : 
    6444              :      Note, that we don't need to check the BBs here, because all
    6445              :      statements of both sequences have to be in the same BB.  */
    6446              : 
    6447           43 :   tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
    6448           43 :   if (TREE_CODE (seq2_v_in) != SSA_NAME)
    6449              :     return false;
    6450              : 
    6451           43 :   gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
    6452           43 :   if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
    6453           43 :       || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
    6454           39 :       || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
    6455           39 :       || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
    6456              :     {
    6457            4 :       tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
    6458            4 :       if (TREE_CODE (seq1_v_in) != SSA_NAME)
    6459              :         return false;
    6460              : 
    6461            4 :       gassign *seq1_v_in_stmt
    6462            4 :         = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
    6463              :       /* Let's try to see if we succeed when swapping the sequences.  */
    6464            4 :       if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
    6465            0 :           || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
    6466            0 :           || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
    6467            0 :           || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
    6468              :         return false;
    6469            0 :       *need_swap = true;
    6470              :     }
    6471              :   else
    6472           39 :     *need_swap = false;
    6473              : 
    6474           39 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6475           13 :     fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
    6476              : 
    6477              :   return true;
    6478              : }
    6479              : 
    6480              : /* Calculate the permutations for blending the two given vec permute
    6481              :    sequences.  This may fail if the resulting permutation is not
    6482              :    supported.  */
    6483              : 
    6484              : static bool
    6485           39 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
    6486              :                                   vec_perm_simplify_seq seq2,
    6487              :                                   vec_perm_indices *seq2_stmt_indices,
    6488              :                                   vec_perm_indices *seq1_v_1_stmt_indices,
    6489              :                                   vec_perm_indices *seq1_v_2_stmt_indices)
    6490              : {
    6491           39 :   unsigned int i;
    6492           39 :   unsigned int nelts = seq1->nelts;
    6493           39 :   auto_vec<unsigned int> lane_assignment;
    6494           39 :   lane_assignment.create (nelts);
    6495              : 
    6496              :   /* Mark all lanes as free.  */
    6497           39 :   lane_assignment.quick_grow_cleared (nelts);
    6498              : 
    6499              :   /* Allocate lanes for seq1.  */
    6500          234 :   for (i = 0; i < nelts; i++)
    6501              :     {
    6502          156 :       unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
    6503          156 :       l %= nelts;
    6504          156 :       lane_assignment[l] = 1;
    6505              :     }
    6506              : 
    6507              :   /* Allocate lanes for seq2 and calculate selector for seq2->stmt.  */
    6508           39 :   vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
    6509          195 :   for (i = 0; i < nelts; i++)
    6510              :     {
    6511          156 :       unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
    6512          156 :       unsigned int lane = sel % nelts;
    6513          156 :       unsigned int offs = sel / nelts;
    6514          156 :       unsigned int new_sel;
    6515              : 
    6516              :       /* Check if we already allocated the lane for seq2.  */
    6517          156 :       unsigned int j = 0;
    6518          279 :       for (; j < i; j++)
    6519              :         {
    6520          201 :           unsigned int sel_old;
    6521          201 :           sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
    6522          201 :           unsigned int lane_old = sel_old % nelts;
    6523          201 :           if (lane == lane_old)
    6524              :             {
    6525           78 :               new_sel = seq2_stmt_sel_perm[j].to_constant ();
    6526           78 :               new_sel = (new_sel % nelts) + offs * nelts;
    6527           78 :               break;
    6528              :             }
    6529              :         }
    6530              : 
    6531              :       /* If the lane is not allocated, we need to do that now.  */
    6532          156 :       if (j == i)
    6533              :         {
    6534              :           unsigned int l_orig = lane;
    6535          194 :           while (lane_assignment[lane] != 0)
    6536              :             {
    6537          116 :               lane = (lane + 1) % nelts;
    6538              : 
    6539              :               /* This should not happen if both sequences utilize no more than
    6540              :                  half of the lanes.  Test anyway to guarantee termination.  */
    6541          116 :               if (lane == l_orig)
    6542           39 :                 return false;
    6543              :             }
    6544              : 
    6545              :           /* Allocate lane.  */
    6546           78 :           lane_assignment[lane] = 2 + l_orig;
    6547           78 :           new_sel = lane + offs * nelts;
    6548              :         }
    6549              : 
    6550          156 :       seq2_stmt_sel_perm.quick_push (new_sel);
    6551              :     }
    6552              : 
    6553              :   /* Check if the resulting permutation is cheap.  */
    6554           39 :   seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
    6555           39 :   tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
    6556           39 :   machine_mode vmode = TYPE_MODE (vectype);
    6557           39 :   if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
    6558              :     return false;
    6559              : 
    6560              :   /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt.  */
    6561           39 :   vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
    6562           39 :   vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
    6563          195 :   for (i = 0; i < nelts; i++)
    6564              :     {
    6565          156 :       bool use_seq1 = lane_assignment[i] < 2;
    6566          156 :       unsigned int l1, l2;
    6567              : 
    6568          156 :       if (use_seq1)
    6569              :         {
    6570              :           /* Just reuse the selector indices.  */
    6571           78 :           tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
    6572           78 :           tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
    6573           78 :           l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
    6574           78 :           l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
    6575              :         }
    6576              :       else
    6577              :         {
    6578              :           /* We moved the lanes for seq2, so we need to adjust for that.  */
    6579           78 :           tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
    6580           78 :           tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
    6581           78 :           l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, lane_assignment[i] - 2));
    6582           78 :           l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, lane_assignment[i] - 2));
    6583              :         }
    6584              : 
    6585          156 :       l1 %= nelts;
    6586          156 :       l2 %= nelts;
    6587          234 :       seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
    6588          156 :       seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
    6589              :     }
    6590              : 
    6591           39 :   seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
    6592           39 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
    6593           39 :   vmode = TYPE_MODE (vectype);
    6594           39 :   if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
    6595              :     return false;
    6596              : 
    6597           39 :   seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
    6598           39 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
    6599           39 :   vmode = TYPE_MODE (vectype);
    6600           39 :   if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
    6601              :     return false;
    6602              : 
    6603              :   return true;
    6604           78 : }
    6605              : 
    6606              : /* Blend the two given simplifiable vec permute sequences using the
    6607              :    given permutations.  */
    6608              : 
    6609              : static void
    6610           39 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
    6611              :                               vec_perm_simplify_seq seq2,
    6612              :                               const vec_perm_indices &seq2_stmt_indices,
    6613              :                               const vec_perm_indices &seq1_v_1_stmt_indices,
    6614              :                               const vec_perm_indices &seq1_v_2_stmt_indices)
    6615              : {
    6616              :   /* We don't need to adjust seq1->stmt because its lanes consumption
    6617              :      was already narrowed before entering this function.  */
    6618              : 
    6619              :   /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel.  */
    6620           39 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6621              :     {
    6622           13 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6623           13 :       fprintf (dump_file, "Old stmt: ");
    6624           13 :       print_gimple_stmt (dump_file, seq2->stmt, 0);
    6625              :     }
    6626              : 
    6627           39 :   gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
    6628           78 :   gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
    6629           39 :   tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
    6630           39 :   tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
    6631           39 :   gimple_assign_set_rhs3 (seq2->stmt, sel);
    6632           39 :   update_stmt (seq2->stmt);
    6633              : 
    6634           39 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6635              :     {
    6636           13 :       fprintf (dump_file, "New stmt: ");
    6637           13 :       print_gimple_stmt (dump_file, seq2->stmt, 0);
    6638              :     }
    6639              : 
    6640              :   /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel.  */
    6641           39 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6642              :     {
    6643           13 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6644           13 :       fprintf (dump_file, "Old stmt: ");
    6645           13 :       print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
    6646              :     }
    6647              : 
    6648           39 :   gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
    6649           39 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
    6650           39 :   sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
    6651           39 :   gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
    6652           39 :   update_stmt (seq1->v_1_stmt);
    6653              : 
    6654           39 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6655              :     {
    6656           13 :       fprintf (dump_file, "New stmt: ");
    6657           13 :       print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
    6658              :     }
    6659              : 
    6660              :   /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel.  */
    6661           39 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6662              :     {
    6663           13 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6664           13 :       fprintf (dump_file, "Old stmt: ");
    6665           13 :       print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
    6666              :     }
    6667              : 
    6668           39 :   gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
    6669           39 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
    6670           39 :   sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
    6671           39 :   gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
    6672           39 :   update_stmt (seq1->v_2_stmt);
    6673              : 
    6674           39 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6675              :     {
    6676           13 :       fprintf (dump_file, "New stmt: ");
    6677           13 :       print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
    6678              :     }
    6679              : 
    6680              :   /* At this point, we have four unmodified seq2 stmts, which will be
    6681              :      eliminated by DCE.  */
    6682              : 
    6683           39 :   if (dump_file)
    6684           13 :     fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
    6685           39 : }
    6686              : 
    6687              : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
    6688              :    The provided list will be empty after this call.  */
    6689              : 
    6690              : static void
    6691    140027121 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
    6692              : {
    6693    140027121 :   unsigned int i, j;
    6694    140027121 :   vec_perm_simplify_seq seq1, seq2;
    6695              : 
    6696    140027121 :   if (l->is_empty ())
    6697    140027074 :     return;
    6698              : 
    6699           47 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6700           15 :     fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
    6701              :              l->length ());
    6702              : 
    6703          160 :   FOR_EACH_VEC_ELT (*l, i, seq1)
    6704              :     {
    6705           66 :       if (i + 1 < l->length ())
    6706              :         {
    6707          119 :           FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
    6708              :             {
    6709           49 :               bool swap = false;
    6710           49 :               if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
    6711              :                 {
    6712           39 :                   vec_perm_indices seq2_stmt_indices;
    6713           39 :                   vec_perm_indices seq1_v_1_stmt_indices;
    6714           39 :                   vec_perm_indices seq1_v_2_stmt_indices;
    6715          117 :                   if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
    6716              :                                                         swap ? seq1 : seq2,
    6717              :                                                         &seq2_stmt_indices,
    6718              :                                                         &seq1_v_1_stmt_indices,
    6719              :                                                         &seq1_v_2_stmt_indices))
    6720              :                     {
    6721              :                       /* Narrow lane usage.  */
    6722           39 :                       narrow_vec_perm_simplify_seq (seq1);
    6723           39 :                       narrow_vec_perm_simplify_seq (seq2);
    6724              : 
    6725              :                       /* Blend sequences.  */
    6726           39 :                       blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
    6727              :                                                     swap ? seq1 : seq2,
    6728              :                                                     seq2_stmt_indices,
    6729              :                                                     seq1_v_1_stmt_indices,
    6730              :                                                     seq1_v_2_stmt_indices);
    6731              : 
    6732              :                       /* We can use unordered_remove as we break the loop.  */
    6733           39 :                       l->unordered_remove (j);
    6734           39 :                       XDELETE (seq2);
    6735           39 :                       break;
    6736              :                     }
    6737           39 :                 }
    6738              :             }
    6739              :         }
    6740              : 
    6741              :       /* We don't need to call l->remove for seq1.  */
    6742           66 :       XDELETE (seq1);
    6743              :     }
    6744              : 
    6745           47 :   l->truncate (0);
    6746              : }
    6747              : 
    6748              : static void
    6749          105 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
    6750              :                                    const vec_perm_simplify_seq &seq)
    6751              : {
    6752              :   /* If no space on list left, then process the list.  */
    6753          105 :   if (!l->space (1))
    6754            0 :       process_vec_perm_simplify_seq_list (l);
    6755              : 
    6756          105 :   l->quick_push (seq);
    6757          105 : }
    6758              : 
    6759              : /* Main entry point for the forward propagation and statement combine
    6760              :    optimizer.  */
    6761              : 
    6762              : namespace {
    6763              : 
    6764              : const pass_data pass_data_forwprop =
    6765              : {
    6766              :   GIMPLE_PASS, /* type */
    6767              :   "forwprop", /* name */
    6768              :   OPTGROUP_NONE, /* optinfo_flags */
    6769              :   TV_TREE_FORWPROP, /* tv_id */
    6770              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    6771              :   0, /* properties_provided */
    6772              :   0, /* properties_destroyed */
    6773              :   0, /* todo_flags_start */
    6774              :   0, /* todo_flags_finish */
    6775              : };
    6776              : 
    6777              : class pass_forwprop : public gimple_opt_pass
    6778              : {
    6779              : public:
    6780      1514925 :   pass_forwprop (gcc::context *ctxt)
    6781      3029850 :     : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
    6782              :   {}
    6783              : 
    6784              :   /* opt_pass methods: */
    6785      1211940 :   opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
    6786      1817910 :   void set_pass_param (unsigned int n, bool param) final override
    6787              :     {
    6788      1817910 :       switch (n)
    6789              :         {
    6790      1211940 :           case 0:
    6791      1211940 :             m_full_walk = param;
    6792      1211940 :             break;
    6793       605970 :           case 1:
    6794       605970 :             last_p = param;
    6795       605970 :             break;
    6796            0 :           default:
    6797            0 :           gcc_unreachable();
    6798              :         }
    6799      1817910 :     }
    6800      5734569 :   bool gate (function *) final override { return flag_tree_forwprop; }
    6801              :   unsigned int execute (function *) final override;
    6802              : 
    6803              :  private:
    6804              :   /* Determines whether the pass instance should set PROP_last_full_fold.  */
    6805              :   bool last_p;
    6806              : 
    6807              :   /* True if the aggregate props are doing a full walk or not.  */
    6808              :   bool m_full_walk = false;
    6809              : }; // class pass_forwprop
    6810              : 
    6811              : /* Attempt to make the BB block of __builtin_unreachable unreachable by changing
    6812              :    the incoming jumps.  Return true if at least one jump was changed.  */
    6813              : 
    6814              : static bool
    6815         1257 : optimize_unreachable (basic_block bb)
    6816              : {
    6817         1257 :   gimple_stmt_iterator gsi;
    6818         1257 :   gimple *stmt;
    6819         1257 :   edge_iterator ei;
    6820         1257 :   edge e;
    6821         1257 :   bool ret;
    6822              : 
    6823         1257 :   ret = false;
    6824         2612 :   FOR_EACH_EDGE (e, ei, bb->preds)
    6825              :     {
    6826         1355 :       gsi = gsi_last_bb (e->src);
    6827         1355 :       if (gsi_end_p (gsi))
    6828          327 :         continue;
    6829              : 
    6830         1028 :       stmt = gsi_stmt (gsi);
    6831         1028 :       if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
    6832              :         {
    6833              :           /* If the condition is already true/false
    6834              :              ignore it. This can happen during copy prop of forwprop. */
    6835          720 :           if (gimple_cond_true_p (cond_stmt)
    6836          712 :               || gimple_cond_false_p (cond_stmt))
    6837            8 :             continue;
    6838          704 :           else if (e->flags & EDGE_TRUE_VALUE)
    6839          609 :             gimple_cond_make_false (cond_stmt);
    6840           95 :           else if (e->flags & EDGE_FALSE_VALUE)
    6841           95 :             gimple_cond_make_true (cond_stmt);
    6842              :           else
    6843            0 :             gcc_unreachable ();
    6844          704 :           update_stmt (cond_stmt);
    6845              :         }
    6846              :       else
    6847              :         {
    6848              :           /* Todo: handle other cases.  e.g. switch.  */
    6849          316 :           continue;
    6850              :         }
    6851              : 
    6852          704 :       ret = true;
    6853              :     }
    6854              : 
    6855         1257 :   return ret;
    6856              : }
    6857              : 
    6858              : unsigned int
    6859      5731602 : pass_forwprop::execute (function *fun)
    6860              : {
    6861      5731602 :   unsigned int todoflags = 0;
    6862              :   /* Handle a full walk only when expensive optimizations are on.  */
    6863      5731602 :   bool full_walk = m_full_walk && flag_expensive_optimizations;
    6864              : 
    6865      5731602 :   cfg_changed = false;
    6866      5731602 :   if (last_p)
    6867      1070164 :     fun->curr_properties |= PROP_last_full_fold;
    6868              : 
    6869      5731602 :   calculate_dominance_info (CDI_DOMINATORS);
    6870              : 
    6871              :   /* Combine stmts with the stmts defining their operands.  Do that
    6872              :      in an order that guarantees visiting SSA defs before SSA uses.  */
    6873     11463204 :   lattice.create (num_ssa_names);
    6874     11463204 :   lattice.quick_grow_cleared (num_ssa_names);
    6875      5731602 :   int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
    6876      5731602 :   int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
    6877              :                                                          postorder, false);
    6878      5731602 :   int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
    6879     57672149 :   for (int i = 0; i < postorder_num; ++i)
    6880              :     {
    6881     46208945 :       bb_to_rpo[postorder[i]] = i;
    6882     46208945 :       edge_iterator ei;
    6883     46208945 :       edge e;
    6884    111240247 :       FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
    6885     65031302 :         e->flags &= ~EDGE_EXECUTABLE;
    6886              :     }
    6887      5731602 :   single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
    6888      5731602 :     |= EDGE_EXECUTABLE;
    6889      5731602 :   auto_vec<gimple *, 4> to_fixup;
    6890      5731602 :   auto_vec<gimple *, 32> to_remove;
    6891      5731602 :   auto_vec<unsigned, 32> to_remove_defs;
    6892      5731602 :   auto_vec<std::pair<int, int>, 10> edges_to_remove;
    6893      5731602 :   auto_bitmap simple_dce_worklist;
    6894      5731602 :   auto_bitmap need_ab_cleanup;
    6895      5731602 :   to_purge = BITMAP_ALLOC (NULL);
    6896      5731602 :   auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
    6897     51940547 :   for (int i = 0; i < postorder_num; ++i)
    6898              :     {
    6899     46208945 :       gimple_stmt_iterator gsi;
    6900     46208945 :       basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
    6901     46208945 :       edge_iterator ei;
    6902     46208945 :       edge e;
    6903              : 
    6904              :       /* Skip processing not executable blocks.  We could improve
    6905              :          single_use tracking by at least unlinking uses from unreachable
    6906              :          blocks but since blocks with uses are not processed in a
    6907              :          meaningful order this is probably not worth it.  */
    6908     46208945 :       bool any = false;
    6909     47436583 :       FOR_EACH_EDGE (e, ei, bb->preds)
    6910              :         {
    6911     47409736 :           if ((e->flags & EDGE_EXECUTABLE)
    6912              :               /* We can handle backedges in natural loops correctly but
    6913              :                  for irreducible regions we have to take all backedges
    6914              :                  conservatively when we did not visit the source yet.  */
    6915     47409736 :               || (bb_to_rpo[e->src->index] > i
    6916       737882 :                   && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
    6917              :             {
    6918              :               any = true;
    6919              :               break;
    6920              :             }
    6921              :         }
    6922     46208945 :       if (!any)
    6923        27510 :         continue;
    6924              : 
    6925              :       /* Remove conditions that go directly to unreachable when this is the last forwprop.  */
    6926     46182098 :       if (last_p
    6927     10180090 :           && !(flag_sanitize & SANITIZE_UNREACHABLE))
    6928              :         {
    6929     10175142 :           gimple_stmt_iterator gsi;
    6930     10175142 :           gsi = gsi_start_nondebug_after_labels_bb (bb);
    6931     10175805 :           if (!gsi_end_p (gsi)
    6932      9211674 :               && gimple_call_builtin_p (*gsi, BUILT_IN_UNREACHABLE)
    6933     10176399 :               && optimize_unreachable (bb))
    6934              :             {
    6935          663 :               cfg_changed = true;
    6936          663 :               continue;
    6937              :             }
    6938              :         }
    6939              : 
    6940              :       /* Fold PHI-form long-multiply carries and record degenerate
    6941              :          PHIs in the lattice.  Iterator advanced up front so a folded
    6942              :          PHI can be removed in-flight; a long-mul carry PHI is never
    6943              :          degenerate, so the two cases are disjoint.  */
    6944     62605679 :       for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);)
    6945              :         {
    6946     16424244 :           gphi *phi = si.phi ();
    6947     16424244 :           gsi_next (&si);
    6948     16424244 :           tree res = gimple_phi_result (phi);
    6949     32848488 :           if (virtual_operand_p (res))
    6950      7479681 :             continue;
    6951      8944578 :           if (match_long_mul_phi (phi))
    6952           15 :             continue;
    6953              : 
    6954      8944563 :           tree first = NULL_TREE;
    6955      8944563 :           bool all_same = true;
    6956      8944563 :           edge_iterator ei;
    6957      8944563 :           edge e;
    6958     18406836 :           FOR_EACH_EDGE (e, ei, bb->preds)
    6959              :             {
    6960              :               /* Ignore not executable forward edges.  */
    6961     18151966 :               if (!(e->flags & EDGE_EXECUTABLE))
    6962              :                 {
    6963      4243126 :                   if (bb_to_rpo[e->src->index] < i)
    6964        17273 :                     continue;
    6965              :                   /* Avoid equivalences from backedges - while we might
    6966              :                      be able to make irreducible regions reducible and
    6967              :                      thus turning a back into a forward edge we do not
    6968              :                      want to deal with the intermediate SSA issues that
    6969              :                      exposes.  */
    6970              :                   all_same = false;
    6971              :                 }
    6972     18134693 :               tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
    6973     18134693 :               if (use == res)
    6974              :                 /* The PHI result can also appear on a backedge, if so
    6975              :                    we can ignore this case for the purpose of determining
    6976              :                    the singular value.  */
    6977              :                 ;
    6978     18128824 :               else if (! first)
    6979              :                 first = use;
    6980      9184261 :               else if (! operand_equal_p (first, use, 0))
    6981              :                 {
    6982              :                   all_same = false;
    6983              :                   break;
    6984              :                 }
    6985              :             }
    6986      8944563 :           if (all_same)
    6987              :             {
    6988       249033 :               if (may_propagate_copy (res, first))
    6989       248335 :                 to_remove_defs.safe_push (SSA_NAME_VERSION (res));
    6990       249033 :               fwprop_set_lattice_val (res, first);
    6991              :             }
    6992              :         }
    6993              : 
    6994              :       /* Apply forward propagation to all stmts in the basic-block.
    6995              :          Note we update GSI within the loop as necessary.  */
    6996     46181435 :       unsigned int uid = 1;
    6997    450290037 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
    6998              :         {
    6999    357927167 :           gimple *stmt = gsi_stmt (gsi);
    7000    357927167 :           tree lhs, rhs;
    7001    357927167 :           enum tree_code code;
    7002              : 
    7003    357927167 :           if (is_gimple_debug (stmt))
    7004              :             {
    7005    195043798 :               gsi_next (&gsi);
    7006    195043798 :               continue;
    7007              :             }
    7008              : 
    7009    162883369 :           gimple_set_uid (stmt, uid++);
    7010              : 
    7011    162883369 :           gassign *assign = dyn_cast<gassign *> (stmt);
    7012    216507211 :           if (!assign)
    7013              :             {
    7014     53623842 :               process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    7015     53623842 :               gsi_next (&gsi);
    7016     53623842 :               continue;
    7017              :             }
    7018              : 
    7019    109259527 :           lhs = gimple_assign_lhs (assign);
    7020    109259527 :           rhs = gimple_assign_rhs1 (assign);
    7021    109259527 :           code = gimple_assign_rhs_code (assign);
    7022              : 
    7023    149481371 :           if (TREE_CODE (lhs) != SSA_NAME
    7024    109259527 :               || has_zero_uses (lhs))
    7025              :             {
    7026     40221844 :               process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    7027     40221844 :               gsi_next (&gsi);
    7028     40221844 :               continue;
    7029              :             }
    7030              : 
    7031              :           /* If this statement sets an SSA_NAME to an address,
    7032              :              try to propagate the address into the uses of the SSA_NAME.  */
    7033     69037683 :           if ((code == ADDR_EXPR
    7034              :                /* Handle pointer conversions on invariant addresses
    7035              :                   as well, as this is valid gimple.  */
    7036     66709017 :                || (CONVERT_EXPR_CODE_P (code)
    7037      9258566 :                    && TREE_CODE (rhs) == ADDR_EXPR
    7038       365575 :                    && POINTER_TYPE_P (TREE_TYPE (lhs))))
    7039     69037907 :               && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
    7040              :             {
    7041      2328212 :               tree base = get_base_address (TREE_OPERAND (rhs, 0));
    7042      2328212 :               if ((!base
    7043      2328212 :                    || !DECL_P (base)
    7044       132411 :                    || decl_address_invariant_p (base))
    7045      2328212 :                   && !stmt_references_abnormal_ssa_name (stmt)
    7046      4656408 :                   && forward_propagate_addr_expr (lhs, rhs, true))
    7047              :                 {
    7048       476187 :                   fwprop_invalidate_lattice (gimple_get_lhs (stmt));
    7049       476187 :                   release_defs (stmt);
    7050       476187 :                   gsi_remove (&gsi, true);
    7051              :                 }
    7052              :               else
    7053      1852025 :                 gsi_next (&gsi);
    7054              :             }
    7055     66709471 :           else if (code == POINTER_PLUS_EXPR)
    7056              :             {
    7057      3703331 :               tree off = gimple_assign_rhs2 (stmt);
    7058      3703331 :               if (TREE_CODE (off) == INTEGER_CST
    7059      1140609 :                   && can_propagate_from (stmt)
    7060      1140258 :                   && !simple_iv_increment_p (stmt)
    7061              :                   /* ???  Better adjust the interface to that function
    7062              :                      instead of building new trees here.  */
    7063      4545039 :                   && forward_propagate_addr_expr
    7064      2525124 :                        (lhs,
    7065              :                         build1_loc (gimple_location (stmt),
    7066       841708 :                                     ADDR_EXPR, TREE_TYPE (rhs),
    7067       841708 :                                     fold_build2 (MEM_REF,
    7068              :                                                  TREE_TYPE (TREE_TYPE (rhs)),
    7069              :                                                  rhs,
    7070              :                                                  fold_convert (ptr_type_node,
    7071              :                                                                off))), true))
    7072              :                 {
    7073       321372 :                   fwprop_invalidate_lattice (gimple_get_lhs (stmt));
    7074       321372 :                   release_defs (stmt);
    7075       321372 :                   gsi_remove (&gsi, true);
    7076              :                 }
    7077      3381959 :               else if (is_gimple_min_invariant (rhs))
    7078              :                 {
    7079              :                   /* Make sure to fold &a[0] + off_1 here.  */
    7080       417249 :                   fold_stmt_inplace (&gsi);
    7081       417249 :                   update_stmt (stmt);
    7082       417249 :                   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
    7083       417225 :                     gsi_next (&gsi);
    7084              :                 }
    7085              :               else
    7086      2964710 :                 gsi_next (&gsi);
    7087              :             }
    7088     63006140 :           else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
    7089       215114 :                    && gimple_assign_load_p (stmt)
    7090       135417 :                    && !gimple_has_volatile_ops (stmt)
    7091        41342 :                    && TREE_CODE (rhs) != TARGET_MEM_REF
    7092        41264 :                    && TREE_CODE (rhs) != BIT_FIELD_REF
    7093     63047400 :                    && !stmt_can_throw_internal (fun, stmt))
    7094              :             {
    7095              :               /* Rewrite loads used only in real/imagpart extractions to
    7096              :                  component-wise loads.  */
    7097        41142 :               use_operand_p use_p;
    7098        41142 :               imm_use_iterator iter;
    7099        41142 :               tree vuse = gimple_vuse (stmt);
    7100        41142 :               bool rewrite = true;
    7101        46329 :               FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
    7102              :                 {
    7103        44202 :                   gimple *use_stmt = USE_STMT (use_p);
    7104        44202 :                   if (is_gimple_debug (use_stmt))
    7105         1016 :                     continue;
    7106        43186 :                   if (!is_gimple_assign (use_stmt)
    7107        28445 :                       || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
    7108        26332 :                           && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
    7109         4182 :                       || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs
    7110              :                       /* We are moving the def, so avoid abnormals.  */
    7111        47368 :                       || SSA_NAME_OCCURS_IN_ABNORMAL_PHI
    7112              :                            (gimple_assign_lhs (use_stmt)))
    7113              :                     {
    7114              :                       rewrite = false;
    7115              :                       break;
    7116              :                     }
    7117        41142 :                 }
    7118        41142 :               if (rewrite)
    7119              :                 {
    7120         2127 :                   gimple *use_stmt;
    7121         6758 :                   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    7122              :                     {
    7123         4631 :                       if (is_gimple_debug (use_stmt))
    7124              :                         {
    7125          493 :                           if (gimple_debug_bind_p (use_stmt))
    7126              :                             {
    7127          493 :                               gimple_debug_bind_reset_value (use_stmt);
    7128          493 :                               update_stmt (use_stmt);
    7129              :                             }
    7130          493 :                           continue;
    7131              :                         }
    7132              : 
    7133         8276 :                       tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
    7134         4138 :                                              TREE_TYPE (TREE_TYPE (rhs)),
    7135              :                                              unshare_expr (rhs));
    7136         4138 :                       gimple *new_stmt
    7137         4138 :                         = gimple_build_assign (gimple_assign_lhs (use_stmt),
    7138              :                                                new_rhs);
    7139              : 
    7140         4138 :                       location_t loc = gimple_location (use_stmt);
    7141         4138 :                       gimple_set_location (new_stmt, loc);
    7142         4138 :                       gimple_set_vuse (new_stmt, vuse);
    7143         4138 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    7144         4138 :                       unlink_stmt_vdef (use_stmt);
    7145         4138 :                       gsi_remove (&gsi2, true);
    7146              : 
    7147         4138 :                       gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
    7148         2127 :                     }
    7149              : 
    7150         2127 :                   release_defs (stmt);
    7151         2127 :                   gsi_remove (&gsi, true);
    7152              :                 }
    7153              :               else
    7154        39015 :                 gsi_next (&gsi);
    7155              :             }
    7156     62964998 :           else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
    7157      1831043 :                    && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
    7158              :                        /* After vector lowering rewrite all loads, but
    7159              :                           initially do not since this conflicts with
    7160              :                           vector CONSTRUCTOR to shuffle optimization.  */
    7161      1803199 :                        || (fun->curr_properties & PROP_gimple_lvec))
    7162       936352 :                    && gimple_assign_load_p (stmt)
    7163       301379 :                    && !gimple_has_volatile_ops (stmt)
    7164       286665 :                    && !stmt_can_throw_internal (fun, stmt)
    7165     63251663 :                    && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
    7166       286162 :             optimize_vector_load (&gsi);
    7167              : 
    7168     62678836 :           else if (code == COMPLEX_EXPR)
    7169              :             {
    7170              :               /* Rewrite stores of a single-use complex build expression
    7171              :                  to component-wise stores.  */
    7172        38022 :               use_operand_p use_p;
    7173        38022 :               gimple *use_stmt, *def1, *def2;
    7174        38022 :               tree rhs2;
    7175        38022 :               gassign *assign;
    7176        38022 :               if (single_imm_use (lhs, &use_p, &use_stmt)
    7177        35855 :                   && gimple_store_p (use_stmt)
    7178        42104 :                   && !gimple_has_volatile_ops (use_stmt)
    7179         3136 :                   && (assign = dyn_cast<gassign *> (use_stmt))
    7180         3133 :                   && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (assign)))
    7181              :                       == COMPLEX_TYPE)
    7182        41150 :                   && (TREE_CODE (gimple_assign_lhs (assign))
    7183              :                       != TARGET_MEM_REF))
    7184              :                 {
    7185         3124 :                   tree use_lhs = gimple_assign_lhs (assign);
    7186         3124 :                   if (auto_var_p (use_lhs))
    7187          601 :                     DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
    7188         6248 :                   tree new_lhs = build1 (REALPART_EXPR,
    7189         3124 :                                          TREE_TYPE (TREE_TYPE (use_lhs)),
    7190              :                                          unshare_expr (use_lhs));
    7191         3124 :                   gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
    7192         3124 :                   location_t loc = gimple_location (use_stmt);
    7193         3124 :                   gimple_set_location (new_stmt, loc);
    7194         6248 :                   gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
    7195         3124 :                   gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
    7196         6248 :                   SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
    7197         6248 :                   gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
    7198         3124 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    7199         3124 :                   gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
    7200              : 
    7201         6248 :                   new_lhs = build1 (IMAGPART_EXPR,
    7202         3124 :                                     TREE_TYPE (TREE_TYPE (use_lhs)),
    7203              :                                     unshare_expr (use_lhs));
    7204         3124 :                   gimple_assign_set_lhs (assign, new_lhs);
    7205         3124 :                   gimple_assign_set_rhs1 (assign, gimple_assign_rhs2 (stmt));
    7206         3124 :                   update_stmt (use_stmt);
    7207              : 
    7208         3124 :                   release_defs (stmt);
    7209         3124 :                   gsi_remove (&gsi, true);
    7210              :                 }
    7211              :               /* Rewrite a component-wise load of a complex to a complex
    7212              :                  load if the components are not used separately.  */
    7213        34898 :               else if (TREE_CODE (rhs) == SSA_NAME
    7214        34428 :                        && has_single_use (rhs)
    7215        30913 :                        && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
    7216        30913 :                        && TREE_CODE (rhs2) == SSA_NAME
    7217        29090 :                        && has_single_use (rhs2)
    7218        28653 :                        && (def1 = SSA_NAME_DEF_STMT (rhs),
    7219        28653 :                            gimple_assign_load_p (def1))
    7220         1145 :                        && (def2 = SSA_NAME_DEF_STMT (rhs2),
    7221         1145 :                            gimple_assign_load_p (def2))
    7222         1700 :                        && (gimple_vuse (def1) == gimple_vuse (def2))
    7223          847 :                        && !gimple_has_volatile_ops (def1)
    7224          847 :                        && !gimple_has_volatile_ops (def2)
    7225          847 :                        && !stmt_can_throw_internal (fun, def1)
    7226          847 :                        && !stmt_can_throw_internal (fun, def2)
    7227          847 :                        && gimple_assign_rhs_code (def1) == REALPART_EXPR
    7228          601 :                        && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
    7229        35499 :                        && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
    7230              :                                                                  (def1), 0),
    7231          601 :                                            TREE_OPERAND (gimple_assign_rhs1
    7232              :                                                                  (def2), 0)))
    7233              :                 {
    7234          601 :                   tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
    7235          601 :                   gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
    7236          601 :                   gcc_assert (gsi_stmt (gsi) == stmt);
    7237         1202 :                   gimple_set_vuse (stmt, gimple_vuse (def1));
    7238          601 :                   gimple_set_modified (stmt, true);
    7239          601 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
    7240          601 :                   gsi_remove (&gsi, false);
    7241          601 :                   gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
    7242              :                 }
    7243              :               else
    7244        34297 :                 gsi_next (&gsi);
    7245              :             }
    7246     62640814 :           else if (code == CONSTRUCTOR
    7247       170840 :                    && VECTOR_TYPE_P (TREE_TYPE (rhs))
    7248       170840 :                    && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
    7249         4745 :                    && CONSTRUCTOR_NELTS (rhs) > 0
    7250     62645559 :                    && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
    7251         2104 :                        || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
    7252              :                            != BLKmode)))
    7253              :             {
    7254              :               /* Rewrite stores of a single-use vector constructors
    7255              :                  to component-wise stores if the mode isn't supported.  */
    7256         4388 :               use_operand_p use_p;
    7257         4388 :               gimple *use_stmt;
    7258         4388 :               if (single_imm_use (lhs, &use_p, &use_stmt)
    7259         3912 :                   && gimple_store_p (use_stmt)
    7260         3428 :                   && !gimple_has_volatile_ops (use_stmt)
    7261         1708 :                   && !stmt_can_throw_internal (fun, use_stmt)
    7262         6089 :                   && is_gimple_assign (use_stmt))
    7263              :                 {
    7264         1701 :                   tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
    7265         1701 :                   unsigned HOST_WIDE_INT elt_w
    7266         1701 :                     = tree_to_uhwi (TYPE_SIZE (elt_t));
    7267         1701 :                   unsigned HOST_WIDE_INT n
    7268         1701 :                     = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
    7269         1701 :                   tree use_lhs = gimple_assign_lhs (use_stmt);
    7270         1701 :                   if (auto_var_p (use_lhs))
    7271          575 :                     DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
    7272         1126 :                   else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
    7273              :                     {
    7274            1 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    7275            1 :                       use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
    7276              :                     }
    7277        43684 :                   for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
    7278              :                     {
    7279        41983 :                       unsigned HOST_WIDE_INT ci = bi / elt_w;
    7280        41983 :                       tree new_rhs;
    7281        41983 :                       if (ci < CONSTRUCTOR_NELTS (rhs))
    7282        41365 :                         new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
    7283              :                       else
    7284          618 :                         new_rhs = build_zero_cst (elt_t);
    7285        41983 :                       tree new_lhs = build3 (BIT_FIELD_REF,
    7286              :                                              elt_t,
    7287              :                                              unshare_expr (use_lhs),
    7288        41983 :                                              bitsize_int (elt_w),
    7289        41983 :                                              bitsize_int (bi));
    7290        41983 :                       gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
    7291        41983 :                       location_t loc = gimple_location (use_stmt);
    7292        41983 :                       gimple_set_location (new_stmt, loc);
    7293        83966 :                       gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
    7294        41983 :                       gimple_set_vdef (new_stmt,
    7295              :                                        make_ssa_name (gimple_vop (fun)));
    7296        83966 :                       SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
    7297        83966 :                       gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
    7298        41983 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    7299        41983 :                       gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
    7300              :                     }
    7301         1701 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    7302         1701 :                   unlink_stmt_vdef (use_stmt);
    7303         1701 :                   release_defs (use_stmt);
    7304         1701 :                   gsi_remove (&gsi2, true);
    7305         1701 :                   release_defs (stmt);
    7306         1701 :                   gsi_remove (&gsi, true);
    7307              :                 }
    7308              :               else
    7309         2687 :                 gsi_next (&gsi);
    7310              :             }
    7311     62636426 :           else if (code == VEC_PERM_EXPR)
    7312              :             {
    7313              :               /* Find vectorized sequences, where we can reduce the lane
    7314              :                  utilization.  The narrowing will be donw later and only
    7315              :                  if we find a pair of sequences that can be blended.  */
    7316       191322 :               gassign *assign = dyn_cast <gassign *> (stmt);
    7317       191322 :               vec_perm_simplify_seq seq;
    7318       191322 :               if (recognise_vec_perm_simplify_seq (assign, &seq))
    7319          105 :                 append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
    7320              :                                                    seq);
    7321              : 
    7322       191322 :               gsi_next (&gsi);
    7323              :           }
    7324              :           else
    7325     62445104 :             gsi_next (&gsi);
    7326              :         }
    7327              : 
    7328     46181435 :       process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    7329              : 
    7330              :       /* Combine stmts with the stmts defining their operands.
    7331              :          Note we update GSI within the loop as necessary.  */
    7332    449958889 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    7333              :         {
    7334    357596019 :           gimple *stmt = gsi_stmt (gsi);
    7335              : 
    7336              :           /* Mark stmt as potentially needing revisiting.  */
    7337    357596019 :           gimple_set_plf (stmt, GF_PLF_1, false);
    7338              : 
    7339    357596019 :           bool can_make_abnormal_goto = (is_gimple_call (stmt)
    7340    357596019 :                                          && stmt_can_make_abnormal_goto (stmt));
    7341              : 
    7342              :           /* Substitute from our lattice.  We need to do so only once.  */
    7343    357596019 :           bool substituted_p = false;
    7344    357596019 :           use_operand_p usep;
    7345    357596019 :           ssa_op_iter iter;
    7346    524910603 :           FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
    7347              :             {
    7348    167314584 :               tree use = USE_FROM_PTR (usep);
    7349    167314584 :               tree val = fwprop_ssa_val (use);
    7350    167314584 :               if (val && val != use)
    7351              :                 {
    7352      1972977 :                   if (!is_gimple_debug (stmt))
    7353      1641892 :                     bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
    7354      1972977 :                   if (may_propagate_copy (use, val))
    7355              :                     {
    7356      1969522 :                       propagate_value (usep, val);
    7357      1969522 :                       substituted_p = true;
    7358              :                     }
    7359              :                 }
    7360              :             }
    7361    357596019 :           if (substituted_p)
    7362      1914307 :             update_stmt (stmt);
    7363    357596019 :           auto assign = dyn_cast<gassign *> (stmt);
    7364    357596019 :           if (substituted_p
    7365    357596019 :               && assign
    7366    358760272 :               && gimple_assign_rhs_code (assign) == ADDR_EXPR)
    7367        21058 :             recompute_tree_invariant_for_addr_expr
    7368        21058 :               (gimple_assign_rhs1 (assign));
    7369    357596019 :           if (substituted_p
    7370    357596019 :               && can_make_abnormal_goto
    7371    357596019 :               && !stmt_can_make_abnormal_goto (stmt))
    7372            3 :             bitmap_set_bit (need_ab_cleanup, bb->index);
    7373              : 
    7374    360542634 :           bool changed;
    7375    721085268 :           do
    7376              :             {
    7377    360542634 :               gimple *orig_stmt = stmt = gsi_stmt (gsi);
    7378    360542634 :               bool was_call = is_gimple_call (stmt);
    7379    360542634 :               bool was_noreturn = (was_call
    7380    360542634 :                                    && gimple_call_noreturn_p (stmt));
    7381    360542634 :               changed = false;
    7382              : 
    7383    360542634 :               auto_vec<tree, 8> uses;
    7384    530930502 :               FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
    7385    340381020 :                 if (uses.space (1))
    7386    169993152 :                   uses.quick_push (USE_FROM_PTR (usep));
    7387              : 
    7388    360542634 :               if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
    7389              :                 {
    7390      2630456 :                   changed = true;
    7391              :                   /* There is no updating of the address
    7392              :                      taken after the last forwprop so update
    7393              :                      the addresses when a folding happened to a call.
    7394              :                      The va_* builtins can remove taking of the address so
    7395              :                      can the sincos->cexpi transformation.  See PR 39643 and PR 20983. */
    7396      2630456 :                   if (was_call && last_p)
    7397      2630456 :                     todoflags |= TODO_update_address_taken;
    7398      2630456 :                   stmt = gsi_stmt (gsi);
    7399              :                   /* Cleanup the CFG if we simplified a condition to
    7400              :                      true or false.  */
    7401      2630456 :                   if (gcond *cond = dyn_cast <gcond *> (stmt))
    7402      1051505 :                     if (gimple_cond_true_p (cond)
    7403      1051505 :                         || gimple_cond_false_p (cond))
    7404        30943 :                       cfg_changed = true;
    7405              :                   /* Queue old uses for simple DCE if not debug statement.  */
    7406      2630456 :                   if (!is_gimple_debug (stmt))
    7407     11093431 :                     for (tree use : uses)
    7408      3222406 :                       if (TREE_CODE (use) == SSA_NAME
    7409      3222406 :                           && !SSA_NAME_IS_DEFAULT_DEF (use))
    7410      3022955 :                         bitmap_set_bit (simple_dce_worklist,
    7411      3022955 :                                         SSA_NAME_VERSION (use));
    7412      2630456 :                   update_stmt (stmt);
    7413              :                 }
    7414              : 
    7415    360542634 :               switch (gimple_code (stmt))
    7416              :                 {
    7417    110345280 :                 case GIMPLE_ASSIGN:
    7418    110345280 :                   {
    7419    110345280 :                     tree rhs1 = gimple_assign_rhs1 (stmt);
    7420    110345280 :                     enum tree_code code = gimple_assign_rhs_code (stmt);
    7421    110345280 :                     if (gimple_clobber_p (stmt))
    7422      7522876 :                       do_simple_agr_dse (as_a<gassign*>(stmt), full_walk);
    7423    102822404 :                     else if (gimple_store_p (stmt))
    7424              :                       {
    7425     32062119 :                         optimize_aggr_zeroprop (stmt, full_walk);
    7426     32062119 :                         if (gimple_assign_load_p (stmt))
    7427      3930044 :                           optimize_agr_copyprop (stmt);
    7428              :                       }
    7429     70760285 :                     else if (TREE_CODE_CLASS (code) == tcc_comparison)
    7430      2664298 :                       changed |= forward_propagate_into_comparison (&gsi);
    7431     68095987 :                     else if ((code == PLUS_EXPR || code == BIT_IOR_EXPR))
    7432              :                       {
    7433     10367115 :                         bool folded = match_long_mul (as_a <gassign *> (stmt));
    7434     10367115 :                         if (!folded)
    7435     10365171 :                           folded = simplify_rotate (&gsi);
    7436     10367115 :                         changed |= folded;
    7437              :                       }
    7438              :                     else if (code == BIT_XOR_EXPR)
    7439       151081 :                       changed |= simplify_rotate (&gsi);
    7440              :                     else if (code == VEC_PERM_EXPR)
    7441       194057 :                       changed |= simplify_permutation (&gsi);
    7442              :                     else if (code == CONSTRUCTOR
    7443       168476 :                              && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
    7444       168476 :                       changed |= simplify_vector_constructor (&gsi);
    7445     57215258 :                     else if (code == BIT_INSERT_EXPR)
    7446         2758 :                       changed |= simplify_bit_insert_chain (&gsi,
    7447              :                                                            simple_dce_worklist);
    7448     57212500 :                     else if (code == ARRAY_REF)
    7449      2046494 :                       changed |= simplify_count_zeroes (&gsi);
    7450     55166006 :                     else if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
    7451     12809780 :                              && TREE_CODE (
    7452              :                                     gimple_assign_rhs2 (stmt)) == SSA_NAME
    7453      6790504 :                              && has_single_use (gimple_assign_rhs2 (stmt))
    7454     59335593 :                              && is_a<gphi*> (SSA_NAME_DEF_STMT (
    7455              :                                                 gimple_assign_rhs2 (stmt))))
    7456       139879 :                       changed |= simplify_phi_result_movdiv (stmt, code);
    7457              :                     break;
    7458              :                   }
    7459              : 
    7460       111627 :                 case GIMPLE_SWITCH:
    7461       111627 :                   changed |= simplify_gimple_switch (as_a <gswitch *> (stmt),
    7462              :                                                      edges_to_remove,
    7463              :                                                      simple_dce_worklist);
    7464       111627 :                   break;
    7465              : 
    7466     19881624 :                 case GIMPLE_COND:
    7467     19881624 :                   {
    7468     19881624 :                     int did_something = forward_propagate_into_gimple_cond
    7469     19881624 :                                                         (as_a <gcond *> (stmt));
    7470     19881624 :                     if (did_something == 2)
    7471         1619 :                       cfg_changed = true;
    7472     19881624 :                     changed |= did_something != 0;
    7473     19881624 :                     break;
    7474              :                   }
    7475              : 
    7476     24019315 :                 case GIMPLE_CALL:
    7477     24019315 :                   {
    7478     24019315 :                     tree callee = gimple_call_fndecl (stmt);
    7479     24019315 :                     if (callee != NULL_TREE
    7480     24019315 :                         && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
    7481      6366002 :                       changed |= simplify_builtin_call (&gsi, callee, full_walk);
    7482              :                     break;
    7483              :                   }
    7484              : 
    7485    360542634 :                 default:;
    7486              :                 }
    7487              : 
    7488    360542634 :               if (changed || substituted_p)
    7489              :                 {
    7490      4289770 :                   substituted_p = false;
    7491      4289770 :                   stmt = gsi_stmt (gsi);
    7492      4289770 :                   if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
    7493           70 :                     bitmap_set_bit (to_purge, bb->index);
    7494      4289770 :                   if (!was_noreturn
    7495      4289770 :                       && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
    7496           12 :                     to_fixup.safe_push (stmt);
    7497              :                 }
    7498      4289770 :               if (changed)
    7499              :                 {
    7500              :                   /* If the stmt changed then re-visit it and the statements
    7501              :                      inserted before it.  */
    7502      6345918 :                   for (; !gsi_end_p (gsi); gsi_prev (&gsi))
    7503      5869466 :                     if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
    7504              :                       break;
    7505      2946615 :                   if (gsi_end_p (gsi))
    7506       476452 :                     gsi = gsi_start_bb (bb);
    7507              :                   else
    7508      2708389 :                     gsi_next (&gsi);
    7509              :                 }
    7510    360542634 :             }
    7511              :           while (changed);
    7512              : 
    7513              :           /* Stmt no longer needs to be revisited.  */
    7514    357596019 :           stmt = gsi_stmt (gsi);
    7515    357596019 :           gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
    7516    357596019 :           gimple_set_plf (stmt, GF_PLF_1, true);
    7517              : 
    7518              :           /* Fill up the lattice.  */
    7519    357596019 :           if (gimple_assign_single_p (stmt))
    7520              :             {
    7521     72980981 :               tree lhs = gimple_assign_lhs (stmt);
    7522     72980981 :               tree rhs = gimple_assign_rhs1 (stmt);
    7523     72980981 :               if (TREE_CODE (lhs) == SSA_NAME)
    7524              :                 {
    7525     33409054 :                   tree val = lhs;
    7526     33409054 :                   if (TREE_CODE (rhs) == SSA_NAME)
    7527       788596 :                     val = fwprop_ssa_val (rhs);
    7528     32620458 :                   else if (is_gimple_min_invariant (rhs))
    7529       471504 :                     val = rhs;
    7530              :                   /* If we can propagate the lattice-value mark the
    7531              :                      stmt for removal.  */
    7532     33409054 :                   if (val != lhs
    7533     33409054 :                       && may_propagate_copy (lhs, val))
    7534      1256385 :                     to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
    7535     33409054 :                   fwprop_set_lattice_val (lhs, val);
    7536              :                 }
    7537              :             }
    7538    284615038 :           else if (gimple_nop_p (stmt))
    7539       103257 :             to_remove.safe_push (stmt);
    7540              :         }
    7541              : 
    7542              :       /* Substitute in destination PHI arguments.  */
    7543    111197673 :       FOR_EACH_EDGE (e, ei, bb->succs)
    7544     65016238 :         for (gphi_iterator gsi = gsi_start_phis (e->dest);
    7545    108090617 :              !gsi_end_p (gsi); gsi_next (&gsi))
    7546              :           {
    7547     43074379 :             gphi *phi = gsi.phi ();
    7548     43074379 :             use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
    7549     43074379 :             tree arg = USE_FROM_PTR (use_p);
    7550     71044054 :             if (TREE_CODE (arg) != SSA_NAME
    7551     43074379 :                 || virtual_operand_p (arg))
    7552     27969675 :               continue;
    7553     15104704 :             tree val = fwprop_ssa_val (arg);
    7554     15104704 :             if (val != arg
    7555     15104704 :                 && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
    7556       259888 :               propagate_value (use_p, val);
    7557              :           }
    7558              : 
    7559              :       /* Mark outgoing executable edges.  */
    7560     46181435 :       if (edge e = find_taken_edge (bb, NULL))
    7561              :         {
    7562     19717112 :           e->flags |= EDGE_EXECUTABLE;
    7563     46218306 :           if (EDGE_COUNT (bb->succs) > 1)
    7564        36871 :             cfg_changed = true;
    7565              :         }
    7566              :       else
    7567              :         {
    7568     71726577 :           FOR_EACH_EDGE (e, ei, bb->succs)
    7569     45262254 :             e->flags |= EDGE_EXECUTABLE;
    7570              :         }
    7571              :     }
    7572      5731602 :   free (postorder);
    7573      5731602 :   free (bb_to_rpo);
    7574      5731602 :   lattice.release ();
    7575              : 
    7576              :   /* First remove chains of stmts where we check no uses remain.  */
    7577      5731602 :   simple_dce_from_worklist (simple_dce_worklist, to_purge);
    7578              : 
    7579      6098217 :   auto remove = [](gimple *stmt)
    7580              :     {
    7581       366615 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7582              :         {
    7583            6 :           fprintf (dump_file, "Removing dead stmt ");
    7584            6 :           print_gimple_stmt (dump_file, stmt, 0);
    7585            6 :           fprintf (dump_file, "\n");
    7586              :         }
    7587       366615 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    7588       366615 :       if (gimple_code (stmt) == GIMPLE_PHI)
    7589        95238 :         remove_phi_node (&gsi, true);
    7590              :       else
    7591              :         {
    7592       271377 :           unlink_stmt_vdef (stmt);
    7593       271377 :           gsi_remove (&gsi, true);
    7594       271377 :           release_defs (stmt);
    7595              :         }
    7596       366615 :     };
    7597              : 
    7598              :   /* Then remove stmts we know we can remove even though we did not
    7599              :      substitute in dead code regions, so uses can remain.  Do so in reverse
    7600              :      order to make debug stmt creation possible.  */
    7601     12967924 :   while (!to_remove_defs.is_empty())
    7602              :     {
    7603      1504720 :       tree def = ssa_name (to_remove_defs.pop ());
    7604              :       /* For example remove_prop_source_from_use can remove stmts queued
    7605              :          for removal.  Deal with this gracefully.  */
    7606      1504720 :       if (!def)
    7607      1241362 :         continue;
    7608       263358 :       gimple *stmt = SSA_NAME_DEF_STMT (def);
    7609       263358 :       remove (stmt);
    7610              :     }
    7611              : 
    7612              :   /* Wipe other queued stmts that do not have SSA defs.  */
    7613      5834859 :   while (!to_remove.is_empty())
    7614              :     {
    7615       103257 :       gimple *stmt = to_remove.pop ();
    7616       103257 :       remove (stmt);
    7617              :     }
    7618              : 
    7619              :   /* Fixup stmts that became noreturn calls.  This may require splitting
    7620              :      blocks and thus isn't possible during the walk.  Do this
    7621              :      in reverse order so we don't inadvertently remove a stmt we want to
    7622              :      fixup by visiting a dominating now noreturn call first.  */
    7623      5731614 :   while (!to_fixup.is_empty ())
    7624              :     {
    7625           12 :       gimple *stmt = to_fixup.pop ();
    7626           12 :       if (dump_file && dump_flags & TDF_DETAILS)
    7627              :         {
    7628            0 :           fprintf (dump_file, "Fixing up noreturn call ");
    7629            0 :           print_gimple_stmt (dump_file, stmt, 0);
    7630            0 :           fprintf (dump_file, "\n");
    7631              :         }
    7632           12 :       cfg_changed |= fixup_noreturn_call (stmt);
    7633              :     }
    7634              : 
    7635      5731602 :   cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
    7636      5731602 :   cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
    7637      5731602 :   BITMAP_FREE (to_purge);
    7638              : 
    7639              :   /* Remove edges queued from switch stmt simplification.  */
    7640     17194806 :   for (auto ep : edges_to_remove)
    7641              :     {
    7642            0 :       basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
    7643            0 :       basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
    7644            0 :       edge e;
    7645            0 :       if (src && dest && (e = find_edge (src, dest)))
    7646              :         {
    7647            0 :           free_dominance_info (CDI_DOMINATORS);
    7648            0 :           remove_edge (e);
    7649            0 :           cfg_changed = true;
    7650              :         }
    7651              :     }
    7652              : 
    7653      5731605 :   if (get_range_query (fun) != get_global_range_query ())
    7654         1616 :     disable_ranger (fun);
    7655              : 
    7656      5731602 :   if (cfg_changed)
    7657        13050 :     todoflags |= TODO_cleanup_cfg;
    7658              : 
    7659      5731602 :   return todoflags;
    7660      5731602 : }
    7661              : 
    7662              : } // anon namespace
    7663              : 
    7664              : gimple_opt_pass *
    7665       302985 : make_pass_forwprop (gcc::context *ctxt)
    7666              : {
    7667       302985 :   return new pass_forwprop (ctxt);
    7668              : }
        

Generated by: LCOV version 2.6-beta

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