LCOV - code coverage report
Current view: top level - gcc - tree-ssa-forwprop.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.6 % 3577 3418
Test Date: 2026-09-19 16:22:48 Functions: 100.0 % 93 93
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     33449944 : fwprop_set_lattice_val (tree name, tree val)
     223              : {
     224     33449944 :   if (TREE_CODE (name) == SSA_NAME)
     225              :     {
     226     33449944 :       if (SSA_NAME_VERSION (name) >= lattice.length ())
     227              :         {
     228        31434 :           lattice.reserve (num_ssa_names - lattice.length ());
     229        20956 :           lattice.quick_grow_cleared (num_ssa_names);
     230              :         }
     231     33449944 :       lattice[SSA_NAME_VERSION (name)] = val;
     232              :       /* As this now constitutes a copy duplicate points-to
     233              :          and range info appropriately.  */
     234     33449944 :       if (TREE_CODE (val) == SSA_NAME)
     235     32957342 :         maybe_duplicate_ssa_info_at_copy (name, val);
     236              :     }
     237     33449944 : }
     238              : 
     239              : /* Invalidate the lattice entry for NAME, done when releasing SSA names.  */
     240              : static void
     241       945696 : fwprop_invalidate_lattice (tree name)
     242              : {
     243       945696 :   if (name
     244       943187 :       && TREE_CODE (name) == SSA_NAME
     245      1888755 :       && SSA_NAME_VERSION (name) < lattice.length ())
     246       943028 :     lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
     247       945696 : }
     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     28529399 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
     259              : {
     260     28529399 :   bool single_use = true;
     261              : 
     262     28530385 :   do {
     263     28529892 :     gimple *def_stmt = SSA_NAME_DEF_STMT (name);
     264              : 
     265     28529892 :     if (!has_single_use (name))
     266              :       {
     267     15606313 :         single_use = false;
     268     15606313 :         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     28528326 :     if (!is_gimple_assign (def_stmt))
     274              :       return NULL;
     275              : 
     276              :     /* If def_stmt is a simple copy, continue looking.  */
     277     20106798 :     if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
     278          493 :       name = gimple_assign_rhs1 (def_stmt);
     279              :     else
     280              :       {
     281     20106305 :         if (!single_use_only && single_use_p)
     282     19776706 :           *single_use_p = single_use;
     283              : 
     284              :         return def_stmt;
     285              :       }
     286          493 :   } 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     28161686 : can_propagate_from (gimple *def_stmt)
     294              : {
     295     28161686 :   gcc_assert (is_gimple_assign (def_stmt));
     296              : 
     297              :   /* If the rhs has side-effects we cannot propagate from it.  */
     298     28161686 :   if (gimple_has_volatile_ops (def_stmt))
     299              :     return false;
     300              : 
     301              :   /* If the rhs is a load we cannot propagate from it.  */
     302     27568968 :   if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
     303     27568968 :       || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
     304              :     return false;
     305              : 
     306              :   /* Constants can be always propagated.  */
     307     13512946 :   if (gimple_assign_single_p (def_stmt)
     308     13512946 :       && 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     13512946 :   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     13512256 :   if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
     320              :     {
     321      3278501 :       tree rhs = gimple_assign_rhs1 (def_stmt);
     322      3278501 :       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       233291 : remove_prop_source_from_use (tree name)
     337              : {
     338       294619 :   gimple_stmt_iterator gsi;
     339       294619 :   gimple *stmt;
     340              : 
     341       294619 :   do {
     342       294619 :     basic_block bb;
     343              : 
     344       294619 :     if (SSA_NAME_IN_FREE_LIST (name)
     345       294576 :         || SSA_NAME_IS_DEFAULT_DEF (name)
     346       587925 :         || !has_zero_uses (name))
     347              :       break;
     348              : 
     349        61786 :     stmt = SSA_NAME_DEF_STMT (name);
     350        61786 :     if (gimple_code (stmt) == GIMPLE_PHI
     351        61786 :         || gimple_has_side_effects (stmt))
     352              :       break;
     353              : 
     354        61786 :     bb = gimple_bb (stmt);
     355        61786 :     gsi = gsi_for_stmt (stmt);
     356        61786 :     unlink_stmt_vdef (stmt);
     357        61786 :     if (gsi_remove (&gsi, true))
     358            6 :       bitmap_set_bit (to_purge, bb->index);
     359        61786 :     fwprop_invalidate_lattice (gimple_get_lhs (stmt));
     360        61786 :     release_defs (stmt);
     361              : 
     362        61786 :     name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
     363        61786 :   } while (name && TREE_CODE (name) == SSA_NAME);
     364              : 
     365       233291 : }
     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      7387824 : rhs_to_tree (tree type, gimple *stmt)
     376              : {
     377      7387824 :   location_t loc = gimple_location (stmt);
     378      7387824 :   enum tree_code code = gimple_assign_rhs_code (stmt);
     379      7387824 :   switch (get_gimple_rhs_class (code))
     380              :     {
     381        13268 :     case GIMPLE_TERNARY_RHS:
     382        13268 :       return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
     383              :                               gimple_assign_rhs2 (stmt),
     384        13268 :                               gimple_assign_rhs3 (stmt));
     385      5020841 :     case GIMPLE_BINARY_RHS:
     386      5020841 :       return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
     387      5020841 :                               gimple_assign_rhs2 (stmt));
     388      2069253 :     case GIMPLE_UNARY_RHS:
     389      2069253 :       return build1 (code, type, gimple_assign_rhs1 (stmt));
     390       284462 :     case GIMPLE_SINGLE_RHS:
     391       284462 :       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      8316541 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
     405              :                         tree op0, tree op1, bool invariant_only)
     406              : {
     407      8316541 :   tree t;
     408              : 
     409      8316541 :   gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
     410              : 
     411      8316541 :   t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
     412      8316541 :   if (!t)
     413              :     return NULL_TREE;
     414              : 
     415              :   /* Require that we got a boolean type out if we put one in.  */
     416      3632409 :   gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
     417              : 
     418              :   /* Canonicalize the combined condition for use in a COND_EXPR.  */
     419      3632409 :   t = canonicalize_cond_expr_cond (t);
     420              : 
     421              :   /* Bail out if we required an invariant but didn't get one.  */
     422      3632409 :   if (!t || (invariant_only && !is_gimple_min_invariant (t)))
     423      3401370 :     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     22463537 : forward_propagate_into_comparison_1 (gimple *stmt,
     434              :                                      enum tree_code code, tree type,
     435              :                                      tree op0, tree op1)
     436              : {
     437     22463537 :   tree tmp = NULL_TREE;
     438     22463537 :   tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
     439     22463537 :   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     22463537 :   if (TREE_CODE (op0) == SSA_NAME)
     444              :     {
     445     22392042 :       gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
     446     22392042 :       if (def_stmt && can_propagate_from (def_stmt))
     447              :         {
     448      5593916 :           enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
     449      5593916 :           bool invariant_only_p = !single_use0_p;
     450              : 
     451      5593916 :           rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
     452              : 
     453              :           /* Always combine comparisons or conversions from booleans.  */
     454      5593916 :           if (TREE_CODE (op1) == INTEGER_CST
     455      5593916 :               && ((CONVERT_EXPR_CODE_P (def_code)
     456       899046 :                    && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
     457              :                       == BOOLEAN_TYPE)
     458      3572650 :                   || TREE_CODE_CLASS (def_code) == tcc_comparison))
     459              :             invariant_only_p = false;
     460              : 
     461      5593916 :           tmp = combine_cond_expr_cond (stmt, code, type,
     462              :                                         rhs0, op1, invariant_only_p);
     463      5593916 :           if (tmp)
     464              :             return tmp;
     465              :         }
     466              :     }
     467              : 
     468              :   /* If that wasn't successful, try the second operand.  */
     469     22240933 :   if (TREE_CODE (op1) == SSA_NAME)
     470              :     {
     471      5537265 :       gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
     472      5537265 :       if (def_stmt && can_propagate_from (def_stmt))
     473              :         {
     474      1793908 :           rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
     475      1793908 :           tmp = combine_cond_expr_cond (stmt, code, type,
     476              :                                         op0, rhs1, !single_use1_p);
     477      1793908 :           if (tmp)
     478              :             return tmp;
     479              :         }
     480              :     }
     481              : 
     482              :   /* If that wasn't successful either, try both operands.  */
     483     22234328 :   if (rhs0 != NULL_TREE
     484     22234328 :       && rhs1 != NULL_TREE)
     485       928717 :     tmp = combine_cond_expr_cond (stmt, code, type,
     486              :                                   rhs0, rhs1,
     487       928717 :                                   !(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      2658236 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
     498              : {
     499      2658236 :   gimple *stmt = gsi_stmt (*gsi);
     500      2658236 :   tree tmp;
     501      2658236 :   tree type = TREE_TYPE (gimple_assign_lhs (stmt));
     502      2658236 :   tree rhs1 = gimple_assign_rhs1 (stmt);
     503      2658236 :   tree rhs2 = gimple_assign_rhs2 (stmt);
     504              : 
     505              :   /* Combine the comparison with defining statements.  */
     506      2658236 :   tmp = forward_propagate_into_comparison_1 (stmt,
     507              :                                              gimple_assign_rhs_code (stmt),
     508              :                                              type, rhs1, rhs2);
     509      2658236 :   if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
     510              :     {
     511         7277 :       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         7277 :       gimple_assign_set_rhs_from_tree (gsi, tmp);
     520         7277 :       fold_stmt (gsi);
     521         7277 :       update_stmt (gsi_stmt (*gsi));
     522              : 
     523         7277 :       if (TREE_CODE (rhs1) == SSA_NAME)
     524         7277 :         remove_prop_source_from_use (rhs1);
     525         7277 :       if (TREE_CODE (rhs2) == SSA_NAME)
     526         3038 :         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     19805301 : forward_propagate_into_gimple_cond (gcond *stmt)
     540              : {
     541     19805301 :   tree tmp;
     542     19805301 :   enum tree_code code = gimple_cond_code (stmt);
     543     19805301 :   tree rhs1 = gimple_cond_lhs (stmt);
     544     19805301 :   tree rhs2 = gimple_cond_rhs (stmt);
     545              : 
     546              :   /* GIMPLE_COND will always be a comparison.  */
     547     19805301 :   gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison);
     548              : 
     549     19805301 :   tmp = forward_propagate_into_comparison_1 (stmt, code,
     550              :                                              boolean_type_node,
     551              :                                              rhs1, rhs2);
     552     19805301 :   if (tmp
     553     19805301 :       && is_gimple_condexpr_for_cond (tmp))
     554              :     {
     555       217289 :       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       217289 :       gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
     565       217289 :       update_stmt (stmt);
     566              : 
     567       217289 :       if (TREE_CODE (rhs1) == SSA_NAME)
     568       217289 :         remove_prop_source_from_use (rhs1);
     569       217289 :       if (TREE_CODE (rhs2) == SSA_NAME)
     570         5686 :         remove_prop_source_from_use (rhs2);
     571       217289 :       return is_gimple_min_invariant (tmp) ? 2 : 1;
     572              :     }
     573              : 
     574     19588012 :   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      2028147 : tidy_after_forward_propagate_addr (gimple *stmt)
     585              : {
     586              :   /* We may have turned a trapping insn into a non-trapping insn.  */
     587      2028147 :   if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
     588           39 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
     589              : 
     590      2028147 :   if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
     591       255005 :      recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
     592      2028147 : }
     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      2881901 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
     606              :                                gimple_stmt_iterator *use_stmt_gsi,
     607              :                                bool single_use_p)
     608              : {
     609      2881901 :   tree lhs, rhs, rhs2, array_ref;
     610      2881901 :   gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
     611      2881901 :   enum tree_code rhs_code;
     612      2881901 :   bool res = true;
     613              : 
     614      2881901 :   gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
     615              : 
     616      2881901 :   lhs = gimple_assign_lhs (use_stmt);
     617      2881901 :   rhs_code = gimple_assign_rhs_code (use_stmt);
     618      2881901 :   rhs = gimple_assign_rhs1 (use_stmt);
     619              : 
     620              :   /* Do not perform copy-propagation but recurse through copy chains.  */
     621      2881901 :   if (TREE_CODE (lhs) == SSA_NAME
     622      1434384 :       && rhs_code == SSA_NAME)
     623         8685 :     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      2873216 :   if (TREE_CODE (lhs) == SSA_NAME
     631      1425699 :       && 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        24694 :       if (single_use_p
     637        24694 :           && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
     638              :         {
     639            1 :           gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
     640            1 :           gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
     641            1 :           return true;
     642              :         }
     643              : 
     644              :       /* Else recurse if the conversion preserves the address value.  */
     645        49386 :       if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
     646            2 :            || POINTER_TYPE_P (TREE_TYPE (lhs)))
     647        49386 :           && (TYPE_PRECISION (TREE_TYPE (lhs))
     648        24693 :               >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
     649        24626 :         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      2848522 :   if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
     657              :     return false;
     658              : 
     659              :   /* Propagate through constant pointer adjustments.  */
     660      2827319 :   if (TREE_CODE (lhs) == SSA_NAME
     661      1381492 :       && rhs_code == POINTER_PLUS_EXPR
     662      1381492 :       && rhs == name
     663      2996046 :       && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
     664              :     {
     665       120252 :       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       120252 :       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       120252 :       if (TREE_CODE (new_def_rhs) == MEM_REF
     675       120252 :           && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
     676              :         return false;
     677       116233 :       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       116233 :       if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
     682              :         return true;
     683              : 
     684        39117 :       if (useless_type_conversion_p (TREE_TYPE (lhs),
     685        39117 :                                      TREE_TYPE (new_def_rhs)))
     686        39117 :         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        39117 :       gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
     693        39117 :       update_stmt (use_stmt);
     694        39117 :       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      2707067 :   tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
     700      4094286 :   while (handled_component_p (*lhsp))
     701      1387219 :     lhsp = &TREE_OPERAND (*lhsp, 0);
     702      2707067 :   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      2707067 :   if (TREE_CODE (lhs) == MEM_REF
     707      2707067 :       && TREE_OPERAND (lhs, 0) == name)
     708              :     {
     709       913282 :       tree def_rhs_base;
     710       913282 :       poly_int64 def_rhs_offset;
     711              :       /* If the address is invariant we can always fold it.  */
     712       913282 :       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
     713              :                                                          &def_rhs_offset)))
     714              :         {
     715       865689 :           poly_offset_int off = mem_ref_offset (lhs);
     716       865689 :           tree new_ptr;
     717       865689 :           off += def_rhs_offset;
     718       865689 :           if (TREE_CODE (def_rhs_base) == MEM_REF)
     719              :             {
     720       844190 :               off += mem_ref_offset (def_rhs_base);
     721       844190 :               new_ptr = TREE_OPERAND (def_rhs_base, 0);
     722              :             }
     723              :           else
     724        21499 :             new_ptr = build_fold_addr_expr (def_rhs_base);
     725       865689 :           TREE_OPERAND (lhs, 0) = new_ptr;
     726       865689 :           TREE_OPERAND (lhs, 1)
     727       865689 :             = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
     728       865689 :           tidy_after_forward_propagate_addr (use_stmt);
     729              :           /* Continue propagating into the RHS if this was not the only use.  */
     730       865689 :           if (single_use_p)
     731       232101 :             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        47593 :       else if (integer_zerop (TREE_OPERAND (lhs, 1))
     737        18630 :                && ((gimple_assign_lhs (use_stmt) == lhs
     738        15011 :                     && useless_type_conversion_p
     739        15011 :                          (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
     740        15011 :                           TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
     741        13693 :                    || types_compatible_p (TREE_TYPE (lhs),
     742        13693 :                                           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        55786 :                && (!gimple_clobber_p (use_stmt)
     746          166 :                    || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
     747              :         {
     748         8027 :           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
     749         8027 :           tree new_offset, new_base, saved, new_lhs;
     750        29128 :           while (handled_component_p (*def_rhs_basep))
     751        13074 :             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
     752         8027 :           saved = *def_rhs_basep;
     753         8027 :           if (TREE_CODE (*def_rhs_basep) == MEM_REF)
     754              :             {
     755         3941 :               new_base = TREE_OPERAND (*def_rhs_basep, 0);
     756         3941 :               new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
     757              :                                          TREE_OPERAND (*def_rhs_basep, 1));
     758              :             }
     759              :           else
     760              :             {
     761         4086 :               new_base = build_fold_addr_expr (*def_rhs_basep);
     762         4086 :               new_offset = TREE_OPERAND (lhs, 1);
     763              :             }
     764         8027 :           tree atype = TREE_TYPE (*def_rhs_basep);
     765         8027 :           if (TYPE_ALIGN (TREE_TYPE (lhs)) < TYPE_ALIGN (atype))
     766          312 :             atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (lhs)));
     767         8027 :           *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
     768         8027 :           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
     769         8027 :           TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
     770         8027 :           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
     771         8027 :           new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
     772         8027 :           *lhsp = new_lhs;
     773         8027 :           TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
     774         8027 :           TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
     775         8027 :           *def_rhs_basep = saved;
     776         8027 :           tidy_after_forward_propagate_addr (use_stmt);
     777              :           /* Continue propagating into the RHS if this was not the
     778              :              only use.  */
     779         8027 :           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      2470754 :   tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
     792      2470754 :   if (TREE_CODE (*rhsp) == ADDR_EXPR)
     793       243012 :     rhsp = &TREE_OPERAND (*rhsp, 0);
     794      3506388 :   while (handled_component_p (*rhsp))
     795      1035634 :     rhsp = &TREE_OPERAND (*rhsp, 0);
     796      2470754 :   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      2470754 :   if (TREE_CODE (rhs) == MEM_REF
     801      2470754 :       && TREE_OPERAND (rhs, 0) == name)
     802              :     {
     803      1176393 :       tree def_rhs_base;
     804      1176393 :       poly_int64 def_rhs_offset;
     805      1176393 :       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
     806              :                                                          &def_rhs_offset)))
     807              :         {
     808      1139094 :           poly_offset_int off = mem_ref_offset (rhs);
     809      1139094 :           tree new_ptr;
     810      1139094 :           off += def_rhs_offset;
     811      1139094 :           if (TREE_CODE (def_rhs_base) == MEM_REF)
     812              :             {
     813      1112358 :               off += mem_ref_offset (def_rhs_base);
     814      1112358 :               new_ptr = TREE_OPERAND (def_rhs_base, 0);
     815              :             }
     816              :           else
     817        26736 :             new_ptr = build_fold_addr_expr (def_rhs_base);
     818      1139094 :           TREE_OPERAND (rhs, 0) = new_ptr;
     819      1139094 :           TREE_OPERAND (rhs, 1)
     820      1139094 :             = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
     821      1139094 :           fold_stmt_inplace (use_stmt_gsi);
     822      1139094 :           tidy_after_forward_propagate_addr (use_stmt);
     823      1139094 :           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        37299 :       else if (integer_zerop (TREE_OPERAND (rhs, 1))
     829        37299 :                && ((gimple_assign_rhs1 (use_stmt) == rhs
     830        20358 :                     && useless_type_conversion_p
     831        20358 :                          (TREE_TYPE (gimple_assign_lhs (use_stmt)),
     832        20358 :                           TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
     833        22638 :                    || types_compatible_p (TREE_TYPE (rhs),
     834        22638 :                                           TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
     835              :         {
     836        15337 :           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
     837        15337 :           tree new_offset, new_base, saved, new_rhs;
     838        54557 :           while (handled_component_p (*def_rhs_basep))
     839        23883 :             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
     840        15337 :           saved = *def_rhs_basep;
     841        15337 :           if (TREE_CODE (*def_rhs_basep) == MEM_REF)
     842              :             {
     843         7357 :               new_base = TREE_OPERAND (*def_rhs_basep, 0);
     844         7357 :               new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
     845              :                                          TREE_OPERAND (*def_rhs_basep, 1));
     846              :             }
     847              :           else
     848              :             {
     849         7980 :               new_base = build_fold_addr_expr (*def_rhs_basep);
     850         7980 :               new_offset = TREE_OPERAND (rhs, 1);
     851              :             }
     852        15337 :           tree atype = TREE_TYPE (*def_rhs_basep);
     853        15337 :           if (TYPE_ALIGN (TREE_TYPE (rhs)) < TYPE_ALIGN (atype))
     854          526 :             atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (rhs)));
     855        15337 :           *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
     856        15337 :           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
     857        15337 :           TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
     858        15337 :           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
     859        15337 :           new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
     860        15337 :           *rhsp = new_rhs;
     861        15337 :           TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
     862        15337 :           TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
     863        15337 :           *def_rhs_basep = saved;
     864        15337 :           fold_stmt_inplace (use_stmt_gsi);
     865        15337 :           tidy_after_forward_propagate_addr (use_stmt);
     866        15337 :           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      1316323 :   if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
     873      1316323 :       || 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        48475 :   array_ref = TREE_OPERAND (def_rhs, 0);
     881        48475 :   if ((TREE_CODE (array_ref) != ARRAY_REF
     882         4711 :        || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
     883         4711 :        || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
     884        49984 :       && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
     885              :     return false;
     886              : 
     887        24874 :   rhs2 = gimple_assign_rhs2 (use_stmt);
     888              :   /* Optimize &x[C1] p+ C2 to  &x p+ C3 with C3 = C1 * element_size + C2.  */
     889        24874 :   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      3333680 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
     922              : {
     923      3333680 :   bool all = true;
     924      3333680 :   bool single_use_p = parent_single_use_p && has_single_use (name);
     925              : 
     926     17643217 :   for (gimple *use_stmt : gather_imm_use_stmts (name))
     927              :     {
     928      7642177 :       bool result;
     929      7642177 :       tree use_rhs;
     930              : 
     931              :       /* If the use is not in a simple assignment statement, then
     932              :          there is nothing we can do.  */
     933      7642177 :       if (!is_gimple_assign (use_stmt))
     934              :         {
     935      4760276 :           if (!is_gimple_debug (use_stmt))
     936      1948143 :             all = false;
     937      4760276 :           continue;
     938              :         }
     939              : 
     940      2881901 :       gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
     941      2881901 :       result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
     942              :                                               single_use_p);
     943              :       /* If the use has moved to a different statement adjust
     944              :          the update machinery for the old statement too.  */
     945      2881901 :       if (use_stmt != gsi_stmt (gsi))
     946              :         {
     947            0 :           update_stmt (use_stmt);
     948            0 :           use_stmt = gsi_stmt (gsi);
     949              :         }
     950      2881901 :       update_stmt (use_stmt);
     951      2881901 :       all &= result;
     952              : 
     953              :       /* Remove intermediate now unused copy and conversion chains.  */
     954      2881901 :       use_rhs = gimple_assign_rhs1 (use_stmt);
     955      2881901 :       if (result
     956      1512230 :           && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
     957      1263205 :           && TREE_CODE (use_rhs) == SSA_NAME
     958      2964269 :           && has_zero_uses (gimple_assign_lhs (use_stmt)))
     959              :         {
     960        82368 :           gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
     961        82368 :           fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
     962        82368 :           release_defs (use_stmt);
     963        82368 :           gsi_remove (&gsi, true);
     964              :         }
     965      3333680 :     }
     966              : 
     967      3333680 :   return all && has_zero_uses (name);
     968              : }
     969              : 
     970              : 
     971              : /* Helper function for simplify_gimple_switch.  Remove case labels that
     972              :    have values outside the range of the new type.  */
     973              : 
     974              : static void
     975        11708 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
     976              :                                   vec<std::pair<int, int> > &edges_to_remove)
     977              : {
     978        11708 :   unsigned int branch_num = gimple_switch_num_labels (stmt);
     979        11708 :   auto_vec<tree> labels (branch_num);
     980        11708 :   unsigned int i, len;
     981              : 
     982              :   /* Collect the existing case labels in a VEC, and preprocess it as if
     983              :      we are gimplifying a GENERIC SWITCH_EXPR.  */
     984        73710 :   for (i = 1; i < branch_num; i++)
     985        50294 :     labels.quick_push (gimple_switch_label (stmt, i));
     986        11708 :   preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
     987              : 
     988              :   /* If any labels were removed, replace the existing case labels
     989              :      in the GIMPLE_SWITCH statement with the correct ones.
     990              :      Note that the type updates were done in-place on the case labels,
     991              :      so we only have to replace the case labels in the GIMPLE_SWITCH
     992              :      if the number of labels changed.  */
     993        11708 :   len = labels.length ();
     994        11708 :   if (len < branch_num - 1)
     995              :     {
     996            0 :       bitmap target_blocks;
     997            0 :       edge_iterator ei;
     998            0 :       edge e;
     999              : 
    1000              :       /* Corner case: *all* case labels have been removed as being
    1001              :          out-of-range for INDEX_TYPE.  Push one label and let the
    1002              :          CFG cleanups deal with this further.  */
    1003            0 :       if (len == 0)
    1004              :         {
    1005            0 :           tree label, elt;
    1006              : 
    1007            0 :           label = CASE_LABEL (gimple_switch_default_label (stmt));
    1008            0 :           elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
    1009            0 :           labels.quick_push (elt);
    1010            0 :           len = 1;
    1011              :         }
    1012              : 
    1013            0 :       for (i = 0; i < labels.length (); i++)
    1014            0 :         gimple_switch_set_label (stmt, i + 1, labels[i]);
    1015            0 :       for (i++ ; i < branch_num; i++)
    1016            0 :         gimple_switch_set_label (stmt, i, NULL_TREE);
    1017            0 :       gimple_switch_set_num_labels (stmt, len + 1);
    1018              : 
    1019              :       /* Cleanup any edges that are now dead.  */
    1020            0 :       target_blocks = BITMAP_ALLOC (NULL);
    1021            0 :       for (i = 0; i < gimple_switch_num_labels (stmt); i++)
    1022              :         {
    1023            0 :           tree elt = gimple_switch_label (stmt, i);
    1024            0 :           basic_block target = label_to_block (cfun, CASE_LABEL (elt));
    1025            0 :           bitmap_set_bit (target_blocks, target->index);
    1026              :         }
    1027            0 :       for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
    1028              :         {
    1029            0 :           if (! bitmap_bit_p (target_blocks, e->dest->index))
    1030            0 :             edges_to_remove.safe_push (std::make_pair (e->src->index,
    1031            0 :                                                        e->dest->index));
    1032              :           else
    1033            0 :             ei_next (&ei);
    1034              :         }
    1035            0 :       BITMAP_FREE (target_blocks);
    1036              :     }
    1037        11708 : }
    1038              : 
    1039              : /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
    1040              :    the condition which we may be able to optimize better.  */
    1041              : 
    1042              : static bool
    1043       108800 : simplify_gimple_switch (gswitch *stmt,
    1044              :                         vec<std::pair<int, int> > &edges_to_remove,
    1045              :                         bitmap simple_dce_worklist)
    1046              : {
    1047              :   /* The optimization that we really care about is removing unnecessary
    1048              :      casts.  That will let us do much better in propagating the inferred
    1049              :      constant at the switch target.  */
    1050       108800 :   tree cond = gimple_switch_index (stmt);
    1051       108800 :   if (TREE_CODE (cond) == SSA_NAME)
    1052              :     {
    1053       108799 :       gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
    1054       108799 :       if (gimple_assign_cast_p (def_stmt))
    1055              :         {
    1056        12678 :           tree def = gimple_assign_rhs1 (def_stmt);
    1057        12678 :           if (TREE_CODE (def) != SSA_NAME)
    1058              :             return false;
    1059              : 
    1060              :           /* If we have an extension or sign-change that preserves the
    1061              :              values we check against then we can copy the source value into
    1062              :              the switch.  */
    1063        12678 :           tree ti = TREE_TYPE (def);
    1064        12678 :           if (INTEGRAL_TYPE_P (ti)
    1065        12678 :               && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
    1066              :             {
    1067        12415 :               size_t n = gimple_switch_num_labels (stmt);
    1068        12415 :               tree min = NULL_TREE, max = NULL_TREE;
    1069        12415 :               if (n > 1)
    1070              :                 {
    1071        12415 :                   min = CASE_LOW (gimple_switch_label (stmt, 1));
    1072        12415 :                   if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
    1073          159 :                     max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
    1074              :                   else
    1075        12256 :                     max = CASE_LOW (gimple_switch_label (stmt, n - 1));
    1076              :                 }
    1077        12415 :               if ((!min || int_fits_type_p (min, ti))
    1078        12411 :                   && (!max || int_fits_type_p (max, ti)))
    1079              :                 {
    1080        11708 :                   bitmap_set_bit (simple_dce_worklist,
    1081        11708 :                                   SSA_NAME_VERSION (cond));
    1082        11708 :                   gimple_switch_set_index (stmt, def);
    1083        11708 :                   simplify_gimple_switch_label_vec (stmt, ti,
    1084              :                                                     edges_to_remove);
    1085        11708 :                   update_stmt (stmt);
    1086        11708 :                   return true;
    1087              :                 }
    1088              :             }
    1089              :         }
    1090              :     }
    1091              : 
    1092              :   return false;
    1093              : }
    1094              : 
    1095              : /* For pointers p2 and p1 return p2 - p1 if the
    1096              :    difference is known and constant, otherwise return NULL.  */
    1097              : 
    1098              : static tree
    1099         5481 : constant_pointer_difference (tree p1, tree p2)
    1100              : {
    1101         5481 :   int i, j;
    1102              : #define CPD_ITERATIONS 5
    1103         5481 :   tree exps[2][CPD_ITERATIONS];
    1104         5481 :   tree offs[2][CPD_ITERATIONS];
    1105         5481 :   int cnt[2];
    1106              : 
    1107        16443 :   for (i = 0; i < 2; i++)
    1108              :     {
    1109        10962 :       tree p = i ? p1 : p2;
    1110        10962 :       tree off = size_zero_node;
    1111        10962 :       gimple *stmt;
    1112        10962 :       enum tree_code code;
    1113              : 
    1114              :       /* For each of p1 and p2 we need to iterate at least
    1115              :          twice, to handle ADDR_EXPR directly in p1/p2,
    1116              :          SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
    1117              :          on definition's stmt RHS.  Iterate a few extra times.  */
    1118        10962 :       j = 0;
    1119        12752 :       do
    1120              :         {
    1121        12752 :           if (!POINTER_TYPE_P (TREE_TYPE (p)))
    1122              :             break;
    1123        12746 :           if (TREE_CODE (p) == ADDR_EXPR)
    1124              :             {
    1125         9599 :               tree q = TREE_OPERAND (p, 0);
    1126         9599 :               poly_int64 offset;
    1127         9599 :               tree base = get_addr_base_and_unit_offset (q, &offset);
    1128         9599 :               if (base)
    1129              :                 {
    1130         8807 :                   q = base;
    1131         8807 :                   if (maybe_ne (offset, 0))
    1132         3754 :                     off = size_binop (PLUS_EXPR, off, size_int (offset));
    1133              :                 }
    1134         9599 :               if (TREE_CODE (q) == MEM_REF
    1135         9599 :                   && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
    1136              :                 {
    1137          155 :                   p = TREE_OPERAND (q, 0);
    1138          155 :                   off = size_binop (PLUS_EXPR, off,
    1139              :                                     wide_int_to_tree (sizetype,
    1140              :                                                       mem_ref_offset (q)));
    1141              :                 }
    1142              :               else
    1143              :                 {
    1144         9444 :                   exps[i][j] = q;
    1145         9444 :                   offs[i][j++] = off;
    1146         9444 :                   break;
    1147              :                 }
    1148              :             }
    1149         3302 :           if (TREE_CODE (p) != SSA_NAME)
    1150              :             break;
    1151         3302 :           exps[i][j] = p;
    1152         3302 :           offs[i][j++] = off;
    1153         3302 :           if (j == CPD_ITERATIONS)
    1154              :             break;
    1155         3302 :           stmt = SSA_NAME_DEF_STMT (p);
    1156         3302 :           if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
    1157              :             break;
    1158         2643 :           code = gimple_assign_rhs_code (stmt);
    1159         2643 :           if (code == POINTER_PLUS_EXPR)
    1160              :             {
    1161         1390 :               if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
    1162              :                 break;
    1163          863 :               off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
    1164          863 :               p = gimple_assign_rhs1 (stmt);
    1165              :             }
    1166              :           else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code))
    1167          927 :             p = gimple_assign_rhs1 (stmt);
    1168              :           else
    1169              :             break;
    1170              :         }
    1171              :       while (1);
    1172        10962 :       cnt[i] = j;
    1173              :     }
    1174              : 
    1175         7465 :   for (i = 0; i < cnt[0]; i++)
    1176         9720 :     for (j = 0; j < cnt[1]; j++)
    1177         7736 :       if (exps[0][i] == exps[1][j])
    1178         4596 :         return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
    1179              : 
    1180              :   return NULL_TREE;
    1181              : }
    1182              : 
    1183              : /* Helper function for optimize_aggr_zeroprop.
    1184              :    Props the zeroing (memset, VAL) that was done in DEST+OFFSET:LEN
    1185              :    (DEFSTMT) into the STMT.  Returns true if the STMT was updated.  */
    1186              : static void
    1187     22643358 : optimize_aggr_zeroprop_1 (gimple *defstmt, gimple *stmt,
    1188              :                           tree dest, poly_int64 offset, tree val,
    1189              :                           poly_offset_int len)
    1190              : {
    1191     22643358 :   tree src2;
    1192     22643358 :   tree len2 = NULL_TREE;
    1193     22643358 :   poly_int64 offset2;
    1194              : 
    1195     22643358 :   if (gimple_call_builtin_p (stmt, BUILT_IN_MEMCPY)
    1196        22220 :       && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR
    1197     22657394 :       && poly_int_tree_p (gimple_call_arg (stmt, 2)))
    1198              :     {
    1199        12996 :       src2 = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
    1200        12996 :       len2 = gimple_call_arg (stmt, 2);
    1201              :     }
    1202     22630362 :    else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
    1203              :      {
    1204      1911834 :         src2 = gimple_assign_rhs1 (stmt);
    1205      1911834 :         len2 = (TREE_CODE (src2) == COMPONENT_REF
    1206      1911834 :                 ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
    1207      1743749 :                 : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
    1208              :         /* Can only handle zero memsets. */
    1209      1911834 :         if (!integer_zerop (val))
    1210     22621747 :           return;
    1211              :      }
    1212              :    else
    1213              :      return;
    1214              : 
    1215      1923804 :   if (len2 == NULL_TREE
    1216      1923804 :       || !poly_int_tree_p (len2))
    1217              :     return;
    1218              : 
    1219      1923804 :   src2 = get_addr_base_and_unit_offset (src2, &offset2);
    1220      1923804 :   if (src2 == NULL_TREE
    1221      1923804 :       || maybe_lt (offset2, offset))
    1222              :     return;
    1223              : 
    1224       875176 :   if (!operand_equal_p (dest, src2, 0))
    1225              :     return;
    1226              : 
    1227              :   /* [ dest + offset, dest + offset + len - 1 ] is set to val.
    1228              :      Make sure that
    1229              :      [ dest + offset2, dest + offset2 + len2 - 1 ] is a subset of that.  */
    1230       131326 :   if (maybe_gt (wi::to_poly_offset (len2) + (offset2 - offset),
    1231              :                 len))
    1232              :     return;
    1233              : 
    1234        21611 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1235              :     {
    1236           32 :       fprintf (dump_file, "Simplified\n  ");
    1237           32 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1238           32 :       fprintf (dump_file, "after previous\n  ");
    1239           32 :       print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1240              :     }
    1241        21611 :   gimple *orig_stmt = stmt;
    1242              :   /* For simplicity, don't change the kind of the stmt,
    1243              :      turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
    1244              :      into memset (&dest, val, len);
    1245              :      In theory we could change dest = src into memset if dest
    1246              :      is addressable (maybe beneficial if val is not 0), or
    1247              :      memcpy (&dest, &src, len) into dest = {} if len is the size
    1248              :      of dest, dest isn't volatile.  */
    1249        21611 :   if (is_gimple_assign (stmt))
    1250              :     {
    1251        21606 :       tree ctor_type = TREE_TYPE (gimple_assign_lhs (stmt));
    1252        21606 :       tree ctor = build_constructor (ctor_type, NULL);
    1253        21606 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    1254        21606 :       gimple_assign_set_rhs_from_tree (&gsi, ctor);
    1255        21606 :       update_stmt (stmt);
    1256        21606 :       statistics_counter_event (cfun, "copy zeroing propagation of aggregate", 1);
    1257              :     }
    1258              :   else /* If stmt is memcpy, transform it into memset.  */
    1259              :     {
    1260            5 :       gcall *call = as_a <gcall *> (stmt);
    1261            5 :       tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET);
    1262            5 :       gimple_call_set_fndecl (call, fndecl);
    1263            5 :       gimple_call_set_fntype (call, TREE_TYPE (fndecl));
    1264            5 :       gimple_call_set_arg (call, 1, val);
    1265            5 :       update_stmt (stmt);
    1266            5 :       statistics_counter_event (cfun, "memcpy to memset changed", 1);
    1267              :     }
    1268              : 
    1269        21611 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1270              :     {
    1271           32 :       fprintf (dump_file, "into\n  ");
    1272           32 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1273              :     }
    1274              : 
    1275              :   /* Mark the bb for eh cleanup if needed.  */
    1276        21611 :   if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
    1277            6 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
    1278              : }
    1279              : 
    1280              : /* Optimize
    1281              :    a = {}; // DEST = value ;; LEN(nullptr)
    1282              :    b = a;
    1283              :    into
    1284              :    a = {};
    1285              :    b = {};
    1286              :    Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
    1287              :    and/or memcpy (&b, &a, sizeof (a)); instead of b = a;  */
    1288              : 
    1289              : static void
    1290     31603765 : optimize_aggr_zeroprop (gimple *stmt, bool full_walk)
    1291              : {
    1292     31603765 :   ao_ref read;
    1293     63207530 :   if (gimple_has_volatile_ops (stmt))
    1294     27518428 :     return;
    1295              : 
    1296     30674324 :   tree dest = NULL_TREE;
    1297     30674324 :   tree val = integer_zero_node;
    1298     30674324 :   tree len = NULL_TREE;
    1299     30674324 :   bool can_use_tbba = true;
    1300              : 
    1301     30674324 :   if (gimple_call_builtin_p (stmt, BUILT_IN_MEMSET)
    1302       112508 :       && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR
    1303        57487 :       && TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
    1304     30729524 :       && poly_int_tree_p (gimple_call_arg (stmt, 2)))
    1305              :     {
    1306        52434 :       dest = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
    1307        52434 :       len = gimple_call_arg (stmt, 2);
    1308        52434 :       val = gimple_call_arg (stmt, 1);
    1309        52434 :       ao_ref_init_from_ptr_and_size (&read, gimple_call_arg (stmt, 0), len);
    1310        52434 :       can_use_tbba = false;
    1311              :     }
    1312     30621890 :   else if (gimple_store_p (stmt)
    1313     30561656 :            && gimple_assign_single_p (stmt)
    1314     61183546 :            && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST)
    1315              :     {
    1316        26584 :       tree str = gimple_assign_rhs1 (stmt);
    1317        26584 :       dest = gimple_assign_lhs (stmt);
    1318        26584 :       ao_ref_init (&read, dest);
    1319              :       /* The string must contain all null char's for now.  */
    1320        58542 :       for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
    1321              :         {
    1322        29338 :           if (TREE_STRING_POINTER (str)[i] != 0)
    1323              :             {
    1324              :               dest = NULL_TREE;
    1325              :               break;
    1326              :             }
    1327              :         }
    1328              :     }
    1329              :   /* A store of integer (scalar, vector or complex) zeros is
    1330              :      a zero store. */
    1331     30595306 :   else if (gimple_store_p (stmt)
    1332     30535072 :            && gimple_assign_single_p (stmt)
    1333     61130378 :            && integer_zerop (gimple_assign_rhs1 (stmt)))
    1334              :     {
    1335      3600585 :       tree rhs = gimple_assign_rhs1 (stmt);
    1336      3600585 :       tree type = TREE_TYPE (rhs);
    1337      3600585 :       dest = gimple_assign_lhs (stmt);
    1338      3600585 :       ao_ref_init (&read, dest);
    1339              :       /* For integral types, the type precision needs to be a multiply of BITS_PER_UNIT. */
    1340      3600585 :       if (INTEGRAL_TYPE_P (type)
    1341      3600585 :           && (TYPE_PRECISION (type) % BITS_PER_UNIT) != 0)
    1342              :         dest = NULL_TREE;
    1343              :     }
    1344     26994721 :   else if (gimple_store_p (stmt)
    1345     26934487 :            && gimple_assign_single_p (stmt)
    1346     26934487 :            && TREE_CODE (gimple_assign_rhs1 (stmt)) == CONSTRUCTOR
    1347     27720084 :            && !gimple_clobber_p (stmt))
    1348              :     {
    1349       725363 :       dest = gimple_assign_lhs (stmt);
    1350       725363 :       ao_ref_init (&read, dest);
    1351              :     }
    1352              : 
    1353      4190821 :   if (dest == NULL_TREE)
    1354              :     return;
    1355              : 
    1356      4166857 :   if (len == NULL_TREE)
    1357      4114423 :     len = (TREE_CODE (dest) == COMPONENT_REF
    1358      4114423 :            ? DECL_SIZE_UNIT (TREE_OPERAND (dest, 1))
    1359      1795105 :            : TYPE_SIZE_UNIT (TREE_TYPE (dest)));
    1360      4114423 :   if (len == NULL_TREE
    1361      4166857 :       || !poly_int_tree_p (len))
    1362              :     return;
    1363              : 
    1364              :   /* Sometimes memset can have no vdef due to invalid declaration of memset (const, etc.).  */
    1365     35852118 :   if (!gimple_vdef (stmt))
    1366              :     return;
    1367              : 
    1368              :   /* This store needs to be on the byte boundary and pointing to an object.  */
    1369      4166833 :   poly_int64 offset;
    1370      4166833 :   tree dest_base = get_addr_base_and_unit_offset (dest, &offset);
    1371      4166833 :   if (dest_base == NULL_TREE)
    1372              :     return;
    1373              : 
    1374              :   /* Setup the worklist.  */
    1375      4085337 :   auto_vec<std::pair<tree, unsigned>> worklist;
    1376      4085337 :   unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 0;
    1377      8170674 :   worklist.safe_push (std::make_pair (gimple_vdef (stmt), limit));
    1378              : 
    1379     28270222 :   while (!worklist.is_empty ())
    1380              :     {
    1381     20099548 :       std::pair<tree, unsigned> top = worklist.pop ();
    1382     20099548 :       tree vdef = top.first;
    1383     20099548 :       limit = top.second;
    1384     20099548 :       gimple *use_stmt;
    1385     20099548 :       imm_use_iterator iter;
    1386     44788045 :       FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
    1387              :         {
    1388              :           /* Handling PHI nodes might not be worth it so don't.  */
    1389     24688497 :           if (is_a <gphi*> (use_stmt))
    1390      2045139 :             continue;
    1391              : 
    1392              :           /* If this statement does not clobber add the vdef stmt to the
    1393              :              worklist.
    1394              :              After hitting the limit, allow clobbers to able to pass through.  */
    1395      2071220 :           if ((limit != 0 || gimple_clobber_p (use_stmt))
    1396     20609061 :               && gimple_vdef (use_stmt)
    1397     40113255 :               && !stmt_may_clobber_ref_p_1 (use_stmt, &read,
    1398              :                                            /* tbaa_p = */ can_use_tbba))
    1399              :               {
    1400     16014211 :                 unsigned new_limit = limit == 0 ? 0 : limit - 1;
    1401     32028422 :                 worklist.safe_push (std::make_pair (gimple_vdef (use_stmt),
    1402              :                                                     new_limit));
    1403              :               }
    1404              : 
    1405     22643358 :            optimize_aggr_zeroprop_1 (stmt, use_stmt, dest_base, offset,
    1406     22643358 :                                      val, wi::to_poly_offset (len));
    1407     20099548 :         }
    1408              :     }
    1409              : 
    1410      4085337 : }
    1411              : 
    1412              : /* Returns the pointer to the base of the object of the
    1413              :    reference EXPR and extracts the information about
    1414              :    the offset of the access, storing it to PBYTESIZE,
    1415              :    PBYTEPOS and PREVERSEP.
    1416              :    If the access is not a byte sized or position is not
    1417              :    on the byte, return NULL.  */
    1418              : static tree
    1419      5323620 : split_core_and_offset_size (tree expr,
    1420              :                             poly_int64 *pbytesize, poly_int64 *pbytepos,
    1421              :                             tree *poffset, int *preversep)
    1422              : {
    1423      5323620 :   tree core;
    1424      5323620 :   machine_mode mode;
    1425      5323620 :   int unsignedp, volatilep;
    1426      5323620 :   poly_int64 bitsize;
    1427      5323620 :   poly_int64 bitpos;
    1428      5323620 :   location_t loc = EXPR_LOCATION (expr);
    1429              : 
    1430      5323620 :   core = get_inner_reference (expr, &bitsize, &bitpos,
    1431              :                               poffset, &mode, &unsignedp, preversep,
    1432              :                               &volatilep);
    1433     10647240 :   if (!multiple_p (bitsize, BITS_PER_UNIT, pbytesize))
    1434              :     return NULL_TREE;
    1435      5323620 :   if (!multiple_p (bitpos, BITS_PER_UNIT, pbytepos))
    1436              :     return NULL_TREE;
    1437              :   /* If we are left with MEM[a + CST] strip that and add it to the
    1438              :      pbytepos and return a. */
    1439      5323620 :   if (TREE_CODE (core) == MEM_REF)
    1440              :     {
    1441      1249557 :       poly_offset_int tem;
    1442      1249557 :       tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
    1443      1249557 :       tem += *pbytepos;
    1444      1249557 :       if (tem.to_shwi (pbytepos))
    1445      1247634 :         return TREE_OPERAND (core, 0);
    1446              :     }
    1447      4075986 :   core = build_fold_addr_expr_loc (loc, core);
    1448      4075986 :   STRIP_NOPS (core);
    1449      4075986 :   return core;
    1450              : }
    1451              : 
    1452              : /* Returns a new src based on the
    1453              :    copy `DEST = SRC` and for the old SRC2.
    1454              :    Returns null if SRC2 is not related to DEST.  */
    1455              : 
    1456              : static tree
    1457      1271520 : new_src_based_on_copy (tree src2, tree dest, tree src)
    1458              : {
    1459              :   /* If the second src is not exactly the same as dest,
    1460              :      try to handle it separately; see it is address/size equivalent.
    1461              :      Handles `a` and `a.b` and `MEM<char[N]>(&a)` which all have
    1462              :      the same size and offsets as address/size equivalent.
    1463              :      This allows copying over a memcpy and also one for copying
    1464              :      where one field is the same size as the whole struct.  */
    1465      1271520 :   if (operand_equal_p (dest, src2))
    1466              :     return src;
    1467              :   /* if both dest and src2 are decls, then we know these 2
    1468              :      accesses can't be the same.  */
    1469       722772 :   if (DECL_P (dest) && DECL_P (src2))
    1470              :     return NULL_TREE;
    1471              :   /* A VCE can't be used with imag/real or BFR so reject them early. */
    1472       379346 :   if (TREE_CODE (src) == IMAGPART_EXPR
    1473       379346 :       || TREE_CODE (src) == REALPART_EXPR
    1474       379346 :       || TREE_CODE (src) == BIT_FIELD_REF)
    1475              :     return NULL_TREE;
    1476       379346 :   tree core1, core2;
    1477       379346 :   poly_int64 bytepos1, bytepos2;
    1478       379346 :   poly_int64 bytesize1, bytesize2;
    1479       379346 :   tree toffset1, toffset2;
    1480       379346 :   int reversep1 = 0;
    1481       379346 :   int reversep2 = 0;
    1482       379346 :   poly_int64 diff = 0;
    1483       379346 :   core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
    1484              :                                           &toffset1, &reversep1);
    1485       379346 :   core2 = split_core_and_offset_size (src2, &bytesize2, &bytepos2,
    1486              :                                           &toffset2, &reversep2);
    1487       379346 :   if (!core1 || !core2)
    1488              :     return NULL_TREE;
    1489       379346 :   if (reversep1 != reversep2)
    1490              :     return NULL_TREE;
    1491              :   /* The sizes of the 2 accesses need to be the same. */
    1492       379346 :   if (!known_eq (bytesize1, bytesize2))
    1493              :     return NULL_TREE;
    1494       172284 :   if (!operand_equal_p (core1, core2, 0))
    1495              :     return NULL_TREE;
    1496              : 
    1497        23543 :   if (toffset1 && toffset2)
    1498              :     {
    1499            2 :       tree type = TREE_TYPE (toffset1);
    1500            2 :       if (type != TREE_TYPE (toffset2))
    1501            0 :         toffset2 = fold_convert (type, toffset2);
    1502              : 
    1503            2 :       tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
    1504            2 :       if (!cst_and_fits_in_hwi (tdiff))
    1505              :         return NULL_TREE;
    1506              : 
    1507            0 :       diff = int_cst_value (tdiff);
    1508            0 :     }
    1509        23541 :   else if (toffset1 || toffset2)
    1510              :     {
    1511              :       /* If only one of the offsets is non-constant, the difference cannot
    1512              :          be a constant.  */
    1513              :       return NULL_TREE;
    1514              :     }
    1515        23509 :   diff += bytepos1 - bytepos2;
    1516              :   /* The offset between the 2 need to be 0. */
    1517        23509 :   if (!known_eq (diff, 0))
    1518              :     return NULL_TREE;
    1519        22676 :   return fold_build1 (VIEW_CONVERT_EXPR,TREE_TYPE (src2), src);
    1520              : }
    1521              : 
    1522              : /* Returns true if SRC and DEST are the same address such that
    1523              :    `SRC == DEST;` is considered a nop. This is more than an
    1524              :    operand_equal_p check as it needs to be similar to
    1525              :    new_src_based_on_copy.  */
    1526              : 
    1527              : static bool
    1528      4524089 : same_for_assignment (tree src, tree dest)
    1529              : {
    1530      4524089 :   if (operand_equal_p (dest, src, 0))
    1531              :     return true;
    1532              :   /* if both dest and src2 are decls, then we know these 2
    1533              :      accesses can't be the same.  */
    1534      4521182 :   if (DECL_P (dest) && DECL_P (src))
    1535              :     return false;
    1536              : 
    1537      2282464 :   tree core1, core2;
    1538      2282464 :   poly_int64 bytepos1, bytepos2;
    1539      2282464 :   poly_int64 bytesize1, bytesize2;
    1540      2282464 :   tree toffset1, toffset2;
    1541      2282464 :   int reversep1 = 0;
    1542      2282464 :   int reversep2 = 0;
    1543      2282464 :   poly_int64 diff = 0;
    1544      2282464 :   core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
    1545              :                                       &toffset1, &reversep1);
    1546      2282464 :   core2 = split_core_and_offset_size (src, &bytesize2, &bytepos2,
    1547              :                                       &toffset2, &reversep2);
    1548      2282464 :   if (!core1 || !core2)
    1549              :     return false;
    1550      2282464 :   if (reversep1 != reversep2)
    1551              :     return false;
    1552              :   /* The sizes of the 2 accesses need to be the same. */
    1553      2282464 :   if (!known_eq (bytesize1, bytesize2))
    1554              :     return false;
    1555      2281546 :   if (!operand_equal_p (core1, core2, 0))
    1556              :     return false;
    1557         6191 :   if (toffset1 && toffset2)
    1558              :     {
    1559          313 :       tree type = TREE_TYPE (toffset1);
    1560          313 :       if (type != TREE_TYPE (toffset2))
    1561            0 :         toffset2 = fold_convert (type, toffset2);
    1562              : 
    1563          313 :       tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
    1564          313 :       if (!cst_and_fits_in_hwi (tdiff))
    1565              :         return false;
    1566              : 
    1567            0 :       diff = int_cst_value (tdiff);
    1568            0 :     }
    1569         5878 :   else if (toffset1 || toffset2)
    1570              :     {
    1571              :       /* If only one of the offsets is non-constant, the difference cannot
    1572              :          be a constant.  */
    1573              :       return false;
    1574              :     }
    1575         5878 :   diff += bytepos1 - bytepos2;
    1576              :   /* The offset between the 2 need to be 0. */
    1577         5878 :   if (!known_eq (diff, 0))
    1578         5632 :     return false;
    1579              :   return true;
    1580              : }
    1581              : 
    1582              : /* Helper function for optimize_agr_copyprop.
    1583              :    For aggregate copies in USE_STMT, see if DEST
    1584              :    is on the lhs of USE_STMT and replace it with SRC. */
    1585              : static void
    1586      1027500 : optimize_agr_copyprop_1 (gimple *stmt, gimple *use_stmt,
    1587              :                          tree dest, tree src)
    1588              : {
    1589      1027500 :   gcc_assert (gimple_assign_load_p (use_stmt)
    1590              :               && gimple_store_p (use_stmt));
    1591      2055000 :   if (gimple_has_volatile_ops (use_stmt))
    1592       613333 :     return;
    1593      1027499 :   tree dest2 = gimple_assign_lhs (use_stmt);
    1594      1027499 :   tree src2 = gimple_assign_rhs1 (use_stmt);
    1595              :   /* If the new store is `src2 = src2;` skip over it. */
    1596      1027499 :   if (same_for_assignment (src2, dest2))
    1597              :     return;
    1598      1026936 :   src = new_src_based_on_copy (src2, dest, src);
    1599      1026936 :   if (!src)
    1600              :     return;
    1601              :   /* For 2 memory references and using a temporary to do the copy,
    1602              :      don't remove the temporary as the 2 memory references might overlap.
    1603              :      Note t does not need to be decl as it could be field.
    1604              :      See PR 22237 for full details.
    1605              :      E.g.
    1606              :      t = *a; #DEST = SRC;
    1607              :      *b = t; #DEST2 = SRC2;
    1608              :      Cannot be convert into
    1609              :      t = *a;
    1610              :      *b = *a;
    1611              :      Though the following is allowed to be done:
    1612              :      t = *a;
    1613              :      *a = t;
    1614              :      And convert it into:
    1615              :      t = *a;
    1616              :      *a = *a;
    1617              :      */
    1618       442725 :   if (!operand_equal_p (dest2, src, 0)
    1619       442725 :       && !DECL_P (dest2) && !DECL_P (src))
    1620              :     {
    1621              :       /* If *a and *b have the same base see if
    1622              :          the offset between the two is greater than
    1623              :          or equal to the size of the type. */
    1624        31910 :       poly_int64 offset1, offset2;
    1625        31910 :       tree len = TYPE_SIZE_UNIT (TREE_TYPE (src));
    1626        31910 :       if (len == NULL_TREE
    1627        31910 :           || !tree_fits_poly_int64_p (len))
    1628        28558 :         return;
    1629        31910 :       tree base1 = get_addr_base_and_unit_offset (dest2, &offset1);
    1630        31910 :       tree base2 = get_addr_base_and_unit_offset (src, &offset2);
    1631        31910 :       poly_int64 size = tree_to_poly_int64 (len);
    1632              :       /* If the bases are 2 different decls,
    1633              :          then there can be no overlapping.  */
    1634        31910 :       if (base1 && base2
    1635        31002 :           && DECL_P (base1) && DECL_P (base2)
    1636         1890 :           && base1 != base2)
    1637              :         ;
    1638              :       /* If we can't figure out the base or the bases are
    1639              :          not equal then fall back to an alignment check.  */
    1640        30249 :       else if (!base1
    1641        30249 :                || !base2
    1642        30249 :                || !operand_equal_p (base1, base2))
    1643              :         {
    1644        29877 :           unsigned int align1 = get_object_alignment (src);
    1645        29877 :           unsigned int align2 = get_object_alignment (dest2);
    1646        29877 :           align1 /= BITS_PER_UNIT;
    1647        29877 :           align2 /= BITS_PER_UNIT;
    1648              :           /* If the alignment of either object is less
    1649              :              than the size then there is a possibility
    1650              :              of overlapping.  */
    1651        29877 :           if (maybe_lt (align1, size)
    1652        29877 :               || maybe_lt (align2, size))
    1653        28558 :             return;
    1654              :         }
    1655              :       /* Make sure [offset1, offset1 + len - 1] does
    1656              :          not overlap with [offset2, offset2 + len - 1],
    1657              :          it is ok if they are at the same location though.  */
    1658          372 :       else if (ranges_maybe_overlap_p (offset1, size, offset2, size)
    1659          372 :           && !known_eq (offset2, offset1))
    1660              :         return;
    1661              :     }
    1662              : 
    1663       414167 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1664              :     {
    1665           11 :       fprintf (dump_file, "Simplified\n  ");
    1666           11 :       print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
    1667           11 :       fprintf (dump_file, "after previous\n  ");
    1668           11 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1669              :     }
    1670       414167 :   gimple *orig_stmt = use_stmt;
    1671       414167 :   gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
    1672       414167 :   gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (src));
    1673       414167 :   update_stmt (use_stmt);
    1674              : 
    1675       414167 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1676              :     {
    1677           11 :       fprintf (dump_file, "into\n  ");
    1678           11 :       print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
    1679              :     }
    1680       414167 :   if (maybe_clean_or_replace_eh_stmt (orig_stmt, use_stmt))
    1681            0 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
    1682       414167 :   statistics_counter_event (cfun, "copy prop for aggregate", 1);
    1683              : }
    1684              : 
    1685              : /* Helper function for optimize_agr_copyprop_1, propagate aggregates
    1686              :    into the arguments of USE_STMT if the argument matches with DEST;
    1687              :    replacing it with SRC.  */
    1688              : static void
    1689       710633 : optimize_agr_copyprop_arg (gimple *defstmt, gcall *call,
    1690              :                            tree dest, tree src)
    1691              : {
    1692       710633 :   bool changed = false;
    1693      2382058 :   for (unsigned arg = 0; arg < gimple_call_num_args (call); arg++)
    1694              :     {
    1695      1671425 :       tree *argptr = gimple_call_arg_ptr (call, arg);
    1696      3151429 :       if (TREE_CODE (*argptr) == SSA_NAME
    1697       953425 :           || is_gimple_min_invariant (*argptr)
    1698      1862846 :           || TYPE_VOLATILE (TREE_TYPE (*argptr)))
    1699      1480004 :         continue;
    1700       191421 :       tree newsrc = new_src_based_on_copy (*argptr, dest, src);
    1701       191421 :       if (!newsrc)
    1702       115797 :         continue;
    1703              : 
    1704        75624 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1705              :         {
    1706            9 :           fprintf (dump_file, "Simplified\n  ");
    1707            9 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    1708            9 :           fprintf (dump_file, "after previous\n  ");
    1709            9 :           print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1710              :         }
    1711        75624 :       *argptr = unshare_expr (newsrc);
    1712        75624 :       changed = true;
    1713        75624 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1714              :         {
    1715            9 :           fprintf (dump_file, "into\n  ");
    1716            9 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    1717              :         }
    1718              :     }
    1719       710633 :   if (changed)
    1720        75448 :     update_stmt (call);
    1721       710633 : }
    1722              : 
    1723              : /* Helper function for optimize_agr_copyprop, propagate aggregates
    1724              :    into the return stmt USE if the operand of the return matches DEST;
    1725              :    replacing it with SRC.  */
    1726              : static void
    1727       129042 : optimize_agr_copyprop_return (gimple *defstmt, greturn *use,
    1728              :                               tree dest, tree src)
    1729              : {
    1730       129042 :   tree rvalue = gimple_return_retval (use);
    1731       129042 :   if (!rvalue
    1732        84025 :       || TREE_CODE (rvalue) == SSA_NAME
    1733        75229 :       || is_gimple_min_invariant (rvalue)
    1734       203858 :       || TYPE_VOLATILE (TREE_TYPE (rvalue)))
    1735              :     return;
    1736              : 
    1737              :   /* `return <retval>;` is already the best it could be.
    1738              :      Likewise `return *<retval>_N(D)`.  */
    1739        74815 :   if (TREE_CODE (rvalue) == RESULT_DECL
    1740        74815 :       || (TREE_CODE (rvalue) == MEM_REF
    1741            0 :           && TREE_CODE (TREE_OPERAND (rvalue, 0)) == SSA_NAME
    1742            0 :           && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (rvalue, 0)))
    1743              :                == RESULT_DECL))
    1744              :     return;
    1745        53163 :   tree newsrc = new_src_based_on_copy (rvalue, dest, src);
    1746        53163 :   if (!newsrc)
    1747              :     return;
    1748              :   /* Currently only support non-global vars.
    1749              :      See PR 124099 on enumtls not supporting expanding for GIMPLE_RETURN.
    1750              :      FIXME: could support VCEs too?  */
    1751        53075 :   if (!VAR_P (newsrc) || is_global_var (newsrc))
    1752              :     return;
    1753        26715 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1754              :     {
    1755            1 :       fprintf (dump_file, "Simplified\n  ");
    1756            1 :       print_gimple_stmt (dump_file, use, 0, dump_flags);
    1757            1 :       fprintf (dump_file, "after previous\n  ");
    1758            1 :       print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1759              :     }
    1760        26715 :   gimple_return_set_retval (use, newsrc);
    1761        26715 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1762              :     {
    1763            1 :       fprintf (dump_file, "into\n  ");
    1764            1 :       print_gimple_stmt (dump_file, use, 0, dump_flags);
    1765              :     }
    1766        26715 :   update_stmt (use);
    1767              : }
    1768              : 
    1769              : /* Optimizes
    1770              :    DEST = SRC;
    1771              :    DEST2 = DEST; # DEST2 = SRC2;
    1772              :    into
    1773              :    DEST = SRC;
    1774              :    DEST2 = SRC;
    1775              :    STMT is the first statement and SRC is the common
    1776              :    between the statements.
    1777              : 
    1778              :    Also optimizes:
    1779              :    DEST = SRC;
    1780              :    call_func(..., DEST, ...);
    1781              :    into:
    1782              :    DEST = SRC;
    1783              :    call_func(..., SRC, ...);
    1784              : 
    1785              : */
    1786              : static void
    1787      3905669 : optimize_agr_copyprop (gimple *stmt)
    1788              : {
    1789      7811338 :   if (gimple_has_volatile_ops (stmt))
    1790       411669 :     return;
    1791              : 
    1792              :   /* Can't prop if the statement could throw.  */
    1793      3904458 :   if (stmt_could_throw_p (cfun, stmt))
    1794              :     return;
    1795              : 
    1796      3496590 :   tree dest = gimple_assign_lhs (stmt);
    1797      3496590 :   tree src = gimple_assign_rhs1 (stmt);
    1798              :   /* If the statement is `src = src;` then ignore it. */
    1799      3496590 :   if (same_for_assignment (dest, src))
    1800              :     return;
    1801              : 
    1802      3494000 :   tree vdef = gimple_vdef (stmt);
    1803      3494000 :   imm_use_iterator iter;
    1804      3494000 :   gimple *use_stmt;
    1805     10075246 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
    1806              :     {
    1807      6581246 :       if (gimple_assign_load_p (use_stmt)
    1808      6581246 :           && gimple_store_p (use_stmt))
    1809      1027500 :         optimize_agr_copyprop_1 (stmt, use_stmt, dest, src);
    1810      5553746 :       else if (is_gimple_call (use_stmt))
    1811       710633 :         optimize_agr_copyprop_arg (stmt, as_a<gcall*>(use_stmt), dest, src);
    1812      4843113 :       else if (is_a<greturn*> (use_stmt))
    1813       129042 :         optimize_agr_copyprop_return (stmt, as_a<greturn*>(use_stmt), dest, src);
    1814      3494000 :     }
    1815              : }
    1816              : 
    1817              : /* Simple DSE of the lhs from a clobber STMT.
    1818              :    This is used mostly to clean up from optimize_agr_copyprop and
    1819              :    to remove (exactly one) extra copy that might later on confuse SRA.
    1820              :    An example is:
    1821              :    ;; write to a and such.
    1822              :    b = a; // This statement is to be removed
    1823              :    b = {CLOBBER};
    1824              :    SRA will totally scalarize b (which means also a) here for the extra copy
    1825              :    which is not something welcomed. So removing the copy will
    1826              :    allow SRA to move the scalarization of a further down or not at all.
    1827              :    */
    1828              : static void
    1829      7476333 : do_simple_agr_dse (gassign *stmt, bool full_walk)
    1830              : {
    1831              :   /* Don't do this while in -Og as we want to keep around the copy
    1832              :      for debuggability.  */
    1833      7476333 :   if (optimize_debug)
    1834      5169806 :     return;
    1835      7472790 :   ao_ref read;
    1836      7472790 :   basic_block bb = gimple_bb (stmt);
    1837      7472790 :   tree lhs = gimple_assign_lhs (stmt);
    1838              :   /* Only handle clobbers of a full decl.  */
    1839      7472790 :   if (!DECL_P (lhs))
    1840              :     return;
    1841      6740564 :   ao_ref_init (&read, lhs);
    1842      6740564 :   tree vuse = gimple_vuse (stmt);
    1843      6740564 :   unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 4;
    1844     17152007 :   while (limit)
    1845              :     {
    1846     17138633 :       gimple *ostmt = SSA_NAME_DEF_STMT (vuse);
    1847              :       /* Don't handle phis, just declare to be done. */
    1848     17138633 :       if (is_a<gphi*>(ostmt) || gimple_nop_p (ostmt))
    1849              :         break;
    1850     14845480 :       basic_block obb = gimple_bb (ostmt);
    1851              :       /* If the clobber is not fully dominating the statement define,
    1852              :          then it is not "simple" to detect if the define is fully clobbered.  */
    1853     14845480 :       if (obb != bb && !dominated_by_p (CDI_DOMINATORS, bb, obb))
    1854      4434037 :         return;
    1855     14845480 :       gimple *use_stmt;
    1856     14845480 :       imm_use_iterator iter;
    1857     45217947 :       FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_vdef (ostmt))
    1858              :         {
    1859     17512755 :           basic_block ubb = gimple_bb (use_stmt);
    1860     17512755 :           if (stmt == use_stmt)
    1861      5179364 :             continue;
    1862              :           /* If the use is a clobber for lhs,
    1863              :              then it can be safely skipped; this happens with eh
    1864              :              and sometimes jump threading.  */
    1865     12333391 :           if (gimple_clobber_p (use_stmt)
    1866     12333391 :               && lhs == gimple_assign_lhs (use_stmt))
    1867       176859 :             continue;
    1868              :           /* If the use is a phi and it is single use then check if that single use
    1869              :              is a clobber and lhs is the same.  */
    1870     12156532 :           if (gphi *use_phi = dyn_cast<gphi*>(use_stmt))
    1871              :             {
    1872       365873 :               use_operand_p ou;
    1873       365873 :               gimple *ostmt;
    1874       365873 :               if (single_imm_use (gimple_phi_result (use_phi), &ou, &ostmt)
    1875       305925 :                   && gimple_clobber_p (ostmt)
    1876       613572 :                   && lhs == gimple_assign_lhs (ostmt))
    1877        68718 :                 continue;
    1878              :               /* A phi node will never be dominating the clobber.  */
    1879       297155 :               return;
    1880              :             }
    1881              :           /* The use needs to be dominating the clobber. */
    1882      1503192 :           if ((ubb != bb && !dominated_by_p (CDI_DOMINATORS, bb, ubb))
    1883     12548574 :               || ref_maybe_used_by_stmt_p (use_stmt, &read, false))
    1884              :             return;
    1885              :           /* Count the above alias lookup towards the limit. */
    1886     10567382 :           limit--;
    1887     10567382 :           if (limit == 0)
    1888              :             return;
    1889      1985768 :         }
    1890     12859712 :       vuse = gimple_vuse (ostmt);
    1891              :       /* This is a call with an assignment to the clobber decl,
    1892              :          remove the lhs or the whole stmt if it was pure/const. */
    1893     12859712 :       if (is_a <gcall*>(ostmt)
    1894     12859712 :           && lhs == gimple_call_lhs (ostmt))
    1895              :         {
    1896              :           /* Don't remove stores/statements that are needed for non-call
    1897              :               eh to work.  */
    1898         4011 :           if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
    1899              :             return;
    1900              :           /* If we delete a stmt that could throw, mark the block
    1901              :              in to_purge to cleanup afterwards.  */
    1902         4005 :           if (stmt_could_throw_p (cfun, ostmt))
    1903         1006 :             bitmap_set_bit (to_purge, obb->index);
    1904         4005 :           int flags = gimple_call_flags (ostmt);
    1905         4005 :           if ((flags & (ECF_PURE|ECF_CONST|ECF_NOVOPS))
    1906          203 :               && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
    1907              :             {
    1908          119 :                gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
    1909          119 :                if (dump_file && (dump_flags & TDF_DETAILS))
    1910              :                 {
    1911           14 :                   fprintf (dump_file, "Removing dead call store stmt ");
    1912           14 :                   print_gimple_stmt (dump_file, ostmt, 0);
    1913           14 :                   fprintf (dump_file, "\n");
    1914              :                 }
    1915          119 :               unlink_stmt_vdef (ostmt);
    1916          119 :               release_defs (ostmt);
    1917          119 :               gsi_remove (&gsi, true);
    1918          119 :               statistics_counter_event (cfun, "delete call dead store", 1);
    1919              :               /* Only remove the first store previous statement. */
    1920          119 :               return;
    1921              :             }
    1922              :           /* Make sure we do not remove a return slot we cannot reconstruct
    1923              :              later.  */
    1924         3886 :           if (gimple_call_return_slot_opt_p (as_a <gcall *>(ostmt))
    1925         3886 :               && (TREE_ADDRESSABLE (TREE_TYPE (gimple_call_fntype (ostmt)))
    1926          545 :                   || !poly_int_tree_p
    1927          545 :                       (TYPE_SIZE (TREE_TYPE (gimple_call_fntype (ostmt))))))
    1928              :             return;
    1929          682 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1930              :             {
    1931            6 :               fprintf (dump_file, "Removing lhs of call stmt ");
    1932            6 :               print_gimple_stmt (dump_file, ostmt, 0);
    1933            6 :               fprintf (dump_file, "\n");
    1934              :             }
    1935          682 :           gimple_call_set_lhs (ostmt, NULL_TREE);
    1936          682 :           update_stmt (ostmt);
    1937          682 :           statistics_counter_event (cfun, "removed lhs call", 1);
    1938          682 :           return;
    1939              :         }
    1940              :       /* This an assignment store to the clobbered decl,
    1941              :          then maybe remove it. */
    1942     12855701 :       if (is_a <gassign*>(ostmt)
    1943     10854765 :           && gimple_store_p (ostmt)
    1944     10854765 :           && !gimple_clobber_p (ostmt)
    1945     16080446 :           && lhs == gimple_assign_lhs (ostmt))
    1946              :         {
    1947              :           /* Don't remove stores/statements that are needed for non-call
    1948              :               eh to work.  */
    1949       167808 :           if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
    1950              :             return;
    1951              :           /* If we delete a stmt that could throw, mark the block
    1952              :              in to_purge to cleanup afterwards.  */
    1953       162728 :           if (stmt_could_throw_p (cfun, ostmt))
    1954            0 :             bitmap_set_bit (to_purge, obb->index);
    1955       162728 :           gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
    1956       162728 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1957              :             {
    1958           12 :               fprintf (dump_file, "Removing dead store stmt ");
    1959           12 :               print_gimple_stmt (dump_file, ostmt, 0);
    1960           12 :               fprintf (dump_file, "\n");
    1961              :             }
    1962       162728 :           unlink_stmt_vdef (ostmt);
    1963       162728 :           release_defs (ostmt);
    1964       162728 :           gsi_remove (&gsi, true);
    1965       162728 :           statistics_counter_event (cfun, "delete dead store", 1);
    1966              :           /* Only remove the first store previous statement. */
    1967       162728 :           return;
    1968              :         }
    1969              :       /* If the statement uses or maybe writes to the decl,
    1970              :          then nothing is to be removed. Don't know if the write
    1971              :          to the decl is partial write or a full one so the need
    1972              :          to stop.
    1973              :          e.g.
    1974              :          b.c = a;
    1975              :            Easier to stop here rather than do a full partial
    1976              :            dse of this statement.
    1977              :          b = {CLOBBER}; */
    1978     12687893 :       if (stmt_may_clobber_ref_p_1 (ostmt, &read, false)
    1979     12687893 :           || ref_maybe_used_by_stmt_p (ostmt, &read, false))
    1980              :         return;
    1981     10411443 :       limit--;
    1982              :     }
    1983              : }
    1984              : 
    1985              : /* Optimizes builtin memcmps for small constant sizes.
    1986              :    GSI_P is the GSI for the call. STMT is the call itself.
    1987              :    */
    1988              : 
    1989              : static bool
    1990       472184 : simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
    1991              : {
    1992              :   /* Make sure memcmp arguments are the correct type.  */
    1993       472184 :   if (gimple_call_num_args (stmt) != 3)
    1994              :     return false;
    1995       472184 :   tree arg1 = gimple_call_arg (stmt, 0);
    1996       472184 :   tree arg2 = gimple_call_arg (stmt, 1);
    1997       472184 :   tree len = gimple_call_arg (stmt, 2);
    1998              : 
    1999       472184 :   if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
    2000              :     return false;
    2001       472184 :   if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
    2002              :     return false;
    2003       472184 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (len)))
    2004              :     return false;
    2005              : 
    2006              :   /* The return value of the memcmp has to be used
    2007              :      equality comparison to zero. */
    2008       472184 :   tree res = gimple_call_lhs (stmt);
    2009              : 
    2010       472184 :   if (!res || !use_in_zero_equality (res))
    2011              :     return false;
    2012              : 
    2013       456984 :   unsigned HOST_WIDE_INT leni;
    2014              : 
    2015       456984 :   if (tree_fits_uhwi_p (len)
    2016       635742 :       && (leni = tree_to_uhwi (len)) <= GET_MODE_SIZE (word_mode)
    2017       538372 :       && pow2p_hwi (leni))
    2018              :     {
    2019        19242 :       leni *= CHAR_TYPE_SIZE;
    2020        19242 :       unsigned align1 = get_pointer_alignment (arg1);
    2021        19242 :       unsigned align2 = get_pointer_alignment (arg2);
    2022        19242 :       unsigned align = MIN (align1, align2);
    2023        19242 :       scalar_int_mode mode;
    2024        38484 :       if (int_mode_for_size (leni, 1).exists (&mode)
    2025        19242 :           && (align >= leni || !targetm.slow_unaligned_access (mode, align)))
    2026              :         {
    2027        19242 :           location_t loc = gimple_location (stmt);
    2028        19242 :           tree type, off;
    2029        19242 :           type = build_nonstandard_integer_type (leni, 1);
    2030        38484 :           gcc_assert (known_eq (GET_MODE_BITSIZE (TYPE_MODE (type)), leni));
    2031        19242 :           tree ptrtype = build_pointer_type_for_mode (char_type_node,
    2032              :                                                       ptr_mode, true);
    2033        19242 :           off = build_int_cst (ptrtype, 0);
    2034              : 
    2035              :           /* Create unaligned types if needed. */
    2036        19242 :           tree type1 = type, type2 = type;
    2037        19242 :           if (TYPE_ALIGN (type1) > align1)
    2038         7926 :             type1 = build_aligned_type (type1, align1);
    2039        19242 :           if (TYPE_ALIGN (type2) > align2)
    2040         8421 :             type2 = build_aligned_type (type2, align2);
    2041              : 
    2042        19242 :           arg1 = build2_loc (loc, MEM_REF, type1, arg1, off);
    2043        19242 :           arg2 = build2_loc (loc, MEM_REF, type2, arg2, off);
    2044        19242 :           tree tem1 = fold_const_aggregate_ref (arg1);
    2045        19242 :           if (tem1)
    2046          222 :             arg1 = tem1;
    2047        19242 :           tree tem2 = fold_const_aggregate_ref (arg2);
    2048        19242 :           if (tem2)
    2049         7576 :             arg2 = tem2;
    2050        19242 :           res = fold_convert_loc (loc, TREE_TYPE (res),
    2051              :                                   fold_build2_loc (loc, NE_EXPR,
    2052              :                                                    boolean_type_node,
    2053              :                                                    arg1, arg2));
    2054        19242 :           gimplify_and_update_call_from_tree (gsi_p, res);
    2055        19242 :           return true;
    2056              :         }
    2057              :     }
    2058              : 
    2059              :   /* Replace memcmp with memcmp_eq if the above fails. */
    2060       437742 :   if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
    2061              :     return false;
    2062       346536 :   if (!fold_before_rtl_expansion_p ())
    2063              :     return false;
    2064        91206 :   gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
    2065        91206 :   update_stmt (stmt);
    2066        91206 :   return true;
    2067              : }
    2068              : 
    2069              : /* Optimizes builtin memchrs for small constant sizes with a const string.
    2070              :    GSI_P is the GSI for the call. STMT is the call itself.
    2071              :    */
    2072              : 
    2073              : static bool
    2074        14532 : simplify_builtin_memchr (gimple_stmt_iterator *gsi_p, gcall *stmt)
    2075              : {
    2076        14532 :   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
    2077              :     return false;
    2078              : 
    2079        14532 :   if (gimple_call_num_args (stmt) != 3)
    2080              :     return false;
    2081              : 
    2082        14532 :   tree res = gimple_call_lhs (stmt);
    2083        14532 :   if (!res || !use_in_zero_equality (res))
    2084              :     return false;
    2085              : 
    2086         1459 :   tree ptr = gimple_call_arg (stmt, 0);
    2087         1459 :   if (TREE_CODE (ptr) != ADDR_EXPR
    2088         1459 :       || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
    2089              :     return false;
    2090              : 
    2091          452 :   unsigned HOST_WIDE_INT slen
    2092          452 :     = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
    2093              :   /* It must be a non-empty string constant.  */
    2094          452 :   if (slen < 2)
    2095              :     return false;
    2096              : 
    2097              :   /* For -Os, only simplify strings with a single character.  */
    2098          448 :   if (!optimize_bb_for_speed_p (gimple_bb (stmt))
    2099          448 :       && slen > 2)
    2100              :     return false;
    2101              : 
    2102          432 :   tree size = gimple_call_arg (stmt, 2);
    2103              :   /* Size must be a constant which is <= UNITS_PER_WORD and
    2104              :      <= the string length.  */
    2105          432 :   if (!tree_fits_uhwi_p (size))
    2106              :     return false;
    2107              : 
    2108          432 :   unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
    2109          433 :   if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
    2110              :     return false;
    2111              : 
    2112          380 :   tree ch = gimple_call_arg (stmt, 1);
    2113          380 :   location_t loc = gimple_location (stmt);
    2114          380 :   if (!useless_type_conversion_p (char_type_node,
    2115          380 :                                   TREE_TYPE (ch)))
    2116          380 :     ch = fold_convert_loc (loc, char_type_node, ch);
    2117          380 :   const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
    2118          380 :   unsigned int isize = sz;
    2119          380 :   tree *op = XALLOCAVEC (tree, isize);
    2120         1369 :   for (unsigned int i = 0; i < isize; i++)
    2121              :     {
    2122          989 :       op[i] = build_int_cst (char_type_node, p[i]);
    2123          989 :       op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
    2124              :                                op[i], ch);
    2125              :     }
    2126          989 :   for (unsigned int i = isize - 1; i >= 1; i--)
    2127          609 :     op[i - 1] = fold_convert_loc (loc, boolean_type_node,
    2128              :                                   fold_build2_loc (loc,
    2129              :                                                    BIT_IOR_EXPR,
    2130              :                                                    boolean_type_node,
    2131          609 :                                                    op[i - 1],
    2132          609 :                                                    op[i]));
    2133          380 :   res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
    2134          380 :   gimplify_and_update_call_from_tree (gsi_p, res);
    2135          380 :   return true;
    2136              : }
    2137              : 
    2138              : /* *GSI_P is a GIMPLE_CALL to a builtin function.
    2139              :    Optimize
    2140              :    memcpy (p, "abcd", 4); // STMT1
    2141              :    memset (p + 4, ' ', 3); // STMT2
    2142              :    into
    2143              :    memcpy (p, "abcd   ", 7);
    2144              :    call if the latter can be stored by pieces during expansion.
    2145              : */
    2146              : 
    2147              : static bool
    2148       112668 : simplify_builtin_memcpy_memset (gimple_stmt_iterator *gsi_p, gcall *stmt2)
    2149              : {
    2150       112668 :   if (gimple_call_num_args (stmt2) != 3
    2151       112668 :       || gimple_call_lhs (stmt2)
    2152              :       || CHAR_BIT != 8
    2153       112668 :       || BITS_PER_UNIT != 8)
    2154              :     return false;
    2155              : 
    2156       106501 :   tree vuse = gimple_vuse (stmt2);
    2157       106501 :   if (vuse == NULL)
    2158              :     return false;
    2159       106485 :   gimple *stmt1 = SSA_NAME_DEF_STMT (vuse);
    2160              : 
    2161       106485 :   tree callee1;
    2162       106485 :   tree ptr1, src1, str1, off1, len1, lhs1;
    2163       106485 :   tree ptr2 = gimple_call_arg (stmt2, 0);
    2164       106485 :   tree val2 = gimple_call_arg (stmt2, 1);
    2165       106485 :   tree len2 = gimple_call_arg (stmt2, 2);
    2166       106485 :   tree diff, vdef, new_str_cst;
    2167       106485 :   gimple *use_stmt;
    2168       106485 :   unsigned int ptr1_align;
    2169       106485 :   unsigned HOST_WIDE_INT src_len;
    2170       106485 :   char *src_buf;
    2171       106485 :   use_operand_p use_p;
    2172              : 
    2173       106485 :   if (!tree_fits_shwi_p (val2)
    2174       102007 :       || !tree_fits_uhwi_p (len2)
    2175       171019 :       || compare_tree_int (len2, 1024) == 1)
    2176              :     return false;
    2177              : 
    2178        58977 :   if (is_gimple_call (stmt1))
    2179              :     {
    2180              :       /* If first stmt is a call, it needs to be memcpy
    2181              :          or mempcpy, with string literal as second argument and
    2182              :          constant length.  */
    2183        30608 :       callee1 = gimple_call_fndecl (stmt1);
    2184        30608 :       if (callee1 == NULL_TREE
    2185        30492 :           || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
    2186        57440 :           || gimple_call_num_args (stmt1) != 3)
    2187              :         return false;
    2188        25500 :       if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
    2189        25500 :           && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
    2190              :         return false;
    2191        11105 :       ptr1 = gimple_call_arg (stmt1, 0);
    2192        11105 :       src1 = gimple_call_arg (stmt1, 1);
    2193        11105 :       len1 = gimple_call_arg (stmt1, 2);
    2194        11105 :       lhs1 = gimple_call_lhs (stmt1);
    2195        11105 :       if (!tree_fits_uhwi_p (len1))
    2196              :         return false;
    2197        11018 :       str1 = string_constant (src1, &off1, NULL, NULL);
    2198        11018 :       if (str1 == NULL_TREE)
    2199              :         return false;
    2200         5131 :       if (!tree_fits_uhwi_p (off1)
    2201         5131 :           || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
    2202         5131 :           || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
    2203         5131 :                                      - tree_to_uhwi (off1)) > 0
    2204         5131 :           || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
    2205        15393 :           || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
    2206         5131 :              != TYPE_MODE (char_type_node))
    2207              :         return false;
    2208              :     }
    2209        28369 :   else if (gimple_assign_single_p (stmt1))
    2210              :     {
    2211              :       /* Otherwise look for length 1 memcpy optimized into
    2212              :          assignment.  */
    2213        17355 :       ptr1 = gimple_assign_lhs (stmt1);
    2214        17355 :       src1 = gimple_assign_rhs1 (stmt1);
    2215        17355 :       if (TREE_CODE (ptr1) != MEM_REF
    2216         3441 :           || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
    2217        18339 :           || !tree_fits_shwi_p (src1))
    2218              :         return false;
    2219          343 :       ptr1 = build_fold_addr_expr (ptr1);
    2220          343 :       STRIP_USELESS_TYPE_CONVERSION (ptr1);
    2221          343 :       callee1 = NULL_TREE;
    2222          343 :       len1 = size_one_node;
    2223          343 :       lhs1 = NULL_TREE;
    2224          343 :       off1 = size_zero_node;
    2225          343 :       str1 = NULL_TREE;
    2226              :     }
    2227              :   else
    2228              :     return false;
    2229              : 
    2230         5474 :   diff = constant_pointer_difference (ptr1, ptr2);
    2231         5474 :   if (diff == NULL && lhs1 != NULL)
    2232              :     {
    2233            7 :       diff = constant_pointer_difference (lhs1, ptr2);
    2234            7 :       if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
    2235            7 :           && diff != NULL)
    2236            7 :         diff = size_binop (PLUS_EXPR, diff,
    2237              :                            fold_convert (sizetype, len1));
    2238              :     }
    2239              :   /* If the difference between the second and first destination pointer
    2240              :      is not constant, or is bigger than memcpy length, bail out.  */
    2241         5474 :   if (diff == NULL
    2242         4596 :       || !tree_fits_uhwi_p (diff)
    2243         4596 :       || tree_int_cst_lt (len1, diff)
    2244         9814 :       || compare_tree_int (diff, 1024) == 1)
    2245              :     return false;
    2246              : 
    2247              :   /* Use maximum of difference plus memset length and memcpy length
    2248              :      as the new memcpy length, if it is too big, bail out.  */
    2249         4340 :   src_len = tree_to_uhwi (diff);
    2250         4340 :   src_len += tree_to_uhwi (len2);
    2251         4340 :   if (src_len < tree_to_uhwi (len1))
    2252              :     src_len = tree_to_uhwi (len1);
    2253         4340 :   if (src_len > 1024)
    2254              :     return false;
    2255              : 
    2256              :   /* If mempcpy value is used elsewhere, bail out, as mempcpy
    2257              :      with bigger length will return different result.  */
    2258         4340 :   if (lhs1 != NULL_TREE
    2259           64 :       && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
    2260         4347 :       && (TREE_CODE (lhs1) != SSA_NAME
    2261            7 :           || !single_imm_use (lhs1, &use_p, &use_stmt)
    2262            7 :           || use_stmt != stmt2))
    2263              :     return false;
    2264              : 
    2265              :   /* If anything reads memory in between memcpy and memset
    2266              :      call, the modified memcpy call might change it.  */
    2267         4340 :   vdef = gimple_vdef (stmt1);
    2268         4340 :   if (vdef != NULL
    2269         4340 :       && (!single_imm_use (vdef, &use_p, &use_stmt)
    2270         3621 :           || use_stmt != stmt2))
    2271              :     return false;
    2272              : 
    2273         3621 :   ptr1_align = get_pointer_alignment (ptr1);
    2274              :   /* Construct the new source string literal.  */
    2275         3621 :   src_buf = XALLOCAVEC (char, src_len + 1);
    2276         3621 :   if (callee1)
    2277         3455 :     memcpy (src_buf,
    2278         3455 :             TREE_STRING_POINTER (str1) + tree_to_uhwi (off1),
    2279              :             tree_to_uhwi (len1));
    2280              :   else
    2281          166 :     src_buf[0] = tree_to_shwi (src1);
    2282         3621 :   memset (src_buf + tree_to_uhwi (diff),
    2283         3621 :           tree_to_shwi (val2), tree_to_uhwi (len2));
    2284         3621 :   src_buf[src_len] = '\0';
    2285              :   /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
    2286              :      handle embedded '\0's.  */
    2287         3621 :   if (strlen (src_buf) != src_len)
    2288              :     return false;
    2289         3527 :   rtl_profile_for_bb (gimple_bb (stmt2));
    2290              :   /* If the new memcpy wouldn't be emitted by storing the literal
    2291              :      by pieces, this optimization might enlarge .rodata too much,
    2292              :      as commonly used string literals couldn't be shared any
    2293              :      longer.  */
    2294         3527 :   if (!can_store_by_pieces (src_len,
    2295              :                             builtin_strncpy_read_str,
    2296              :                             src_buf, ptr1_align, false))
    2297              :     return false;
    2298              : 
    2299         2637 :   new_str_cst = build_string_literal (src_len, src_buf);
    2300         2637 :   if (callee1)
    2301              :     {
    2302              :       /* If STMT1 is a mem{,p}cpy call, adjust it and remove
    2303              :          memset call.  */
    2304         2509 :       if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
    2305            7 :         gimple_call_set_lhs (stmt1, NULL_TREE);
    2306         2509 :       gimple_call_set_arg (stmt1, 1, new_str_cst);
    2307         2509 :       gimple_call_set_arg (stmt1, 2,
    2308         2509 :                            build_int_cst (TREE_TYPE (len1), src_len));
    2309         2509 :       update_stmt (stmt1);
    2310         2509 :       unlink_stmt_vdef (stmt2);
    2311         2509 :       gsi_replace (gsi_p, gimple_build_nop (), false);
    2312         2509 :       fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
    2313         2509 :       release_defs (stmt2);
    2314         2509 :       if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
    2315              :         {
    2316            7 :           fwprop_invalidate_lattice (lhs1);
    2317            7 :           release_ssa_name (lhs1);
    2318              :         }
    2319              :       return true;
    2320              :     }
    2321              :   else
    2322              :     {
    2323              :       /* Otherwise, if STMT1 is length 1 memcpy optimized into
    2324              :          assignment, remove STMT1 and change memset call into
    2325              :          memcpy call.  */
    2326          128 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
    2327              : 
    2328          128 :       if (!is_gimple_val (ptr1))
    2329           12 :         ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
    2330              :                                          true, GSI_SAME_STMT);
    2331          128 :       tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCPY);
    2332          128 :       gimple_call_set_fndecl (stmt2, fndecl);
    2333          128 :       gimple_call_set_fntype (stmt2,
    2334          128 :                               TREE_TYPE (fndecl));
    2335          128 :       gimple_call_set_arg (stmt2, 0, ptr1);
    2336          128 :       gimple_call_set_arg (stmt2, 1, new_str_cst);
    2337          128 :       gimple_call_set_arg (stmt2, 2,
    2338          128 :                            build_int_cst (TREE_TYPE (len2), src_len));
    2339          128 :       unlink_stmt_vdef (stmt1);
    2340          128 :       gsi_remove (&gsi, true);
    2341          128 :       fwprop_invalidate_lattice (gimple_get_lhs (stmt1));
    2342          128 :       release_defs (stmt1);
    2343          128 :       update_stmt (stmt2);
    2344          128 :       return false;
    2345              :     }
    2346              : }
    2347              : 
    2348              : 
    2349              : /* Try to optimize out __builtin_stack_restore.  Optimize it out
    2350              :    if there is another __builtin_stack_restore in the same basic
    2351              :    block and no calls or ASM_EXPRs are in between, or if this block's
    2352              :    only outgoing edge is to EXIT_BLOCK and there are no calls or
    2353              :    ASM_EXPRs after this __builtin_stack_restore.
    2354              :    Note restore right before a noreturn function is not needed.
    2355              :    And skip some cheap calls that will most likely become an instruction.
    2356              :    Restoring the stack before a call is important to be able to keep
    2357              :    stack usage down so that call does not run out of stack.  */
    2358              : 
    2359              : 
    2360              : static bool
    2361        10609 : optimize_stack_restore (gimple_stmt_iterator *gsi, gimple *call)
    2362              : {
    2363        10609 :   if (!fold_before_rtl_expansion_p ())
    2364              :     return false;
    2365         2587 :   tree callee;
    2366         2587 :   gimple *stmt;
    2367              : 
    2368         2587 :   basic_block bb = gsi_bb (*gsi);
    2369              : 
    2370         2587 :   if (gimple_call_num_args (call) != 1
    2371         2587 :       || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
    2372         5174 :       || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
    2373              :     return false;
    2374              : 
    2375         2587 :   gimple_stmt_iterator i = *gsi;
    2376         6481 :   for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
    2377              :     {
    2378         4363 :       stmt = gsi_stmt (i);
    2379         4363 :       if (is_a<gasm*> (stmt))
    2380              :         return false;
    2381         4362 :       gcall *call = dyn_cast<gcall*>(stmt);
    2382         4362 :       if (!call)
    2383         3679 :         continue;
    2384              : 
    2385              :       /* We can remove the restore in front of noreturn
    2386              :          calls.  Since the restore will happen either
    2387              :          via an unwind/longjmp or not at all. */
    2388          683 :       if (gimple_call_noreturn_p (call))
    2389              :         break;
    2390              : 
    2391              :       /* Internal calls are ok, to bypass
    2392              :          check first since fndecl will be null. */
    2393          667 :       if (gimple_call_internal_p (call))
    2394            1 :         continue;
    2395              : 
    2396          666 :       callee = gimple_call_fndecl (call);
    2397              :       /* Non-builtin calls are not ok. */
    2398          666 :       if (!callee
    2399          666 :           || !fndecl_built_in_p (callee))
    2400              :         return false;
    2401              : 
    2402              :       /* Do not remove stack updates before strub leave.  */
    2403          586 :       if (fndecl_built_in_p (callee, BUILT_IN___STRUB_LEAVE)
    2404              :           /* Alloca calls are not ok either. */
    2405          586 :           || fndecl_builtin_alloc_p (callee))
    2406              :         return false;
    2407              : 
    2408          371 :       if (fndecl_built_in_p (callee, BUILT_IN_STACK_RESTORE))
    2409           57 :         goto second_stack_restore;
    2410              : 
    2411              :       /* If not a simple or inexpensive builtin, then it is not ok either. */
    2412          314 :       if (!is_simple_builtin (callee)
    2413          314 :           && !is_inexpensive_builtin (callee))
    2414              :         return false;
    2415              :     }
    2416              : 
    2417              :   /* Allow one successor of the exit block, or zero successors.  */
    2418         2134 :   switch (EDGE_COUNT (bb->succs))
    2419              :     {
    2420              :     case 0:
    2421              :       break;
    2422         2044 :     case 1:
    2423         2044 :       if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
    2424              :         return false;
    2425              :       break;
    2426              :     default:
    2427              :       return false;
    2428              :     }
    2429         1774 :  second_stack_restore:
    2430              : 
    2431              :   /* If there's exactly one use, then zap the call to __builtin_stack_save.
    2432              :      If there are multiple uses, then the last one should remove the call.
    2433              :      In any case, whether the call to __builtin_stack_save can be removed
    2434              :      or not is irrelevant to removing the call to __builtin_stack_restore.  */
    2435         1774 :   if (has_single_use (gimple_call_arg (call, 0)))
    2436              :     {
    2437         1602 :       gimple *stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
    2438         1602 :       if (is_gimple_call (stack_save))
    2439              :         {
    2440         1600 :           callee = gimple_call_fndecl (stack_save);
    2441         1600 :           if (callee && fndecl_built_in_p (callee, BUILT_IN_STACK_SAVE))
    2442              :             {
    2443         1600 :               gimple_stmt_iterator stack_save_gsi;
    2444         1600 :               tree rhs;
    2445              : 
    2446         1600 :               stack_save_gsi = gsi_for_stmt (stack_save);
    2447         1600 :               rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
    2448         1600 :               replace_call_with_value (&stack_save_gsi, rhs);
    2449              :             }
    2450              :         }
    2451              :     }
    2452              : 
    2453              :   /* No effect, so the statement will be deleted.  */
    2454         1774 :   replace_call_with_value (gsi, NULL_TREE);
    2455         1774 :   return true;
    2456              : }
    2457              : 
    2458              : /* Optimizes strlen (s) ==/!= 0 to *s ==/!= 0. */
    2459              : static bool
    2460        64692 : optimize_strlen_comp (gimple_stmt_iterator *gsi, gimple *call)
    2461              : {
    2462        64692 :   if (!fold_before_rtl_expansion_p ())
    2463              :     return false;
    2464              : 
    2465        14010 :   tree lhs = gimple_call_lhs (call);
    2466        14010 :   if (lhs == NULL_TREE || use_in_zero_equality (lhs, true) == NULL)
    2467              :     return false;
    2468              : 
    2469              :   /* The string passed to strlen. */
    2470          162 :   tree ptr = gimple_call_arg (call, 0);
    2471              : 
    2472              :   /* Dereference the string. */
    2473          162 :   tree deref = fold_build2 (MEM_REF, char_type_node, ptr,
    2474              :                             build_zero_cst (ptr_type_node));
    2475              : 
    2476              :   /* Perform a type conversion. */
    2477          162 :   deref = fold_convert_loc (gimple_location (call),
    2478          162 :                             TREE_TYPE (lhs),
    2479              :                             deref);
    2480              : 
    2481              :   /* Replace the original call to strlen with the dereference we just built. */
    2482          162 :   gimplify_and_update_call_from_tree (gsi, deref);
    2483              : 
    2484          162 :   return true;
    2485              : }
    2486              : 
    2487              : /* *GSI_P is a GIMPLE_CALL to a builtin function.
    2488              :    Optimize
    2489              :    memcpy (p, "abcd", 4);
    2490              :    memset (p + 4, ' ', 3);
    2491              :    into
    2492              :    memcpy (p, "abcd   ", 7);
    2493              :    call if the latter can be stored by pieces during expansion.
    2494              : 
    2495              :    Optimize
    2496              :    memchr ("abcd", a, 4) == 0;
    2497              :    or
    2498              :    memchr ("abcd", a, 4) != 0;
    2499              :    to
    2500              :    (a == 'a' || a == 'b' || a == 'c' || a == 'd') == 0
    2501              :    or
    2502              :    (a == 'a' || a == 'b' || a == 'c' || a == 'd') != 0
    2503              : 
    2504              :    Also canonicalize __atomic_fetch_op (p, x, y) op x
    2505              :    to __atomic_op_fetch (p, x, y) or
    2506              :    __atomic_op_fetch (p, x, y) iop x
    2507              :    to __atomic_fetch_op (p, x, y) when possible (also __sync).  */
    2508              : 
    2509              : static bool
    2510      6311643 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2, bool full_walk)
    2511              : {
    2512      6311643 :   gimple *stmt2 = gsi_stmt (*gsi_p);
    2513      6311643 :   enum built_in_function other_atomic = END_BUILTINS;
    2514      6311643 :   enum tree_code atomic_op = ERROR_MARK;
    2515              : 
    2516      6311643 :   switch (DECL_FUNCTION_CODE (callee2))
    2517              :     {
    2518        64692 :     case BUILT_IN_STRLEN:
    2519        64692 :       return optimize_strlen_comp (gsi_p, as_a<gcall*>(stmt2));
    2520        10609 :     case BUILT_IN_STACK_RESTORE:
    2521        10609 :       return optimize_stack_restore (gsi_p, as_a<gcall*>(stmt2));
    2522       472184 :     case BUILT_IN_MEMCMP:
    2523       472184 :     case BUILT_IN_MEMCMP_EQ:
    2524       472184 :       return simplify_builtin_memcmp (gsi_p, as_a<gcall*>(stmt2));
    2525        14532 :     case BUILT_IN_MEMCHR:
    2526        14532 :       return simplify_builtin_memchr (gsi_p, as_a<gcall*>(stmt2));
    2527              : 
    2528       112668 :     case BUILT_IN_MEMSET:
    2529       112668 :       if (gimple_call_num_args (stmt2) == 3)
    2530              :         {
    2531              :           /* Try to prop the zeroing/value of the memset to memcpy
    2532              :              if the dest is an address and the value is a constant. */
    2533       112668 :           optimize_aggr_zeroprop (stmt2, full_walk);
    2534              :         }
    2535       112668 :       return simplify_builtin_memcpy_memset (gsi_p, as_a<gcall*>(stmt2));
    2536              : 
    2537              :  #define CASE_ATOMIC(NAME, OTHER, OP) \
    2538              :     case BUILT_IN_##NAME##_1:                                           \
    2539              :     case BUILT_IN_##NAME##_2:                                           \
    2540              :     case BUILT_IN_##NAME##_4:                                           \
    2541              :     case BUILT_IN_##NAME##_8:                                           \
    2542              :     case BUILT_IN_##NAME##_16:                                          \
    2543              :       atomic_op = OP;                                                   \
    2544              :       other_atomic                                                      \
    2545              :         = (enum built_in_function) (BUILT_IN_##OTHER##_1                \
    2546              :                                     + (DECL_FUNCTION_CODE (callee2)     \
    2547              :                                        - BUILT_IN_##NAME##_1));         \
    2548              :       goto handle_atomic_fetch_op;
    2549              : 
    2550        48818 :     CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
    2551         7133 :     CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
    2552         2904 :     CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
    2553         2923 :     CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
    2554         3874 :     CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR)
    2555              : 
    2556         2373 :     CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR)
    2557         2012 :     CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR)
    2558         1876 :     CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR)
    2559         2144 :     CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR)
    2560         1987 :     CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR)
    2561              : 
    2562        14413 :     CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
    2563         8560 :     CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
    2564         2408 :     CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR)
    2565              : 
    2566          854 :     CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR)
    2567          740 :     CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR)
    2568          800 :     CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR)
    2569              : 
    2570              : #undef CASE_ATOMIC
    2571              : 
    2572       103819 :     handle_atomic_fetch_op:
    2573       103819 :       if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
    2574              :         {
    2575        60293 :           tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
    2576        60293 :           tree arg = gimple_call_arg (stmt2, 1);
    2577        60293 :           gimple *use_stmt, *cast_stmt = NULL;
    2578        60293 :           use_operand_p use_p;
    2579        60293 :           tree ndecl = builtin_decl_explicit (other_atomic);
    2580              : 
    2581        60293 :           if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
    2582              :             break;
    2583              : 
    2584        59164 :           if (gimple_assign_cast_p (use_stmt))
    2585              :             {
    2586        31602 :               cast_stmt = use_stmt;
    2587        31602 :               lhsc = gimple_assign_lhs (cast_stmt);
    2588        31602 :               if (lhsc == NULL_TREE
    2589        31602 :                   || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
    2590        31051 :                   || (TYPE_PRECISION (TREE_TYPE (lhsc))
    2591        31051 :                       != TYPE_PRECISION (TREE_TYPE (lhs2)))
    2592        61063 :                   || !single_imm_use (lhsc, &use_p, &use_stmt))
    2593              :                 {
    2594         2669 :                   use_stmt = cast_stmt;
    2595         2669 :                   cast_stmt = NULL;
    2596         2669 :                   lhsc = lhs2;
    2597              :                 }
    2598              :             }
    2599              : 
    2600        59164 :           bool ok = false;
    2601        59164 :           tree oarg = NULL_TREE;
    2602        59164 :           enum tree_code ccode = ERROR_MARK;
    2603        59164 :           tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
    2604        59164 :           if (is_gimple_assign (use_stmt)
    2605        59164 :               && gimple_assign_rhs_code (use_stmt) == atomic_op)
    2606              :             {
    2607         1416 :               if (gimple_assign_rhs1 (use_stmt) == lhsc)
    2608         1016 :                 oarg = gimple_assign_rhs2 (use_stmt);
    2609          400 :               else if (atomic_op != MINUS_EXPR)
    2610              :                 oarg = gimple_assign_rhs1 (use_stmt);
    2611              :             }
    2612        57748 :           else if (atomic_op == MINUS_EXPR
    2613        13283 :                    && is_gimple_assign (use_stmt)
    2614         3638 :                    && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
    2615          199 :                    && TREE_CODE (arg) == INTEGER_CST
    2616        57947 :                    && (TREE_CODE (gimple_assign_rhs2 (use_stmt))
    2617              :                        == INTEGER_CST))
    2618              :             {
    2619          183 :               tree a = fold_convert (TREE_TYPE (lhs2), arg);
    2620          183 :               tree o = fold_convert (TREE_TYPE (lhs2),
    2621              :                                      gimple_assign_rhs2 (use_stmt));
    2622          183 :               if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
    2623              :                 ok = true;
    2624              :             }
    2625        57565 :           else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
    2626              :             ;
    2627        52267 :           else if (gimple_code (use_stmt) == GIMPLE_COND)
    2628              :             {
    2629        19586 :               ccode = gimple_cond_code (use_stmt);
    2630        19586 :               crhs1 = gimple_cond_lhs (use_stmt);
    2631        19586 :               crhs2 = gimple_cond_rhs (use_stmt);
    2632              :             }
    2633        32681 :           else if (is_gimple_assign (use_stmt))
    2634              :             {
    2635         9583 :               if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
    2636              :                 {
    2637         3935 :                   ccode = gimple_assign_rhs_code (use_stmt);
    2638         3935 :                   crhs1 = gimple_assign_rhs1 (use_stmt);
    2639         3935 :                   crhs2 = gimple_assign_rhs2 (use_stmt);
    2640              :                 }
    2641              :             }
    2642        24537 :           if (ccode == EQ_EXPR || ccode == NE_EXPR)
    2643              :             {
    2644              :               /* Deal with x - y == 0 or x ^ y == 0
    2645              :                  being optimized into x == y and x + cst == 0
    2646              :                  into x == -cst.  */
    2647        22337 :               tree o = NULL_TREE;
    2648        22337 :               if (crhs1 == lhsc)
    2649              :                 o = crhs2;
    2650          133 :               else if (crhs2 == lhsc)
    2651          133 :                 o = crhs1;
    2652        22337 :               if (o && atomic_op != PLUS_EXPR)
    2653              :                 oarg = o;
    2654        10117 :               else if (o
    2655        10117 :                        && TREE_CODE (o) == INTEGER_CST
    2656        10117 :                        && TREE_CODE (arg) == INTEGER_CST)
    2657              :                 {
    2658         9407 :                   tree a = fold_convert (TREE_TYPE (lhs2), arg);
    2659         9407 :                   o = fold_convert (TREE_TYPE (lhs2), o);
    2660         9407 :                   if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
    2661        59164 :                     ok = true;
    2662              :                 }
    2663              :             }
    2664        59164 :           if (oarg && !ok)
    2665              :             {
    2666        13636 :               if (operand_equal_p (arg, oarg, 0))
    2667              :                 ok = true;
    2668        12307 :               else if (TREE_CODE (arg) == SSA_NAME
    2669         2207 :                        && TREE_CODE (oarg) == SSA_NAME)
    2670              :                 {
    2671          745 :                   tree oarg2 = oarg;
    2672          745 :                   if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg)))
    2673              :                     {
    2674          104 :                       gimple *g = SSA_NAME_DEF_STMT (oarg);
    2675          104 :                       oarg2 = gimple_assign_rhs1 (g);
    2676          104 :                       if (TREE_CODE (oarg2) != SSA_NAME
    2677          104 :                           || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2))
    2678          208 :                           || (TYPE_PRECISION (TREE_TYPE (oarg2))
    2679          104 :                               != TYPE_PRECISION (TREE_TYPE (oarg))))
    2680              :                         oarg2 = oarg;
    2681              :                     }
    2682          745 :                   if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg)))
    2683              :                     {
    2684          544 :                       gimple *g = SSA_NAME_DEF_STMT (arg);
    2685          544 :                       tree rhs1 = gimple_assign_rhs1 (g);
    2686              :                       /* Handle e.g.
    2687              :                          x.0_1 = (long unsigned int) x_4(D);
    2688              :                          _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0);
    2689              :                          _3 = (long int) _2;
    2690              :                          _7 = x_4(D) + _3;  */
    2691          544 :                       if (rhs1 == oarg || rhs1 == oarg2)
    2692              :                         ok = true;
    2693              :                       /* Handle e.g.
    2694              :                          x.18_1 = (short unsigned int) x_5(D);
    2695              :                          _2 = (int) x.18_1;
    2696              :                          _3 = __atomic_fetch_xor_2 (&vshort, _2, 0);
    2697              :                          _4 = (short int) _3;
    2698              :                          _8 = x_5(D) ^ _4;
    2699              :                          This happens only for char/short.  */
    2700          160 :                       else if (TREE_CODE (rhs1) == SSA_NAME
    2701          160 :                                && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
    2702          320 :                                && (TYPE_PRECISION (TREE_TYPE (rhs1))
    2703          160 :                                    == TYPE_PRECISION (TREE_TYPE (lhs2))))
    2704              :                         {
    2705          160 :                           g = SSA_NAME_DEF_STMT (rhs1);
    2706          160 :                           if (gimple_assign_cast_p (g)
    2707          160 :                               && (gimple_assign_rhs1 (g) == oarg
    2708            0 :                                   || gimple_assign_rhs1 (g) == oarg2))
    2709              :                             ok = true;
    2710              :                         }
    2711              :                     }
    2712          745 :                   if (!ok && arg == oarg2)
    2713              :                     /* Handle e.g.
    2714              :                        _1 = __sync_fetch_and_add_4 (&v, x_5(D));
    2715              :                        _2 = (int) _1;
    2716              :                        x.0_3 = (int) x_5(D);
    2717              :                        _7 = _2 + x.0_3;  */
    2718              :                     ok = true;
    2719              :                 }
    2720              :             }
    2721              : 
    2722        57835 :           if (ok)
    2723              :             {
    2724         2606 :               tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
    2725         2606 :               gimple_call_set_lhs (stmt2, new_lhs);
    2726         2606 :               gimple_call_set_fndecl (stmt2, ndecl);
    2727         2606 :               gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
    2728         2606 :               if (ccode == ERROR_MARK)
    2729         2000 :                 gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
    2730              :                                                 ? NOP_EXPR : SSA_NAME,
    2731              :                                                 new_lhs);
    2732              :               else
    2733              :                 {
    2734         1383 :                   crhs1 = new_lhs;
    2735         1383 :                   crhs2 = build_zero_cst (TREE_TYPE (lhs2));
    2736         1383 :                   if (gimple_code (use_stmt) == GIMPLE_COND)
    2737              :                     {
    2738         1044 :                       gcond *cond_stmt = as_a <gcond *> (use_stmt);
    2739         1044 :                       gimple_cond_set_lhs (cond_stmt, crhs1);
    2740         1044 :                       gimple_cond_set_rhs (cond_stmt, crhs2);
    2741              :                     }
    2742          339 :                   else if (gimple_assign_rhs_class (use_stmt)
    2743              :                            == GIMPLE_BINARY_RHS)
    2744              :                     {
    2745          339 :                       gimple_assign_set_rhs1 (use_stmt, crhs1);
    2746          339 :                       gimple_assign_set_rhs2 (use_stmt, crhs2);
    2747              :                     }
    2748              :                 }
    2749         2606 :               update_stmt (use_stmt);
    2750         2606 :               if (atomic_op != BIT_AND_EXPR
    2751         2606 :                   && atomic_op != BIT_IOR_EXPR
    2752         2606 :                   && !stmt_ends_bb_p (stmt2))
    2753              :                 {
    2754              :                   /* For the benefit of debug stmts, emit stmt(s) to set
    2755              :                      lhs2 to the value it had from the new builtin.
    2756              :                      E.g. if it was previously:
    2757              :                      lhs2 = __atomic_fetch_add_8 (ptr, arg, 0);
    2758              :                      emit:
    2759              :                      new_lhs = __atomic_add_fetch_8 (ptr, arg, 0);
    2760              :                      lhs2 = new_lhs - arg;
    2761              :                      We also keep cast_stmt if any in the IL for
    2762              :                      the same reasons.
    2763              :                      These stmts will be DCEd later and proper debug info
    2764              :                      will be emitted.
    2765              :                      This is only possible for reversible operations
    2766              :                      (+/-/^) and without -fnon-call-exceptions.  */
    2767         2265 :                   gsi = gsi_for_stmt (stmt2);
    2768         2265 :                   tree type = TREE_TYPE (lhs2);
    2769         2265 :                   if (TREE_CODE (arg) == INTEGER_CST)
    2770         1683 :                     arg = fold_convert (type, arg);
    2771          582 :                   else if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
    2772              :                     {
    2773            0 :                       tree narg = make_ssa_name (type);
    2774            0 :                       gimple *g = gimple_build_assign (narg, NOP_EXPR, arg);
    2775            0 :                       gsi_insert_after (&gsi, g, GSI_NEW_STMT);
    2776            0 :                       arg = narg;
    2777              :                     }
    2778         2265 :                   enum tree_code rcode;
    2779         2265 :                   switch (atomic_op)
    2780              :                     {
    2781              :                     case PLUS_EXPR: rcode = MINUS_EXPR; break;
    2782          727 :                     case MINUS_EXPR: rcode = PLUS_EXPR; break;
    2783          492 :                     case BIT_XOR_EXPR: rcode = atomic_op; break;
    2784              :                     default: gcc_unreachable ();
    2785              :                     }
    2786         2265 :                   gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
    2787         2265 :                   gsi_insert_after (&gsi, g, GSI_NEW_STMT);
    2788         2265 :                   update_stmt (stmt2);
    2789              :                 }
    2790              :               else
    2791              :                 {
    2792              :                   /* For e.g.
    2793              :                      lhs2 = __atomic_fetch_or_8 (ptr, arg, 0);
    2794              :                      after we change it to
    2795              :                      new_lhs = __atomic_or_fetch_8 (ptr, arg, 0);
    2796              :                      there is no way to find out the lhs2 value (i.e.
    2797              :                      what the atomic memory contained before the operation),
    2798              :                      values of some bits are lost.  We have checked earlier
    2799              :                      that we don't have any non-debug users except for what
    2800              :                      we are already changing, so we need to reset the
    2801              :                      debug stmts and remove the cast_stmt if any.  */
    2802          341 :                   imm_use_iterator iter;
    2803          676 :                   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2)
    2804          335 :                     if (use_stmt != cast_stmt)
    2805              :                       {
    2806          168 :                         gcc_assert (is_gimple_debug (use_stmt));
    2807          168 :                         gimple_debug_bind_reset_value (use_stmt);
    2808          168 :                         update_stmt (use_stmt);
    2809          341 :                       }
    2810          341 :                   if (cast_stmt)
    2811              :                     {
    2812          167 :                       gsi = gsi_for_stmt (cast_stmt);
    2813          167 :                       gsi_remove (&gsi, true);
    2814              :                     }
    2815          341 :                   update_stmt (stmt2);
    2816          341 :                   release_ssa_name (lhs2);
    2817              :                 }
    2818              :             }
    2819              :         }
    2820              :       break;
    2821              : 
    2822              :     default:
    2823              :       break;
    2824              :     }
    2825              :   return false;
    2826              : }
    2827              : 
    2828              : /* Given a ssa_name in NAME see if it was defined by an assignment and
    2829              :    set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
    2830              :    to the second operand on the rhs. */
    2831              : 
    2832              : static inline void
    2833     17605295 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
    2834              : {
    2835     17605295 :   gimple *def;
    2836     17605295 :   enum tree_code code1;
    2837     17605295 :   tree arg11;
    2838     17605295 :   tree arg21;
    2839     17605295 :   tree arg31;
    2840     17605295 :   enum gimple_rhs_class grhs_class;
    2841              : 
    2842     17605295 :   code1 = TREE_CODE (name);
    2843     17605295 :   arg11 = name;
    2844     17605295 :   arg21 = NULL_TREE;
    2845     17605295 :   arg31 = NULL_TREE;
    2846     17605295 :   grhs_class = get_gimple_rhs_class (code1);
    2847              : 
    2848     17605295 :   if (code1 == SSA_NAME)
    2849              :     {
    2850     11691475 :       def = SSA_NAME_DEF_STMT (name);
    2851              : 
    2852     11691475 :       if (def && is_gimple_assign (def)
    2853     18935479 :           && can_propagate_from (def))
    2854              :         {
    2855      4956815 :           code1 = gimple_assign_rhs_code (def);
    2856      4956815 :           arg11 = gimple_assign_rhs1 (def);
    2857      4956815 :           arg21 = gimple_assign_rhs2 (def);
    2858      4956815 :           arg31 = gimple_assign_rhs3 (def);
    2859              :         }
    2860              :     }
    2861      5913820 :   else if (grhs_class != GIMPLE_SINGLE_RHS)
    2862            0 :     code1 = ERROR_MARK;
    2863              : 
    2864     17605295 :   *code = code1;
    2865     17605295 :   *arg1 = arg11;
    2866     17605295 :   if (arg2)
    2867     17588036 :     *arg2 = arg21;
    2868     17605295 :   if (arg31)
    2869         2688 :     *code = ERROR_MARK;
    2870     17605295 : }
    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     10335616 : simplify_rotate (gimple_stmt_iterator *gsi)
    2918              : {
    2919     10335616 :   gimple *stmt = gsi_stmt (*gsi);
    2920     10335616 :   tree arg[2], rtype, rotcnt = NULL_TREE;
    2921     10335616 :   tree def_arg1[2], def_arg2[2];
    2922     10335616 :   enum tree_code def_code[2];
    2923     10335616 :   tree lhs;
    2924     10335616 :   int i;
    2925     10335616 :   bool swapped_p = false;
    2926     10335616 :   gimple *g;
    2927     10335616 :   gimple *def_arg_stmt[2] = { NULL, NULL };
    2928     10335616 :   int wider_prec = 0;
    2929     10335616 :   bool add_masking = false;
    2930              : 
    2931     10335616 :   arg[0] = gimple_assign_rhs1 (stmt);
    2932     10335616 :   arg[1] = gimple_assign_rhs2 (stmt);
    2933     10335616 :   rtype = TREE_TYPE (arg[0]);
    2934              : 
    2935              :   /* Only create rotates in complete modes.  Other cases are not
    2936              :      expanded properly.  */
    2937     10335616 :   if (!INTEGRAL_TYPE_P (rtype)
    2938     10335616 :       || !type_has_mode_precision_p (rtype))
    2939              :     return false;
    2940              : 
    2941     26256303 :   for (i = 0; i < 2; i++)
    2942              :     {
    2943     17504202 :       defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2944     17504202 :       if (TREE_CODE (arg[i]) == SSA_NAME)
    2945     11590382 :         def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
    2946              :     }
    2947              : 
    2948              :   /* Look through narrowing (or same precision) conversions.  */
    2949      7792387 :   if (CONVERT_EXPR_CODE_P (def_code[0])
    2950       959714 :       && CONVERT_EXPR_CODE_P (def_code[1])
    2951       143608 :       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
    2952       119816 :       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
    2953       112139 :       && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
    2954       112139 :          == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
    2955        65791 :       && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
    2956        47163 :       && has_single_use (arg[0])
    2957      8786417 :       && has_single_use (arg[1]))
    2958              :     {
    2959        29225 :       wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
    2960        87675 :       for (i = 0; i < 2; i++)
    2961              :         {
    2962        58450 :           arg[i] = def_arg1[i];
    2963        58450 :           defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2964        58450 :           if (TREE_CODE (arg[i]) == SSA_NAME)
    2965        58450 :             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      8722876 :       i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
    2973      7774828 :       if (CONVERT_EXPR_CODE_P (def_code[i])
    2974       948048 :           && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
    2975        32080 :           && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
    2976        30818 :           && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
    2977      8727849 :           && has_single_use (arg[i]))
    2978              :         {
    2979         2119 :           arg[i] = def_arg1[i];
    2980         2119 :           defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2981         2119 :           if (TREE_CODE (arg[i]) == SSA_NAME)
    2982         2119 :             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      8938065 :   for (i = 0; i < 2; i++)
    2988      8913314 :     if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
    2989              :       return false;
    2990       226903 :     else if (!has_single_use (arg[i]))
    2991              :       return false;
    2992        24751 :   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        20450 :   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        20449 :   if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
    3012        32367 :       || !types_compatible_p (TREE_TYPE (def_arg1[0]),
    3013        11918 :                               TREE_TYPE (def_arg1[1])))
    3014              :     {
    3015         8531 :       if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
    3016         8531 :            != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
    3017         8531 :           || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
    3018         8531 :               == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
    3019         8507 :         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          547 :       i = def_code[0] != RSHIFT_EXPR;
    3024          547 :       if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
    3025              :         return false;
    3026              : 
    3027          506 :       tree tem;
    3028          506 :       enum tree_code code;
    3029          506 :       defcodefor_name (def_arg1[i], &code, &tem, NULL);
    3030          303 :       if (!CONVERT_EXPR_CODE_P (code)
    3031          203 :           || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
    3032          709 :           || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
    3033              :         return false;
    3034          194 :       def_arg1[i] = tem;
    3035          194 :       if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
    3036          218 :           || !types_compatible_p (TREE_TYPE (def_arg1[0]),
    3037           24 :                                   TREE_TYPE (def_arg1[1])))
    3038              :         return false;
    3039              :     }
    3040        11918 :   else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
    3041              :     return false;
    3042              : 
    3043              :   /* CNT1 + CNT2 == B case above.  */
    3044        10687 :   if (tree_fits_uhwi_p (def_arg2[0])
    3045         1190 :       && tree_fits_uhwi_p (def_arg2[1])
    3046        10687 :       && tree_to_uhwi (def_arg2[0])
    3047         1190 :          + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype))
    3048              :     rotcnt = def_arg2[0];
    3049         9764 :   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           17 : check_table_array (tree ctor, HOST_WIDE_INT &zero_val, unsigned bits,
    3297              :                   ValidateFn validate_fn)
    3298              : {
    3299              :   tree elt, idx;
    3300           17 :   unsigned HOST_WIDE_INT i, raw_idx = 0;
    3301           17 :   unsigned matched = 0;
    3302              : 
    3303           17 :   zero_val = 0;
    3304              : 
    3305          734 :   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
    3306              :     {
    3307          734 :       if (!tree_fits_shwi_p (idx))
    3308              :         return false;
    3309          734 :       if (!tree_fits_shwi_p (elt) && TREE_CODE (elt) != RAW_DATA_CST)
    3310              :         return false;
    3311              : 
    3312          734 :       unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
    3313              :       HOST_WIDE_INT val;
    3314              : 
    3315          734 :       if (TREE_CODE (elt) == INTEGER_CST)
    3316          670 :         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          734 :       if (index > bits * 2)
    3334              :         return false;
    3335              : 
    3336          734 :       if (index == 0)
    3337              :         {
    3338           17 :           zero_val = val;
    3339           17 :           matched++;
    3340              :         }
    3341              : 
    3342          734 :       if (val >= 0 && val < bits && validate_fn (val, index))
    3343          672 :         matched++;
    3344              : 
    3345          734 :       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            4 : check_table_string (tree string, HOST_WIDE_INT &zero_val,unsigned bits,
    3356              :                     ValidateFn validate_fn)
    3357              : {
    3358            4 :   unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
    3359            4 :   unsigned matched = 0;
    3360            4 :   const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
    3361              : 
    3362            4 :   if (len < bits || len > bits * 2)
    3363              :     return false;
    3364              : 
    3365            4 :   zero_val = p[0];
    3366              : 
    3367          164 :   for (unsigned i = 0; i < len; i++)
    3368          160 :     if (p[i] < bits && validate_fn (p[i], i))
    3369          160 :       matched++;
    3370              : 
    3371            4 :   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           29 : check_table (tree ctor, tree type, HOST_WIDE_INT &zero_val, unsigned bits,
    3378              :              ValidateFn validate_fn)
    3379              : {
    3380           29 :   if (TREE_CODE (ctor) == CONSTRUCTOR)
    3381           17 :     return check_table_array (ctor, zero_val, bits, validate_fn);
    3382              :   else if (TREE_CODE (ctor) == STRING_CST
    3383           12 :            && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
    3384            4 :     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      2003925 : simplify_count_zeroes (gimple_stmt_iterator *gsi)
    3403              : {
    3404      2003925 :   gimple *stmt = gsi_stmt (*gsi);
    3405      2003925 :   tree array_ref = gimple_assign_rhs1 (stmt);
    3406      2003925 :   tree res_ops[3];
    3407              : 
    3408      2003925 :   gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
    3409              : 
    3410      2003925 :   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      2003925 :   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      2003925 :   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      2003902 :   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      2003892 :   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           34 :   HOST_WIDE_INT zero_val;
    3447           34 :   tree type = TREE_TYPE (array_ref);
    3448           34 :   tree array = TREE_OPERAND (array_ref, 0);
    3449           34 :   tree input_type = TREE_TYPE (res_ops[0]);
    3450           34 :   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           34 :   if (!INTEGRAL_TYPE_P (type) || TYPE_PRECISION (type) > 64
    3458           68 :       || !TYPE_UNSIGNED (input_type))
    3459              :     return false;
    3460           30 :   if (input_bits != 32 && input_bits != 64)
    3461              :     return false;
    3462              : 
    3463           30 :   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           30 :   tree low = array_ref_low_bound (array_ref);
    3468           30 :   if (!low || !integer_zerop (low))
    3469              :     return false;
    3470              : 
    3471              :   /* Check the shift extracts the top 5..7 bits.  */
    3472           30 :   unsigned shiftval = tree_to_shwi (res_ops[2]);
    3473           30 :   if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
    3474              :     return false;
    3475              : 
    3476           29 :   tree ctor = ctor_for_folding (array);
    3477           29 :   if (!ctor)
    3478              :     return false;
    3479           29 :   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           29 :   if (fn == IFN_CTZ || clz_via_ctz)
    3483              :     {
    3484          559 :       auto checkfn = [&](unsigned data, unsigned i) -> bool
    3485              :         {
    3486          540 :           unsigned HOST_WIDE_INT mask
    3487          540 :             = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
    3488          540 :           return (((mulval << data) & mask) >> shiftval) == i;
    3489           19 :         };
    3490           19 :       if (!check_table (ctor, type, zero_val, input_bits, checkfn))
    3491            8 :         return false;
    3492              :     }
    3493           10 :   else if (fn == IFN_CLZ)
    3494              :     {
    3495          362 :       auto checkfn = [&](unsigned data, unsigned i) -> bool
    3496              :         {
    3497          352 :           unsigned HOST_WIDE_INT mask
    3498          352 :             = ((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          703 :           unsigned HOST_WIDE_INT all_bits_below
    3509              :             = (data + 1 == HOST_BITS_PER_WIDE_INT)
    3510          352 :               ? HOST_WIDE_INT_M1U
    3511          351 :               : ((HOST_WIDE_INT_1U << (data + 1)) - 1);
    3512          352 :           return (((all_bits_below * mulval) & mask) >> shiftval) == i;
    3513           10 :         };
    3514           10 :     if (!check_table (ctor, type, zero_val, input_bits, checkfn))
    3515            0 :       return false;
    3516              :     }
    3517              : 
    3518           21 :   HOST_WIDE_INT ctz_val = -1;
    3519           21 :   bool zero_ok;
    3520           21 :   if (fn == IFN_CTZ)
    3521              :     {
    3522           10 :       ctz_val = 0;
    3523           20 :       zero_ok = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
    3524              :                                            ctz_val) == 2;
    3525              :     }
    3526           11 :   else if (fn == IFN_CLZ)
    3527              :     {
    3528           11 :       ctz_val = 32;
    3529           11 :       zero_ok = CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
    3530              :                                            ctz_val) == 2;
    3531           11 :       zero_val = input_bits - 1 - zero_val;
    3532              :     }
    3533           21 :   int nargs = 2;
    3534              : 
    3535              :   /* If the input value can't be zero, don't special case ctz (0).  */
    3536           21 :   range_query *q = get_range_query (cfun);
    3537           21 :   if (q == get_global_range_query ())
    3538           21 :     q = enable_ranger (cfun);
    3539           21 :   int_range_max vr;
    3540           21 :   if (q->range_of_expr (vr, res_ops[0], stmt)
    3541           21 :       && !range_includes_zero_p (vr))
    3542              :     {
    3543            4 :       zero_ok = true;
    3544            4 :       zero_val = 0;
    3545            4 :       ctz_val = 0;
    3546            4 :       nargs = 1;
    3547              :     }
    3548              : 
    3549           21 :   gimple_seq seq = NULL;
    3550           21 :   gimple *g;
    3551           21 :   gcall *call = gimple_build_call_internal (fn, nargs, res_ops[0],
    3552              :                                             nargs == 1 ? NULL_TREE
    3553           38 :                                             : build_int_cst (integer_type_node,
    3554           17 :                                                              ctz_val));
    3555           21 :   gimple_set_location (call, gimple_location (stmt));
    3556           21 :   gimple_set_lhs (call, make_ssa_name (integer_type_node));
    3557           21 :   gimple_seq_add_stmt (&seq, call);
    3558              : 
    3559           21 :   tree prev_lhs = gimple_call_lhs (call);
    3560              : 
    3561           21 :   if (zero_ok && zero_val == ctz_val)
    3562              :     ;
    3563              :   /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0.  */
    3564            6 :   else if (zero_ok && zero_val == 0 && ctz_val == input_bits)
    3565              :     {
    3566            5 :       g = gimple_build_assign (make_ssa_name (integer_type_node),
    3567              :                                BIT_AND_EXPR, prev_lhs,
    3568              :                                build_int_cst (integer_type_node,
    3569            5 :                                               input_bits - 1));
    3570            5 :       gimple_set_location (g, gimple_location (stmt));
    3571            5 :       gimple_seq_add_stmt (&seq, g);
    3572            5 :       prev_lhs = gimple_assign_lhs (g);
    3573              :     }
    3574              :   /* As fallback emit a conditional move.  */
    3575              :   else
    3576              :     {
    3577           10 :       g = gimple_build_assign (make_ssa_name (boolean_type_node), EQ_EXPR,
    3578              :                                res_ops[0], build_zero_cst (input_type));
    3579           10 :       gimple_set_location (g, gimple_location (stmt));
    3580           10 :       gimple_seq_add_stmt (&seq, g);
    3581           10 :       tree cond = gimple_assign_lhs (g);
    3582           10 :       g = gimple_build_assign (make_ssa_name (integer_type_node),
    3583              :                                COND_EXPR, cond,
    3584           10 :                                build_int_cst (integer_type_node, zero_val),
    3585              :                                prev_lhs);
    3586           10 :       gimple_set_location (g, gimple_location (stmt));
    3587           10 :       gimple_seq_add_stmt (&seq, g);
    3588           10 :       prev_lhs = gimple_assign_lhs (g);
    3589              :     }
    3590              : 
    3591           21 :   if (fn == IFN_CLZ)
    3592              :     {
    3593           11 :       g = gimple_build_assign (make_ssa_name (integer_type_node),
    3594              :                                MINUS_EXPR,
    3595              :                                build_int_cst (integer_type_node,
    3596           11 :                                               input_bits - 1),
    3597              :                                prev_lhs);
    3598           11 :       gimple_set_location (g, gimple_location (stmt));
    3599           11 :       gimple_seq_add_stmt (&seq, g);
    3600           11 :       prev_lhs = gimple_assign_lhs (g);
    3601              :     }
    3602              : 
    3603           21 :   g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
    3604           21 :   gimple_seq_add_stmt (&seq, g);
    3605           21 :   gsi_replace_with_seq (gsi, seq, true);
    3606           21 :   return true;
    3607           21 : }
    3608              : 
    3609              : /* Long-multiply fold framework.
    3610              : 
    3611              :    Walks the outer addition or bit_ior chain on a candidate statement,
    3612              :    classifies each summand against the atom match patterns from
    3613              :    match.pd, and looks the resulting multiset of (kind, extract) tuples
    3614              :    up in a table.  On a hit, three cross-summand consistency checks
    3615              :    decide whether the wide multiply is emitted.  */
    3616              : 
    3617              : /* Match.pd recognizers for the conditional carry-add pattern. */
    3618              : 
    3619              : extern bool gimple_cond_carry_add (tree, tree *, tree (*)(tree));
    3620              : 
    3621              : /* Match.pd functions to match long multiplication.  */
    3622              : 
    3623              : extern bool gimple_mul_hi (tree, tree *, tree (*)(tree));
    3624              : extern bool gimple_mul_lo (tree, tree *, tree (*)(tree));
    3625              : extern bool gimple_mul_hilo (tree, tree *, tree (*)(tree));
    3626              : extern bool gimple_mul_lolo (tree, tree *, tree (*)(tree));
    3627              : extern bool gimple_mul_hihi (tree, tree *, tree (*)(tree));
    3628              : extern bool gimple_mul_cross_sum (tree, tree *, tree (*)(tree));
    3629              : extern bool gimple_mul_low_sum (tree, tree *, tree (*)(tree));
    3630              : extern bool gimple_mul_low_accum (tree, tree *, tree (*)(tree));
    3631              : extern bool gimple_mul_carry_cross_sum (tree, tree *, tree (*)(tree));
    3632              : extern bool gimple_mul_carry_low_sum (tree, tree *, tree (*)(tree));
    3633              : extern bool gimple_mul_carry_low (tree, tree *, tree (*)(tree));
    3634              : extern bool gimple_mul_ladder_sum1 (tree, tree *, tree (*)(tree));
    3635              : extern bool gimple_mul_ladder_sum2 (tree, tree *, tree (*)(tree));
    3636              : extern bool gimple_mul_ladder_sum3 (tree, tree *, tree (*)(tree));
    3637              : extern bool gimple_mul_ladder_part_sum (tree, tree *, tree (*)(tree));
    3638              : 
    3639              : /* Append to SEQ statements assigning DEST the high-part multiply of
    3640              :    OP1 and OP2, emitted as
    3641              :      (N)(((2N) op1 * (2N) op2) >> N).
    3642              :    pass_optimize_widening_mul's convert_mult_to_widen and
    3643              :    convert_mult_to_highpart later rewrite this to a single
    3644              :    WIDEN_MULT_EXPR or MULT_HIGHPART_EXPR when the target supports it,
    3645              :    otherwise the 2N multiply expands directly.  Emitting the canonical
    3646              :    widening shape keeps target-capability decisions in the layer that
    3647              :    already owns them.  */
    3648              : 
    3649              : static void
    3650         1919 : build_mul_high_seq (tree op1, tree op2, tree dest, location_t loc,
    3651              :                     gimple_seq *seq)
    3652              : {
    3653         1919 :   tree op_type = TREE_TYPE (op1);
    3654         1919 :   unsigned int width = TYPE_PRECISION (op_type);
    3655         1919 :   tree wide_type = build_nonstandard_integer_type (width * 2, 1);
    3656              : 
    3657         1919 :   tree wide_a = gimple_convert (seq, loc, wide_type, op1);
    3658         1919 :   tree wide_b = gimple_convert (seq, loc, wide_type, op2);
    3659         1919 :   tree wide_prod = gimple_build (seq, loc, MULT_EXPR, wide_type,
    3660              :                                  wide_a, wide_b);
    3661         1919 :   tree hi = gimple_build (seq, loc, RSHIFT_EXPR, wide_type, wide_prod,
    3662         1919 :                           build_int_cst (integer_type_node, width));
    3663              : 
    3664         1919 :   gimple *prod = gimple_build_assign (dest, NOP_EXPR, hi);
    3665         1919 :   gimple_set_location (prod, loc);
    3666         1919 :   gimple_seq_add_stmt (seq, prod);
    3667         1919 : }
    3668              : 
    3669              : /* Append to SEQ statements combining ACC with each of EXTRAS under
    3670              :    OUTER, the last one assigning to STMT's lhs.  EXTRAS are leaves of
    3671              :    STMT's own chain, so any combining order is valid.  */
    3672              : 
    3673              : static void
    3674          361 : long_mul_apply_extras (tree acc, const vec<tree> &extras, tree_code outer,
    3675              :                        gassign *stmt, gimple_seq *seq)
    3676              : {
    3677          361 :   location_t loc = gimple_location (stmt);
    3678          361 :   tree lhs = gimple_assign_lhs (stmt);
    3679          388 :   for (unsigned i = 0; i + 1 < extras.length (); i++)
    3680           27 :     acc = gimple_build (seq, loc, outer, TREE_TYPE (lhs), acc, extras[i]);
    3681          361 :   gimple *last = gimple_build_assign (lhs, outer, acc, extras.last ());
    3682          361 :   gimple_set_location (last, loc);
    3683          361 :   gimple_seq_add_stmt (seq, last);
    3684          361 : }
    3685              : 
    3686              : /* Replace STMT with a high-part multiply of OP1 and OP2, combining any
    3687              :    EXTRAS back on top under OUTER.  */
    3688              : 
    3689              : static void
    3690         1905 : create_mul_high_seq (tree op1, tree op2, gassign *stmt,
    3691              :                      const vec<tree> &extras, tree_code outer)
    3692              : {
    3693         1905 :   gimple_seq seq = NULL;
    3694         1905 :   tree lhs = gimple_assign_lhs (stmt);
    3695         2266 :   tree dest = extras.is_empty () ? lhs : make_ssa_name (TREE_TYPE (lhs));
    3696         1905 :   build_mul_high_seq (op1, op2, dest, gimple_location (stmt), &seq);
    3697         1905 :   if (!extras.is_empty ())
    3698          361 :     long_mul_apply_extras (dest, extras, outer, stmt, &seq);
    3699         1905 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    3700         1905 :   gsi_replace_with_seq (&gsi, seq, true);
    3701         1905 : }
    3702              : 
    3703              : /* Replace STMT with a low-part multiply of OP1 and OP2, combining any
    3704              :    EXTRAS back on top under OUTER.  */
    3705              : 
    3706              : static void
    3707           32 : create_mul_low_seq (tree op1, tree op2, gassign *stmt,
    3708              :                     const vec<tree> &extras, tree_code outer)
    3709              : {
    3710           32 :   gimple_seq seq = NULL;
    3711           32 :   tree lhs = gimple_assign_lhs (stmt);
    3712           32 :   tree dest = extras.is_empty () ? lhs : make_ssa_name (TREE_TYPE (lhs));
    3713           32 :   gimple *prod = gimple_build_assign (dest, MULT_EXPR, op1, op2);
    3714           32 :   gimple_set_location (prod, gimple_location (stmt));
    3715           32 :   gimple_seq_add_stmt (&seq, prod);
    3716           32 :   if (!extras.is_empty ())
    3717            0 :     long_mul_apply_extras (dest, extras, outer, stmt, &seq);
    3718           32 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    3719           32 :   gsi_replace_with_seq (&gsi, seq, true);
    3720           32 : }
    3721              : 
    3722              : /* Widest match.pd atom (mul_carry_low_sum) takes 7 captures; round up
    3723              :    to 8 for the scratch buffers below.  */
    3724              : static constexpr unsigned LONG_MUL_MAX_CAPTURES = 8;
    3725              : 
    3726              : /* Longest variant in long_mul_table has 4 summands.  */
    3727              : static constexpr unsigned LONG_MUL_MAX_SUMMANDS = 4;
    3728              : 
    3729              : /* Cap on the leaves set aside as not part of the idiom, so an
    3730              :    arbitrarily long unrelated chain still bails early.  */
    3731              : static constexpr unsigned LONG_MUL_MAX_EXTRAS = 4;
    3732              : 
    3733              : namespace {
    3734              : 
    3735              : enum long_mul_kind : unsigned char {
    3736              :   LMK_INVALID,
    3737              :   LMK_MUL_HIHI,
    3738              :   LMK_MUL_LOLO,
    3739              :   LMK_MUL_HILO,
    3740              :   LMK_CROSS_SUM,
    3741              :   LMK_LOW_ACCUM,
    3742              :   LMK_LOW_SUM,
    3743              :   LMK_LADDER_SUM1,
    3744              :   LMK_LADDER_SUM2,
    3745              :   LMK_LADDER_SUM3,
    3746              :   LMK_LADDER_PART_SUM,
    3747              :   LMK_CARRY_LOW,
    3748              :   LMK_CARRY_CROSS_SUM,
    3749              :   LMK_CARRY_LOW_SUM,
    3750              : };
    3751              : 
    3752              : /* How the leaf wraps its inner kind.  Carry kinds use LMX_NONE: their
    3753              :    match.pd pattern bakes the lshift in, so the leaf is already the
    3754              :    complete carry expression.  */
    3755              : 
    3756              : enum long_mul_extract : unsigned char  {
    3757              :   LMX_NONE,
    3758              :   LMX_HI,
    3759              :   LMX_LO,
    3760              :   LMX_SHL_N,
    3761              : };
    3762              : 
    3763              : struct long_mul_summand {
    3764      6966992 :   long_mul_summand () {}
    3765              :   long_mul_summand (tree);
    3766              :   long_mul_summand (const long_mul_summand &) = default;
    3767              :   long_mul_kind kind = LMK_INVALID;
    3768              :   long_mul_extract extract = LMX_NONE;
    3769              :   tree op0 = NULL_TREE, op1 = NULL_TREE;
    3770              :   tree hilo[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
    3771              :   tree carry_a = NULL_TREE, carry_b = NULL_TREE;
    3772              :   unsigned HOST_WIDE_INT shift = 0;
    3773      6947895 :   operator bool () { return kind != LMK_INVALID; }
    3774              : private:
    3775              :   bool classify_carry (tree leaf);
    3776              :   bool classify_hi_extract (tree, unsigned HOST_WIDE_INT);
    3777              :   bool classify_lo_extract (tree);
    3778              :   bool classify_shl_extract (tree, unsigned HOST_WIDE_INT);
    3779              :   bool classify_plus_kinds (tree);
    3780              :   bool classify_bare (tree);
    3781              :   void set (long_mul_kind kind, const tree *res_ops);
    3782              : };
    3783              : 
    3784              : }
    3785              : 
    3786              : /* Walk the OUTER addition or BIT_IOR chain rooted at STMT and collect
    3787              :    the leaf operands into LEAVES.  Descends through single-use
    3788              :    intermediate stmts of the same code.  Returns false once the leaf
    3789              :    count exceeds LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS, so an
    3790              :    overlong chain bails mid-walk instead of after a full traversal.
    3791              : 
    3792              :    If SHARED_DEF_OUT is non-NULL, record there the first inner stmt that
    3793              :    shares the outer code but has more than one use -- descending into it
    3794              :    would change semantics, so it stays as a leaf.  Such a leaf often
    3795              :    classifies as something no row matches, silently disabling the fold;
    3796              :    the caller surfaces this as a dump-file hint.  */
    3797              : 
    3798              : static bool
    3799      3391076 : long_mul_linearize_chain (gimple *stmt, tree_code outer, vec<tree> &leaves,
    3800              :                           gimple **shared_def_out = NULL)
    3801              : {
    3802      3391076 :   auto_vec<tree, 8> stack;
    3803      3391076 :   stack.safe_push (gimple_assign_rhs2 (stmt));
    3804      3391076 :   stack.safe_push (gimple_assign_rhs1 (stmt));
    3805              : 
    3806     10537777 :   while (!stack.is_empty ())
    3807              :     {
    3808      7147498 :       tree t = stack.pop ();
    3809      7147498 :       if (TREE_CODE (t) == SSA_NAME)
    3810              :         {
    3811      4684121 :           gimple *def = SSA_NAME_DEF_STMT (t);
    3812      4684121 :           if (def
    3813      4684121 :               && is_gimple_assign (def)
    3814      7915690 :               && gimple_assign_rhs_code (def) == outer)
    3815              :             {
    3816       345983 :               if (has_single_use (t))
    3817              :                 {
    3818       185021 :                   stack.safe_push (gimple_assign_rhs2 (def));
    3819       185021 :                   stack.safe_push (gimple_assign_rhs1 (def));
    3820       185021 :                   continue;
    3821              :                 }
    3822       160962 :               if (shared_def_out && !*shared_def_out)
    3823       145969 :                 *shared_def_out = def;
    3824              :             }
    3825              :         }
    3826      6962477 :       leaves.safe_push (t);
    3827      6962477 :       if (leaves.length () > LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS)
    3828          797 :         return false;
    3829              :     }
    3830      6780558 :   return !leaves.is_empty ();
    3831      3391076 : }
    3832              : 
    3833              : /* If EXPR is defined by LSHIFT_EXPR with a uhwi-valued amount, return
    3834              :    the shifted input via *INNER_OUT and the amount via *SHIFT_OUT.  */
    3835              : 
    3836              : static bool
    3837      6888137 : long_mul_is_lshift_def (tree expr, tree *inner_out,
    3838              :                         unsigned HOST_WIDE_INT *shift_out)
    3839              : {
    3840      6888137 :   if (TREE_CODE (expr) != SSA_NAME)
    3841              :     return false;
    3842      4425239 :   gimple *def = SSA_NAME_DEF_STMT (expr);
    3843      4425239 :   if (!def || !is_gimple_assign (def)
    3844      7398688 :       || gimple_assign_rhs_code (def) != LSHIFT_EXPR)
    3845              :     return false;
    3846        84871 :   tree amount = gimple_assign_rhs2 (def);
    3847        84871 :   if (!tree_fits_uhwi_p (amount))
    3848              :     return false;
    3849        58649 :   *inner_out = gimple_assign_rhs1 (def);
    3850        58649 :   *shift_out = tree_to_uhwi (amount);
    3851        58649 :   return true;
    3852              : }
    3853              : 
    3854              : /* Fill THIS's kind plus the captures from RES_OPS that the kind requires.
    3855              :    The kind itself determines how many (op0, op1) and hilo captures to
    3856              :    pick up from RES_OPS, and whether a baked-in shift is present.  */
    3857              : 
    3858              : void
    3859        55477 : long_mul_summand::set (long_mul_kind kind, const tree *res_ops)
    3860              : {
    3861        55477 :   this->kind = kind;
    3862        55477 :   unsigned n_ops = 0;
    3863        55477 :   unsigned n_hilos = 0;
    3864        55477 :   int shift_idx = -1;
    3865        53030 :   switch (kind)
    3866              :     {
    3867            0 :     default:
    3868            0 :       gcc_unreachable ();
    3869              :     case LMK_MUL_HIHI:
    3870              :     case LMK_MUL_LOLO:
    3871              :     case LMK_MUL_HILO:
    3872              :       n_ops = 2;
    3873              :       break;
    3874         5735 :     case LMK_CROSS_SUM:
    3875         5735 :       n_hilos = 2;
    3876         5735 :       break;
    3877        18568 :     case LMK_LOW_ACCUM:
    3878        18568 :     case LMK_LOW_SUM:
    3879        18568 :     case LMK_LADDER_SUM1:
    3880        18568 :     case LMK_LADDER_SUM2:
    3881        18568 :     case LMK_LADDER_SUM3:
    3882        18568 :       n_ops = 2;
    3883        18568 :       n_hilos = 2;
    3884        18568 :       break;
    3885          135 :     case LMK_LADDER_PART_SUM:
    3886          135 :       n_ops = 2;
    3887          135 :       n_hilos = 1;
    3888          135 :       break;
    3889              :     case LMK_CARRY_CROSS_SUM:
    3890              :       n_hilos = 3;
    3891              :       shift_idx = 3;
    3892              :       break;
    3893          106 :     case LMK_CARRY_LOW_SUM:
    3894          106 :       n_ops = 2;
    3895          106 :       n_hilos = 3;
    3896          106 :       shift_idx = 5;
    3897          106 :       break;
    3898         2447 :     case LMK_CARRY_LOW:
    3899         2447 :       this->carry_a = res_ops[0];
    3900         2447 :       this->carry_b = res_ops[1];
    3901            0 :       return;
    3902              :     }
    3903        24544 :   if (n_ops >= 1)
    3904        47125 :     this->op0 = res_ops[0];
    3905        47125 :   if (n_ops >= 2)
    3906        47125 :     this->op1 = res_ops[1];
    3907        53030 :   gcc_checking_assert (n_hilos <= 3);
    3908       102599 :   for (unsigned i = 0; i < n_hilos; i++)
    3909        49569 :     this->hilo[i] = res_ops[n_ops + i];
    3910        53030 :   if (shift_idx >= 0)
    3911              :     /* The carry atoms (mul_carry_cross_sum, mul_carry_low_sum) capture the
    3912              :        shift since it is always less than TYPE_PRECISION, using tree_to_uhwi is safe.  */
    3913          276 :     this->shift = tree_to_uhwi (res_ops[shift_idx]);
    3914              : }
    3915              : 
    3916              : /* Classify LEAF as a carry-kind summand.  The lshift amount is baked
    3917              :    into mul_carry_cross_sum / mul_carry_low_sum, so they're tried before
    3918              :    any branch that looks for a generic (X >> N) or (X << N) wrapper.  */
    3919              : 
    3920              : bool
    3921      6947895 : long_mul_summand::classify_carry (tree leaf)
    3922              : {
    3923      6947895 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    3924              :   /* mul_carry_low_sum's inner is constrained to mul_low_sum (cross_sum
    3925              :      + mul_hi(mul_lolo)); mul_carry_cross_sum's inner is just
    3926              :      mul_cross_sum (any plus); mul_carry_low matches gt:c (@0, plus(@0,
    3927              :      @1)) without a baked-in shift.  Most specific first, so the
    3928              :      less-constrained pattern doesn't shadow the more-constrained one.  */
    3929      6947895 :   if (gimple_mul_carry_low_sum (leaf, res_ops, NULL))
    3930              :     {
    3931          106 :       set (LMK_CARRY_LOW_SUM, res_ops);
    3932          106 :       return true;
    3933              :     }
    3934      6947789 :   if (gimple_mul_carry_cross_sum (leaf, res_ops, NULL))
    3935              :     {
    3936          170 :       set (LMK_CARRY_CROSS_SUM, res_ops);
    3937          170 :       return true;
    3938              :     }
    3939      6947619 :   if (gimple_mul_carry_low (leaf, res_ops, NULL))
    3940              :     {
    3941         2447 :       set (LMK_CARRY_LOW, res_ops);
    3942         2447 :       return true;
    3943              :     }
    3944              :   return false;
    3945              : }
    3946              : 
    3947              : /* Plus-based summand kinds shared by the (X >> SHIFT) and (X << SHIFT)
    3948              :    classifiers.  Order is by specificity: mul_low_sum's first arm is any
    3949              :    plus, so mul_ladder_sum1/3 (which constrain that arm to a plus
    3950              :    containing a mul_lo) and mul_low_accum (which constrains both arms)
    3951              :    shadow it and must come first.  */
    3952              : 
    3953              : bool
    3954        84784 : long_mul_summand::classify_plus_kinds (tree inner)
    3955              : {
    3956        84784 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    3957        84784 :   if (gimple_mul_low_accum (inner, res_ops, NULL))
    3958              :     {
    3959           19 :       set (LMK_LOW_ACCUM, res_ops);
    3960           19 :       return true;
    3961              :     }
    3962        84765 :   if (gimple_mul_ladder_sum3 (inner, res_ops, NULL))
    3963              :     {
    3964           15 :       set (LMK_LADDER_SUM3, res_ops);
    3965           15 :       return true;
    3966              :     }
    3967        84750 :   if (gimple_mul_ladder_sum1 (inner, res_ops, NULL))
    3968              :     {
    3969        18214 :       set (LMK_LADDER_SUM1, res_ops);
    3970        18214 :       return true;
    3971              :     }
    3972        66536 :   if (gimple_mul_low_sum (inner, res_ops, NULL))
    3973              :     {
    3974          230 :       set (LMK_LOW_SUM, res_ops);
    3975          230 :       return true;
    3976              :     }
    3977        66306 :   if (gimple_mul_ladder_sum2 (inner, res_ops, NULL))
    3978              :     {
    3979           90 :       set (LMK_LADDER_SUM2, res_ops);
    3980           90 :       return true;
    3981              :     }
    3982              :   return false;
    3983              : }
    3984              : 
    3985              : /* Classify INNER -- already unwrapped from an outer (X >> SHIFT) -- as
    3986              :    a high-half-extracted summand.  mul_hilo (mult-shape) is orthogonal
    3987              :    to the plus-based kinds and is tried first; ladder_part_sum (one arm
    3988              :    unconstrained) and mul_cross_sum (any plus) are the fallbacks after
    3989              :    the shared plus-based ladder.  */
    3990              : 
    3991              : bool
    3992        35385 : long_mul_summand::classify_hi_extract (tree inner, unsigned HOST_WIDE_INT shift)
    3993              : {
    3994        35385 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    3995        35385 :   this->extract = LMX_HI;
    3996        35385 :   this->shift = shift;
    3997        35385 :   if (gimple_mul_hilo (inner, res_ops, NULL))
    3998              :     {
    3999         9238 :       set (LMK_MUL_HILO, res_ops);
    4000         9238 :       return true;
    4001              :     }
    4002        26147 :   if (classify_plus_kinds (inner))
    4003              :     return true;
    4004        16748 :   if (gimple_mul_ladder_part_sum (inner, res_ops, NULL))
    4005              :     {
    4006          135 :       set (LMK_LADDER_PART_SUM, res_ops);
    4007          135 :       return true;
    4008              :     }
    4009        16613 :   if (gimple_mul_cross_sum (inner, res_ops, NULL))
    4010              :     {
    4011         2805 :       set (LMK_CROSS_SUM, res_ops);
    4012         2805 :       return true;
    4013              :     }
    4014              :   return false;
    4015              : }
    4016              : 
    4017              : /* Classify INNER -- already unwrapped from an outer (X & MASK) -- as
    4018              :    a low-half-masked summand.  */
    4019              : 
    4020              : bool
    4021        21664 : long_mul_summand::classify_lo_extract (tree inner)
    4022              : {
    4023        21664 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4024        21664 :   this->extract = LMX_LO;
    4025        21664 :   if (gimple_mul_lolo (inner, res_ops, NULL))
    4026              :     {
    4027         9329 :       set (LMK_MUL_LOLO, res_ops);
    4028         9329 :       return true;
    4029              :     }
    4030              :   return false;
    4031              : }
    4032              : 
    4033              : /* Classify INNER -- already unwrapped from an outer (X << SHIFT) -- as
    4034              :    a left-shifted summand.  No mul_hilo / ladder_part_sum here -- those
    4035              :    shapes appear only under (X >> SHIFT).  */
    4036              : 
    4037              : bool
    4038        58637 : long_mul_summand::classify_shl_extract (tree inner,
    4039              :                                         unsigned HOST_WIDE_INT shift)
    4040              : {
    4041        58637 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4042        58637 :   this->extract = LMX_SHL_N;
    4043        58637 :   this->shift = shift;
    4044        58637 :   if (classify_plus_kinds (inner))
    4045              :     return true;
    4046        49468 :   if (gimple_mul_cross_sum (inner, res_ops, NULL))
    4047              :     {
    4048         2930 :       set (LMK_CROSS_SUM, res_ops);
    4049         2930 :       return true;
    4050              :     }
    4051              :   return false;
    4052              : }
    4053              : 
    4054              : /* Classify LEAF as one of the bare-kind summands (no extraction
    4055              :    wrapper): mul_hihi or mul_lolo standing on their own.  */
    4056              : 
    4057              : bool
    4058      6829486 : long_mul_summand::classify_bare (tree leaf)
    4059              : {
    4060      6829486 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4061      6829486 :   if (gimple_mul_hihi (leaf, res_ops, NULL))
    4062              :     {
    4063         9686 :       set (LMK_MUL_HIHI, res_ops);
    4064         9686 :       return true;
    4065              :     }
    4066      6819800 :   if (gimple_mul_lolo (leaf, res_ops, NULL))
    4067              :     {
    4068           63 :       set (LMK_MUL_LOLO, res_ops);
    4069           63 :       return true;
    4070              :     }
    4071              :   return false;
    4072              : }
    4073              : 
    4074              : /* Classify LEAF as one of the long-multiply summand shapes.  On success,
    4075              :    fill *INFO with the kind, extract, captured operands and shift.
    4076              :    Dispatches to per-extract helpers; the order matters because the
    4077              :    carry kinds bake an lshift into the pattern and would otherwise be
    4078              :    misread by the (X << N) branch.  */
    4079              : 
    4080      6947895 : long_mul_summand::long_mul_summand (tree leaf)
    4081      6947895 :  : long_mul_summand()
    4082              : {
    4083      6947895 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4084              : 
    4085      6947895 :   if (classify_carry (leaf))
    4086       118409 :     return;
    4087              : 
    4088      6945172 :   if (gimple_mul_hi (leaf, res_ops, NULL))
    4089              :     {
    4090        35385 :       classify_hi_extract (res_ops[0], tree_to_uhwi (res_ops[1]));
    4091        35385 :       return;
    4092              :     }
    4093              : 
    4094      6909787 :   if (gimple_mul_lo (leaf, res_ops, NULL))
    4095              :     {
    4096        21664 :       classify_lo_extract (res_ops[0]);
    4097        21664 :       return;
    4098              :     }
    4099              : 
    4100      6888123 :   tree inner;
    4101      6888123 :   unsigned HOST_WIDE_INT shift;
    4102      6888123 :   if (long_mul_is_lshift_def (leaf, &inner, &shift))
    4103              :     {
    4104        58637 :       classify_shl_extract (inner, shift);
    4105        58637 :       return;
    4106              :     }
    4107              : 
    4108      6829486 :   classify_bare (leaf);
    4109              : }
    4110              : 
    4111              : /* qsort comparator: sort summands by (kind, extract) to put a multiset
    4112              :    into canonical order for table lookup.  Unstable sort within a tie is
    4113              :    harmless: no row in long_mul_table pairs distinct subterms under the
    4114              :    same (kind, extract), and long_mul_check_consistency cross-validates
    4115              :    that matching summands share one canonical (op0, op1).  */
    4116              : 
    4117              : static int
    4118       139514 : long_mul_summand_compare (const void *a, const void *b)
    4119              : {
    4120       139514 :   const long_mul_summand *sa = (const long_mul_summand *) a;
    4121       139514 :   const long_mul_summand *sb = (const long_mul_summand *) b;
    4122       139514 :   gcc_checking_assert (sa->kind != LMK_INVALID);
    4123       139514 :   gcc_checking_assert (sb->kind != LMK_INVALID);
    4124       139514 :   if (sa->kind != sb->kind)
    4125       138651 :     return (int) sa->kind - (int) sb->kind;
    4126          863 :   return (int) sa->extract - (int) sb->extract;
    4127              : }
    4128              : 
    4129              : /* One row of the long-multiply variant table.  COUNT is how many entries
    4130              :    of SIG carry the row's signature (2 to LONG_MUL_MAX_SUMMANDS); a row
    4131              :    with fewer summands leaves the remaining SIG entries zero-initialized.
    4132              :    Those zeros are not a terminator -- {LMK_MUL_HIHI, LMX_NONE} is itself a
    4133              :    valid signature -- so long_mul_signature_matches is bounded by COUNT,
    4134              :    never by a sentinel entry.  */
    4135              : 
    4136              : struct long_mul_row {
    4137              :   enum long_mul_row_part { HIGH_PART, LOW_PART } part;
    4138              :   tree_code outer;
    4139              :   unsigned char count;
    4140              :   struct {
    4141              :     long_mul_kind kind;
    4142              :     long_mul_extract extract;
    4143              :   } sig[LONG_MUL_MAX_SUMMANDS];
    4144              :   bool (*extra_check) (const vec<long_mul_summand> &, gimple *);
    4145              : };
    4146              : 
    4147              : /* True if (A, B) is the same pair as (OP0, OP1) in either order.  */
    4148              : 
    4149              : static inline bool
    4150        23145 : long_mul_same_ops (tree a, tree b, tree op0, tree op1)
    4151              : {
    4152        32480 :   return (a == op0 && b == op1) || (a == op1 && b == op0);
    4153              : }
    4154              : 
    4155              : /* True if H is a cross-half product of (OP0, OP1) -- gimple_mul_hilo
    4156              :    recognizes it and its captured operands match the pair.  */
    4157              : 
    4158              : static bool
    4159         4374 : long_mul_is_cross_half (tree h, tree op0, tree op1)
    4160              : {
    4161         4374 :   tree scratch[LONG_MUL_MAX_CAPTURES];
    4162         4374 :   return gimple_mul_hilo (h, scratch, NULL)
    4163         4374 :          && long_mul_same_ops (scratch[0], scratch[1], op0, op1);
    4164              : }
    4165              : 
    4166              : /* Orientation of the mul_hilo capture H relative to (OP0, OP1):
    4167              :    returns 0 for high(OP0)*low(OP1), 1 for high(OP1)*low(OP0), or -1
    4168              :    if H does not decompose that way.  A cross-sum of two mul_hilos must
    4169              :    see one of each orientation -- otherwise a doubled factor would fold
    4170              :    to the wrong value.  (In a self-multiply the two orientations
    4171              :    coincide; see the OP0 == OP1 bypass in long_mul_check_consistency.)  */
    4172              : 
    4173              : static int
    4174         4146 : long_mul_hilo_orientation (tree h, tree op0, tree op1)
    4175              : {
    4176         4146 :   tree scratch[LONG_MUL_MAX_CAPTURES];
    4177         4146 :   if (!gimple_mul_hilo (h, scratch, NULL))
    4178              :     return -1;
    4179         4146 :   if (scratch[0] == op0 && scratch[1] == op1)
    4180              :     return 0;
    4181         2073 :   if (scratch[0] == op1 && scratch[1] == op0)
    4182         2073 :     return 1;
    4183              :   return -1;
    4184              : }
    4185              : 
    4186              : /* Find the first summand that carries operand captures, and return its
    4187              :    (op0, op1) pair in *OP0_OUT / *OP1_OUT.  Returns false if no summand
    4188              :    provides them.  */
    4189              : 
    4190              : static bool
    4191         8103 : long_mul_canonical_ops (const vec<long_mul_summand> &summands,
    4192              :                         tree *op0_out, tree *op1_out)
    4193              : {
    4194        24309 :   for (const long_mul_summand &s : summands)
    4195         8103 :     if (s.op0)
    4196              :       {
    4197         8103 :         *op0_out = s.op0;
    4198         8103 :         *op1_out = s.op1;
    4199         8103 :         return true;
    4200              :       }
    4201              :   return false;
    4202              : }
    4203              : 
    4204              : /* Return the first summand in SUMMANDS whose kind matches KIND, or NULL.  */
    4205              : 
    4206              : static const long_mul_summand *
    4207           12 : long_mul_find_summand (const vec<long_mul_summand> &summands,
    4208              :                        long_mul_kind kind)
    4209              : {
    4210           12 :   gcc_checking_assert (kind != LMK_INVALID);
    4211           60 :   for (const long_mul_summand &s : summands)
    4212           36 :     if (s.kind == kind)
    4213              :       return &s;
    4214              :   return NULL;
    4215              : }
    4216              : 
    4217              : /* Run the cross-summand validation invariants and return the canonical
    4218              :    (op0, op1).  Returns false unless all summands that carry operands use
    4219              :    the same (op0, op1) pair (in either order), every LMX_HI/LMX_SHL_N shift
    4220              :    equals halfwidth, every captured hilo is a true cross-half product of
    4221              :    (op0, op1), and every cross-half pair (both those inside a single
    4222              :    mul_cross_sum-bearing summand and those spread across separate
    4223              :    LMK_MUL_HILO summands) contains one of each orientation.  */
    4224              : 
    4225              : static bool
    4226         8091 : long_mul_check_consistency (const vec<long_mul_summand> &summands,
    4227              :                    tree *op0_out, tree *op1_out)
    4228              : {
    4229         8091 :   tree op0, op1;
    4230         8091 :   if (!long_mul_canonical_ops (summands, &op0, &op1))
    4231              :     return false;
    4232              : 
    4233         8091 :   tree op_type = TREE_TYPE (op0);
    4234         8091 :   if (!INTEGRAL_TYPE_P (op_type)
    4235         8091 :       || TYPE_PRECISION (op_type) % 2 != 0)
    4236              :     return false;
    4237         8091 :   unsigned int halfwidth = TYPE_PRECISION (op_type) / 2;
    4238              : 
    4239              :   /* Self-multiply (x*x) collapses the two cross-halves onto one value,
    4240              :      so the complementarity constraint is a trivial no-op there.  */
    4241         8091 :   bool need_orient = op0 != op1;
    4242         8091 :   int mul_hilo_orient[2] = { 0, 0 };
    4243              : 
    4244        37049 :   for (const long_mul_summand &s : summands)
    4245              :     {
    4246        18880 :       if (s.op0 && !long_mul_same_ops (s.op0, s.op1, op0, op1))
    4247              :         return false;
    4248        12802 :       if ((s.extract == LMX_HI || s.extract == LMX_SHL_N)
    4249         4626 :           && s.shift != halfwidth)
    4250              :         return false;
    4251        51154 :       for (tree h : s.hilo)
    4252        38378 :         if (h && !long_mul_is_cross_half (h, op0, op1))
    4253              :           return false;
    4254              : 
    4255        12776 :       if (!need_orient)
    4256            0 :         continue;
    4257              : 
    4258              :       /* The two cross-sum operands are the last two non-null hilos:
    4259              :          (hilo[1], hilo[2]) for the CARRY_*_SUM kinds, (hilo[0], hilo[1]) for
    4260              :          the CROSS_SUM / SUM / ACCUM / LADDER_SUM kinds, none for the
    4261              :          rest.  */
    4262        12776 :       tree a = NULL_TREE;
    4263        12776 :       tree b = NULL_TREE;
    4264        12776 :       if (s.hilo[2])
    4265              :         {
    4266           73 :           a = s.hilo[1];
    4267           73 :           b = s.hilo[2];
    4268              :         }
    4269        12703 :       else if (s.hilo[1])
    4270              :         {
    4271         2000 :           a = s.hilo[0];
    4272         2000 :           b = s.hilo[1];
    4273              :         }
    4274        12776 :       if (a && b
    4275        14849 :           && (long_mul_hilo_orientation (a, op0, op1)
    4276         2073 :               == long_mul_hilo_orientation (b, op0, op1)))
    4277              :         return false;
    4278              : 
    4279              :       /* Two LMK_MUL_HILO summands (the two-hilos ladder form) stand for
    4280              :          the two cross-halves separately; count orientations and require
    4281              :          the pair to be complementary.  s.op0/op1 is already validated to
    4282              :          match (op0, op1) in some order above.  */
    4283        12776 :       if (s.kind == LMK_MUL_HILO && s.op0)
    4284         2519 :         mul_hilo_orient[s.op0 == op1]++;
    4285              :     }
    4286              : 
    4287         1987 :   if (mul_hilo_orient[0] + mul_hilo_orient[1] >= 2
    4288           10 :       && (mul_hilo_orient[0] == 0 || mul_hilo_orient[1] == 0))
    4289              :     return false;
    4290              : 
    4291         1987 :   *op0_out = op0;
    4292         1987 :   *op1_out = op1;
    4293         1987 :   return true;
    4294              : }
    4295              : 
    4296              : /* Compare the (already-sorted) SUMMANDS multiset against ROW.sig.  */
    4297              : 
    4298              : static bool
    4299       124433 : long_mul_signature_matches (const vec<long_mul_summand> &summands,
    4300              :                    const long_mul_row &row)
    4301              : {
    4302       248866 :   if (row.count != summands.length ())
    4303              :     return false;
    4304        75408 :   for (unsigned i = 0; i < row.count; i++)
    4305        67317 :     if (summands[i].kind != row.sig[i].kind
    4306        67317 :         || summands[i].extract != row.sig[i].extract)
    4307              :       return false;
    4308              :   return true;
    4309              : }
    4310              : 
    4311              : /* Extra check for the two-carries high-part row: the LMK_CARRY_LOW summand's
    4312              :    two operands (carry_a, carry_b) must be a (cross_shifted, mul_lolo) pair
    4313              :    consistent with the multiset's canonical (op0, op1).  */
    4314              : 
    4315              : static bool
    4316           12 : long_mul_check_two_carries (const vec<long_mul_summand> &summands,
    4317              :                             gimple *)
    4318              : {
    4319           12 :   tree op0, op1;
    4320           12 :   if (!long_mul_canonical_ops (summands, &op0, &op1))
    4321              :     return false;
    4322           12 :   unsigned int halfwidth = TYPE_PRECISION (TREE_TYPE (op0)) / 2;
    4323              : 
    4324           12 :   const long_mul_summand *cl = long_mul_find_summand (summands, LMK_CARRY_LOW);
    4325           12 :   if (!cl)
    4326              :     return false;
    4327              : 
    4328              :   /* The two carry_low operands must be (cross_shifted, mul_lolo) in either
    4329              :      order.  cross_shifted = LSHIFT_EXPR (mul_cross_sum, halfwidth).  */
    4330           12 :   tree cs = cl->carry_a, lolo = cl->carry_b;
    4331           12 :   tree inner;
    4332           12 :   unsigned HOST_WIDE_INT shift;
    4333           12 :   if (!long_mul_is_lshift_def (cs, &inner, &shift))
    4334              :     {
    4335            2 :       std::swap (cs, lolo);
    4336            2 :       if (!long_mul_is_lshift_def (cs, &inner, &shift))
    4337              :         return false;
    4338              :     }
    4339           12 :   if (shift != halfwidth)
    4340              :     return false;
    4341              : 
    4342           12 :   tree scratch[LONG_MUL_MAX_CAPTURES];
    4343           12 :   if (!gimple_mul_cross_sum (inner, scratch, NULL))
    4344              :     return false;
    4345           36 :   for (int i = 0; i < 2; i++)
    4346           24 :     if (!long_mul_is_cross_half (scratch[i], op0, op1))
    4347              :       return false;
    4348           12 :   if (!gimple_mul_lolo (lolo, scratch, NULL)
    4349           12 :       || !long_mul_same_ops (scratch[0], scratch[1], op0, op1))
    4350              :     return false;
    4351              : 
    4352              :   return true;
    4353              : }
    4354              : 
    4355              : /* The lolo + cross_shifted shape is also the low half of a two-carry
    4356              :    long-multiply, where an unsigned overflow compare against one of
    4357              :    the PLUS operands is the low-carry term consumed by the matching
    4358              :    high-part fold.  Folding to mul_lo here destroys cross_shifted,
    4359              :    which both the compare and the high-part match still need; defer
    4360              :    so the high-part fold runs first.  After it does, the compare is
    4361              :    dead and the surviving lolo + cross_shifted is picked up by this
    4362              :    row in the next forwprop instance.  Returns false to defer.  */
    4363              : 
    4364              : static bool
    4365           51 : long_mul_check_low_plus_defer (const vec<long_mul_summand> &, gimple *stmt)
    4366              : {
    4367              :   /* The PHI entry passes its gphi as the candidate but commits only to
    4368              :      HIGH_PART rows, so a LOW_PART row never folds from there.  Guard the
    4369              :      gimple_assign accessors regardless, so this stays correct if a future
    4370              :      PLUS-shaped row reachable from the PHI path uses it.  */
    4371           51 :   if (!is_gimple_assign (stmt))
    4372              :     return false;
    4373              : 
    4374           51 :   tree lhs = gimple_assign_lhs (stmt);
    4375           51 :   tree rhs1 = gimple_assign_rhs1 (stmt);
    4376           51 :   tree rhs2 = gimple_assign_rhs2 (stmt);
    4377              : 
    4378           51 :   imm_use_iterator iter;
    4379           51 :   gimple *use_stmt;
    4380           79 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    4381              :     {
    4382           64 :       tree cmp_op1 = NULL_TREE, cmp_op2 = NULL_TREE;
    4383           64 :       enum tree_code use_code = ERROR_MARK;
    4384           64 :       if (is_gimple_assign (use_stmt))
    4385              :         {
    4386           42 :           use_code = gimple_assign_rhs_code (use_stmt);
    4387           42 :           cmp_op1 = gimple_assign_rhs1 (use_stmt);
    4388           42 :           cmp_op2 = gimple_assign_rhs2 (use_stmt);
    4389              :         }
    4390           22 :       else if (gcond *cond = dyn_cast<gcond *> (use_stmt))
    4391              :         {
    4392            9 :           use_code = gimple_cond_code (cond);
    4393            9 :           cmp_op1 = gimple_cond_lhs (cond);
    4394            9 :           cmp_op2 = gimple_cond_rhs (cond);
    4395              :         }
    4396           64 :       if (use_code == GT_EXPR || use_code == LT_EXPR
    4397           28 :           || use_code == GE_EXPR || use_code == LE_EXPR)
    4398              :         {
    4399           36 :           tree other = (cmp_op1 == lhs) ? cmp_op2
    4400           23 :                      : (cmp_op2 == lhs) ? cmp_op1 : NULL_TREE;
    4401           36 :           if (other && (other == rhs1 || other == rhs2))
    4402           36 :             return false;
    4403              :         }
    4404           36 :     }
    4405           15 :   return true;
    4406              : }
    4407              : 
    4408              : /* Long-multiply variant table.  Each row enumerates the multiset of
    4409              :    (kind, extract) summands that compose one long-multiply form.  Rows
    4410              :    are sorted by long_mul_summand_compare, matching the input summands'
    4411              :    sort order, so a plain element-wise compare suffices.  Rows describe
    4412              :    unsigned schoolbook expansions on an even-width 2N-bit type split at
    4413              :    half-width N; EXTRA_CHECK carries invariants the (kind, extract)
    4414              :    signature cannot express.
    4415              : 
    4416              :    The formula on each row uses xh, xl, yh, yl for the half-width pieces
    4417              :    of x and y, cross_sum for xh*yl + xl*yh, and hilo for either cross-half
    4418              :    product (consumers validate the operand shape).  */
    4419              : 
    4420              : static const long_mul_row long_mul_table[] = {
    4421              :   /* HIGH-PART folds.  */
    4422              :   /* xh*yh + (low_sum >> N) + ((hilo > low_sum) << N),
    4423              :      low_sum = cross_sum + (xl*yl >> N).  */
    4424              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
    4425              :     { { LMK_MUL_HIHI, LMX_NONE },
    4426              :       { LMK_LOW_SUM, LMX_HI },
    4427              :       { LMK_CARRY_LOW_SUM, LMX_NONE } },
    4428              :     NULL },
    4429              :   /* xh*yh + (low_accum >> N) + (cross_sum >> N) + ((hilo > cross_sum) << N),
    4430              :      low_accum = (xl*yl >> N) + (cross_sum & mask).  */
    4431              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
    4432              :     { { LMK_MUL_HIHI, LMX_NONE },
    4433              :       { LMK_CROSS_SUM, LMX_HI },
    4434              :       { LMK_LOW_ACCUM, LMX_HI },
    4435              :       { LMK_CARRY_CROSS_SUM, LMX_NONE } },
    4436              :     NULL },
    4437              :   /* xh*yh + (cross_sum >> N) + carry_low + ((hilo > cross_sum) << N),
    4438              :      carry_low = (xl*yl + (cross_sum << N)) < (cross_sum << N).  */
    4439              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
    4440              :     { { LMK_MUL_HIHI, LMX_NONE },
    4441              :       { LMK_CROSS_SUM, LMX_HI },
    4442              :       { LMK_CARRY_LOW, LMX_NONE },
    4443              :       { LMK_CARRY_CROSS_SUM, LMX_NONE } },
    4444              :     long_mul_check_two_carries },
    4445              :   /* xh*yh + (hilo >> N) + (ladder_sum1 >> N),
    4446              :      ladder_sum1 = (hilo & mask) + hilo' + (xl*yl >> N),
    4447              :      hilo, hilo' the two cross-half products.  */
    4448              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
    4449              :     { { LMK_MUL_HIHI, LMX_NONE },
    4450              :       { LMK_MUL_HILO, LMX_HI },
    4451              :       { LMK_LADDER_SUM1, LMX_HI } },
    4452              :     NULL },
    4453              :   /* xh*yh + (ladder_sum2 >> N) + (ladder_part_sum >> N),
    4454              :      ladder_part_sum = (xl*yl >> N) + hilo,
    4455              :      ladder_sum2 = (ladder_part_sum & mask) + hilo'.  */
    4456              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
    4457              :     { { LMK_MUL_HIHI, LMX_NONE },
    4458              :       { LMK_LADDER_SUM2, LMX_HI },
    4459              :       { LMK_LADDER_PART_SUM, LMX_HI } },
    4460              :     NULL },
    4461              :   /* xh*yh + (hilo >> N) + (hilo' >> N) + (ladder_sum3 >> N),
    4462              :      ladder_sum3 = (hilo & mask) + (hilo' & mask) + (xl*yl >> N).  */
    4463              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
    4464              :     { { LMK_MUL_HIHI, LMX_NONE },
    4465              :       { LMK_MUL_HILO, LMX_HI },
    4466              :       { LMK_MUL_HILO, LMX_HI },
    4467              :       { LMK_LADDER_SUM3, LMX_HI } },
    4468              :     NULL },
    4469              :   /* LOW-PART folds.  Recover the lower 2N bits from xl*yl plus a
    4470              :      shifted cross-half term.  */
    4471              :   /* xl*yl + (cross_sum << N).  */
    4472              :   { long_mul_row::LOW_PART, PLUS_EXPR, 2,
    4473              :     { { LMK_MUL_LOLO, LMX_NONE },
    4474              :       { LMK_CROSS_SUM, LMX_SHL_N } },
    4475              :     long_mul_check_low_plus_defer },
    4476              :   /* (xl*yl & mask) | (low_accum << N),
    4477              :      low_accum = (xl*yl >> N) + (cross_sum & mask).  */
    4478              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4479              :     { { LMK_MUL_LOLO, LMX_LO },
    4480              :       { LMK_LOW_ACCUM, LMX_SHL_N } },
    4481              :     NULL },
    4482              :   /* (xl*yl & mask) | (low_sum << N),
    4483              :      low_sum = cross_sum + (xl*yl >> N).  */
    4484              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4485              :     { { LMK_MUL_LOLO, LMX_LO },
    4486              :       { LMK_LOW_SUM, LMX_SHL_N } },
    4487              :     NULL },
    4488              :   /* (xl*yl & mask) | (ladder_sum1 << N),
    4489              :      ladder_sum1 as in the high ladder row above.  */
    4490              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4491              :     { { LMK_MUL_LOLO, LMX_LO },
    4492              :       { LMK_LADDER_SUM1, LMX_SHL_N } },
    4493              :     NULL },
    4494              :   /* (xl*yl & mask) | (ladder_sum2 << N),
    4495              :      ladder_sum2 as in the high ladder row above.  */
    4496              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4497              :     { { LMK_MUL_LOLO, LMX_LO },
    4498              :       { LMK_LADDER_SUM2, LMX_SHL_N } },
    4499              :     NULL },
    4500              :   /* (xl*yl & mask) | (ladder_sum3 << N),
    4501              :      ladder_sum3 as in the high ladder-long row above.  */
    4502              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4503              :     { { LMK_MUL_LOLO, LMX_LO },
    4504              :       { LMK_LADDER_SUM3, LMX_SHL_N } },
    4505              :     NULL },
    4506              : };
    4507              : 
    4508              : /* If a multi-used inner addition (sharing the chain's outer code) blocked
    4509              :    linearization of a long-mul candidate, emit a dump-file hint pointing
    4510              :    at it.  */
    4511              : 
    4512              : static void
    4513        22935 : long_mul_hint_shared_intermediate (gimple *shared_def)
    4514              : {
    4515        22935 :   if (!shared_def || !dump_file || !(dump_flags & TDF_DETAILS))
    4516              :     return;
    4517            0 :   fprintf (dump_file, "long-mul fold rejected: shared intermediate at ");
    4518            0 :   print_gimple_stmt (dump_file, shared_def, 0, TDF_SLIM);
    4519              : }
    4520              : 
    4521              : /* Search long_mul_table for a row whose multiset matches SUMMANDS for
    4522              :    outer kind OUTER on a result of type LHS_TYPE.  CANDIDATE_STMT is
    4523              :    passed to per-row extra_check predicates.  On a hit, returns the
    4524              :    matching row and writes the half-width operands via OUT_OP0/OUT_OP1.
    4525              :    No IR mutation.  */
    4526              : 
    4527              : static const long_mul_row *
    4528        18719 : long_mul_classify_match (const vec<long_mul_summand> &summands,
    4529              :                          tree lhs_type, tree_code outer,
    4530              :                          gimple *candidate_stmt,
    4531              :                          tree *out_op0, tree *out_op1)
    4532              : {
    4533              :   /* HIGH_PART rows emit a 2N-bit multiply that pass_optimize_widening_mul
    4534              :      consumes -- either via WIDEN_MULT_EXPR / MULT_HIGHPART conversion when
    4535              :      the target has a native 2N multiply, or via lower_long_mul_high_chain
    4536              :      when it does not.  LOW_PART rows emit a plain MULT_EXPR.  Emission
    4537              :      needs a 2N mode to exist in the mode table AND the widening_mul pass
    4538              :      to be active: without the pass, the emit could reach RTL expand as an
    4539              :      unexpandable 2N multiply (e.g. OImode).  BITINT_TYPE is
    4540              :      refused -- the long_mul_high_chain atom excludes it.  */
    4541        18719 :   scalar_int_mode mode, wide_mode;
    4542        18719 :   bool can_emit_high
    4543        18719 :     = optimize_widening_mul_active_p ()
    4544        18628 :       && TREE_CODE (lhs_type) != BITINT_TYPE
    4545        37256 :       && is_a <scalar_int_mode> (TYPE_MODE (lhs_type), &mode)
    4546        37347 :       && GET_MODE_2XWIDER_MODE (mode).exists (&wide_mode);
    4547              : 
    4548       225854 :   for (const long_mul_row &row : long_mul_table)
    4549              :     {
    4550       410081 :       if (row.outer != outer
    4551       124979 :           || (row.part == long_mul_row::HIGH_PART && !can_emit_high)
    4552       333519 :           || !long_mul_signature_matches (summands, row))
    4553       207135 :         continue;
    4554              : 
    4555         8091 :       tree op0, op1;
    4556         8091 :       if (!long_mul_check_consistency (summands, &op0, &op1))
    4557         6104 :         continue;
    4558              : 
    4559              :       /* Do not emit the wide chain when an operand is subject to
    4560              :          abnormal coalescing: the widening_mul-side consumers refuse
    4561              :          such operands (see convert_mult_to_widen), which would leave
    4562              :          the chain without a consumer.  */
    4563         1987 :       if (row.part == long_mul_row::HIGH_PART
    4564         1987 :           && ((TREE_CODE (op0) == SSA_NAME
    4565         1919 :                && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
    4566         1919 :               || (TREE_CODE (op1) == SSA_NAME
    4567         1919 :                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
    4568            0 :         continue;
    4569              : 
    4570         1987 :       if (row.extra_check && !row.extra_check (summands, candidate_stmt))
    4571           36 :         continue;
    4572              : 
    4573         1951 :       *out_op0 = op0;
    4574         1951 :       *out_op1 = op1;
    4575         1951 :       return &row;
    4576              :     }
    4577              :   return NULL;
    4578              : }
    4579              : 
    4580              : /* Walk STMT's outer chain (kind OUTER), classify each leaf as a
    4581              :    long-multiply summand, optionally add the already-classified EXTRA,
    4582              :    and look the multiset up in long_mul_table for a result of type
    4583              :    LHS_TYPE.  CANDIDATE is passed to per-row extra_check predicates.
    4584              : 
    4585              :    If EXTRAS_OUT is non-NULL, leaves matching no summand are set aside
    4586              :    there instead of failing the match, and the caller must re-apply
    4587              :    them on top of the folded multiply.  A leaf that does match is
    4588              :    always consumed: if that makes the signature miss every row the
    4589              :    match fails, rather than retrying with the leaf demoted to an extra
    4590              :    (subset search would be exponential).
    4591              : 
    4592              :    Returns the matched row and the half-width operands via
    4593              :    OUT_OP0/OUT_OP1, or NULL on a miss.  No IR mutation.  */
    4594              : 
    4595              : static const long_mul_row *
    4596      3391076 : long_mul_classify_chain (gimple *stmt, tree_code outer, tree lhs_type,
    4597              :                          gimple *candidate, const long_mul_summand *extra,
    4598              :                          vec<tree> *extras_out,
    4599              :                          tree *out_op0, tree *out_op1)
    4600              : {
    4601      3391076 :   auto_vec<tree, LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS> leaves;
    4602      3391076 :   gimple *shared_def = NULL;
    4603      3391076 :   if (!long_mul_linearize_chain (stmt, outer, leaves, &shared_def))
    4604              :     return NULL;
    4605              : 
    4606      3390279 :   auto_vec<long_mul_summand,
    4607      3390279 :            LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS + 1> summands;
    4608     17112565 :   for (tree leaf : leaves)
    4609              :     {
    4610      6947895 :       if (long_mul_summand s{leaf})
    4611              :         {
    4612        55477 :           gcc_checking_assert (s.kind != LMK_INVALID);
    4613        55477 :           summands.quick_push (s);
    4614              :         }
    4615     13784560 :       else if (extras_out && extras_out->length () < LONG_MUL_MAX_EXTRAS)
    4616      6886251 :         extras_out->safe_push (leaf);
    4617              :       else
    4618              :         {
    4619         6167 :           long_mul_hint_shared_intermediate (shared_def);
    4620         6167 :           return NULL;
    4621              :         }
    4622              :     }
    4623      3384112 :   if (extra)
    4624              :     {
    4625          354 :       gcc_checking_assert (extra->kind != LMK_INVALID);
    4626          354 :       summands.quick_push (*extra);
    4627              :     }
    4628      6774391 :   if (summands.length () < 2
    4629      3384112 :       || summands.length () > LONG_MUL_MAX_SUMMANDS)
    4630              :     return NULL;
    4631        18719 :   summands.qsort (long_mul_summand_compare);
    4632              : 
    4633        18719 :   const long_mul_row *row
    4634        18719 :     = long_mul_classify_match (summands, lhs_type, outer, candidate,
    4635              :                                out_op0, out_op1);
    4636        18719 :   if (!row)
    4637        16768 :     long_mul_hint_shared_intermediate (shared_def);
    4638              :   return row;
    4639      3390279 : }
    4640              : 
    4641              : /* Top-level entry for long-multiply folding.  Walks STMT's outer
    4642              :    addition or BIT_IOR chain, classifies the summands, and dispatches
    4643              :    to create_mul_high_seq / create_mul_low_seq if the multiset matches
    4644              :    a known long-multiply form.  Returns true on success.  */
    4645              : 
    4646              : static bool
    4647     10194410 : match_long_mul (gassign *stmt)
    4648              : {
    4649     10194410 :   tree_code outer = gimple_assign_rhs_code (stmt);
    4650     10194410 :   if (outer != PLUS_EXPR && outer != BIT_IOR_EXPR)
    4651              :     return false;
    4652              : 
    4653              :   /* Skip non-candidate adds (signed, pointer, odd-width) before walking the
    4654              :      chain.  No legitimate long-mul leaf has a type the atoms would reject.
    4655              :      This just avoids the linearize/classify work on every other PLUS/IOR.  */
    4656     10194410 :   tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
    4657     10194410 :   if (!INTEGRAL_TYPE_P (lhs_type)
    4658      8903936 :       || !TYPE_UNSIGNED (lhs_type)
    4659     14014824 :       || TYPE_PRECISION (lhs_type) % 2 != 0)
    4660              :     return false;
    4661              : 
    4662              :   /* Only start at the end of a chain: a consumer with the same code
    4663              :      linearizes through this statement anyway, so starting here is
    4664              :      redundant.  A consumer in another block does not count -- folding
    4665              :      at the later use could sink a loop-invariant multiply into a
    4666              :      loop.  */
    4667      3566928 :   use_operand_p use_p;
    4668      3566928 :   gimple *use_stmt;
    4669      3566928 :   if (single_imm_use (gimple_assign_lhs (stmt), &use_p, &use_stmt)
    4670      2529109 :       && is_gimple_assign (use_stmt)
    4671      1372056 :       && gimple_assign_rhs_code (use_stmt) == outer
    4672      3750228 :       && gimple_bb (use_stmt) == gimple_bb (stmt))
    4673              :     return false;
    4674              : 
    4675      3390446 :   auto_vec<tree, LONG_MUL_MAX_EXTRAS> extras;
    4676      3390446 :   tree op0, op1;
    4677      3390446 :   const long_mul_row *row
    4678      3390446 :     = long_mul_classify_chain (stmt, outer, lhs_type, stmt, NULL, &extras,
    4679              :                                &op0, &op1);
    4680      3390446 :   if (!row)
    4681              :     return false;
    4682              : 
    4683         1937 :   if (row->part == long_mul_row::HIGH_PART)
    4684              :     {
    4685         1905 :       create_mul_high_seq (op0, op1, stmt, extras, outer);
    4686         1905 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4687           32 :         fprintf (dump_file, "Long multiplication high part folded.\n");
    4688              :       return true;
    4689              :     }
    4690           32 :   create_mul_low_seq (op0, op1, stmt, extras, outer);
    4691           32 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4692            8 :     fprintf (dump_file, "Long multiplication low part folded.\n");
    4693              :   return true;
    4694      3390446 : }
    4695              : 
    4696              : /* PHI-driven entry for long-multiply folding.  When PHI's value
    4697              :    flattens to base + (carry << N), probe sum to classify the carry
    4698              :    kind, linearize base for the remaining high-part summands, and run
    4699              :    the long-multiply table.  On a hit, emit a 2N-bit multiply at the
    4700              :    top of the join block with PHI_RES as its LHS and remove the PHI.
    4701              :    Otherwise leave the IR untouched.  Only HIGH_PART rows are
    4702              :    reachable.  LOW_PART rows are BIT_IOR-shaped and never produce a
    4703              :    carry PHI.  */
    4704              : 
    4705              : static bool
    4706      8764984 : match_long_mul_phi (gphi *phi)
    4707              : {
    4708      8764984 :   tree phi_res = gimple_phi_result (phi);
    4709      8764984 :   tree lhs_type = TREE_TYPE (phi_res);
    4710      6195747 :   if (!INTEGRAL_TYPE_P (lhs_type) || !TYPE_UNSIGNED (lhs_type)
    4711     11512945 :       || TYPE_PRECISION (lhs_type) % 2 != 0)
    4712              :     return false;
    4713              : 
    4714      2022359 :   tree cca_ops[4];
    4715      2022359 :   if (!gimple_cond_carry_add (phi_res, cca_ops, NULL))
    4716              :     return false;
    4717        19097 :   tree cmp_lhs = cca_ops[0];
    4718        19097 :   tree sum = cca_ops[1];
    4719        19097 :   tree base = cca_ops[2];
    4720              : 
    4721              :   /* Classify sum and populate the carry summand directly.  Most
    4722              :      specific first, mirroring long_mul_classify_carry's order.  */
    4723        19097 :   long_mul_summand carry{};
    4724        19097 :   tree sum_ops[LONG_MUL_MAX_CAPTURES];
    4725        19097 :   unsigned HOST_WIDE_INT shift_amt
    4726        19097 :     = wi::exact_log2 (wi::to_wide (cca_ops[3]));
    4727        19097 :   unsigned HOST_WIDE_INT halfwidth = TYPE_PRECISION (lhs_type) / 2;
    4728        19097 :   carry.shift = shift_amt;
    4729              : 
    4730        19097 :   if (shift_amt == halfwidth
    4731        19097 :       && gimple_mul_low_sum (sum, sum_ops, NULL))
    4732              :     {
    4733              :       /* mul_carry_low_sum's flat form ties the outer lshift amount to
    4734              :          the inner mul_hi's INTEGER_CST@0 via match.pd capture re-use;
    4735              :          the PHI form has no such tie, so gate on shift_amt explicitly.  */
    4736           14 :       carry.kind = LMK_CARRY_LOW_SUM;
    4737           14 :       carry.op0 = sum_ops[0];
    4738           14 :       carry.op1 = sum_ops[1];
    4739           14 :       carry.hilo[0] = cmp_lhs;
    4740           14 :       carry.hilo[1] = sum_ops[2];
    4741           14 :       carry.hilo[2] = sum_ops[3];
    4742              :     }
    4743        19083 :   else if (shift_amt == halfwidth
    4744        19083 :            && gimple_mul_cross_sum (sum, sum_ops, NULL))
    4745              :     {
    4746              :       /* mul_cross_sum is just (plus:c @0 @1) with no half-width
    4747              :          constraint.  Gate here to mirror mul_carry_cross_sum;
    4748              :          a mismatch falls through to the LMK_CARRY_LOW branch.  */
    4749            1 :       carry.kind = LMK_CARRY_CROSS_SUM;
    4750            1 :       carry.hilo[0] = cmp_lhs;
    4751            1 :       carry.hilo[1] = sum_ops[0];
    4752            1 :       carry.hilo[2] = sum_ops[1];
    4753              :     }
    4754        19082 :   else if (shift_amt == 0 && TREE_CODE (sum) == SSA_NAME)
    4755              :     {
    4756        17944 :       gimple *def = SSA_NAME_DEF_STMT (sum);
    4757        17944 :       if (!is_gimple_assign (def)
    4758        17944 :           || gimple_assign_rhs_code (def) != PLUS_EXPR)
    4759              :         return false;
    4760         4734 :       tree p1 = gimple_assign_rhs1 (def);
    4761         4734 :       tree p2 = gimple_assign_rhs2 (def);
    4762         4734 :       if (p1 != cmp_lhs && p2 != cmp_lhs)
    4763              :         return false;
    4764         4009 :       carry.kind = LMK_CARRY_LOW;
    4765         4009 :       carry.carry_a = cmp_lhs;
    4766         4009 :       carry.carry_b = p1 == cmp_lhs ? p2 : p1;
    4767         4009 :     }
    4768              :   else
    4769              :     return false;
    4770              : 
    4771              :   /* Linearize base, the rest of the high-part chain.  */
    4772         4024 :   if (TREE_CODE (base) != SSA_NAME)
    4773              :     return false;
    4774         4024 :   gimple *base_def = SSA_NAME_DEF_STMT (base);
    4775         4024 :   if (!is_gimple_assign (base_def)
    4776         4024 :       || gimple_assign_rhs_code (base_def) != PLUS_EXPR)
    4777              :     return false;
    4778              : 
    4779          630 :   tree op0, op1;
    4780          630 :   const long_mul_row *row
    4781          630 :     = long_mul_classify_chain (base_def, PLUS_EXPR, lhs_type, phi, &carry,
    4782              :                                NULL, &op0, &op1);
    4783          630 :   if (!row || row->part != long_mul_row::HIGH_PART)
    4784              :     return false;
    4785              : 
    4786           14 :   gimple_seq seq = NULL;
    4787           14 :   build_mul_high_seq (op0, op1, phi_res, gimple_location (phi), &seq);
    4788           14 :   gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (phi));
    4789           14 :   gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
    4790           14 :   gimple_stmt_iterator psi = gsi_for_stmt (phi);
    4791           14 :   remove_phi_node (&psi, false);
    4792           14 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4793            4 :     fprintf (dump_file,
    4794              :              "Long multiplication high part folded (carry PHI).\n");
    4795              :   return true;
    4796              : }
    4797              : 
    4798              : /* Verify if we have the following structure:
    4799              : 
    4800              :    iftmp1 = PHI <pow2a, pow2b, pow2c, ...>
    4801              :    _ssa1 = _ssa2 MOD|DIV iftmp1;
    4802              :    _ssa3 = _ssa1 EQ|NE 0;
    4803              : 
    4804              :    And, if the right conditions are met, change the PHI args
    4805              :    and "_ssa1" stmt to a cheaper alternative.
    4806              : 
    4807              :    - for MOD, if either "_ssa2" is known to be positive or
    4808              :    "_ssa1" is used just in zero comparisons:
    4809              : 
    4810              :    iftmp1 = PHI <(pow2a - 1), (pow2b - 1), (pow2c - 1), ...>
    4811              :    _ssa1 = _ssa2 & iftmp1;
    4812              : 
    4813              :    - for DIV, if "_ssa2" is known to be positive:
    4814              : 
    4815              :    iftmp1 = PHI <log2 (pow2a), log2 (pow2b), log2 (pow2c), ...>
    4816              :    _ssa1 = _ssa2 >> iftmp1;  */
    4817              : static bool
    4818       133066 : simplify_phi_result_movdiv (gimple *stmt, tree_code code)
    4819              : {
    4820       133066 :   tree rhs1 = gimple_assign_rhs1 (stmt);
    4821       133066 :   tree_code new_code;
    4822              : 
    4823              :   /* Skip complex types (PR127163) */
    4824       133066 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    4825              :     return false;
    4826              : 
    4827        66850 :   switch (code)
    4828              :     {
    4829          211 :       case TRUNC_MOD_EXPR:
    4830          211 :       case FLOOR_MOD_EXPR:
    4831          211 :         if (!tree_expr_nonnegative_p (rhs1)
    4832          211 :             && !use_in_zero_equality (gimple_assign_lhs (stmt), true))
    4833              :           return false;
    4834              : 
    4835              :         new_code = BIT_AND_EXPR;
    4836              :         break;
    4837              : 
    4838          699 :       case TRUNC_DIV_EXPR:
    4839          699 :       case FLOOR_DIV_EXPR:
    4840          699 :       case EXACT_DIV_EXPR:
    4841          699 :         if (!tree_expr_nonnegative_p (rhs1))
    4842              :           return false;
    4843              : 
    4844              :         new_code = RSHIFT_EXPR;
    4845              :         break;
    4846              : 
    4847              :      default:
    4848              :         return false;
    4849              :     }
    4850              : 
    4851          531 :   gphi *phi = as_a<gphi *> (SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt)));
    4852              : 
    4853         1101 :   for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
    4854          563 :     if (!integer_pow2p (gimple_phi_arg_def (phi, i))
    4855          563 :         || tree_int_cst_sgn (gimple_phi_arg_def (phi, i)) < 0)
    4856              :       return false;
    4857              : 
    4858            7 :   tree type = TREE_TYPE (gimple_phi_result (phi));
    4859            7 :   tree new_phires = make_ssa_name (type);
    4860            7 :   gphi *new_phi = create_phi_node (new_phires, phi->bb);
    4861              : 
    4862           30 :   for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
    4863              :     {
    4864           16 :       tree phi_arg = gimple_phi_arg_def (phi, i);
    4865           16 :       edge e = gimple_phi_arg_edge (phi, i);
    4866           16 :       tree arg;
    4867              : 
    4868           16 :       if (new_code == RSHIFT_EXPR)
    4869            4 :         arg = wide_int_to_tree (type, wi::exact_log2 (wi::to_wide (phi_arg)));
    4870              :       else
    4871           12 :         arg = wide_int_to_tree (type, wi::to_wide (phi_arg) - 1);
    4872              : 
    4873           16 :       add_phi_arg (new_phi, arg, e,
    4874           16 :                    gimple_phi_arg_location (phi, e->dest_idx));
    4875              :     }
    4876              : 
    4877              :   /* Add a gimple_convert to integer_type_node for new_phires
    4878              :      since it might be a long long which we want to convert
    4879              :      into an integer or a bit_int that we want to convert into
    4880              :      an integer.  */
    4881            7 :   gimple_stmt_iterator gsi;
    4882            7 :   if (new_code == RSHIFT_EXPR)
    4883              :     {
    4884            2 :       gsi = gsi_for_stmt (stmt);
    4885            2 :       new_phires = gimple_convert (&gsi, true, GSI_SAME_STMT,
    4886              :                                    gimple_location (stmt),
    4887              :                                    integer_type_node, new_phires);
    4888              :     }
    4889              : 
    4890            7 :   gimple_assign_set_rhs2 (stmt, new_phires);
    4891            7 :   gimple_assign_set_rhs_code (stmt, new_code);
    4892            7 :   update_stmt (stmt);
    4893              : 
    4894            7 :   gsi = gsi_for_phi (phi);
    4895            7 :   remove_phi_node (&gsi, true);
    4896              : 
    4897            7 :   return true;
    4898              : }
    4899              : 
    4900              : /* Determine whether applying the 2 permutations (mask1 then mask2)
    4901              :    gives back one of the input.  */
    4902              : 
    4903              : static int
    4904           42 : is_combined_permutation_identity (tree mask1, tree mask2)
    4905              : {
    4906           42 :   tree mask;
    4907           42 :   unsigned HOST_WIDE_INT nelts, i, j;
    4908           42 :   bool maybe_identity1 = true;
    4909           42 :   bool maybe_identity2 = true;
    4910              : 
    4911           42 :   gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
    4912              :                        && TREE_CODE (mask2) == VECTOR_CST);
    4913              : 
    4914              :   /* For VLA masks, check for the following pattern:
    4915              :      v1 = VEC_PERM_EXPR (v0, ..., mask1)
    4916              :      v2 = VEC_PERM_EXPR (v1, ..., mask2)
    4917              :      -->
    4918              :      v2 = v0
    4919              :      if mask1 == mask2 == {nelts - 1, nelts - 2, ...}.  */
    4920              : 
    4921           42 :   if (operand_equal_p (mask1, mask2, 0)
    4922           42 :       && !VECTOR_CST_NELTS (mask1).is_constant ())
    4923              :     {
    4924              :       vec_perm_builder builder;
    4925              :       if (tree_to_vec_perm_builder (&builder, mask1))
    4926              :         {
    4927              :           poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1));
    4928              :           vec_perm_indices sel (builder, 1, nelts);
    4929              :           if (sel.series_p (0, 1, nelts - 1, -1))
    4930              :             return 1;
    4931              :         }
    4932              :     }
    4933              : 
    4934           42 :   mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
    4935           42 :   if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
    4936              :     return 0;
    4937              : 
    4938           42 :   if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
    4939              :     return 0;
    4940           72 :   for (i = 0; i < nelts; i++)
    4941              :     {
    4942           72 :       tree val = VECTOR_CST_ELT (mask, i);
    4943           72 :       gcc_assert (TREE_CODE (val) == INTEGER_CST);
    4944           72 :       j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
    4945           72 :       if (j == i)
    4946              :         maybe_identity2 = false;
    4947           55 :       else if (j == i + nelts)
    4948              :         maybe_identity1 = false;
    4949              :       else
    4950              :         return 0;
    4951              :     }
    4952            0 :   return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
    4953              : }
    4954              : 
    4955              : /* Combine a shuffle with its arguments.  Returns true if there were any
    4956              :    changes made.  */
    4957              : 
    4958              : static bool
    4959       192510 : simplify_permutation (gimple_stmt_iterator *gsi)
    4960              : {
    4961       192510 :   gimple *stmt = gsi_stmt (*gsi);
    4962       192510 :   gimple *def_stmt = NULL;
    4963       192510 :   tree op0, op1, op2, op3, arg0, arg1;
    4964       192510 :   enum tree_code code, code2 = ERROR_MARK;
    4965       192510 :   bool single_use_op0 = false;
    4966              : 
    4967       192510 :   gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
    4968              : 
    4969       192510 :   op0 = gimple_assign_rhs1 (stmt);
    4970       192510 :   op1 = gimple_assign_rhs2 (stmt);
    4971       192510 :   op2 = gimple_assign_rhs3 (stmt);
    4972              : 
    4973       192510 :   if (TREE_CODE (op2) != VECTOR_CST)
    4974              :     return false;
    4975              : 
    4976       189741 :   if (TREE_CODE (op0) == VECTOR_CST)
    4977              :     {
    4978              :       code = VECTOR_CST;
    4979              :       arg0 = op0;
    4980              :     }
    4981       187579 :   else if (TREE_CODE (op0) == SSA_NAME)
    4982              :     {
    4983       187579 :       def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
    4984       187579 :       if (!def_stmt)
    4985              :         return false;
    4986       179125 :       code = gimple_assign_rhs_code (def_stmt);
    4987       179125 :       if (code == VIEW_CONVERT_EXPR)
    4988              :         {
    4989         1628 :           tree rhs = gimple_assign_rhs1 (def_stmt);
    4990         1628 :           tree name = TREE_OPERAND (rhs, 0);
    4991         1628 :           if (TREE_CODE (name) != SSA_NAME)
    4992              :             return false;
    4993         1628 :           if (!has_single_use (name))
    4994          246 :             single_use_op0 = false;
    4995              :           /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
    4996              :              but still keep the code to indicate it comes from
    4997              :              VIEW_CONVERT_EXPR.  */
    4998         1628 :           def_stmt = SSA_NAME_DEF_STMT (name);
    4999         1628 :           if (!def_stmt || !is_gimple_assign (def_stmt))
    5000              :             return false;
    5001          841 :           if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
    5002              :             return false;
    5003              :         }
    5004       177763 :       if (!can_propagate_from (def_stmt))
    5005              :         return false;
    5006        24812 :       arg0 = gimple_assign_rhs1 (def_stmt);
    5007              :     }
    5008              :   else
    5009              :     return false;
    5010              : 
    5011              :   /* Two consecutive shuffles.  */
    5012        24812 :   if (code == VEC_PERM_EXPR)
    5013              :     {
    5014         6778 :       tree orig;
    5015         6778 :       int ident;
    5016              : 
    5017         6778 :       if (op0 != op1)
    5018              :         return false;
    5019           42 :       op3 = gimple_assign_rhs3 (def_stmt);
    5020           42 :       if (TREE_CODE (op3) != VECTOR_CST)
    5021              :         return false;
    5022           42 :       ident = is_combined_permutation_identity (op3, op2);
    5023           42 :       if (!ident)
    5024              :         return false;
    5025            0 :       orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
    5026            0 :                           : gimple_assign_rhs2 (def_stmt);
    5027            0 :       gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
    5028            0 :       gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
    5029            0 :       gimple_set_num_ops (stmt, 2);
    5030            0 :       update_stmt (stmt);
    5031            0 :       remove_prop_source_from_use (op0);
    5032            0 :       return true;
    5033              :     }
    5034        20196 :   else if (code == CONSTRUCTOR
    5035        20196 :            || code == VECTOR_CST
    5036              :            || code == VIEW_CONVERT_EXPR)
    5037              :     {
    5038         4962 :       if (op0 != op1)
    5039              :         {
    5040         4778 :           if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
    5041              :             return false;
    5042              : 
    5043         4115 :           if (TREE_CODE (op1) == VECTOR_CST)
    5044              :             arg1 = op1;
    5045         3482 :           else if (TREE_CODE (op1) == SSA_NAME)
    5046              :             {
    5047         3482 :               gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
    5048         3482 :               if (!def_stmt2)
    5049              :                 return false;
    5050         1675 :               code2 = gimple_assign_rhs_code (def_stmt2);
    5051         1675 :               if (code2 == VIEW_CONVERT_EXPR)
    5052              :                 {
    5053            0 :                   tree rhs = gimple_assign_rhs1 (def_stmt2);
    5054            0 :                   tree name = TREE_OPERAND (rhs, 0);
    5055            0 :                   if (TREE_CODE (name) != SSA_NAME)
    5056              :                     return false;
    5057            0 :                   if (!has_single_use (name))
    5058              :                     return false;
    5059            0 :                   def_stmt2 = SSA_NAME_DEF_STMT (name);
    5060            0 :                   if (!def_stmt2 || !is_gimple_assign (def_stmt2))
    5061              :                     return false;
    5062            0 :                   if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
    5063              :                     return false;
    5064              :                 }
    5065         1675 :               else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
    5066              :                 return false;
    5067         1485 :               if (!can_propagate_from (def_stmt2))
    5068              :                 return false;
    5069         1485 :               arg1 = gimple_assign_rhs1 (def_stmt2);
    5070              :             }
    5071              :           else
    5072              :             return false;
    5073              :         }
    5074              :       else
    5075              :         {
    5076              :           /* Already used twice in this statement.  */
    5077          184 :           if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
    5078              :             return false;
    5079              :           arg1 = arg0;
    5080              :         }
    5081              : 
    5082              :       /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
    5083              :          operands source, check whether it's valid to transform and prepare
    5084              :          the required new operands.  */
    5085         2234 :       if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
    5086              :         {
    5087              :           /* Figure out the target vector type to which operands should be
    5088              :              converted.  If both are CONSTRUCTOR, the types should be the
    5089              :              same, otherwise, use the one of CONSTRUCTOR.  */
    5090           24 :           tree tgt_type = NULL_TREE;
    5091           24 :           if (code == VIEW_CONVERT_EXPR)
    5092              :             {
    5093           24 :               gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
    5094           24 :               code = CONSTRUCTOR;
    5095           24 :               tgt_type = TREE_TYPE (arg0);
    5096              :             }
    5097           24 :           if (code2 == VIEW_CONVERT_EXPR)
    5098              :             {
    5099            0 :               tree arg1_type = TREE_TYPE (arg1);
    5100            0 :               if (tgt_type == NULL_TREE)
    5101              :                 tgt_type = arg1_type;
    5102            0 :               else if (tgt_type != arg1_type)
    5103           23 :                 return false;
    5104              :             }
    5105              : 
    5106           24 :           if (!VECTOR_TYPE_P (tgt_type))
    5107              :             return false;
    5108           24 :           tree op2_type = TREE_TYPE (op2);
    5109              : 
    5110              :           /* Figure out the shrunk factor.  */
    5111           24 :           poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
    5112           24 :           poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
    5113           24 :           if (maybe_gt (tgt_units, op2_units))
    5114              :             return false;
    5115           24 :           unsigned int factor;
    5116           47 :           if (!constant_multiple_p (op2_units, tgt_units, &factor))
    5117              :             return false;
    5118              : 
    5119              :           /* Build the new permutation control vector as target vector.  */
    5120           24 :           vec_perm_builder builder;
    5121           24 :           if (!tree_to_vec_perm_builder (&builder, op2))
    5122              :             return false;
    5123           24 :           vec_perm_indices indices (builder, 2, op2_units);
    5124           24 :           vec_perm_indices new_indices;
    5125           24 :           if (new_indices.new_shrunk_vector (indices, factor))
    5126              :             {
    5127            1 :               tree mask_type = tgt_type;
    5128            1 :               if (!VECTOR_INTEGER_TYPE_P (mask_type))
    5129              :                 {
    5130            0 :                   tree elem_type = TREE_TYPE (mask_type);
    5131            0 :                   unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
    5132            0 :                   tree int_type = build_nonstandard_integer_type (elem_size, 0);
    5133            0 :                   mask_type = build_vector_type (int_type, tgt_units);
    5134              :                 }
    5135            1 :               op2 = vec_perm_indices_to_tree (mask_type, new_indices);
    5136              :             }
    5137              :           else
    5138           23 :             return false;
    5139              : 
    5140              :           /* Convert the VECTOR_CST to the appropriate vector type.  */
    5141            1 :           if (tgt_type != TREE_TYPE (arg0))
    5142            0 :             arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
    5143            1 :           else if (tgt_type != TREE_TYPE (arg1))
    5144            0 :             arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
    5145           47 :         }
    5146              : 
    5147              :       /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before.  */
    5148         2211 :       gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
    5149              : 
    5150              :       /* Shuffle of a constructor.  */
    5151         2211 :       tree res_type
    5152         2211 :         = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)),
    5153         2211 :                              TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)));
    5154         2211 :       tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
    5155         2211 :       if (!opt
    5156          280 :           || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
    5157              :         return false;
    5158              :       /* Found VIEW_CONVERT_EXPR before, need one explicit conversion.  */
    5159          280 :       if (res_type != TREE_TYPE (op0))
    5160              :         {
    5161            1 :           tree name = make_ssa_name (TREE_TYPE (opt));
    5162            1 :           gimple *ass_stmt = gimple_build_assign (name, opt);
    5163            1 :           gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
    5164            1 :           opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
    5165              :         }
    5166          280 :       gimple_assign_set_rhs_from_tree (gsi, opt);
    5167          280 :       update_stmt (gsi_stmt (*gsi));
    5168          280 :       if (TREE_CODE (op0) == SSA_NAME)
    5169            1 :         remove_prop_source_from_use (op0);
    5170          280 :       if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
    5171            0 :         remove_prop_source_from_use (op1);
    5172              :       return true;
    5173              :     }
    5174              : 
    5175              :   return false;
    5176              : }
    5177              : 
    5178              : /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
    5179              :    conversions with code CONV_CODE or update it if still ERROR_MARK.
    5180              :    Return NULL_TREE if no such matching def was found.  */
    5181              : 
    5182              : static tree
    5183       439349 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
    5184              : {
    5185       439349 :   if (TREE_CODE (val) != SSA_NAME)
    5186              :     return NULL_TREE ;
    5187       409031 :   gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
    5188       409031 :   if (!def_stmt)
    5189              :     return NULL_TREE;
    5190       327924 :   enum tree_code code = gimple_assign_rhs_code (def_stmt);
    5191       327924 :   if (code == FLOAT_EXPR
    5192       327924 :       || code == FIX_TRUNC_EXPR
    5193              :       || CONVERT_EXPR_CODE_P (code))
    5194              :     {
    5195       189808 :       tree op1 = gimple_assign_rhs1 (def_stmt);
    5196       189808 :       if (conv_code == ERROR_MARK)
    5197        91182 :         conv_code = code;
    5198        98626 :       else if (conv_code != code)
    5199              :         return NULL_TREE;
    5200       189783 :       if (TREE_CODE (op1) != SSA_NAME)
    5201              :         return NULL_TREE;
    5202        81189 :       def_stmt = SSA_NAME_DEF_STMT (op1);
    5203        81189 :       if (! is_gimple_assign (def_stmt))
    5204              :         return NULL_TREE;
    5205        65432 :       code = gimple_assign_rhs_code (def_stmt);
    5206              :     }
    5207       203548 :   if (code != BIT_FIELD_REF)
    5208              :     return NULL_TREE;
    5209        24549 :   return gimple_assign_rhs1 (def_stmt);
    5210              : }
    5211              : 
    5212              : /* Recognize a VEC_PERM_EXPR.  Returns true if there were any changes.  */
    5213              : 
    5214              : static bool
    5215       169380 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
    5216              : {
    5217       169380 :   gimple *stmt = gsi_stmt (*gsi);
    5218       169380 :   tree op, orig[2], type;
    5219       169380 :   unsigned i;
    5220       169380 :   unsigned HOST_WIDE_INT nelts;
    5221       169380 :   unsigned HOST_WIDE_INT refnelts;
    5222       169380 :   enum tree_code conv_code;
    5223       169380 :   constructor_elt *elt;
    5224              : 
    5225       169380 :   op = gimple_assign_rhs1 (stmt);
    5226       169380 :   type = TREE_TYPE (op);
    5227       169380 :   gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
    5228              :                        && TREE_CODE (type) == VECTOR_TYPE);
    5229              : 
    5230       169380 :   if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
    5231              :     return false;
    5232              : 
    5233       169380 :   orig[0] = NULL;
    5234       169380 :   orig[1] = NULL;
    5235       169380 :   tree orig_elem_type[2] = {};
    5236       169380 :   conv_code = ERROR_MARK;
    5237       169380 :   bool maybe_ident = true;
    5238       169380 :   bool maybe_blend[2] = { true, true };
    5239       169380 :   tree one_constant = NULL_TREE;
    5240       169380 :   tree one_nonconstant = NULL_TREE;
    5241       169380 :   tree subelt;
    5242       169380 :   auto_vec<tree> constants;
    5243       169380 :   constants.safe_grow_cleared (nelts, true);
    5244       169380 :   auto_vec<std::pair<unsigned, unsigned>, 64> elts;
    5245       169380 :   unsigned int tsubelts = 0;
    5246       473507 :   FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
    5247              :     {
    5248       439349 :       tree ref, op1;
    5249       439349 :       unsigned int elem, src_elem_size;
    5250       439349 :       unsigned HOST_WIDE_INT nsubelts = 1;
    5251              : 
    5252       439349 :       if (i >= nelts)
    5253       169380 :         return false;
    5254              : 
    5255              :       /* Look for elements extracted and possibly converted from
    5256              :          another vector.  */
    5257       439349 :       op1 = get_bit_field_ref_def (elt->value, conv_code);
    5258       444842 :       if (op1
    5259        24549 :           && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
    5260         6188 :           && VECTOR_TYPE_P (TREE_TYPE (ref))
    5261         6173 :           && (tree_nop_conversion_p (TREE_TYPE (op1),
    5262         6173 :                                      TREE_TYPE (TREE_TYPE (ref)))
    5263          813 :               || (VECTOR_TYPE_P (TREE_TYPE (op1))
    5264          133 :                   && tree_nop_conversion_p (TREE_TYPE (TREE_TYPE (op1)),
    5265          133 :                                             TREE_TYPE (TREE_TYPE (ref)))
    5266          133 :                   && TYPE_VECTOR_SUBPARTS (TREE_TYPE (op1))
    5267          133 :                         .is_constant (&nsubelts)))
    5268         5493 :           && constant_multiple_p (bit_field_size (op1), nsubelts,
    5269              :                                   &src_elem_size)
    5270       444842 :           && constant_multiple_p (bit_field_offset (op1), src_elem_size, &elem)
    5271       444842 :           && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
    5272              :         {
    5273              :           unsigned int j;
    5274         5867 :           for (j = 0; j < 2; ++j)
    5275              :             {
    5276         5838 :               if (!orig[j])
    5277              :                 {
    5278         2560 :                   if (j == 0
    5279         2766 :                       || useless_type_conversion_p (TREE_TYPE (orig[0]),
    5280          206 :                                                     TREE_TYPE (ref)))
    5281              :                     break;
    5282              :                 }
    5283         3278 :               else if (ref == orig[j])
    5284              :                 break;
    5285              :             }
    5286              :           /* Found a suitable vector element.  */
    5287         5493 :           if (j < 2)
    5288              :             {
    5289         5464 :               orig[j] = ref;
    5290              :               /* Track what element type was actually extracted (which may
    5291              :                  differ in signedness from the vector's element type due to
    5292              :                  tree_nop_conversion_p).  */
    5293         5464 :               if (!orig_elem_type[j])
    5294         2556 :                 orig_elem_type[j] = TREE_TYPE (op1);
    5295         5464 :               if (elem != i || j != 0)
    5296         2301 :                 maybe_ident = false;
    5297         5464 :               if (elem != i)
    5298         2240 :                 maybe_blend[j] = false;
    5299        11187 :               for (unsigned int k = 0; k < nsubelts; ++k)
    5300         5723 :                 elts.safe_push (std::make_pair (j, elem + k));
    5301         5464 :               tsubelts += nsubelts;
    5302         5464 :               continue;
    5303         5464 :             }
    5304              :           /* Else fallthru.  */
    5305              :         }
    5306              :       /* Handle elements not extracted from a vector.
    5307              :           1. constants by permuting with constant vector
    5308              :           2. a unique non-constant element by permuting with a splat vector  */
    5309       433885 :       if (orig[1]
    5310       265701 :           && orig[1] != error_mark_node)
    5311              :         return false;
    5312       433856 :       orig[1] = error_mark_node;
    5313       433856 :       if (VECTOR_TYPE_P (TREE_TYPE (elt->value))
    5314       433856 :           && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (elt->value))
    5315         5771 :                         .is_constant (&nsubelts))
    5316              :         return false;
    5317       433856 :       if (CONSTANT_CLASS_P (elt->value))
    5318              :         {
    5319        30314 :           if (one_nonconstant)
    5320              :             return false;
    5321        21282 :           if (!one_constant)
    5322         9632 :             one_constant = TREE_CODE (elt->value) == VECTOR_CST
    5323         9632 :                            ? VECTOR_CST_ELT (elt->value, 0)
    5324              :                            : elt->value;
    5325        21282 :           if (TREE_CODE (elt->value) == VECTOR_CST)
    5326              :             {
    5327          777 :               for (unsigned int k = 0; k < nsubelts; k++)
    5328          587 :                 constants[tsubelts + k] = VECTOR_CST_ELT (elt->value, k);
    5329              :             }
    5330              :           else
    5331        21092 :             constants[tsubelts] = elt->value;
    5332              :         }
    5333              :       else
    5334              :         {
    5335       403542 :           if (one_constant)
    5336              :             return false;
    5337       394461 :           subelt = VECTOR_TYPE_P (TREE_TYPE (elt->value))
    5338       394461 :                    ? ssa_uniform_vector_p (elt->value)
    5339              :                    : elt->value;
    5340       394461 :           if (!subelt)
    5341              :             return false;
    5342       389132 :           if (!one_nonconstant)
    5343              :             one_nonconstant = subelt;
    5344       235909 :           else if (!operand_equal_p (one_nonconstant, subelt, 0))
    5345              :             return false;
    5346              :         }
    5347       597725 :       for (unsigned int k = 0; k < nsubelts; ++k)
    5348       299062 :         elts.safe_push (std::make_pair (1, tsubelts + k));
    5349       298663 :       tsubelts += nsubelts;
    5350       298663 :       maybe_ident = false;
    5351              :     }
    5352              : 
    5353        68316 :   if (elts.length () < nelts)
    5354              :     return false;
    5355              : 
    5356        32986 :   if (! orig[0]
    5357        32986 :       || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
    5358              :     return false;
    5359         1731 :   refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
    5360              :   /* We currently do not handle larger destination vectors.  */
    5361         1731 :   if (refnelts < nelts)
    5362              :     return false;
    5363              : 
    5364              :   /* Determine the element type for the conversion source.
    5365              :      As orig_elem_type keeps track of the original type, check
    5366              :      if we need to perform a sign swap after permuting.
    5367              :      We need to be able to construct a vector type from the element
    5368              :      type which is not possible for e.g. BitInt or pointers
    5369              :      so pun with an integer type if needed.   */
    5370         1494 :   tree perm_eltype = TREE_TYPE (TREE_TYPE (orig[0]));
    5371         1494 :   bool sign_change_p = false;
    5372         1494 :   if (conv_code != ERROR_MARK
    5373          373 :       && orig_elem_type[0]
    5374         1867 :       && TYPE_SIGN (orig_elem_type[0]) != TYPE_SIGN (perm_eltype))
    5375              :     {
    5376           35 :       perm_eltype = signed_or_unsigned_type_for
    5377           35 :         (TYPE_UNSIGNED (orig_elem_type[0]), perm_eltype);
    5378           35 :       sign_change_p = true;
    5379              :     }
    5380         1494 :   tree conv_src_type = build_vector_type (perm_eltype, nelts);
    5381              : 
    5382         1494 :   if (maybe_ident)
    5383              :     {
    5384              :       /* When there is no conversion, use the target type directly.  */
    5385          529 :       if (conv_code == ERROR_MARK && nelts != refnelts)
    5386          529 :         conv_src_type = type;
    5387          529 :       if (conv_code != ERROR_MARK
    5388          529 :           && !supportable_convert_operation (conv_code, type, conv_src_type))
    5389              :         {
    5390              :           /* Only few targets implement direct conversion patterns so try
    5391              :              some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR.  */
    5392          115 :           optab optab;
    5393          115 :           insn_code icode;
    5394          115 :           tree halfvectype, dblvectype;
    5395          115 :           enum tree_code unpack_op;
    5396              : 
    5397          115 :           if (!BYTES_BIG_ENDIAN)
    5398          207 :             unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
    5399          115 :                          ? VEC_UNPACK_FLOAT_LO_EXPR
    5400              :                          : VEC_UNPACK_LO_EXPR);
    5401              :           else
    5402              :             unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
    5403              :                          ? VEC_UNPACK_FLOAT_HI_EXPR
    5404              :                          : VEC_UNPACK_HI_EXPR);
    5405              : 
    5406              :           /* Conversions between DFP and FP have no special tree code
    5407              :              but we cannot handle those since all relevant vector conversion
    5408              :              optabs only have a single mode.  */
    5409           15 :           if (CONVERT_EXPR_CODE_P (conv_code)
    5410          100 :               && FLOAT_TYPE_P (TREE_TYPE (type))
    5411          131 :               && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
    5412            8 :                   != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
    5413              :             return false;
    5414              : 
    5415           15 :           if (CONVERT_EXPR_CODE_P (conv_code)
    5416           99 :               && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
    5417           99 :                   == TYPE_PRECISION (TREE_TYPE (type)))
    5418            6 :               && orig_elem_type[0]
    5419            6 :               && useless_type_conversion_p (orig_elem_type[0],
    5420            6 :                                             TREE_TYPE (TREE_TYPE (orig[0])))
    5421            6 :               && mode_for_vector (as_a <scalar_mode>
    5422            6 :                                   (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
    5423           12 :                                   nelts * 2).exists ()
    5424            6 :               && (dblvectype
    5425            6 :                   = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
    5426            6 :                                        nelts * 2))
    5427              :               /* Only use it for vector modes or for vector booleans
    5428              :                  represented as scalar bitmasks.  See PR95528.  */
    5429            6 :               && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
    5430            0 :                   || VECTOR_BOOLEAN_TYPE_P (dblvectype))
    5431            6 :               && (optab = optab_for_tree_code (unpack_op,
    5432              :                                                dblvectype,
    5433              :                                                optab_default))
    5434            6 :               && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
    5435              :                   != CODE_FOR_nothing)
    5436          114 :               && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
    5437              :             {
    5438            0 :               gimple_seq stmts = NULL;
    5439            0 :               tree dbl;
    5440            0 :               if (refnelts == nelts)
    5441              :                 {
    5442              :                   /* ???  Paradoxical subregs don't exist, so insert into
    5443              :                      the lower half of a wider zero vector.  */
    5444            0 :                   dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
    5445              :                                       build_zero_cst (dblvectype), orig[0],
    5446            0 :                                       bitsize_zero_node);
    5447              :                 }
    5448            0 :               else if (refnelts == 2 * nelts)
    5449              :                 dbl = orig[0];
    5450              :               else
    5451            0 :                 dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
    5452            0 :                                     orig[0], TYPE_SIZE (dblvectype),
    5453            0 :                                     bitsize_zero_node);
    5454            0 :               gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    5455            0 :               gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
    5456              :             }
    5457           15 :           else if (CONVERT_EXPR_CODE_P (conv_code)
    5458           99 :                    && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
    5459           99 :                        == 2 * TYPE_PRECISION (TREE_TYPE (type)))
    5460            5 :                    && orig_elem_type[0]
    5461            5 :                    && useless_type_conversion_p (orig_elem_type[0],
    5462            5 :                                                  TREE_TYPE (TREE_TYPE (orig[0])))
    5463            1 :                    && mode_for_vector (as_a <scalar_mode>
    5464            1 :                                          (TYPE_MODE
    5465              :                                            (TREE_TYPE (TREE_TYPE (orig[0])))),
    5466            2 :                                        nelts / 2).exists ()
    5467            1 :                    && (halfvectype
    5468            1 :                          = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
    5469            1 :                                               nelts / 2))
    5470              :                    /* Only use it for vector modes or for vector booleans
    5471              :                       represented as scalar bitmasks.  See PR95528.  */
    5472            1 :                    && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
    5473            0 :                        || VECTOR_BOOLEAN_TYPE_P (halfvectype))
    5474            1 :                    && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
    5475              :                                                     halfvectype,
    5476              :                                                     optab_default))
    5477            1 :                    && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
    5478              :                        != CODE_FOR_nothing)
    5479          115 :                    && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
    5480              :             {
    5481            0 :               gimple_seq stmts = NULL;
    5482            0 :               tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
    5483            0 :                                        orig[0], TYPE_SIZE (halfvectype),
    5484            0 :                                        bitsize_zero_node);
    5485            0 :               tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
    5486            0 :                                        orig[0], TYPE_SIZE (halfvectype),
    5487            0 :                                        TYPE_SIZE (halfvectype));
    5488            0 :               gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    5489            0 :               gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
    5490              :                                               low, hig);
    5491              :             }
    5492              :           else
    5493          114 :             return false;
    5494            0 :           update_stmt (gsi_stmt (*gsi));
    5495            0 :           return true;
    5496              :         }
    5497          414 :       if (nelts != refnelts)
    5498              :         {
    5499           14 :           gassign *lowpart
    5500           14 :             = gimple_build_assign (make_ssa_name (conv_src_type),
    5501              :                                    build3 (BIT_FIELD_REF, conv_src_type,
    5502           14 :                                            orig[0], TYPE_SIZE (conv_src_type),
    5503              :                                            bitsize_zero_node));
    5504           14 :           gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
    5505           14 :           orig[0] = gimple_assign_lhs (lowpart);
    5506              :         }
    5507          400 :       else if (sign_change_p)
    5508              :         {
    5509            0 :           gassign *conv
    5510            0 :             = gimple_build_assign (make_ssa_name (conv_src_type),
    5511              :                                    build1 (VIEW_CONVERT_EXPR, conv_src_type,
    5512              :                                            orig[0]));
    5513            0 :           gsi_insert_before (gsi, conv, GSI_SAME_STMT);
    5514            0 :           orig[0] = gimple_assign_lhs (conv);
    5515              :         }
    5516          414 :       if (conv_code == ERROR_MARK)
    5517              :         {
    5518          397 :           tree src_type = TREE_TYPE (orig[0]);
    5519          397 :           if (!useless_type_conversion_p (type, src_type))
    5520              :             {
    5521            0 :               gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
    5522              :                                     TYPE_VECTOR_SUBPARTS (src_type))
    5523              :                           && tree_nop_conversion_p (TREE_TYPE (type),
    5524              :                                                     TREE_TYPE (src_type)));
    5525            0 :               tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
    5526            0 :               orig[0] = make_ssa_name (type);
    5527            0 :               gassign *assign = gimple_build_assign (orig[0], rhs);
    5528            0 :               gsi_insert_before (gsi, assign, GSI_SAME_STMT);
    5529              :             }
    5530          397 :           gimple_assign_set_rhs_from_tree (gsi, orig[0]);
    5531              :         }
    5532              :       else
    5533           17 :         gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
    5534              :                                         NULL_TREE, NULL_TREE);
    5535              :     }
    5536              :   else
    5537              :     {
    5538              :       /* If we combine a vector with a non-vector avoid cases where
    5539              :          we'll obviously end up with more GIMPLE stmts which is when
    5540              :          we'll later not fold this to a single insert into the vector
    5541              :          and we had a single extract originally.  See PR92819.  */
    5542          965 :       if (nelts == 2
    5543          511 :           && refnelts > 2
    5544          122 :           && orig[1] == error_mark_node
    5545           31 :           && !maybe_blend[0])
    5546          158 :         return false;
    5547          940 :       tree mask_type, perm_type;
    5548          940 :       perm_type = TREE_TYPE (orig[0]);
    5549          940 :       if (conv_code != ERROR_MARK
    5550          940 :           && !supportable_convert_operation (conv_code, type, conv_src_type))
    5551              :         return false;
    5552              : 
    5553              :       /* Now that we know the number of elements of the source build the
    5554              :          permute vector.
    5555              :          ???  When the second vector has constant values we can shuffle
    5556              :          it and its source indexes to make the permutation supported.
    5557              :          For now it mimics a blend.  */
    5558          930 :       vec_perm_builder sel (refnelts, refnelts, 1);
    5559          930 :       bool all_same_p = true;
    5560         9472 :       for (i = 0; i < elts.length (); ++i)
    5561              :         {
    5562         3806 :           sel.quick_push (elts[i].second + elts[i].first * refnelts);
    5563         3806 :           all_same_p &= known_eq (sel[i], sel[0]);
    5564              :         }
    5565              :       /* And fill the tail with "something".  It's really don't care,
    5566              :          and ideally we'd allow VEC_PERM to have a smaller destination
    5567              :          vector.  As a heuristic:
    5568              : 
    5569              :          (a) if what we have so far duplicates a single element, make the
    5570              :              tail do the same
    5571              : 
    5572              :          (b) otherwise preserve a uniform orig[0].  This facilitates
    5573              :              later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR.  */
    5574         1542 :       for (; i < refnelts; ++i)
    5575         1224 :         sel.quick_push (all_same_p
    5576         1836 :                         ? sel[0]
    5577          156 :                         : (elts[0].second == 0 && elts[0].first == 0
    5578          908 :                            ? 0 : refnelts) + i);
    5579         1128 :       vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
    5580          930 :       machine_mode vmode = TYPE_MODE (perm_type);
    5581          930 :       if ((cfun->curr_properties & PROP_gimple_lvec)
    5582          930 :           && !can_vec_perm_const_p (vmode, vmode, indices))
    5583              :         return false;
    5584          807 :       mask_type = build_vector_type (ssizetype, refnelts);
    5585          807 :       tree op2 = vec_perm_indices_to_tree (mask_type, indices);
    5586          807 :       bool converted_orig1 = false;
    5587          807 :       gimple_seq stmts = NULL;
    5588          807 :       if (!orig[1])
    5589          173 :         orig[1] = orig[0];
    5590          634 :       else if (orig[1] == error_mark_node
    5591          510 :                && one_nonconstant)
    5592              :         {
    5593              :           /* ???  We can see if we can safely convert to the original
    5594              :              element type.  */
    5595          195 :           converted_orig1 = conv_code != ERROR_MARK;
    5596          195 :           tree target_type = converted_orig1 ? type : perm_type;
    5597          195 :           tree nonconstant_for_splat = one_nonconstant;
    5598              :           /* If there's a nop conversion between the target element type and
    5599              :              the nonconstant's type, convert it.  */
    5600          195 :           if (!useless_type_conversion_p (TREE_TYPE (target_type),
    5601          195 :                                           TREE_TYPE (one_nonconstant)))
    5602            0 :             nonconstant_for_splat
    5603            0 :               = gimple_build (&stmts, NOP_EXPR, TREE_TYPE (target_type),
    5604              :                               one_nonconstant);
    5605          195 :           orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
    5606              :                                                   target_type,
    5607              :                                                   nonconstant_for_splat);
    5608          195 :         }
    5609          439 :       else if (orig[1] == error_mark_node)
    5610              :         {
    5611              :           /* ???  See if we can convert the vector to the original type.  */
    5612          315 :           converted_orig1 = conv_code != ERROR_MARK;
    5613          315 :           unsigned n = converted_orig1 ? nelts : refnelts;
    5614          298 :           tree target_type = converted_orig1 ? type : perm_type;
    5615          315 :           tree_vector_builder vec (target_type, n, 1);
    5616         2528 :           for (unsigned i = 0; i < n; ++i)
    5617         3672 :             if (i < nelts && constants[i])
    5618              :               {
    5619         1061 :                 tree constant = constants[i];
    5620              :                 /* If there's a nop conversion, convert the constant.  */
    5621         1061 :                 if (!useless_type_conversion_p (TREE_TYPE (target_type),
    5622         1061 :                                                 TREE_TYPE (constant)))
    5623            2 :                   constant = fold_convert (TREE_TYPE (target_type), constant);
    5624         1061 :                 vec.quick_push (constant);
    5625              :               }
    5626              :             else
    5627              :               {
    5628              :                 /* ??? Push a don't-care value.  */
    5629          837 :                 tree constant = one_constant;
    5630          837 :                 if (!useless_type_conversion_p (TREE_TYPE (target_type),
    5631          837 :                                                 TREE_TYPE (constant)))
    5632            2 :                   constant = fold_convert (TREE_TYPE (target_type), constant);
    5633          837 :                 vec.quick_push (constant);
    5634              :               }
    5635          315 :           orig[1] = vec.build ();
    5636          315 :         }
    5637          683 :       tree blend_op2 = NULL_TREE;
    5638          683 :       if (converted_orig1)
    5639              :         {
    5640              :           /* Make sure we can do a blend in the target type.  */
    5641          123 :           vec_perm_builder sel (nelts, nelts, 1);
    5642          403 :           for (i = 0; i < elts.length (); ++i)
    5643          280 :             sel.quick_push (elts[i].first
    5644          280 :                             ? elts[i].second + nelts : i);
    5645          123 :           vec_perm_indices indices (sel, 2, nelts);
    5646          123 :           machine_mode vmode = TYPE_MODE (type);
    5647          123 :           if ((cfun->curr_properties & PROP_gimple_lvec)
    5648          123 :               && !can_vec_perm_const_p (vmode, vmode, indices))
    5649            0 :             return false;
    5650          123 :           mask_type = build_vector_type (ssizetype, nelts);
    5651          123 :           blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
    5652          123 :         }
    5653              : 
    5654              :       /* For a real orig[1] (no splat, constant etc.) we might need to
    5655              :          nop-convert it.  Do so here.  */
    5656          807 :       if (orig[1] && orig[1] != error_mark_node
    5657          807 :           && !converted_orig1
    5658          684 :           && !useless_type_conversion_p (perm_type, TREE_TYPE (orig[1]))
    5659          807 :           && tree_nop_conversion_p (TREE_TYPE (perm_type),
    5660            0 :                                     TREE_TYPE (TREE_TYPE (orig[1]))))
    5661            0 :         orig[1] = gimple_build (&stmts, VIEW_CONVERT_EXPR, perm_type,
    5662              :                                 orig[1]);
    5663              : 
    5664          807 :       tree orig1_for_perm
    5665          807 :         = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
    5666          807 :       tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
    5667              :                                orig[0], orig1_for_perm, op2);
    5668              :       /* If we're building a smaller vector, extract the element
    5669              :          with the proper type.  */
    5670          807 :       if (nelts != refnelts)
    5671          244 :         res = gimple_build (&stmts, BIT_FIELD_REF,
    5672              :                             conv_code != ERROR_MARK ? conv_src_type : type,
    5673              :                             res,
    5674          122 :                             TYPE_SIZE (conv_code != ERROR_MARK ? conv_src_type
    5675              :                                                                : type),
    5676          122 :                             bitsize_zero_node);
    5677              :       /* Otherwise, we can still have an intermediate sign change.
    5678              :          ??? In that case we have two subsequent conversions.
    5679              :          We should be able to merge them.  */
    5680          685 :       else if (sign_change_p)
    5681           15 :         res = gimple_build (&stmts, VIEW_CONVERT_EXPR, conv_src_type, res);
    5682              :       /* Finally, apply the conversion.  */
    5683          807 :       if (conv_code != ERROR_MARK)
    5684          157 :         res = gimple_build (&stmts, conv_code, type, res);
    5685          650 :       else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
    5686              :         {
    5687            3 :           gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
    5688              :                                 TYPE_VECTOR_SUBPARTS (perm_type))
    5689              :                       && tree_nop_conversion_p (TREE_TYPE (type),
    5690              :                                                 TREE_TYPE (perm_type)));
    5691            3 :           res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
    5692              :         }
    5693              :       /* Blend in the actual constant.  */
    5694          807 :       if (converted_orig1)
    5695          123 :         res = gimple_build (&stmts, VEC_PERM_EXPR, type,
    5696          123 :                             res, orig[1], blend_op2);
    5697          807 :       gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    5698          807 :       gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
    5699          930 :     }
    5700         1221 :   update_stmt (gsi_stmt (*gsi));
    5701         1221 :   return true;
    5702       169380 : }
    5703              : 
    5704              : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
    5705              :    lvalue.  This splits out an address computation stmt before *GSI
    5706              :    and returns a MEM_REF wrapping the address.  */
    5707              : 
    5708              : static tree
    5709         1244 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
    5710              : {
    5711         1244 :   if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
    5712          250 :     mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
    5713         1244 :   tree ptrtype = build_pointer_type (TREE_TYPE (ref));
    5714         1244 :   tree tem = make_ssa_name (ptrtype);
    5715         1244 :   gimple *new_stmt
    5716         1244 :     = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
    5717              :                                         unshare_expr (ref)));
    5718         1244 :   gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5719         2488 :   ref = build2_loc (EXPR_LOCATION (ref),
    5720         1244 :                     MEM_REF, TREE_TYPE (ref), tem,
    5721         1244 :                     build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
    5722         1244 :   return ref;
    5723              : }
    5724              : 
    5725              : /* Rewrite the vector load at *GSI to component-wise loads if the load
    5726              :    is only used in BIT_FIELD_REF extractions with eventual intermediate
    5727              :    widening.  */
    5728              : 
    5729              : static void
    5730       298118 : optimize_vector_load (gimple_stmt_iterator *gsi)
    5731              : {
    5732       298118 :   gimple *stmt = gsi_stmt (*gsi);
    5733       298118 :   tree lhs = gimple_assign_lhs (stmt);
    5734       298118 :   tree rhs = gimple_assign_rhs1 (stmt);
    5735       298118 :   tree vuse = gimple_vuse (stmt);
    5736              : 
    5737              :   /* Gather BIT_FIELD_REFs to rewrite, looking through
    5738              :      VEC_UNPACK_{LO,HI}_EXPR.  */
    5739       298118 :   use_operand_p use_p;
    5740       298118 :   imm_use_iterator iter;
    5741       298118 :   bool rewrite = true;
    5742       298118 :   bool scalar_use = false;
    5743       298118 :   bool unpack_use = false;
    5744       298118 :   auto_vec<gimple *, 8> bf_stmts;
    5745       298118 :   auto_vec<tree, 8> worklist;
    5746       298118 :   worklist.quick_push (lhs);
    5747       299935 :   do
    5748              :     {
    5749       299935 :       tree def = worklist.pop ();
    5750       299935 :       unsigned HOST_WIDE_INT def_eltsize
    5751       299935 :         = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
    5752       378936 :       FOR_EACH_IMM_USE_FAST (use_p, iter, def)
    5753              :         {
    5754       357845 :           gimple *use_stmt = USE_STMT (use_p);
    5755       357845 :           if (is_gimple_debug (use_stmt))
    5756        79001 :             continue;
    5757       356499 :           tree use_lhs;
    5758       356499 :           if (!is_gimple_assign (use_stmt)
    5759              :               /* For alias reasons we move the use to the place of the
    5760              :                  load.  Avoid this when abnormals are involved.  */
    5761       356499 :               || ((TREE_CODE ((use_lhs = gimple_assign_lhs (use_stmt)))
    5762              :                    == SSA_NAME)
    5763       245677 :                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs)))
    5764              :             {
    5765              :               rewrite = false;
    5766       278844 :               break;
    5767              :             }
    5768       321680 :           enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
    5769       321680 :           tree use_rhs = gimple_assign_rhs1 (use_stmt);
    5770       395720 :           if (use_code == BIT_FIELD_REF
    5771        74041 :               && TREE_OPERAND (use_rhs, 0) == def
    5772              :               /* If its on the VEC_UNPACK_{HI,LO}_EXPR
    5773              :                  def need to verify it is element aligned.  */
    5774       395721 :               && (def == lhs
    5775          153 :                   || (known_eq (bit_field_size (use_rhs), def_eltsize)
    5776        74193 :                       && constant_multiple_p (bit_field_offset (use_rhs),
    5777              :                                               def_eltsize)
    5778              :                       /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
    5779              :                          via a NOP_EXPR only for integral types.
    5780              :                          ???  Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR.  */
    5781          153 :                       && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
    5782              :             {
    5783        74040 :               if (!VECTOR_TYPE_P (TREE_TYPE (gimple_assign_lhs (use_stmt))))
    5784        71725 :                 scalar_use = true;
    5785        74040 :               bf_stmts.safe_push (use_stmt);
    5786        74040 :               continue;
    5787              :             }
    5788              :           /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR.  */
    5789       247640 :           if (def == lhs
    5790       245904 :               && (use_code == VEC_UNPACK_HI_EXPR
    5791       245904 :                   || use_code == VEC_UNPACK_LO_EXPR)
    5792         3615 :               && use_rhs == lhs)
    5793              :             {
    5794         3615 :               unpack_use = true;
    5795         3615 :               worklist.safe_push (gimple_assign_lhs (use_stmt));
    5796         3615 :               continue;
    5797              :             }
    5798              :           rewrite = false;
    5799              :           break;
    5800       299935 :         }
    5801       299935 :       if (!rewrite)
    5802              :         break;
    5803              :     }
    5804        42182 :   while (!worklist.is_empty ());
    5805              : 
    5806       298118 :   rewrite = rewrite && (scalar_use
    5807        19274 :                         || unpack_use
    5808          626 :                         || !can_implement_p (mov_optab,
    5809          626 :                                              TYPE_MODE (TREE_TYPE (lhs))));
    5810       279026 :   if (!rewrite)
    5811              :     {
    5812       279026 :       gsi_next (gsi);
    5813       279026 :       return;
    5814              :     }
    5815              :   /* We now have all ultimate uses of the load to rewrite in bf_stmts.  */
    5816              : 
    5817              :   /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
    5818              :      For TARGET_MEM_REFs we have to separate the LEA from the reference.  */
    5819        19092 :   tree load_rhs = rhs;
    5820        19092 :   if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
    5821         1243 :     load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
    5822              : 
    5823              :   /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
    5824              :      the place of the original load.  */
    5825       124520 :   for (gimple *use_stmt : bf_stmts)
    5826              :     {
    5827        67244 :       tree bfr = gimple_assign_rhs1 (use_stmt);
    5828        67244 :       tree new_rhs = unshare_expr (load_rhs);
    5829        67244 :       if (TREE_OPERAND (bfr, 0) != lhs)
    5830              :         {
    5831              :           /* When the BIT_FIELD_REF is on the promoted vector we have to
    5832              :              adjust it and emit a conversion afterwards.  */
    5833          152 :           gimple *def_stmt
    5834          152 :               = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
    5835          152 :           enum tree_code def_code
    5836          152 :               = gimple_assign_rhs_code (def_stmt);
    5837              : 
    5838              :           /* The adjusted BIT_FIELD_REF is of the promotion source
    5839              :              vector size and at half of the offset...  */
    5840          152 :           new_rhs = fold_build3 (BIT_FIELD_REF,
    5841              :                                  TREE_TYPE (TREE_TYPE (lhs)),
    5842              :                                  new_rhs,
    5843              :                                  TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
    5844              :                                  size_binop (EXACT_DIV_EXPR,
    5845              :                                              TREE_OPERAND (bfr, 2),
    5846              :                                              bitsize_int (2)));
    5847              :           /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR.  */
    5848          152 :           if (def_code == (!BYTES_BIG_ENDIAN
    5849              :                            ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
    5850           76 :             TREE_OPERAND (new_rhs, 2)
    5851          152 :               = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
    5852              :                             size_binop (EXACT_DIV_EXPR,
    5853              :                                         TYPE_SIZE (TREE_TYPE (lhs)),
    5854              :                                         bitsize_int (2)));
    5855          152 :           tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
    5856          152 :           gimple *new_stmt = gimple_build_assign (tem, new_rhs);
    5857          152 :           location_t loc = gimple_location (use_stmt);
    5858          152 :           gimple_set_location (new_stmt, loc);
    5859          152 :           gimple_set_vuse (new_stmt, vuse);
    5860          152 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5861              :           /* Perform scalar promotion.  */
    5862          152 :           new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
    5863              :                                           NOP_EXPR, tem);
    5864          152 :           gimple_set_location (new_stmt, loc);
    5865          152 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5866              :         }
    5867              :       else
    5868              :         {
    5869              :           /* When the BIT_FIELD_REF is on the original load result
    5870              :              we can just wrap that.  */
    5871        67092 :           tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
    5872              :                                       unshare_expr (load_rhs),
    5873              :                                       TREE_OPERAND (bfr, 1),
    5874              :                                       TREE_OPERAND (bfr, 2));
    5875        67092 :           gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
    5876              :                                                   new_rhs);
    5877        67092 :           location_t loc = gimple_location (use_stmt);
    5878        67092 :           gimple_set_location (new_stmt, loc);
    5879        67092 :           gimple_set_vuse (new_stmt, vuse);
    5880        67092 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5881              :         }
    5882        67244 :       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    5883        67244 :       unlink_stmt_vdef (use_stmt);
    5884        67244 :       gsi_remove (&gsi2, true);
    5885              :     }
    5886              : 
    5887              :   /* Finally get rid of the intermediate stmts.  */
    5888        19092 :   gimple *use_stmt;
    5889        19690 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    5890              :     {
    5891          598 :       if (is_gimple_debug (use_stmt))
    5892              :         {
    5893          536 :           if (gimple_debug_bind_p (use_stmt))
    5894              :             {
    5895          536 :               gimple_debug_bind_reset_value (use_stmt);
    5896          536 :               update_stmt (use_stmt);
    5897              :             }
    5898          536 :           continue;
    5899              :         }
    5900           62 :       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    5901           62 :       unlink_stmt_vdef (use_stmt);
    5902           62 :       release_defs (use_stmt);
    5903           62 :       gsi_remove (&gsi2, true);
    5904        19092 :     }
    5905              :   /* And the original load.  */
    5906        19092 :   release_defs (stmt);
    5907        19092 :   gsi_remove (gsi, true);
    5908       298118 : }
    5909              : 
    5910              : 
    5911              : /* Primitive "lattice" function for gimple_simplify.  */
    5912              : 
    5913              : static tree
    5914   1818111503 : fwprop_ssa_val (tree name)
    5915              : {
    5916              :   /* First valueize NAME.  */
    5917   1818111503 :   if (TREE_CODE (name) == SSA_NAME
    5918   1818111503 :       && SSA_NAME_VERSION (name) < lattice.length ())
    5919              :     {
    5920   1817054793 :       tree val = lattice[SSA_NAME_VERSION (name)];
    5921   1817054793 :       if (val)
    5922   1818111503 :         name = val;
    5923              :     }
    5924              :   /* We continue matching along SSA use-def edges for SSA names
    5925              :      that are not single-use.  Currently there are no patterns
    5926              :      that would cause any issues with that.  */
    5927   1818111503 :   return name;
    5928              : }
    5929              : 
    5930              : /* Search for opportunities to free half of the lanes in the following pattern:
    5931              : 
    5932              :      v_in = {e0, e1, e2, e3}
    5933              :      v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
    5934              :      // v_1 = {e0, e2, e0, e2}
    5935              :      v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
    5936              :      // v_2 = {e1, e3, e1, e3}
    5937              : 
    5938              :      v_x = v_1 + v_2
    5939              :      // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
    5940              :      v_y = v_1 - v_2
    5941              :      // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
    5942              : 
    5943              :      v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
    5944              :      // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
    5945              : 
    5946              :    The last statement could be simplified to:
    5947              :      v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
    5948              :      // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
    5949              : 
    5950              :    Characteristic properties:
    5951              :    - v_1 and v_2 are created from the same input vector v_in and introduce the
    5952              :      lane duplication (in the selection operand) that we can eliminate.
    5953              :    - v_x and v_y are results from lane-preserving operations that use v_1 and
    5954              :      v_2 as inputs.
    5955              :    - v_out is created by selecting from duplicated lanes.  */
    5956              : 
    5957              : static bool
    5958       190176 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
    5959              : {
    5960       190176 :   unsigned HOST_WIDE_INT nelts;
    5961              : 
    5962       190176 :   gcc_checking_assert (stmt);
    5963       190176 :   gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
    5964       190176 :   basic_block bb = gimple_bb (stmt);
    5965              : 
    5966              :   /* Decompose the final vec permute statement.  */
    5967       190176 :   tree v_x = gimple_assign_rhs1 (stmt);
    5968       190176 :   tree v_y = gimple_assign_rhs2 (stmt);
    5969       190176 :   tree sel = gimple_assign_rhs3 (stmt);
    5970              : 
    5971       190176 :   if (TREE_CODE (sel) != VECTOR_CST
    5972       265590 :       || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
    5973       187407 :       || TREE_CODE (v_x) != SSA_NAME
    5974       185338 :       || TREE_CODE (v_y) != SSA_NAME
    5975       179715 :       || !has_single_use (v_x)
    5976       302015 :       || !has_single_use (v_y))
    5977              :     return false;
    5978              : 
    5979              :   /* Don't analyse sequences with many lanes.  */
    5980       109924 :   if (nelts > 4)
    5981              :     return false;
    5982              : 
    5983              :   /* Lookup the definition of v_x and v_y.  */
    5984       106305 :   gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
    5985       106305 :   gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
    5986       105928 :   if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
    5987       212233 :       || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
    5988              :     return false;
    5989              : 
    5990              :   /* Check the operations that define v_x and v_y.  */
    5991       105921 :   if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
    5992       108036 :       || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
    5993              :     return false;
    5994              : 
    5995         2115 :   tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
    5996         2115 :   tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
    5997         2115 :   tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
    5998         2115 :   tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
    5999              : 
    6000         2115 :   if (v_x_stmt == v_y_stmt
    6001         2115 :       || TREE_CODE (v_x_1) != SSA_NAME
    6002         2112 :       || TREE_CODE (v_x_2) != SSA_NAME
    6003         2088 :       || num_imm_uses (v_x_1) != 2
    6004         4039 :       || num_imm_uses (v_x_2) != 2)
    6005              :     return false;
    6006              : 
    6007         1883 :   if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
    6008              :     {
    6009              :       /* Allow operands of commutative operators to swap.  */
    6010          660 :       if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
    6011              :         {
    6012              :           /* Keep v_x_1 the first operand for non-commutative operators.  */
    6013          258 :           std::swap (v_x_1, v_x_2);
    6014          258 :           if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
    6015              :             return false;
    6016              :         }
    6017          402 :       else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
    6018              :         {
    6019          402 :           if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
    6020              :             return false;
    6021              :         }
    6022              :       else
    6023              :         return false;
    6024              :     }
    6025         1883 :   gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
    6026         1883 :   gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
    6027         1819 :   if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
    6028         3702 :       || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
    6029              :     return false;
    6030              : 
    6031         1813 :   if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
    6032         1935 :       || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
    6033              :     return false;
    6034              : 
    6035              :   /* Decompose initial VEC_PERM_EXPRs.  */
    6036          108 :   tree v_in = gimple_assign_rhs1 (v_1_stmt);
    6037          108 :   tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
    6038          108 :   tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
    6039          108 :   if (v_in != gimple_assign_rhs2 (v_1_stmt)
    6040          103 :       || v_in != gimple_assign_rhs1 (v_2_stmt)
    6041          209 :       || v_in != gimple_assign_rhs2 (v_2_stmt))
    6042              :     return false;
    6043              : 
    6044          101 :   unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
    6045          101 :   if (TREE_CODE (v_1_sel) != VECTOR_CST
    6046          101 :       || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
    6047          101 :       || TREE_CODE (v_2_sel) != VECTOR_CST
    6048          202 :       || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
    6049              :     return false;
    6050              : 
    6051          101 :   if (nelts != v_1_nelts || nelts != v_2_nelts)
    6052              :     return false;
    6053              : 
    6054              :   /* Create the new selector.  */
    6055          101 :   vec_perm_builder new_sel_perm (nelts, nelts, 1);
    6056          101 :   auto_vec<bool> lanes (nelts);
    6057          101 :   lanes.quick_grow_cleared (nelts);
    6058          606 :   for (unsigned int i = 0; i < nelts; i++)
    6059              :     {
    6060              :       /* Extract the i-th value from the selector.  */
    6061          404 :       unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
    6062          404 :       unsigned int lane = sel_cst % nelts;
    6063          404 :       unsigned int offs = sel_cst / nelts;
    6064              : 
    6065              :       /* Check what's in the lane.  */
    6066          404 :       unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
    6067          404 :       unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
    6068              : 
    6069              :       /* Reuse previous lane (if any).  */
    6070          404 :       unsigned int l = 0;
    6071          687 :       for (; l < lane; l++)
    6072              :         {
    6073          481 :           if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
    6074          481 :               && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
    6075              :             break;
    6076              :         }
    6077              : 
    6078              :       /* Add to narrowed selector.  */
    6079          404 :       new_sel_perm.quick_push (l + offs * nelts);
    6080              : 
    6081              :       /* Mark lane as used.  */
    6082          404 :       lanes[l] = true;
    6083              :     }
    6084              : 
    6085              :   /* Count how many lanes are need.  */
    6086              :   unsigned int cnt = 0;
    6087          505 :   for (unsigned int i = 0; i < nelts; i++)
    6088          404 :     cnt += lanes[i];
    6089              : 
    6090              :   /* If more than (nelts/2) lanes are needed, skip the sequence.  */
    6091          101 :   if (cnt > nelts / 2)
    6092              :     return false;
    6093              : 
    6094              :   /* Check if the resulting permutation is cheap.  */
    6095          101 :   vec_perm_indices new_indices (new_sel_perm, 2, nelts);
    6096          101 :   tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
    6097          101 :   machine_mode vmode = TYPE_MODE (vectype);
    6098          101 :   if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
    6099              :     return false;
    6100              : 
    6101          101 :   *seq = XNEW (struct _vec_perm_simplify_seq);
    6102          101 :   (*seq)->stmt = stmt;
    6103          101 :   (*seq)->v_1_stmt = v_1_stmt;
    6104          101 :   (*seq)->v_2_stmt = v_2_stmt;
    6105          101 :   (*seq)->v_x_stmt = v_x_stmt;
    6106          101 :   (*seq)->v_y_stmt = v_y_stmt;
    6107          101 :   (*seq)->nelts = nelts;
    6108          101 :   (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
    6109              : 
    6110          101 :   if (dump_file)
    6111              :     {
    6112           28 :       fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
    6113           28 :       print_gimple_stmt (dump_file, stmt, 0);
    6114              : 
    6115           28 :       if (dump_flags & TDF_DETAILS)
    6116              :         {
    6117           28 :           fprintf (dump_file, "\tNarrowed vec_perm selector: ");
    6118           28 :           print_generic_expr (dump_file, (*seq)->new_sel);
    6119           28 :           fprintf (dump_file, "\n");
    6120              :         }
    6121              :     }
    6122              : 
    6123              :   return true;
    6124          202 : }
    6125              : 
    6126              : /* Reduce the lane consumption of a simplifiable vec perm sequence.  */
    6127              : 
    6128              : static void
    6129           74 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
    6130              : {
    6131           74 :   gassign *stmt = seq->stmt;
    6132           74 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6133              :     {
    6134           22 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6135           22 :       fprintf (dump_file, "Old stmt: ");
    6136           22 :       print_gimple_stmt (dump_file, stmt, 0);
    6137              :     }
    6138              : 
    6139              :   /* Update the last VEC_PERM statement.  */
    6140           74 :   gimple_assign_set_rhs3 (stmt, seq->new_sel);
    6141           74 :   update_stmt (stmt);
    6142              : 
    6143           74 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6144              :     {
    6145           22 :       fprintf (dump_file, "New stmt: ");
    6146           22 :       print_gimple_stmt (dump_file, stmt, 0);
    6147              :     }
    6148           74 : }
    6149              : 
    6150              : /* Test if we can blend two simplifiable vec permute sequences.
    6151              :    NEED_SWAP will be set, if sequences must be swapped for blending.  */
    6152              : 
    6153              : static bool
    6154           47 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
    6155              :                                     vec_perm_simplify_seq seq2,
    6156              :                                     bool *need_swap)
    6157              : {
    6158           47 :   unsigned int nelts = seq1->nelts;
    6159           47 :   basic_block bb = gimple_bb (seq1->stmt);
    6160              : 
    6161           47 :   gcc_assert (gimple_bb (seq2->stmt) == bb);
    6162              : 
    6163              :   /* BBs and number of elements must be equal.  */
    6164           47 :   if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
    6165              :     return false;
    6166              : 
    6167              :   /* We need vectors of the same type.  */
    6168           47 :   if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
    6169           47 :       != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
    6170              :     return false;
    6171              : 
    6172              :   /* We require isomorphic operators.  */
    6173           41 :   if (((gimple_assign_rhs_code (seq1->v_x_stmt)
    6174           41 :         != gimple_assign_rhs_code (seq2->v_x_stmt))
    6175           41 :        || (gimple_assign_rhs_code (seq1->v_y_stmt)
    6176           41 :            != gimple_assign_rhs_code (seq2->v_y_stmt))))
    6177              :     return false;
    6178              : 
    6179              :   /* We cannot have any dependencies between the sequences.
    6180              : 
    6181              :      For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
    6182              :      seq1's v_in is defined before these statements, but we need
    6183              :      to check if seq2's v_in is defined before them as well.
    6184              : 
    6185              :      Further, we will reuse seq2->stmt.  We need to ensure that
    6186              :      seq1->v_x_stmt and seq1->v_y_stmt are before it.
    6187              : 
    6188              :      Note, that we don't need to check the BBs here, because all
    6189              :      statements of both sequences have to be in the same BB.  */
    6190              : 
    6191           41 :   tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
    6192           41 :   if (TREE_CODE (seq2_v_in) != SSA_NAME)
    6193              :     return false;
    6194              : 
    6195           41 :   gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
    6196           41 :   if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
    6197           41 :       || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
    6198           37 :       || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
    6199           37 :       || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
    6200              :     {
    6201            4 :       tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
    6202            4 :       if (TREE_CODE (seq1_v_in) != SSA_NAME)
    6203              :         return false;
    6204              : 
    6205            4 :       gassign *seq1_v_in_stmt
    6206            4 :         = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
    6207              :       /* Let's try to see if we succeed when swapping the sequences.  */
    6208            4 :       if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
    6209            0 :           || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
    6210            0 :           || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
    6211            0 :           || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
    6212              :         return false;
    6213            0 :       *need_swap = true;
    6214              :     }
    6215              :   else
    6216           37 :     *need_swap = false;
    6217              : 
    6218           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6219           11 :     fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
    6220              : 
    6221              :   return true;
    6222              : }
    6223              : 
    6224              : /* Calculate the permutations for blending the two given vec permute
    6225              :    sequences.  This may fail if the resulting permutation is not
    6226              :    supported.  */
    6227              : 
    6228              : static bool
    6229           37 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
    6230              :                                   vec_perm_simplify_seq seq2,
    6231              :                                   vec_perm_indices *seq2_stmt_indices,
    6232              :                                   vec_perm_indices *seq1_v_1_stmt_indices,
    6233              :                                   vec_perm_indices *seq1_v_2_stmt_indices)
    6234              : {
    6235           37 :   unsigned int i;
    6236           37 :   unsigned int nelts = seq1->nelts;
    6237           37 :   auto_vec<unsigned int> lane_assignment;
    6238           37 :   lane_assignment.create (nelts);
    6239              : 
    6240              :   /* Mark all lanes as free.  */
    6241           37 :   lane_assignment.quick_grow_cleared (nelts);
    6242              : 
    6243              :   /* Allocate lanes for seq1.  */
    6244          222 :   for (i = 0; i < nelts; i++)
    6245              :     {
    6246          148 :       unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
    6247          148 :       l %= nelts;
    6248          148 :       lane_assignment[l] = 1;
    6249              :     }
    6250              : 
    6251              :   /* Allocate lanes for seq2 and calculate selector for seq2->stmt.  */
    6252           37 :   vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
    6253          185 :   for (i = 0; i < nelts; i++)
    6254              :     {
    6255          148 :       unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
    6256          148 :       unsigned int lane = sel % nelts;
    6257          148 :       unsigned int offs = sel / nelts;
    6258          148 :       unsigned int new_sel;
    6259              : 
    6260              :       /* Check if we already allocated the lane for seq2.  */
    6261          148 :       unsigned int j = 0;
    6262          263 :       for (; j < i; j++)
    6263              :         {
    6264          189 :           unsigned int sel_old;
    6265          189 :           sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
    6266          189 :           unsigned int lane_old = sel_old % nelts;
    6267          189 :           if (lane == lane_old)
    6268              :             {
    6269           74 :               new_sel = seq2_stmt_sel_perm[j].to_constant ();
    6270           74 :               new_sel = (new_sel % nelts) + offs * nelts;
    6271           74 :               break;
    6272              :             }
    6273              :         }
    6274              : 
    6275              :       /* If the lane is not allocated, we need to do that now.  */
    6276          148 :       if (j == i)
    6277              :         {
    6278              :           unsigned int l_orig = lane;
    6279          182 :           while (lane_assignment[lane] != 0)
    6280              :             {
    6281          108 :               lane = (lane + 1) % nelts;
    6282              : 
    6283              :               /* This should not happen if both sequences utilize no more than
    6284              :                  half of the lanes.  Test anyway to guarantee termination.  */
    6285          108 :               if (lane == l_orig)
    6286           37 :                 return false;
    6287              :             }
    6288              : 
    6289              :           /* Allocate lane.  */
    6290           74 :           lane_assignment[lane] = 2 + l_orig;
    6291           74 :           new_sel = lane + offs * nelts;
    6292              :         }
    6293              : 
    6294          148 :       seq2_stmt_sel_perm.quick_push (new_sel);
    6295              :     }
    6296              : 
    6297              :   /* Check if the resulting permutation is cheap.  */
    6298           37 :   seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
    6299           37 :   tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
    6300           37 :   machine_mode vmode = TYPE_MODE (vectype);
    6301           37 :   if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
    6302              :     return false;
    6303              : 
    6304              :   /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt.  */
    6305           37 :   vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
    6306           37 :   vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
    6307          185 :   for (i = 0; i < nelts; i++)
    6308              :     {
    6309          148 :       bool use_seq1 = lane_assignment[i] < 2;
    6310          148 :       unsigned int l1, l2;
    6311              : 
    6312          148 :       if (use_seq1)
    6313              :         {
    6314              :           /* Just reuse the selector indices.  */
    6315           74 :           tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
    6316           74 :           tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
    6317           74 :           l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
    6318           74 :           l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
    6319              :         }
    6320              :       else
    6321              :         {
    6322              :           /* We moved the lanes for seq2, so we need to adjust for that.  */
    6323           74 :           tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
    6324           74 :           tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
    6325           74 :           l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, lane_assignment[i] - 2));
    6326           74 :           l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, lane_assignment[i] - 2));
    6327              :         }
    6328              : 
    6329          148 :       l1 %= nelts;
    6330          148 :       l2 %= nelts;
    6331          222 :       seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
    6332          148 :       seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
    6333              :     }
    6334              : 
    6335           37 :   seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
    6336           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
    6337           37 :   vmode = TYPE_MODE (vectype);
    6338           37 :   if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
    6339              :     return false;
    6340              : 
    6341           37 :   seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
    6342           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
    6343           37 :   vmode = TYPE_MODE (vectype);
    6344           37 :   if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
    6345              :     return false;
    6346              : 
    6347              :   return true;
    6348           74 : }
    6349              : 
    6350              : /* Blend the two given simplifiable vec permute sequences using the
    6351              :    given permutations.  */
    6352              : 
    6353              : static void
    6354           37 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
    6355              :                               vec_perm_simplify_seq seq2,
    6356              :                               const vec_perm_indices &seq2_stmt_indices,
    6357              :                               const vec_perm_indices &seq1_v_1_stmt_indices,
    6358              :                               const vec_perm_indices &seq1_v_2_stmt_indices)
    6359              : {
    6360              :   /* We don't need to adjust seq1->stmt because its lanes consumption
    6361              :      was already narrowed before entering this function.  */
    6362              : 
    6363              :   /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel.  */
    6364           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6365              :     {
    6366           11 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6367           11 :       fprintf (dump_file, "Old stmt: ");
    6368           11 :       print_gimple_stmt (dump_file, seq2->stmt, 0);
    6369              :     }
    6370              : 
    6371           37 :   gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
    6372           74 :   gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
    6373           37 :   tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
    6374           37 :   tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
    6375           37 :   gimple_assign_set_rhs3 (seq2->stmt, sel);
    6376           37 :   update_stmt (seq2->stmt);
    6377              : 
    6378           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6379              :     {
    6380           11 :       fprintf (dump_file, "New stmt: ");
    6381           11 :       print_gimple_stmt (dump_file, seq2->stmt, 0);
    6382              :     }
    6383              : 
    6384              :   /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel.  */
    6385           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6386              :     {
    6387           11 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6388           11 :       fprintf (dump_file, "Old stmt: ");
    6389           11 :       print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
    6390              :     }
    6391              : 
    6392           37 :   gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
    6393           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
    6394           37 :   sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
    6395           37 :   gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
    6396           37 :   update_stmt (seq1->v_1_stmt);
    6397              : 
    6398           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6399              :     {
    6400           11 :       fprintf (dump_file, "New stmt: ");
    6401           11 :       print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
    6402              :     }
    6403              : 
    6404              :   /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel.  */
    6405           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6406              :     {
    6407           11 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6408           11 :       fprintf (dump_file, "Old stmt: ");
    6409           11 :       print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
    6410              :     }
    6411              : 
    6412           37 :   gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
    6413           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
    6414           37 :   sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
    6415           37 :   gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
    6416           37 :   update_stmt (seq1->v_2_stmt);
    6417              : 
    6418           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6419              :     {
    6420           11 :       fprintf (dump_file, "New stmt: ");
    6421           11 :       print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
    6422              :     }
    6423              : 
    6424              :   /* At this point, we have four unmodified seq2 stmts, which will be
    6425              :      eliminated by DCE.  */
    6426              : 
    6427           37 :   if (dump_file)
    6428           11 :     fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
    6429           37 : }
    6430              : 
    6431              : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
    6432              :    The provided list will be empty after this call.  */
    6433              : 
    6434              : static void
    6435    337101029 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
    6436              : {
    6437    337101029 :   unsigned int i, j;
    6438    337101029 :   vec_perm_simplify_seq seq1, seq2;
    6439              : 
    6440    337101029 :   if (l->is_empty ())
    6441    337100984 :     return;
    6442              : 
    6443           45 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6444           13 :     fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
    6445              :              l->length ());
    6446              : 
    6447          154 :   FOR_EACH_VEC_ELT (*l, i, seq1)
    6448              :     {
    6449           64 :       if (i + 1 < l->length ())
    6450              :         {
    6451          115 :           FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
    6452              :             {
    6453           47 :               bool swap = false;
    6454           47 :               if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
    6455              :                 {
    6456           37 :                   vec_perm_indices seq2_stmt_indices;
    6457           37 :                   vec_perm_indices seq1_v_1_stmt_indices;
    6458           37 :                   vec_perm_indices seq1_v_2_stmt_indices;
    6459          111 :                   if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
    6460              :                                                         swap ? seq1 : seq2,
    6461              :                                                         &seq2_stmt_indices,
    6462              :                                                         &seq1_v_1_stmt_indices,
    6463              :                                                         &seq1_v_2_stmt_indices))
    6464              :                     {
    6465              :                       /* Narrow lane usage.  */
    6466           37 :                       narrow_vec_perm_simplify_seq (seq1);
    6467           37 :                       narrow_vec_perm_simplify_seq (seq2);
    6468              : 
    6469              :                       /* Blend sequences.  */
    6470           37 :                       blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
    6471              :                                                     swap ? seq1 : seq2,
    6472              :                                                     seq2_stmt_indices,
    6473              :                                                     seq1_v_1_stmt_indices,
    6474              :                                                     seq1_v_2_stmt_indices);
    6475              : 
    6476              :                       /* We can use unordered_remove as we break the loop.  */
    6477           37 :                       l->unordered_remove (j);
    6478           37 :                       XDELETE (seq2);
    6479           37 :                       break;
    6480              :                     }
    6481           37 :                 }
    6482              :             }
    6483              :         }
    6484              : 
    6485              :       /* We don't need to call l->remove for seq1.  */
    6486           64 :       XDELETE (seq1);
    6487              :     }
    6488              : 
    6489           45 :   l->truncate (0);
    6490              : }
    6491              : 
    6492              : static void
    6493          101 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
    6494              :                                    const vec_perm_simplify_seq &seq)
    6495              : {
    6496              :   /* If no space on list left, then process the list.  */
    6497          101 :   if (!l->space (1))
    6498            0 :       process_vec_perm_simplify_seq_list (l);
    6499              : 
    6500          101 :   l->quick_push (seq);
    6501          101 : }
    6502              : 
    6503              : /* Main entry point for the forward propagation and statement combine
    6504              :    optimizer.  */
    6505              : 
    6506              : namespace {
    6507              : 
    6508              : const pass_data pass_data_forwprop =
    6509              : {
    6510              :   GIMPLE_PASS, /* type */
    6511              :   "forwprop", /* name */
    6512              :   OPTGROUP_NONE, /* optinfo_flags */
    6513              :   TV_TREE_FORWPROP, /* tv_id */
    6514              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    6515              :   0, /* properties_provided */
    6516              :   0, /* properties_destroyed */
    6517              :   0, /* todo_flags_start */
    6518              :   0, /* todo_flags_finish */
    6519              : };
    6520              : 
    6521              : class pass_forwprop : public gimple_opt_pass
    6522              : {
    6523              : public:
    6524      1472935 :   pass_forwprop (gcc::context *ctxt)
    6525      2945870 :     : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
    6526              :   {}
    6527              : 
    6528              :   /* opt_pass methods: */
    6529      1178348 :   opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
    6530      1767522 :   void set_pass_param (unsigned int n, bool param) final override
    6531              :     {
    6532      1767522 :       switch (n)
    6533              :         {
    6534      1178348 :           case 0:
    6535      1178348 :             m_full_walk = param;
    6536      1178348 :             break;
    6537       589174 :           case 1:
    6538       589174 :             last_p = param;
    6539       589174 :             break;
    6540            0 :           default:
    6541            0 :           gcc_unreachable();
    6542              :         }
    6543      1767522 :     }
    6544      5734550 :   bool gate (function *) final override { return flag_tree_forwprop; }
    6545              :   unsigned int execute (function *) final override;
    6546              : 
    6547              :  private:
    6548              :   /* Determines whether the pass instance should set PROP_last_full_fold.  */
    6549              :   bool last_p;
    6550              : 
    6551              :   /* True if the aggregate props are doing a full walk or not.  */
    6552              :   bool m_full_walk = false;
    6553              : }; // class pass_forwprop
    6554              : 
    6555              : /* Attempt to make the BB block of __builtin_unreachable unreachable by changing
    6556              :    the incoming jumps.  Return true if at least one jump was changed.  */
    6557              : 
    6558              : static bool
    6559         1143 : optimize_unreachable (basic_block bb)
    6560              : {
    6561         1143 :   gimple_stmt_iterator gsi;
    6562         1143 :   gimple *stmt;
    6563         1143 :   edge_iterator ei;
    6564         1143 :   edge e;
    6565         1143 :   bool ret;
    6566              : 
    6567         1143 :   ret = false;
    6568         2358 :   FOR_EACH_EDGE (e, ei, bb->preds)
    6569              :     {
    6570         1215 :       gsi = gsi_last_bb (e->src);
    6571         1215 :       if (gsi_end_p (gsi))
    6572          323 :         continue;
    6573              : 
    6574          892 :       stmt = gsi_stmt (gsi);
    6575          892 :       if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
    6576              :         {
    6577              :           /* If the condition is already true/false
    6578              :              ignore it. This can happen during copy prop of forwprop. */
    6579          704 :           if (gimple_cond_true_p (cond_stmt)
    6580          696 :               || gimple_cond_false_p (cond_stmt))
    6581            8 :             continue;
    6582          688 :           else if (e->flags & EDGE_TRUE_VALUE)
    6583          596 :             gimple_cond_make_false (cond_stmt);
    6584           92 :           else if (e->flags & EDGE_FALSE_VALUE)
    6585           92 :             gimple_cond_make_true (cond_stmt);
    6586              :           else
    6587            0 :             gcc_unreachable ();
    6588          688 :           update_stmt (cond_stmt);
    6589              :         }
    6590              :       else
    6591              :         {
    6592              :           /* Todo: handle other cases.  e.g. switch.  */
    6593          196 :           continue;
    6594              :         }
    6595              : 
    6596          688 :       ret = true;
    6597              :     }
    6598              : 
    6599         1143 :   return ret;
    6600              : }
    6601              : 
    6602              : unsigned int
    6603      5731900 : pass_forwprop::execute (function *fun)
    6604              : {
    6605      5731900 :   unsigned int todoflags = 0;
    6606              :   /* Handle a full walk only when expensive optimizations are on.  */
    6607      5731900 :   bool full_walk = m_full_walk && flag_expensive_optimizations;
    6608              : 
    6609      5731900 :   cfg_changed = false;
    6610      5731900 :   if (last_p)
    6611      1064325 :     fun->curr_properties |= PROP_last_full_fold;
    6612              : 
    6613      5731900 :   calculate_dominance_info (CDI_DOMINATORS);
    6614              : 
    6615              :   /* Combine stmts with the stmts defining their operands.  Do that
    6616              :      in an order that guarantees visiting SSA defs before SSA uses.  */
    6617     11463800 :   lattice.create (num_ssa_names);
    6618     11463800 :   lattice.quick_grow_cleared (num_ssa_names);
    6619      5731900 :   int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
    6620      5731900 :   int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
    6621              :                                                          postorder, false);
    6622      5731900 :   int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
    6623     57313082 :   for (int i = 0; i < postorder_num; ++i)
    6624              :     {
    6625     45849282 :       bb_to_rpo[postorder[i]] = i;
    6626     45849282 :       edge_iterator ei;
    6627     45849282 :       edge e;
    6628    110344388 :       FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
    6629     64495106 :         e->flags &= ~EDGE_EXECUTABLE;
    6630              :     }
    6631      5731900 :   single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
    6632      5731900 :     |= EDGE_EXECUTABLE;
    6633      5731900 :   auto_vec<gimple *, 4> to_fixup;
    6634      5731900 :   auto_vec<gimple *, 32> to_remove;
    6635      5731900 :   auto_vec<unsigned, 32> to_remove_defs;
    6636      5731900 :   auto_vec<std::pair<int, int>, 10> edges_to_remove;
    6637      5731900 :   auto_bitmap simple_dce_worklist;
    6638      5731900 :   auto_bitmap need_ab_cleanup;
    6639      5731900 :   to_purge = BITMAP_ALLOC (NULL);
    6640      5731900 :   auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
    6641     51581182 :   for (int i = 0; i < postorder_num; ++i)
    6642              :     {
    6643     45849282 :       gimple_stmt_iterator gsi;
    6644     45849282 :       basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
    6645     45849282 :       edge_iterator ei;
    6646     45849282 :       edge e;
    6647              : 
    6648              :       /* Skip processing not executable blocks.  We could improve
    6649              :          single_use tracking by at least unlinking uses from unreachable
    6650              :          blocks but since blocks with uses are not processed in a
    6651              :          meaningful order this is probably not worth it.  */
    6652     45849282 :       bool any = false;
    6653     47035108 :       FOR_EACH_EDGE (e, ei, bb->preds)
    6654              :         {
    6655     47007725 :           if ((e->flags & EDGE_EXECUTABLE)
    6656              :               /* We can handle backedges in natural loops correctly but
    6657              :                  for irreducible regions we have to take all backedges
    6658              :                  conservatively when we did not visit the source yet.  */
    6659     47007725 :               || (bb_to_rpo[e->src->index] > i
    6660       680347 :                   && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
    6661              :             {
    6662              :               any = true;
    6663              :               break;
    6664              :             }
    6665              :         }
    6666     45849282 :       if (!any)
    6667        28033 :         continue;
    6668              : 
    6669              :       /* Remove conditions that go directly to unreachable when this is the last forwprop.  */
    6670     45821899 :       if (last_p
    6671     10011381 :           && !(flag_sanitize & SANITIZE_UNREACHABLE))
    6672              :         {
    6673     10006430 :           gimple_stmt_iterator gsi;
    6674     10006430 :           gsi = gsi_start_nondebug_after_labels_bb (bb);
    6675     10007080 :           if (!gsi_end_p (gsi)
    6676      9148781 :               && gimple_call_builtin_p (*gsi, BUILT_IN_UNREACHABLE)
    6677     10007573 :               && optimize_unreachable (bb))
    6678              :             {
    6679          650 :               cfg_changed = true;
    6680          650 :               continue;
    6681              :             }
    6682              :         }
    6683              : 
    6684              :       /* Fold PHI-form long-multiply carries and record degenerate
    6685              :          PHIs in the lattice.  Iterator advanced up front so a folded
    6686              :          PHI can be removed in-flight; a long-mul carry PHI is never
    6687              :          degenerate, so the two cases are disjoint.  */
    6688     62009164 :       for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);)
    6689              :         {
    6690     16187915 :           gphi *phi = si.phi ();
    6691     16187915 :           gsi_next (&si);
    6692     16187915 :           tree res = gimple_phi_result (phi);
    6693     32375830 :           if (virtual_operand_p (res))
    6694      7422945 :             continue;
    6695      8764984 :           if (match_long_mul_phi (phi))
    6696           14 :             continue;
    6697              : 
    6698      8764970 :           tree first = NULL_TREE;
    6699      8764970 :           bool all_same = true;
    6700      8764970 :           edge_iterator ei;
    6701      8764970 :           edge e;
    6702     18056439 :           FOR_EACH_EDGE (e, ei, bb->preds)
    6703              :             {
    6704              :               /* Ignore not executable forward edges.  */
    6705     17819978 :               if (!(e->flags & EDGE_EXECUTABLE))
    6706              :                 {
    6707      4132516 :                   if (bb_to_rpo[e->src->index] < i)
    6708        18223 :                     continue;
    6709              :                   /* Avoid equivalences from backedges - while we might
    6710              :                      be able to make irreducible regions reducible and
    6711              :                      thus turning a back into a forward edge we do not
    6712              :                      want to deal with the intermediate SSA issues that
    6713              :                      exposes.  */
    6714              :                   all_same = false;
    6715              :                 }
    6716     17801755 :               tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
    6717     17801755 :               if (use == res)
    6718              :                 /* The PHI result can also appear on a backedge, if so
    6719              :                    we can ignore this case for the purpose of determining
    6720              :                    the singular value.  */
    6721              :                 ;
    6722     17788705 :               else if (! first)
    6723              :                 first = use;
    6724      9023735 :               else if (! operand_equal_p (first, use, 0))
    6725              :                 {
    6726              :                   all_same = false;
    6727              :                   break;
    6728              :                 }
    6729              :             }
    6730      8764970 :           if (all_same)
    6731              :             {
    6732       231789 :               if (may_propagate_copy (res, first))
    6733       231131 :                 to_remove_defs.safe_push (SSA_NAME_VERSION (res));
    6734       231789 :               fwprop_set_lattice_val (res, first);
    6735              :             }
    6736              :         }
    6737              : 
    6738              :       /* Apply forward propagation to all stmts in the basic-block.
    6739              :          Note we update GSI within the loop as necessary.  */
    6740     45821249 :       unsigned int uid = 1;
    6741    451272656 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
    6742              :         {
    6743    359630158 :           gimple *stmt = gsi_stmt (gsi);
    6744    359630158 :           tree lhs, rhs;
    6745    359630158 :           enum tree_code code;
    6746              : 
    6747    359630158 :           gimple_set_uid (stmt, uid++);
    6748              : 
    6749    359630158 :           if (!is_gimple_assign (stmt))
    6750              :             {
    6751    251681723 :               process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    6752    251681723 :               gsi_next (&gsi);
    6753    251681723 :               continue;
    6754              :             }
    6755              : 
    6756    107948435 :           lhs = gimple_assign_lhs (stmt);
    6757    107948435 :           rhs = gimple_assign_rhs1 (stmt);
    6758    107948435 :           code = gimple_assign_rhs_code (stmt);
    6759              : 
    6760    147546492 :           if (TREE_CODE (lhs) != SSA_NAME
    6761    107948435 :               || has_zero_uses (lhs))
    6762              :             {
    6763     39598057 :               process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    6764     39598057 :               gsi_next (&gsi);
    6765     39598057 :               continue;
    6766              :             }
    6767              : 
    6768              :           /* If this statement sets an SSA_NAME to an address,
    6769              :              try to propagate the address into the uses of the SSA_NAME.  */
    6770     68350378 :           if ((code == ADDR_EXPR
    6771              :                /* Handle pointer conversions on invariant addresses
    6772              :                   as well, as this is valid gimple.  */
    6773     66008419 :                || (CONVERT_EXPR_CODE_P (code)
    6774      9125748 :                    && TREE_CODE (rhs) == ADDR_EXPR
    6775       362510 :                    && POINTER_TYPE_P (TREE_TYPE (lhs))))
    6776     68350602 :               && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
    6777              :             {
    6778      2341587 :               tree base = get_base_address (TREE_OPERAND (rhs, 0));
    6779      2341587 :               if ((!base
    6780      2341587 :                    || !DECL_P (base)
    6781       134654 :                    || decl_address_invariant_p (base))
    6782      2341587 :                   && !stmt_references_abnormal_ssa_name (stmt)
    6783      4683158 :                   && forward_propagate_addr_expr (lhs, rhs, true))
    6784              :                 {
    6785       479050 :                   fwprop_invalidate_lattice (gimple_get_lhs (stmt));
    6786       479050 :                   release_defs (stmt);
    6787       479050 :                   gsi_remove (&gsi, true);
    6788              :                 }
    6789              :               else
    6790      1862537 :                 gsi_next (&gsi);
    6791              :             }
    6792     66008791 :           else if (code == POINTER_PLUS_EXPR)
    6793              :             {
    6794      3696058 :               tree off = gimple_assign_rhs2 (stmt);
    6795      3696058 :               if (TREE_CODE (off) == INTEGER_CST
    6796      1140853 :                   && can_propagate_from (stmt)
    6797      1140502 :                   && !simple_iv_increment_p (stmt)
    6798              :                   /* ???  Better adjust the interface to that function
    6799              :                      instead of building new trees here.  */
    6800      4538623 :                   && forward_propagate_addr_expr
    6801      2527695 :                        (lhs,
    6802              :                         build1_loc (gimple_location (stmt),
    6803       842565 :                                     ADDR_EXPR, TREE_TYPE (rhs),
    6804       842565 :                                     fold_build2 (MEM_REF,
    6805              :                                                  TREE_TYPE (TREE_TYPE (rhs)),
    6806              :                                                  rhs,
    6807              :                                                  fold_convert (ptr_type_node,
    6808              :                                                                off))), true))
    6809              :                 {
    6810       319848 :                   fwprop_invalidate_lattice (gimple_get_lhs (stmt));
    6811       319848 :                   release_defs (stmt);
    6812       319848 :                   gsi_remove (&gsi, true);
    6813              :                 }
    6814      3376210 :               else if (is_gimple_min_invariant (rhs))
    6815              :                 {
    6816              :                   /* Make sure to fold &a[0] + off_1 here.  */
    6817       418403 :                   fold_stmt_inplace (&gsi);
    6818       418403 :                   update_stmt (stmt);
    6819       418403 :                   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
    6820       418385 :                     gsi_next (&gsi);
    6821              :                 }
    6822              :               else
    6823      2957807 :                 gsi_next (&gsi);
    6824              :             }
    6825     62312733 :           else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
    6826       214375 :                    && gimple_assign_load_p (stmt)
    6827       135139 :                    && !gimple_has_volatile_ops (stmt)
    6828        41064 :                    && TREE_CODE (rhs) != TARGET_MEM_REF
    6829        41034 :                    && TREE_CODE (rhs) != BIT_FIELD_REF
    6830     62353763 :                    && !stmt_can_throw_internal (fun, stmt))
    6831              :             {
    6832              :               /* Rewrite loads used only in real/imagpart extractions to
    6833              :                  component-wise loads.  */
    6834        40912 :               use_operand_p use_p;
    6835        40912 :               imm_use_iterator iter;
    6836        40912 :               tree vuse = gimple_vuse (stmt);
    6837        40912 :               bool rewrite = true;
    6838        46095 :               FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
    6839              :                 {
    6840        43969 :                   gimple *use_stmt = USE_STMT (use_p);
    6841        43969 :                   if (is_gimple_debug (use_stmt))
    6842         1011 :                     continue;
    6843        42958 :                   if (!is_gimple_assign (use_stmt)
    6844        28248 :                       || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
    6845        26147 :                           && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
    6846        47130 :                       || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
    6847              :                     {
    6848              :                       rewrite = false;
    6849              :                       break;
    6850              :                     }
    6851        40912 :                 }
    6852        40912 :               if (rewrite)
    6853              :                 {
    6854         2126 :                   gimple *use_stmt;
    6855         6763 :                   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    6856              :                     {
    6857         4637 :                       if (is_gimple_debug (use_stmt))
    6858              :                         {
    6859          498 :                           if (gimple_debug_bind_p (use_stmt))
    6860              :                             {
    6861          498 :                               gimple_debug_bind_reset_value (use_stmt);
    6862          498 :                               update_stmt (use_stmt);
    6863              :                             }
    6864          498 :                           continue;
    6865              :                         }
    6866              : 
    6867         8278 :                       tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
    6868         4139 :                                              TREE_TYPE (TREE_TYPE (rhs)),
    6869              :                                              unshare_expr (rhs));
    6870         4139 :                       gimple *new_stmt
    6871         4139 :                         = gimple_build_assign (gimple_assign_lhs (use_stmt),
    6872              :                                                new_rhs);
    6873              : 
    6874         4139 :                       location_t loc = gimple_location (use_stmt);
    6875         4139 :                       gimple_set_location (new_stmt, loc);
    6876         4139 :                       gimple_set_vuse (new_stmt, vuse);
    6877         4139 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    6878         4139 :                       unlink_stmt_vdef (use_stmt);
    6879         4139 :                       gsi_remove (&gsi2, true);
    6880              : 
    6881         4139 :                       gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
    6882         2126 :                     }
    6883              : 
    6884         2126 :                   release_defs (stmt);
    6885         2126 :                   gsi_remove (&gsi, true);
    6886              :                 }
    6887              :               else
    6888        38786 :                 gsi_next (&gsi);
    6889              :             }
    6890     62271821 :           else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
    6891      1832933 :                    && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
    6892              :                        /* After vector lowering rewrite all loads, but
    6893              :                           initially do not since this conflicts with
    6894              :                           vector CONSTRUCTOR to shuffle optimization.  */
    6895      1808214 :                        || (fun->curr_properties & PROP_gimple_lvec))
    6896       947037 :                    && gimple_assign_load_p (stmt)
    6897       312752 :                    && !gimple_has_volatile_ops (stmt)
    6898       298620 :                    && !stmt_can_throw_internal (fun, stmt)
    6899     62570441 :                    && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
    6900       298118 :             optimize_vector_load (&gsi);
    6901              : 
    6902     61973703 :           else if (code == COMPLEX_EXPR)
    6903              :             {
    6904              :               /* Rewrite stores of a single-use complex build expression
    6905              :                  to component-wise stores.  */
    6906        37926 :               use_operand_p use_p;
    6907        37926 :               gimple *use_stmt, *def1, *def2;
    6908        37926 :               tree rhs2;
    6909        37926 :               if (single_imm_use (lhs, &use_p, &use_stmt)
    6910        35743 :                   && gimple_store_p (use_stmt)
    6911        42098 :                   && !gimple_has_volatile_ops (use_stmt)
    6912         3133 :                   && is_gimple_assign (use_stmt)
    6913         3129 :                   && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (use_stmt)))
    6914              :                       == COMPLEX_TYPE)
    6915        41050 :                   && (TREE_CODE (gimple_assign_lhs (use_stmt))
    6916              :                       != TARGET_MEM_REF))
    6917              :                 {
    6918         3120 :                   tree use_lhs = gimple_assign_lhs (use_stmt);
    6919         3120 :                   if (auto_var_p (use_lhs))
    6920          601 :                     DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
    6921         6240 :                   tree new_lhs = build1 (REALPART_EXPR,
    6922         3120 :                                          TREE_TYPE (TREE_TYPE (use_lhs)),
    6923              :                                          unshare_expr (use_lhs));
    6924         3120 :                   gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
    6925         3120 :                   location_t loc = gimple_location (use_stmt);
    6926         3120 :                   gimple_set_location (new_stmt, loc);
    6927         6240 :                   gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
    6928         3120 :                   gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
    6929         6240 :                   SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
    6930         6240 :                   gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
    6931         3120 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    6932         3120 :                   gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
    6933              : 
    6934         6240 :                   new_lhs = build1 (IMAGPART_EXPR,
    6935         3120 :                                     TREE_TYPE (TREE_TYPE (use_lhs)),
    6936              :                                     unshare_expr (use_lhs));
    6937         3120 :                   gimple_assign_set_lhs (use_stmt, new_lhs);
    6938         3120 :                   gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
    6939         3120 :                   update_stmt (use_stmt);
    6940              : 
    6941         3120 :                   release_defs (stmt);
    6942         3120 :                   gsi_remove (&gsi, true);
    6943              :                 }
    6944              :               /* Rewrite a component-wise load of a complex to a complex
    6945              :                  load if the components are not used separately.  */
    6946        34806 :               else if (TREE_CODE (rhs) == SSA_NAME
    6947        34365 :                        && has_single_use (rhs)
    6948        30815 :                        && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
    6949        30815 :                        && TREE_CODE (rhs2) == SSA_NAME
    6950        28997 :                        && has_single_use (rhs2)
    6951        28577 :                        && (def1 = SSA_NAME_DEF_STMT (rhs),
    6952        28577 :                            gimple_assign_load_p (def1))
    6953         1093 :                        && (def2 = SSA_NAME_DEF_STMT (rhs2),
    6954         1093 :                            gimple_assign_load_p (def2))
    6955         1598 :                        && (gimple_vuse (def1) == gimple_vuse (def2))
    6956          796 :                        && !gimple_has_volatile_ops (def1)
    6957          796 :                        && !gimple_has_volatile_ops (def2)
    6958          796 :                        && !stmt_can_throw_internal (fun, def1)
    6959          796 :                        && !stmt_can_throw_internal (fun, def2)
    6960          796 :                        && gimple_assign_rhs_code (def1) == REALPART_EXPR
    6961          550 :                        && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
    6962        35356 :                        && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
    6963              :                                                                  (def1), 0),
    6964          550 :                                            TREE_OPERAND (gimple_assign_rhs1
    6965              :                                                                  (def2), 0)))
    6966              :                 {
    6967          550 :                   tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
    6968          550 :                   gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
    6969          550 :                   gcc_assert (gsi_stmt (gsi) == stmt);
    6970         1100 :                   gimple_set_vuse (stmt, gimple_vuse (def1));
    6971          550 :                   gimple_set_modified (stmt, true);
    6972          550 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
    6973          550 :                   gsi_remove (&gsi, false);
    6974          550 :                   gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
    6975              :                 }
    6976              :               else
    6977        34256 :                 gsi_next (&gsi);
    6978              :             }
    6979     61935777 :           else if (code == CONSTRUCTOR
    6980       171501 :                    && VECTOR_TYPE_P (TREE_TYPE (rhs))
    6981       171501 :                    && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
    6982         4576 :                    && CONSTRUCTOR_NELTS (rhs) > 0
    6983     61940353 :                    && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
    6984         2096 :                        || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
    6985              :                            != BLKmode)))
    6986              :             {
    6987              :               /* Rewrite stores of a single-use vector constructors
    6988              :                  to component-wise stores if the mode isn't supported.  */
    6989         4219 :               use_operand_p use_p;
    6990         4219 :               gimple *use_stmt;
    6991         4219 :               if (single_imm_use (lhs, &use_p, &use_stmt)
    6992         3752 :                   && gimple_store_p (use_stmt)
    6993         3076 :                   && !gimple_has_volatile_ops (use_stmt)
    6994         1532 :                   && !stmt_can_throw_internal (fun, use_stmt)
    6995         5744 :                   && is_gimple_assign (use_stmt))
    6996              :                 {
    6997         1525 :                   tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
    6998         1525 :                   unsigned HOST_WIDE_INT elt_w
    6999         1525 :                     = tree_to_uhwi (TYPE_SIZE (elt_t));
    7000         1525 :                   unsigned HOST_WIDE_INT n
    7001         1525 :                     = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
    7002         1525 :                   tree use_lhs = gimple_assign_lhs (use_stmt);
    7003         1525 :                   if (auto_var_p (use_lhs))
    7004          575 :                     DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
    7005          950 :                   else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
    7006              :                     {
    7007            1 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    7008            1 :                       use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
    7009              :                     }
    7010        33572 :                   for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
    7011              :                     {
    7012        32047 :                       unsigned HOST_WIDE_INT ci = bi / elt_w;
    7013        32047 :                       tree new_rhs;
    7014        32047 :                       if (ci < CONSTRUCTOR_NELTS (rhs))
    7015        31429 :                         new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
    7016              :                       else
    7017          618 :                         new_rhs = build_zero_cst (elt_t);
    7018        32047 :                       tree new_lhs = build3 (BIT_FIELD_REF,
    7019              :                                              elt_t,
    7020              :                                              unshare_expr (use_lhs),
    7021        32047 :                                              bitsize_int (elt_w),
    7022        32047 :                                              bitsize_int (bi));
    7023        32047 :                       gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
    7024        32047 :                       location_t loc = gimple_location (use_stmt);
    7025        32047 :                       gimple_set_location (new_stmt, loc);
    7026        64094 :                       gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
    7027        32047 :                       gimple_set_vdef (new_stmt,
    7028              :                                        make_ssa_name (gimple_vop (fun)));
    7029        64094 :                       SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
    7030        64094 :                       gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
    7031        32047 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    7032        32047 :                       gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
    7033              :                     }
    7034         1525 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    7035         1525 :                   unlink_stmt_vdef (use_stmt);
    7036         1525 :                   release_defs (use_stmt);
    7037         1525 :                   gsi_remove (&gsi2, true);
    7038         1525 :                   release_defs (stmt);
    7039         1525 :                   gsi_remove (&gsi, true);
    7040              :                 }
    7041              :               else
    7042         2694 :                 gsi_next (&gsi);
    7043              :             }
    7044     61931558 :           else if (code == VEC_PERM_EXPR)
    7045              :             {
    7046              :               /* Find vectorized sequences, where we can reduce the lane
    7047              :                  utilization.  The narrowing will be donw later and only
    7048              :                  if we find a pair of sequences that can be blended.  */
    7049       190176 :               gassign *assign = dyn_cast <gassign *> (stmt);
    7050       190176 :               vec_perm_simplify_seq seq;
    7051       190176 :               if (recognise_vec_perm_simplify_seq (assign, &seq))
    7052          101 :                 append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
    7053              :                                                    seq);
    7054              : 
    7055       190176 :               gsi_next (&gsi);
    7056              :           }
    7057              :           else
    7058     61741382 :             gsi_next (&gsi);
    7059              :         }
    7060              : 
    7061     45821249 :       process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    7062              : 
    7063              :       /* Combine stmts with the stmts defining their operands.
    7064              :          Note we update GSI within the loop as necessary.  */
    7065    450943245 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    7066              :         {
    7067    359300747 :           gimple *stmt = gsi_stmt (gsi);
    7068              : 
    7069              :           /* Mark stmt as potentially needing revisiting.  */
    7070    359300747 :           gimple_set_plf (stmt, GF_PLF_1, false);
    7071              : 
    7072    359300747 :           bool can_make_abnormal_goto = (is_gimple_call (stmt)
    7073    359300747 :                                          && stmt_can_make_abnormal_goto (stmt));
    7074              : 
    7075              :           /* Substitute from our lattice.  We need to do so only once.  */
    7076    359300747 :           bool substituted_p = false;
    7077    359300747 :           use_operand_p usep;
    7078    359300747 :           ssa_op_iter iter;
    7079    526091224 :           FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
    7080              :             {
    7081    166790477 :               tree use = USE_FROM_PTR (usep);
    7082    166790477 :               tree val = fwprop_ssa_val (use);
    7083    166790477 :               if (val && val != use)
    7084              :                 {
    7085      1940488 :                   if (!is_gimple_debug (stmt))
    7086      1607493 :                     bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
    7087      1940488 :                   if (may_propagate_copy (use, val))
    7088              :                     {
    7089      1937241 :                       propagate_value (usep, val);
    7090      1937241 :                       substituted_p = true;
    7091              :                     }
    7092              :                 }
    7093              :             }
    7094    359300747 :           if (substituted_p)
    7095      1881994 :             update_stmt (stmt);
    7096      1881994 :           if (substituted_p
    7097      1881994 :               && is_gimple_assign (stmt)
    7098      1133208 :               && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
    7099        20403 :             recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
    7100    359300747 :           if (substituted_p
    7101    359300747 :               && can_make_abnormal_goto
    7102    359300747 :               && !stmt_can_make_abnormal_goto (stmt))
    7103            3 :             bitmap_set_bit (need_ab_cleanup, bb->index);
    7104              : 
    7105    362225816 :           bool changed;
    7106    724451632 :           do
    7107              :             {
    7108    362225816 :               gimple *orig_stmt = stmt = gsi_stmt (gsi);
    7109    362225816 :               bool was_call = is_gimple_call (stmt);
    7110    362225816 :               bool was_noreturn = (was_call
    7111    362225816 :                                    && gimple_call_noreturn_p (stmt));
    7112    362225816 :               changed = false;
    7113              : 
    7114    362225816 :               auto_vec<tree, 8> uses;
    7115    532055299 :               FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
    7116    339267881 :                 if (uses.space (1))
    7117    169438398 :                   uses.quick_push (USE_FROM_PTR (usep));
    7118              : 
    7119    362225816 :               if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
    7120              :                 {
    7121      2609473 :                   changed = true;
    7122              :                   /* There is no updating of the address
    7123              :                      taken after the last forwprop so update
    7124              :                      the addresses when a folding happened to a call.
    7125              :                      The va_* builtins can remove taking of the address so
    7126              :                      can the sincos->cexpi transformation.  See PR 39643 and PR 20983. */
    7127      2609473 :                   if (was_call && last_p)
    7128      2609473 :                     todoflags |= TODO_update_address_taken;
    7129      2609473 :                   stmt = gsi_stmt (gsi);
    7130              :                   /* Cleanup the CFG if we simplified a condition to
    7131              :                      true or false.  */
    7132      2609473 :                   if (gcond *cond = dyn_cast <gcond *> (stmt))
    7133      1050348 :                     if (gimple_cond_true_p (cond)
    7134      1050348 :                         || gimple_cond_false_p (cond))
    7135        31528 :                       cfg_changed = true;
    7136              :                   /* Queue old uses for simple DCE if not debug statement.  */
    7137      2609473 :                   if (!is_gimple_debug (stmt))
    7138     10999120 :                     for (tree use : uses)
    7139      3192268 :                       if (TREE_CODE (use) == SSA_NAME
    7140      3192268 :                           && !SSA_NAME_IS_DEFAULT_DEF (use))
    7141      2993452 :                         bitmap_set_bit (simple_dce_worklist,
    7142      2993452 :                                         SSA_NAME_VERSION (use));
    7143      2609473 :                   update_stmt (stmt);
    7144              :                 }
    7145              : 
    7146    362225816 :               switch (gimple_code (stmt))
    7147              :                 {
    7148    109013703 :                 case GIMPLE_ASSIGN:
    7149    109013703 :                   {
    7150    109013703 :                     tree rhs1 = gimple_assign_rhs1 (stmt);
    7151    109013703 :                     enum tree_code code = gimple_assign_rhs_code (stmt);
    7152    109013703 :                     if (gimple_clobber_p (stmt))
    7153      7476333 :                       do_simple_agr_dse (as_a<gassign*>(stmt), full_walk);
    7154    101537370 :                     else if (gimple_store_p (stmt))
    7155              :                       {
    7156     31491097 :                         optimize_aggr_zeroprop (stmt, full_walk);
    7157     31491097 :                         if (gimple_assign_load_p (stmt))
    7158      3905669 :                           optimize_agr_copyprop (stmt);
    7159              :                       }
    7160     70046273 :                     else if (TREE_CODE_CLASS (code) == tcc_comparison)
    7161      2658236 :                       changed |= forward_propagate_into_comparison (&gsi);
    7162     67388037 :                     else if ((code == PLUS_EXPR || code == BIT_IOR_EXPR))
    7163              :                       {
    7164     10194410 :                         bool folded = match_long_mul (as_a <gassign *> (stmt));
    7165     10194410 :                         if (!folded)
    7166     10192473 :                           folded = simplify_rotate (&gsi);
    7167     10194410 :                         changed |= folded;
    7168              :                       }
    7169              :                     else if (code == BIT_XOR_EXPR)
    7170       143143 :                       changed |= simplify_rotate (&gsi);
    7171              :                     else if (code == VEC_PERM_EXPR)
    7172       192510 :                       changed |= simplify_permutation (&gsi);
    7173              :                     else if (code == CONSTRUCTOR
    7174       169380 :                              && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
    7175       169380 :                       changed |= simplify_vector_constructor (&gsi);
    7176     56688594 :                     else if (code == ARRAY_REF)
    7177      2003925 :                       changed |= simplify_count_zeroes (&gsi);
    7178     54684669 :                     else if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
    7179     12624925 :                              && TREE_CODE (
    7180              :                                     gimple_assign_rhs2 (stmt)) == SSA_NAME
    7181      6714492 :                              && has_single_use (gimple_assign_rhs2 (stmt))
    7182     58806151 :                              && is_a<gphi*> (SSA_NAME_DEF_STMT (
    7183              :                                                 gimple_assign_rhs2 (stmt))))
    7184       133066 :                       changed |= simplify_phi_result_movdiv (stmt, code);
    7185              :                     break;
    7186              :                   }
    7187              : 
    7188       108800 :                 case GIMPLE_SWITCH:
    7189       108800 :                   changed |= simplify_gimple_switch (as_a <gswitch *> (stmt),
    7190              :                                                      edges_to_remove,
    7191              :                                                      simple_dce_worklist);
    7192       108800 :                   break;
    7193              : 
    7194     19805301 :                 case GIMPLE_COND:
    7195     19805301 :                   {
    7196     19805301 :                     int did_something = forward_propagate_into_gimple_cond
    7197     19805301 :                                                         (as_a <gcond *> (stmt));
    7198     19805301 :                     if (did_something == 2)
    7199         1629 :                       cfg_changed = true;
    7200     19805301 :                     changed |= did_something != 0;
    7201     19805301 :                     break;
    7202              :                   }
    7203              : 
    7204     23871233 :                 case GIMPLE_CALL:
    7205     23871233 :                   {
    7206     23871233 :                     tree callee = gimple_call_fndecl (stmt);
    7207     23871233 :                     if (callee != NULL_TREE
    7208     23871233 :                         && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
    7209      6311643 :                       changed |= simplify_builtin_call (&gsi, callee, full_walk);
    7210              :                     break;
    7211              :                   }
    7212              : 
    7213    362225816 :                 default:;
    7214              :                 }
    7215              : 
    7216    362225816 :               if (changed || substituted_p)
    7217              :                 {
    7218      4238858 :                   substituted_p = false;
    7219      4238858 :                   stmt = gsi_stmt (gsi);
    7220      4238858 :                   if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
    7221           70 :                     bitmap_set_bit (to_purge, bb->index);
    7222      4238858 :                   if (!was_noreturn
    7223      4238858 :                       && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
    7224           12 :                     to_fixup.safe_push (stmt);
    7225              :                 }
    7226      4238858 :               if (changed)
    7227              :                 {
    7228              :                   /* If the stmt changed then re-visit it and the statements
    7229              :                      inserted before it.  */
    7230      6298382 :                   for (; !gsi_end_p (gsi); gsi_prev (&gsi))
    7231      5836032 :                     if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
    7232              :                       break;
    7233      2925069 :                   if (gsi_end_p (gsi))
    7234       462350 :                     gsi = gsi_start_bb (bb);
    7235              :                   else
    7236      2693894 :                     gsi_next (&gsi);
    7237              :                 }
    7238    362225816 :             }
    7239              :           while (changed);
    7240              : 
    7241              :           /* Stmt no longer needs to be revisited.  */
    7242    359300747 :           stmt = gsi_stmt (gsi);
    7243    359300747 :           gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
    7244    359300747 :           gimple_set_plf (stmt, GF_PLF_1, true);
    7245              : 
    7246              :           /* Fill up the lattice.  */
    7247    359300747 :           if (gimple_assign_single_p (stmt))
    7248              :             {
    7249     72173063 :               tree lhs = gimple_assign_lhs (stmt);
    7250     72173063 :               tree rhs = gimple_assign_rhs1 (stmt);
    7251     72173063 :               if (TREE_CODE (lhs) == SSA_NAME)
    7252              :                 {
    7253     33218155 :                   tree val = lhs;
    7254     33218155 :                   if (TREE_CODE (rhs) == SSA_NAME)
    7255       783932 :                     val = fwprop_ssa_val (rhs);
    7256     32434223 :                   else if (is_gimple_min_invariant (rhs))
    7257       469030 :                     val = rhs;
    7258              :                   /* If we can propagate the lattice-value mark the
    7259              :                      stmt for removal.  */
    7260     33218155 :                   if (val != lhs
    7261     33218155 :                       && may_propagate_copy (lhs, val))
    7262      1249556 :                     to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
    7263     33218155 :                   fwprop_set_lattice_val (lhs, val);
    7264              :                 }
    7265              :             }
    7266    287127684 :           else if (gimple_nop_p (stmt))
    7267       103004 :             to_remove.safe_push (stmt);
    7268              :         }
    7269              : 
    7270              :       /* Substitute in destination PHI arguments.  */
    7271    110300685 :       FOR_EACH_EDGE (e, ei, bb->succs)
    7272     64479436 :         for (gphi_iterator gsi = gsi_start_phis (e->dest);
    7273    107305150 :              !gsi_end_p (gsi); gsi_next (&gsi))
    7274              :           {
    7275     42825714 :             gphi *phi = gsi.phi ();
    7276     42825714 :             use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
    7277     42825714 :             tree arg = USE_FROM_PTR (use_p);
    7278     70511455 :             if (TREE_CODE (arg) != SSA_NAME
    7279     42825714 :                 || virtual_operand_p (arg))
    7280     27685741 :               continue;
    7281     15139973 :             tree val = fwprop_ssa_val (arg);
    7282     15139973 :             if (val != arg
    7283     15139973 :                 && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
    7284       257151 :               propagate_value (use_p, val);
    7285              :           }
    7286              : 
    7287              :       /* Mark outgoing executable edges.  */
    7288     45821249 :       if (edge e = find_taken_edge (bb, NULL))
    7289              :         {
    7290     19490167 :           e->flags |= EDGE_EXECUTABLE;
    7291     45858715 :           if (EDGE_COUNT (bb->succs) > 1)
    7292        37466 :             cfg_changed = true;
    7293              :         }
    7294              :       else
    7295              :         {
    7296     71282884 :           FOR_EACH_EDGE (e, ei, bb->succs)
    7297     44951802 :             e->flags |= EDGE_EXECUTABLE;
    7298              :         }
    7299              :     }
    7300      5731900 :   free (postorder);
    7301      5731900 :   free (bb_to_rpo);
    7302      5731900 :   lattice.release ();
    7303              : 
    7304              :   /* First remove chains of stmts where we check no uses remain.  */
    7305      5731900 :   simple_dce_from_worklist (simple_dce_worklist, to_purge);
    7306              : 
    7307      6096631 :   auto remove = [](gimple *stmt)
    7308              :     {
    7309       364731 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7310              :         {
    7311            1 :           fprintf (dump_file, "Removing dead stmt ");
    7312            1 :           print_gimple_stmt (dump_file, stmt, 0);
    7313            1 :           fprintf (dump_file, "\n");
    7314              :         }
    7315       364731 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    7316       364731 :       if (gimple_code (stmt) == GIMPLE_PHI)
    7317        97235 :         remove_phi_node (&gsi, true);
    7318              :       else
    7319              :         {
    7320       267496 :           unlink_stmt_vdef (stmt);
    7321       267496 :           gsi_remove (&gsi, true);
    7322       267496 :           release_defs (stmt);
    7323              :         }
    7324       364731 :     };
    7325              : 
    7326              :   /* Then remove stmts we know we can remove even though we did not
    7327              :      substitute in dead code regions, so uses can remain.  Do so in reverse
    7328              :      order to make debug stmt creation possible.  */
    7329     12944487 :   while (!to_remove_defs.is_empty())
    7330              :     {
    7331      1480687 :       tree def = ssa_name (to_remove_defs.pop ());
    7332              :       /* For example remove_prop_source_from_use can remove stmts queued
    7333              :          for removal.  Deal with this gracefully.  */
    7334      1480687 :       if (!def)
    7335      1218960 :         continue;
    7336       261727 :       gimple *stmt = SSA_NAME_DEF_STMT (def);
    7337       261727 :       remove (stmt);
    7338              :     }
    7339              : 
    7340              :   /* Wipe other queued stmts that do not have SSA defs.  */
    7341      5834904 :   while (!to_remove.is_empty())
    7342              :     {
    7343       103004 :       gimple *stmt = to_remove.pop ();
    7344       103004 :       remove (stmt);
    7345              :     }
    7346              : 
    7347              :   /* Fixup stmts that became noreturn calls.  This may require splitting
    7348              :      blocks and thus isn't possible during the walk.  Do this
    7349              :      in reverse order so we don't inadvertently remove a stmt we want to
    7350              :      fixup by visiting a dominating now noreturn call first.  */
    7351      5731912 :   while (!to_fixup.is_empty ())
    7352              :     {
    7353           12 :       gimple *stmt = to_fixup.pop ();
    7354           12 :       if (dump_file && dump_flags & TDF_DETAILS)
    7355              :         {
    7356            0 :           fprintf (dump_file, "Fixing up noreturn call ");
    7357            0 :           print_gimple_stmt (dump_file, stmt, 0);
    7358            0 :           fprintf (dump_file, "\n");
    7359              :         }
    7360           12 :       cfg_changed |= fixup_noreturn_call (stmt);
    7361              :     }
    7362              : 
    7363      5731900 :   cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
    7364      5731900 :   cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
    7365      5731900 :   BITMAP_FREE (to_purge);
    7366              : 
    7367              :   /* Remove edges queued from switch stmt simplification.  */
    7368     17195700 :   for (auto ep : edges_to_remove)
    7369              :     {
    7370            0 :       basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
    7371            0 :       basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
    7372            0 :       edge e;
    7373            0 :       if (src && dest && (e = find_edge (src, dest)))
    7374              :         {
    7375            0 :           free_dominance_info (CDI_DOMINATORS);
    7376            0 :           remove_edge (e);
    7377            0 :           cfg_changed = true;
    7378              :         }
    7379              :     }
    7380              : 
    7381      5731903 :   if (get_range_query (fun) != get_global_range_query ())
    7382         1543 :     disable_ranger (fun);
    7383              : 
    7384      5731900 :   if (cfg_changed)
    7385        13468 :     todoflags |= TODO_cleanup_cfg;
    7386              : 
    7387      5731900 :   return todoflags;
    7388      5731900 : }
    7389              : 
    7390              : } // anon namespace
    7391              : 
    7392              : gimple_opt_pass *
    7393       294587 : make_pass_forwprop (gcc::context *ctxt)
    7394              : {
    7395       294587 :   return new pass_forwprop (ctxt);
    7396              : }
        

Generated by: LCOV version 2.4-beta

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