LCOV - code coverage report
Current view: top level - gcc - tree-ssa-forwprop.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.5 % 3528 3371
Test Date: 2026-08-22 16:33:35 Functions: 100.0 % 92 92
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     33387032 : fwprop_set_lattice_val (tree name, tree val)
     223              : {
     224     33387032 :   if (TREE_CODE (name) == SSA_NAME)
     225              :     {
     226     33387032 :       if (SSA_NAME_VERSION (name) >= lattice.length ())
     227              :         {
     228        32226 :           lattice.reserve (num_ssa_names - lattice.length ());
     229        21484 :           lattice.quick_grow_cleared (num_ssa_names);
     230              :         }
     231     33387032 :       lattice[SSA_NAME_VERSION (name)] = val;
     232              :       /* As this now constitutes a copy duplicate points-to
     233              :          and range info appropriately.  */
     234     33387032 :       if (TREE_CODE (val) == SSA_NAME)
     235     32930388 :         maybe_duplicate_ssa_info_at_copy (name, val);
     236              :     }
     237     33387032 : }
     238              : 
     239              : /* Invalidate the lattice entry for NAME, done when releasing SSA names.  */
     240              : static void
     241       942750 : fwprop_invalidate_lattice (tree name)
     242              : {
     243       942750 :   if (name
     244       940246 :       && TREE_CODE (name) == SSA_NAME
     245      1882868 :       && SSA_NAME_VERSION (name) < lattice.length ())
     246       940085 :     lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
     247       942750 : }
     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     28530225 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
     259              : {
     260     28530225 :   bool single_use = true;
     261              : 
     262     28531211 :   do {
     263     28530718 :     gimple *def_stmt = SSA_NAME_DEF_STMT (name);
     264              : 
     265     28530718 :     if (!has_single_use (name))
     266              :       {
     267     15586106 :         single_use = false;
     268     15586106 :         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     28529224 :     if (!is_gimple_assign (def_stmt))
     274              :       return NULL;
     275              : 
     276              :     /* If def_stmt is a simple copy, continue looking.  */
     277     20107180 :     if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
     278          493 :       name = gimple_assign_rhs1 (def_stmt);
     279              :     else
     280              :       {
     281     20106687 :         if (!single_use_only && single_use_p)
     282     19779704 :           *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     28196952 : can_propagate_from (gimple *def_stmt)
     294              : {
     295     28196952 :   gcc_assert (is_gimple_assign (def_stmt));
     296              : 
     297              :   /* If the rhs has side-effects we cannot propagate from it.  */
     298     28196952 :   if (gimple_has_volatile_ops (def_stmt))
     299              :     return false;
     300              : 
     301              :   /* If the rhs is a load we cannot propagate from it.  */
     302     27604372 :   if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
     303     27604372 :       || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
     304              :     return false;
     305              : 
     306              :   /* Constants can be always propagated.  */
     307     13606846 :   if (gimple_assign_single_p (def_stmt)
     308     13606846 :       && 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     13606846 :   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     13606150 :   if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
     320              :     {
     321      3308950 :       tree rhs = gimple_assign_rhs1 (def_stmt);
     322      3308950 :       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       243766 : remove_prop_source_from_use (tree name)
     337              : {
     338       304799 :   gimple_stmt_iterator gsi;
     339       304799 :   gimple *stmt;
     340              : 
     341       304799 :   do {
     342       304799 :     basic_block bb;
     343              : 
     344       304799 :     if (SSA_NAME_IN_FREE_LIST (name)
     345       304756 :         || SSA_NAME_IS_DEFAULT_DEF (name)
     346       608105 :         || !has_zero_uses (name))
     347              :       break;
     348              : 
     349        61491 :     stmt = SSA_NAME_DEF_STMT (name);
     350        61491 :     if (gimple_code (stmt) == GIMPLE_PHI
     351        61491 :         || gimple_has_side_effects (stmt))
     352              :       break;
     353              : 
     354        61491 :     bb = gimple_bb (stmt);
     355        61491 :     gsi = gsi_for_stmt (stmt);
     356        61491 :     unlink_stmt_vdef (stmt);
     357        61491 :     if (gsi_remove (&gsi, true))
     358            6 :       bitmap_set_bit (to_purge, bb->index);
     359        61491 :     fwprop_invalidate_lattice (gimple_get_lhs (stmt));
     360        61491 :     release_defs (stmt);
     361              : 
     362        61491 :     name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
     363        61491 :   } while (name && TREE_CODE (name) == SSA_NAME);
     364              : 
     365       243766 : }
     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      7450650 : rhs_to_tree (tree type, gimple *stmt)
     376              : {
     377      7450650 :   location_t loc = gimple_location (stmt);
     378      7450650 :   enum tree_code code = gimple_assign_rhs_code (stmt);
     379      7450650 :   switch (get_gimple_rhs_class (code))
     380              :     {
     381        13245 :     case GIMPLE_TERNARY_RHS:
     382        13245 :       return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
     383              :                               gimple_assign_rhs2 (stmt),
     384        13245 :                               gimple_assign_rhs3 (stmt));
     385      5069587 :     case GIMPLE_BINARY_RHS:
     386      5069587 :       return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
     387      5069587 :                               gimple_assign_rhs2 (stmt));
     388      2082814 :     case GIMPLE_UNARY_RHS:
     389      2082814 :       return build1 (code, type, gimple_assign_rhs1 (stmt));
     390       285004 :     case GIMPLE_SINGLE_RHS:
     391       285004 :       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      8381213 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
     405              :                         tree op0, tree op1, bool invariant_only)
     406              : {
     407      8381213 :   tree t;
     408              : 
     409      8381213 :   gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
     410              : 
     411      8381213 :   t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
     412      8381213 :   if (!t)
     413              :     return NULL_TREE;
     414              : 
     415              :   /* Require that we got a boolean type out if we put one in.  */
     416      3639954 :   gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
     417              : 
     418              :   /* Canonicalize the combined condition for use in a COND_EXPR.  */
     419      3639954 :   t = canonicalize_cond_expr_cond (t);
     420              : 
     421              :   /* Bail out if we required an invariant but didn't get one.  */
     422      3639954 :   if (!t || (invariant_only && !is_gimple_min_invariant (t)))
     423      3398642 :     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     22452322 : forward_propagate_into_comparison_1 (gimple *stmt,
     434              :                                      enum tree_code code, tree type,
     435              :                                      tree op0, tree op1)
     436              : {
     437     22452322 :   tree tmp = NULL_TREE;
     438     22452322 :   tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
     439     22452322 :   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     22452322 :   if (TREE_CODE (op0) == SSA_NAME)
     444              :     {
     445     22415074 :       gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
     446     22415074 :       if (def_stmt && can_propagate_from (def_stmt))
     447              :         {
     448      5659760 :           enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
     449      5659760 :           bool invariant_only_p = !single_use0_p;
     450              : 
     451      5659760 :           rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
     452              : 
     453              :           /* Always combine comparisons or conversions from booleans.  */
     454      5659760 :           if (TREE_CODE (op1) == INTEGER_CST
     455      5659760 :               && ((CONVERT_EXPR_CODE_P (def_code)
     456       910655 :                    && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
     457              :                       == BOOLEAN_TYPE)
     458      3626054 :                   || TREE_CODE_CLASS (def_code) == tcc_comparison))
     459              :             invariant_only_p = false;
     460              : 
     461      5659760 :           tmp = combine_cond_expr_cond (stmt, code, type,
     462              :                                         rhs0, op1, invariant_only_p);
     463      5659760 :           if (tmp)
     464              :             return tmp;
     465              :         }
     466              :     }
     467              : 
     468              :   /* If that wasn't successful, try the second operand.  */
     469     22219505 :   if (TREE_CODE (op1) == SSA_NAME)
     470              :     {
     471      5519109 :       gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
     472      5519109 :       if (def_stmt && can_propagate_from (def_stmt))
     473              :         {
     474      1790890 :           rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
     475      1790890 :           tmp = combine_cond_expr_cond (stmt, code, type,
     476              :                                         op0, rhs1, !single_use1_p);
     477      1790890 :           if (tmp)
     478              :             return tmp;
     479              :         }
     480              :     }
     481              : 
     482              :   /* If that wasn't successful either, try both operands.  */
     483     22212932 :   if (rhs0 != NULL_TREE
     484     22212932 :       && rhs1 != NULL_TREE)
     485       930563 :     tmp = combine_cond_expr_cond (stmt, code, type,
     486              :                                   rhs0, rhs1,
     487       930563 :                                   !(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      2679467 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
     498              : {
     499      2679467 :   gimple *stmt = gsi_stmt (*gsi);
     500      2679467 :   tree tmp;
     501      2679467 :   tree type = TREE_TYPE (gimple_assign_lhs (stmt));
     502      2679467 :   tree rhs1 = gimple_assign_rhs1 (stmt);
     503      2679467 :   tree rhs2 = gimple_assign_rhs2 (stmt);
     504              : 
     505              :   /* Combine the comparison with defining statements.  */
     506      2679467 :   tmp = forward_propagate_into_comparison_1 (stmt,
     507              :                                              gimple_assign_rhs_code (stmt),
     508              :                                              type, rhs1, rhs2);
     509      2679467 :   if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
     510              :     {
     511         7322 :       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         7322 :       gimple_assign_set_rhs_from_tree (gsi, tmp);
     520         7322 :       fold_stmt (gsi);
     521         7322 :       update_stmt (gsi_stmt (*gsi));
     522              : 
     523         7322 :       if (TREE_CODE (rhs1) == SSA_NAME)
     524         7322 :         remove_prop_source_from_use (rhs1);
     525         7322 :       if (TREE_CODE (rhs2) == SSA_NAME)
     526         3082 :         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     19772855 : forward_propagate_into_gimple_cond (gcond *stmt)
     540              : {
     541     19772855 :   tree tmp;
     542     19772855 :   enum tree_code code = gimple_cond_code (stmt);
     543     19772855 :   tree rhs1 = gimple_cond_lhs (stmt);
     544     19772855 :   tree rhs2 = gimple_cond_rhs (stmt);
     545              : 
     546              :   /* GIMPLE_COND will always be a comparison.  */
     547     19772855 :   gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison);
     548              : 
     549     19772855 :   tmp = forward_propagate_into_comparison_1 (stmt, code,
     550              :                                              boolean_type_node,
     551              :                                              rhs1, rhs2);
     552     19772855 :   if (tmp
     553     19772855 :       && is_gimple_condexpr_for_cond (tmp))
     554              :     {
     555       227659 :       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       227659 :       gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
     565       227659 :       update_stmt (stmt);
     566              : 
     567       227659 :       if (TREE_CODE (rhs1) == SSA_NAME)
     568       227659 :         remove_prop_source_from_use (rhs1);
     569       227659 :       if (TREE_CODE (rhs2) == SSA_NAME)
     570         5702 :         remove_prop_source_from_use (rhs2);
     571       227659 :       return is_gimple_min_invariant (tmp) ? 2 : 1;
     572              :     }
     573              : 
     574     19545196 :   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      2025293 : tidy_after_forward_propagate_addr (gimple *stmt)
     585              : {
     586              :   /* We may have turned a trapping insn into a non-trapping insn.  */
     587      2025293 :   if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
     588          131 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
     589              : 
     590      2025293 :   if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
     591       255128 :      recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
     592      2025293 : }
     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      2877820 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
     606              :                                gimple_stmt_iterator *use_stmt_gsi,
     607              :                                bool single_use_p)
     608              : {
     609      2877820 :   tree lhs, rhs, rhs2, array_ref;
     610      2877820 :   gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
     611      2877820 :   enum tree_code rhs_code;
     612      2877820 :   bool res = true;
     613              : 
     614      2877820 :   gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
     615              : 
     616      2877820 :   lhs = gimple_assign_lhs (use_stmt);
     617      2877820 :   rhs_code = gimple_assign_rhs_code (use_stmt);
     618      2877820 :   rhs = gimple_assign_rhs1 (use_stmt);
     619              : 
     620              :   /* Do not perform copy-propagation but recurse through copy chains.  */
     621      2877820 :   if (TREE_CODE (lhs) == SSA_NAME
     622      1431189 :       && 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      2869135 :   if (TREE_CODE (lhs) == SSA_NAME
     631      1422504 :       && 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        24772 :       if (single_use_p
     637        24772 :           && 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        49542 :       if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
     646            2 :            || POINTER_TYPE_P (TREE_TYPE (lhs)))
     647        49542 :           && (TYPE_PRECISION (TREE_TYPE (lhs))
     648        24771 :               >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
     649        24704 :         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      2844363 :   if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
     657              :     return false;
     658              : 
     659              :   /* Propagate through constant pointer adjustments.  */
     660      2823147 :   if (TREE_CODE (lhs) == SSA_NAME
     661      1378206 :       && rhs_code == POINTER_PLUS_EXPR
     662      1378206 :       && rhs == name
     663      2990791 :       && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
     664              :     {
     665       119204 :       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       119204 :       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       119204 :       if (TREE_CODE (new_def_rhs) == MEM_REF
     675       119204 :           && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
     676              :         return false;
     677       115201 :       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       115201 :       if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
     682              :         return true;
     683              : 
     684        38864 :       if (useless_type_conversion_p (TREE_TYPE (lhs),
     685        38864 :                                      TREE_TYPE (new_def_rhs)))
     686        38864 :         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        38864 :       gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
     693        38864 :       update_stmt (use_stmt);
     694        38864 :       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      2703943 :   tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
     700      4091328 :   while (handled_component_p (*lhsp))
     701      1387385 :     lhsp = &TREE_OPERAND (*lhsp, 0);
     702      2703943 :   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      2703943 :   if (TREE_CODE (lhs) == MEM_REF
     707      2703943 :       && TREE_OPERAND (lhs, 0) == name)
     708              :     {
     709       912203 :       tree def_rhs_base;
     710       912203 :       poly_int64 def_rhs_offset;
     711              :       /* If the address is invariant we can always fold it.  */
     712       912203 :       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
     713              :                                                          &def_rhs_offset)))
     714              :         {
     715       865038 :           poly_offset_int off = mem_ref_offset (lhs);
     716       865038 :           tree new_ptr;
     717       865038 :           off += def_rhs_offset;
     718       865038 :           if (TREE_CODE (def_rhs_base) == MEM_REF)
     719              :             {
     720       843623 :               off += mem_ref_offset (def_rhs_base);
     721       843623 :               new_ptr = TREE_OPERAND (def_rhs_base, 0);
     722              :             }
     723              :           else
     724        21415 :             new_ptr = build_fold_addr_expr (def_rhs_base);
     725       865038 :           TREE_OPERAND (lhs, 0) = new_ptr;
     726       865038 :           TREE_OPERAND (lhs, 1)
     727       865038 :             = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
     728       865038 :           tidy_after_forward_propagate_addr (use_stmt);
     729              :           /* Continue propagating into the RHS if this was not the only use.  */
     730       865038 :           if (single_use_p)
     731       231763 :             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        47165 :       else if (integer_zerop (TREE_OPERAND (lhs, 1))
     737        18559 :                && ((gimple_assign_lhs (use_stmt) == lhs
     738        14940 :                     && useless_type_conversion_p
     739        14940 :                          (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
     740        14940 :                           TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
     741        13629 :                    || types_compatible_p (TREE_TYPE (lhs),
     742        13629 :                                           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        55351 :                && (!gimple_clobber_p (use_stmt)
     746          164 :                    || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
     747              :         {
     748         8022 :           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
     749         8022 :           tree new_offset, new_base, saved, new_lhs;
     750        29113 :           while (handled_component_p (*def_rhs_basep))
     751        13069 :             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
     752         8022 :           saved = *def_rhs_basep;
     753         8022 :           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         4081 :               new_base = build_fold_addr_expr (*def_rhs_basep);
     762         4081 :               new_offset = TREE_OPERAND (lhs, 1);
     763              :             }
     764         8022 :           tree atype = TREE_TYPE (*def_rhs_basep);
     765         8022 :           if (TYPE_ALIGN (TREE_TYPE (lhs)) < TYPE_ALIGN (atype))
     766          312 :             atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (lhs)));
     767         8022 :           *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
     768         8022 :           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
     769         8022 :           TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
     770         8022 :           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
     771         8022 :           new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
     772         8022 :           *lhsp = new_lhs;
     773         8022 :           TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
     774         8022 :           TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
     775         8022 :           *def_rhs_basep = saved;
     776         8022 :           tidy_after_forward_propagate_addr (use_stmt);
     777              :           /* Continue propagating into the RHS if this was not the
     778              :              only use.  */
     779         8022 :           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      2467973 :   tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
     792      2467973 :   if (TREE_CODE (*rhsp) == ADDR_EXPR)
     793       243104 :     rhsp = &TREE_OPERAND (*rhsp, 0);
     794      3503533 :   while (handled_component_p (*rhsp))
     795      1035560 :     rhsp = &TREE_OPERAND (*rhsp, 0);
     796      2467973 :   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      2467973 :   if (TREE_CODE (rhs) == MEM_REF
     801      2467973 :       && TREE_OPERAND (rhs, 0) == name)
     802              :     {
     803      1174056 :       tree def_rhs_base;
     804      1174056 :       poly_int64 def_rhs_offset;
     805      1174056 :       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
     806              :                                                          &def_rhs_offset)))
     807              :         {
     808      1136947 :           poly_offset_int off = mem_ref_offset (rhs);
     809      1136947 :           tree new_ptr;
     810      1136947 :           off += def_rhs_offset;
     811      1136947 :           if (TREE_CODE (def_rhs_base) == MEM_REF)
     812              :             {
     813      1110246 :               off += mem_ref_offset (def_rhs_base);
     814      1110246 :               new_ptr = TREE_OPERAND (def_rhs_base, 0);
     815              :             }
     816              :           else
     817        26701 :             new_ptr = build_fold_addr_expr (def_rhs_base);
     818      1136947 :           TREE_OPERAND (rhs, 0) = new_ptr;
     819      1136947 :           TREE_OPERAND (rhs, 1)
     820      1136947 :             = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
     821      1136947 :           fold_stmt_inplace (use_stmt_gsi);
     822      1136947 :           tidy_after_forward_propagate_addr (use_stmt);
     823      1136947 :           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        37109 :       else if (integer_zerop (TREE_OPERAND (rhs, 1))
     829        37109 :                && ((gimple_assign_rhs1 (use_stmt) == rhs
     830        20326 :                     && useless_type_conversion_p
     831        20326 :                          (TREE_TYPE (gimple_assign_lhs (use_stmt)),
     832        20326 :                           TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
     833        22581 :                    || types_compatible_p (TREE_TYPE (rhs),
     834        22581 :                                           TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
     835              :         {
     836        15286 :           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
     837        15286 :           tree new_offset, new_base, saved, new_rhs;
     838        54400 :           while (handled_component_p (*def_rhs_basep))
     839        23828 :             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
     840        15286 :           saved = *def_rhs_basep;
     841        15286 :           if (TREE_CODE (*def_rhs_basep) == MEM_REF)
     842              :             {
     843         7359 :               new_base = TREE_OPERAND (*def_rhs_basep, 0);
     844         7359 :               new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
     845              :                                          TREE_OPERAND (*def_rhs_basep, 1));
     846              :             }
     847              :           else
     848              :             {
     849         7927 :               new_base = build_fold_addr_expr (*def_rhs_basep);
     850         7927 :               new_offset = TREE_OPERAND (rhs, 1);
     851              :             }
     852        15286 :           tree atype = TREE_TYPE (*def_rhs_basep);
     853        15286 :           if (TYPE_ALIGN (TREE_TYPE (rhs)) < TYPE_ALIGN (atype))
     854          526 :             atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (rhs)));
     855        15286 :           *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
     856        15286 :           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
     857        15286 :           TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
     858        15286 :           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
     859        15286 :           new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
     860        15286 :           *rhsp = new_rhs;
     861        15286 :           TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
     862        15286 :           TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
     863        15286 :           *def_rhs_basep = saved;
     864        15286 :           fold_stmt_inplace (use_stmt_gsi);
     865        15286 :           tidy_after_forward_propagate_addr (use_stmt);
     866        15286 :           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      1315740 :   if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
     873      1315740 :       || 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        48440 :   array_ref = TREE_OPERAND (def_rhs, 0);
     881        48440 :   if ((TREE_CODE (array_ref) != ARRAY_REF
     882         4603 :        || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
     883         4603 :        || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
     884        49939 :       && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
     885              :     return false;
     886              : 
     887        24873 :   rhs2 = gimple_assign_rhs2 (use_stmt);
     888              :   /* Optimize &x[C1] p+ C2 to  &x p+ C3 with C3 = C1 * element_size + C2.  */
     889        24873 :   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      3330990 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
     922              : {
     923      3330990 :   bool all = true;
     924      3330990 :   bool single_use_p = parent_single_use_p && has_single_use (name);
     925              : 
     926     17633014 :   for (gimple *use_stmt : gather_imm_use_stmts (name))
     927              :     {
     928      7640044 :       bool result;
     929      7640044 :       tree use_rhs;
     930              : 
     931              :       /* If the use is not in a simple assignment statement, then
     932              :          there is nothing we can do.  */
     933      7640044 :       if (!is_gimple_assign (use_stmt))
     934              :         {
     935      4762224 :           if (!is_gimple_debug (use_stmt))
     936      1947547 :             all = false;
     937      4762224 :           continue;
     938              :         }
     939              : 
     940      2877820 :       gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
     941      2877820 :       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      2877820 :       if (use_stmt != gsi_stmt (gsi))
     946              :         {
     947            0 :           update_stmt (use_stmt);
     948            0 :           use_stmt = gsi_stmt (gsi);
     949              :         }
     950      2877820 :       update_stmt (use_stmt);
     951      2877820 :       all &= result;
     952              : 
     953              :       /* Remove intermediate now unused copy and conversion chains.  */
     954      2877820 :       use_rhs = gimple_assign_rhs1 (use_stmt);
     955      2877820 :       if (result
     956      1508652 :           && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
     957      1260002 :           && TREE_CODE (use_rhs) == SSA_NAME
     958      2959404 :           && has_zero_uses (gimple_assign_lhs (use_stmt)))
     959              :         {
     960        81584 :           gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
     961        81584 :           fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
     962        81584 :           release_defs (use_stmt);
     963        81584 :           gsi_remove (&gsi, true);
     964              :         }
     965      3330990 :     }
     966              : 
     967      3330990 :   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        11713 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
     976              :                                   vec<std::pair<int, int> > &edges_to_remove)
     977              : {
     978        11713 :   unsigned int branch_num = gimple_switch_num_labels (stmt);
     979        11713 :   auto_vec<tree> labels (branch_num);
     980        11713 :   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        73707 :   for (i = 1; i < branch_num; i++)
     985        50281 :     labels.quick_push (gimple_switch_label (stmt, i));
     986        11713 :   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        11713 :   len = labels.length ();
     994        11713 :   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        11713 : }
    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       108712 : 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       108712 :   tree cond = gimple_switch_index (stmt);
    1051       108712 :   if (TREE_CODE (cond) == SSA_NAME)
    1052              :     {
    1053       108711 :       gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
    1054       108711 :       if (gimple_assign_cast_p (def_stmt))
    1055              :         {
    1056        12661 :           tree def = gimple_assign_rhs1 (def_stmt);
    1057        12661 :           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        12661 :           tree ti = TREE_TYPE (def);
    1064        12661 :           if (INTEGRAL_TYPE_P (ti)
    1065        12661 :               && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
    1066              :             {
    1067        12416 :               size_t n = gimple_switch_num_labels (stmt);
    1068        12416 :               tree min = NULL_TREE, max = NULL_TREE;
    1069        12416 :               if (n > 1)
    1070              :                 {
    1071        12416 :                   min = CASE_LOW (gimple_switch_label (stmt, 1));
    1072        12416 :                   if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
    1073          159 :                     max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
    1074              :                   else
    1075        12257 :                     max = CASE_LOW (gimple_switch_label (stmt, n - 1));
    1076              :                 }
    1077        12416 :               if ((!min || int_fits_type_p (min, ti))
    1078        12412 :                   && (!max || int_fits_type_p (max, ti)))
    1079              :                 {
    1080        11713 :                   bitmap_set_bit (simple_dce_worklist,
    1081        11713 :                                   SSA_NAME_VERSION (cond));
    1082        11713 :                   gimple_switch_set_index (stmt, def);
    1083        11713 :                   simplify_gimple_switch_label_vec (stmt, ti,
    1084              :                                                     edges_to_remove);
    1085        11713 :                   update_stmt (stmt);
    1086        11713 :                   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         5476 : constant_pointer_difference (tree p1, tree p2)
    1100              : {
    1101         5476 :   int i, j;
    1102              : #define CPD_ITERATIONS 5
    1103         5476 :   tree exps[2][CPD_ITERATIONS];
    1104         5476 :   tree offs[2][CPD_ITERATIONS];
    1105         5476 :   int cnt[2];
    1106              : 
    1107        16428 :   for (i = 0; i < 2; i++)
    1108              :     {
    1109        10952 :       tree p = i ? p1 : p2;
    1110        10952 :       tree off = size_zero_node;
    1111        10952 :       gimple *stmt;
    1112        10952 :       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        10952 :       j = 0;
    1119        12742 :       do
    1120              :         {
    1121        12742 :           if (!POINTER_TYPE_P (TREE_TYPE (p)))
    1122              :             break;
    1123        12736 :           if (TREE_CODE (p) == ADDR_EXPR)
    1124              :             {
    1125         9589 :               tree q = TREE_OPERAND (p, 0);
    1126         9589 :               poly_int64 offset;
    1127         9589 :               tree base = get_addr_base_and_unit_offset (q, &offset);
    1128         9589 :               if (base)
    1129              :                 {
    1130         8797 :                   q = base;
    1131         8797 :                   if (maybe_ne (offset, 0))
    1132         3749 :                     off = size_binop (PLUS_EXPR, off, size_int (offset));
    1133              :                 }
    1134         9589 :               if (TREE_CODE (q) == MEM_REF
    1135         9589 :                   && 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         9434 :                   exps[i][j] = q;
    1145         9434 :                   offs[i][j++] = off;
    1146         9434 :                   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        10952 :       cnt[i] = j;
    1173              :     }
    1174              : 
    1175         7460 :   for (i = 0; i < cnt[0]; i++)
    1176         9715 :     for (j = 0; j < cnt[1]; j++)
    1177         7731 :       if (exps[0][i] == exps[1][j])
    1178         4591 :         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     22658782 : optimize_aggr_zeroprop_1 (gimple *defstmt, gimple *stmt,
    1188              :                           tree dest, poly_int64 offset, tree val,
    1189              :                           poly_offset_int len)
    1190              : {
    1191     22658782 :   tree src2;
    1192     22658782 :   tree len2 = NULL_TREE;
    1193     22658782 :   poly_int64 offset2;
    1194              : 
    1195     22658782 :   if (gimple_call_builtin_p (stmt, BUILT_IN_MEMCPY)
    1196        22158 :       && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR
    1197     22672778 :       && poly_int_tree_p (gimple_call_arg (stmt, 2)))
    1198              :     {
    1199        12983 :       src2 = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
    1200        12983 :       len2 = gimple_call_arg (stmt, 2);
    1201              :     }
    1202     22645799 :    else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
    1203              :      {
    1204      1911074 :         src2 = gimple_assign_rhs1 (stmt);
    1205      1911074 :         len2 = (TREE_CODE (src2) == COMPONENT_REF
    1206      1911074 :                 ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
    1207      1743403 :                 : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
    1208              :         /* Can only handle zero memsets. */
    1209      1911074 :         if (!integer_zerop (val))
    1210     22637057 :           return;
    1211              :      }
    1212              :    else
    1213              :      return;
    1214              : 
    1215      1923060 :   if (len2 == NULL_TREE
    1216      1923060 :       || !poly_int_tree_p (len2))
    1217              :     return;
    1218              : 
    1219      1923060 :   src2 = get_addr_base_and_unit_offset (src2, &offset2);
    1220      1923060 :   if (src2 == NULL_TREE
    1221      1923060 :       || maybe_lt (offset2, offset))
    1222              :     return;
    1223              : 
    1224       875128 :   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       131486 :   if (maybe_gt (wi::to_poly_offset (len2) + (offset2 - offset),
    1231              :                 len))
    1232              :     return;
    1233              : 
    1234        21725 :   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        21725 :   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        21725 :   if (is_gimple_assign (stmt))
    1250              :     {
    1251        21720 :       tree ctor_type = TREE_TYPE (gimple_assign_lhs (stmt));
    1252        21720 :       tree ctor = build_constructor (ctor_type, NULL);
    1253        21720 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    1254        21720 :       gimple_assign_set_rhs_from_tree (&gsi, ctor);
    1255        21720 :       update_stmt (stmt);
    1256        21720 :       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        21725 :   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        21725 :   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     31574893 : optimize_aggr_zeroprop (gimple *stmt, bool full_walk)
    1291              : {
    1292     31574893 :   ao_ref read;
    1293     63149786 :   if (gimple_has_volatile_ops (stmt))
    1294     27489589 :     return;
    1295              : 
    1296     30645813 :   tree dest = NULL_TREE;
    1297     30645813 :   tree val = integer_zero_node;
    1298     30645813 :   tree len = NULL_TREE;
    1299     30645813 :   bool can_use_tbba = true;
    1300              : 
    1301     30645813 :   if (gimple_call_builtin_p (stmt, BUILT_IN_MEMSET)
    1302       112048 :       && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR
    1303        57318 :       && TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
    1304     30700844 :       && poly_int_tree_p (gimple_call_arg (stmt, 2)))
    1305              :     {
    1306        52265 :       dest = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
    1307        52265 :       len = gimple_call_arg (stmt, 2);
    1308        52265 :       val = gimple_call_arg (stmt, 1);
    1309        52265 :       ao_ref_init_from_ptr_and_size (&read, gimple_call_arg (stmt, 0), len);
    1310        52265 :       can_use_tbba = false;
    1311              :     }
    1312     30593548 :   else if (gimple_store_p (stmt)
    1313     30533605 :            && gimple_assign_single_p (stmt)
    1314     61127153 :            && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST)
    1315              :     {
    1316        26582 :       tree str = gimple_assign_rhs1 (stmt);
    1317        26582 :       dest = gimple_assign_lhs (stmt);
    1318        26582 :       ao_ref_init (&read, dest);
    1319              :       /* The string must contain all null char's for now.  */
    1320        58530 :       for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
    1321              :         {
    1322        29336 :           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     30566966 :   else if (gimple_store_p (stmt)
    1332     30507023 :            && gimple_assign_single_p (stmt)
    1333     61073989 :            && integer_zerop (gimple_assign_rhs1 (stmt)))
    1334              :     {
    1335      3609131 :       tree rhs = gimple_assign_rhs1 (stmt);
    1336      3609131 :       tree type = TREE_TYPE (rhs);
    1337      3609131 :       dest = gimple_assign_lhs (stmt);
    1338      3609131 :       ao_ref_init (&read, dest);
    1339              :       /* For integral types, the type precision needs to be a multiply of BITS_PER_UNIT. */
    1340      3609131 :       if (INTEGRAL_TYPE_P (type)
    1341      3609131 :           && (TYPE_PRECISION (type) % BITS_PER_UNIT) != 0)
    1342              :         dest = NULL_TREE;
    1343              :     }
    1344     26957835 :   else if (gimple_store_p (stmt)
    1345     26897892 :            && gimple_assign_single_p (stmt)
    1346     26897892 :            && TREE_CODE (gimple_assign_rhs1 (stmt)) == CONSTRUCTOR
    1347     27678345 :            && !gimple_clobber_p (stmt))
    1348              :     {
    1349       720510 :       dest = gimple_assign_lhs (stmt);
    1350       720510 :       ao_ref_init (&read, dest);
    1351              :     }
    1352              : 
    1353      4195670 :   if (dest == NULL_TREE)
    1354              :     return;
    1355              : 
    1356      4171700 :   if (len == NULL_TREE)
    1357      4119435 :     len = (TREE_CODE (dest) == COMPONENT_REF
    1358      4119435 :            ? DECL_SIZE_UNIT (TREE_OPERAND (dest, 1))
    1359      1798835 :            : TYPE_SIZE_UNIT (TREE_TYPE (dest)));
    1360      4119435 :   if (len == NULL_TREE
    1361      4171700 :       || !poly_int_tree_p (len))
    1362              :     return;
    1363              : 
    1364              :   /* Sometimes memset can have no vdef due to invalid declaration of memset (const, etc.).  */
    1365     35832965 :   if (!gimple_vdef (stmt))
    1366              :     return;
    1367              : 
    1368              :   /* This store needs to be on the byte boundary and pointing to an object.  */
    1369      4171676 :   poly_int64 offset;
    1370      4171676 :   tree dest_base = get_addr_base_and_unit_offset (dest, &offset);
    1371      4171676 :   if (dest_base == NULL_TREE)
    1372              :     return;
    1373              : 
    1374              :   /* Setup the worklist.  */
    1375      4085304 :   auto_vec<std::pair<tree, unsigned>> worklist;
    1376      4085304 :   unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 0;
    1377      8170608 :   worklist.safe_push (std::make_pair (gimple_vdef (stmt), limit));
    1378              : 
    1379     28288225 :   while (!worklist.is_empty ())
    1380              :     {
    1381     20117617 :       std::pair<tree, unsigned> top = worklist.pop ();
    1382     20117617 :       tree vdef = top.first;
    1383     20117617 :       limit = top.second;
    1384     20117617 :       gimple *use_stmt;
    1385     20117617 :       imm_use_iterator iter;
    1386     44819724 :       FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
    1387              :         {
    1388              :           /* Handling PHI nodes might not be worth it so don't.  */
    1389     24702107 :           if (is_a <gphi*> (use_stmt))
    1390      2043325 :             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      2071366 :           if ((limit != 0 || gimple_clobber_p (use_stmt))
    1396     20624192 :               && gimple_vdef (use_stmt)
    1397     40146939 :               && !stmt_may_clobber_ref_p_1 (use_stmt, &read,
    1398              :                                            /* tbaa_p = */ can_use_tbba))
    1399              :               {
    1400     16032313 :                 unsigned new_limit = limit == 0 ? 0 : limit - 1;
    1401     32064626 :                 worklist.safe_push (std::make_pair (gimple_vdef (use_stmt),
    1402              :                                                     new_limit));
    1403              :               }
    1404              : 
    1405     22658782 :            optimize_aggr_zeroprop_1 (stmt, use_stmt, dest_base, offset,
    1406     22658782 :                                      val, wi::to_poly_offset (len));
    1407     20117617 :         }
    1408              :     }
    1409              : 
    1410      4085304 : }
    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      5316356 : split_core_and_offset_size (tree expr,
    1420              :                             poly_int64 *pbytesize, poly_int64 *pbytepos,
    1421              :                             tree *poffset, int *preversep)
    1422              : {
    1423      5316356 :   tree core;
    1424      5316356 :   machine_mode mode;
    1425      5316356 :   int unsignedp, volatilep;
    1426      5316356 :   poly_int64 bitsize;
    1427      5316356 :   poly_int64 bitpos;
    1428      5316356 :   location_t loc = EXPR_LOCATION (expr);
    1429              : 
    1430      5316356 :   core = get_inner_reference (expr, &bitsize, &bitpos,
    1431              :                               poffset, &mode, &unsignedp, preversep,
    1432              :                               &volatilep);
    1433     10632712 :   if (!multiple_p (bitsize, BITS_PER_UNIT, pbytesize))
    1434              :     return NULL_TREE;
    1435      5316356 :   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      5316356 :   if (TREE_CODE (core) == MEM_REF)
    1440              :     {
    1441      1246533 :       poly_offset_int tem;
    1442      1246533 :       tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
    1443      1246533 :       tem += *pbytepos;
    1444      1246533 :       if (tem.to_shwi (pbytepos))
    1445      1244638 :         return TREE_OPERAND (core, 0);
    1446              :     }
    1447      4071718 :   core = build_fold_addr_expr_loc (loc, core);
    1448      4071718 :   STRIP_NOPS (core);
    1449      4071718 :   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      1269224 : 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      1269224 :   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       720877 :   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       378673 :   if (TREE_CODE (src) == IMAGPART_EXPR
    1473       378673 :       || TREE_CODE (src) == REALPART_EXPR
    1474       378673 :       || TREE_CODE (src) == BIT_FIELD_REF)
    1475              :     return NULL_TREE;
    1476       378673 :   tree core1, core2;
    1477       378673 :   poly_int64 bytepos1, bytepos2;
    1478       378673 :   poly_int64 bytesize1, bytesize2;
    1479       378673 :   tree toffset1, toffset2;
    1480       378673 :   int reversep1 = 0;
    1481       378673 :   int reversep2 = 0;
    1482       378673 :   poly_int64 diff = 0;
    1483       378673 :   core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
    1484              :                                           &toffset1, &reversep1);
    1485       378673 :   core2 = split_core_and_offset_size (src2, &bytesize2, &bytepos2,
    1486              :                                           &toffset2, &reversep2);
    1487       378673 :   if (!core1 || !core2)
    1488              :     return NULL_TREE;
    1489       378673 :   if (reversep1 != reversep2)
    1490              :     return NULL_TREE;
    1491              :   /* The sizes of the 2 accesses need to be the same. */
    1492       378673 :   if (!known_eq (bytesize1, bytesize2))
    1493              :     return NULL_TREE;
    1494       171976 :   if (!operand_equal_p (core1, core2, 0))
    1495              :     return NULL_TREE;
    1496              : 
    1497        23428 :   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        23426 :   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        23394 :   diff += bytepos1 - bytepos2;
    1516              :   /* The offset between the 2 need to be 0. */
    1517        23394 :   if (!known_eq (diff, 0))
    1518              :     return NULL_TREE;
    1519        22578 :   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      4518344 : same_for_assignment (tree src, tree dest)
    1529              : {
    1530      4518344 :   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      4515437 :   if (DECL_P (dest) && DECL_P (src))
    1535              :     return false;
    1536              : 
    1537      2279505 :   tree core1, core2;
    1538      2279505 :   poly_int64 bytepos1, bytepos2;
    1539      2279505 :   poly_int64 bytesize1, bytesize2;
    1540      2279505 :   tree toffset1, toffset2;
    1541      2279505 :   int reversep1 = 0;
    1542      2279505 :   int reversep2 = 0;
    1543      2279505 :   poly_int64 diff = 0;
    1544      2279505 :   core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
    1545              :                                       &toffset1, &reversep1);
    1546      2279505 :   core2 = split_core_and_offset_size (src, &bytesize2, &bytepos2,
    1547              :                                       &toffset2, &reversep2);
    1548      2279505 :   if (!core1 || !core2)
    1549              :     return false;
    1550      2279505 :   if (reversep1 != reversep2)
    1551              :     return false;
    1552              :   /* The sizes of the 2 accesses need to be the same. */
    1553      2279505 :   if (!known_eq (bytesize1, bytesize2))
    1554              :     return false;
    1555      2278587 :   if (!operand_equal_p (core1, core2, 0))
    1556              :     return false;
    1557         6106 :   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         5793 :   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         5793 :   diff += bytepos1 - bytepos2;
    1576              :   /* The offset between the 2 need to be 0. */
    1577         5793 :   if (!known_eq (diff, 0))
    1578         5547 :     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      1026350 : optimize_agr_copyprop_1 (gimple *stmt, gimple *use_stmt,
    1587              :                          tree dest, tree src)
    1588              : {
    1589      1026350 :   gcc_assert (gimple_assign_load_p (use_stmt)
    1590              :               && gimple_store_p (use_stmt));
    1591      2052700 :   if (gimple_has_volatile_ops (use_stmt))
    1592       612335 :     return;
    1593      1026349 :   tree dest2 = gimple_assign_lhs (use_stmt);
    1594      1026349 :   tree src2 = gimple_assign_rhs1 (use_stmt);
    1595              :   /* If the new store is `src2 = src2;` skip over it. */
    1596      1026349 :   if (same_for_assignment (src2, dest2))
    1597              :     return;
    1598      1025786 :   src = new_src_based_on_copy (src2, dest, src);
    1599      1025786 :   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       442470 :   if (!operand_equal_p (dest2, src, 0)
    1619       442470 :       && !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        31809 :       poly_int64 offset1, offset2;
    1625        31809 :       tree len = TYPE_SIZE_UNIT (TREE_TYPE (src));
    1626        31809 :       if (len == NULL_TREE
    1627        31809 :           || !tree_fits_poly_int64_p (len))
    1628        28455 :         return;
    1629        31809 :       tree base1 = get_addr_base_and_unit_offset (dest2, &offset1);
    1630        31809 :       tree base2 = get_addr_base_and_unit_offset (src, &offset2);
    1631        31809 :       poly_int64 size = tree_to_poly_int64 (len);
    1632              :       /* If the bases are 2 different decls,
    1633              :          then there can be no overlapping.  */
    1634        31809 :       if (base1 && base2
    1635        30941 :           && DECL_P (base1) && DECL_P (base2)
    1636         1892 :           && 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        30146 :       else if (!base1
    1641        30146 :                || !base2
    1642        30146 :                || !operand_equal_p (base1, base2))
    1643              :         {
    1644        29774 :           unsigned int align1 = get_object_alignment (src);
    1645        29774 :           unsigned int align2 = get_object_alignment (dest2);
    1646        29774 :           align1 /= BITS_PER_UNIT;
    1647        29774 :           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        29774 :           if (maybe_lt (align1, size)
    1652        29774 :               || maybe_lt (align2, size))
    1653        28455 :             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       414015 :   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       414015 :   gimple *orig_stmt = use_stmt;
    1671       414015 :   gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
    1672       414015 :   gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (src));
    1673       414015 :   update_stmt (use_stmt);
    1674              : 
    1675       414015 :   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       414015 :   if (maybe_clean_or_replace_eh_stmt (orig_stmt, use_stmt))
    1681            0 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
    1682       414015 :   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       709050 : optimize_agr_copyprop_arg (gimple *defstmt, gcall *call,
    1690              :                            tree dest, tree src)
    1691              : {
    1692       709050 :   bool changed = false;
    1693      2376175 :   for (unsigned arg = 0; arg < gimple_call_num_args (call); arg++)
    1694              :     {
    1695      1667125 :       tree *argptr = gimple_call_arg_ptr (call, arg);
    1696      3143828 :       if (TREE_CODE (*argptr) == SSA_NAME
    1697       950083 :           || is_gimple_min_invariant (*argptr)
    1698      1857547 :           || TYPE_VOLATILE (TREE_TYPE (*argptr)))
    1699      1476703 :         continue;
    1700       190422 :       tree newsrc = new_src_based_on_copy (*argptr, dest, src);
    1701       190422 :       if (!newsrc)
    1702       114895 :         continue;
    1703              : 
    1704        75527 :       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        75527 :       *argptr = unshare_expr (newsrc);
    1712        75527 :       changed = true;
    1713        75527 :       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       709050 :   if (changed)
    1720        75351 :     update_stmt (call);
    1721       709050 : }
    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       128674 : optimize_agr_copyprop_return (gimple *defstmt, greturn *use,
    1728              :                               tree dest, tree src)
    1729              : {
    1730       128674 :   tree rvalue = gimple_return_retval (use);
    1731       128674 :   if (!rvalue
    1732        83890 :       || TREE_CODE (rvalue) == SSA_NAME
    1733        75082 :       || is_gimple_min_invariant (rvalue)
    1734       203348 :       || 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        74673 :   if (TREE_CODE (rvalue) == RESULT_DECL
    1740        74673 :       || (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        53016 :   tree newsrc = new_src_based_on_copy (rvalue, dest, src);
    1746        53016 :   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        52928 :   if (!VAR_P (newsrc) || is_global_var (newsrc))
    1752              :     return;
    1753        26604 :   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        26604 :   gimple_return_set_retval (use, newsrc);
    1761        26604 :   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        26604 :   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      3900925 : optimize_agr_copyprop (gimple *stmt)
    1788              : {
    1789      7801850 :   if (gimple_has_volatile_ops (stmt))
    1790       411520 :     return;
    1791              : 
    1792              :   /* Can't prop if the statement could throw.  */
    1793      3899714 :   if (stmt_could_throw_p (cfun, stmt))
    1794              :     return;
    1795              : 
    1796      3491995 :   tree dest = gimple_assign_lhs (stmt);
    1797      3491995 :   tree src = gimple_assign_rhs1 (stmt);
    1798              :   /* If the statement is `src = src;` then ignore it. */
    1799      3491995 :   if (same_for_assignment (dest, src))
    1800              :     return;
    1801              : 
    1802      3489405 :   tree vdef = gimple_vdef (stmt);
    1803      3489405 :   imm_use_iterator iter;
    1804      3489405 :   gimple *use_stmt;
    1805     10062496 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
    1806              :     {
    1807      6573091 :       if (gimple_assign_load_p (use_stmt)
    1808      6573091 :           && gimple_store_p (use_stmt))
    1809      1026350 :         optimize_agr_copyprop_1 (stmt, use_stmt, dest, src);
    1810      5546741 :       else if (is_gimple_call (use_stmt))
    1811       709050 :         optimize_agr_copyprop_arg (stmt, as_a<gcall*>(use_stmt), dest, src);
    1812      4837691 :       else if (is_a<greturn*> (use_stmt))
    1813       128674 :         optimize_agr_copyprop_return (stmt, as_a<greturn*>(use_stmt), dest, src);
    1814      3489405 :     }
    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      7469041 : 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      7469041 :   if (optimize_debug)
    1834      5164311 :     return;
    1835      7465636 :   ao_ref read;
    1836      7465636 :   basic_block bb = gimple_bb (stmt);
    1837      7465636 :   tree lhs = gimple_assign_lhs (stmt);
    1838              :   /* Only handle clobbers of a full decl.  */
    1839      7465636 :   if (!DECL_P (lhs))
    1840              :     return;
    1841      6734851 :   ao_ref_init (&read, lhs);
    1842      6734851 :   tree vuse = gimple_vuse (stmt);
    1843      6734851 :   unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 4;
    1844     17126579 :   while (limit)
    1845              :     {
    1846     17113159 :       gimple *ostmt = SSA_NAME_DEF_STMT (vuse);
    1847              :       /* Don't handle phis, just declare to be done. */
    1848     17113159 :       if (is_a<gphi*>(ostmt) || gimple_nop_p (ostmt))
    1849              :         break;
    1850     14821849 :       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     14821849 :       if (obb != bb && !dominated_by_p (CDI_DOMINATORS, bb, obb))
    1854      4430121 :         return;
    1855     14821849 :       gimple *use_stmt;
    1856     14821849 :       imm_use_iterator iter;
    1857     45147367 :       FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_vdef (ostmt))
    1858              :         {
    1859     17485335 :           basic_block ubb = gimple_bb (use_stmt);
    1860     17485335 :           if (stmt == use_stmt)
    1861      5175046 :             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     12310289 :           if (gimple_clobber_p (use_stmt)
    1866     12310289 :               && lhs == gimple_assign_lhs (use_stmt))
    1867       176617 :             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     12133672 :           if (gphi *use_phi = dyn_cast<gphi*>(use_stmt))
    1871              :             {
    1872       365128 :               use_operand_p ou;
    1873       365128 :               gimple *ostmt;
    1874       365128 :               if (single_imm_use (gimple_phi_result (use_phi), &ou, &ostmt)
    1875       305250 :                   && gimple_clobber_p (ostmt)
    1876       612283 :                   && lhs == gimple_assign_lhs (ostmt))
    1877        68773 :                 continue;
    1878              :               /* A phi node will never be dominating the clobber.  */
    1879       296355 :               return;
    1880              :             }
    1881              :           /* The use needs to be dominating the clobber. */
    1882      1499959 :           if ((ubb != bb && !dominated_by_p (CDI_DOMINATORS, bb, ubb))
    1883     12524440 :               || ref_maybe_used_by_stmt_p (use_stmt, &read, false))
    1884              :             return;
    1885              :           /* Count the above alias lookup towards the limit. */
    1886     10547970 :           limit--;
    1887     10547970 :           if (limit == 0)
    1888              :             return;
    1889      1981666 :         }
    1890     12840183 :       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     12840183 :       if (is_a <gcall*>(ostmt)
    1894     12840183 :           && 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     12836172 :       if (is_a <gassign*>(ostmt)
    1943     10836150 :           && gimple_store_p (ostmt)
    1944     10836150 :           && !gimple_clobber_p (ostmt)
    1945     16053433 :           && lhs == gimple_assign_lhs (ostmt))
    1946              :         {
    1947              :           /* Don't remove stores/statements that are needed for non-call
    1948              :               eh to work.  */
    1949       167557 :           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       162477 :           if (stmt_could_throw_p (cfun, ostmt))
    1954            0 :             bitmap_set_bit (to_purge, obb->index);
    1955       162477 :           gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
    1956       162477 :           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       162477 :           unlink_stmt_vdef (ostmt);
    1963       162477 :           release_defs (ostmt);
    1964       162477 :           gsi_remove (&gsi, true);
    1965       162477 :           statistics_counter_event (cfun, "delete dead store", 1);
    1966              :           /* Only remove the first store previous statement. */
    1967       162477 :           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     12668615 :       if (stmt_may_clobber_ref_p_1 (ostmt, &read, false)
    1979     12668615 :           || ref_maybe_used_by_stmt_p (ostmt, &read, false))
    1980              :         return;
    1981     10391728 :       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       472049 : simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
    1991              : {
    1992              :   /* Make sure memcmp arguments are the correct type.  */
    1993       472049 :   if (gimple_call_num_args (stmt) != 3)
    1994              :     return false;
    1995       472049 :   tree arg1 = gimple_call_arg (stmt, 0);
    1996       472049 :   tree arg2 = gimple_call_arg (stmt, 1);
    1997       472049 :   tree len = gimple_call_arg (stmt, 2);
    1998              : 
    1999       472049 :   if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
    2000              :     return false;
    2001       472049 :   if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
    2002              :     return false;
    2003       472049 :   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       472049 :   tree res = gimple_call_lhs (stmt);
    2009              : 
    2010       472049 :   if (!res || !use_in_zero_equality (res))
    2011              :     return false;
    2012              : 
    2013       456844 :   unsigned HOST_WIDE_INT leni;
    2014              : 
    2015       456844 :   if (tree_fits_uhwi_p (len)
    2016       635330 :       && (leni = tree_to_uhwi (len)) <= GET_MODE_SIZE (word_mode)
    2017       538091 :       && pow2p_hwi (leni))
    2018              :     {
    2019        19198 :       leni *= CHAR_TYPE_SIZE;
    2020        19198 :       unsigned align1 = get_pointer_alignment (arg1);
    2021        19198 :       unsigned align2 = get_pointer_alignment (arg2);
    2022        19198 :       unsigned align = MIN (align1, align2);
    2023        19198 :       scalar_int_mode mode;
    2024        38396 :       if (int_mode_for_size (leni, 1).exists (&mode)
    2025        19198 :           && (align >= leni || !targetm.slow_unaligned_access (mode, align)))
    2026              :         {
    2027        19198 :           location_t loc = gimple_location (stmt);
    2028        19198 :           tree type, off;
    2029        19198 :           type = build_nonstandard_integer_type (leni, 1);
    2030        38396 :           gcc_assert (known_eq (GET_MODE_BITSIZE (TYPE_MODE (type)), leni));
    2031        19198 :           tree ptrtype = build_pointer_type_for_mode (char_type_node,
    2032              :                                                       ptr_mode, true);
    2033        19198 :           off = build_int_cst (ptrtype, 0);
    2034              : 
    2035              :           /* Create unaligned types if needed. */
    2036        19198 :           tree type1 = type, type2 = type;
    2037        19198 :           if (TYPE_ALIGN (type1) > align1)
    2038         7882 :             type1 = build_aligned_type (type1, align1);
    2039        19198 :           if (TYPE_ALIGN (type2) > align2)
    2040         8377 :             type2 = build_aligned_type (type2, align2);
    2041              : 
    2042        19198 :           arg1 = build2_loc (loc, MEM_REF, type1, arg1, off);
    2043        19198 :           arg2 = build2_loc (loc, MEM_REF, type2, arg2, off);
    2044        19198 :           tree tem1 = fold_const_aggregate_ref (arg1);
    2045        19198 :           if (tem1)
    2046          222 :             arg1 = tem1;
    2047        19198 :           tree tem2 = fold_const_aggregate_ref (arg2);
    2048        19198 :           if (tem2)
    2049         7550 :             arg2 = tem2;
    2050        19198 :           res = fold_convert_loc (loc, TREE_TYPE (res),
    2051              :                                   fold_build2_loc (loc, NE_EXPR,
    2052              :                                                    boolean_type_node,
    2053              :                                                    arg1, arg2));
    2054        19198 :           gimplify_and_update_call_from_tree (gsi_p, res);
    2055        19198 :           return true;
    2056              :         }
    2057              :     }
    2058              : 
    2059              :   /* Replace memcmp with memcmp_eq if the above fails. */
    2060       437646 :   if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
    2061              :     return false;
    2062       346450 :   if (!fold_before_rtl_expansion_p ())
    2063              :     return false;
    2064        91196 :   gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
    2065        91196 :   update_stmt (stmt);
    2066        91196 :   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        14687 : simplify_builtin_memchr (gimple_stmt_iterator *gsi_p, gcall *stmt)
    2075              : {
    2076        14687 :   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
    2077              :     return false;
    2078              : 
    2079        14687 :   if (gimple_call_num_args (stmt) != 3)
    2080              :     return false;
    2081              : 
    2082        14687 :   tree res = gimple_call_lhs (stmt);
    2083        14687 :   if (!res || !use_in_zero_equality (res))
    2084              :     return false;
    2085              : 
    2086         1461 :   tree ptr = gimple_call_arg (stmt, 0);
    2087         1461 :   if (TREE_CODE (ptr) != ADDR_EXPR
    2088         1461 :       || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
    2089              :     return false;
    2090              : 
    2091          454 :   unsigned HOST_WIDE_INT slen
    2092          454 :     = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
    2093              :   /* It must be a non-empty string constant.  */
    2094          454 :   if (slen < 2)
    2095              :     return false;
    2096              : 
    2097              :   /* For -Os, only simplify strings with a single character.  */
    2098          450 :   if (!optimize_bb_for_speed_p (gimple_bb (stmt))
    2099          450 :       && slen > 2)
    2100              :     return false;
    2101              : 
    2102          434 :   tree size = gimple_call_arg (stmt, 2);
    2103              :   /* Size must be a constant which is <= UNITS_PER_WORD and
    2104              :      <= the string length.  */
    2105          434 :   if (!tree_fits_uhwi_p (size))
    2106              :     return false;
    2107              : 
    2108          434 :   unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
    2109          435 :   if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
    2110              :     return false;
    2111              : 
    2112          382 :   tree ch = gimple_call_arg (stmt, 1);
    2113          382 :   location_t loc = gimple_location (stmt);
    2114          382 :   if (!useless_type_conversion_p (char_type_node,
    2115          382 :                                   TREE_TYPE (ch)))
    2116          382 :     ch = fold_convert_loc (loc, char_type_node, ch);
    2117          382 :   const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
    2118          382 :   unsigned int isize = sz;
    2119          382 :   tree *op = XALLOCAVEC (tree, isize);
    2120         1377 :   for (unsigned int i = 0; i < isize; i++)
    2121              :     {
    2122          995 :       op[i] = build_int_cst (char_type_node, p[i]);
    2123          995 :       op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
    2124              :                                op[i], ch);
    2125              :     }
    2126          995 :   for (unsigned int i = isize - 1; i >= 1; i--)
    2127          613 :     op[i - 1] = fold_convert_loc (loc, boolean_type_node,
    2128              :                                   fold_build2_loc (loc,
    2129              :                                                    BIT_IOR_EXPR,
    2130              :                                                    boolean_type_node,
    2131          613 :                                                    op[i - 1],
    2132          613 :                                                    op[i]));
    2133          382 :   res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
    2134          382 :   gimplify_and_update_call_from_tree (gsi_p, res);
    2135          382 :   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       112208 : simplify_builtin_memcpy_memset (gimple_stmt_iterator *gsi_p, gcall *stmt2)
    2149              : {
    2150       112208 :   if (gimple_call_num_args (stmt2) != 3
    2151       112208 :       || gimple_call_lhs (stmt2)
    2152              :       || CHAR_BIT != 8
    2153       112208 :       || BITS_PER_UNIT != 8)
    2154              :     return false;
    2155              : 
    2156       106043 :   tree vuse = gimple_vuse (stmt2);
    2157       106043 :   if (vuse == NULL)
    2158              :     return false;
    2159       106027 :   gimple *stmt1 = SSA_NAME_DEF_STMT (vuse);
    2160              : 
    2161       106027 :   tree callee1;
    2162       106027 :   tree ptr1, src1, str1, off1, len1, lhs1;
    2163       106027 :   tree ptr2 = gimple_call_arg (stmt2, 0);
    2164       106027 :   tree val2 = gimple_call_arg (stmt2, 1);
    2165       106027 :   tree len2 = gimple_call_arg (stmt2, 2);
    2166       106027 :   tree diff, vdef, new_str_cst;
    2167       106027 :   gimple *use_stmt;
    2168       106027 :   unsigned int ptr1_align;
    2169       106027 :   unsigned HOST_WIDE_INT src_len;
    2170       106027 :   char *src_buf;
    2171       106027 :   use_operand_p use_p;
    2172              : 
    2173       106027 :   if (!tree_fits_shwi_p (val2)
    2174       101551 :       || !tree_fits_uhwi_p (len2)
    2175       170361 :       || compare_tree_int (len2, 1024) == 1)
    2176              :     return false;
    2177              : 
    2178        58777 :   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        30542 :       callee1 = gimple_call_fndecl (stmt1);
    2184        30542 :       if (callee1 == NULL_TREE
    2185        30426 :           || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
    2186        57370 :           || gimple_call_num_args (stmt1) != 3)
    2187              :         return false;
    2188        25495 :       if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
    2189        25495 :           && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
    2190              :         return false;
    2191        11100 :       ptr1 = gimple_call_arg (stmt1, 0);
    2192        11100 :       src1 = gimple_call_arg (stmt1, 1);
    2193        11100 :       len1 = gimple_call_arg (stmt1, 2);
    2194        11100 :       lhs1 = gimple_call_lhs (stmt1);
    2195        11100 :       if (!tree_fits_uhwi_p (len1))
    2196              :         return false;
    2197        11013 :       str1 = string_constant (src1, &off1, NULL, NULL);
    2198        11013 :       if (str1 == NULL_TREE)
    2199              :         return false;
    2200         5126 :       if (!tree_fits_uhwi_p (off1)
    2201         5126 :           || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
    2202         5126 :           || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
    2203         5126 :                                      - tree_to_uhwi (off1)) > 0
    2204         5126 :           || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
    2205        15378 :           || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
    2206         5126 :              != TYPE_MODE (char_type_node))
    2207              :         return false;
    2208              :     }
    2209        28235 :   else if (gimple_assign_single_p (stmt1))
    2210              :     {
    2211              :       /* Otherwise look for length 1 memcpy optimized into
    2212              :          assignment.  */
    2213        17315 :       ptr1 = gimple_assign_lhs (stmt1);
    2214        17315 :       src1 = gimple_assign_rhs1 (stmt1);
    2215        17315 :       if (TREE_CODE (ptr1) != MEM_REF
    2216         3435 :           || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
    2217        18294 :           || !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         5469 :   diff = constant_pointer_difference (ptr1, ptr2);
    2231         5469 :   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         5469 :   if (diff == NULL
    2242         4591 :       || !tree_fits_uhwi_p (diff)
    2243         4591 :       || tree_int_cst_lt (len1, diff)
    2244         9804 :       || 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         4335 :   src_len = tree_to_uhwi (diff);
    2250         4335 :   src_len += tree_to_uhwi (len2);
    2251         4335 :   if (src_len < tree_to_uhwi (len1))
    2252              :     src_len = tree_to_uhwi (len1);
    2253         4335 :   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         4335 :   if (lhs1 != NULL_TREE
    2259           64 :       && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
    2260         4342 :       && (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         4335 :   vdef = gimple_vdef (stmt1);
    2268         4335 :   if (vdef != NULL
    2269         4335 :       && (!single_imm_use (vdef, &use_p, &use_stmt)
    2270         3616 :           || use_stmt != stmt2))
    2271              :     return false;
    2272              : 
    2273         3616 :   ptr1_align = get_pointer_alignment (ptr1);
    2274              :   /* Construct the new source string literal.  */
    2275         3616 :   src_buf = XALLOCAVEC (char, src_len + 1);
    2276         3616 :   if (callee1)
    2277         3450 :     memcpy (src_buf,
    2278         3450 :             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         3616 :   memset (src_buf + tree_to_uhwi (diff),
    2283         3616 :           tree_to_shwi (val2), tree_to_uhwi (len2));
    2284         3616 :   src_buf[src_len] = '\0';
    2285              :   /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
    2286              :      handle embedded '\0's.  */
    2287         3616 :   if (strlen (src_buf) != src_len)
    2288              :     return false;
    2289         3522 :   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         3522 :   if (!can_store_by_pieces (src_len,
    2295              :                             builtin_strncpy_read_str,
    2296              :                             src_buf, ptr1_align, false))
    2297              :     return false;
    2298              : 
    2299         2632 :   new_str_cst = build_string_literal (src_len, src_buf);
    2300         2632 :   if (callee1)
    2301              :     {
    2302              :       /* If STMT1 is a mem{,p}cpy call, adjust it and remove
    2303              :          memset call.  */
    2304         2504 :       if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
    2305            7 :         gimple_call_set_lhs (stmt1, NULL_TREE);
    2306         2504 :       gimple_call_set_arg (stmt1, 1, new_str_cst);
    2307         2504 :       gimple_call_set_arg (stmt1, 2,
    2308         2504 :                            build_int_cst (TREE_TYPE (len1), src_len));
    2309         2504 :       update_stmt (stmt1);
    2310         2504 :       unlink_stmt_vdef (stmt2);
    2311         2504 :       gsi_replace (gsi_p, gimple_build_nop (), false);
    2312         2504 :       fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
    2313         2504 :       release_defs (stmt2);
    2314         2504 :       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        10377 : optimize_stack_restore (gimple_stmt_iterator *gsi, gimple *call)
    2362              : {
    2363        10377 :   if (!fold_before_rtl_expansion_p ())
    2364              :     return false;
    2365         2527 :   tree callee;
    2366         2527 :   gimple *stmt;
    2367              : 
    2368         2527 :   basic_block bb = gsi_bb (*gsi);
    2369              : 
    2370         2527 :   if (gimple_call_num_args (call) != 1
    2371         2527 :       || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
    2372         5054 :       || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
    2373              :     return false;
    2374              : 
    2375         2527 :   gimple_stmt_iterator i = *gsi;
    2376         6292 :   for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
    2377              :     {
    2378         4220 :       stmt = gsi_stmt (i);
    2379         4220 :       if (is_a<gasm*> (stmt))
    2380              :         return false;
    2381         4219 :       gcall *call = dyn_cast<gcall*>(stmt);
    2382         4219 :       if (!call)
    2383         3556 :         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          663 :       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          647 :       if (gimple_call_internal_p (call))
    2394            1 :         continue;
    2395              : 
    2396          646 :       callee = gimple_call_fndecl (call);
    2397              :       /* Non-builtin calls are not ok. */
    2398          646 :       if (!callee
    2399          646 :           || !fndecl_built_in_p (callee))
    2400              :         return false;
    2401              : 
    2402              :       /* Do not remove stack updates before strub leave.  */
    2403          570 :       if (fndecl_built_in_p (callee, BUILT_IN___STRUB_LEAVE)
    2404              :           /* Alloca calls are not ok either. */
    2405          570 :           || fndecl_builtin_alloc_p (callee))
    2406              :         return false;
    2407              : 
    2408          355 :       if (fndecl_built_in_p (callee, BUILT_IN_STACK_RESTORE))
    2409           52 :         goto second_stack_restore;
    2410              : 
    2411              :       /* If not a simple or inexpensive builtin, then it is not ok either. */
    2412          303 :       if (!is_simple_builtin (callee)
    2413          303 :           && !is_inexpensive_builtin (callee))
    2414              :         return false;
    2415              :     }
    2416              : 
    2417              :   /* Allow one successor of the exit block, or zero successors.  */
    2418         2088 :   switch (EDGE_COUNT (bb->succs))
    2419              :     {
    2420              :     case 0:
    2421              :       break;
    2422         2001 :     case 1:
    2423         2001 :       if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
    2424              :         return false;
    2425              :       break;
    2426              :     default:
    2427              :       return false;
    2428              :     }
    2429         1731 :  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         1731 :   if (has_single_use (gimple_call_arg (call, 0)))
    2436              :     {
    2437         1561 :       gimple *stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
    2438         1561 :       if (is_gimple_call (stack_save))
    2439              :         {
    2440         1559 :           callee = gimple_call_fndecl (stack_save);
    2441         1559 :           if (callee && fndecl_built_in_p (callee, BUILT_IN_STACK_SAVE))
    2442              :             {
    2443         1559 :               gimple_stmt_iterator stack_save_gsi;
    2444         1559 :               tree rhs;
    2445              : 
    2446         1559 :               stack_save_gsi = gsi_for_stmt (stack_save);
    2447         1559 :               rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
    2448         1559 :               replace_call_with_value (&stack_save_gsi, rhs);
    2449              :             }
    2450              :         }
    2451              :     }
    2452              : 
    2453              :   /* No effect, so the statement will be deleted.  */
    2454         1731 :   replace_call_with_value (gsi, NULL_TREE);
    2455         1731 :   return true;
    2456              : }
    2457              : 
    2458              : /* Optimizes strlen (s) ==/!= 0 to *s ==/!= 0. */
    2459              : static bool
    2460        64884 : optimize_strlen_comp (gimple_stmt_iterator *gsi, gimple *call)
    2461              : {
    2462        64884 :   if (!fold_before_rtl_expansion_p ())
    2463              :     return false;
    2464              : 
    2465        14058 :   tree lhs = gimple_call_lhs (call);
    2466        14058 :   if (lhs == NULL_TREE || use_in_zero_equality (lhs, true) == NULL)
    2467              :     return false;
    2468              : 
    2469              :   /* The string passed to strlen. */
    2470          165 :   tree ptr = gimple_call_arg (call, 0);
    2471              : 
    2472              :   /* Dereference the string. */
    2473          165 :   tree deref = fold_build2 (MEM_REF, char_type_node, ptr,
    2474              :                             build_zero_cst (ptr_type_node));
    2475              : 
    2476              :   /* Perform a type conversion. */
    2477          165 :   deref = fold_convert_loc (gimple_location (call),
    2478          165 :                             TREE_TYPE (lhs),
    2479              :                             deref);
    2480              : 
    2481              :   /* Replace the original call to strlen with the dereference we just built. */
    2482          165 :   gimplify_and_update_call_from_tree (gsi, deref);
    2483              : 
    2484          165 :   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      6312514 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2, bool full_walk)
    2511              : {
    2512      6312514 :   gimple *stmt2 = gsi_stmt (*gsi_p);
    2513      6312514 :   enum built_in_function other_atomic = END_BUILTINS;
    2514      6312514 :   enum tree_code atomic_op = ERROR_MARK;
    2515              : 
    2516      6312514 :   switch (DECL_FUNCTION_CODE (callee2))
    2517              :     {
    2518        64884 :     case BUILT_IN_STRLEN:
    2519        64884 :       return optimize_strlen_comp (gsi_p, as_a<gcall*>(stmt2));
    2520        10377 :     case BUILT_IN_STACK_RESTORE:
    2521        10377 :       return optimize_stack_restore (gsi_p, as_a<gcall*>(stmt2));
    2522       472049 :     case BUILT_IN_MEMCMP:
    2523       472049 :     case BUILT_IN_MEMCMP_EQ:
    2524       472049 :       return simplify_builtin_memcmp (gsi_p, as_a<gcall*>(stmt2));
    2525        14687 :     case BUILT_IN_MEMCHR:
    2526        14687 :       return simplify_builtin_memchr (gsi_p, as_a<gcall*>(stmt2));
    2527              : 
    2528       112208 :     case BUILT_IN_MEMSET:
    2529       112208 :       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       112208 :           optimize_aggr_zeroprop (stmt2, full_walk);
    2534              :         }
    2535       112208 :       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        48772 :     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         3868 :     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        14409 :     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       103763 :     handle_atomic_fetch_op:
    2573       103763 :       if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
    2574              :         {
    2575        60289 :           tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
    2576        60289 :           tree arg = gimple_call_arg (stmt2, 1);
    2577        60289 :           gimple *use_stmt, *cast_stmt = NULL;
    2578        60289 :           use_operand_p use_p;
    2579        60289 :           tree ndecl = builtin_decl_explicit (other_atomic);
    2580              : 
    2581        60289 :           if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
    2582              :             break;
    2583              : 
    2584        59160 :           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        59160 :           bool ok = false;
    2601        59160 :           tree oarg = NULL_TREE;
    2602        59160 :           enum tree_code ccode = ERROR_MARK;
    2603        59160 :           tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
    2604        59160 :           if (is_gimple_assign (use_stmt)
    2605        59160 :               && 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        57744 :           else if (atomic_op == MINUS_EXPR
    2613        13279 :                    && is_gimple_assign (use_stmt)
    2614         3638 :                    && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
    2615          199 :                    && TREE_CODE (arg) == INTEGER_CST
    2616        57943 :                    && (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        57561 :           else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
    2626              :             ;
    2627        52263 :           else if (gimple_code (use_stmt) == GIMPLE_COND)
    2628              :             {
    2629        19582 :               ccode = gimple_cond_code (use_stmt);
    2630        19582 :               crhs1 = gimple_cond_lhs (use_stmt);
    2631        19582 :               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        24533 :           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        22333 :               tree o = NULL_TREE;
    2648        22333 :               if (crhs1 == lhsc)
    2649              :                 o = crhs2;
    2650          133 :               else if (crhs2 == lhsc)
    2651          133 :                 o = crhs1;
    2652        22333 :               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        59160 :                     ok = true;
    2662              :                 }
    2663              :             }
    2664        59160 :           if (oarg && !ok)
    2665              :             {
    2666        13632 :               if (operand_equal_p (arg, oarg, 0))
    2667              :                 ok = true;
    2668        12303 :               else if (TREE_CODE (arg) == SSA_NAME
    2669         2203 :                        && 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        57831 :           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     17586458 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
    2834              : {
    2835     17586458 :   gimple *def;
    2836     17586458 :   enum tree_code code1;
    2837     17586458 :   tree arg11;
    2838     17586458 :   tree arg21;
    2839     17586458 :   tree arg31;
    2840     17586458 :   enum gimple_rhs_class grhs_class;
    2841              : 
    2842     17586458 :   code1 = TREE_CODE (name);
    2843     17586458 :   arg11 = name;
    2844     17586458 :   arg21 = NULL_TREE;
    2845     17586458 :   arg31 = NULL_TREE;
    2846     17586458 :   grhs_class = get_gimple_rhs_class (code1);
    2847              : 
    2848     17586458 :   if (code1 == SSA_NAME)
    2849              :     {
    2850     11696563 :       def = SSA_NAME_DEF_STMT (name);
    2851              : 
    2852     11696563 :       if (def && is_gimple_assign (def)
    2853     18975320 :           && can_propagate_from (def))
    2854              :         {
    2855      4990533 :           code1 = gimple_assign_rhs_code (def);
    2856      4990533 :           arg11 = gimple_assign_rhs1 (def);
    2857      4990533 :           arg21 = gimple_assign_rhs2 (def);
    2858      4990533 :           arg31 = gimple_assign_rhs3 (def);
    2859              :         }
    2860              :     }
    2861      5889895 :   else if (grhs_class != GIMPLE_SINGLE_RHS)
    2862            0 :     code1 = ERROR_MARK;
    2863              : 
    2864     17586458 :   *code = code1;
    2865     17586458 :   *arg1 = arg11;
    2866     17586458 :   if (arg2)
    2867     17569199 :     *arg2 = arg21;
    2868     17586458 :   if (arg31)
    2869         2383 :     *code = ERROR_MARK;
    2870     17586458 : }
    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     10323937 : simplify_rotate (gimple_stmt_iterator *gsi)
    2918              : {
    2919     10323937 :   gimple *stmt = gsi_stmt (*gsi);
    2920     10323937 :   tree arg[2], rtype, rotcnt = NULL_TREE;
    2921     10323937 :   tree def_arg1[2], def_arg2[2];
    2922     10323937 :   enum tree_code def_code[2];
    2923     10323937 :   tree lhs;
    2924     10323937 :   int i;
    2925     10323937 :   bool swapped_p = false;
    2926     10323937 :   gimple *g;
    2927     10323937 :   gimple *def_arg_stmt[2] = { NULL, NULL };
    2928     10323937 :   int wider_prec = 0;
    2929     10323937 :   bool add_masking = false;
    2930              : 
    2931     10323937 :   arg[0] = gimple_assign_rhs1 (stmt);
    2932     10323937 :   arg[1] = gimple_assign_rhs2 (stmt);
    2933     10323937 :   rtype = TREE_TYPE (arg[0]);
    2934              : 
    2935              :   /* Only create rotates in complete modes.  Other cases are not
    2936              :      expanded properly.  */
    2937     10323937 :   if (!INTEGRAL_TYPE_P (rtype)
    2938     10323937 :       || !type_has_mode_precision_p (rtype))
    2939              :     return false;
    2940              : 
    2941     26229828 :   for (i = 0; i < 2; i++)
    2942              :     {
    2943     17486552 :       defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2944     17486552 :       if (TREE_CODE (arg[i]) == SSA_NAME)
    2945     11596657 :         def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
    2946              :     }
    2947              : 
    2948              :   /* Look through narrowing (or same precision) conversions.  */
    2949      7765278 :   if (CONVERT_EXPR_CODE_P (def_code[0])
    2950       977998 :       && CONVERT_EXPR_CODE_P (def_code[1])
    2951       141053 :       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
    2952       116956 :       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
    2953       109328 :       && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
    2954       109328 :          == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
    2955        62762 :       && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
    2956        44436 :       && has_single_use (arg[0])
    2957      8776295 :       && has_single_use (arg[1]))
    2958              :     {
    2959        28629 :       wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
    2960        85887 :       for (i = 0; i < 2; i++)
    2961              :         {
    2962        57258 :           arg[i] = def_arg1[i];
    2963        57258 :           defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2964        57258 :           if (TREE_CODE (arg[i]) == SSA_NAME)
    2965        57258 :             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      8714647 :       i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
    2973      7747608 :       if (CONVERT_EXPR_CODE_P (def_code[i])
    2974       967039 :           && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
    2975        32130 :           && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
    2976        30877 :           && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
    2977      8719613 :           && has_single_use (arg[i]))
    2978              :         {
    2979         2124 :           arg[i] = def_arg1[i];
    2980         2124 :           defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2981         2124 :           if (TREE_CODE (arg[i]) == SSA_NAME)
    2982         2124 :             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      8929818 :   for (i = 0; i < 2; i++)
    2988      8905052 :     if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
    2989              :       return false;
    2990       227416 :     else if (!has_single_use (arg[i]))
    2991              :       return false;
    2992        24766 :   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        20448 :   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        20447 :   if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
    3012        32365 :       || !types_compatible_p (TREE_TYPE (def_arg1[0]),
    3013        11918 :                               TREE_TYPE (def_arg1[1])))
    3014              :     {
    3015         8529 :       if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
    3016         8529 :            != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
    3017         8529 :           || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
    3018         8529 :               == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
    3019         8505 :         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          545 :       i = def_code[0] != RSHIFT_EXPR;
    3024          545 :       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      2004590 : simplify_count_zeroes (gimple_stmt_iterator *gsi)
    3403              : {
    3404      2004590 :   gimple *stmt = gsi_stmt (*gsi);
    3405      2004590 :   tree array_ref = gimple_assign_rhs1 (stmt);
    3406      2004590 :   tree res_ops[3];
    3407              : 
    3408      2004590 :   gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
    3409              : 
    3410      2004590 :   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      2004590 :   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      2004590 :   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      2004567 :   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      2004557 :   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.  The
    3618              :    two names split the gcond polarity: cond_carry_add matches when
    3619              :    the true edge selects (base + pow2), cond_carry_add_neg when the
    3620              :    true edge selects base.  */
    3621              : 
    3622              : extern bool gimple_cond_carry_add (tree, tree *, tree (*)(tree));
    3623              : extern bool gimple_cond_carry_add_neg (tree, tree *, tree (*)(tree));
    3624              : 
    3625              : /* Match.pd functions to match long multiplication.  */
    3626              : 
    3627              : extern bool gimple_mul_hi (tree, tree *, tree (*)(tree));
    3628              : extern bool gimple_mul_lo (tree, tree *, tree (*)(tree));
    3629              : extern bool gimple_mul_hilo (tree, tree *, tree (*)(tree));
    3630              : extern bool gimple_mul_lolo (tree, tree *, tree (*)(tree));
    3631              : extern bool gimple_mul_hihi (tree, tree *, tree (*)(tree));
    3632              : extern bool gimple_mul_cross_sum (tree, tree *, tree (*)(tree));
    3633              : extern bool gimple_mul_low_sum (tree, tree *, tree (*)(tree));
    3634              : extern bool gimple_mul_low_accum (tree, tree *, tree (*)(tree));
    3635              : extern bool gimple_mul_carry_cross_sum (tree, tree *, tree (*)(tree));
    3636              : extern bool gimple_mul_carry_low_sum (tree, tree *, tree (*)(tree));
    3637              : extern bool gimple_mul_carry_low (tree, tree *, tree (*)(tree));
    3638              : extern bool gimple_mul_ladder_sum1 (tree, tree *, tree (*)(tree));
    3639              : extern bool gimple_mul_ladder_sum2 (tree, tree *, tree (*)(tree));
    3640              : extern bool gimple_mul_ladder_sum3 (tree, tree *, tree (*)(tree));
    3641              : extern bool gimple_mul_ladder_part_sum (tree, tree *, tree (*)(tree));
    3642              : 
    3643              : /* Append to SEQ statements assigning DEST the high-part multiply of
    3644              :    OP1 and OP2, emitted as
    3645              :      (N)(((2N) op1 * (2N) op2) >> N).
    3646              :    pass_optimize_widening_mul's convert_mult_to_widen and
    3647              :    convert_mult_to_highpart later rewrite this to a single
    3648              :    WIDEN_MULT_EXPR or MULT_HIGHPART_EXPR when the target supports it,
    3649              :    otherwise the 2N multiply expands directly.  Emitting the canonical
    3650              :    widening shape keeps target-capability decisions in the layer that
    3651              :    already owns them.  */
    3652              : 
    3653              : static void
    3654         1919 : build_mul_high_seq (tree op1, tree op2, tree dest, location_t loc,
    3655              :                     gimple_seq *seq)
    3656              : {
    3657         1919 :   tree op_type = TREE_TYPE (op1);
    3658         1919 :   unsigned int width = TYPE_PRECISION (op_type);
    3659         1919 :   tree wide_type = build_nonstandard_integer_type (width * 2, 1);
    3660              : 
    3661         1919 :   tree wide_a = gimple_convert (seq, loc, wide_type, op1);
    3662         1919 :   tree wide_b = gimple_convert (seq, loc, wide_type, op2);
    3663         1919 :   tree wide_prod = gimple_build (seq, loc, MULT_EXPR, wide_type,
    3664              :                                  wide_a, wide_b);
    3665         1919 :   tree hi = gimple_build (seq, loc, RSHIFT_EXPR, wide_type, wide_prod,
    3666         1919 :                           build_int_cst (integer_type_node, width));
    3667              : 
    3668         1919 :   gimple *prod = gimple_build_assign (dest, NOP_EXPR, hi);
    3669         1919 :   gimple_set_location (prod, loc);
    3670         1919 :   gimple_seq_add_stmt (seq, prod);
    3671         1919 : }
    3672              : 
    3673              : /* Append to SEQ statements combining ACC with each of EXTRAS under
    3674              :    OUTER, the last one assigning to STMT's lhs.  EXTRAS are leaves of
    3675              :    STMT's own chain, so any combining order is valid.  */
    3676              : 
    3677              : static void
    3678          361 : long_mul_apply_extras (tree acc, const vec<tree> &extras, tree_code outer,
    3679              :                        gassign *stmt, gimple_seq *seq)
    3680              : {
    3681          361 :   location_t loc = gimple_location (stmt);
    3682          361 :   tree lhs = gimple_assign_lhs (stmt);
    3683          388 :   for (unsigned i = 0; i + 1 < extras.length (); i++)
    3684           27 :     acc = gimple_build (seq, loc, outer, TREE_TYPE (lhs), acc, extras[i]);
    3685          361 :   gimple *last = gimple_build_assign (lhs, outer, acc, extras.last ());
    3686          361 :   gimple_set_location (last, loc);
    3687          361 :   gimple_seq_add_stmt (seq, last);
    3688          361 : }
    3689              : 
    3690              : /* Replace STMT with a high-part multiply of OP1 and OP2, combining any
    3691              :    EXTRAS back on top under OUTER.  */
    3692              : 
    3693              : static void
    3694         1905 : create_mul_high_seq (tree op1, tree op2, gassign *stmt,
    3695              :                      const vec<tree> &extras, tree_code outer)
    3696              : {
    3697         1905 :   gimple_seq seq = NULL;
    3698         1905 :   tree lhs = gimple_assign_lhs (stmt);
    3699         2266 :   tree dest = extras.is_empty () ? lhs : make_ssa_name (TREE_TYPE (lhs));
    3700         1905 :   build_mul_high_seq (op1, op2, dest, gimple_location (stmt), &seq);
    3701         1905 :   if (!extras.is_empty ())
    3702          361 :     long_mul_apply_extras (dest, extras, outer, stmt, &seq);
    3703         1905 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    3704         1905 :   gsi_replace_with_seq (&gsi, seq, true);
    3705         1905 : }
    3706              : 
    3707              : /* Replace STMT with a low-part multiply of OP1 and OP2, combining any
    3708              :    EXTRAS back on top under OUTER.  */
    3709              : 
    3710              : static void
    3711           32 : create_mul_low_seq (tree op1, tree op2, gassign *stmt,
    3712              :                     const vec<tree> &extras, tree_code outer)
    3713              : {
    3714           32 :   gimple_seq seq = NULL;
    3715           32 :   tree lhs = gimple_assign_lhs (stmt);
    3716           32 :   tree dest = extras.is_empty () ? lhs : make_ssa_name (TREE_TYPE (lhs));
    3717           32 :   gimple *prod = gimple_build_assign (dest, MULT_EXPR, op1, op2);
    3718           32 :   gimple_set_location (prod, gimple_location (stmt));
    3719           32 :   gimple_seq_add_stmt (&seq, prod);
    3720           32 :   if (!extras.is_empty ())
    3721            0 :     long_mul_apply_extras (dest, extras, outer, stmt, &seq);
    3722           32 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    3723           32 :   gsi_replace_with_seq (&gsi, seq, true);
    3724           32 : }
    3725              : 
    3726              : /* Widest match.pd atom (mul_carry_low_sum) takes 7 captures; round up
    3727              :    to 8 for the scratch buffers below.  */
    3728              : static constexpr unsigned LONG_MUL_MAX_CAPTURES = 8;
    3729              : 
    3730              : /* Longest variant in long_mul_table has 4 summands.  */
    3731              : static constexpr unsigned LONG_MUL_MAX_SUMMANDS = 4;
    3732              : 
    3733              : /* Cap on the leaves set aside as not part of the idiom, so an
    3734              :    arbitrarily long unrelated chain still bails early.  */
    3735              : static constexpr unsigned LONG_MUL_MAX_EXTRAS = 4;
    3736              : 
    3737              : enum long_mul_kind {
    3738              :   LMK_MUL_HIHI,
    3739              :   LMK_MUL_LOLO,
    3740              :   LMK_MUL_HILO,
    3741              :   LMK_CROSS_SUM,
    3742              :   LMK_LOW_ACCUM,
    3743              :   LMK_LOW_SUM,
    3744              :   LMK_LADDER_SUM1,
    3745              :   LMK_LADDER_SUM2,
    3746              :   LMK_LADDER_SUM3,
    3747              :   LMK_LADDER_PART_SUM,
    3748              :   LMK_CARRY_LOW,
    3749              :   LMK_CARRY_CROSS_SUM,
    3750              :   LMK_CARRY_LOW_SUM,
    3751              : };
    3752              : 
    3753              : /* How the leaf wraps its inner kind.  Carry kinds use LMX_NONE: their
    3754              :    match.pd pattern bakes the lshift in, so the leaf is already the
    3755              :    complete carry expression.  */
    3756              : 
    3757              : enum long_mul_extract {
    3758              :   LMX_NONE,
    3759              :   LMX_HI,
    3760              :   LMX_LO,
    3761              :   LMX_SHL_N,
    3762              : };
    3763              : 
    3764              : struct long_mul_summand {
    3765              :   long_mul_kind kind;
    3766              :   long_mul_extract extract;
    3767              :   tree op0, op1;
    3768              :   tree hilo0, hilo1, hilo2;
    3769              :   tree carry_a, carry_b;
    3770              :   unsigned HOST_WIDE_INT shift;
    3771              : };
    3772              : 
    3773              : /* Walk the OUTER addition or BIT_IOR chain rooted at STMT and collect
    3774              :    the leaf operands into LEAVES.  Descends through single-use
    3775              :    intermediate stmts of the same code.  Returns false once the leaf
    3776              :    count exceeds LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS, so an
    3777              :    overlong chain bails mid-walk instead of after a full traversal.
    3778              : 
    3779              :    If SHARED_DEF_OUT is non-NULL, record there the first inner stmt that
    3780              :    shares the outer code but has more than one use -- descending into it
    3781              :    would change semantics, so it stays as a leaf.  Such a leaf often
    3782              :    classifies as something no row matches, silently disabling the fold;
    3783              :    the caller surfaces this as a dump-file hint.  */
    3784              : 
    3785              : static bool
    3786      3393896 : long_mul_linearize_chain (gimple *stmt, tree_code outer, vec<tree> &leaves,
    3787              :                           gimple **shared_def_out = NULL)
    3788              : {
    3789      3393896 :   auto_vec<tree, 8> stack;
    3790      3393896 :   stack.safe_push (gimple_assign_rhs2 (stmt));
    3791      3393896 :   stack.safe_push (gimple_assign_rhs1 (stmt));
    3792              : 
    3793     10541542 :   while (!stack.is_empty ())
    3794              :     {
    3795      7148426 :       tree t = stack.pop ();
    3796      7148426 :       if (TREE_CODE (t) == SSA_NAME)
    3797              :         {
    3798      4698668 :           gimple *def = SSA_NAME_DEF_STMT (t);
    3799      4698668 :           if (def
    3800      4698668 :               && is_gimple_assign (def)
    3801      7964597 :               && gimple_assign_rhs_code (def) == outer)
    3802              :             {
    3803       346902 :               if (has_single_use (t))
    3804              :                 {
    3805       182660 :                   stack.safe_push (gimple_assign_rhs2 (def));
    3806       182660 :                   stack.safe_push (gimple_assign_rhs1 (def));
    3807       182660 :                   continue;
    3808              :                 }
    3809       164242 :               if (shared_def_out && !*shared_def_out)
    3810       149467 :                 *shared_def_out = def;
    3811              :             }
    3812              :         }
    3813      6965766 :       leaves.safe_push (t);
    3814      6965766 :       if (leaves.length () > LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS)
    3815          780 :         return false;
    3816              :     }
    3817      6786232 :   return !leaves.is_empty ();
    3818      3393896 : }
    3819              : 
    3820              : /* If EXPR is defined by LSHIFT_EXPR with a uhwi-valued amount, return
    3821              :    the shifted input via *INNER_OUT and the amount via *SHIFT_OUT.  */
    3822              : 
    3823              : static bool
    3824      6891726 : long_mul_is_lshift_def (tree expr, tree *inner_out,
    3825              :                         unsigned HOST_WIDE_INT *shift_out)
    3826              : {
    3827      6891726 :   if (TREE_CODE (expr) != SSA_NAME)
    3828              :     return false;
    3829      4442440 :   gimple *def = SSA_NAME_DEF_STMT (expr);
    3830      4442440 :   if (!def || !is_gimple_assign (def)
    3831      7452893 :       || gimple_assign_rhs_code (def) != LSHIFT_EXPR)
    3832              :     return false;
    3833        84967 :   tree amount = gimple_assign_rhs2 (def);
    3834        84967 :   if (!tree_fits_uhwi_p (amount))
    3835              :     return false;
    3836        58734 :   *inner_out = gimple_assign_rhs1 (def);
    3837        58734 :   *shift_out = tree_to_uhwi (amount);
    3838        58734 :   return true;
    3839              : }
    3840              : 
    3841              : /* Fill INFO's kind plus the captures from RES_OPS that the kind requires.
    3842              :    The kind itself determines how many (op0, op1) and hilo captures to
    3843              :    pick up from RES_OPS, and whether a baked-in shift is present.  */
    3844              : 
    3845              : static void
    3846        55485 : long_mul_set_summand (long_mul_summand *info, long_mul_kind kind,
    3847              :                       const tree *res_ops)
    3848              : {
    3849        55485 :   info->kind = kind;
    3850        55485 :   unsigned n_ops = 0;
    3851        55485 :   unsigned n_hilos = 0;
    3852        55485 :   int shift_idx = -1;
    3853        53038 :   switch (kind)
    3854              :     {
    3855        28316 :     case LMK_MUL_HIHI:
    3856        28316 :     case LMK_MUL_LOLO:
    3857        28316 :     case LMK_MUL_HILO:
    3858        28316 :       n_ops = 2;
    3859        28316 :       break;
    3860         5743 :     case LMK_CROSS_SUM:
    3861         5743 :       n_hilos = 2;
    3862         5743 :       break;
    3863        18568 :     case LMK_LOW_ACCUM:
    3864        18568 :     case LMK_LOW_SUM:
    3865        18568 :     case LMK_LADDER_SUM1:
    3866        18568 :     case LMK_LADDER_SUM2:
    3867        18568 :     case LMK_LADDER_SUM3:
    3868        18568 :       n_ops = 2;
    3869        18568 :       n_hilos = 2;
    3870        18568 :       break;
    3871          135 :     case LMK_LADDER_PART_SUM:
    3872          135 :       n_ops = 2;
    3873          135 :       n_hilos = 1;
    3874          135 :       break;
    3875              :     case LMK_CARRY_CROSS_SUM:
    3876              :       n_hilos = 3;
    3877              :       shift_idx = 3;
    3878              :       break;
    3879              :     case LMK_CARRY_LOW_SUM:
    3880              :       n_ops = 2;
    3881              :       n_hilos = 3;
    3882              :       shift_idx = 5;
    3883              :       break;
    3884         2447 :     case LMK_CARRY_LOW:
    3885         2447 :       info->carry_a = res_ops[0];
    3886         2447 :       info->carry_b = res_ops[1];
    3887            0 :       return;
    3888              :     }
    3889        52762 :   if (n_ops >= 1)
    3890        47125 :     info->op0 = res_ops[0];
    3891        47125 :   if (n_ops >= 2)
    3892        47125 :     info->op1 = res_ops[1];
    3893        53038 :   if (n_hilos >= 1)
    3894        24722 :     info->hilo0 = res_ops[n_ops];
    3895        24722 :   if (n_hilos >= 2)
    3896        24587 :     info->hilo1 = res_ops[n_ops + 1];
    3897        24587 :   if (n_hilos >= 3)
    3898          276 :     info->hilo2 = res_ops[n_ops + 2];
    3899        53038 :   if (shift_idx >= 0)
    3900              :     /* The carry atoms (mul_carry_cross_sum, mul_carry_low_sum) capture the
    3901              :        shift as an INTEGER_CST already checked with tree_fits_uhwi_p, so this
    3902              :        cannot overflow.  */
    3903          276 :     info->shift = tree_to_uhwi (res_ops[shift_idx]);
    3904              : }
    3905              : 
    3906              : /* Classify LEAF as a carry-kind summand.  The lshift amount is baked
    3907              :    into mul_carry_cross_sum / mul_carry_low_sum, so they're tried before
    3908              :    any branch that looks for a generic (X >> N) or (X << N) wrapper.  */
    3909              : 
    3910              : static bool
    3911      6951488 : long_mul_classify_carry (tree leaf, long_mul_summand *info)
    3912              : {
    3913      6951488 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    3914              :   /* mul_carry_low_sum's inner is constrained to mul_low_sum (cross_sum
    3915              :      + mul_hi(mul_lolo)); mul_carry_cross_sum's inner is just
    3916              :      mul_cross_sum (any plus); mul_carry_low matches gt:c (@0, plus(@0,
    3917              :      @1)) without a baked-in shift.  Most specific first, so the
    3918              :      less-constrained pattern doesn't shadow the more-constrained one.  */
    3919      6951488 :   if (gimple_mul_carry_low_sum (leaf, res_ops, NULL))
    3920              :     {
    3921          106 :       long_mul_set_summand (info, LMK_CARRY_LOW_SUM, res_ops);
    3922          106 :       return true;
    3923              :     }
    3924      6951382 :   if (gimple_mul_carry_cross_sum (leaf, res_ops, NULL))
    3925              :     {
    3926          170 :       long_mul_set_summand (info, LMK_CARRY_CROSS_SUM, res_ops);
    3927          170 :       return true;
    3928              :     }
    3929      6951212 :   if (gimple_mul_carry_low (leaf, res_ops, NULL))
    3930              :     {
    3931         2447 :       long_mul_set_summand (info, LMK_CARRY_LOW, res_ops);
    3932         2447 :       return true;
    3933              :     }
    3934              :   return false;
    3935              : }
    3936              : 
    3937              : /* Plus-based summand kinds shared by the (X >> SHIFT) and (X << SHIFT)
    3938              :    classifiers.  Order is by specificity: mul_low_sum's first arm is any
    3939              :    plus, so mul_ladder_sum1/3 (which constrain that arm to a plus
    3940              :    containing a mul_lo) and mul_low_accum (which constrains both arms)
    3941              :    shadow it and must come first.  */
    3942              : 
    3943              : static bool
    3944        84872 : long_mul_classify_plus_kinds (tree inner, long_mul_summand *info)
    3945              : {
    3946        84872 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    3947        84872 :   if (gimple_mul_low_accum (inner, res_ops, NULL))
    3948              :     {
    3949           19 :       long_mul_set_summand (info, LMK_LOW_ACCUM, res_ops);
    3950           19 :       return true;
    3951              :     }
    3952        84853 :   if (gimple_mul_ladder_sum3 (inner, res_ops, NULL))
    3953              :     {
    3954           15 :       long_mul_set_summand (info, LMK_LADDER_SUM3, res_ops);
    3955           15 :       return true;
    3956              :     }
    3957        84838 :   if (gimple_mul_ladder_sum1 (inner, res_ops, NULL))
    3958              :     {
    3959        18214 :       long_mul_set_summand (info, LMK_LADDER_SUM1, res_ops);
    3960        18214 :       return true;
    3961              :     }
    3962        66624 :   if (gimple_mul_low_sum (inner, res_ops, NULL))
    3963              :     {
    3964          230 :       long_mul_set_summand (info, LMK_LOW_SUM, res_ops);
    3965          230 :       return true;
    3966              :     }
    3967        66394 :   if (gimple_mul_ladder_sum2 (inner, res_ops, NULL))
    3968              :     {
    3969           90 :       long_mul_set_summand (info, LMK_LADDER_SUM2, res_ops);
    3970           90 :       return true;
    3971              :     }
    3972              :   return false;
    3973              : }
    3974              : 
    3975              : /* Classify INNER -- already unwrapped from an outer (X >> SHIFT) -- as
    3976              :    a high-half-extracted summand.  mul_hilo (mult-shape) is orthogonal
    3977              :    to the plus-based kinds and is tried first; ladder_part_sum (one arm
    3978              :    unconstrained) and mul_cross_sum (any plus) are the fallbacks after
    3979              :    the shared plus-based ladder.  */
    3980              : 
    3981              : static bool
    3982        35388 : long_mul_classify_hi_extract (tree inner, unsigned HOST_WIDE_INT shift,
    3983              :                               long_mul_summand *info)
    3984              : {
    3985        35388 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    3986        35388 :   info->extract = LMX_HI;
    3987        35388 :   info->shift = shift;
    3988        35388 :   if (gimple_mul_hilo (inner, res_ops, NULL))
    3989              :     {
    3990         9238 :       long_mul_set_summand (info, LMK_MUL_HILO, res_ops);
    3991         9238 :       return true;
    3992              :     }
    3993        26150 :   if (long_mul_classify_plus_kinds (inner, info))
    3994              :     return true;
    3995        16751 :   if (gimple_mul_ladder_part_sum (inner, res_ops, NULL))
    3996              :     {
    3997          135 :       long_mul_set_summand (info, LMK_LADDER_PART_SUM, res_ops);
    3998          135 :       return true;
    3999              :     }
    4000        16616 :   if (gimple_mul_cross_sum (inner, res_ops, NULL))
    4001              :     {
    4002         2808 :       long_mul_set_summand (info, LMK_CROSS_SUM, res_ops);
    4003         2808 :       return true;
    4004              :     }
    4005              :   return false;
    4006              : }
    4007              : 
    4008              : /* Classify INNER -- already unwrapped from an outer (X & MASK) -- as
    4009              :    a low-half-masked summand.  */
    4010              : 
    4011              : static bool
    4012        21665 : long_mul_classify_lo_extract (tree inner, long_mul_summand *info)
    4013              : {
    4014        21665 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4015        21665 :   info->extract = LMX_LO;
    4016        21665 :   if (gimple_mul_lolo (inner, res_ops, NULL))
    4017              :     {
    4018         9329 :       long_mul_set_summand (info, LMK_MUL_LOLO, res_ops);
    4019         9329 :       return true;
    4020              :     }
    4021              :   return false;
    4022              : }
    4023              : 
    4024              : /* Classify INNER -- already unwrapped from an outer (X << SHIFT) -- as
    4025              :    a left-shifted summand.  No mul_hilo / ladder_part_sum here -- those
    4026              :    shapes appear only under (X >> SHIFT).  */
    4027              : 
    4028              : static bool
    4029        58722 : long_mul_classify_shl_extract (tree inner, unsigned HOST_WIDE_INT shift,
    4030              :                                long_mul_summand *info)
    4031              : {
    4032        58722 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4033        58722 :   info->extract = LMX_SHL_N;
    4034        58722 :   info->shift = shift;
    4035        58722 :   if (long_mul_classify_plus_kinds (inner, info))
    4036              :     return true;
    4037        49553 :   if (gimple_mul_cross_sum (inner, res_ops, NULL))
    4038              :     {
    4039         2935 :       long_mul_set_summand (info, LMK_CROSS_SUM, res_ops);
    4040         2935 :       return true;
    4041              :     }
    4042              :   return false;
    4043              : }
    4044              : 
    4045              : /* Classify LEAF as one of the bare-kind summands (no extraction
    4046              :    wrapper): mul_hihi or mul_lolo standing on their own.  */
    4047              : 
    4048              : static bool
    4049      6832990 : long_mul_classify_bare (tree leaf, long_mul_summand *info)
    4050              : {
    4051      6832990 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4052      6832990 :   if (gimple_mul_hihi (leaf, res_ops, NULL))
    4053              :     {
    4054         9686 :       long_mul_set_summand (info, LMK_MUL_HIHI, res_ops);
    4055         9686 :       return true;
    4056              :     }
    4057      6823304 :   if (gimple_mul_lolo (leaf, res_ops, NULL))
    4058              :     {
    4059           63 :       long_mul_set_summand (info, LMK_MUL_LOLO, res_ops);
    4060           63 :       return true;
    4061              :     }
    4062              :   return false;
    4063              : }
    4064              : 
    4065              : /* Classify LEAF as one of the long-multiply summand shapes.  On success,
    4066              :    fill *INFO with the kind, extract, captured operands and shift.
    4067              :    Dispatches to per-extract helpers; the order matters because the
    4068              :    carry kinds bake an lshift into the pattern and would otherwise be
    4069              :    misread by the (X << N) branch.  */
    4070              : 
    4071              : static bool
    4072      6951488 : long_mul_classify_summand (tree leaf, long_mul_summand *info)
    4073              : {
    4074      6951488 :   tree res_ops[LONG_MUL_MAX_CAPTURES];
    4075      6951488 :   *info = {};
    4076              : 
    4077      6951488 :   if (long_mul_classify_carry (leaf, info))
    4078              :     return true;
    4079              : 
    4080      6948765 :   if (gimple_mul_hi (leaf, res_ops, NULL))
    4081        35388 :     return long_mul_classify_hi_extract (res_ops[0],
    4082        35388 :                                          tree_to_uhwi (res_ops[1]), info);
    4083              : 
    4084      6913377 :   if (gimple_mul_lo (leaf, res_ops, NULL))
    4085        21665 :     return long_mul_classify_lo_extract (res_ops[0], info);
    4086              : 
    4087      6891712 :   tree inner;
    4088      6891712 :   unsigned HOST_WIDE_INT shift;
    4089      6891712 :   if (long_mul_is_lshift_def (leaf, &inner, &shift))
    4090        58722 :     return long_mul_classify_shl_extract (inner, shift, info);
    4091              : 
    4092      6832990 :   return long_mul_classify_bare (leaf, info);
    4093              : }
    4094              : 
    4095              : /* qsort comparator: sort summands by (kind, extract) to put a multiset
    4096              :    into canonical order for table lookup.  Unstable sort within a tie is
    4097              :    harmless: no row in long_mul_table pairs distinct subterms under the
    4098              :    same (kind, extract), and long_mul_check_consistency cross-validates
    4099              :    that matching summands share one canonical (op0, op1).  */
    4100              : 
    4101              : static int
    4102       139514 : long_mul_summand_compare (const void *a, const void *b)
    4103              : {
    4104       139514 :   const long_mul_summand *sa = (const long_mul_summand *) a;
    4105       139514 :   const long_mul_summand *sb = (const long_mul_summand *) b;
    4106       139514 :   if (sa->kind != sb->kind)
    4107       138651 :     return (int) sa->kind - (int) sb->kind;
    4108          863 :   return (int) sa->extract - (int) sb->extract;
    4109              : }
    4110              : 
    4111              : /* One row of the long-multiply variant table.  COUNT is how many entries
    4112              :    of SIG carry the row's signature (2 to LONG_MUL_MAX_SUMMANDS); a row
    4113              :    with fewer summands leaves the remaining SIG entries zero-initialized.
    4114              :    Those zeros are not a terminator -- {LMK_MUL_HIHI, LMX_NONE} is itself a
    4115              :    valid signature -- so long_mul_signature_matches is bounded by COUNT,
    4116              :    never by a sentinel entry.  */
    4117              : 
    4118              : struct long_mul_row {
    4119              :   enum long_mul_row_part { HIGH_PART, LOW_PART } part;
    4120              :   tree_code outer;
    4121              :   unsigned char count;
    4122              :   struct {
    4123              :     long_mul_kind kind;
    4124              :     long_mul_extract extract;
    4125              :   } sig[LONG_MUL_MAX_SUMMANDS];
    4126              :   bool (*extra_check) (const vec<long_mul_summand> &, gimple *);
    4127              : };
    4128              : 
    4129              : /* True if (A, B) is the same pair as (OP0, OP1) in either order.  */
    4130              : 
    4131              : static inline bool
    4132        23145 : long_mul_same_ops (tree a, tree b, tree op0, tree op1)
    4133              : {
    4134        32480 :   return (a == op0 && b == op1) || (a == op1 && b == op0);
    4135              : }
    4136              : 
    4137              : /* True if H is a cross-half product of (OP0, OP1) -- gimple_mul_hilo
    4138              :    recognizes it and its captured operands match the pair.  */
    4139              : 
    4140              : static bool
    4141         4374 : long_mul_is_cross_half (tree h, tree op0, tree op1)
    4142              : {
    4143         4374 :   tree scratch[LONG_MUL_MAX_CAPTURES];
    4144         4374 :   return gimple_mul_hilo (h, scratch, NULL)
    4145         4374 :          && long_mul_same_ops (scratch[0], scratch[1], op0, op1);
    4146              : }
    4147              : 
    4148              : /* Orientation of the mul_hilo capture H relative to (OP0, OP1):
    4149              :    returns 0 for high(OP0)*low(OP1), 1 for high(OP1)*low(OP0), or -1
    4150              :    if H does not decompose that way.  A cross-sum of two mul_hilos must
    4151              :    see one of each orientation -- otherwise a doubled factor would fold
    4152              :    to the wrong value.  (In a self-multiply the two orientations
    4153              :    coincide; see the OP0 == OP1 bypass in long_mul_check_consistency.)  */
    4154              : 
    4155              : static int
    4156         4146 : long_mul_hilo_orientation (tree h, tree op0, tree op1)
    4157              : {
    4158         4146 :   tree scratch[LONG_MUL_MAX_CAPTURES];
    4159         4146 :   if (!gimple_mul_hilo (h, scratch, NULL))
    4160              :     return -1;
    4161         4146 :   if (scratch[0] == op0 && scratch[1] == op1)
    4162              :     return 0;
    4163         2073 :   if (scratch[0] == op1 && scratch[1] == op0)
    4164         2073 :     return 1;
    4165              :   return -1;
    4166              : }
    4167              : 
    4168              : /* Find the first summand that carries operand captures, and return its
    4169              :    (op0, op1) pair in *OP0_OUT / *OP1_OUT.  Returns false if no summand
    4170              :    provides them.  */
    4171              : 
    4172              : static bool
    4173         8103 : long_mul_canonical_ops (const vec<long_mul_summand> &summands,
    4174              :                         tree *op0_out, tree *op1_out)
    4175              : {
    4176        24309 :   for (const long_mul_summand &s : summands)
    4177         8103 :     if (s.op0)
    4178              :       {
    4179         8103 :         *op0_out = s.op0;
    4180         8103 :         *op1_out = s.op1;
    4181         8103 :         return true;
    4182              :       }
    4183              :   return false;
    4184              : }
    4185              : 
    4186              : /* Return the first summand in SUMMANDS whose kind matches KIND, or NULL.  */
    4187              : 
    4188              : static const long_mul_summand *
    4189           12 : long_mul_find_summand (const vec<long_mul_summand> &summands,
    4190              :                        long_mul_kind kind)
    4191              : {
    4192           60 :   for (const long_mul_summand &s : summands)
    4193           36 :     if (s.kind == kind)
    4194              :       return &s;
    4195              :   return NULL;
    4196              : }
    4197              : 
    4198              : /* Run the cross-summand validation invariants and return the canonical
    4199              :    (op0, op1).  Returns false unless all summands that carry operands use
    4200              :    the same (op0, op1) pair (in either order), every LMX_HI/LMX_SHL_N shift
    4201              :    equals halfwidth, every captured hilo is a true cross-half product of
    4202              :    (op0, op1), and every cross-half pair (both those inside a single
    4203              :    mul_cross_sum-bearing summand and those spread across separate
    4204              :    LMK_MUL_HILO summands) contains one of each orientation.  */
    4205              : 
    4206              : static bool
    4207         8091 : long_mul_check_consistency (const vec<long_mul_summand> &summands,
    4208              :                    tree *op0_out, tree *op1_out)
    4209              : {
    4210         8091 :   tree op0, op1;
    4211         8091 :   if (!long_mul_canonical_ops (summands, &op0, &op1))
    4212              :     return false;
    4213              : 
    4214         8091 :   tree op_type = TREE_TYPE (op0);
    4215         8091 :   if (!INTEGRAL_TYPE_P (op_type)
    4216         8091 :       || TYPE_PRECISION (op_type) % 2 != 0)
    4217              :     return false;
    4218         8091 :   unsigned int halfwidth = TYPE_PRECISION (op_type) / 2;
    4219              : 
    4220              :   /* Self-multiply (x*x) collapses the two cross-halves onto one value,
    4221              :      so the complementarity constraint is a trivial no-op there.  */
    4222         8091 :   bool need_orient = op0 != op1;
    4223         8091 :   int mul_hilo_orient[2] = { 0, 0 };
    4224              : 
    4225        37049 :   for (const long_mul_summand &s : summands)
    4226              :     {
    4227        18880 :       if (s.op0 && !long_mul_same_ops (s.op0, s.op1, op0, op1))
    4228         6104 :         return false;
    4229        12802 :       if ((s.extract == LMX_HI || s.extract == LMX_SHL_N)
    4230         4626 :           && s.shift != halfwidth)
    4231              :         return false;
    4232        12802 :       tree hilos[3] = { s.hilo0, s.hilo1, s.hilo2 };
    4233        51154 :       for (tree h : hilos)
    4234        38378 :         if (h && !long_mul_is_cross_half (h, op0, op1))
    4235              :           return false;
    4236              : 
    4237        12776 :       if (!need_orient)
    4238            0 :         continue;
    4239              : 
    4240              :       /* The two cross-sum operands are the last two non-null hilos:
    4241              :          (hilo1, hilo2) for the CARRY_*_SUM kinds, (hilo0, hilo1) for
    4242              :          the CROSS_SUM / SUM / ACCUM / LADDER_SUM kinds, none for the
    4243              :          rest.  */
    4244        12776 :       tree a = NULL_TREE;
    4245        12776 :       tree b = NULL_TREE;
    4246        12776 :       if (s.hilo2)
    4247              :         {
    4248           73 :           a = s.hilo1;
    4249           73 :           b = s.hilo2;
    4250              :         }
    4251        12703 :       else if (s.hilo1)
    4252              :         {
    4253         2000 :           a = s.hilo0;
    4254         2000 :           b = s.hilo1;
    4255              :         }
    4256        12776 :       if (a && b
    4257        14849 :           && (long_mul_hilo_orientation (a, op0, op1)
    4258         2073 :               == long_mul_hilo_orientation (b, op0, op1)))
    4259              :         return false;
    4260              : 
    4261              :       /* Two LMK_MUL_HILO summands (the two-hilos ladder form) stand for
    4262              :          the two cross-halves separately; count orientations and require
    4263              :          the pair to be complementary.  s.op0/op1 is already validated to
    4264              :          match (op0, op1) in some order above.  */
    4265        12776 :       if (s.kind == LMK_MUL_HILO && s.op0)
    4266         2519 :         mul_hilo_orient[s.op0 == op1]++;
    4267              :     }
    4268              : 
    4269         1987 :   if (mul_hilo_orient[0] + mul_hilo_orient[1] >= 2
    4270           10 :       && (mul_hilo_orient[0] == 0 || mul_hilo_orient[1] == 0))
    4271              :     return false;
    4272              : 
    4273         1987 :   *op0_out = op0;
    4274         1987 :   *op1_out = op1;
    4275         1987 :   return true;
    4276              : }
    4277              : 
    4278              : /* Compare the (already-sorted) SUMMANDS multiset against ROW.sig.  */
    4279              : 
    4280              : static bool
    4281       124433 : long_mul_signature_matches (const vec<long_mul_summand> &summands,
    4282              :                    const long_mul_row &row)
    4283              : {
    4284       248866 :   if (row.count != summands.length ())
    4285              :     return false;
    4286        75408 :   for (unsigned i = 0; i < row.count; i++)
    4287        67317 :     if (summands[i].kind != row.sig[i].kind
    4288        67317 :         || summands[i].extract != row.sig[i].extract)
    4289              :       return false;
    4290              :   return true;
    4291              : }
    4292              : 
    4293              : /* Extra check for the two-carries high-part row: the LMK_CARRY_LOW summand's
    4294              :    two operands (carry_a, carry_b) must be a (cross_shifted, mul_lolo) pair
    4295              :    consistent with the multiset's canonical (op0, op1).  */
    4296              : 
    4297              : static bool
    4298           12 : long_mul_check_two_carries (const vec<long_mul_summand> &summands,
    4299              :                             gimple *)
    4300              : {
    4301           12 :   tree op0, op1;
    4302           12 :   if (!long_mul_canonical_ops (summands, &op0, &op1))
    4303              :     return false;
    4304           12 :   unsigned int halfwidth = TYPE_PRECISION (TREE_TYPE (op0)) / 2;
    4305              : 
    4306           12 :   const long_mul_summand *cl = long_mul_find_summand (summands, LMK_CARRY_LOW);
    4307           12 :   if (!cl)
    4308              :     return false;
    4309              : 
    4310              :   /* The two carry_low operands must be (cross_shifted, mul_lolo) in either
    4311              :      order.  cross_shifted = LSHIFT_EXPR (mul_cross_sum, halfwidth).  */
    4312           12 :   tree cs = cl->carry_a, lolo = cl->carry_b;
    4313           12 :   tree inner;
    4314           12 :   unsigned HOST_WIDE_INT shift;
    4315           12 :   if (!long_mul_is_lshift_def (cs, &inner, &shift))
    4316              :     {
    4317            2 :       std::swap (cs, lolo);
    4318            2 :       if (!long_mul_is_lshift_def (cs, &inner, &shift))
    4319              :         return false;
    4320              :     }
    4321           12 :   if (shift != halfwidth)
    4322              :     return false;
    4323              : 
    4324           12 :   tree scratch[LONG_MUL_MAX_CAPTURES];
    4325           12 :   if (!gimple_mul_cross_sum (inner, scratch, NULL))
    4326              :     return false;
    4327           36 :   for (int i = 0; i < 2; i++)
    4328           24 :     if (!long_mul_is_cross_half (scratch[i], op0, op1))
    4329              :       return false;
    4330           12 :   if (!gimple_mul_lolo (lolo, scratch, NULL)
    4331           12 :       || !long_mul_same_ops (scratch[0], scratch[1], op0, op1))
    4332              :     return false;
    4333              : 
    4334              :   return true;
    4335              : }
    4336              : 
    4337              : /* The lolo + cross_shifted shape is also the low half of a two-carry
    4338              :    long-multiply, where an unsigned overflow compare against one of
    4339              :    the PLUS operands is the low-carry term consumed by the matching
    4340              :    high-part fold.  Folding to mul_lo here destroys cross_shifted,
    4341              :    which both the compare and the high-part match still need; defer
    4342              :    so the high-part fold runs first.  After it does, the compare is
    4343              :    dead and the surviving lolo + cross_shifted is picked up by this
    4344              :    row in the next forwprop instance.  Returns false to defer.  */
    4345              : 
    4346              : static bool
    4347           51 : long_mul_check_low_plus_defer (const vec<long_mul_summand> &, gimple *stmt)
    4348              : {
    4349              :   /* The PHI entry passes its gphi as the candidate but commits only to
    4350              :      HIGH_PART rows, so a LOW_PART row never folds from there.  Guard the
    4351              :      gimple_assign accessors regardless, so this stays correct if a future
    4352              :      PLUS-shaped row reachable from the PHI path uses it.  */
    4353           51 :   if (!is_gimple_assign (stmt))
    4354              :     return false;
    4355              : 
    4356           51 :   tree lhs = gimple_assign_lhs (stmt);
    4357           51 :   tree rhs1 = gimple_assign_rhs1 (stmt);
    4358           51 :   tree rhs2 = gimple_assign_rhs2 (stmt);
    4359              : 
    4360           51 :   imm_use_iterator iter;
    4361           51 :   gimple *use_stmt;
    4362           79 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    4363              :     {
    4364           64 :       tree cmp_op1 = NULL_TREE, cmp_op2 = NULL_TREE;
    4365           64 :       enum tree_code use_code = ERROR_MARK;
    4366           64 :       if (is_gimple_assign (use_stmt))
    4367              :         {
    4368           42 :           use_code = gimple_assign_rhs_code (use_stmt);
    4369           42 :           cmp_op1 = gimple_assign_rhs1 (use_stmt);
    4370           42 :           cmp_op2 = gimple_assign_rhs2 (use_stmt);
    4371              :         }
    4372           22 :       else if (gcond *cond = dyn_cast<gcond *> (use_stmt))
    4373              :         {
    4374            9 :           use_code = gimple_cond_code (cond);
    4375            9 :           cmp_op1 = gimple_cond_lhs (cond);
    4376            9 :           cmp_op2 = gimple_cond_rhs (cond);
    4377              :         }
    4378           64 :       if (use_code == GT_EXPR || use_code == LT_EXPR
    4379           28 :           || use_code == GE_EXPR || use_code == LE_EXPR)
    4380              :         {
    4381           36 :           tree other = (cmp_op1 == lhs) ? cmp_op2
    4382           23 :                      : (cmp_op2 == lhs) ? cmp_op1 : NULL_TREE;
    4383           36 :           if (other && (other == rhs1 || other == rhs2))
    4384           36 :             return false;
    4385              :         }
    4386           36 :     }
    4387           15 :   return true;
    4388              : }
    4389              : 
    4390              : /* Long-multiply variant table.  Each row enumerates the multiset of
    4391              :    (kind, extract) summands that compose one long-multiply form.  Rows
    4392              :    are sorted by long_mul_summand_compare, matching the input summands'
    4393              :    sort order, so a plain element-wise compare suffices.  Rows describe
    4394              :    unsigned schoolbook expansions on an even-width 2N-bit type split at
    4395              :    half-width N; EXTRA_CHECK carries invariants the (kind, extract)
    4396              :    signature cannot express.
    4397              : 
    4398              :    The formula on each row uses xh, xl, yh, yl for the half-width pieces
    4399              :    of x and y, cross_sum for xh*yl + xl*yh, and hilo for either cross-half
    4400              :    product (consumers validate the operand shape).  */
    4401              : 
    4402              : static const long_mul_row long_mul_table[] = {
    4403              :   /* HIGH-PART folds.  */
    4404              :   /* xh*yh + (low_sum >> N) + ((hilo > low_sum) << N),
    4405              :      low_sum = cross_sum + (xl*yl >> N).  */
    4406              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
    4407              :     { { LMK_MUL_HIHI, LMX_NONE },
    4408              :       { LMK_LOW_SUM, LMX_HI },
    4409              :       { LMK_CARRY_LOW_SUM, LMX_NONE } },
    4410              :     NULL },
    4411              :   /* xh*yh + (low_accum >> N) + (cross_sum >> N) + ((hilo > cross_sum) << N),
    4412              :      low_accum = (xl*yl >> N) + (cross_sum & mask).  */
    4413              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
    4414              :     { { LMK_MUL_HIHI, LMX_NONE },
    4415              :       { LMK_CROSS_SUM, LMX_HI },
    4416              :       { LMK_LOW_ACCUM, LMX_HI },
    4417              :       { LMK_CARRY_CROSS_SUM, LMX_NONE } },
    4418              :     NULL },
    4419              :   /* xh*yh + (cross_sum >> N) + carry_low + ((hilo > cross_sum) << N),
    4420              :      carry_low = (xl*yl + (cross_sum << N)) < (cross_sum << N).  */
    4421              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
    4422              :     { { LMK_MUL_HIHI, LMX_NONE },
    4423              :       { LMK_CROSS_SUM, LMX_HI },
    4424              :       { LMK_CARRY_LOW, LMX_NONE },
    4425              :       { LMK_CARRY_CROSS_SUM, LMX_NONE } },
    4426              :     long_mul_check_two_carries },
    4427              :   /* xh*yh + (hilo >> N) + (ladder_sum1 >> N),
    4428              :      ladder_sum1 = (hilo & mask) + hilo' + (xl*yl >> N),
    4429              :      hilo, hilo' the two cross-half products.  */
    4430              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
    4431              :     { { LMK_MUL_HIHI, LMX_NONE },
    4432              :       { LMK_MUL_HILO, LMX_HI },
    4433              :       { LMK_LADDER_SUM1, LMX_HI } },
    4434              :     NULL },
    4435              :   /* xh*yh + (ladder_sum2 >> N) + (ladder_part_sum >> N),
    4436              :      ladder_part_sum = (xl*yl >> N) + hilo,
    4437              :      ladder_sum2 = (ladder_part_sum & mask) + hilo'.  */
    4438              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 3,
    4439              :     { { LMK_MUL_HIHI, LMX_NONE },
    4440              :       { LMK_LADDER_SUM2, LMX_HI },
    4441              :       { LMK_LADDER_PART_SUM, LMX_HI } },
    4442              :     NULL },
    4443              :   /* xh*yh + (hilo >> N) + (hilo' >> N) + (ladder_sum3 >> N),
    4444              :      ladder_sum3 = (hilo & mask) + (hilo' & mask) + (xl*yl >> N).  */
    4445              :   { long_mul_row::HIGH_PART, PLUS_EXPR, 4,
    4446              :     { { LMK_MUL_HIHI, LMX_NONE },
    4447              :       { LMK_MUL_HILO, LMX_HI },
    4448              :       { LMK_MUL_HILO, LMX_HI },
    4449              :       { LMK_LADDER_SUM3, LMX_HI } },
    4450              :     NULL },
    4451              :   /* LOW-PART folds.  Recover the lower 2N bits from xl*yl plus a
    4452              :      shifted cross-half term.  */
    4453              :   /* xl*yl + (cross_sum << N).  */
    4454              :   { long_mul_row::LOW_PART, PLUS_EXPR, 2,
    4455              :     { { LMK_MUL_LOLO, LMX_NONE },
    4456              :       { LMK_CROSS_SUM, LMX_SHL_N } },
    4457              :     long_mul_check_low_plus_defer },
    4458              :   /* (xl*yl & mask) | (low_accum << N),
    4459              :      low_accum = (xl*yl >> N) + (cross_sum & mask).  */
    4460              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4461              :     { { LMK_MUL_LOLO, LMX_LO },
    4462              :       { LMK_LOW_ACCUM, LMX_SHL_N } },
    4463              :     NULL },
    4464              :   /* (xl*yl & mask) | (low_sum << N),
    4465              :      low_sum = cross_sum + (xl*yl >> N).  */
    4466              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4467              :     { { LMK_MUL_LOLO, LMX_LO },
    4468              :       { LMK_LOW_SUM, LMX_SHL_N } },
    4469              :     NULL },
    4470              :   /* (xl*yl & mask) | (ladder_sum1 << N),
    4471              :      ladder_sum1 as in the high ladder row above.  */
    4472              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4473              :     { { LMK_MUL_LOLO, LMX_LO },
    4474              :       { LMK_LADDER_SUM1, LMX_SHL_N } },
    4475              :     NULL },
    4476              :   /* (xl*yl & mask) | (ladder_sum2 << N),
    4477              :      ladder_sum2 as in the high ladder row above.  */
    4478              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4479              :     { { LMK_MUL_LOLO, LMX_LO },
    4480              :       { LMK_LADDER_SUM2, LMX_SHL_N } },
    4481              :     NULL },
    4482              :   /* (xl*yl & mask) | (ladder_sum3 << N),
    4483              :      ladder_sum3 as in the high ladder-long row above.  */
    4484              :   { long_mul_row::LOW_PART, BIT_IOR_EXPR, 2,
    4485              :     { { LMK_MUL_LOLO, LMX_LO },
    4486              :       { LMK_LADDER_SUM3, LMX_SHL_N } },
    4487              :     NULL },
    4488              : };
    4489              : 
    4490              : /* If a multi-used inner addition (sharing the chain's outer code) blocked
    4491              :    linearization of a long-mul candidate, emit a dump-file hint pointing
    4492              :    at it.  */
    4493              : 
    4494              : static void
    4495        22606 : long_mul_hint_shared_intermediate (gimple *shared_def)
    4496              : {
    4497        22606 :   if (!shared_def || !dump_file || !(dump_flags & TDF_DETAILS))
    4498              :     return;
    4499            0 :   fprintf (dump_file, "long-mul fold rejected: shared intermediate at ");
    4500            0 :   print_gimple_stmt (dump_file, shared_def, 0, TDF_SLIM);
    4501              : }
    4502              : 
    4503              : /* Search long_mul_table for a row whose multiset matches SUMMANDS for
    4504              :    outer kind OUTER on a result of type LHS_TYPE.  CANDIDATE_STMT is
    4505              :    passed to per-row extra_check predicates.  On a hit, returns the
    4506              :    matching row and writes the half-width operands via OUT_OP0/OUT_OP1.
    4507              :    No IR mutation.  */
    4508              : 
    4509              : static const long_mul_row *
    4510        18719 : long_mul_classify_match (const vec<long_mul_summand> &summands,
    4511              :                          tree lhs_type, tree_code outer,
    4512              :                          gimple *candidate_stmt,
    4513              :                          tree *out_op0, tree *out_op1)
    4514              : {
    4515              :   /* HIGH_PART rows emit a 2N-bit multiply that pass_optimize_widening_mul
    4516              :      consumes -- either via WIDEN_MULT_EXPR / MULT_HIGHPART conversion when
    4517              :      the target has a native 2N multiply, or via lower_long_mul_high_chain
    4518              :      when it does not.  LOW_PART rows emit a plain MULT_EXPR.  Emission
    4519              :      needs a 2N mode to exist in the mode table AND the widening_mul pass
    4520              :      to be active: without the pass, the emit could reach RTL expand as an
    4521              :      unexpandable 2N multiply (e.g. OImode).  BITINT_TYPE is
    4522              :      refused -- the long_mul_high_chain atom excludes it.  */
    4523        18719 :   scalar_int_mode mode, wide_mode;
    4524        18719 :   bool can_emit_high
    4525        18719 :     = optimize_widening_mul_active_p ()
    4526        18628 :       && TREE_CODE (lhs_type) != BITINT_TYPE
    4527        37256 :       && is_a <scalar_int_mode> (TYPE_MODE (lhs_type), &mode)
    4528        37347 :       && GET_MODE_2XWIDER_MODE (mode).exists (&wide_mode);
    4529              : 
    4530       225854 :   for (const long_mul_row &row : long_mul_table)
    4531              :     {
    4532       410081 :       if (row.outer != outer
    4533       124979 :           || (row.part == long_mul_row::HIGH_PART && !can_emit_high)
    4534       333519 :           || !long_mul_signature_matches (summands, row))
    4535       207135 :         continue;
    4536              : 
    4537         8091 :       tree op0, op1;
    4538         8091 :       if (!long_mul_check_consistency (summands, &op0, &op1))
    4539         6104 :         continue;
    4540              : 
    4541              :       /* Do not emit the wide chain when an operand is subject to
    4542              :          abnormal coalescing: the widening_mul-side consumers refuse
    4543              :          such operands (see convert_mult_to_widen), which would leave
    4544              :          the chain without a consumer.  */
    4545         1987 :       if (row.part == long_mul_row::HIGH_PART
    4546         1987 :           && ((TREE_CODE (op0) == SSA_NAME
    4547         1919 :                && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
    4548         1919 :               || (TREE_CODE (op1) == SSA_NAME
    4549         1919 :                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
    4550            0 :         continue;
    4551              : 
    4552         1987 :       if (row.extra_check && !row.extra_check (summands, candidate_stmt))
    4553           36 :         continue;
    4554              : 
    4555         1951 :       *out_op0 = op0;
    4556         1951 :       *out_op1 = op1;
    4557         1951 :       return &row;
    4558              :     }
    4559              :   return NULL;
    4560              : }
    4561              : 
    4562              : /* Walk STMT's outer chain (kind OUTER), classify each leaf as a
    4563              :    long-multiply summand, optionally add the already-classified EXTRA,
    4564              :    and look the multiset up in long_mul_table for a result of type
    4565              :    LHS_TYPE.  CANDIDATE is passed to per-row extra_check predicates.
    4566              : 
    4567              :    If EXTRAS_OUT is non-NULL, leaves matching no summand are set aside
    4568              :    there instead of failing the match, and the caller must re-apply
    4569              :    them on top of the folded multiply.  A leaf that does match is
    4570              :    always consumed: if that makes the signature miss every row the
    4571              :    match fails, rather than retrying with the leaf demoted to an extra
    4572              :    (subset search would be exponential).
    4573              : 
    4574              :    Returns the matched row and the half-width operands via
    4575              :    OUT_OP0/OUT_OP1, or NULL on a miss.  No IR mutation.  */
    4576              : 
    4577              : static const long_mul_row *
    4578      3393896 : long_mul_classify_chain (gimple *stmt, tree_code outer, tree lhs_type,
    4579              :                          gimple *candidate, const long_mul_summand *extra,
    4580              :                          vec<tree> *extras_out,
    4581              :                          tree *out_op0, tree *out_op1)
    4582              : {
    4583      3393896 :   auto_vec<tree, LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS> leaves;
    4584      3393896 :   gimple *shared_def = NULL;
    4585      3393896 :   if (!long_mul_linearize_chain (stmt, outer, leaves, &shared_def))
    4586              :     return NULL;
    4587              : 
    4588      3393116 :   auto_vec<long_mul_summand,
    4589      3393116 :            LONG_MUL_MAX_SUMMANDS + LONG_MUL_MAX_EXTRAS + 1> summands;
    4590     17124998 :   for (tree leaf : leaves)
    4591              :     {
    4592      6951488 :       long_mul_summand s;
    4593      6951488 :       if (long_mul_classify_summand (leaf, &s))
    4594        55485 :         summands.quick_push (s);
    4595     13791730 :       else if (extras_out && extras_out->length () < LONG_MUL_MAX_EXTRAS)
    4596      6890165 :         extras_out->safe_push (leaf);
    4597              :       else
    4598              :         {
    4599         5838 :           long_mul_hint_shared_intermediate (shared_def);
    4600         5838 :           return NULL;
    4601              :         }
    4602              :     }
    4603      3387278 :   if (extra)
    4604          354 :     summands.quick_push (*extra);
    4605      6780394 :   if (summands.length () < 2
    4606      3387278 :       || summands.length () > LONG_MUL_MAX_SUMMANDS)
    4607              :     return NULL;
    4608        18719 :   summands.qsort (long_mul_summand_compare);
    4609              : 
    4610        18719 :   const long_mul_row *row
    4611        18719 :     = long_mul_classify_match (summands, lhs_type, outer, candidate,
    4612              :                                out_op0, out_op1);
    4613        18719 :   if (!row)
    4614        16768 :     long_mul_hint_shared_intermediate (shared_def);
    4615              :   return row;
    4616      3393116 : }
    4617              : 
    4618              : /* Top-level entry for long-multiply folding.  Walks STMT's outer
    4619              :    addition or BIT_IOR chain, classifies the summands, and dispatches
    4620              :    to create_mul_high_seq / create_mul_low_seq if the multiset matches
    4621              :    a known long-multiply form.  Returns true on success.  */
    4622              : 
    4623              : static bool
    4624     10185039 : match_long_mul (gassign *stmt)
    4625              : {
    4626     10185039 :   tree_code outer = gimple_assign_rhs_code (stmt);
    4627     10185039 :   if (outer != PLUS_EXPR && outer != BIT_IOR_EXPR)
    4628              :     return false;
    4629              : 
    4630              :   /* Skip non-candidate adds (signed, pointer, odd-width) before walking the
    4631              :      chain.  No legitimate long-mul leaf has a type the atoms would reject.
    4632              :      This just avoids the linearize/classify work on every other PLUS/IOR.  */
    4633     10185039 :   tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
    4634     10185039 :   if (!INTEGRAL_TYPE_P (lhs_type)
    4635      8896937 :       || !TYPE_UNSIGNED (lhs_type)
    4636     14003967 :       || TYPE_PRECISION (lhs_type) % 2 != 0)
    4637              :     return false;
    4638              : 
    4639              :   /* Only start at the end of a chain: a consumer with the same code
    4640              :      linearizes through this statement anyway, so starting here is
    4641              :      redundant.  A consumer in another block does not count -- folding
    4642              :      at the later use could sink a loop-invariant multiply into a
    4643              :      loop.  */
    4644      3566473 :   use_operand_p use_p;
    4645      3566473 :   gimple *use_stmt;
    4646      3566473 :   if (single_imm_use (gimple_assign_lhs (stmt), &use_p, &use_stmt)
    4647      2513559 :       && is_gimple_assign (use_stmt)
    4648      1375341 :       && gimple_assign_rhs_code (use_stmt) == outer
    4649      3747318 :       && gimple_bb (use_stmt) == gimple_bb (stmt))
    4650              :     return false;
    4651              : 
    4652      3393266 :   auto_vec<tree, LONG_MUL_MAX_EXTRAS> extras;
    4653      3393266 :   tree op0, op1;
    4654      3393266 :   const long_mul_row *row
    4655      3393266 :     = long_mul_classify_chain (stmt, outer, lhs_type, stmt, NULL, &extras,
    4656              :                                &op0, &op1);
    4657      3393266 :   if (!row)
    4658              :     return false;
    4659              : 
    4660         1937 :   if (row->part == long_mul_row::HIGH_PART)
    4661              :     {
    4662         1905 :       create_mul_high_seq (op0, op1, stmt, extras, outer);
    4663         1905 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4664           32 :         fprintf (dump_file, "Long multiplication high part folded.\n");
    4665              :       return true;
    4666              :     }
    4667           32 :   create_mul_low_seq (op0, op1, stmt, extras, outer);
    4668           32 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4669            8 :     fprintf (dump_file, "Long multiplication low part folded.\n");
    4670              :   return true;
    4671      3393266 : }
    4672              : 
    4673              : /* PHI-driven entry for long-multiply folding.  When PHI's value
    4674              :    flattens to base + (carry << N), probe sum to classify the carry
    4675              :    kind, linearize base for the remaining high-part summands, and run
    4676              :    the long-multiply table.  On a hit, emit a 2N-bit multiply at the
    4677              :    top of the join block with PHI_RES as its LHS and remove the PHI.
    4678              :    Otherwise leave the IR untouched.  Only HIGH_PART rows are
    4679              :    reachable.  LOW_PART rows are BIT_IOR-shaped and never produce a
    4680              :    carry PHI.  */
    4681              : 
    4682              : static bool
    4683      8774871 : match_long_mul_phi (gphi *phi)
    4684              : {
    4685      8774871 :   tree phi_res = gimple_phi_result (phi);
    4686      8774871 :   tree lhs_type = TREE_TYPE (phi_res);
    4687      6209618 :   if (!INTEGRAL_TYPE_P (lhs_type) || !TYPE_UNSIGNED (lhs_type)
    4688     11540895 :       || TYPE_PRECISION (lhs_type) % 2 != 0)
    4689              :     return false;
    4690              : 
    4691      2004052 :   tree cca_ops[4];
    4692      2004052 :   if (!gimple_cond_carry_add (phi_res, cca_ops, NULL)
    4693      2004052 :       && !gimple_cond_carry_add_neg (phi_res, cca_ops, NULL))
    4694              :     return false;
    4695        19087 :   tree cmp_lhs = cca_ops[0];
    4696        19087 :   tree sum = cca_ops[1];
    4697        19087 :   tree base = cca_ops[2];
    4698              : 
    4699              :   /* Classify sum and populate the carry summand directly.  Most
    4700              :      specific first, mirroring long_mul_classify_carry's order.  */
    4701        19087 :   long_mul_summand carry = {};
    4702        19087 :   tree sum_ops[LONG_MUL_MAX_CAPTURES];
    4703        19087 :   unsigned HOST_WIDE_INT shift_amt
    4704        19087 :     = wi::exact_log2 (wi::to_wide (cca_ops[3]));
    4705        19087 :   unsigned HOST_WIDE_INT halfwidth = TYPE_PRECISION (lhs_type) / 2;
    4706        19087 :   carry.shift = shift_amt;
    4707              : 
    4708        19087 :   if (gimple_mul_low_sum (sum, sum_ops, NULL)
    4709        19087 :       && shift_amt == halfwidth)
    4710              :     {
    4711              :       /* mul_carry_low_sum's flat form ties the outer lshift amount to
    4712              :          the inner mul_hi's INTEGER_CST@0 via match.pd capture re-use;
    4713              :          the PHI form has no such tie, so gate on shift_amt explicitly.  */
    4714           14 :       carry.kind = LMK_CARRY_LOW_SUM;
    4715           14 :       carry.op0 = sum_ops[0];
    4716           14 :       carry.op1 = sum_ops[1];
    4717           14 :       carry.hilo0 = cmp_lhs;
    4718           14 :       carry.hilo1 = sum_ops[2];
    4719           14 :       carry.hilo2 = sum_ops[3];
    4720              :     }
    4721        19073 :   else if (gimple_mul_cross_sum (sum, sum_ops, NULL)
    4722        19073 :            && shift_amt == halfwidth)
    4723              :     {
    4724              :       /* mul_cross_sum is just (plus:c @0 @1) with no half-width
    4725              :          constraint.  Gate here to mirror mul_carry_cross_sum;
    4726              :          a mismatch falls through to the LMK_CARRY_LOW branch.  */
    4727            1 :       carry.kind = LMK_CARRY_CROSS_SUM;
    4728            1 :       carry.hilo0 = cmp_lhs;
    4729            1 :       carry.hilo1 = sum_ops[0];
    4730            1 :       carry.hilo2 = sum_ops[1];
    4731              :     }
    4732        19072 :   else if (shift_amt == 0 && TREE_CODE (sum) == SSA_NAME)
    4733              :     {
    4734        17947 :       gimple *def = SSA_NAME_DEF_STMT (sum);
    4735        17947 :       if (!is_gimple_assign (def)
    4736        17947 :           || gimple_assign_rhs_code (def) != PLUS_EXPR)
    4737              :         return false;
    4738         4734 :       tree p1 = gimple_assign_rhs1 (def);
    4739         4734 :       tree p2 = gimple_assign_rhs2 (def);
    4740         4734 :       if (p1 != cmp_lhs && p2 != cmp_lhs)
    4741              :         return false;
    4742         4009 :       carry.kind = LMK_CARRY_LOW;
    4743         4009 :       carry.carry_a = cmp_lhs;
    4744         4009 :       carry.carry_b = p1 == cmp_lhs ? p2 : p1;
    4745         4009 :     }
    4746              :   else
    4747              :     return false;
    4748              : 
    4749              :   /* Linearize base, the rest of the high-part chain.  */
    4750         4024 :   if (TREE_CODE (base) != SSA_NAME)
    4751              :     return false;
    4752         4024 :   gimple *base_def = SSA_NAME_DEF_STMT (base);
    4753         4024 :   if (!is_gimple_assign (base_def)
    4754         4024 :       || gimple_assign_rhs_code (base_def) != PLUS_EXPR)
    4755              :     return false;
    4756              : 
    4757          630 :   tree op0, op1;
    4758          630 :   const long_mul_row *row
    4759          630 :     = long_mul_classify_chain (base_def, PLUS_EXPR, lhs_type, phi, &carry,
    4760              :                                NULL, &op0, &op1);
    4761          630 :   if (!row || row->part != long_mul_row::HIGH_PART)
    4762              :     return false;
    4763              : 
    4764           14 :   gimple_seq seq = NULL;
    4765           14 :   build_mul_high_seq (op0, op1, phi_res, gimple_location (phi), &seq);
    4766           14 :   gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (phi));
    4767           14 :   gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
    4768           14 :   gimple_stmt_iterator psi = gsi_for_stmt (phi);
    4769           14 :   remove_phi_node (&psi, false);
    4770           14 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4771            4 :     fprintf (dump_file,
    4772              :              "Long multiplication high part folded (carry PHI).\n");
    4773              :   return true;
    4774              : }
    4775              : 
    4776              : /* Determine whether applying the 2 permutations (mask1 then mask2)
    4777              :    gives back one of the input.  */
    4778              : 
    4779              : static int
    4780           42 : is_combined_permutation_identity (tree mask1, tree mask2)
    4781              : {
    4782           42 :   tree mask;
    4783           42 :   unsigned HOST_WIDE_INT nelts, i, j;
    4784           42 :   bool maybe_identity1 = true;
    4785           42 :   bool maybe_identity2 = true;
    4786              : 
    4787           42 :   gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
    4788              :                        && TREE_CODE (mask2) == VECTOR_CST);
    4789              : 
    4790              :   /* For VLA masks, check for the following pattern:
    4791              :      v1 = VEC_PERM_EXPR (v0, ..., mask1)
    4792              :      v2 = VEC_PERM_EXPR (v1, ..., mask2)
    4793              :      -->
    4794              :      v2 = v0
    4795              :      if mask1 == mask2 == {nelts - 1, nelts - 2, ...}.  */
    4796              : 
    4797           42 :   if (operand_equal_p (mask1, mask2, 0)
    4798           42 :       && !VECTOR_CST_NELTS (mask1).is_constant ())
    4799              :     {
    4800              :       vec_perm_builder builder;
    4801              :       if (tree_to_vec_perm_builder (&builder, mask1))
    4802              :         {
    4803              :           poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1));
    4804              :           vec_perm_indices sel (builder, 1, nelts);
    4805              :           if (sel.series_p (0, 1, nelts - 1, -1))
    4806              :             return 1;
    4807              :         }
    4808              :     }
    4809              : 
    4810           42 :   mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
    4811           42 :   if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
    4812              :     return 0;
    4813              : 
    4814           42 :   if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
    4815              :     return 0;
    4816           72 :   for (i = 0; i < nelts; i++)
    4817              :     {
    4818           72 :       tree val = VECTOR_CST_ELT (mask, i);
    4819           72 :       gcc_assert (TREE_CODE (val) == INTEGER_CST);
    4820           72 :       j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
    4821           72 :       if (j == i)
    4822              :         maybe_identity2 = false;
    4823           55 :       else if (j == i + nelts)
    4824              :         maybe_identity1 = false;
    4825              :       else
    4826              :         return 0;
    4827              :     }
    4828            0 :   return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
    4829              : }
    4830              : 
    4831              : /* Combine a shuffle with its arguments.  Returns true if there were any
    4832              :    changes made.  */
    4833              : 
    4834              : static bool
    4835       191420 : simplify_permutation (gimple_stmt_iterator *gsi)
    4836              : {
    4837       191420 :   gimple *stmt = gsi_stmt (*gsi);
    4838       191420 :   gimple *def_stmt = NULL;
    4839       191420 :   tree op0, op1, op2, op3, arg0, arg1;
    4840       191420 :   enum tree_code code, code2 = ERROR_MARK;
    4841       191420 :   bool single_use_op0 = false;
    4842              : 
    4843       191420 :   gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
    4844              : 
    4845       191420 :   op0 = gimple_assign_rhs1 (stmt);
    4846       191420 :   op1 = gimple_assign_rhs2 (stmt);
    4847       191420 :   op2 = gimple_assign_rhs3 (stmt);
    4848              : 
    4849       191420 :   if (TREE_CODE (op2) != VECTOR_CST)
    4850              :     return false;
    4851              : 
    4852       188651 :   if (TREE_CODE (op0) == VECTOR_CST)
    4853              :     {
    4854              :       code = VECTOR_CST;
    4855              :       arg0 = op0;
    4856              :     }
    4857       186778 :   else if (TREE_CODE (op0) == SSA_NAME)
    4858              :     {
    4859       186778 :       def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
    4860       186778 :       if (!def_stmt)
    4861              :         return false;
    4862       178420 :       code = gimple_assign_rhs_code (def_stmt);
    4863       178420 :       if (code == VIEW_CONVERT_EXPR)
    4864              :         {
    4865         1625 :           tree rhs = gimple_assign_rhs1 (def_stmt);
    4866         1625 :           tree name = TREE_OPERAND (rhs, 0);
    4867         1625 :           if (TREE_CODE (name) != SSA_NAME)
    4868              :             return false;
    4869         1625 :           if (!has_single_use (name))
    4870          246 :             single_use_op0 = false;
    4871              :           /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
    4872              :              but still keep the code to indicate it comes from
    4873              :              VIEW_CONVERT_EXPR.  */
    4874         1625 :           def_stmt = SSA_NAME_DEF_STMT (name);
    4875         1625 :           if (!def_stmt || !is_gimple_assign (def_stmt))
    4876              :             return false;
    4877          838 :           if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
    4878              :             return false;
    4879              :         }
    4880       177061 :       if (!can_propagate_from (def_stmt))
    4881              :         return false;
    4882        24652 :       arg0 = gimple_assign_rhs1 (def_stmt);
    4883              :     }
    4884              :   else
    4885              :     return false;
    4886              : 
    4887              :   /* Two consecutive shuffles.  */
    4888        24652 :   if (code == VEC_PERM_EXPR)
    4889              :     {
    4890         6662 :       tree orig;
    4891         6662 :       int ident;
    4892              : 
    4893         6662 :       if (op0 != op1)
    4894              :         return false;
    4895           42 :       op3 = gimple_assign_rhs3 (def_stmt);
    4896           42 :       if (TREE_CODE (op3) != VECTOR_CST)
    4897              :         return false;
    4898           42 :       ident = is_combined_permutation_identity (op3, op2);
    4899           42 :       if (!ident)
    4900              :         return false;
    4901            0 :       orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
    4902            0 :                           : gimple_assign_rhs2 (def_stmt);
    4903            0 :       gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
    4904            0 :       gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
    4905            0 :       gimple_set_num_ops (stmt, 2);
    4906            0 :       update_stmt (stmt);
    4907            0 :       remove_prop_source_from_use (op0);
    4908            0 :       return true;
    4909              :     }
    4910        19863 :   else if (code == CONSTRUCTOR
    4911        19863 :            || code == VECTOR_CST
    4912              :            || code == VIEW_CONVERT_EXPR)
    4913              :     {
    4914         4665 :       if (op0 != op1)
    4915              :         {
    4916         4481 :           if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
    4917              :             return false;
    4918              : 
    4919         3826 :           if (TREE_CODE (op1) == VECTOR_CST)
    4920              :             arg1 = op1;
    4921         3193 :           else if (TREE_CODE (op1) == SSA_NAME)
    4922              :             {
    4923         3193 :               gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
    4924         3193 :               if (!def_stmt2)
    4925              :                 return false;
    4926         1674 :               code2 = gimple_assign_rhs_code (def_stmt2);
    4927         1674 :               if (code2 == VIEW_CONVERT_EXPR)
    4928              :                 {
    4929            0 :                   tree rhs = gimple_assign_rhs1 (def_stmt2);
    4930            0 :                   tree name = TREE_OPERAND (rhs, 0);
    4931            0 :                   if (TREE_CODE (name) != SSA_NAME)
    4932              :                     return false;
    4933            0 :                   if (!has_single_use (name))
    4934              :                     return false;
    4935            0 :                   def_stmt2 = SSA_NAME_DEF_STMT (name);
    4936            0 :                   if (!def_stmt2 || !is_gimple_assign (def_stmt2))
    4937              :                     return false;
    4938            0 :                   if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
    4939              :                     return false;
    4940              :                 }
    4941         1674 :               else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
    4942              :                 return false;
    4943         1485 :               if (!can_propagate_from (def_stmt2))
    4944              :                 return false;
    4945         1485 :               arg1 = gimple_assign_rhs1 (def_stmt2);
    4946              :             }
    4947              :           else
    4948              :             return false;
    4949              :         }
    4950              :       else
    4951              :         {
    4952              :           /* Already used twice in this statement.  */
    4953          184 :           if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
    4954              :             return false;
    4955              :           arg1 = arg0;
    4956              :         }
    4957              : 
    4958              :       /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
    4959              :          operands source, check whether it's valid to transform and prepare
    4960              :          the required new operands.  */
    4961         2234 :       if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
    4962              :         {
    4963              :           /* Figure out the target vector type to which operands should be
    4964              :              converted.  If both are CONSTRUCTOR, the types should be the
    4965              :              same, otherwise, use the one of CONSTRUCTOR.  */
    4966           24 :           tree tgt_type = NULL_TREE;
    4967           24 :           if (code == VIEW_CONVERT_EXPR)
    4968              :             {
    4969           24 :               gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
    4970           24 :               code = CONSTRUCTOR;
    4971           24 :               tgt_type = TREE_TYPE (arg0);
    4972              :             }
    4973           24 :           if (code2 == VIEW_CONVERT_EXPR)
    4974              :             {
    4975            0 :               tree arg1_type = TREE_TYPE (arg1);
    4976            0 :               if (tgt_type == NULL_TREE)
    4977              :                 tgt_type = arg1_type;
    4978            0 :               else if (tgt_type != arg1_type)
    4979           23 :                 return false;
    4980              :             }
    4981              : 
    4982           24 :           if (!VECTOR_TYPE_P (tgt_type))
    4983              :             return false;
    4984           24 :           tree op2_type = TREE_TYPE (op2);
    4985              : 
    4986              :           /* Figure out the shrunk factor.  */
    4987           24 :           poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
    4988           24 :           poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
    4989           24 :           if (maybe_gt (tgt_units, op2_units))
    4990              :             return false;
    4991           24 :           unsigned int factor;
    4992           47 :           if (!constant_multiple_p (op2_units, tgt_units, &factor))
    4993              :             return false;
    4994              : 
    4995              :           /* Build the new permutation control vector as target vector.  */
    4996           24 :           vec_perm_builder builder;
    4997           24 :           if (!tree_to_vec_perm_builder (&builder, op2))
    4998              :             return false;
    4999           24 :           vec_perm_indices indices (builder, 2, op2_units);
    5000           24 :           vec_perm_indices new_indices;
    5001           24 :           if (new_indices.new_shrunk_vector (indices, factor))
    5002              :             {
    5003            1 :               tree mask_type = tgt_type;
    5004            1 :               if (!VECTOR_INTEGER_TYPE_P (mask_type))
    5005              :                 {
    5006            0 :                   tree elem_type = TREE_TYPE (mask_type);
    5007            0 :                   unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
    5008            0 :                   tree int_type = build_nonstandard_integer_type (elem_size, 0);
    5009            0 :                   mask_type = build_vector_type (int_type, tgt_units);
    5010              :                 }
    5011            1 :               op2 = vec_perm_indices_to_tree (mask_type, new_indices);
    5012              :             }
    5013              :           else
    5014           23 :             return false;
    5015              : 
    5016              :           /* Convert the VECTOR_CST to the appropriate vector type.  */
    5017            1 :           if (tgt_type != TREE_TYPE (arg0))
    5018            0 :             arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
    5019            1 :           else if (tgt_type != TREE_TYPE (arg1))
    5020            0 :             arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
    5021           47 :         }
    5022              : 
    5023              :       /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before.  */
    5024         2211 :       gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
    5025              : 
    5026              :       /* Shuffle of a constructor.  */
    5027         2211 :       tree res_type
    5028         2211 :         = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)),
    5029         2211 :                              TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)));
    5030         2211 :       tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
    5031         2211 :       if (!opt
    5032          280 :           || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
    5033              :         return false;
    5034              :       /* Found VIEW_CONVERT_EXPR before, need one explicit conversion.  */
    5035          280 :       if (res_type != TREE_TYPE (op0))
    5036              :         {
    5037            1 :           tree name = make_ssa_name (TREE_TYPE (opt));
    5038            1 :           gimple *ass_stmt = gimple_build_assign (name, opt);
    5039            1 :           gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
    5040            1 :           opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
    5041              :         }
    5042          280 :       gimple_assign_set_rhs_from_tree (gsi, opt);
    5043          280 :       update_stmt (gsi_stmt (*gsi));
    5044          280 :       if (TREE_CODE (op0) == SSA_NAME)
    5045            1 :         remove_prop_source_from_use (op0);
    5046          280 :       if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
    5047            0 :         remove_prop_source_from_use (op1);
    5048              :       return true;
    5049              :     }
    5050              : 
    5051              :   return false;
    5052              : }
    5053              : 
    5054              : /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
    5055              :    conversions with code CONV_CODE or update it if still ERROR_MARK.
    5056              :    Return NULL_TREE if no such matching def was found.  */
    5057              : 
    5058              : static tree
    5059       436033 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
    5060              : {
    5061       436033 :   if (TREE_CODE (val) != SSA_NAME)
    5062              :     return NULL_TREE ;
    5063       406071 :   gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
    5064       406071 :   if (!def_stmt)
    5065              :     return NULL_TREE;
    5066       325309 :   enum tree_code code = gimple_assign_rhs_code (def_stmt);
    5067       325309 :   if (code == FLOAT_EXPR
    5068       325309 :       || code == FIX_TRUNC_EXPR
    5069              :       || CONVERT_EXPR_CODE_P (code))
    5070              :     {
    5071       187717 :       tree op1 = gimple_assign_rhs1 (def_stmt);
    5072       187717 :       if (conv_code == ERROR_MARK)
    5073        90112 :         conv_code = code;
    5074        97605 :       else if (conv_code != code)
    5075              :         return NULL_TREE;
    5076       187692 :       if (TREE_CODE (op1) != SSA_NAME)
    5077              :         return NULL_TREE;
    5078        79074 :       def_stmt = SSA_NAME_DEF_STMT (op1);
    5079        79074 :       if (! is_gimple_assign (def_stmt))
    5080              :         return NULL_TREE;
    5081        63358 :       code = gimple_assign_rhs_code (def_stmt);
    5082              :     }
    5083       200950 :   if (code != BIT_FIELD_REF)
    5084              :     return NULL_TREE;
    5085        24445 :   return gimple_assign_rhs1 (def_stmt);
    5086              : }
    5087              : 
    5088              : /* Recognize a VEC_PERM_EXPR.  Returns true if there were any changes.  */
    5089              : 
    5090              : static bool
    5091       167858 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
    5092              : {
    5093       167858 :   gimple *stmt = gsi_stmt (*gsi);
    5094       167858 :   tree op, orig[2], type;
    5095       167858 :   unsigned i;
    5096       167858 :   unsigned HOST_WIDE_INT nelts;
    5097       167858 :   unsigned HOST_WIDE_INT refnelts;
    5098       167858 :   enum tree_code conv_code;
    5099       167858 :   constructor_elt *elt;
    5100              : 
    5101       167858 :   op = gimple_assign_rhs1 (stmt);
    5102       167858 :   type = TREE_TYPE (op);
    5103       167858 :   gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
    5104              :                        && TREE_CODE (type) == VECTOR_TYPE);
    5105              : 
    5106       167858 :   if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
    5107              :     return false;
    5108              : 
    5109       167858 :   orig[0] = NULL;
    5110       167858 :   orig[1] = NULL;
    5111       167858 :   tree orig_elem_type[2] = {};
    5112       167858 :   conv_code = ERROR_MARK;
    5113       167858 :   bool maybe_ident = true;
    5114       167858 :   bool maybe_blend[2] = { true, true };
    5115       167858 :   tree one_constant = NULL_TREE;
    5116       167858 :   tree one_nonconstant = NULL_TREE;
    5117       167858 :   tree subelt;
    5118       167858 :   auto_vec<tree> constants;
    5119       167858 :   constants.safe_grow_cleared (nelts, true);
    5120       167858 :   auto_vec<std::pair<unsigned, unsigned>, 64> elts;
    5121       167858 :   unsigned int tsubelts = 0;
    5122       469996 :   FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
    5123              :     {
    5124       436033 :       tree ref, op1;
    5125       436033 :       unsigned int elem, src_elem_size;
    5126       436033 :       unsigned HOST_WIDE_INT nsubelts = 1;
    5127              : 
    5128       436033 :       if (i >= nelts)
    5129       167858 :         return false;
    5130              : 
    5131              :       /* Look for elements extracted and possibly converted from
    5132              :          another vector.  */
    5133       436033 :       op1 = get_bit_field_ref_def (elt->value, conv_code);
    5134       441422 :       if (op1
    5135        24445 :           && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
    5136         6084 :           && VECTOR_TYPE_P (TREE_TYPE (ref))
    5137         6069 :           && (tree_nop_conversion_p (TREE_TYPE (op1),
    5138         6069 :                                      TREE_TYPE (TREE_TYPE (ref)))
    5139          813 :               || (VECTOR_TYPE_P (TREE_TYPE (op1))
    5140          133 :                   && tree_nop_conversion_p (TREE_TYPE (TREE_TYPE (op1)),
    5141          133 :                                             TREE_TYPE (TREE_TYPE (ref)))
    5142          133 :                   && TYPE_VECTOR_SUBPARTS (TREE_TYPE (op1))
    5143          133 :                         .is_constant (&nsubelts)))
    5144         5389 :           && constant_multiple_p (bit_field_size (op1), nsubelts,
    5145              :                                   &src_elem_size)
    5146       441422 :           && constant_multiple_p (bit_field_offset (op1), src_elem_size, &elem)
    5147       441422 :           && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
    5148              :         {
    5149              :           unsigned int j;
    5150         5759 :           for (j = 0; j < 2; ++j)
    5151              :             {
    5152         5730 :               if (!orig[j])
    5153              :                 {
    5154         2456 :                   if (j == 0
    5155         2658 :                       || useless_type_conversion_p (TREE_TYPE (orig[0]),
    5156          202 :                                                     TREE_TYPE (ref)))
    5157              :                     break;
    5158              :                 }
    5159         3274 :               else if (ref == orig[j])
    5160              :                 break;
    5161              :             }
    5162              :           /* Found a suitable vector element.  */
    5163         5389 :           if (j < 2)
    5164              :             {
    5165         5360 :               orig[j] = ref;
    5166              :               /* Track what element type was actually extracted (which may
    5167              :                  differ in signedness from the vector's element type due to
    5168              :                  tree_nop_conversion_p).  */
    5169         5360 :               if (!orig_elem_type[j])
    5170         2452 :                 orig_elem_type[j] = TREE_TYPE (op1);
    5171         5360 :               if (elem != i || j != 0)
    5172         2221 :                 maybe_ident = false;
    5173         5360 :               if (elem != i)
    5174         2160 :                 maybe_blend[j] = false;
    5175        10979 :               for (unsigned int k = 0; k < nsubelts; ++k)
    5176         5619 :                 elts.safe_push (std::make_pair (j, elem + k));
    5177         5360 :               tsubelts += nsubelts;
    5178         5360 :               continue;
    5179         5360 :             }
    5180              :           /* Else fallthru.  */
    5181              :         }
    5182              :       /* Handle elements not extracted from a vector.
    5183              :           1. constants by permuting with constant vector
    5184              :           2. a unique non-constant element by permuting with a splat vector  */
    5185       430673 :       if (orig[1]
    5186       264007 :           && orig[1] != error_mark_node)
    5187              :         return false;
    5188       430644 :       orig[1] = error_mark_node;
    5189       430644 :       if (VECTOR_TYPE_P (TREE_TYPE (elt->value))
    5190       430644 :           && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (elt->value))
    5191         5734 :                         .is_constant (&nsubelts))
    5192              :         return false;
    5193       430644 :       if (CONSTANT_CLASS_P (elt->value))
    5194              :         {
    5195        29958 :           if (one_nonconstant)
    5196              :             return false;
    5197        20973 :           if (!one_constant)
    5198         9515 :             one_constant = TREE_CODE (elt->value) == VECTOR_CST
    5199         9515 :                            ? VECTOR_CST_ELT (elt->value, 0)
    5200              :                            : elt->value;
    5201        20973 :           if (TREE_CODE (elt->value) == VECTOR_CST)
    5202              :             {
    5203          687 :               for (unsigned int k = 0; k < nsubelts; k++)
    5204          507 :                 constants[tsubelts + k] = VECTOR_CST_ELT (elt->value, k);
    5205              :             }
    5206              :           else
    5207        20793 :             constants[tsubelts] = elt->value;
    5208              :         }
    5209              :       else
    5210              :         {
    5211       400686 :           if (one_constant)
    5212              :             return false;
    5213       391626 :           subelt = VECTOR_TYPE_P (TREE_TYPE (elt->value))
    5214       391626 :                    ? ssa_uniform_vector_p (elt->value)
    5215              :                    : elt->value;
    5216       391626 :           if (!subelt)
    5217              :             return false;
    5218       386314 :           if (!one_nonconstant)
    5219              :             one_nonconstant = subelt;
    5220       234475 :           else if (!operand_equal_p (one_nonconstant, subelt, 0))
    5221              :             return false;
    5222              :         }
    5223       593885 :       for (unsigned int k = 0; k < nsubelts; ++k)
    5224       297107 :         elts.safe_push (std::make_pair (1, tsubelts + k));
    5225       296778 :       tsubelts += nsubelts;
    5226       296778 :       maybe_ident = false;
    5227              :     }
    5228              : 
    5229        67926 :   if (elts.length () < nelts)
    5230              :     return false;
    5231              : 
    5232        32791 :   if (! orig[0]
    5233        32791 :       || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
    5234              :     return false;
    5235         1631 :   refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
    5236              :   /* We currently do not handle larger destination vectors.  */
    5237         1631 :   if (refnelts < nelts)
    5238              :     return false;
    5239              : 
    5240              :   /* Determine the element type for the conversion source.
    5241              :      As orig_elem_type keeps track of the original type, check
    5242              :      if we need to perform a sign swap after permuting.
    5243              :      We need to be able to construct a vector type from the element
    5244              :      type which is not possible for e.g. BitInt or pointers
    5245              :      so pun with an integer type if needed.   */
    5246         1394 :   tree perm_eltype = TREE_TYPE (TREE_TYPE (orig[0]));
    5247         1394 :   bool sign_change_p = false;
    5248         1394 :   if (conv_code != ERROR_MARK
    5249          373 :       && orig_elem_type[0]
    5250         1767 :       && TYPE_SIGN (orig_elem_type[0]) != TYPE_SIGN (perm_eltype))
    5251              :     {
    5252           35 :       perm_eltype = signed_or_unsigned_type_for
    5253           35 :         (TYPE_UNSIGNED (orig_elem_type[0]), perm_eltype);
    5254           35 :       sign_change_p = true;
    5255              :     }
    5256         1394 :   tree conv_src_type = build_vector_type (perm_eltype, nelts);
    5257              : 
    5258         1394 :   if (maybe_ident)
    5259              :     {
    5260              :       /* When there is no conversion, use the target type directly.  */
    5261          529 :       if (conv_code == ERROR_MARK && nelts != refnelts)
    5262          529 :         conv_src_type = type;
    5263          529 :       if (conv_code != ERROR_MARK
    5264          529 :           && !supportable_convert_operation (conv_code, type, conv_src_type))
    5265              :         {
    5266              :           /* Only few targets implement direct conversion patterns so try
    5267              :              some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR.  */
    5268          115 :           optab optab;
    5269          115 :           insn_code icode;
    5270          115 :           tree halfvectype, dblvectype;
    5271          115 :           enum tree_code unpack_op;
    5272              : 
    5273          115 :           if (!BYTES_BIG_ENDIAN)
    5274          207 :             unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
    5275          115 :                          ? VEC_UNPACK_FLOAT_LO_EXPR
    5276              :                          : VEC_UNPACK_LO_EXPR);
    5277              :           else
    5278              :             unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
    5279              :                          ? VEC_UNPACK_FLOAT_HI_EXPR
    5280              :                          : VEC_UNPACK_HI_EXPR);
    5281              : 
    5282              :           /* Conversions between DFP and FP have no special tree code
    5283              :              but we cannot handle those since all relevant vector conversion
    5284              :              optabs only have a single mode.  */
    5285           15 :           if (CONVERT_EXPR_CODE_P (conv_code)
    5286          100 :               && FLOAT_TYPE_P (TREE_TYPE (type))
    5287          131 :               && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
    5288            8 :                   != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
    5289              :             return false;
    5290              : 
    5291           15 :           if (CONVERT_EXPR_CODE_P (conv_code)
    5292           99 :               && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
    5293           99 :                   == TYPE_PRECISION (TREE_TYPE (type)))
    5294            6 :               && orig_elem_type[0]
    5295            6 :               && useless_type_conversion_p (orig_elem_type[0],
    5296            6 :                                             TREE_TYPE (TREE_TYPE (orig[0])))
    5297            6 :               && mode_for_vector (as_a <scalar_mode>
    5298            6 :                                   (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
    5299           12 :                                   nelts * 2).exists ()
    5300            6 :               && (dblvectype
    5301            6 :                   = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
    5302            6 :                                        nelts * 2))
    5303              :               /* Only use it for vector modes or for vector booleans
    5304              :                  represented as scalar bitmasks.  See PR95528.  */
    5305            6 :               && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
    5306            0 :                   || VECTOR_BOOLEAN_TYPE_P (dblvectype))
    5307            6 :               && (optab = optab_for_tree_code (unpack_op,
    5308              :                                                dblvectype,
    5309              :                                                optab_default))
    5310            6 :               && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
    5311              :                   != CODE_FOR_nothing)
    5312          114 :               && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
    5313              :             {
    5314            0 :               gimple_seq stmts = NULL;
    5315            0 :               tree dbl;
    5316            0 :               if (refnelts == nelts)
    5317              :                 {
    5318              :                   /* ???  Paradoxical subregs don't exist, so insert into
    5319              :                      the lower half of a wider zero vector.  */
    5320            0 :                   dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
    5321              :                                       build_zero_cst (dblvectype), orig[0],
    5322            0 :                                       bitsize_zero_node);
    5323              :                 }
    5324            0 :               else if (refnelts == 2 * nelts)
    5325              :                 dbl = orig[0];
    5326              :               else
    5327            0 :                 dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
    5328            0 :                                     orig[0], TYPE_SIZE (dblvectype),
    5329            0 :                                     bitsize_zero_node);
    5330            0 :               gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    5331            0 :               gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
    5332              :             }
    5333           15 :           else if (CONVERT_EXPR_CODE_P (conv_code)
    5334           99 :                    && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
    5335           99 :                        == 2 * TYPE_PRECISION (TREE_TYPE (type)))
    5336            5 :                    && orig_elem_type[0]
    5337            5 :                    && useless_type_conversion_p (orig_elem_type[0],
    5338            5 :                                                  TREE_TYPE (TREE_TYPE (orig[0])))
    5339            1 :                    && mode_for_vector (as_a <scalar_mode>
    5340            1 :                                          (TYPE_MODE
    5341              :                                            (TREE_TYPE (TREE_TYPE (orig[0])))),
    5342            2 :                                        nelts / 2).exists ()
    5343            1 :                    && (halfvectype
    5344            1 :                          = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
    5345            1 :                                               nelts / 2))
    5346              :                    /* Only use it for vector modes or for vector booleans
    5347              :                       represented as scalar bitmasks.  See PR95528.  */
    5348            1 :                    && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
    5349            0 :                        || VECTOR_BOOLEAN_TYPE_P (halfvectype))
    5350            1 :                    && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
    5351              :                                                     halfvectype,
    5352              :                                                     optab_default))
    5353            1 :                    && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
    5354              :                        != CODE_FOR_nothing)
    5355          115 :                    && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
    5356              :             {
    5357            0 :               gimple_seq stmts = NULL;
    5358            0 :               tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
    5359            0 :                                        orig[0], TYPE_SIZE (halfvectype),
    5360            0 :                                        bitsize_zero_node);
    5361            0 :               tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
    5362            0 :                                        orig[0], TYPE_SIZE (halfvectype),
    5363            0 :                                        TYPE_SIZE (halfvectype));
    5364            0 :               gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    5365            0 :               gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
    5366              :                                               low, hig);
    5367              :             }
    5368              :           else
    5369          114 :             return false;
    5370            0 :           update_stmt (gsi_stmt (*gsi));
    5371            0 :           return true;
    5372              :         }
    5373          414 :       if (nelts != refnelts)
    5374              :         {
    5375           14 :           gassign *lowpart
    5376           14 :             = gimple_build_assign (make_ssa_name (conv_src_type),
    5377              :                                    build3 (BIT_FIELD_REF, conv_src_type,
    5378           14 :                                            orig[0], TYPE_SIZE (conv_src_type),
    5379              :                                            bitsize_zero_node));
    5380           14 :           gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
    5381           14 :           orig[0] = gimple_assign_lhs (lowpart);
    5382              :         }
    5383          400 :       else if (sign_change_p)
    5384              :         {
    5385            0 :           gassign *conv
    5386            0 :             = gimple_build_assign (make_ssa_name (conv_src_type),
    5387              :                                    build1 (VIEW_CONVERT_EXPR, conv_src_type,
    5388              :                                            orig[0]));
    5389            0 :           gsi_insert_before (gsi, conv, GSI_SAME_STMT);
    5390            0 :           orig[0] = gimple_assign_lhs (conv);
    5391              :         }
    5392          414 :       if (conv_code == ERROR_MARK)
    5393              :         {
    5394          397 :           tree src_type = TREE_TYPE (orig[0]);
    5395          397 :           if (!useless_type_conversion_p (type, src_type))
    5396              :             {
    5397            0 :               gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
    5398              :                                     TYPE_VECTOR_SUBPARTS (src_type))
    5399              :                           && tree_nop_conversion_p (TREE_TYPE (type),
    5400              :                                                     TREE_TYPE (src_type)));
    5401            0 :               tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
    5402            0 :               orig[0] = make_ssa_name (type);
    5403            0 :               gassign *assign = gimple_build_assign (orig[0], rhs);
    5404            0 :               gsi_insert_before (gsi, assign, GSI_SAME_STMT);
    5405              :             }
    5406          397 :           gimple_assign_set_rhs_from_tree (gsi, orig[0]);
    5407              :         }
    5408              :       else
    5409           17 :         gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
    5410              :                                         NULL_TREE, NULL_TREE);
    5411              :     }
    5412              :   else
    5413              :     {
    5414              :       /* If we combine a vector with a non-vector avoid cases where
    5415              :          we'll obviously end up with more GIMPLE stmts which is when
    5416              :          we'll later not fold this to a single insert into the vector
    5417              :          and we had a single extract originally.  See PR92819.  */
    5418          865 :       if (nelts == 2
    5419          507 :           && refnelts > 2
    5420          118 :           && orig[1] == error_mark_node
    5421           31 :           && !maybe_blend[0])
    5422          158 :         return false;
    5423          840 :       tree mask_type, perm_type;
    5424          840 :       perm_type = TREE_TYPE (orig[0]);
    5425          840 :       if (conv_code != ERROR_MARK
    5426          840 :           && !supportable_convert_operation (conv_code, type, conv_src_type))
    5427              :         return false;
    5428              : 
    5429              :       /* Now that we know the number of elements of the source build the
    5430              :          permute vector.
    5431              :          ???  When the second vector has constant values we can shuffle
    5432              :          it and its source indexes to make the permutation supported.
    5433              :          For now it mimics a blend.  */
    5434          830 :       vec_perm_builder sel (refnelts, refnelts, 1);
    5435          830 :       bool all_same_p = true;
    5436         8488 :       for (i = 0; i < elts.length (); ++i)
    5437              :         {
    5438         3414 :           sel.quick_push (elts[i].second + elts[i].first * refnelts);
    5439         3414 :           all_same_p &= known_eq (sel[i], sel[0]);
    5440              :         }
    5441              :       /* And fill the tail with "something".  It's really don't care,
    5442              :          and ideally we'd allow VEC_PERM to have a smaller destination
    5443              :          vector.  As a heuristic:
    5444              : 
    5445              :          (a) if what we have so far duplicates a single element, make the
    5446              :              tail do the same
    5447              : 
    5448              :          (b) otherwise preserve a uniform orig[0].  This facilitates
    5449              :              later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR.  */
    5450         1418 :       for (; i < refnelts; ++i)
    5451         1176 :         sel.quick_push (all_same_p
    5452         1764 :                         ? sel[0]
    5453          156 :                         : (elts[0].second == 0 && elts[0].first == 0
    5454          860 :                            ? 0 : refnelts) + i);
    5455         1028 :       vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
    5456          830 :       machine_mode vmode = TYPE_MODE (perm_type);
    5457          830 :       if ((cfun->curr_properties & PROP_gimple_lvec)
    5458          830 :           && !can_vec_perm_const_p (vmode, vmode, indices))
    5459              :         return false;
    5460          707 :       mask_type = build_vector_type (ssizetype, refnelts);
    5461          707 :       tree op2 = vec_perm_indices_to_tree (mask_type, indices);
    5462          707 :       bool converted_orig1 = false;
    5463          707 :       gimple_seq stmts = NULL;
    5464          707 :       if (!orig[1])
    5465          173 :         orig[1] = orig[0];
    5466          534 :       else if (orig[1] == error_mark_node
    5467          414 :                && one_nonconstant)
    5468              :         {
    5469              :           /* ???  We can see if we can safely convert to the original
    5470              :              element type.  */
    5471          195 :           converted_orig1 = conv_code != ERROR_MARK;
    5472          195 :           tree target_type = converted_orig1 ? type : perm_type;
    5473          195 :           tree nonconstant_for_splat = one_nonconstant;
    5474              :           /* If there's a nop conversion between the target element type and
    5475              :              the nonconstant's type, convert it.  */
    5476          195 :           if (!useless_type_conversion_p (TREE_TYPE (target_type),
    5477          195 :                                           TREE_TYPE (one_nonconstant)))
    5478            0 :             nonconstant_for_splat
    5479            0 :               = gimple_build (&stmts, NOP_EXPR, TREE_TYPE (target_type),
    5480              :                               one_nonconstant);
    5481          195 :           orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
    5482              :                                                   target_type,
    5483              :                                                   nonconstant_for_splat);
    5484          195 :         }
    5485          339 :       else if (orig[1] == error_mark_node)
    5486              :         {
    5487              :           /* ???  See if we can convert the vector to the original type.  */
    5488          219 :           converted_orig1 = conv_code != ERROR_MARK;
    5489          219 :           unsigned n = converted_orig1 ? nelts : refnelts;
    5490          202 :           tree target_type = converted_orig1 ? type : perm_type;
    5491          219 :           tree_vector_builder vec (target_type, n, 1);
    5492         1952 :           for (unsigned i = 0; i < n; ++i)
    5493         2904 :             if (i < nelts && constants[i])
    5494              :               {
    5495          773 :                 tree constant = constants[i];
    5496              :                 /* If there's a nop conversion, convert the constant.  */
    5497          773 :                 if (!useless_type_conversion_p (TREE_TYPE (target_type),
    5498          773 :                                                 TREE_TYPE (constant)))
    5499            2 :                   constant = fold_convert (TREE_TYPE (target_type), constant);
    5500          773 :                 vec.quick_push (constant);
    5501              :               }
    5502              :             else
    5503              :               {
    5504              :                 /* ??? Push a don't-care value.  */
    5505          741 :                 tree constant = one_constant;
    5506          741 :                 if (!useless_type_conversion_p (TREE_TYPE (target_type),
    5507          741 :                                                 TREE_TYPE (constant)))
    5508            2 :                   constant = fold_convert (TREE_TYPE (target_type), constant);
    5509          741 :                 vec.quick_push (constant);
    5510              :               }
    5511          219 :           orig[1] = vec.build ();
    5512          219 :         }
    5513          587 :       tree blend_op2 = NULL_TREE;
    5514          587 :       if (converted_orig1)
    5515              :         {
    5516              :           /* Make sure we can do a blend in the target type.  */
    5517          123 :           vec_perm_builder sel (nelts, nelts, 1);
    5518          403 :           for (i = 0; i < elts.length (); ++i)
    5519          280 :             sel.quick_push (elts[i].first
    5520          280 :                             ? elts[i].second + nelts : i);
    5521          123 :           vec_perm_indices indices (sel, 2, nelts);
    5522          123 :           machine_mode vmode = TYPE_MODE (type);
    5523          123 :           if ((cfun->curr_properties & PROP_gimple_lvec)
    5524          123 :               && !can_vec_perm_const_p (vmode, vmode, indices))
    5525            0 :             return false;
    5526          123 :           mask_type = build_vector_type (ssizetype, nelts);
    5527          123 :           blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
    5528          123 :         }
    5529              : 
    5530              :       /* For a real orig[1] (no splat, constant etc.) we might need to
    5531              :          nop-convert it.  Do so here.  */
    5532          707 :       if (orig[1] && orig[1] != error_mark_node
    5533          707 :           && !converted_orig1
    5534          584 :           && !useless_type_conversion_p (perm_type, TREE_TYPE (orig[1]))
    5535          707 :           && tree_nop_conversion_p (TREE_TYPE (perm_type),
    5536            0 :                                     TREE_TYPE (TREE_TYPE (orig[1]))))
    5537            0 :         orig[1] = gimple_build (&stmts, VIEW_CONVERT_EXPR, perm_type,
    5538              :                                 orig[1]);
    5539              : 
    5540          707 :       tree orig1_for_perm
    5541          707 :         = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
    5542          707 :       tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
    5543              :                                orig[0], orig1_for_perm, op2);
    5544              :       /* If we're building a smaller vector, extract the element
    5545              :          with the proper type.  */
    5546          707 :       if (nelts != refnelts)
    5547          236 :         res = gimple_build (&stmts, BIT_FIELD_REF,
    5548              :                             conv_code != ERROR_MARK ? conv_src_type : type,
    5549              :                             res,
    5550          118 :                             TYPE_SIZE (conv_code != ERROR_MARK ? conv_src_type
    5551              :                                                                : type),
    5552          118 :                             bitsize_zero_node);
    5553              :       /* Otherwise, we can still have an intermediate sign change.
    5554              :          ??? In that case we have two subsequent conversions.
    5555              :          We should be able to merge them.  */
    5556          589 :       else if (sign_change_p)
    5557           15 :         res = gimple_build (&stmts, VIEW_CONVERT_EXPR, conv_src_type, res);
    5558              :       /* Finally, apply the conversion.  */
    5559          707 :       if (conv_code != ERROR_MARK)
    5560          157 :         res = gimple_build (&stmts, conv_code, type, res);
    5561          550 :       else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
    5562              :         {
    5563            3 :           gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
    5564              :                                 TYPE_VECTOR_SUBPARTS (perm_type))
    5565              :                       && tree_nop_conversion_p (TREE_TYPE (type),
    5566              :                                                 TREE_TYPE (perm_type)));
    5567            3 :           res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
    5568              :         }
    5569              :       /* Blend in the actual constant.  */
    5570          707 :       if (converted_orig1)
    5571          123 :         res = gimple_build (&stmts, VEC_PERM_EXPR, type,
    5572          123 :                             res, orig[1], blend_op2);
    5573          707 :       gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    5574          707 :       gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
    5575          830 :     }
    5576         1121 :   update_stmt (gsi_stmt (*gsi));
    5577         1121 :   return true;
    5578       167858 : }
    5579              : 
    5580              : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
    5581              :    lvalue.  This splits out an address computation stmt before *GSI
    5582              :    and returns a MEM_REF wrapping the address.  */
    5583              : 
    5584              : static tree
    5585         1244 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
    5586              : {
    5587         1244 :   if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
    5588          250 :     mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
    5589         1244 :   tree ptrtype = build_pointer_type (TREE_TYPE (ref));
    5590         1244 :   tree tem = make_ssa_name (ptrtype);
    5591         1244 :   gimple *new_stmt
    5592         1244 :     = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
    5593              :                                         unshare_expr (ref)));
    5594         1244 :   gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5595         2488 :   ref = build2_loc (EXPR_LOCATION (ref),
    5596         1244 :                     MEM_REF, TREE_TYPE (ref), tem,
    5597         1244 :                     build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
    5598         1244 :   return ref;
    5599              : }
    5600              : 
    5601              : /* Rewrite the vector load at *GSI to component-wise loads if the load
    5602              :    is only used in BIT_FIELD_REF extractions with eventual intermediate
    5603              :    widening.  */
    5604              : 
    5605              : static void
    5606       294468 : optimize_vector_load (gimple_stmt_iterator *gsi)
    5607              : {
    5608       294468 :   gimple *stmt = gsi_stmt (*gsi);
    5609       294468 :   tree lhs = gimple_assign_lhs (stmt);
    5610       294468 :   tree rhs = gimple_assign_rhs1 (stmt);
    5611       294468 :   tree vuse = gimple_vuse (stmt);
    5612              : 
    5613              :   /* Gather BIT_FIELD_REFs to rewrite, looking through
    5614              :      VEC_UNPACK_{LO,HI}_EXPR.  */
    5615       294468 :   use_operand_p use_p;
    5616       294468 :   imm_use_iterator iter;
    5617       294468 :   bool rewrite = true;
    5618       294468 :   bool scalar_use = false;
    5619       294468 :   bool unpack_use = false;
    5620       294468 :   auto_vec<gimple *, 8> bf_stmts;
    5621       294468 :   auto_vec<tree, 8> worklist;
    5622       294468 :   worklist.quick_push (lhs);
    5623       296418 :   do
    5624              :     {
    5625       296418 :       tree def = worklist.pop ();
    5626       296418 :       unsigned HOST_WIDE_INT def_eltsize
    5627       296418 :         = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
    5628       375649 :       FOR_EACH_IMM_USE_FAST (use_p, iter, def)
    5629              :         {
    5630       354412 :           gimple *use_stmt = USE_STMT (use_p);
    5631       354412 :           if (is_gimple_debug (use_stmt))
    5632        79231 :             continue;
    5633       353070 :           tree use_lhs;
    5634       353070 :           if (!is_gimple_assign (use_stmt)
    5635              :               /* For alias reasons we move the use to the place of the
    5636              :                  load.  Avoid this when abnormals are involved.  */
    5637       353070 :               || ((TREE_CODE ((use_lhs = gimple_assign_lhs (use_stmt)))
    5638              :                    == SSA_NAME)
    5639       242831 :                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs)))
    5640              :             {
    5641              :               rewrite = false;
    5642       275181 :               break;
    5643              :             }
    5644       318709 :           enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
    5645       318709 :           tree use_rhs = gimple_assign_rhs1 (use_stmt);
    5646       392717 :           if (use_code == BIT_FIELD_REF
    5647        74009 :               && TREE_OPERAND (use_rhs, 0) == def
    5648              :               /* If its on the VEC_UNPACK_{HI,LO}_EXPR
    5649              :                  def need to verify it is element aligned.  */
    5650       392718 :               && (def == lhs
    5651          153 :                   || (known_eq (bit_field_size (use_rhs), def_eltsize)
    5652        74161 :                       && constant_multiple_p (bit_field_offset (use_rhs),
    5653              :                                               def_eltsize)
    5654              :                       /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
    5655              :                          via a NOP_EXPR only for integral types.
    5656              :                          ???  Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR.  */
    5657          153 :                       && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
    5658              :             {
    5659        74008 :               if (!VECTOR_TYPE_P (TREE_TYPE (gimple_assign_lhs (use_stmt))))
    5660        71691 :                 scalar_use = true;
    5661        74008 :               bf_stmts.safe_push (use_stmt);
    5662        74008 :               continue;
    5663              :             }
    5664              :           /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR.  */
    5665       244701 :           if (def == lhs
    5666       242832 :               && (use_code == VEC_UNPACK_HI_EXPR
    5667       242832 :                   || use_code == VEC_UNPACK_LO_EXPR)
    5668         3881 :               && use_rhs == lhs)
    5669              :             {
    5670         3881 :               unpack_use = true;
    5671         3881 :               worklist.safe_push (gimple_assign_lhs (use_stmt));
    5672         3881 :               continue;
    5673              :             }
    5674              :           rewrite = false;
    5675              :           break;
    5676       296418 :         }
    5677       296418 :       if (!rewrite)
    5678              :         break;
    5679              :     }
    5680        42474 :   while (!worklist.is_empty ());
    5681              : 
    5682       294468 :   rewrite = rewrite && (scalar_use
    5683        19287 :                         || unpack_use
    5684          626 :                         || !can_implement_p (mov_optab,
    5685          626 :                                              TYPE_MODE (TREE_TYPE (lhs))));
    5686       275363 :   if (!rewrite)
    5687              :     {
    5688       275363 :       gsi_next (gsi);
    5689       275363 :       return;
    5690              :     }
    5691              :   /* We now have all ultimate uses of the load to rewrite in bf_stmts.  */
    5692              : 
    5693              :   /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
    5694              :      For TARGET_MEM_REFs we have to separate the LEA from the reference.  */
    5695        19105 :   tree load_rhs = rhs;
    5696        19105 :   if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
    5697         1243 :     load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
    5698              : 
    5699              :   /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
    5700              :      the place of the original load.  */
    5701       124567 :   for (gimple *use_stmt : bf_stmts)
    5702              :     {
    5703        67252 :       tree bfr = gimple_assign_rhs1 (use_stmt);
    5704        67252 :       tree new_rhs = unshare_expr (load_rhs);
    5705        67252 :       if (TREE_OPERAND (bfr, 0) != lhs)
    5706              :         {
    5707              :           /* When the BIT_FIELD_REF is on the promoted vector we have to
    5708              :              adjust it and emit a conversion afterwards.  */
    5709          152 :           gimple *def_stmt
    5710          152 :               = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
    5711          152 :           enum tree_code def_code
    5712          152 :               = gimple_assign_rhs_code (def_stmt);
    5713              : 
    5714              :           /* The adjusted BIT_FIELD_REF is of the promotion source
    5715              :              vector size and at half of the offset...  */
    5716          152 :           new_rhs = fold_build3 (BIT_FIELD_REF,
    5717              :                                  TREE_TYPE (TREE_TYPE (lhs)),
    5718              :                                  new_rhs,
    5719              :                                  TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
    5720              :                                  size_binop (EXACT_DIV_EXPR,
    5721              :                                              TREE_OPERAND (bfr, 2),
    5722              :                                              bitsize_int (2)));
    5723              :           /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR.  */
    5724          152 :           if (def_code == (!BYTES_BIG_ENDIAN
    5725              :                            ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
    5726           76 :             TREE_OPERAND (new_rhs, 2)
    5727          152 :               = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
    5728              :                             size_binop (EXACT_DIV_EXPR,
    5729              :                                         TYPE_SIZE (TREE_TYPE (lhs)),
    5730              :                                         bitsize_int (2)));
    5731          152 :           tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
    5732          152 :           gimple *new_stmt = gimple_build_assign (tem, new_rhs);
    5733          152 :           location_t loc = gimple_location (use_stmt);
    5734          152 :           gimple_set_location (new_stmt, loc);
    5735          152 :           gimple_set_vuse (new_stmt, vuse);
    5736          152 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5737              :           /* Perform scalar promotion.  */
    5738          152 :           new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
    5739              :                                           NOP_EXPR, tem);
    5740          152 :           gimple_set_location (new_stmt, loc);
    5741          152 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5742              :         }
    5743              :       else
    5744              :         {
    5745              :           /* When the BIT_FIELD_REF is on the original load result
    5746              :              we can just wrap that.  */
    5747        67100 :           tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
    5748              :                                       unshare_expr (load_rhs),
    5749              :                                       TREE_OPERAND (bfr, 1),
    5750              :                                       TREE_OPERAND (bfr, 2));
    5751        67100 :           gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
    5752              :                                                   new_rhs);
    5753        67100 :           location_t loc = gimple_location (use_stmt);
    5754        67100 :           gimple_set_location (new_stmt, loc);
    5755        67100 :           gimple_set_vuse (new_stmt, vuse);
    5756        67100 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5757              :         }
    5758        67252 :       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    5759        67252 :       unlink_stmt_vdef (use_stmt);
    5760        67252 :       gsi_remove (&gsi2, true);
    5761              :     }
    5762              : 
    5763              :   /* Finally get rid of the intermediate stmts.  */
    5764        19105 :   gimple *use_stmt;
    5765        19707 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    5766              :     {
    5767          602 :       if (is_gimple_debug (use_stmt))
    5768              :         {
    5769          540 :           if (gimple_debug_bind_p (use_stmt))
    5770              :             {
    5771          540 :               gimple_debug_bind_reset_value (use_stmt);
    5772          540 :               update_stmt (use_stmt);
    5773              :             }
    5774          540 :           continue;
    5775              :         }
    5776           62 :       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    5777           62 :       unlink_stmt_vdef (use_stmt);
    5778           62 :       release_defs (use_stmt);
    5779           62 :       gsi_remove (&gsi2, true);
    5780        19105 :     }
    5781              :   /* And the original load.  */
    5782        19105 :   release_defs (stmt);
    5783        19105 :   gsi_remove (gsi, true);
    5784       294468 : }
    5785              : 
    5786              : 
    5787              : /* Primitive "lattice" function for gimple_simplify.  */
    5788              : 
    5789              : static tree
    5790   1771877779 : fwprop_ssa_val (tree name)
    5791              : {
    5792              :   /* First valueize NAME.  */
    5793   1771877779 :   if (TREE_CODE (name) == SSA_NAME
    5794   1771877779 :       && SSA_NAME_VERSION (name) < lattice.length ())
    5795              :     {
    5796   1770850821 :       tree val = lattice[SSA_NAME_VERSION (name)];
    5797   1770850821 :       if (val)
    5798   1771877779 :         name = val;
    5799              :     }
    5800              :   /* We continue matching along SSA use-def edges for SSA names
    5801              :      that are not single-use.  Currently there are no patterns
    5802              :      that would cause any issues with that.  */
    5803   1771877779 :   return name;
    5804              : }
    5805              : 
    5806              : /* Search for opportunities to free half of the lanes in the following pattern:
    5807              : 
    5808              :      v_in = {e0, e1, e2, e3}
    5809              :      v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
    5810              :      // v_1 = {e0, e2, e0, e2}
    5811              :      v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
    5812              :      // v_2 = {e1, e3, e1, e3}
    5813              : 
    5814              :      v_x = v_1 + v_2
    5815              :      // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
    5816              :      v_y = v_1 - v_2
    5817              :      // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
    5818              : 
    5819              :      v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
    5820              :      // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
    5821              : 
    5822              :    The last statement could be simplified to:
    5823              :      v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
    5824              :      // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
    5825              : 
    5826              :    Characteristic properties:
    5827              :    - v_1 and v_2 are created from the same input vector v_in and introduce the
    5828              :      lane duplication (in the selection operand) that we can eliminate.
    5829              :    - v_x and v_y are results from lane-preserving operations that use v_1 and
    5830              :      v_2 as inputs.
    5831              :    - v_out is created by selecting from duplicated lanes.  */
    5832              : 
    5833              : static bool
    5834       189186 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
    5835              : {
    5836       189186 :   unsigned HOST_WIDE_INT nelts;
    5837              : 
    5838       189186 :   gcc_checking_assert (stmt);
    5839       189186 :   gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
    5840       189186 :   basic_block bb = gimple_bb (stmt);
    5841              : 
    5842              :   /* Decompose the final vec permute statement.  */
    5843       189186 :   tree v_x = gimple_assign_rhs1 (stmt);
    5844       189186 :   tree v_y = gimple_assign_rhs2 (stmt);
    5845       189186 :   tree sel = gimple_assign_rhs3 (stmt);
    5846              : 
    5847       189186 :   if (TREE_CODE (sel) != VECTOR_CST
    5848       264133 :       || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
    5849       186417 :       || TREE_CODE (v_x) != SSA_NAME
    5850       184565 :       || TREE_CODE (v_y) != SSA_NAME
    5851       179022 :       || !has_single_use (v_x)
    5852       300639 :       || !has_single_use (v_y))
    5853              :     return false;
    5854              : 
    5855              :   /* Don't analyse sequences with many lanes.  */
    5856       109618 :   if (nelts > 4)
    5857              :     return false;
    5858              : 
    5859              :   /* Lookup the definition of v_x and v_y.  */
    5860       106006 :   gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
    5861       106006 :   gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
    5862       105629 :   if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
    5863       211635 :       || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
    5864              :     return false;
    5865              : 
    5866              :   /* Check the operations that define v_x and v_y.  */
    5867       105622 :   if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
    5868       107728 :       || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
    5869              :     return false;
    5870              : 
    5871         2106 :   tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
    5872         2106 :   tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
    5873         2106 :   tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
    5874         2106 :   tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
    5875              : 
    5876         2106 :   if (v_x_stmt == v_y_stmt
    5877         2106 :       || TREE_CODE (v_x_1) != SSA_NAME
    5878         2103 :       || TREE_CODE (v_x_2) != SSA_NAME
    5879         2079 :       || num_imm_uses (v_x_1) != 2
    5880         4024 :       || num_imm_uses (v_x_2) != 2)
    5881              :     return false;
    5882              : 
    5883         1877 :   if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
    5884              :     {
    5885              :       /* Allow operands of commutative operators to swap.  */
    5886          660 :       if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
    5887              :         {
    5888              :           /* Keep v_x_1 the first operand for non-commutative operators.  */
    5889          259 :           std::swap (v_x_1, v_x_2);
    5890          259 :           if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
    5891              :             return false;
    5892              :         }
    5893          401 :       else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
    5894              :         {
    5895          401 :           if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
    5896              :             return false;
    5897              :         }
    5898              :       else
    5899              :         return false;
    5900              :     }
    5901         1877 :   gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
    5902         1877 :   gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
    5903         1813 :   if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
    5904         3690 :       || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
    5905              :     return false;
    5906              : 
    5907         1809 :   if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
    5908         1931 :       || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
    5909              :     return false;
    5910              : 
    5911              :   /* Decompose initial VEC_PERM_EXPRs.  */
    5912          108 :   tree v_in = gimple_assign_rhs1 (v_1_stmt);
    5913          108 :   tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
    5914          108 :   tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
    5915          108 :   if (v_in != gimple_assign_rhs2 (v_1_stmt)
    5916          103 :       || v_in != gimple_assign_rhs1 (v_2_stmt)
    5917          209 :       || v_in != gimple_assign_rhs2 (v_2_stmt))
    5918              :     return false;
    5919              : 
    5920          101 :   unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
    5921          101 :   if (TREE_CODE (v_1_sel) != VECTOR_CST
    5922          101 :       || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
    5923          101 :       || TREE_CODE (v_2_sel) != VECTOR_CST
    5924          202 :       || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
    5925              :     return false;
    5926              : 
    5927          101 :   if (nelts != v_1_nelts || nelts != v_2_nelts)
    5928              :     return false;
    5929              : 
    5930              :   /* Create the new selector.  */
    5931          101 :   vec_perm_builder new_sel_perm (nelts, nelts, 1);
    5932          101 :   auto_vec<bool> lanes (nelts);
    5933          101 :   lanes.quick_grow_cleared (nelts);
    5934          606 :   for (unsigned int i = 0; i < nelts; i++)
    5935              :     {
    5936              :       /* Extract the i-th value from the selector.  */
    5937          404 :       unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
    5938          404 :       unsigned int lane = sel_cst % nelts;
    5939          404 :       unsigned int offs = sel_cst / nelts;
    5940              : 
    5941              :       /* Check what's in the lane.  */
    5942          404 :       unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
    5943          404 :       unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
    5944              : 
    5945              :       /* Reuse previous lane (if any).  */
    5946          404 :       unsigned int l = 0;
    5947          687 :       for (; l < lane; l++)
    5948              :         {
    5949          481 :           if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
    5950          481 :               && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
    5951              :             break;
    5952              :         }
    5953              : 
    5954              :       /* Add to narrowed selector.  */
    5955          404 :       new_sel_perm.quick_push (l + offs * nelts);
    5956              : 
    5957              :       /* Mark lane as used.  */
    5958          404 :       lanes[l] = true;
    5959              :     }
    5960              : 
    5961              :   /* Count how many lanes are need.  */
    5962              :   unsigned int cnt = 0;
    5963          505 :   for (unsigned int i = 0; i < nelts; i++)
    5964          404 :     cnt += lanes[i];
    5965              : 
    5966              :   /* If more than (nelts/2) lanes are needed, skip the sequence.  */
    5967          101 :   if (cnt > nelts / 2)
    5968              :     return false;
    5969              : 
    5970              :   /* Check if the resulting permutation is cheap.  */
    5971          101 :   vec_perm_indices new_indices (new_sel_perm, 2, nelts);
    5972          101 :   tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
    5973          101 :   machine_mode vmode = TYPE_MODE (vectype);
    5974          101 :   if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
    5975              :     return false;
    5976              : 
    5977          101 :   *seq = XNEW (struct _vec_perm_simplify_seq);
    5978          101 :   (*seq)->stmt = stmt;
    5979          101 :   (*seq)->v_1_stmt = v_1_stmt;
    5980          101 :   (*seq)->v_2_stmt = v_2_stmt;
    5981          101 :   (*seq)->v_x_stmt = v_x_stmt;
    5982          101 :   (*seq)->v_y_stmt = v_y_stmt;
    5983          101 :   (*seq)->nelts = nelts;
    5984          101 :   (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
    5985              : 
    5986          101 :   if (dump_file)
    5987              :     {
    5988           28 :       fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
    5989           28 :       print_gimple_stmt (dump_file, stmt, 0);
    5990              : 
    5991           28 :       if (dump_flags & TDF_DETAILS)
    5992              :         {
    5993           28 :           fprintf (dump_file, "\tNarrowed vec_perm selector: ");
    5994           28 :           print_generic_expr (dump_file, (*seq)->new_sel);
    5995           28 :           fprintf (dump_file, "\n");
    5996              :         }
    5997              :     }
    5998              : 
    5999              :   return true;
    6000          202 : }
    6001              : 
    6002              : /* Reduce the lane consumption of a simplifiable vec perm sequence.  */
    6003              : 
    6004              : static void
    6005           74 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
    6006              : {
    6007           74 :   gassign *stmt = seq->stmt;
    6008           74 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6009              :     {
    6010           22 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6011           22 :       fprintf (dump_file, "Old stmt: ");
    6012           22 :       print_gimple_stmt (dump_file, stmt, 0);
    6013              :     }
    6014              : 
    6015              :   /* Update the last VEC_PERM statement.  */
    6016           74 :   gimple_assign_set_rhs3 (stmt, seq->new_sel);
    6017           74 :   update_stmt (stmt);
    6018              : 
    6019           74 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6020              :     {
    6021           22 :       fprintf (dump_file, "New stmt: ");
    6022           22 :       print_gimple_stmt (dump_file, stmt, 0);
    6023              :     }
    6024           74 : }
    6025              : 
    6026              : /* Test if we can blend two simplifiable vec permute sequences.
    6027              :    NEED_SWAP will be set, if sequences must be swapped for blending.  */
    6028              : 
    6029              : static bool
    6030           47 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
    6031              :                                     vec_perm_simplify_seq seq2,
    6032              :                                     bool *need_swap)
    6033              : {
    6034           47 :   unsigned int nelts = seq1->nelts;
    6035           47 :   basic_block bb = gimple_bb (seq1->stmt);
    6036              : 
    6037           47 :   gcc_assert (gimple_bb (seq2->stmt) == bb);
    6038              : 
    6039              :   /* BBs and number of elements must be equal.  */
    6040           47 :   if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
    6041              :     return false;
    6042              : 
    6043              :   /* We need vectors of the same type.  */
    6044           47 :   if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
    6045           47 :       != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
    6046              :     return false;
    6047              : 
    6048              :   /* We require isomorphic operators.  */
    6049           41 :   if (((gimple_assign_rhs_code (seq1->v_x_stmt)
    6050           41 :         != gimple_assign_rhs_code (seq2->v_x_stmt))
    6051           41 :        || (gimple_assign_rhs_code (seq1->v_y_stmt)
    6052           41 :            != gimple_assign_rhs_code (seq2->v_y_stmt))))
    6053              :     return false;
    6054              : 
    6055              :   /* We cannot have any dependencies between the sequences.
    6056              : 
    6057              :      For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
    6058              :      seq1's v_in is defined before these statements, but we need
    6059              :      to check if seq2's v_in is defined before them as well.
    6060              : 
    6061              :      Further, we will reuse seq2->stmt.  We need to ensure that
    6062              :      seq1->v_x_stmt and seq1->v_y_stmt are before it.
    6063              : 
    6064              :      Note, that we don't need to check the BBs here, because all
    6065              :      statements of both sequences have to be in the same BB.  */
    6066              : 
    6067           41 :   tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
    6068           41 :   if (TREE_CODE (seq2_v_in) != SSA_NAME)
    6069              :     return false;
    6070              : 
    6071           41 :   gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
    6072           41 :   if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
    6073           41 :       || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
    6074           37 :       || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
    6075           37 :       || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
    6076              :     {
    6077            4 :       tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
    6078            4 :       if (TREE_CODE (seq1_v_in) != SSA_NAME)
    6079              :         return false;
    6080              : 
    6081            4 :       gassign *seq1_v_in_stmt
    6082            4 :         = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
    6083              :       /* Let's try to see if we succeed when swapping the sequences.  */
    6084            4 :       if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
    6085            0 :           || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
    6086            0 :           || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
    6087            0 :           || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
    6088              :         return false;
    6089            0 :       *need_swap = true;
    6090              :     }
    6091              :   else
    6092           37 :     *need_swap = false;
    6093              : 
    6094           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6095           11 :     fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
    6096              : 
    6097              :   return true;
    6098              : }
    6099              : 
    6100              : /* Calculate the permutations for blending the two given vec permute
    6101              :    sequences.  This may fail if the resulting permutation is not
    6102              :    supported.  */
    6103              : 
    6104              : static bool
    6105           37 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
    6106              :                                   vec_perm_simplify_seq seq2,
    6107              :                                   vec_perm_indices *seq2_stmt_indices,
    6108              :                                   vec_perm_indices *seq1_v_1_stmt_indices,
    6109              :                                   vec_perm_indices *seq1_v_2_stmt_indices)
    6110              : {
    6111           37 :   unsigned int i;
    6112           37 :   unsigned int nelts = seq1->nelts;
    6113           37 :   auto_vec<unsigned int> lane_assignment;
    6114           37 :   lane_assignment.create (nelts);
    6115              : 
    6116              :   /* Mark all lanes as free.  */
    6117           37 :   lane_assignment.quick_grow_cleared (nelts);
    6118              : 
    6119              :   /* Allocate lanes for seq1.  */
    6120          222 :   for (i = 0; i < nelts; i++)
    6121              :     {
    6122          148 :       unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
    6123          148 :       l %= nelts;
    6124          148 :       lane_assignment[l] = 1;
    6125              :     }
    6126              : 
    6127              :   /* Allocate lanes for seq2 and calculate selector for seq2->stmt.  */
    6128           37 :   vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
    6129          185 :   for (i = 0; i < nelts; i++)
    6130              :     {
    6131          148 :       unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
    6132          148 :       unsigned int lane = sel % nelts;
    6133          148 :       unsigned int offs = sel / nelts;
    6134          148 :       unsigned int new_sel;
    6135              : 
    6136              :       /* Check if we already allocated the lane for seq2.  */
    6137          148 :       unsigned int j = 0;
    6138          263 :       for (; j < i; j++)
    6139              :         {
    6140          189 :           unsigned int sel_old;
    6141          189 :           sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
    6142          189 :           unsigned int lane_old = sel_old % nelts;
    6143          189 :           if (lane == lane_old)
    6144              :             {
    6145           74 :               new_sel = seq2_stmt_sel_perm[j].to_constant ();
    6146           74 :               new_sel = (new_sel % nelts) + offs * nelts;
    6147           74 :               break;
    6148              :             }
    6149              :         }
    6150              : 
    6151              :       /* If the lane is not allocated, we need to do that now.  */
    6152          148 :       if (j == i)
    6153              :         {
    6154              :           unsigned int l_orig = lane;
    6155          182 :           while (lane_assignment[lane] != 0)
    6156              :             {
    6157          108 :               lane = (lane + 1) % nelts;
    6158              : 
    6159              :               /* This should not happen if both sequences utilize no more than
    6160              :                  half of the lanes.  Test anyway to guarantee termination.  */
    6161          108 :               if (lane == l_orig)
    6162           37 :                 return false;
    6163              :             }
    6164              : 
    6165              :           /* Allocate lane.  */
    6166           74 :           lane_assignment[lane] = 2 + l_orig;
    6167           74 :           new_sel = lane + offs * nelts;
    6168              :         }
    6169              : 
    6170          148 :       seq2_stmt_sel_perm.quick_push (new_sel);
    6171              :     }
    6172              : 
    6173              :   /* Check if the resulting permutation is cheap.  */
    6174           37 :   seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
    6175           37 :   tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
    6176           37 :   machine_mode vmode = TYPE_MODE (vectype);
    6177           37 :   if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
    6178              :     return false;
    6179              : 
    6180              :   /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt.  */
    6181           37 :   vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
    6182           37 :   vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
    6183          185 :   for (i = 0; i < nelts; i++)
    6184              :     {
    6185          148 :       bool use_seq1 = lane_assignment[i] < 2;
    6186          148 :       unsigned int l1, l2;
    6187              : 
    6188          148 :       if (use_seq1)
    6189              :         {
    6190              :           /* Just reuse the selector indices.  */
    6191           74 :           tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
    6192           74 :           tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
    6193           74 :           l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
    6194           74 :           l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
    6195              :         }
    6196              :       else
    6197              :         {
    6198              :           /* We moved the lanes for seq2, so we need to adjust for that.  */
    6199           74 :           tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
    6200           74 :           tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
    6201           74 :           l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, lane_assignment[i] - 2));
    6202           74 :           l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, lane_assignment[i] - 2));
    6203              :         }
    6204              : 
    6205          148 :       l1 %= nelts;
    6206          148 :       l2 %= nelts;
    6207          222 :       seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
    6208          148 :       seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
    6209              :     }
    6210              : 
    6211           37 :   seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
    6212           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
    6213           37 :   vmode = TYPE_MODE (vectype);
    6214           37 :   if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
    6215              :     return false;
    6216              : 
    6217           37 :   seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
    6218           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
    6219           37 :   vmode = TYPE_MODE (vectype);
    6220           37 :   if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
    6221              :     return false;
    6222              : 
    6223              :   return true;
    6224           74 : }
    6225              : 
    6226              : /* Blend the two given simplifiable vec permute sequences using the
    6227              :    given permutations.  */
    6228              : 
    6229              : static void
    6230           37 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
    6231              :                               vec_perm_simplify_seq seq2,
    6232              :                               const vec_perm_indices &seq2_stmt_indices,
    6233              :                               const vec_perm_indices &seq1_v_1_stmt_indices,
    6234              :                               const vec_perm_indices &seq1_v_2_stmt_indices)
    6235              : {
    6236              :   /* We don't need to adjust seq1->stmt because its lanes consumption
    6237              :      was already narrowed before entering this function.  */
    6238              : 
    6239              :   /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel.  */
    6240           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6241              :     {
    6242           11 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6243           11 :       fprintf (dump_file, "Old stmt: ");
    6244           11 :       print_gimple_stmt (dump_file, seq2->stmt, 0);
    6245              :     }
    6246              : 
    6247           37 :   gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
    6248           74 :   gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
    6249           37 :   tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
    6250           37 :   tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
    6251           37 :   gimple_assign_set_rhs3 (seq2->stmt, sel);
    6252           37 :   update_stmt (seq2->stmt);
    6253              : 
    6254           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6255              :     {
    6256           11 :       fprintf (dump_file, "New stmt: ");
    6257           11 :       print_gimple_stmt (dump_file, seq2->stmt, 0);
    6258              :     }
    6259              : 
    6260              :   /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel.  */
    6261           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6262              :     {
    6263           11 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6264           11 :       fprintf (dump_file, "Old stmt: ");
    6265           11 :       print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
    6266              :     }
    6267              : 
    6268           37 :   gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
    6269           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
    6270           37 :   sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
    6271           37 :   gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
    6272           37 :   update_stmt (seq1->v_1_stmt);
    6273              : 
    6274           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6275              :     {
    6276           11 :       fprintf (dump_file, "New stmt: ");
    6277           11 :       print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
    6278              :     }
    6279              : 
    6280              :   /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel.  */
    6281           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6282              :     {
    6283           11 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    6284           11 :       fprintf (dump_file, "Old stmt: ");
    6285           11 :       print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
    6286              :     }
    6287              : 
    6288           37 :   gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
    6289           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
    6290           37 :   sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
    6291           37 :   gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
    6292           37 :   update_stmt (seq1->v_2_stmt);
    6293              : 
    6294           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6295              :     {
    6296           11 :       fprintf (dump_file, "New stmt: ");
    6297           11 :       print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
    6298              :     }
    6299              : 
    6300              :   /* At this point, we have four unmodified seq2 stmts, which will be
    6301              :      eliminated by DCE.  */
    6302              : 
    6303           37 :   if (dump_file)
    6304           11 :     fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
    6305           37 : }
    6306              : 
    6307              : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
    6308              :    The provided list will be empty after this call.  */
    6309              : 
    6310              : static void
    6311    337170979 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
    6312              : {
    6313    337170979 :   unsigned int i, j;
    6314    337170979 :   vec_perm_simplify_seq seq1, seq2;
    6315              : 
    6316    337170979 :   if (l->is_empty ())
    6317    337170934 :     return;
    6318              : 
    6319           45 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6320           13 :     fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
    6321              :              l->length ());
    6322              : 
    6323          154 :   FOR_EACH_VEC_ELT (*l, i, seq1)
    6324              :     {
    6325           64 :       if (i + 1 < l->length ())
    6326              :         {
    6327          115 :           FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
    6328              :             {
    6329           47 :               bool swap = false;
    6330           47 :               if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
    6331              :                 {
    6332           37 :                   vec_perm_indices seq2_stmt_indices;
    6333           37 :                   vec_perm_indices seq1_v_1_stmt_indices;
    6334           37 :                   vec_perm_indices seq1_v_2_stmt_indices;
    6335          111 :                   if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
    6336              :                                                         swap ? seq1 : seq2,
    6337              :                                                         &seq2_stmt_indices,
    6338              :                                                         &seq1_v_1_stmt_indices,
    6339              :                                                         &seq1_v_2_stmt_indices))
    6340              :                     {
    6341              :                       /* Narrow lane usage.  */
    6342           37 :                       narrow_vec_perm_simplify_seq (seq1);
    6343           37 :                       narrow_vec_perm_simplify_seq (seq2);
    6344              : 
    6345              :                       /* Blend sequences.  */
    6346           37 :                       blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
    6347              :                                                     swap ? seq1 : seq2,
    6348              :                                                     seq2_stmt_indices,
    6349              :                                                     seq1_v_1_stmt_indices,
    6350              :                                                     seq1_v_2_stmt_indices);
    6351              : 
    6352              :                       /* We can use unordered_remove as we break the loop.  */
    6353           37 :                       l->unordered_remove (j);
    6354           37 :                       XDELETE (seq2);
    6355           37 :                       break;
    6356              :                     }
    6357           37 :                 }
    6358              :             }
    6359              :         }
    6360              : 
    6361              :       /* We don't need to call l->remove for seq1.  */
    6362           64 :       XDELETE (seq1);
    6363              :     }
    6364              : 
    6365           45 :   l->truncate (0);
    6366              : }
    6367              : 
    6368              : static void
    6369          101 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
    6370              :                                    const vec_perm_simplify_seq &seq)
    6371              : {
    6372              :   /* If no space on list left, then process the list.  */
    6373          101 :   if (!l->space (1))
    6374            0 :       process_vec_perm_simplify_seq_list (l);
    6375              : 
    6376          101 :   l->quick_push (seq);
    6377          101 : }
    6378              : 
    6379              : /* Main entry point for the forward propagation and statement combine
    6380              :    optimizer.  */
    6381              : 
    6382              : namespace {
    6383              : 
    6384              : const pass_data pass_data_forwprop =
    6385              : {
    6386              :   GIMPLE_PASS, /* type */
    6387              :   "forwprop", /* name */
    6388              :   OPTGROUP_NONE, /* optinfo_flags */
    6389              :   TV_TREE_FORWPROP, /* tv_id */
    6390              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    6391              :   0, /* properties_provided */
    6392              :   0, /* properties_destroyed */
    6393              :   0, /* todo_flags_start */
    6394              :   0, /* todo_flags_finish */
    6395              : };
    6396              : 
    6397              : class pass_forwprop : public gimple_opt_pass
    6398              : {
    6399              : public:
    6400      1470980 :   pass_forwprop (gcc::context *ctxt)
    6401      2941960 :     : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
    6402              :   {}
    6403              : 
    6404              :   /* opt_pass methods: */
    6405      1176784 :   opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
    6406      1765176 :   void set_pass_param (unsigned int n, bool param) final override
    6407              :     {
    6408      1765176 :       switch (n)
    6409              :         {
    6410      1176784 :           case 0:
    6411      1176784 :             m_full_walk = param;
    6412      1176784 :             break;
    6413       588392 :           case 1:
    6414       588392 :             last_p = param;
    6415       588392 :             break;
    6416            0 :           default:
    6417            0 :           gcc_unreachable();
    6418              :         }
    6419      1765176 :     }
    6420      5725317 :   bool gate (function *) final override { return flag_tree_forwprop; }
    6421              :   unsigned int execute (function *) final override;
    6422              : 
    6423              :  private:
    6424              :   /* Determines whether the pass instance should set PROP_last_full_fold.  */
    6425              :   bool last_p;
    6426              : 
    6427              :   /* True if the aggregate props are doing a full walk or not.  */
    6428              :   bool m_full_walk = false;
    6429              : }; // class pass_forwprop
    6430              : 
    6431              : /* Attempt to make the BB block of __builtin_unreachable unreachable by changing
    6432              :    the incoming jumps.  Return true if at least one jump was changed.  */
    6433              : 
    6434              : static bool
    6435         1133 : optimize_unreachable (basic_block bb)
    6436              : {
    6437         1133 :   gimple_stmt_iterator gsi;
    6438         1133 :   gimple *stmt;
    6439         1133 :   edge_iterator ei;
    6440         1133 :   edge e;
    6441         1133 :   bool ret;
    6442              : 
    6443         1133 :   ret = false;
    6444         2336 :   FOR_EACH_EDGE (e, ei, bb->preds)
    6445              :     {
    6446         1203 :       gsi = gsi_last_bb (e->src);
    6447         1203 :       if (gsi_end_p (gsi))
    6448          323 :         continue;
    6449              : 
    6450          880 :       stmt = gsi_stmt (gsi);
    6451          880 :       if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
    6452              :         {
    6453              :           /* If the condition is already true/false
    6454              :              ignore it. This can happen during copy prop of forwprop. */
    6455          694 :           if (gimple_cond_true_p (cond_stmt)
    6456          686 :               || gimple_cond_false_p (cond_stmt))
    6457            8 :             continue;
    6458          678 :           else if (e->flags & EDGE_TRUE_VALUE)
    6459          587 :             gimple_cond_make_false (cond_stmt);
    6460           91 :           else if (e->flags & EDGE_FALSE_VALUE)
    6461           91 :             gimple_cond_make_true (cond_stmt);
    6462              :           else
    6463            0 :             gcc_unreachable ();
    6464          678 :           update_stmt (cond_stmt);
    6465              :         }
    6466              :       else
    6467              :         {
    6468              :           /* Todo: handle other cases.  e.g. switch.  */
    6469          194 :           continue;
    6470              :         }
    6471              : 
    6472          678 :       ret = true;
    6473              :     }
    6474              : 
    6475         1133 :   return ret;
    6476              : }
    6477              : 
    6478              : unsigned int
    6479      5722679 : pass_forwprop::execute (function *fun)
    6480              : {
    6481      5722679 :   unsigned int todoflags = 0;
    6482              :   /* Handle a full walk only when expensive optimizations are on.  */
    6483      5722679 :   bool full_walk = m_full_walk && flag_expensive_optimizations;
    6484              : 
    6485      5722679 :   cfg_changed = false;
    6486      5722679 :   if (last_p)
    6487      1062279 :     fun->curr_properties |= PROP_last_full_fold;
    6488              : 
    6489      5722679 :   calculate_dominance_info (CDI_DOMINATORS);
    6490              : 
    6491              :   /* Combine stmts with the stmts defining their operands.  Do that
    6492              :      in an order that guarantees visiting SSA defs before SSA uses.  */
    6493     11445358 :   lattice.create (num_ssa_names);
    6494     11445358 :   lattice.quick_grow_cleared (num_ssa_names);
    6495      5722679 :   int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
    6496      5722679 :   int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
    6497              :                                                          postorder, false);
    6498      5722679 :   int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
    6499     57337482 :   for (int i = 0; i < postorder_num; ++i)
    6500              :     {
    6501     45892124 :       bb_to_rpo[postorder[i]] = i;
    6502     45892124 :       edge_iterator ei;
    6503     45892124 :       edge e;
    6504    110420437 :       FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
    6505     64528313 :         e->flags &= ~EDGE_EXECUTABLE;
    6506              :     }
    6507      5722679 :   single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
    6508      5722679 :     |= EDGE_EXECUTABLE;
    6509      5722679 :   auto_vec<gimple *, 4> to_fixup;
    6510      5722679 :   auto_vec<gimple *, 32> to_remove;
    6511      5722679 :   auto_vec<unsigned, 32> to_remove_defs;
    6512      5722679 :   auto_vec<std::pair<int, int>, 10> edges_to_remove;
    6513      5722679 :   auto_bitmap simple_dce_worklist;
    6514      5722679 :   auto_bitmap need_ab_cleanup;
    6515      5722679 :   to_purge = BITMAP_ALLOC (NULL);
    6516      5722679 :   auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
    6517     51614803 :   for (int i = 0; i < postorder_num; ++i)
    6518              :     {
    6519     45892124 :       gimple_stmt_iterator gsi;
    6520     45892124 :       basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
    6521     45892124 :       edge_iterator ei;
    6522     45892124 :       edge e;
    6523              : 
    6524              :       /* Skip processing not executable blocks.  We could improve
    6525              :          single_use tracking by at least unlinking uses from unreachable
    6526              :          blocks but since blocks with uses are not processed in a
    6527              :          meaningful order this is probably not worth it.  */
    6528     45892124 :       bool any = false;
    6529     47051053 :       FOR_EACH_EDGE (e, ei, bb->preds)
    6530              :         {
    6531     47037492 :           if ((e->flags & EDGE_EXECUTABLE)
    6532              :               /* We can handle backedges in natural loops correctly but
    6533              :                  for irreducible regions we have to take all backedges
    6534              :                  conservatively when we did not visit the source yet.  */
    6535     47037492 :               || (bb_to_rpo[e->src->index] > i
    6536       681722 :                   && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
    6537              :             {
    6538              :               any = true;
    6539              :               break;
    6540              :             }
    6541              :         }
    6542     45892124 :       if (!any)
    6543        14201 :         continue;
    6544              : 
    6545              :       /* Remove conditions that go directly to unreachable when this is the last forwprop.  */
    6546     45878563 :       if (last_p
    6547     10024799 :           && !(flag_sanitize & SANITIZE_UNREACHABLE))
    6548              :         {
    6549     10019848 :           gimple_stmt_iterator gsi;
    6550     10019848 :           gsi = gsi_start_nondebug_after_labels_bb (bb);
    6551     10020488 :           if (!gsi_end_p (gsi)
    6552      9157022 :               && gimple_call_builtin_p (*gsi, BUILT_IN_UNREACHABLE)
    6553     10020981 :               && optimize_unreachable (bb))
    6554              :             {
    6555          640 :               cfg_changed = true;
    6556          640 :               continue;
    6557              :             }
    6558              :         }
    6559              : 
    6560              :       /* Fold PHI-form long-multiply carries and record degenerate
    6561              :          PHIs in the lattice.  Iterator advanced up front so a folded
    6562              :          PHI can be removed in-flight; a long-mul carry PHI is never
    6563              :          degenerate, so the two cases are disjoint.  */
    6564     62072692 :       for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);)
    6565              :         {
    6566     16194769 :           gphi *phi = si.phi ();
    6567     16194769 :           gsi_next (&si);
    6568     16194769 :           tree res = gimple_phi_result (phi);
    6569     32389538 :           if (virtual_operand_p (res))
    6570      7419912 :             continue;
    6571      8774871 :           if (match_long_mul_phi (phi))
    6572           14 :             continue;
    6573              : 
    6574      8774857 :           tree first = NULL_TREE;
    6575      8774857 :           bool all_same = true;
    6576      8774857 :           edge_iterator ei;
    6577      8774857 :           edge e;
    6578     18063668 :           FOR_EACH_EDGE (e, ei, bb->preds)
    6579              :             {
    6580              :               /* Ignore not executable forward edges.  */
    6581     17839216 :               if (!(e->flags & EDGE_EXECUTABLE))
    6582              :                 {
    6583      4094765 :                   if (bb_to_rpo[e->src->index] < i)
    6584         6830 :                     continue;
    6585              :                   /* Avoid equivalences from backedges - while we might
    6586              :                      be able to make irreducible regions reducible and
    6587              :                      thus turning a back into a forward edge we do not
    6588              :                      want to deal with the intermediate SSA issues that
    6589              :                      exposes.  */
    6590              :                   all_same = false;
    6591              :                 }
    6592     17832386 :               tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
    6593     17832386 :               if (use == res)
    6594              :                 /* The PHI result can also appear on a backedge, if so
    6595              :                    we can ignore this case for the purpose of determining
    6596              :                    the singular value.  */
    6597              :                 ;
    6598     17819341 :               else if (! first)
    6599              :                 first = use;
    6600      9044484 :               else if (! operand_equal_p (first, use, 0))
    6601              :                 {
    6602              :                   all_same = false;
    6603              :                   break;
    6604              :                 }
    6605              :             }
    6606      8774857 :           if (all_same)
    6607              :             {
    6608       219621 :               if (may_propagate_copy (res, first))
    6609       218964 :                 to_remove_defs.safe_push (SSA_NAME_VERSION (res));
    6610       219621 :               fwprop_set_lattice_val (res, first);
    6611              :             }
    6612              :         }
    6613              : 
    6614              :       /* Apply forward propagation to all stmts in the basic-block.
    6615              :          Note we update GSI within the loop as necessary.  */
    6616     45877923 :       unsigned int uid = 1;
    6617    451318598 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
    6618              :         {
    6619    359562752 :           gimple *stmt = gsi_stmt (gsi);
    6620    359562752 :           tree lhs, rhs;
    6621    359562752 :           enum tree_code code;
    6622              : 
    6623    359562752 :           gimple_set_uid (stmt, uid++);
    6624              : 
    6625    359562752 :           if (!is_gimple_assign (stmt))
    6626              :             {
    6627    251735078 :               process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    6628    251735078 :               gsi_next (&gsi);
    6629    251735078 :               continue;
    6630              :             }
    6631              : 
    6632    107827674 :           lhs = gimple_assign_lhs (stmt);
    6633    107827674 :           rhs = gimple_assign_rhs1 (stmt);
    6634    107827674 :           code = gimple_assign_rhs_code (stmt);
    6635              : 
    6636    147385652 :           if (TREE_CODE (lhs) != SSA_NAME
    6637    107827674 :               || has_zero_uses (lhs))
    6638              :             {
    6639     39557978 :               process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    6640     39557978 :               gsi_next (&gsi);
    6641     39557978 :               continue;
    6642              :             }
    6643              : 
    6644              :           /* If this statement sets an SSA_NAME to an address,
    6645              :              try to propagate the address into the uses of the SSA_NAME.  */
    6646     68269696 :           if ((code == ADDR_EXPR
    6647              :                /* Handle pointer conversions on invariant addresses
    6648              :                   as well, as this is valid gimple.  */
    6649     65927213 :                || (CONVERT_EXPR_CODE_P (code)
    6650      9110610 :                    && TREE_CODE (rhs) == ADDR_EXPR
    6651       357928 :                    && POINTER_TYPE_P (TREE_TYPE (lhs))))
    6652     68269920 :               && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
    6653              :             {
    6654      2342118 :               tree base = get_base_address (TREE_OPERAND (rhs, 0));
    6655      2342118 :               if ((!base
    6656      2342118 :                    || !DECL_P (base)
    6657       134525 :                    || decl_address_invariant_p (base))
    6658      2342118 :                   && !stmt_references_abnormal_ssa_name (stmt)
    6659      4684220 :                   && forward_propagate_addr_expr (lhs, rhs, true))
    6660              :                 {
    6661       478413 :                   fwprop_invalidate_lattice (gimple_get_lhs (stmt));
    6662       478413 :                   release_defs (stmt);
    6663       478413 :                   gsi_remove (&gsi, true);
    6664              :                 }
    6665              :               else
    6666      1863705 :                 gsi_next (&gsi);
    6667              :             }
    6668     65927578 :           else if (code == POINTER_PLUS_EXPR)
    6669              :             {
    6670      3689945 :               tree off = gimple_assign_rhs2 (stmt);
    6671      3689945 :               if (TREE_CODE (off) == INTEGER_CST
    6672      1138365 :                   && can_propagate_from (stmt)
    6673      1138012 :                   && !simple_iv_increment_p (stmt)
    6674              :                   /* ???  Better adjust the interface to that function
    6675              :                      instead of building new trees here.  */
    6676      4530243 :                   && forward_propagate_addr_expr
    6677      2520894 :                        (lhs,
    6678              :                         build1_loc (gimple_location (stmt),
    6679       840298 :                                     ADDR_EXPR, TREE_TYPE (rhs),
    6680       840298 :                                     fold_build2 (MEM_REF,
    6681              :                                                  TREE_TYPE (TREE_TYPE (rhs)),
    6682              :                                                  rhs,
    6683              :                                                  fold_convert (ptr_type_node,
    6684              :                                                                off))), true))
    6685              :                 {
    6686       318623 :                   fwprop_invalidate_lattice (gimple_get_lhs (stmt));
    6687       318623 :                   release_defs (stmt);
    6688       318623 :                   gsi_remove (&gsi, true);
    6689              :                 }
    6690      3371322 :               else if (is_gimple_min_invariant (rhs))
    6691              :                 {
    6692              :                   /* Make sure to fold &a[0] + off_1 here.  */
    6693       418431 :                   fold_stmt_inplace (&gsi);
    6694       418431 :                   update_stmt (stmt);
    6695       418431 :                   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
    6696       418413 :                     gsi_next (&gsi);
    6697              :                 }
    6698              :               else
    6699      2952891 :                 gsi_next (&gsi);
    6700              :             }
    6701     62237633 :           else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
    6702       214053 :                    && gimple_assign_load_p (stmt)
    6703       134974 :                    && !gimple_has_volatile_ops (stmt)
    6704        40899 :                    && TREE_CODE (rhs) != TARGET_MEM_REF
    6705        40869 :                    && TREE_CODE (rhs) != BIT_FIELD_REF
    6706     62278498 :                    && !stmt_can_throw_internal (fun, stmt))
    6707              :             {
    6708              :               /* Rewrite loads used only in real/imagpart extractions to
    6709              :                  component-wise loads.  */
    6710        40740 :               use_operand_p use_p;
    6711        40740 :               imm_use_iterator iter;
    6712        40740 :               tree vuse = gimple_vuse (stmt);
    6713        40740 :               bool rewrite = true;
    6714        45923 :               FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
    6715              :                 {
    6716        43797 :                   gimple *use_stmt = USE_STMT (use_p);
    6717        43797 :                   if (is_gimple_debug (use_stmt))
    6718         1011 :                     continue;
    6719        42786 :                   if (!is_gimple_assign (use_stmt)
    6720        28092 :                       || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
    6721        25991 :                           && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
    6722        46958 :                       || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
    6723              :                     {
    6724              :                       rewrite = false;
    6725              :                       break;
    6726              :                     }
    6727        40740 :                 }
    6728        40740 :               if (rewrite)
    6729              :                 {
    6730         2126 :                   gimple *use_stmt;
    6731         6763 :                   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    6732              :                     {
    6733         4637 :                       if (is_gimple_debug (use_stmt))
    6734              :                         {
    6735          498 :                           if (gimple_debug_bind_p (use_stmt))
    6736              :                             {
    6737          498 :                               gimple_debug_bind_reset_value (use_stmt);
    6738          498 :                               update_stmt (use_stmt);
    6739              :                             }
    6740          498 :                           continue;
    6741              :                         }
    6742              : 
    6743         8278 :                       tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
    6744         4139 :                                              TREE_TYPE (TREE_TYPE (rhs)),
    6745              :                                              unshare_expr (rhs));
    6746         4139 :                       gimple *new_stmt
    6747         4139 :                         = gimple_build_assign (gimple_assign_lhs (use_stmt),
    6748              :                                                new_rhs);
    6749              : 
    6750         4139 :                       location_t loc = gimple_location (use_stmt);
    6751         4139 :                       gimple_set_location (new_stmt, loc);
    6752         4139 :                       gimple_set_vuse (new_stmt, vuse);
    6753         4139 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    6754         4139 :                       unlink_stmt_vdef (use_stmt);
    6755         4139 :                       gsi_remove (&gsi2, true);
    6756              : 
    6757         4139 :                       gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
    6758         2126 :                     }
    6759              : 
    6760         2126 :                   release_defs (stmt);
    6761         2126 :                   gsi_remove (&gsi, true);
    6762              :                 }
    6763              :               else
    6764        38614 :                 gsi_next (&gsi);
    6765              :             }
    6766     62196893 :           else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
    6767      1820910 :                    && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
    6768              :                        /* After vector lowering rewrite all loads, but
    6769              :                           initially do not since this conflicts with
    6770              :                           vector CONSTRUCTOR to shuffle optimization.  */
    6771      1796191 :                        || (fun->curr_properties & PROP_gimple_lvec))
    6772       936205 :                    && gimple_assign_load_p (stmt)
    6773       309102 :                    && !gimple_has_volatile_ops (stmt)
    6774       294970 :                    && !stmt_can_throw_internal (fun, stmt)
    6775     62491863 :                    && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
    6776       294468 :             optimize_vector_load (&gsi);
    6777              : 
    6778     61902425 :           else if (code == COMPLEX_EXPR)
    6779              :             {
    6780              :               /* Rewrite stores of a single-use complex build expression
    6781              :                  to component-wise stores.  */
    6782        37866 :               use_operand_p use_p;
    6783        37866 :               gimple *use_stmt, *def1, *def2;
    6784        37866 :               tree rhs2;
    6785        37866 :               if (single_imm_use (lhs, &use_p, &use_stmt)
    6786        35683 :                   && gimple_store_p (use_stmt)
    6787        42088 :                   && !gimple_has_volatile_ops (use_stmt)
    6788         3128 :                   && is_gimple_assign (use_stmt)
    6789         3124 :                   && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (use_stmt)))
    6790              :                       == COMPLEX_TYPE)
    6791        40985 :                   && (TREE_CODE (gimple_assign_lhs (use_stmt))
    6792              :                       != TARGET_MEM_REF))
    6793              :                 {
    6794         3115 :                   tree use_lhs = gimple_assign_lhs (use_stmt);
    6795         3115 :                   if (auto_var_p (use_lhs))
    6796          601 :                     DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
    6797         6230 :                   tree new_lhs = build1 (REALPART_EXPR,
    6798         3115 :                                          TREE_TYPE (TREE_TYPE (use_lhs)),
    6799              :                                          unshare_expr (use_lhs));
    6800         3115 :                   gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
    6801         3115 :                   location_t loc = gimple_location (use_stmt);
    6802         3115 :                   gimple_set_location (new_stmt, loc);
    6803         6230 :                   gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
    6804         3115 :                   gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
    6805         6230 :                   SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
    6806         6230 :                   gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
    6807         3115 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    6808         3115 :                   gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
    6809              : 
    6810         6230 :                   new_lhs = build1 (IMAGPART_EXPR,
    6811         3115 :                                     TREE_TYPE (TREE_TYPE (use_lhs)),
    6812              :                                     unshare_expr (use_lhs));
    6813         3115 :                   gimple_assign_set_lhs (use_stmt, new_lhs);
    6814         3115 :                   gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
    6815         3115 :                   update_stmt (use_stmt);
    6816              : 
    6817         3115 :                   release_defs (stmt);
    6818         3115 :                   gsi_remove (&gsi, true);
    6819              :                 }
    6820              :               /* Rewrite a component-wise load of a complex to a complex
    6821              :                  load if the components are not used separately.  */
    6822        34751 :               else if (TREE_CODE (rhs) == SSA_NAME
    6823        34310 :                        && has_single_use (rhs)
    6824        30780 :                        && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
    6825        30780 :                        && TREE_CODE (rhs2) == SSA_NAME
    6826        28962 :                        && has_single_use (rhs2)
    6827        28542 :                        && (def1 = SSA_NAME_DEF_STMT (rhs),
    6828        28542 :                            gimple_assign_load_p (def1))
    6829         1088 :                        && (def2 = SSA_NAME_DEF_STMT (rhs2),
    6830         1088 :                            gimple_assign_load_p (def2))
    6831         1588 :                        && (gimple_vuse (def1) == gimple_vuse (def2))
    6832          791 :                        && !gimple_has_volatile_ops (def1)
    6833          791 :                        && !gimple_has_volatile_ops (def2)
    6834          791 :                        && !stmt_can_throw_internal (fun, def1)
    6835          791 :                        && !stmt_can_throw_internal (fun, def2)
    6836          791 :                        && gimple_assign_rhs_code (def1) == REALPART_EXPR
    6837          545 :                        && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
    6838        35296 :                        && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
    6839              :                                                                  (def1), 0),
    6840          545 :                                            TREE_OPERAND (gimple_assign_rhs1
    6841              :                                                                  (def2), 0)))
    6842              :                 {
    6843          545 :                   tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
    6844          545 :                   gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
    6845          545 :                   gcc_assert (gsi_stmt (gsi) == stmt);
    6846         1090 :                   gimple_set_vuse (stmt, gimple_vuse (def1));
    6847          545 :                   gimple_set_modified (stmt, true);
    6848          545 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
    6849          545 :                   gsi_remove (&gsi, false);
    6850          545 :                   gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
    6851              :                 }
    6852              :               else
    6853        34206 :                 gsi_next (&gsi);
    6854              :             }
    6855     61864559 :           else if (code == CONSTRUCTOR
    6856       169980 :                    && VECTOR_TYPE_P (TREE_TYPE (rhs))
    6857       169980 :                    && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
    6858         4576 :                    && CONSTRUCTOR_NELTS (rhs) > 0
    6859     61869135 :                    && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
    6860         2096 :                        || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
    6861              :                            != BLKmode)))
    6862              :             {
    6863              :               /* Rewrite stores of a single-use vector constructors
    6864              :                  to component-wise stores if the mode isn't supported.  */
    6865         4219 :               use_operand_p use_p;
    6866         4219 :               gimple *use_stmt;
    6867         4219 :               if (single_imm_use (lhs, &use_p, &use_stmt)
    6868         3752 :                   && gimple_store_p (use_stmt)
    6869         3076 :                   && !gimple_has_volatile_ops (use_stmt)
    6870         1532 :                   && !stmt_can_throw_internal (fun, use_stmt)
    6871         5744 :                   && is_gimple_assign (use_stmt))
    6872              :                 {
    6873         1525 :                   tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
    6874         1525 :                   unsigned HOST_WIDE_INT elt_w
    6875         1525 :                     = tree_to_uhwi (TYPE_SIZE (elt_t));
    6876         1525 :                   unsigned HOST_WIDE_INT n
    6877         1525 :                     = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
    6878         1525 :                   tree use_lhs = gimple_assign_lhs (use_stmt);
    6879         1525 :                   if (auto_var_p (use_lhs))
    6880          575 :                     DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
    6881          950 :                   else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
    6882              :                     {
    6883            1 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    6884            1 :                       use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
    6885              :                     }
    6886        33572 :                   for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
    6887              :                     {
    6888        32047 :                       unsigned HOST_WIDE_INT ci = bi / elt_w;
    6889        32047 :                       tree new_rhs;
    6890        32047 :                       if (ci < CONSTRUCTOR_NELTS (rhs))
    6891        31429 :                         new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
    6892              :                       else
    6893          618 :                         new_rhs = build_zero_cst (elt_t);
    6894        32047 :                       tree new_lhs = build3 (BIT_FIELD_REF,
    6895              :                                              elt_t,
    6896              :                                              unshare_expr (use_lhs),
    6897        32047 :                                              bitsize_int (elt_w),
    6898        32047 :                                              bitsize_int (bi));
    6899        32047 :                       gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
    6900        32047 :                       location_t loc = gimple_location (use_stmt);
    6901        32047 :                       gimple_set_location (new_stmt, loc);
    6902        64094 :                       gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
    6903        32047 :                       gimple_set_vdef (new_stmt,
    6904              :                                        make_ssa_name (gimple_vop (fun)));
    6905        64094 :                       SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
    6906        64094 :                       gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
    6907        32047 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    6908        32047 :                       gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
    6909              :                     }
    6910         1525 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    6911         1525 :                   unlink_stmt_vdef (use_stmt);
    6912         1525 :                   release_defs (use_stmt);
    6913         1525 :                   gsi_remove (&gsi2, true);
    6914         1525 :                   release_defs (stmt);
    6915         1525 :                   gsi_remove (&gsi, true);
    6916              :                 }
    6917              :               else
    6918         2694 :                 gsi_next (&gsi);
    6919              :             }
    6920     61860340 :           else if (code == VEC_PERM_EXPR)
    6921              :             {
    6922              :               /* Find vectorized sequences, where we can reduce the lane
    6923              :                  utilization.  The narrowing will be donw later and only
    6924              :                  if we find a pair of sequences that can be blended.  */
    6925       189186 :               gassign *assign = dyn_cast <gassign *> (stmt);
    6926       189186 :               vec_perm_simplify_seq seq;
    6927       189186 :               if (recognise_vec_perm_simplify_seq (assign, &seq))
    6928          101 :                 append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
    6929              :                                                    seq);
    6930              : 
    6931       189186 :               gsi_next (&gsi);
    6932              :           }
    6933              :           else
    6934     61671154 :             gsi_next (&gsi);
    6935              :         }
    6936              : 
    6937     45877923 :       process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    6938              : 
    6939              :       /* Combine stmts with the stmts defining their operands.
    6940              :          Note we update GSI within the loop as necessary.  */
    6941    450977516 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    6942              :         {
    6943    359221670 :           gimple *stmt = gsi_stmt (gsi);
    6944              : 
    6945              :           /* Mark stmt as potentially needing revisiting.  */
    6946    359221670 :           gimple_set_plf (stmt, GF_PLF_1, false);
    6947              : 
    6948    359221670 :           bool can_make_abnormal_goto = (is_gimple_call (stmt)
    6949    359221670 :                                          && stmt_can_make_abnormal_goto (stmt));
    6950              : 
    6951              :           /* Substitute from our lattice.  We need to do so only once.  */
    6952    359221670 :           bool substituted_p = false;
    6953    359221670 :           use_operand_p usep;
    6954    359221670 :           ssa_op_iter iter;
    6955    525934718 :           FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
    6956              :             {
    6957    166713048 :               tree use = USE_FROM_PTR (usep);
    6958    166713048 :               tree val = fwprop_ssa_val (use);
    6959    166713048 :               if (val && val != use)
    6960              :                 {
    6961      1896303 :                   if (!is_gimple_debug (stmt))
    6962      1572864 :                     bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
    6963      1896303 :                   if (may_propagate_copy (use, val))
    6964              :                     {
    6965      1893059 :                       propagate_value (usep, val);
    6966      1893059 :                       substituted_p = true;
    6967              :                     }
    6968              :                 }
    6969              :             }
    6970    359221670 :           if (substituted_p)
    6971      1837855 :             update_stmt (stmt);
    6972      1837855 :           if (substituted_p
    6973      1837855 :               && is_gimple_assign (stmt)
    6974      1096520 :               && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
    6975        20403 :             recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
    6976    359221670 :           if (substituted_p
    6977    359221670 :               && can_make_abnormal_goto
    6978    359221670 :               && !stmt_can_make_abnormal_goto (stmt))
    6979            3 :             bitmap_set_bit (need_ab_cleanup, bb->index);
    6980              : 
    6981    362082152 :           bool changed;
    6982    724164304 :           do
    6983              :             {
    6984    362082152 :               gimple *orig_stmt = stmt = gsi_stmt (gsi);
    6985    362082152 :               bool was_call = is_gimple_call (stmt);
    6986    362082152 :               bool was_noreturn = (was_call
    6987    362082152 :                                    && gimple_call_noreturn_p (stmt));
    6988    362082152 :               changed = false;
    6989              : 
    6990    362082152 :               auto_vec<tree, 8> uses;
    6991    531836093 :               FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
    6992    339116957 :                 if (uses.space (1))
    6993    169363016 :                   uses.quick_push (USE_FROM_PTR (usep));
    6994              : 
    6995    362082152 :               if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
    6996              :                 {
    6997      2534568 :                   changed = true;
    6998              :                   /* There is no updating of the address
    6999              :                      taken after the last forwprop so update
    7000              :                      the addresses when a folding happened to a call.
    7001              :                      The va_* builtins can remove taking of the address so
    7002              :                      can the sincos->cexpi transformation.  See PR 39643 and PR 20983. */
    7003      2534568 :                   if (was_call && last_p)
    7004      2534568 :                     todoflags |= TODO_update_address_taken;
    7005      2534568 :                   stmt = gsi_stmt (gsi);
    7006              :                   /* Cleanup the CFG if we simplified a condition to
    7007              :                      true or false.  */
    7008      2534568 :                   if (gcond *cond = dyn_cast <gcond *> (stmt))
    7009      1011234 :                     if (gimple_cond_true_p (cond)
    7010      1011234 :                         || gimple_cond_false_p (cond))
    7011        14376 :                       cfg_changed = true;
    7012              :                   /* Queue old uses for simple DCE if not debug statement.  */
    7013      2534568 :                   if (!is_gimple_debug (stmt))
    7014     10714368 :                     for (tree use : uses)
    7015      3132189 :                       if (TREE_CODE (use) == SSA_NAME
    7016      3132189 :                           && !SSA_NAME_IS_DEFAULT_DEF (use))
    7017      2933641 :                         bitmap_set_bit (simple_dce_worklist,
    7018      2933641 :                                         SSA_NAME_VERSION (use));
    7019      2534568 :                   update_stmt (stmt);
    7020              :                 }
    7021              : 
    7022    362082152 :               switch (gimple_code (stmt))
    7023              :                 {
    7024    108845257 :                 case GIMPLE_ASSIGN:
    7025    108845257 :                   {
    7026    108845257 :                     tree rhs1 = gimple_assign_rhs1 (stmt);
    7027    108845257 :                     enum tree_code code = gimple_assign_rhs_code (stmt);
    7028    108845257 :                     if (gimple_clobber_p (stmt))
    7029      7469041 :                       do_simple_agr_dse (as_a<gassign*>(stmt), full_walk);
    7030    101376216 :                     else if (gimple_store_p (stmt))
    7031              :                       {
    7032     31462685 :                         optimize_aggr_zeroprop (stmt, full_walk);
    7033     31462685 :                         if (gimple_assign_load_p (stmt))
    7034      3900925 :                           optimize_agr_copyprop (stmt);
    7035              :                       }
    7036     69913531 :                     else if (TREE_CODE_CLASS (code) == tcc_comparison)
    7037      2679467 :                       changed |= forward_propagate_into_comparison (&gsi);
    7038     67234064 :                     else if ((code == PLUS_EXPR || code == BIT_IOR_EXPR))
    7039              :                       {
    7040     10185039 :                         bool folded = match_long_mul (as_a <gassign *> (stmt));
    7041     10185039 :                         if (!folded)
    7042     10183102 :                           folded = simplify_rotate (&gsi);
    7043     10185039 :                         changed |= folded;
    7044              :                       }
    7045              :                     else if (code == BIT_XOR_EXPR)
    7046       140835 :                       changed |= simplify_rotate (&gsi);
    7047              :                     else if (code == VEC_PERM_EXPR)
    7048       191420 :                       changed |= simplify_permutation (&gsi);
    7049              :                     else if (code == CONSTRUCTOR
    7050       167858 :                              && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
    7051       167858 :                       changed |= simplify_vector_constructor (&gsi);
    7052     56548912 :                     else if (code == ARRAY_REF)
    7053      2004590 :                       changed |= simplify_count_zeroes (&gsi);
    7054              :                     break;
    7055              :                   }
    7056              : 
    7057       108712 :                 case GIMPLE_SWITCH:
    7058       108712 :                   changed |= simplify_gimple_switch (as_a <gswitch *> (stmt),
    7059              :                                                      edges_to_remove,
    7060              :                                                      simple_dce_worklist);
    7061       108712 :                   break;
    7062              : 
    7063     19772855 :                 case GIMPLE_COND:
    7064     19772855 :                   {
    7065     19772855 :                     int did_something = forward_propagate_into_gimple_cond
    7066     19772855 :                                                         (as_a <gcond *> (stmt));
    7067     19772855 :                     if (did_something == 2)
    7068         1665 :                       cfg_changed = true;
    7069     19772855 :                     changed |= did_something != 0;
    7070     19772855 :                     break;
    7071              :                   }
    7072              : 
    7073     23875588 :                 case GIMPLE_CALL:
    7074     23875588 :                   {
    7075     23875588 :                     tree callee = gimple_call_fndecl (stmt);
    7076     23875588 :                     if (callee != NULL_TREE
    7077     23875588 :                         && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
    7078      6312514 :                       changed |= simplify_builtin_call (&gsi, callee, full_walk);
    7079              :                     break;
    7080              :                   }
    7081              : 
    7082    362082152 :                 default:;
    7083              :                 }
    7084              : 
    7085    362082152 :               if (changed || substituted_p)
    7086              :                 {
    7087      4159084 :                   substituted_p = false;
    7088      4159084 :                   stmt = gsi_stmt (gsi);
    7089      4159084 :                   if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
    7090           70 :                     bitmap_set_bit (to_purge, bb->index);
    7091      4159084 :                   if (!was_noreturn
    7092      4159084 :                       && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
    7093           12 :                     to_fixup.safe_push (stmt);
    7094              :                 }
    7095      4159084 :               if (changed)
    7096              :                 {
    7097              :                   /* If the stmt changed then re-visit it and the statements
    7098              :                      inserted before it.  */
    7099      6147575 :                   for (; !gsi_end_p (gsi); gsi_prev (&gsi))
    7100      5700955 :                     if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
    7101              :                       break;
    7102      2860482 :                   if (gsi_end_p (gsi))
    7103       446620 :                     gsi = gsi_start_bb (bb);
    7104              :                   else
    7105      2637172 :                     gsi_next (&gsi);
    7106              :                 }
    7107    362082152 :             }
    7108              :           while (changed);
    7109              : 
    7110              :           /* Stmt no longer needs to be revisited.  */
    7111    359221670 :           stmt = gsi_stmt (gsi);
    7112    359221670 :           gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
    7113    359221670 :           gimple_set_plf (stmt, GF_PLF_1, true);
    7114              : 
    7115              :           /* Fill up the lattice.  */
    7116    359221670 :           if (gimple_assign_single_p (stmt))
    7117              :             {
    7118     72086635 :               tree lhs = gimple_assign_lhs (stmt);
    7119     72086635 :               tree rhs = gimple_assign_rhs1 (stmt);
    7120     72086635 :               if (TREE_CODE (lhs) == SSA_NAME)
    7121              :                 {
    7122     33167411 :                   tree val = lhs;
    7123     33167411 :                   if (TREE_CODE (rhs) == SSA_NAME)
    7124       794740 :                     val = fwprop_ssa_val (rhs);
    7125     32372671 :                   else if (is_gimple_min_invariant (rhs))
    7126       433291 :                     val = rhs;
    7127              :                   /* If we can propagate the lattice-value mark the
    7128              :                      stmt for removal.  */
    7129     33167411 :                   if (val != lhs
    7130     33167411 :                       && may_propagate_copy (lhs, val))
    7131      1224627 :                     to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
    7132     33167411 :                   fwprop_set_lattice_val (lhs, val);
    7133              :                 }
    7134              :             }
    7135    287135035 :           else if (gimple_nop_p (stmt))
    7136       102779 :             to_remove.safe_push (stmt);
    7137              :         }
    7138              : 
    7139              :       /* Substitute in destination PHI arguments.  */
    7140    110394436 :       FOR_EACH_EDGE (e, ei, bb->succs)
    7141     64516513 :         for (gphi_iterator gsi = gsi_start_phis (e->dest);
    7142    107339838 :              !gsi_end_p (gsi); gsi_next (&gsi))
    7143              :           {
    7144     42823325 :             gphi *phi = gsi.phi ();
    7145     42823325 :             use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
    7146     42823325 :             tree arg = USE_FROM_PTR (use_p);
    7147     70504524 :             if (TREE_CODE (arg) != SSA_NAME
    7148     42823325 :                 || virtual_operand_p (arg))
    7149     27681199 :               continue;
    7150     15142126 :             tree val = fwprop_ssa_val (arg);
    7151     15142126 :             if (val != arg
    7152     15142126 :                 && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
    7153       257297 :               propagate_value (use_p, val);
    7154              :           }
    7155              : 
    7156              :       /* Mark outgoing executable edges.  */
    7157     45877923 :       if (edge e = find_taken_edge (bb, NULL))
    7158              :         {
    7159     19519663 :           e->flags |= EDGE_EXECUTABLE;
    7160     45898294 :           if (EDGE_COUNT (bb->succs) > 1)
    7161        20371 :             cfg_changed = true;
    7162              :         }
    7163              :       else
    7164              :         {
    7165     71334738 :           FOR_EACH_EDGE (e, ei, bb->succs)
    7166     44976478 :             e->flags |= EDGE_EXECUTABLE;
    7167              :         }
    7168              :     }
    7169      5722679 :   free (postorder);
    7170      5722679 :   free (bb_to_rpo);
    7171      5722679 :   lattice.release ();
    7172              : 
    7173              :   /* First remove chains of stmts where we check no uses remain.  */
    7174      5722679 :   simple_dce_from_worklist (simple_dce_worklist, to_purge);
    7175              : 
    7176      6084452 :   auto remove = [](gimple *stmt)
    7177              :     {
    7178       361773 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7179              :         {
    7180            1 :           fprintf (dump_file, "Removing dead stmt ");
    7181            1 :           print_gimple_stmt (dump_file, stmt, 0);
    7182            1 :           fprintf (dump_file, "\n");
    7183              :         }
    7184       361773 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    7185       361773 :       if (gimple_code (stmt) == GIMPLE_PHI)
    7186        97010 :         remove_phi_node (&gsi, true);
    7187              :       else
    7188              :         {
    7189       264763 :           unlink_stmt_vdef (stmt);
    7190       264763 :           gsi_remove (&gsi, true);
    7191       264763 :           release_defs (stmt);
    7192              :         }
    7193       361773 :     };
    7194              : 
    7195              :   /* Then remove stmts we know we can remove even though we did not
    7196              :      substitute in dead code regions, so uses can remain.  Do so in reverse
    7197              :      order to make debug stmt creation possible.  */
    7198     12888949 :   while (!to_remove_defs.is_empty())
    7199              :     {
    7200      1443591 :       tree def = ssa_name (to_remove_defs.pop ());
    7201              :       /* For example remove_prop_source_from_use can remove stmts queued
    7202              :          for removal.  Deal with this gracefully.  */
    7203      1443591 :       if (!def)
    7204      1184597 :         continue;
    7205       258994 :       gimple *stmt = SSA_NAME_DEF_STMT (def);
    7206       258994 :       remove (stmt);
    7207              :     }
    7208              : 
    7209              :   /* Wipe other queued stmts that do not have SSA defs.  */
    7210      5825458 :   while (!to_remove.is_empty())
    7211              :     {
    7212       102779 :       gimple *stmt = to_remove.pop ();
    7213       102779 :       remove (stmt);
    7214              :     }
    7215              : 
    7216              :   /* Fixup stmts that became noreturn calls.  This may require splitting
    7217              :      blocks and thus isn't possible during the walk.  Do this
    7218              :      in reverse order so we don't inadvertently remove a stmt we want to
    7219              :      fixup by visiting a dominating now noreturn call first.  */
    7220      5722691 :   while (!to_fixup.is_empty ())
    7221              :     {
    7222           12 :       gimple *stmt = to_fixup.pop ();
    7223           12 :       if (dump_file && dump_flags & TDF_DETAILS)
    7224              :         {
    7225            0 :           fprintf (dump_file, "Fixing up noreturn call ");
    7226            0 :           print_gimple_stmt (dump_file, stmt, 0);
    7227            0 :           fprintf (dump_file, "\n");
    7228              :         }
    7229           12 :       cfg_changed |= fixup_noreturn_call (stmt);
    7230              :     }
    7231              : 
    7232      5722679 :   cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
    7233      5722679 :   cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
    7234      5722679 :   BITMAP_FREE (to_purge);
    7235              : 
    7236              :   /* Remove edges queued from switch stmt simplification.  */
    7237     17168037 :   for (auto ep : edges_to_remove)
    7238              :     {
    7239            0 :       basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
    7240            0 :       basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
    7241            0 :       edge e;
    7242            0 :       if (src && dest && (e = find_edge (src, dest)))
    7243              :         {
    7244            0 :           free_dominance_info (CDI_DOMINATORS);
    7245            0 :           remove_edge (e);
    7246            0 :           cfg_changed = true;
    7247              :         }
    7248              :     }
    7249              : 
    7250     11443815 :   if (get_range_query (fun) != get_global_range_query ())
    7251         1543 :     disable_ranger (fun);
    7252              : 
    7253      5722679 :   if (cfg_changed)
    7254         9457 :     todoflags |= TODO_cleanup_cfg;
    7255              : 
    7256      5722679 :   return todoflags;
    7257      5722679 : }
    7258              : 
    7259              : } // anon namespace
    7260              : 
    7261              : gimple_opt_pass *
    7262       294196 : make_pass_forwprop (gcc::context *ctxt)
    7263              : {
    7264       294196 :   return new pass_forwprop (ctxt);
    7265              : }
        

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.