LCOV - code coverage report
Current view: top level - gcc - tree-ssa-forwprop.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.5 % 3096 2926
Test Date: 2026-07-11 15:47:05 Functions: 100.0 % 63 63
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              : 
      62              : /* This pass propagates the RHS of assignment statements into use
      63              :    sites of the LHS of the assignment.  It's basically a specialized
      64              :    form of tree combination.   It is hoped all of this can disappear
      65              :    when we have a generalized tree combiner.
      66              : 
      67              :    One class of common cases we handle is forward propagating a single use
      68              :    variable into a COND_EXPR.
      69              : 
      70              :      bb0:
      71              :        x = a COND b;
      72              :        if (x) goto ... else goto ...
      73              : 
      74              :    Will be transformed into:
      75              : 
      76              :      bb0:
      77              :        if (a COND b) goto ... else goto ...
      78              : 
      79              :    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
      80              : 
      81              :    Or (assuming c1 and c2 are constants):
      82              : 
      83              :      bb0:
      84              :        x = a + c1;
      85              :        if (x EQ/NEQ c2) goto ... else goto ...
      86              : 
      87              :    Will be transformed into:
      88              : 
      89              :      bb0:
      90              :         if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
      91              : 
      92              :    Similarly for x = a - c1.
      93              : 
      94              :    Or
      95              : 
      96              :      bb0:
      97              :        x = !a
      98              :        if (x) goto ... else goto ...
      99              : 
     100              :    Will be transformed into:
     101              : 
     102              :      bb0:
     103              :         if (a == 0) goto ... else goto ...
     104              : 
     105              :    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
     106              :    For these cases, we propagate A into all, possibly more than one,
     107              :    COND_EXPRs that use X.
     108              : 
     109              :    Or
     110              : 
     111              :      bb0:
     112              :        x = (typecast) a
     113              :        if (x) goto ... else goto ...
     114              : 
     115              :    Will be transformed into:
     116              : 
     117              :      bb0:
     118              :         if (a != 0) goto ... else goto ...
     119              : 
     120              :    (Assuming a is an integral type and x is a boolean or x is an
     121              :     integral and a is a boolean.)
     122              : 
     123              :    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
     124              :    For these cases, we propagate A into all, possibly more than one,
     125              :    COND_EXPRs that use X.
     126              : 
     127              :    In addition to eliminating the variable and the statement which assigns
     128              :    a value to the variable, we may be able to later thread the jump without
     129              :    adding insane complexity in the dominator optimizer.
     130              : 
     131              :    Also note these transformations can cascade.  We handle this by having
     132              :    a worklist of COND_EXPR statements to examine.  As we make a change to
     133              :    a statement, we put it back on the worklist to examine on the next
     134              :    iteration of the main loop.
     135              : 
     136              :    A second class of propagation opportunities arises for ADDR_EXPR
     137              :    nodes.
     138              : 
     139              :      ptr = &x->y->z;
     140              :      res = *ptr;
     141              : 
     142              :    Will get turned into
     143              : 
     144              :      res = x->y->z;
     145              : 
     146              :    Or
     147              :      ptr = (type1*)&type2var;
     148              :      res = *ptr
     149              : 
     150              :    Will get turned into (if type1 and type2 are the same size
     151              :    and neither have volatile on them):
     152              :      res = VIEW_CONVERT_EXPR<type1>(type2var)
     153              : 
     154              :    Or
     155              : 
     156              :      ptr = &x[0];
     157              :      ptr2 = ptr + <constant>;
     158              : 
     159              :    Will get turned into
     160              : 
     161              :      ptr2 = &x[constant/elementsize];
     162              : 
     163              :   Or
     164              : 
     165              :      ptr = &x[0];
     166              :      offset = index * element_size;
     167              :      offset_p = (pointer) offset;
     168              :      ptr2 = ptr + offset_p
     169              : 
     170              :   Will get turned into:
     171              : 
     172              :      ptr2 = &x[index];
     173              : 
     174              :   Or
     175              :     ssa = (int) decl
     176              :     res = ssa & 1
     177              : 
     178              :   Provided that decl has known alignment >= 2, will get turned into
     179              : 
     180              :     res = 0
     181              : 
     182              :   We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
     183              :   allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
     184              :   {NOT_EXPR,NEG_EXPR}.
     185              : 
     186              :    This will (of course) be extended as other needs arise.  */
     187              : 
     188              : /* Data structure that contains simplifiable vectorized permute sequences.
     189              :    See recognise_vec_perm_simplify_seq () for a description of the sequence.  */
     190              : 
     191              : struct _vec_perm_simplify_seq
     192              : {
     193              :   /* Defining stmts of vectors in the sequence.  */
     194              :   gassign *v_1_stmt;
     195              :   gassign *v_2_stmt;
     196              :   gassign *v_x_stmt;
     197              :   gassign *v_y_stmt;
     198              :   /* Final permute statement.  */
     199              :   gassign *stmt;
     200              :   /* New selector indices for stmt.  */
     201              :   tree new_sel;
     202              :   /* Elements of each vector and selector.  */
     203              :   unsigned int nelts;
     204              : };
     205              : typedef struct _vec_perm_simplify_seq *vec_perm_simplify_seq;
     206              : 
     207              : static bool forward_propagate_addr_expr (tree, tree, bool);
     208              : 
     209              : /* Set to true if we delete dead edges during the optimization.  */
     210              : static bool cfg_changed;
     211              : 
     212              : static tree rhs_to_tree (tree type, gimple *stmt);
     213              : 
     214              : static bitmap to_purge;
     215              : 
     216              : /* Const-and-copy lattice.  */
     217              : static vec<tree> lattice;
     218              : 
     219              : /* Set the lattice entry for NAME to VAL.  */
     220              : static void
     221     33132171 : fwprop_set_lattice_val (tree name, tree val)
     222              : {
     223     33132171 :   if (TREE_CODE (name) == SSA_NAME)
     224              :     {
     225     33132171 :       if (SSA_NAME_VERSION (name) >= lattice.length ())
     226              :         {
     227        32205 :           lattice.reserve (num_ssa_names - lattice.length ());
     228        21470 :           lattice.quick_grow_cleared (num_ssa_names);
     229              :         }
     230     33132171 :       lattice[SSA_NAME_VERSION (name)] = val;
     231              :       /* As this now constitutes a copy duplicate points-to
     232              :          and range info appropriately.  */
     233     33132171 :       if (TREE_CODE (val) == SSA_NAME)
     234     32680742 :         maybe_duplicate_ssa_info_at_copy (name, val);
     235              :     }
     236     33132171 : }
     237              : 
     238              : /* Invalidate the lattice entry for NAME, done when releasing SSA names.  */
     239              : static void
     240       937570 : fwprop_invalidate_lattice (tree name)
     241              : {
     242       937570 :   if (name
     243       935171 :       && TREE_CODE (name) == SSA_NAME
     244      1872614 :       && SSA_NAME_VERSION (name) < lattice.length ())
     245       935011 :     lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
     246       937570 : }
     247              : 
     248              : /* Get the statement we can propagate from into NAME skipping
     249              :    trivial copies.  Returns the statement which defines the
     250              :    propagation source or NULL_TREE if there is no such one.
     251              :    If SINGLE_USE_ONLY is set considers only sources which have
     252              :    a single use chain up to NAME.  If SINGLE_USE_P is non-null,
     253              :    it is set to whether the chain to NAME is a single use chain
     254              :    or not.  SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set.  */
     255              : 
     256              : static gimple *
     257     28391684 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
     258              : {
     259     28391684 :   bool single_use = true;
     260              : 
     261     28392672 :   do {
     262     28392178 :     gimple *def_stmt = SSA_NAME_DEF_STMT (name);
     263              : 
     264     28392178 :     if (!has_single_use (name))
     265              :       {
     266     15499260 :         single_use = false;
     267     15499260 :         if (single_use_only)
     268              :           return NULL;
     269              :       }
     270              : 
     271              :     /* If name is defined by a PHI node or is the default def, bail out.  */
     272     28390695 :     if (!is_gimple_assign (def_stmt))
     273              :       return NULL;
     274              : 
     275              :     /* If def_stmt is a simple copy, continue looking.  */
     276     20022804 :     if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
     277          494 :       name = gimple_assign_rhs1 (def_stmt);
     278              :     else
     279              :       {
     280     20022310 :         if (!single_use_only && single_use_p)
     281     19699633 :           *single_use_p = single_use;
     282              : 
     283     20022310 :         return def_stmt;
     284              :       }
     285          494 :   } while (1);
     286              : }
     287              : 
     288              : /* Checks if the destination ssa name in DEF_STMT can be used as
     289              :    propagation source.  Returns true if so, otherwise false.  */
     290              : 
     291              : static bool
     292     28134727 : can_propagate_from (gimple *def_stmt)
     293              : {
     294     28134727 :   gcc_assert (is_gimple_assign (def_stmt));
     295              : 
     296              :   /* If the rhs has side-effects we cannot propagate from it.  */
     297     28134727 :   if (gimple_has_volatile_ops (def_stmt))
     298              :     return false;
     299              : 
     300              :   /* If the rhs is a load we cannot propagate from it.  */
     301     27542203 :   if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
     302     27542203 :       || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
     303              :     return false;
     304              : 
     305              :   /* Constants can be always propagated.  */
     306     13575029 :   if (gimple_assign_single_p (def_stmt)
     307     13575029 :       && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
     308              :     return true;
     309              : 
     310              :   /* We cannot propagate ssa names that occur in abnormal phi nodes.  */
     311     13575029 :   if (stmt_references_abnormal_ssa_name (def_stmt))
     312              :     return false;
     313              : 
     314              :   /* If the definition is a conversion of a pointer to a function type,
     315              :      then we cannot apply optimizations as some targets require
     316              :      function pointers to be canonicalized and in this case this
     317              :      optimization could eliminate a necessary canonicalization.  */
     318     13574338 :   if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
     319              :     {
     320      3261203 :       tree rhs = gimple_assign_rhs1 (def_stmt);
     321      3261203 :       if (FUNCTION_POINTER_TYPE_P (TREE_TYPE (rhs)))
     322              :         return false;
     323              :     }
     324              : 
     325              :   return true;
     326              : }
     327              : 
     328              : /* Remove a chain of dead statements starting at the definition of
     329              :    NAME.  The chain is linked via the first operand of the defining statements.
     330              :    If NAME was replaced in its only use then this function can be used
     331              :    to clean up dead stmts.  The function handles already released SSA
     332              :    names gracefully.  */
     333              : 
     334              : static void
     335       241229 : remove_prop_source_from_use (tree name)
     336              : {
     337       302035 :   gimple_stmt_iterator gsi;
     338       302035 :   gimple *stmt;
     339              : 
     340       302035 :   do {
     341       302035 :     basic_block bb;
     342              : 
     343       302035 :     if (SSA_NAME_IN_FREE_LIST (name)
     344       301992 :         || SSA_NAME_IS_DEFAULT_DEF (name)
     345       602093 :         || !has_zero_uses (name))
     346              :       break;
     347              : 
     348        61263 :     stmt = SSA_NAME_DEF_STMT (name);
     349        61263 :     if (gimple_code (stmt) == GIMPLE_PHI
     350        61263 :         || gimple_has_side_effects (stmt))
     351              :       break;
     352              : 
     353        61263 :     bb = gimple_bb (stmt);
     354        61263 :     gsi = gsi_for_stmt (stmt);
     355        61263 :     unlink_stmt_vdef (stmt);
     356        61263 :     if (gsi_remove (&gsi, true))
     357            6 :       bitmap_set_bit (to_purge, bb->index);
     358        61263 :     fwprop_invalidate_lattice (gimple_get_lhs (stmt));
     359        61263 :     release_defs (stmt);
     360              : 
     361        61263 :     name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
     362        61263 :   } while (name && TREE_CODE (name) == SSA_NAME);
     363              : 
     364       241229 : }
     365              : 
     366              : /* Return the rhs of a gassign *STMT in a form of a single tree,
     367              :    converted to type TYPE.
     368              : 
     369              :    This should disappear, but is needed so we can combine expressions and use
     370              :    the fold() interfaces. Long term, we need to develop folding and combine
     371              :    routines that deal with gimple exclusively . */
     372              : 
     373              : static tree
     374      7404996 : rhs_to_tree (tree type, gimple *stmt)
     375              : {
     376      7404996 :   location_t loc = gimple_location (stmt);
     377      7404996 :   enum tree_code code = gimple_assign_rhs_code (stmt);
     378      7404996 :   switch (get_gimple_rhs_class (code))
     379              :     {
     380        13315 :     case GIMPLE_TERNARY_RHS:
     381        13315 :       return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
     382              :                               gimple_assign_rhs2 (stmt),
     383        13315 :                               gimple_assign_rhs3 (stmt));
     384      5054395 :     case GIMPLE_BINARY_RHS:
     385      5054395 :       return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
     386      5054395 :                               gimple_assign_rhs2 (stmt));
     387      2054397 :     case GIMPLE_UNARY_RHS:
     388      2054397 :       return build1 (code, type, gimple_assign_rhs1 (stmt));
     389       282889 :     case GIMPLE_SINGLE_RHS:
     390       282889 :       return gimple_assign_rhs1 (stmt);
     391            0 :     default:
     392            0 :       gcc_unreachable ();
     393              :     }
     394              : }
     395              : 
     396              : /* Combine OP0 CODE OP1 in the context of a COND_EXPR.  Returns
     397              :    the folded result in a form suitable for COND_EXPR_COND or
     398              :    NULL_TREE, if there is no suitable simplified form.  If
     399              :    INVARIANT_ONLY is true only gimple_min_invariant results are
     400              :    considered simplified.  */
     401              : 
     402              : static tree
     403      8329544 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
     404              :                         tree op0, tree op1, bool invariant_only)
     405              : {
     406      8329544 :   tree t;
     407              : 
     408      8329544 :   gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
     409              : 
     410      8329544 :   t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
     411      8329544 :   if (!t)
     412              :     return NULL_TREE;
     413              : 
     414              :   /* Require that we got a boolean type out if we put one in.  */
     415      3616584 :   gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
     416              : 
     417              :   /* Canonicalize the combined condition for use in a COND_EXPR.  */
     418      3616584 :   t = canonicalize_cond_expr_cond (t);
     419              : 
     420              :   /* Bail out if we required an invariant but didn't get one.  */
     421      3616584 :   if (!t || (invariant_only && !is_gimple_min_invariant (t)))
     422      3377981 :     return NULL_TREE;
     423              : 
     424              :   return t;
     425              : }
     426              : 
     427              : /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
     428              :    of its operand.  Return a new comparison tree or NULL_TREE if there
     429              :    were no simplifying combines.  */
     430              : 
     431              : static tree
     432     22333486 : forward_propagate_into_comparison_1 (gimple *stmt,
     433              :                                      enum tree_code code, tree type,
     434              :                                      tree op0, tree op1)
     435              : {
     436     22333486 :   tree tmp = NULL_TREE;
     437     22333486 :   tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
     438     22333486 :   bool single_use0_p = false, single_use1_p = false;
     439              : 
     440              :   /* For comparisons use the first operand, that is likely to
     441              :      simplify comparisons against constants.  */
     442     22333486 :   if (TREE_CODE (op0) == SSA_NAME)
     443              :     {
     444     22297091 :       gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
     445     22297091 :       if (def_stmt && can_propagate_from (def_stmt))
     446              :         {
     447      5620365 :           enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
     448      5620365 :           bool invariant_only_p = !single_use0_p;
     449              : 
     450      5620365 :           rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
     451              : 
     452              :           /* Always combine comparisons or conversions from booleans.  */
     453      5620365 :           if (TREE_CODE (op1) == INTEGER_CST
     454      5620365 :               && ((CONVERT_EXPR_CODE_P (def_code)
     455       894755 :                    && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
     456              :                       == BOOLEAN_TYPE)
     457      3604916 :                   || TREE_CODE_CLASS (def_code) == tcc_comparison))
     458              :             invariant_only_p = false;
     459              : 
     460      5620365 :           tmp = combine_cond_expr_cond (stmt, code, type,
     461              :                                         rhs0, op1, invariant_only_p);
     462      5620365 :           if (tmp)
     463              :             return tmp;
     464              :         }
     465              :     }
     466              : 
     467              :   /* If that wasn't successful, try the second operand.  */
     468     22103549 :   if (TREE_CODE (op1) == SSA_NAME)
     469              :     {
     470      5505662 :       gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
     471      5505662 :       if (def_stmt && can_propagate_from (def_stmt))
     472              :         {
     473      1784631 :           rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
     474      3569262 :           tmp = combine_cond_expr_cond (stmt, code, type,
     475      1784631 :                                         op0, rhs1, !single_use1_p);
     476      1784631 :           if (tmp)
     477              :             return tmp;
     478              :         }
     479              :     }
     480              : 
     481              :   /* If that wasn't successful either, try both operands.  */
     482     22096796 :   if (rhs0 != NULL_TREE
     483     22096796 :       && rhs1 != NULL_TREE)
     484       924548 :     tmp = combine_cond_expr_cond (stmt, code, type,
     485              :                                   rhs0, rhs1,
     486       924548 :                                   !(single_use0_p && single_use1_p));
     487              : 
     488              :   return tmp;
     489              : }
     490              : 
     491              : /* Propagate from the ssa name definition statements of the assignment
     492              :    from a comparison at *GSI into the conditional if that simplifies it.
     493              :    Returns true if the stmt was modified.  */
     494              : 
     495              : static bool
     496      2662715 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
     497              : {
     498      2662715 :   gimple *stmt = gsi_stmt (*gsi);
     499      2662715 :   tree tmp;
     500      2662715 :   tree type = TREE_TYPE (gimple_assign_lhs (stmt));
     501      2662715 :   tree rhs1 = gimple_assign_rhs1 (stmt);
     502      2662715 :   tree rhs2 = gimple_assign_rhs2 (stmt);
     503              : 
     504              :   /* Combine the comparison with defining statements.  */
     505      2662715 :   tmp = forward_propagate_into_comparison_1 (stmt,
     506              :                                              gimple_assign_rhs_code (stmt),
     507              :                                              type, rhs1, rhs2);
     508      2662715 :   if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
     509              :     {
     510         7211 :       if (dump_file)
     511              :         {
     512            0 :           fprintf (dump_file, "  Replaced '");
     513            0 :           print_gimple_expr (dump_file, stmt, 0);
     514            0 :           fprintf (dump_file, "' with '");
     515            0 :           print_generic_expr (dump_file, tmp);
     516            0 :           fprintf (dump_file, "'\n");
     517              :         }
     518         7211 :       gimple_assign_set_rhs_from_tree (gsi, tmp);
     519         7211 :       fold_stmt (gsi);
     520         7211 :       update_stmt (gsi_stmt (*gsi));
     521              : 
     522         7211 :       if (TREE_CODE (rhs1) == SSA_NAME)
     523         7211 :         remove_prop_source_from_use (rhs1);
     524         7211 :       if (TREE_CODE (rhs2) == SSA_NAME)
     525         3024 :         remove_prop_source_from_use (rhs2);
     526         7211 :       return true;
     527              :     }
     528              : 
     529              :   return false;
     530              : }
     531              : 
     532              : /* Propagate from the ssa name definition statements of COND_EXPR
     533              :    in GIMPLE_COND statement STMT into the conditional if that simplifies it.
     534              :    Returns zero if no statement was changed, one if there were
     535              :    changes and two if cfg_cleanup needs to run.  */
     536              : 
     537              : static int
     538     19670771 : forward_propagate_into_gimple_cond (gcond *stmt)
     539              : {
     540     19670771 :   tree tmp;
     541     19670771 :   enum tree_code code = gimple_cond_code (stmt);
     542     19670771 :   tree rhs1 = gimple_cond_lhs (stmt);
     543     19670771 :   tree rhs2 = gimple_cond_rhs (stmt);
     544              : 
     545              :   /* GIMPLE_COND will always be a comparison.  */
     546     19670771 :   gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison);
     547              : 
     548     19670771 :   tmp = forward_propagate_into_comparison_1 (stmt, code,
     549              :                                              boolean_type_node,
     550              :                                              rhs1, rhs2);
     551     19670771 :   if (tmp
     552     19670771 :       && is_gimple_condexpr_for_cond (tmp))
     553              :     {
     554       225067 :       if (dump_file)
     555              :         {
     556            9 :           fprintf (dump_file, "  Replaced '");
     557            9 :           print_gimple_expr (dump_file, stmt, 0);
     558            9 :           fprintf (dump_file, "' with '");
     559            9 :           print_generic_expr (dump_file, tmp);
     560            9 :           fprintf (dump_file, "'\n");
     561              :         }
     562              : 
     563       225067 :       gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
     564       225067 :       update_stmt (stmt);
     565              : 
     566       225067 :       if (TREE_CODE (rhs1) == SSA_NAME)
     567       225067 :         remove_prop_source_from_use (rhs1);
     568       225067 :       if (TREE_CODE (rhs2) == SSA_NAME)
     569         5926 :         remove_prop_source_from_use (rhs2);
     570       225067 :       return is_gimple_min_invariant (tmp) ? 2 : 1;
     571              :     }
     572              : 
     573     19445704 :   if (canonicalize_bool_cond (stmt, gimple_bb (stmt)))
     574              :     return 1;
     575              : 
     576              :   return 0;
     577              : }
     578              : 
     579              : /* We've just substituted an ADDR_EXPR into stmt.  Update all the
     580              :    relevant data structures to match.  */
     581              : 
     582              : static void
     583      2007544 : tidy_after_forward_propagate_addr (gimple *stmt)
     584              : {
     585              :   /* We may have turned a trapping insn into a non-trapping insn.  */
     586      2007544 :   if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
     587          131 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
     588              : 
     589      2007544 :   if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
     590       253466 :      recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
     591      2007544 : }
     592              : 
     593              : /* NAME is a SSA_NAME representing DEF_RHS which is of the form
     594              :    ADDR_EXPR <whatever>.
     595              : 
     596              :    Try to forward propagate the ADDR_EXPR into the use USE_STMT.
     597              :    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
     598              :    node or for recovery of array indexing from pointer arithmetic.
     599              : 
     600              :    Return true if the propagation was successful (the propagation can
     601              :    be not totally successful, yet things may have been changed).  */
     602              : 
     603              : static bool
     604      2848461 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
     605              :                                gimple_stmt_iterator *use_stmt_gsi,
     606              :                                bool single_use_p)
     607              : {
     608      2848461 :   tree lhs, rhs, rhs2, array_ref;
     609      2848461 :   gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
     610      2848461 :   enum tree_code rhs_code;
     611      2848461 :   bool res = true;
     612              : 
     613      2848461 :   gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
     614              : 
     615      2848461 :   lhs = gimple_assign_lhs (use_stmt);
     616      2848461 :   rhs_code = gimple_assign_rhs_code (use_stmt);
     617      2848461 :   rhs = gimple_assign_rhs1 (use_stmt);
     618              : 
     619              :   /* Do not perform copy-propagation but recurse through copy chains.  */
     620      2848461 :   if (TREE_CODE (lhs) == SSA_NAME
     621      1419608 :       && rhs_code == SSA_NAME)
     622         8616 :     return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
     623              : 
     624              :   /* The use statement could be a conversion.  Recurse to the uses of the
     625              :      lhs as copyprop does not copy through pointer to integer to pointer
     626              :      conversions and FRE does not catch all cases either.
     627              :      Treat the case of a single-use name and
     628              :      a conversion to def_rhs type separate, though.  */
     629      2839845 :   if (TREE_CODE (lhs) == SSA_NAME
     630      1410992 :       && CONVERT_EXPR_CODE_P (rhs_code))
     631              :     {
     632              :       /* If there is a point in a conversion chain where the types match
     633              :          so we can remove a conversion re-materialize the address here
     634              :          and stop.  */
     635        23939 :       if (single_use_p
     636        23939 :           && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
     637              :         {
     638            1 :           gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
     639            1 :           gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
     640            1 :           return true;
     641              :         }
     642              : 
     643              :       /* Else recurse if the conversion preserves the address value.  */
     644        47876 :       if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
     645            2 :            || POINTER_TYPE_P (TREE_TYPE (lhs)))
     646        47876 :           && (TYPE_PRECISION (TREE_TYPE (lhs))
     647        23938 :               >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
     648        23871 :         return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
     649              : 
     650              :       return false;
     651              :     }
     652              : 
     653              :   /* If this isn't a conversion chain from this on we only can propagate
     654              :      into compatible pointer contexts.  */
     655      2815906 :   if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
     656              :     return false;
     657              : 
     658              :   /* Propagate through constant pointer adjustments.  */
     659      2795308 :   if (TREE_CODE (lhs) == SSA_NAME
     660      1367628 :       && rhs_code == POINTER_PLUS_EXPR
     661      1367628 :       && rhs == name
     662      2962167 :       && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
     663              :     {
     664       118428 :       tree new_def_rhs;
     665              :       /* As we come here with non-invariant addresses in def_rhs we need
     666              :          to make sure we can build a valid constant offsetted address
     667              :          for further propagation.  Simply rely on fold building that
     668              :          and check after the fact.  */
     669       118428 :       new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
     670              :                                  def_rhs,
     671              :                                  fold_convert (ptr_type_node,
     672              :                                                gimple_assign_rhs2 (use_stmt)));
     673       118428 :       if (TREE_CODE (new_def_rhs) == MEM_REF
     674       118428 :           && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
     675              :         return false;
     676       114467 :       new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs);
     677              : 
     678              :       /* Recurse.  If we could propagate into all uses of lhs do not
     679              :          bother to replace into the current use but just pretend we did.  */
     680       114467 :       if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
     681              :         return true;
     682              : 
     683        38371 :       if (useless_type_conversion_p (TREE_TYPE (lhs),
     684        38371 :                                      TREE_TYPE (new_def_rhs)))
     685        38371 :         gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
     686              :                                         new_def_rhs);
     687            0 :       else if (is_gimple_min_invariant (new_def_rhs))
     688            0 :         gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs);
     689              :       else
     690              :         return false;
     691        38371 :       gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
     692        38371 :       update_stmt (use_stmt);
     693        38371 :       return true;
     694              :     }
     695              : 
     696              :   /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
     697              :      ADDR_EXPR will not appear on the LHS.  */
     698      2676880 :   tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
     699      4041822 :   while (handled_component_p (*lhsp))
     700      1364942 :     lhsp = &TREE_OPERAND (*lhsp, 0);
     701      2676880 :   lhs = *lhsp;
     702              : 
     703              :   /* Now see if the LHS node is a MEM_REF using NAME.  If so,
     704              :      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
     705      2676880 :   if (TREE_CODE (lhs) == MEM_REF
     706      2676880 :       && TREE_OPERAND (lhs, 0) == name)
     707              :     {
     708       903734 :       tree def_rhs_base;
     709       903734 :       poly_int64 def_rhs_offset;
     710              :       /* If the address is invariant we can always fold it.  */
     711       903734 :       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
     712              :                                                          &def_rhs_offset)))
     713              :         {
     714       856628 :           poly_offset_int off = mem_ref_offset (lhs);
     715       856628 :           tree new_ptr;
     716       856628 :           off += def_rhs_offset;
     717       856628 :           if (TREE_CODE (def_rhs_base) == MEM_REF)
     718              :             {
     719       835250 :               off += mem_ref_offset (def_rhs_base);
     720       835250 :               new_ptr = TREE_OPERAND (def_rhs_base, 0);
     721              :             }
     722              :           else
     723        21378 :             new_ptr = build_fold_addr_expr (def_rhs_base);
     724       856628 :           TREE_OPERAND (lhs, 0) = new_ptr;
     725       856628 :           TREE_OPERAND (lhs, 1)
     726       856628 :             = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
     727       856628 :           tidy_after_forward_propagate_addr (use_stmt);
     728              :           /* Continue propagating into the RHS if this was not the only use.  */
     729       856628 :           if (single_use_p)
     730       231194 :             return true;
     731              :         }
     732              :       /* If the LHS is a plain dereference and the value type is the same as
     733              :          that of the pointed-to type of the address we can put the
     734              :          dereferenced address on the LHS preserving the original alias-type.  */
     735        47106 :       else if (integer_zerop (TREE_OPERAND (lhs, 1))
     736        18520 :                && ((gimple_assign_lhs (use_stmt) == lhs
     737        14893 :                     && useless_type_conversion_p
     738        14893 :                          (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
     739        14893 :                           TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
     740        13598 :                    || types_compatible_p (TREE_TYPE (lhs),
     741        13598 :                                           TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
     742              :                /* Don't forward anything into clobber stmts if it would result
     743              :                   in the lhs no longer being a MEM_REF.  */
     744        55284 :                && (!gimple_clobber_p (use_stmt)
     745          164 :                    || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
     746              :         {
     747         8014 :           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
     748         8014 :           tree new_offset, new_base, saved, new_lhs;
     749        29081 :           while (handled_component_p (*def_rhs_basep))
     750        13053 :             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
     751         8014 :           saved = *def_rhs_basep;
     752         8014 :           if (TREE_CODE (*def_rhs_basep) == MEM_REF)
     753              :             {
     754         3941 :               new_base = TREE_OPERAND (*def_rhs_basep, 0);
     755         3941 :               new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
     756              :                                          TREE_OPERAND (*def_rhs_basep, 1));
     757              :             }
     758              :           else
     759              :             {
     760         4073 :               new_base = build_fold_addr_expr (*def_rhs_basep);
     761         4073 :               new_offset = TREE_OPERAND (lhs, 1);
     762              :             }
     763         8014 :           tree atype = TREE_TYPE (*def_rhs_basep);
     764         8014 :           if (TYPE_ALIGN (TREE_TYPE (lhs)) < TYPE_ALIGN (atype))
     765          312 :             atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (lhs)));
     766         8014 :           *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
     767         8014 :           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
     768         8014 :           TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
     769         8014 :           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
     770         8014 :           new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
     771         8014 :           *lhsp = new_lhs;
     772         8014 :           TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
     773         8014 :           TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
     774         8014 :           *def_rhs_basep = saved;
     775         8014 :           tidy_after_forward_propagate_addr (use_stmt);
     776              :           /* Continue propagating into the RHS if this was not the
     777              :              only use.  */
     778         8014 :           if (single_use_p)
     779              :             return true;
     780              :         }
     781              :       else
     782              :         /* We can have a struct assignment dereferencing our name twice.
     783              :            Note that we didn't propagate into the lhs to not falsely
     784              :            claim we did when propagating into the rhs.  */
     785              :         res = false;
     786              :     }
     787              : 
     788              :   /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
     789              :      nodes from the RHS.  */
     790      2441479 :   tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
     791      2441479 :   if (TREE_CODE (*rhsp) == ADDR_EXPR)
     792       241434 :     rhsp = &TREE_OPERAND (*rhsp, 0);
     793      3461844 :   while (handled_component_p (*rhsp))
     794      1020365 :     rhsp = &TREE_OPERAND (*rhsp, 0);
     795      2441479 :   rhs = *rhsp;
     796              : 
     797              :   /* Now see if the RHS node is a MEM_REF using NAME.  If so,
     798              :      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
     799      2441479 :   if (TREE_CODE (rhs) == MEM_REF
     800      2441479 :       && TREE_OPERAND (rhs, 0) == name)
     801              :     {
     802      1164654 :       tree def_rhs_base;
     803      1164654 :       poly_int64 def_rhs_offset;
     804      1164654 :       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
     805              :                                                          &def_rhs_offset)))
     806              :         {
     807      1127633 :           poly_offset_int off = mem_ref_offset (rhs);
     808      1127633 :           tree new_ptr;
     809      1127633 :           off += def_rhs_offset;
     810      1127633 :           if (TREE_CODE (def_rhs_base) == MEM_REF)
     811              :             {
     812      1101147 :               off += mem_ref_offset (def_rhs_base);
     813      1101147 :               new_ptr = TREE_OPERAND (def_rhs_base, 0);
     814              :             }
     815              :           else
     816        26486 :             new_ptr = build_fold_addr_expr (def_rhs_base);
     817      1127633 :           TREE_OPERAND (rhs, 0) = new_ptr;
     818      1127633 :           TREE_OPERAND (rhs, 1)
     819      1127633 :             = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
     820      1127633 :           fold_stmt_inplace (use_stmt_gsi);
     821      1127633 :           tidy_after_forward_propagate_addr (use_stmt);
     822      1127633 :           return res;
     823              :         }
     824              :       /* If the RHS is a plain dereference and the value type is the same as
     825              :          that of the pointed-to type of the address we can put the
     826              :          dereferenced address on the RHS preserving the original alias-type.  */
     827        37021 :       else if (integer_zerop (TREE_OPERAND (rhs, 1))
     828        37021 :                && ((gimple_assign_rhs1 (use_stmt) == rhs
     829        20263 :                     && useless_type_conversion_p
     830        20263 :                          (TREE_TYPE (gimple_assign_lhs (use_stmt)),
     831        20263 :                           TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
     832        22533 :                    || types_compatible_p (TREE_TYPE (rhs),
     833        22533 :                                           TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
     834              :         {
     835        15269 :           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
     836        15269 :           tree new_offset, new_base, saved, new_rhs;
     837        54333 :           while (handled_component_p (*def_rhs_basep))
     838        23795 :             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
     839        15269 :           saved = *def_rhs_basep;
     840        15269 :           if (TREE_CODE (*def_rhs_basep) == MEM_REF)
     841              :             {
     842         7359 :               new_base = TREE_OPERAND (*def_rhs_basep, 0);
     843         7359 :               new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
     844              :                                          TREE_OPERAND (*def_rhs_basep, 1));
     845              :             }
     846              :           else
     847              :             {
     848         7910 :               new_base = build_fold_addr_expr (*def_rhs_basep);
     849         7910 :               new_offset = TREE_OPERAND (rhs, 1);
     850              :             }
     851        15269 :           tree atype = TREE_TYPE (*def_rhs_basep);
     852        15269 :           if (TYPE_ALIGN (TREE_TYPE (rhs)) < TYPE_ALIGN (atype))
     853          526 :             atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (rhs)));
     854        15269 :           *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
     855        15269 :           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
     856        15269 :           TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
     857        15269 :           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
     858        15269 :           new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
     859        15269 :           *rhsp = new_rhs;
     860        15269 :           TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
     861        15269 :           TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
     862        15269 :           *def_rhs_basep = saved;
     863        15269 :           fold_stmt_inplace (use_stmt_gsi);
     864        15269 :           tidy_after_forward_propagate_addr (use_stmt);
     865        15269 :           return res;
     866              :         }
     867              :     }
     868              : 
     869              :   /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
     870              :      is nothing to do. */
     871      1298577 :   if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
     872      1298577 :       || gimple_assign_rhs1 (use_stmt) != name)
     873              :     return false;
     874              : 
     875              :   /* The remaining cases are all for turning pointer arithmetic into
     876              :      array indexing.  They only apply when we have the address of
     877              :      element zero in an array.  If that is not the case then there
     878              :      is nothing to do.  */
     879        48431 :   array_ref = TREE_OPERAND (def_rhs, 0);
     880        48431 :   if ((TREE_CODE (array_ref) != ARRAY_REF
     881         4570 :        || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
     882         4570 :        || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
     883        49930 :       && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
     884              :     return false;
     885              : 
     886        24807 :   rhs2 = gimple_assign_rhs2 (use_stmt);
     887              :   /* Optimize &x[C1] p+ C2 to  &x p+ C3 with C3 = C1 * element_size + C2.  */
     888        24807 :   if (TREE_CODE (rhs2) == INTEGER_CST)
     889              :     {
     890            0 :       tree new_rhs = build1_loc (gimple_location (use_stmt),
     891            0 :                                  ADDR_EXPR, TREE_TYPE (def_rhs),
     892            0 :                                  fold_build2 (MEM_REF,
     893              :                                               TREE_TYPE (TREE_TYPE (def_rhs)),
     894              :                                               unshare_expr (def_rhs),
     895              :                                               fold_convert (ptr_type_node,
     896              :                                                             rhs2)));
     897            0 :       gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
     898            0 :       use_stmt = gsi_stmt (*use_stmt_gsi);
     899            0 :       update_stmt (use_stmt);
     900            0 :       tidy_after_forward_propagate_addr (use_stmt);
     901            0 :       return true;
     902              :     }
     903              : 
     904              :   return false;
     905              : }
     906              : 
     907              : /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
     908              : 
     909              :    Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
     910              :    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
     911              :    node or for recovery of array indexing from pointer arithmetic.
     912              : 
     913              :    PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
     914              :    the single use in the previous invocation.  Pass true when calling
     915              :    this as toplevel.
     916              : 
     917              :    Returns true, if all uses have been propagated into.  */
     918              : 
     919              : static bool
     920      3301377 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
     921              : {
     922      3301377 :   bool all = true;
     923      3301377 :   bool single_use_p = parent_single_use_p && has_single_use (name);
     924              : 
     925     17458870 :   for (gimple *use_stmt : gather_imm_use_stmts (name))
     926              :     {
     927      7554739 :       bool result;
     928      7554739 :       tree use_rhs;
     929              : 
     930              :       /* If the use is not in a simple assignment statement, then
     931              :          there is nothing we can do.  */
     932      7554739 :       if (!is_gimple_assign (use_stmt))
     933              :         {
     934      4706278 :           if (!is_gimple_debug (use_stmt))
     935      1929180 :             all = false;
     936      4706278 :           continue;
     937              :         }
     938              : 
     939      2848461 :       gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
     940      2848461 :       result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
     941              :                                               single_use_p);
     942              :       /* If the use has moved to a different statement adjust
     943              :          the update machinery for the old statement too.  */
     944      2848461 :       if (use_stmt != gsi_stmt (gsi))
     945              :         {
     946            0 :           update_stmt (use_stmt);
     947            0 :           use_stmt = gsi_stmt (gsi);
     948              :         }
     949      2848461 :       update_stmt (use_stmt);
     950      2848461 :       all &= result;
     951              : 
     952              :       /* Remove intermediate now unused copy and conversion chains.  */
     953      2848461 :       use_rhs = gimple_assign_rhs1 (use_stmt);
     954      2848461 :       if (result
     955      1497981 :           && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
     956      1249937 :           && TREE_CODE (use_rhs) == SSA_NAME
     957      2929767 :           && has_zero_uses (gimple_assign_lhs (use_stmt)))
     958              :         {
     959        81306 :           gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
     960        81306 :           fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
     961        81306 :           release_defs (use_stmt);
     962        81306 :           gsi_remove (&gsi, true);
     963              :         }
     964      3301377 :     }
     965              : 
     966      3301377 :   return all && has_zero_uses (name);
     967              : }
     968              : 
     969              : 
     970              : /* Helper function for simplify_gimple_switch.  Remove case labels that
     971              :    have values outside the range of the new type.  */
     972              : 
     973              : static void
     974        11706 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
     975              :                                   vec<std::pair<int, int> > &edges_to_remove)
     976              : {
     977        11706 :   unsigned int branch_num = gimple_switch_num_labels (stmt);
     978        11706 :   auto_vec<tree> labels (branch_num);
     979        11706 :   unsigned int i, len;
     980              : 
     981              :   /* Collect the existing case labels in a VEC, and preprocess it as if
     982              :      we are gimplifying a GENERIC SWITCH_EXPR.  */
     983        73642 :   for (i = 1; i < branch_num; i++)
     984        50230 :     labels.quick_push (gimple_switch_label (stmt, i));
     985        11706 :   preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
     986              : 
     987              :   /* If any labels were removed, replace the existing case labels
     988              :      in the GIMPLE_SWITCH statement with the correct ones.
     989              :      Note that the type updates were done in-place on the case labels,
     990              :      so we only have to replace the case labels in the GIMPLE_SWITCH
     991              :      if the number of labels changed.  */
     992        11706 :   len = labels.length ();
     993        11706 :   if (len < branch_num - 1)
     994              :     {
     995            0 :       bitmap target_blocks;
     996            0 :       edge_iterator ei;
     997            0 :       edge e;
     998              : 
     999              :       /* Corner case: *all* case labels have been removed as being
    1000              :          out-of-range for INDEX_TYPE.  Push one label and let the
    1001              :          CFG cleanups deal with this further.  */
    1002            0 :       if (len == 0)
    1003              :         {
    1004            0 :           tree label, elt;
    1005              : 
    1006            0 :           label = CASE_LABEL (gimple_switch_default_label (stmt));
    1007            0 :           elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
    1008            0 :           labels.quick_push (elt);
    1009            0 :           len = 1;
    1010              :         }
    1011              : 
    1012            0 :       for (i = 0; i < labels.length (); i++)
    1013            0 :         gimple_switch_set_label (stmt, i + 1, labels[i]);
    1014            0 :       for (i++ ; i < branch_num; i++)
    1015            0 :         gimple_switch_set_label (stmt, i, NULL_TREE);
    1016            0 :       gimple_switch_set_num_labels (stmt, len + 1);
    1017              : 
    1018              :       /* Cleanup any edges that are now dead.  */
    1019            0 :       target_blocks = BITMAP_ALLOC (NULL);
    1020            0 :       for (i = 0; i < gimple_switch_num_labels (stmt); i++)
    1021              :         {
    1022            0 :           tree elt = gimple_switch_label (stmt, i);
    1023            0 :           basic_block target = label_to_block (cfun, CASE_LABEL (elt));
    1024            0 :           bitmap_set_bit (target_blocks, target->index);
    1025              :         }
    1026            0 :       for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
    1027              :         {
    1028            0 :           if (! bitmap_bit_p (target_blocks, e->dest->index))
    1029            0 :             edges_to_remove.safe_push (std::make_pair (e->src->index,
    1030            0 :                                                        e->dest->index));
    1031              :           else
    1032            0 :             ei_next (&ei);
    1033              :         }
    1034            0 :       BITMAP_FREE (target_blocks);
    1035              :     }
    1036        11706 : }
    1037              : 
    1038              : /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
    1039              :    the condition which we may be able to optimize better.  */
    1040              : 
    1041              : static bool
    1042       107050 : simplify_gimple_switch (gswitch *stmt,
    1043              :                         vec<std::pair<int, int> > &edges_to_remove,
    1044              :                         bitmap simple_dce_worklist)
    1045              : {
    1046              :   /* The optimization that we really care about is removing unnecessary
    1047              :      casts.  That will let us do much better in propagating the inferred
    1048              :      constant at the switch target.  */
    1049       107050 :   tree cond = gimple_switch_index (stmt);
    1050       107050 :   if (TREE_CODE (cond) == SSA_NAME)
    1051              :     {
    1052       107049 :       gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
    1053       107049 :       if (gimple_assign_cast_p (def_stmt))
    1054              :         {
    1055        12630 :           tree def = gimple_assign_rhs1 (def_stmt);
    1056        12630 :           if (TREE_CODE (def) != SSA_NAME)
    1057              :             return false;
    1058              : 
    1059              :           /* If we have an extension or sign-change that preserves the
    1060              :              values we check against then we can copy the source value into
    1061              :              the switch.  */
    1062        12630 :           tree ti = TREE_TYPE (def);
    1063        12630 :           if (INTEGRAL_TYPE_P (ti)
    1064        12630 :               && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
    1065              :             {
    1066        12385 :               size_t n = gimple_switch_num_labels (stmt);
    1067        12385 :               tree min = NULL_TREE, max = NULL_TREE;
    1068        12385 :               if (n > 1)
    1069              :                 {
    1070        12385 :                   min = CASE_LOW (gimple_switch_label (stmt, 1));
    1071        12385 :                   if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
    1072          159 :                     max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
    1073              :                   else
    1074        12226 :                     max = CASE_LOW (gimple_switch_label (stmt, n - 1));
    1075              :                 }
    1076        12385 :               if ((!min || int_fits_type_p (min, ti))
    1077        12381 :                   && (!max || int_fits_type_p (max, ti)))
    1078              :                 {
    1079        11706 :                   bitmap_set_bit (simple_dce_worklist,
    1080        11706 :                                   SSA_NAME_VERSION (cond));
    1081        11706 :                   gimple_switch_set_index (stmt, def);
    1082        11706 :                   simplify_gimple_switch_label_vec (stmt, ti,
    1083              :                                                     edges_to_remove);
    1084        11706 :                   update_stmt (stmt);
    1085        11706 :                   return true;
    1086              :                 }
    1087              :             }
    1088              :         }
    1089              :     }
    1090              : 
    1091              :   return false;
    1092              : }
    1093              : 
    1094              : /* For pointers p2 and p1 return p2 - p1 if the
    1095              :    difference is known and constant, otherwise return NULL.  */
    1096              : 
    1097              : static tree
    1098         5360 : constant_pointer_difference (tree p1, tree p2)
    1099              : {
    1100         5360 :   int i, j;
    1101              : #define CPD_ITERATIONS 5
    1102         5360 :   tree exps[2][CPD_ITERATIONS];
    1103         5360 :   tree offs[2][CPD_ITERATIONS];
    1104         5360 :   int cnt[2];
    1105              : 
    1106        16080 :   for (i = 0; i < 2; i++)
    1107              :     {
    1108        10720 :       tree p = i ? p1 : p2;
    1109        10720 :       tree off = size_zero_node;
    1110        10720 :       gimple *stmt;
    1111        10720 :       enum tree_code code;
    1112              : 
    1113              :       /* For each of p1 and p2 we need to iterate at least
    1114              :          twice, to handle ADDR_EXPR directly in p1/p2,
    1115              :          SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
    1116              :          on definition's stmt RHS.  Iterate a few extra times.  */
    1117        10720 :       j = 0;
    1118        12659 :       do
    1119              :         {
    1120        12659 :           if (!POINTER_TYPE_P (TREE_TYPE (p)))
    1121              :             break;
    1122        12653 :           if (TREE_CODE (p) == ADDR_EXPR)
    1123              :             {
    1124         9241 :               tree q = TREE_OPERAND (p, 0);
    1125         9241 :               poly_int64 offset;
    1126         9241 :               tree base = get_addr_base_and_unit_offset (q, &offset);
    1127         9241 :               if (base)
    1128              :                 {
    1129         8425 :                   q = base;
    1130         8425 :                   if (maybe_ne (offset, 0))
    1131         3537 :                     off = size_binop (PLUS_EXPR, off, size_int (offset));
    1132              :                 }
    1133         9241 :               if (TREE_CODE (q) == MEM_REF
    1134         9241 :                   && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
    1135              :                 {
    1136          213 :                   p = TREE_OPERAND (q, 0);
    1137          213 :                   off = size_binop (PLUS_EXPR, off,
    1138              :                                     wide_int_to_tree (sizetype,
    1139              :                                                       mem_ref_offset (q)));
    1140              :                 }
    1141              :               else
    1142              :                 {
    1143         9028 :                   exps[i][j] = q;
    1144         9028 :                   offs[i][j++] = off;
    1145         9028 :                   break;
    1146              :                 }
    1147              :             }
    1148         3625 :           if (TREE_CODE (p) != SSA_NAME)
    1149              :             break;
    1150         3625 :           exps[i][j] = p;
    1151         3625 :           offs[i][j++] = off;
    1152         3625 :           if (j == CPD_ITERATIONS)
    1153              :             break;
    1154         3625 :           stmt = SSA_NAME_DEF_STMT (p);
    1155         3625 :           if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
    1156              :             break;
    1157         2843 :           code = gimple_assign_rhs_code (stmt);
    1158         2843 :           if (code == POINTER_PLUS_EXPR)
    1159              :             {
    1160         1485 :               if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
    1161              :                 break;
    1162          954 :               off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
    1163          954 :               p = gimple_assign_rhs1 (stmt);
    1164              :             }
    1165         1358 :           else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code))
    1166          985 :             p = gimple_assign_rhs1 (stmt);
    1167              :           else
    1168              :             break;
    1169              :         }
    1170              :       while (1);
    1171        10720 :       cnt[i] = j;
    1172              :     }
    1173              : 
    1174         7441 :   for (i = 0; i < cnt[0]; i++)
    1175         9877 :     for (j = 0; j < cnt[1]; j++)
    1176         7796 :       if (exps[0][i] == exps[1][j])
    1177         4467 :         return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
    1178              : 
    1179              :   return NULL_TREE;
    1180              : }
    1181              : 
    1182              : /* Helper function for optimize_aggr_zeroprop.
    1183              :    Props the zeroing (memset, VAL) that was done in DEST+OFFSET:LEN
    1184              :    (DEFSTMT) into the STMT.  Returns true if the STMT was updated.  */
    1185              : static void
    1186     22510976 : optimize_aggr_zeroprop_1 (gimple *defstmt, gimple *stmt,
    1187              :                           tree dest, poly_int64 offset, tree val,
    1188              :                           poly_offset_int len)
    1189              : {
    1190     22510976 :   tree src2;
    1191     22510976 :   tree len2 = NULL_TREE;
    1192     22510976 :   poly_int64 offset2;
    1193              : 
    1194     22510976 :   if (gimple_call_builtin_p (stmt, BUILT_IN_MEMCPY)
    1195        22136 :       && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR
    1196     22524987 :       && poly_int_tree_p (gimple_call_arg (stmt, 2)))
    1197              :     {
    1198        12977 :       src2 = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
    1199        12977 :       len2 = gimple_call_arg (stmt, 2);
    1200              :     }
    1201     22497999 :    else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
    1202              :      {
    1203      1908170 :         src2 = gimple_assign_rhs1 (stmt);
    1204      1908170 :         len2 = (TREE_CODE (src2) == COMPONENT_REF
    1205      1908170 :                 ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
    1206      1740551 :                 : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
    1207              :         /* Can only handle zero memsets. */
    1208      1908170 :         if (!integer_zerop (val))
    1209     22488353 :           return;
    1210              :      }
    1211              :    else
    1212     20589829 :      return;
    1213              : 
    1214      1920141 :   if (len2 == NULL_TREE
    1215      1920141 :       || !poly_int_tree_p (len2))
    1216              :     return;
    1217              : 
    1218      1920141 :   src2 = get_addr_base_and_unit_offset (src2, &offset2);
    1219      1920141 :   if (src2 == NULL_TREE
    1220      1920141 :       || maybe_lt (offset2, offset))
    1221              :     return;
    1222              : 
    1223       875886 :   if (!operand_equal_p (dest, src2, 0))
    1224              :     return;
    1225              : 
    1226              :   /* [ dest + offset, dest + offset + len - 1 ] is set to val.
    1227              :      Make sure that
    1228              :      [ dest + offset2, dest + offset2 + len2 - 1 ] is a subset of that.  */
    1229       132526 :   if (maybe_gt (wi::to_poly_offset (len2) + (offset2 - offset),
    1230              :                 len))
    1231              :     return;
    1232              : 
    1233        22623 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1234              :     {
    1235           32 :       fprintf (dump_file, "Simplified\n  ");
    1236           32 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1237           32 :       fprintf (dump_file, "after previous\n  ");
    1238           32 :       print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1239              :     }
    1240        22623 :   gimple *orig_stmt = stmt;
    1241              :   /* For simplicity, don't change the kind of the stmt,
    1242              :      turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
    1243              :      into memset (&dest, val, len);
    1244              :      In theory we could change dest = src into memset if dest
    1245              :      is addressable (maybe beneficial if val is not 0), or
    1246              :      memcpy (&dest, &src, len) into dest = {} if len is the size
    1247              :      of dest, dest isn't volatile.  */
    1248        22623 :   if (is_gimple_assign (stmt))
    1249              :     {
    1250        22618 :       tree ctor_type = TREE_TYPE (gimple_assign_lhs (stmt));
    1251        22618 :       tree ctor = build_constructor (ctor_type, NULL);
    1252        22618 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    1253        22618 :       gimple_assign_set_rhs_from_tree (&gsi, ctor);
    1254        22618 :       update_stmt (stmt);
    1255        22618 :       statistics_counter_event (cfun, "copy zeroing propagation of aggregate", 1);
    1256              :     }
    1257              :   else /* If stmt is memcpy, transform it into memset.  */
    1258              :     {
    1259            5 :       gcall *call = as_a <gcall *> (stmt);
    1260            5 :       tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET);
    1261            5 :       gimple_call_set_fndecl (call, fndecl);
    1262            5 :       gimple_call_set_fntype (call, TREE_TYPE (fndecl));
    1263            5 :       gimple_call_set_arg (call, 1, val);
    1264            5 :       update_stmt (stmt);
    1265            5 :       statistics_counter_event (cfun, "memcpy to memset changed", 1);
    1266              :     }
    1267              : 
    1268        22623 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1269              :     {
    1270           32 :       fprintf (dump_file, "into\n  ");
    1271           32 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1272              :     }
    1273              : 
    1274              :   /* Mark the bb for eh cleanup if needed.  */
    1275        22623 :   if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
    1276            6 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
    1277              : }
    1278              : 
    1279              : /* Optimize
    1280              :    a = {}; // DEST = value ;; LEN(nullptr)
    1281              :    b = a;
    1282              :    into
    1283              :    a = {};
    1284              :    b = {};
    1285              :    Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
    1286              :    and/or memcpy (&b, &a, sizeof (a)); instead of b = a;  */
    1287              : 
    1288              : static void
    1289     31389573 : optimize_aggr_zeroprop (gimple *stmt, bool full_walk)
    1290              : {
    1291     31389573 :   ao_ref read;
    1292     62779146 :   if (gimple_has_volatile_ops (stmt))
    1293     27333649 :     return;
    1294              : 
    1295     30463407 :   tree dest = NULL_TREE;
    1296     30463407 :   tree val = integer_zero_node;
    1297     30463407 :   tree len = NULL_TREE;
    1298     30463407 :   bool can_use_tbba = true;
    1299              : 
    1300     30463407 :   if (gimple_call_builtin_p (stmt, BUILT_IN_MEMSET)
    1301       111851 :       && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR
    1302        56975 :       && TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
    1303     30518066 :       && poly_int_tree_p (gimple_call_arg (stmt, 2)))
    1304              :     {
    1305        51894 :       dest = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
    1306        51894 :       len = gimple_call_arg (stmt, 2);
    1307        51894 :       val = gimple_call_arg (stmt, 1);
    1308        51894 :       ao_ref_init_from_ptr_and_size (&read, gimple_call_arg (stmt, 0), len);
    1309        51894 :       can_use_tbba = false;
    1310              :     }
    1311     30411513 :   else if (gimple_store_p (stmt)
    1312     30351396 :            && gimple_assign_single_p (stmt)
    1313     60762909 :            && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST)
    1314              :     {
    1315        26377 :       tree str = gimple_assign_rhs1 (stmt);
    1316        26377 :       dest = gimple_assign_lhs (stmt);
    1317        26377 :       ao_ref_init (&read, dest);
    1318              :       /* The string must contain all null char's for now.  */
    1319        31719 :       for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
    1320              :         {
    1321        29131 :           if (TREE_STRING_POINTER (str)[i] != 0)
    1322              :             {
    1323              :               dest = NULL_TREE;
    1324              :               break;
    1325              :             }
    1326              :         }
    1327              :     }
    1328              :   /* A store of integer (scalar, vector or complex) zeros is
    1329              :      a zero store. */
    1330     30385136 :   else if (gimple_store_p (stmt)
    1331     30325019 :            && gimple_assign_single_p (stmt)
    1332     60710155 :            && integer_zerop (gimple_assign_rhs1 (stmt)))
    1333              :     {
    1334      3586595 :       tree rhs = gimple_assign_rhs1 (stmt);
    1335      3586595 :       tree type = TREE_TYPE (rhs);
    1336      3586595 :       dest = gimple_assign_lhs (stmt);
    1337      3586595 :       ao_ref_init (&read, dest);
    1338              :       /* For integral types, the type precision needs to be a multiply of BITS_PER_UNIT. */
    1339      3586595 :       if (INTEGRAL_TYPE_P (type)
    1340      3586595 :           && (TYPE_PRECISION (type) % BITS_PER_UNIT) != 0)
    1341              :         dest = NULL_TREE;
    1342              :     }
    1343     26798541 :   else if (gimple_store_p (stmt)
    1344     26738424 :            && gimple_assign_single_p (stmt)
    1345     26738424 :            && TREE_CODE (gimple_assign_rhs1 (stmt)) == CONSTRUCTOR
    1346     27510861 :            && !gimple_clobber_p (stmt))
    1347              :     {
    1348       712320 :       dest = gimple_assign_lhs (stmt);
    1349       712320 :       ao_ref_init (&read, dest);
    1350              :     }
    1351              : 
    1352      4165307 :   if (dest == NULL_TREE)
    1353     26321889 :     return;
    1354              : 
    1355      4141518 :   if (len == NULL_TREE)
    1356      4089624 :     len = (TREE_CODE (dest) == COMPONENT_REF
    1357      4089624 :            ? DECL_SIZE_UNIT (TREE_OPERAND (dest, 1))
    1358      1775910 :            : TYPE_SIZE_UNIT (TREE_TYPE (dest)));
    1359      4089624 :   if (len == NULL_TREE
    1360      4141518 :       || !poly_int_tree_p (len))
    1361              :     return;
    1362              : 
    1363              :   /* Sometimes memset can have no vdef due to invalid declaration of memset (const, etc.).  */
    1364     35616661 :   if (!gimple_vdef (stmt))
    1365              :     return;
    1366              : 
    1367              :   /* This store needs to be on the byte boundary and pointing to an object.  */
    1368      4141494 :   poly_int64 offset;
    1369      4141494 :   tree dest_base = get_addr_base_and_unit_offset (dest, &offset);
    1370      4141494 :   if (dest_base == NULL_TREE)
    1371              :     return;
    1372              : 
    1373              :   /* Setup the worklist.  */
    1374      4055924 :   auto_vec<std::pair<tree, unsigned>> worklist;
    1375      4055924 :   unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 0;
    1376      8111848 :   worklist.safe_push (std::make_pair (gimple_vdef (stmt), limit));
    1377              : 
    1378     28109463 :   while (!worklist.is_empty ())
    1379              :     {
    1380     19997615 :       std::pair<tree, unsigned> top = worklist.pop ();
    1381     19997615 :       tree vdef = top.first;
    1382     19997615 :       limit = top.second;
    1383     19997615 :       gimple *use_stmt;
    1384     19997615 :       imm_use_iterator iter;
    1385     64527863 :       FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
    1386              :         {
    1387              :           /* Handling PHI nodes might not be worth it so don't.  */
    1388     24532633 :           if (is_a <gphi*> (use_stmt))
    1389      2021657 :             continue;
    1390              : 
    1391              :           /* If this statement does not clobber add the vdef stmt to the
    1392              :              worklist.
    1393              :              After hitting the limit, allow clobbers to able to pass through.  */
    1394      2048969 :           if ((limit != 0 || gimple_clobber_p (use_stmt))
    1395     20497963 :               && gimple_vdef (use_stmt)
    1396     39896685 :               && !stmt_may_clobber_ref_p_1 (use_stmt, &read,
    1397              :                                            /* tbaa_p = */ can_use_tbba))
    1398              :               {
    1399     15941691 :                 unsigned new_limit = limit == 0 ? 0 : limit - 1;
    1400     31883382 :                 worklist.safe_push (std::make_pair (gimple_vdef (use_stmt),
    1401              :                                                     new_limit));
    1402              :               }
    1403              : 
    1404     22510976 :            optimize_aggr_zeroprop_1 (stmt, use_stmt, dest_base, offset,
    1405     22510976 :                                      val, wi::to_poly_offset (len));
    1406     19997615 :         }
    1407              :     }
    1408              : 
    1409      4055924 : }
    1410              : 
    1411              : /* Returns the pointer to the base of the object of the
    1412              :    reference EXPR and extracts the information about
    1413              :    the offset of the access, storing it to PBYTESIZE,
    1414              :    PBYTEPOS and PREVERSEP.
    1415              :    If the access is not a byte sized or position is not
    1416              :    on the byte, return NULL.  */
    1417              : static tree
    1418      5314778 : split_core_and_offset_size (tree expr,
    1419              :                             poly_int64 *pbytesize, poly_int64 *pbytepos,
    1420              :                             tree *poffset, int *preversep)
    1421              : {
    1422      5314778 :   tree core;
    1423      5314778 :   machine_mode mode;
    1424      5314778 :   int unsignedp, volatilep;
    1425      5314778 :   poly_int64 bitsize;
    1426      5314778 :   poly_int64 bitpos;
    1427      5314778 :   location_t loc = EXPR_LOCATION (expr);
    1428              : 
    1429      5314778 :   core = get_inner_reference (expr, &bitsize, &bitpos,
    1430              :                               poffset, &mode, &unsignedp, preversep,
    1431              :                               &volatilep);
    1432     10629556 :   if (!multiple_p (bitsize, BITS_PER_UNIT, pbytesize))
    1433              :     return NULL_TREE;
    1434      5314778 :   if (!multiple_p (bitpos, BITS_PER_UNIT, pbytepos))
    1435              :     return NULL_TREE;
    1436              :   /* If we are left with MEM[a + CST] strip that and add it to the
    1437              :      pbytepos and return a. */
    1438      5314778 :   if (TREE_CODE (core) == MEM_REF)
    1439              :     {
    1440      1240593 :       poly_offset_int tem;
    1441      1240593 :       tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
    1442      1240593 :       tem += *pbytepos;
    1443      1240593 :       if (tem.to_shwi (pbytepos))
    1444      1238707 :         return TREE_OPERAND (core, 0);
    1445              :     }
    1446      4076071 :   core = build_fold_addr_expr_loc (loc, core);
    1447      4076071 :   STRIP_NOPS (core);
    1448      4076071 :   return core;
    1449              : }
    1450              : 
    1451              : /* Returns a new src based on the
    1452              :    copy `DEST = SRC` and for the old SRC2.
    1453              :    Returns null if SRC2 is not related to DEST.  */
    1454              : 
    1455              : static tree
    1456      1263223 : new_src_based_on_copy (tree src2, tree dest, tree src)
    1457              : {
    1458              :   /* If the second src is not exactly the same as dest,
    1459              :      try to handle it separately; see it is address/size equivalent.
    1460              :      Handles `a` and `a.b` and `MEM<char[N]>(&a)` which all have
    1461              :      the same size and offsets as address/size equivalent.
    1462              :      This allows copying over a memcpy and also one for copying
    1463              :      where one field is the same size as the whole struct.  */
    1464      1263223 :   if (operand_equal_p (dest, src2))
    1465              :     return src;
    1466              :   /* if both dest and src2 are decls, then we know these 2
    1467              :      accesses can't be the same.  */
    1468       720216 :   if (DECL_P (dest) && DECL_P (src2))
    1469              :     return NULL_TREE;
    1470              :   /* A VCE can't be used with imag/real or BFR so reject them early. */
    1471       379514 :   if (TREE_CODE (src) == IMAGPART_EXPR
    1472       379514 :       || TREE_CODE (src) == REALPART_EXPR
    1473       379514 :       || TREE_CODE (src) == BIT_FIELD_REF)
    1474              :     return NULL_TREE;
    1475       379514 :   tree core1, core2;
    1476       379514 :   poly_int64 bytepos1, bytepos2;
    1477       379514 :   poly_int64 bytesize1, bytesize2;
    1478       379514 :   tree toffset1, toffset2;
    1479       379514 :   int reversep1 = 0;
    1480       379514 :   int reversep2 = 0;
    1481       379514 :   poly_int64 diff = 0;
    1482       379514 :   core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
    1483              :                                           &toffset1, &reversep1);
    1484       379514 :   core2 = split_core_and_offset_size (src2, &bytesize2, &bytepos2,
    1485              :                                           &toffset2, &reversep2);
    1486       379514 :   if (!core1 || !core2)
    1487              :     return NULL_TREE;
    1488       379514 :   if (reversep1 != reversep2)
    1489              :     return NULL_TREE;
    1490              :   /* The sizes of the 2 accesses need to be the same. */
    1491       379514 :   if (!known_eq (bytesize1, bytesize2))
    1492              :     return NULL_TREE;
    1493       170340 :   if (!operand_equal_p (core1, core2, 0))
    1494              :     return NULL_TREE;
    1495              : 
    1496        22757 :   if (toffset1 && toffset2)
    1497              :     {
    1498            2 :       tree type = TREE_TYPE (toffset1);
    1499            2 :       if (type != TREE_TYPE (toffset2))
    1500            0 :         toffset2 = fold_convert (type, toffset2);
    1501              : 
    1502            2 :       tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
    1503            2 :       if (!cst_and_fits_in_hwi (tdiff))
    1504              :         return NULL_TREE;
    1505              : 
    1506            0 :       diff = int_cst_value (tdiff);
    1507            0 :     }
    1508        22755 :   else if (toffset1 || toffset2)
    1509              :     {
    1510              :       /* If only one of the offsets is non-constant, the difference cannot
    1511              :          be a constant.  */
    1512              :       return NULL_TREE;
    1513              :     }
    1514        22723 :   diff += bytepos1 - bytepos2;
    1515              :   /* The offset between the 2 need to be 0. */
    1516        22723 :   if (!known_eq (diff, 0))
    1517              :     return NULL_TREE;
    1518        21888 :   return fold_build1 (VIEW_CONVERT_EXPR,TREE_TYPE (src2), src);
    1519              : }
    1520              : 
    1521              : /* Returns true if SRC and DEST are the same address such that
    1522              :    `SRC == DEST;` is considered a nop. This is more than an
    1523              :    operand_equal_p check as it needs to be similar to
    1524              :    new_src_based_on_copy.  */
    1525              : 
    1526              : static bool
    1527      4500207 : same_for_assignment (tree src, tree dest)
    1528              : {
    1529      4500207 :   if (operand_equal_p (dest, src, 0))
    1530              :     return true;
    1531              :   /* if both dest and src2 are decls, then we know these 2
    1532              :      accesses can't be the same.  */
    1533      4497300 :   if (DECL_P (dest) && DECL_P (src))
    1534              :     return false;
    1535              : 
    1536      2277875 :   tree core1, core2;
    1537      2277875 :   poly_int64 bytepos1, bytepos2;
    1538      2277875 :   poly_int64 bytesize1, bytesize2;
    1539      2277875 :   tree toffset1, toffset2;
    1540      2277875 :   int reversep1 = 0;
    1541      2277875 :   int reversep2 = 0;
    1542      2277875 :   poly_int64 diff = 0;
    1543      2277875 :   core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
    1544              :                                       &toffset1, &reversep1);
    1545      2277875 :   core2 = split_core_and_offset_size (src, &bytesize2, &bytepos2,
    1546              :                                       &toffset2, &reversep2);
    1547      2277875 :   if (!core1 || !core2)
    1548              :     return false;
    1549      2277875 :   if (reversep1 != reversep2)
    1550              :     return false;
    1551              :   /* The sizes of the 2 accesses need to be the same. */
    1552      2277875 :   if (!known_eq (bytesize1, bytesize2))
    1553              :     return false;
    1554      2276951 :   if (!operand_equal_p (core1, core2, 0))
    1555              :     return false;
    1556         6084 :   if (toffset1 && toffset2)
    1557              :     {
    1558          313 :       tree type = TREE_TYPE (toffset1);
    1559          313 :       if (type != TREE_TYPE (toffset2))
    1560            0 :         toffset2 = fold_convert (type, toffset2);
    1561              : 
    1562          313 :       tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
    1563          313 :       if (!cst_and_fits_in_hwi (tdiff))
    1564              :         return false;
    1565              : 
    1566            0 :       diff = int_cst_value (tdiff);
    1567            0 :     }
    1568         5771 :   else if (toffset1 || toffset2)
    1569              :     {
    1570              :       /* If only one of the offsets is non-constant, the difference cannot
    1571              :          be a constant.  */
    1572              :       return false;
    1573              :     }
    1574         5771 :   diff += bytepos1 - bytepos2;
    1575              :   /* The offset between the 2 need to be 0. */
    1576         5771 :   if (!known_eq (diff, 0))
    1577              :     return false;
    1578              :   return true;
    1579              : }
    1580              : 
    1581              : /* Helper function for optimize_agr_copyprop.
    1582              :    For aggregate copies in USE_STMT, see if DEST
    1583              :    is on the lhs of USE_STMT and replace it with SRC. */
    1584              : static void
    1585      1022581 : optimize_agr_copyprop_1 (gimple *stmt, gimple *use_stmt,
    1586              :                          tree dest, tree src)
    1587              : {
    1588      1022581 :   gcc_assert (gimple_assign_load_p (use_stmt)
    1589              :               && gimple_store_p (use_stmt));
    1590      2045162 :   if (gimple_has_volatile_ops (use_stmt))
    1591       612517 :     return;
    1592      1022580 :   tree dest2 = gimple_assign_lhs (use_stmt);
    1593      1022580 :   tree src2 = gimple_assign_rhs1 (use_stmt);
    1594              :   /* If the new store is `src2 = src2;` skip over it. */
    1595      1022580 :   if (same_for_assignment (src2, dest2))
    1596              :     return;
    1597      1022017 :   src = new_src_based_on_copy (src2, dest, src);
    1598      1022017 :   if (!src)
    1599              :     return;
    1600              :   /* For 2 memory references and using a temporary to do the copy,
    1601              :      don't remove the temporary as the 2 memory references might overlap.
    1602              :      Note t does not need to be decl as it could be field.
    1603              :      See PR 22237 for full details.
    1604              :      E.g.
    1605              :      t = *a; #DEST = SRC;
    1606              :      *b = t; #DEST2 = SRC2;
    1607              :      Cannot be convert into
    1608              :      t = *a;
    1609              :      *b = *a;
    1610              :      Though the following is allowed to be done:
    1611              :      t = *a;
    1612              :      *a = t;
    1613              :      And convert it into:
    1614              :      t = *a;
    1615              :      *a = *a;
    1616              :      */
    1617       438430 :   if (!operand_equal_p (dest2, src, 0)
    1618       438430 :       && !DECL_P (dest2) && !DECL_P (src))
    1619              :     {
    1620              :       /* If *a and *b have the same base see if
    1621              :          the offset between the two is greater than
    1622              :          or equal to the size of the type. */
    1623        31698 :       poly_int64 offset1, offset2;
    1624        31698 :       tree len = TYPE_SIZE_UNIT (TREE_TYPE (src));
    1625        31698 :       if (len == NULL_TREE
    1626        31698 :           || !tree_fits_poly_int64_p (len))
    1627        28366 :         return;
    1628        31698 :       tree base1 = get_addr_base_and_unit_offset (dest2, &offset1);
    1629        31698 :       tree base2 = get_addr_base_and_unit_offset (src, &offset2);
    1630        31698 :       poly_int64 size = tree_to_poly_int64 (len);
    1631              :       /* If the bases are 2 different decls,
    1632              :          then there can be no overlapping.  */
    1633        31698 :       if (base1 && base2
    1634        30831 :           && DECL_P (base1) && DECL_P (base2)
    1635         1874 :           && base1 != base2)
    1636              :         ;
    1637              :       /* If we can't figure out the base or the bases are
    1638              :          not equal then fall back to an alignment check.  */
    1639        30050 :       else if (!base1
    1640        30050 :                || !base2
    1641        30050 :                || !operand_equal_p (base1, base2))
    1642              :         {
    1643        29681 :           unsigned int align1 = get_object_alignment (src);
    1644        29681 :           unsigned int align2 = get_object_alignment (dest2);
    1645        29681 :           align1 /= BITS_PER_UNIT;
    1646        29681 :           align2 /= BITS_PER_UNIT;
    1647              :           /* If the alignment of either object is less
    1648              :              than the size then there is a possibility
    1649              :              of overlapping.  */
    1650        29681 :           if (maybe_lt (align1, size)
    1651        29681 :               || maybe_lt (align2, size))
    1652        28366 :             return;
    1653              :         }
    1654              :       /* Make sure [offset1, offset1 + len - 1] does
    1655              :          not overlap with [offset2, offset2 + len - 1],
    1656              :          it is ok if they are at the same location though.  */
    1657          369 :       else if (ranges_maybe_overlap_p (offset1, size, offset2, size)
    1658          369 :           && !known_eq (offset2, offset1))
    1659              :         return;
    1660              :     }
    1661              : 
    1662       410064 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1663              :     {
    1664           11 :       fprintf (dump_file, "Simplified\n  ");
    1665           11 :       print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
    1666           11 :       fprintf (dump_file, "after previous\n  ");
    1667           11 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1668              :     }
    1669       410064 :   gimple *orig_stmt = use_stmt;
    1670       410064 :   gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
    1671       410064 :   gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (src));
    1672       410064 :   update_stmt (use_stmt);
    1673              : 
    1674       410064 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1675              :     {
    1676           11 :       fprintf (dump_file, "into\n  ");
    1677           11 :       print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
    1678              :     }
    1679       410064 :   if (maybe_clean_or_replace_eh_stmt (orig_stmt, use_stmt))
    1680            0 :     bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
    1681       410064 :   statistics_counter_event (cfun, "copy prop for aggregate", 1);
    1682              : }
    1683              : 
    1684              : /* Helper function for optimize_agr_copyprop_1, propagate aggregates
    1685              :    into the arguments of USE_STMT if the argument matches with DEST;
    1686              :    replacing it with SRC.  */
    1687              : static void
    1688       710397 : optimize_agr_copyprop_arg (gimple *defstmt, gcall *call,
    1689              :                            tree dest, tree src)
    1690              : {
    1691       710397 :   bool changed = false;
    1692      2376921 :   for (unsigned arg = 0; arg < gimple_call_num_args (call); arg++)
    1693              :     {
    1694      1666524 :       tree *argptr = gimple_call_arg_ptr (call, arg);
    1695      3144061 :       if (TREE_CODE (*argptr) == SSA_NAME
    1696       947501 :           || is_gimple_min_invariant (*argptr)
    1697      1855511 :           || TYPE_VOLATILE (TREE_TYPE (*argptr)))
    1698      1477537 :         continue;
    1699       188987 :       tree newsrc = new_src_based_on_copy (*argptr, dest, src);
    1700       188987 :       if (!newsrc)
    1701       114647 :         continue;
    1702              : 
    1703        74340 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1704              :         {
    1705            9 :           fprintf (dump_file, "Simplified\n  ");
    1706            9 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    1707            9 :           fprintf (dump_file, "after previous\n  ");
    1708            9 :           print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1709              :         }
    1710        74340 :       *argptr = unshare_expr (newsrc);
    1711        74340 :       changed = true;
    1712        74340 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1713              :         {
    1714            9 :           fprintf (dump_file, "into\n  ");
    1715            9 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    1716              :         }
    1717              :     }
    1718       710397 :   if (changed)
    1719        74164 :     update_stmt (call);
    1720       710397 : }
    1721              : 
    1722              : /* Helper function for optimize_agr_copyprop, propagate aggregates
    1723              :    into the return stmt USE if the operand of the return matches DEST;
    1724              :    replacing it with SRC.  */
    1725              : static void
    1726       126957 : optimize_agr_copyprop_return (gimple *defstmt, greturn *use,
    1727              :                               tree dest, tree src)
    1728              : {
    1729       126957 :   tree rvalue = gimple_return_retval (use);
    1730       126957 :   if (!rvalue
    1731        82892 :       || TREE_CODE (rvalue) == SSA_NAME
    1732        74115 :       || is_gimple_min_invariant (rvalue)
    1733       200673 :       || TYPE_VOLATILE (TREE_TYPE (rvalue)))
    1734        53242 :     return;
    1735              : 
    1736              :   /* `return <retval>;` is already the best it could be.
    1737              :      Likewise `return *<retval>_N(D)`.  */
    1738        73715 :   if (TREE_CODE (rvalue) == RESULT_DECL
    1739        73715 :       || (TREE_CODE (rvalue) == MEM_REF
    1740            0 :           && TREE_CODE (TREE_OPERAND (rvalue, 0)) == SSA_NAME
    1741            0 :           && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (rvalue, 0)))
    1742              :                == RESULT_DECL))
    1743              :     return;
    1744        52219 :   tree newsrc = new_src_based_on_copy (rvalue, dest, src);
    1745        52219 :   if (!newsrc)
    1746              :     return;
    1747              :   /* Currently only support non-global vars.
    1748              :      See PR 124099 on enumtls not supporting expanding for GIMPLE_RETURN.
    1749              :      FIXME: could support VCEs too?  */
    1750        52125 :   if (!VAR_P (newsrc) || is_global_var (newsrc))
    1751              :     return;
    1752        25989 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1753              :     {
    1754            1 :       fprintf (dump_file, "Simplified\n  ");
    1755            1 :       print_gimple_stmt (dump_file, use, 0, dump_flags);
    1756            1 :       fprintf (dump_file, "after previous\n  ");
    1757            1 :       print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1758              :     }
    1759        25989 :   gimple_return_set_retval (use, newsrc);
    1760        25989 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1761              :     {
    1762            1 :       fprintf (dump_file, "into\n  ");
    1763            1 :       print_gimple_stmt (dump_file, use, 0, dump_flags);
    1764              :     }
    1765        25989 :   update_stmt (use);
    1766              : }
    1767              : 
    1768              : /* Optimizes
    1769              :    DEST = SRC;
    1770              :    DEST2 = DEST; # DEST2 = SRC2;
    1771              :    into
    1772              :    DEST = SRC;
    1773              :    DEST2 = SRC;
    1774              :    STMT is the first statement and SRC is the common
    1775              :    between the statements.
    1776              : 
    1777              :    Also optimizes:
    1778              :    DEST = SRC;
    1779              :    call_func(..., DEST, ...);
    1780              :    into:
    1781              :    DEST = SRC;
    1782              :    call_func(..., SRC, ...);
    1783              : 
    1784              : */
    1785              : static void
    1786      3886465 : optimize_agr_copyprop (gimple *stmt)
    1787              : {
    1788      7772930 :   if (gimple_has_volatile_ops (stmt))
    1789       411428 :     return;
    1790              : 
    1791              :   /* Can't prop if the statement could throw.  */
    1792      3885254 :   if (stmt_could_throw_p (cfun, stmt))
    1793              :     return;
    1794              : 
    1795      3477627 :   tree dest = gimple_assign_lhs (stmt);
    1796      3477627 :   tree src = gimple_assign_rhs1 (stmt);
    1797              :   /* If the statement is `src = src;` then ignore it. */
    1798      3477627 :   if (same_for_assignment (dest, src))
    1799              :     return;
    1800              : 
    1801      3475037 :   tree vdef = gimple_vdef (stmt);
    1802      3475037 :   imm_use_iterator iter;
    1803      3475037 :   gimple *use_stmt;
    1804     13503724 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
    1805              :     {
    1806      6553650 :       if (gimple_assign_load_p (use_stmt)
    1807      6553650 :           && gimple_store_p (use_stmt))
    1808      1022581 :         optimize_agr_copyprop_1 (stmt, use_stmt, dest, src);
    1809      5531069 :       else if (is_gimple_call (use_stmt))
    1810       710397 :         optimize_agr_copyprop_arg (stmt, as_a<gcall*>(use_stmt), dest, src);
    1811      4820672 :       else if (is_a<greturn*> (use_stmt))
    1812       126957 :         optimize_agr_copyprop_return (stmt, as_a<greturn*>(use_stmt), dest, src);
    1813      3475037 :     }
    1814              : }
    1815              : 
    1816              : /* Simple DSE of the lhs from a clobber STMT.
    1817              :    This is used mostly to clean up from optimize_agr_copyprop and
    1818              :    to remove (exactly one) extra copy that might later on confuse SRA.
    1819              :    An example is:
    1820              :    ;; write to a and such.
    1821              :    b = a; // This statement is to be removed
    1822              :    b = {CLOBBER};
    1823              :    SRA will totally scalarize b (which means also a) here for the extra copy
    1824              :    which is not something welcomed. So removing the copy will
    1825              :    allow SRA to move the scalarization of a further down or not at all.
    1826              :    */
    1827              : static void
    1828      7400238 : do_simple_agr_dse (gassign *stmt, bool full_walk)
    1829              : {
    1830              :   /* Don't do this while in -Og as we want to keep around the copy
    1831              :      for debuggability.  */
    1832      7400238 :   if (optimize_debug)
    1833      5113167 :     return;
    1834      7396845 :   ao_ref read;
    1835      7396845 :   basic_block bb = gimple_bb (stmt);
    1836      7396845 :   tree lhs = gimple_assign_lhs (stmt);
    1837              :   /* Only handle clobbers of a full decl.  */
    1838      7396845 :   if (!DECL_P (lhs))
    1839              :     return;
    1840      6673121 :   ao_ref_init (&read, lhs);
    1841      6673121 :   tree vuse = gimple_vuse (stmt);
    1842      6673121 :   unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 4;
    1843     16966896 :   while (limit)
    1844              :     {
    1845     16953729 :       gimple *ostmt = SSA_NAME_DEF_STMT (vuse);
    1846              :       /* Don't handle phis, just declare to be done. */
    1847     16953729 :       if (is_a<gphi*>(ostmt) || gimple_nop_p (ostmt))
    1848              :         break;
    1849     14679825 :       basic_block obb = gimple_bb (ostmt);
    1850              :       /* If the clobber is not fully dominating the statement define,
    1851              :          then it is not "simple" to detect if the define is fully clobbered.  */
    1852     14679825 :       if (obb != bb && !dominated_by_p (CDI_DOMINATORS, bb, obb))
    1853      4386050 :         return;
    1854     14679825 :       gimple *use_stmt;
    1855     14679825 :       imm_use_iterator iter;
    1856     59387200 :       FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_vdef (ostmt))
    1857              :         {
    1858     17310832 :           basic_block ubb = gimple_bb (use_stmt);
    1859     17310832 :           if (stmt == use_stmt)
    1860      5117919 :             continue;
    1861              :           /* If the use is a clobber for lhs,
    1862              :              then it can be safely skipped; this happens with eh
    1863              :              and sometimes jump threading.  */
    1864     12192913 :           if (gimple_clobber_p (use_stmt)
    1865     12192913 :               && lhs == gimple_assign_lhs (use_stmt))
    1866       172961 :             continue;
    1867              :           /* If the use is a phi and it is single use then check if that single use
    1868              :              is a clobber and lhs is the same.  */
    1869     12019952 :           if (gphi *use_phi = dyn_cast<gphi*>(use_stmt))
    1870              :             {
    1871       360135 :               use_operand_p ou;
    1872       360135 :               gimple *ostmt;
    1873       360135 :               if (single_imm_use (gimple_phi_result (use_phi), &ou, &ostmt)
    1874       300968 :                   && gimple_clobber_p (ostmt)
    1875       601651 :                   && lhs == gimple_assign_lhs (ostmt))
    1876        67865 :                 continue;
    1877              :               /* A phi node will never be dominating the clobber.  */
    1878       292270 :               return;
    1879              :             }
    1880              :           /* The use needs to be dominating the clobber. */
    1881      1472726 :           if ((ubb != bb && !dominated_by_p (CDI_DOMINATORS, bb, ubb))
    1882     12402620 :               || ref_maybe_used_by_stmt_p (use_stmt, &read, false))
    1883      1215064 :             return;
    1884              :           /* Count the above alias lookup towards the limit. */
    1885     10444753 :           limit--;
    1886     10444753 :           if (limit == 0)
    1887              :             return;
    1888      1963107 :         }
    1889     12716718 :       vuse = gimple_vuse (ostmt);
    1890              :       /* This is a call with an assignment to the clobber decl,
    1891              :          remove the lhs or the whole stmt if it was pure/const. */
    1892     12716718 :       if (is_a <gcall*>(ostmt)
    1893     12716718 :           && lhs == gimple_call_lhs (ostmt))
    1894              :         {
    1895              :           /* Don't remove stores/statements that are needed for non-call
    1896              :               eh to work.  */
    1897         4029 :           if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
    1898              :             return;
    1899              :           /* If we delete a stmt that could throw, mark the block
    1900              :              in to_purge to cleanup afterwards.  */
    1901         4023 :           if (stmt_could_throw_p (cfun, ostmt))
    1902         1006 :             bitmap_set_bit (to_purge, obb->index);
    1903         4023 :           int flags = gimple_call_flags (ostmt);
    1904         4023 :           if ((flags & (ECF_PURE|ECF_CONST|ECF_NOVOPS))
    1905          203 :               && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
    1906              :             {
    1907          119 :                gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
    1908          119 :                if (dump_file && (dump_flags & TDF_DETAILS))
    1909              :                 {
    1910           14 :                   fprintf (dump_file, "Removing dead call store stmt ");
    1911           14 :                   print_gimple_stmt (dump_file, ostmt, 0);
    1912           14 :                   fprintf (dump_file, "\n");
    1913              :                 }
    1914          119 :               unlink_stmt_vdef (ostmt);
    1915          119 :               release_defs (ostmt);
    1916          119 :               gsi_remove (&gsi, true);
    1917          119 :               statistics_counter_event (cfun, "delete call dead store", 1);
    1918              :               /* Only remove the first store previous statement. */
    1919          119 :               return;
    1920              :             }
    1921              :           /* Make sure we do not remove a return slot we cannot reconstruct
    1922              :              later.  */
    1923         3904 :           if (gimple_call_return_slot_opt_p (as_a <gcall *>(ostmt))
    1924         3904 :               && (TREE_ADDRESSABLE (TREE_TYPE (gimple_call_fntype (ostmt)))
    1925          541 :                   || !poly_int_tree_p
    1926          541 :                       (TYPE_SIZE (TREE_TYPE (gimple_call_fntype (ostmt))))))
    1927              :             return;
    1928          679 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1929              :             {
    1930            6 :               fprintf (dump_file, "Removing lhs of call stmt ");
    1931            6 :               print_gimple_stmt (dump_file, ostmt, 0);
    1932            6 :               fprintf (dump_file, "\n");
    1933              :             }
    1934          679 :           gimple_call_set_lhs (ostmt, NULL_TREE);
    1935          679 :           update_stmt (ostmt);
    1936          679 :           statistics_counter_event (cfun, "removed lhs call", 1);
    1937          679 :           return;
    1938              :         }
    1939              :       /* This an assignment store to the clobbered decl,
    1940              :          then maybe remove it. */
    1941     12712689 :       if (is_a <gassign*>(ostmt)
    1942     10733456 :           && gimple_store_p (ostmt)
    1943     10733456 :           && !gimple_clobber_p (ostmt)
    1944     15903827 :           && lhs == gimple_assign_lhs (ostmt))
    1945              :         {
    1946              :           /* Don't remove stores/statements that are needed for non-call
    1947              :               eh to work.  */
    1948       167004 :           if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
    1949              :             return;
    1950              :           /* If we delete a stmt that could throw, mark the block
    1951              :              in to_purge to cleanup afterwards.  */
    1952       161920 :           if (stmt_could_throw_p (cfun, ostmt))
    1953            0 :             bitmap_set_bit (to_purge, obb->index);
    1954       161920 :           gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
    1955       161920 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1956              :             {
    1957           12 :               fprintf (dump_file, "Removing dead store stmt ");
    1958           12 :               print_gimple_stmt (dump_file, ostmt, 0);
    1959           12 :               fprintf (dump_file, "\n");
    1960              :             }
    1961       161920 :           unlink_stmt_vdef (ostmt);
    1962       161920 :           release_defs (ostmt);
    1963       161920 :           gsi_remove (&gsi, true);
    1964       161920 :           statistics_counter_event (cfun, "delete dead store", 1);
    1965              :           /* Only remove the first store previous statement. */
    1966       161920 :           return;
    1967              :         }
    1968              :       /* If the statement uses or maybe writes to the decl,
    1969              :          then nothing is to be removed. Don't know if the write
    1970              :          to the decl is partial write or a full one so the need
    1971              :          to stop.
    1972              :          e.g.
    1973              :          b.c = a;
    1974              :            Easier to stop here rather than do a full partial
    1975              :            dse of this statement.
    1976              :          b = {CLOBBER}; */
    1977     12545685 :       if (stmt_may_clobber_ref_p_1 (ostmt, &read, false)
    1978     12545685 :           || ref_maybe_used_by_stmt_p (ostmt, &read, false))
    1979      2251910 :         return;
    1980     10293775 :       limit--;
    1981              :     }
    1982              : }
    1983              : 
    1984              : /* Optimizes builtin memcmps for small constant sizes.
    1985              :    GSI_P is the GSI for the call. STMT is the call itself.
    1986              :    */
    1987              : 
    1988              : static bool
    1989       470836 : simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
    1990              : {
    1991              :   /* Make sure memcmp arguments are the correct type.  */
    1992       470836 :   if (gimple_call_num_args (stmt) != 3)
    1993              :     return false;
    1994       470836 :   tree arg1 = gimple_call_arg (stmt, 0);
    1995       470836 :   tree arg2 = gimple_call_arg (stmt, 1);
    1996       470836 :   tree len = gimple_call_arg (stmt, 2);
    1997              : 
    1998       470836 :   if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
    1999              :     return false;
    2000       470836 :   if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
    2001              :     return false;
    2002       470836 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (len)))
    2003              :     return false;
    2004              : 
    2005              :   /* The return value of the memcmp has to be used
    2006              :      equality comparison to zero. */
    2007       470836 :   tree res = gimple_call_lhs (stmt);
    2008              : 
    2009       470836 :   if (!res || !use_in_zero_equality (res))
    2010        15059 :     return false;
    2011              : 
    2012       455777 :   unsigned HOST_WIDE_INT leni;
    2013              : 
    2014       455777 :   if (tree_fits_uhwi_p (len)
    2015       634244 :       && (leni = tree_to_uhwi (len)) <= GET_MODE_SIZE (word_mode)
    2016       536912 :       && pow2p_hwi (leni))
    2017              :     {
    2018        19183 :       leni *= CHAR_TYPE_SIZE;
    2019        19183 :       unsigned align1 = get_pointer_alignment (arg1);
    2020        19183 :       unsigned align2 = get_pointer_alignment (arg2);
    2021        19183 :       unsigned align = MIN (align1, align2);
    2022        19183 :       scalar_int_mode mode;
    2023        19183 :       if (int_mode_for_size (leni, 1).exists (&mode)
    2024        19183 :           && (align >= leni || !targetm.slow_unaligned_access (mode, align)))
    2025              :         {
    2026        19183 :           location_t loc = gimple_location (stmt);
    2027        19183 :           tree type, off;
    2028        19183 :           type = build_nonstandard_integer_type (leni, 1);
    2029        38366 :           gcc_assert (known_eq (GET_MODE_BITSIZE (TYPE_MODE (type)), leni));
    2030        19183 :           tree ptrtype = build_pointer_type_for_mode (char_type_node,
    2031              :                                                       ptr_mode, true);
    2032        19183 :           off = build_int_cst (ptrtype, 0);
    2033              : 
    2034              :           /* Create unaligned types if needed. */
    2035        19183 :           tree type1 = type, type2 = type;
    2036        19183 :           if (TYPE_ALIGN (type1) > align1)
    2037         7861 :             type1 = build_aligned_type (type1, align1);
    2038        19183 :           if (TYPE_ALIGN (type2) > align2)
    2039         8356 :             type2 = build_aligned_type (type2, align2);
    2040              : 
    2041        19183 :           arg1 = build2_loc (loc, MEM_REF, type1, arg1, off);
    2042        19183 :           arg2 = build2_loc (loc, MEM_REF, type2, arg2, off);
    2043        19183 :           tree tem1 = fold_const_aggregate_ref (arg1);
    2044        19183 :           if (tem1)
    2045          222 :             arg1 = tem1;
    2046        19183 :           tree tem2 = fold_const_aggregate_ref (arg2);
    2047        19183 :           if (tem2)
    2048         7529 :             arg2 = tem2;
    2049        19183 :           res = fold_convert_loc (loc, TREE_TYPE (res),
    2050              :                                   fold_build2_loc (loc, NE_EXPR,
    2051              :                                                    boolean_type_node,
    2052              :                                                    arg1, arg2));
    2053        19183 :           gimplify_and_update_call_from_tree (gsi_p, res);
    2054        19183 :           return true;
    2055              :         }
    2056              :     }
    2057              : 
    2058              :   /* Replace memcmp with memcmp_eq if the above fails. */
    2059       436594 :   if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
    2060              :     return false;
    2061       345679 :   if (!fold_before_rtl_expansion_p ())
    2062              :     return false;
    2063        90915 :   gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
    2064        90915 :   update_stmt (stmt);
    2065        90915 :   return true;
    2066              : }
    2067              : 
    2068              : /* Optimizes builtin memchrs for small constant sizes with a const string.
    2069              :    GSI_P is the GSI for the call. STMT is the call itself.
    2070              :    */
    2071              : 
    2072              : static bool
    2073        14509 : simplify_builtin_memchr (gimple_stmt_iterator *gsi_p, gcall *stmt)
    2074              : {
    2075        14509 :   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
    2076              :     return false;
    2077              : 
    2078        14509 :   if (gimple_call_num_args (stmt) != 3)
    2079              :     return false;
    2080              : 
    2081        14509 :   tree res = gimple_call_lhs (stmt);
    2082        14509 :   if (!res || !use_in_zero_equality (res))
    2083        13050 :     return false;
    2084              : 
    2085         1459 :   tree ptr = gimple_call_arg (stmt, 0);
    2086         1459 :   if (TREE_CODE (ptr) != ADDR_EXPR
    2087         1459 :       || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
    2088              :     return false;
    2089              : 
    2090          452 :   unsigned HOST_WIDE_INT slen
    2091          452 :     = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
    2092              :   /* It must be a non-empty string constant.  */
    2093          452 :   if (slen < 2)
    2094              :     return false;
    2095              : 
    2096              :   /* For -Os, only simplify strings with a single character.  */
    2097          448 :   if (!optimize_bb_for_speed_p (gimple_bb (stmt))
    2098          448 :       && slen > 2)
    2099              :     return false;
    2100              : 
    2101          432 :   tree size = gimple_call_arg (stmt, 2);
    2102              :   /* Size must be a constant which is <= UNITS_PER_WORD and
    2103              :      <= the string length.  */
    2104          432 :   if (!tree_fits_uhwi_p (size))
    2105              :     return false;
    2106              : 
    2107          432 :   unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
    2108          433 :   if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
    2109              :     return false;
    2110              : 
    2111          380 :   tree ch = gimple_call_arg (stmt, 1);
    2112          380 :   location_t loc = gimple_location (stmt);
    2113          380 :   if (!useless_type_conversion_p (char_type_node,
    2114          380 :                                   TREE_TYPE (ch)))
    2115          380 :     ch = fold_convert_loc (loc, char_type_node, ch);
    2116          380 :   const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
    2117          380 :   unsigned int isize = sz;
    2118          380 :   tree *op = XALLOCAVEC (tree, isize);
    2119         1369 :   for (unsigned int i = 0; i < isize; i++)
    2120              :     {
    2121          989 :       op[i] = build_int_cst (char_type_node, p[i]);
    2122          989 :       op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
    2123              :                                op[i], ch);
    2124              :     }
    2125          989 :   for (unsigned int i = isize - 1; i >= 1; i--)
    2126          609 :     op[i - 1] = fold_convert_loc (loc, boolean_type_node,
    2127              :                                   fold_build2_loc (loc,
    2128              :                                                    BIT_IOR_EXPR,
    2129              :                                                    boolean_type_node,
    2130          609 :                                                    op[i - 1],
    2131          609 :                                                    op[i]));
    2132          380 :   res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
    2133          380 :   gimplify_and_update_call_from_tree (gsi_p, res);
    2134          380 :   return true;
    2135              : }
    2136              : 
    2137              : /* *GSI_P is a GIMPLE_CALL to a builtin function.
    2138              :    Optimize
    2139              :    memcpy (p, "abcd", 4); // STMT1
    2140              :    memset (p + 4, ' ', 3); // STMT2
    2141              :    into
    2142              :    memcpy (p, "abcd   ", 7);
    2143              :    call if the latter can be stored by pieces during expansion.
    2144              : */
    2145              : 
    2146              : static bool
    2147       112011 : simplify_builtin_memcpy_memset (gimple_stmt_iterator *gsi_p, gcall *stmt2)
    2148              : {
    2149       112011 :   if (gimple_call_num_args (stmt2) != 3
    2150       112011 :       || gimple_call_lhs (stmt2)
    2151              :       || CHAR_BIT != 8
    2152       112011 :       || BITS_PER_UNIT != 8)
    2153              :     return false;
    2154              : 
    2155       215151 :   tree vuse = gimple_vuse (stmt2);
    2156       105539 :   if (vuse == NULL)
    2157              :     return false;
    2158       105523 :   gimple *stmt1 = SSA_NAME_DEF_STMT (vuse);
    2159              : 
    2160       105523 :   tree callee1;
    2161       105523 :   tree ptr1, src1, str1, off1, len1, lhs1;
    2162       105523 :   tree ptr2 = gimple_call_arg (stmt2, 0);
    2163       105523 :   tree val2 = gimple_call_arg (stmt2, 1);
    2164       105523 :   tree len2 = gimple_call_arg (stmt2, 2);
    2165       105523 :   tree diff, vdef, new_str_cst;
    2166       105523 :   gimple *use_stmt;
    2167       105523 :   unsigned int ptr1_align;
    2168       105523 :   unsigned HOST_WIDE_INT src_len;
    2169       105523 :   char *src_buf;
    2170       105523 :   use_operand_p use_p;
    2171              : 
    2172       105523 :   if (!tree_fits_shwi_p (val2)
    2173       101054 :       || !tree_fits_uhwi_p (len2)
    2174       169974 :       || compare_tree_int (len2, 1024) == 1)
    2175        46629 :     return false;
    2176              : 
    2177        58894 :   if (is_gimple_call (stmt1))
    2178              :     {
    2179              :       /* If first stmt is a call, it needs to be memcpy
    2180              :          or mempcpy, with string literal as second argument and
    2181              :          constant length.  */
    2182        30491 :       callee1 = gimple_call_fndecl (stmt1);
    2183        30491 :       if (callee1 == NULL_TREE
    2184        30375 :           || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
    2185        57196 :           || gimple_call_num_args (stmt1) != 3)
    2186              :         return false;
    2187        25413 :       if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
    2188        25413 :           && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
    2189              :         return false;
    2190        10985 :       ptr1 = gimple_call_arg (stmt1, 0);
    2191        10985 :       src1 = gimple_call_arg (stmt1, 1);
    2192        10985 :       len1 = gimple_call_arg (stmt1, 2);
    2193        10985 :       lhs1 = gimple_call_lhs (stmt1);
    2194        10985 :       if (!tree_fits_uhwi_p (len1))
    2195              :         return false;
    2196        10898 :       str1 = string_constant (src1, &off1, NULL, NULL);
    2197        10898 :       if (str1 == NULL_TREE)
    2198              :         return false;
    2199         5001 :       if (!tree_fits_uhwi_p (off1)
    2200         5001 :           || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
    2201         5001 :           || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
    2202         5001 :                                      - tree_to_uhwi (off1)) > 0
    2203         5001 :           || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
    2204        15003 :           || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
    2205         5001 :              != TYPE_MODE (char_type_node))
    2206            0 :         return false;
    2207              :     }
    2208        28403 :   else if (gimple_assign_single_p (stmt1))
    2209              :     {
    2210              :       /* Otherwise look for length 1 memcpy optimized into
    2211              :          assignment.  */
    2212        17382 :       ptr1 = gimple_assign_lhs (stmt1);
    2213        17382 :       src1 = gimple_assign_rhs1 (stmt1);
    2214        17382 :       if (TREE_CODE (ptr1) != MEM_REF
    2215         3550 :           || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
    2216        18416 :           || !tree_fits_shwi_p (src1))
    2217        17030 :         return false;
    2218          352 :       ptr1 = build_fold_addr_expr (ptr1);
    2219          352 :       STRIP_USELESS_TYPE_CONVERSION (ptr1);
    2220          352 :       callee1 = NULL_TREE;
    2221          352 :       len1 = size_one_node;
    2222          352 :       lhs1 = NULL_TREE;
    2223          352 :       off1 = size_zero_node;
    2224          352 :       str1 = NULL_TREE;
    2225              :     }
    2226              :   else
    2227              :     return false;
    2228              : 
    2229         5353 :   diff = constant_pointer_difference (ptr1, ptr2);
    2230         5353 :   if (diff == NULL && lhs1 != NULL)
    2231              :     {
    2232            7 :       diff = constant_pointer_difference (lhs1, ptr2);
    2233            7 :       if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
    2234            7 :           && diff != NULL)
    2235            7 :         diff = size_binop (PLUS_EXPR, diff,
    2236              :                            fold_convert (sizetype, len1));
    2237              :     }
    2238              :   /* If the difference between the second and first destination pointer
    2239              :      is not constant, or is bigger than memcpy length, bail out.  */
    2240         5353 :   if (diff == NULL
    2241         4467 :       || !tree_fits_uhwi_p (diff)
    2242         4467 :       || tree_int_cst_lt (len1, diff)
    2243         9564 :       || compare_tree_int (diff, 1024) == 1)
    2244         1142 :     return false;
    2245              : 
    2246              :   /* Use maximum of difference plus memset length and memcpy length
    2247              :      as the new memcpy length, if it is too big, bail out.  */
    2248         4211 :   src_len = tree_to_uhwi (diff);
    2249         4211 :   src_len += tree_to_uhwi (len2);
    2250         4211 :   if (src_len < tree_to_uhwi (len1))
    2251              :     src_len = tree_to_uhwi (len1);
    2252         4211 :   if (src_len > 1024)
    2253              :     return false;
    2254              : 
    2255              :   /* If mempcpy value is used elsewhere, bail out, as mempcpy
    2256              :      with bigger length will return different result.  */
    2257         4211 :   if (lhs1 != NULL_TREE
    2258           64 :       && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
    2259         4218 :       && (TREE_CODE (lhs1) != SSA_NAME
    2260            7 :           || !single_imm_use (lhs1, &use_p, &use_stmt)
    2261            7 :           || use_stmt != stmt2))
    2262            0 :     return false;
    2263              : 
    2264              :   /* If anything reads memory in between memcpy and memset
    2265              :      call, the modified memcpy call might change it.  */
    2266         4211 :   vdef = gimple_vdef (stmt1);
    2267         4211 :   if (vdef != NULL
    2268         4211 :       && (!single_imm_use (vdef, &use_p, &use_stmt)
    2269         3456 :           || use_stmt != stmt2))
    2270              :     return false;
    2271              : 
    2272         3456 :   ptr1_align = get_pointer_alignment (ptr1);
    2273              :   /* Construct the new source string literal.  */
    2274         3456 :   src_buf = XALLOCAVEC (char, src_len + 1);
    2275         3456 :   if (callee1)
    2276         3295 :     memcpy (src_buf,
    2277         3295 :             TREE_STRING_POINTER (str1) + tree_to_uhwi (off1),
    2278              :             tree_to_uhwi (len1));
    2279              :   else
    2280          161 :     src_buf[0] = tree_to_shwi (src1);
    2281         3456 :   memset (src_buf + tree_to_uhwi (diff),
    2282         3456 :           tree_to_shwi (val2), tree_to_uhwi (len2));
    2283         3456 :   src_buf[src_len] = '\0';
    2284              :   /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
    2285              :      handle embedded '\0's.  */
    2286         3456 :   if (strlen (src_buf) != src_len)
    2287              :     return false;
    2288         3366 :   rtl_profile_for_bb (gimple_bb (stmt2));
    2289              :   /* If the new memcpy wouldn't be emitted by storing the literal
    2290              :      by pieces, this optimization might enlarge .rodata too much,
    2291              :      as commonly used string literals couldn't be shared any
    2292              :      longer.  */
    2293         3366 :   if (!can_store_by_pieces (src_len,
    2294              :                             builtin_strncpy_read_str,
    2295              :                             src_buf, ptr1_align, false))
    2296              :     return false;
    2297              : 
    2298         2526 :   new_str_cst = build_string_literal (src_len, src_buf);
    2299         2526 :   if (callee1)
    2300              :     {
    2301              :       /* If STMT1 is a mem{,p}cpy call, adjust it and remove
    2302              :          memset call.  */
    2303         2399 :       if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
    2304            7 :         gimple_call_set_lhs (stmt1, NULL_TREE);
    2305         2399 :       gimple_call_set_arg (stmt1, 1, new_str_cst);
    2306         2399 :       gimple_call_set_arg (stmt1, 2,
    2307         2399 :                            build_int_cst (TREE_TYPE (len1), src_len));
    2308         2399 :       update_stmt (stmt1);
    2309         2399 :       unlink_stmt_vdef (stmt2);
    2310         2399 :       gsi_replace (gsi_p, gimple_build_nop (), false);
    2311         2399 :       fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
    2312         2399 :       release_defs (stmt2);
    2313         2399 :       if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
    2314              :         {
    2315            7 :           fwprop_invalidate_lattice (lhs1);
    2316            7 :           release_ssa_name (lhs1);
    2317              :         }
    2318         2399 :       return true;
    2319              :     }
    2320              :   else
    2321              :     {
    2322              :       /* Otherwise, if STMT1 is length 1 memcpy optimized into
    2323              :          assignment, remove STMT1 and change memset call into
    2324              :          memcpy call.  */
    2325          127 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
    2326              : 
    2327          127 :       if (!is_gimple_val (ptr1))
    2328           12 :         ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
    2329              :                                          true, GSI_SAME_STMT);
    2330          127 :       tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCPY);
    2331          127 :       gimple_call_set_fndecl (stmt2, fndecl);
    2332          127 :       gimple_call_set_fntype (stmt2,
    2333          127 :                               TREE_TYPE (fndecl));
    2334          127 :       gimple_call_set_arg (stmt2, 0, ptr1);
    2335          127 :       gimple_call_set_arg (stmt2, 1, new_str_cst);
    2336          127 :       gimple_call_set_arg (stmt2, 2,
    2337          127 :                            build_int_cst (TREE_TYPE (len2), src_len));
    2338          127 :       unlink_stmt_vdef (stmt1);
    2339          127 :       gsi_remove (&gsi, true);
    2340          127 :       fwprop_invalidate_lattice (gimple_get_lhs (stmt1));
    2341          127 :       release_defs (stmt1);
    2342          127 :       update_stmt (stmt2);
    2343          127 :       return false;
    2344              :     }
    2345              : }
    2346              : 
    2347              : 
    2348              : /* Try to optimize out __builtin_stack_restore.  Optimize it out
    2349              :    if there is another __builtin_stack_restore in the same basic
    2350              :    block and no calls or ASM_EXPRs are in between, or if this block's
    2351              :    only outgoing edge is to EXIT_BLOCK and there are no calls or
    2352              :    ASM_EXPRs after this __builtin_stack_restore.
    2353              :    Note restore right before a noreturn function is not needed.
    2354              :    And skip some cheap calls that will most likely become an instruction.
    2355              :    Restoring the stack before a call is important to be able to keep
    2356              :    stack usage down so that call does not run out of stack.  */
    2357              : 
    2358              : 
    2359              : static bool
    2360        10369 : optimize_stack_restore (gimple_stmt_iterator *gsi, gimple *call)
    2361              : {
    2362        10369 :   if (!fold_before_rtl_expansion_p ())
    2363              :     return false;
    2364         2525 :   tree callee;
    2365         2525 :   gimple *stmt;
    2366              : 
    2367         2525 :   basic_block bb = gsi_bb (*gsi);
    2368              : 
    2369         2525 :   if (gimple_call_num_args (call) != 1
    2370         2525 :       || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
    2371         5050 :       || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
    2372              :     return false;
    2373              : 
    2374         2525 :   gimple_stmt_iterator i = *gsi;
    2375         6288 :   for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
    2376              :     {
    2377         4218 :       stmt = gsi_stmt (i);
    2378         4218 :       if (is_a<gasm*> (stmt))
    2379              :         return false;
    2380         4217 :       gcall *call = dyn_cast<gcall*>(stmt);
    2381         4217 :       if (!call)
    2382         3554 :         continue;
    2383              : 
    2384              :       /* We can remove the restore in front of noreturn
    2385              :          calls.  Since the restore will happen either
    2386              :          via an unwind/longjmp or not at all. */
    2387          663 :       if (gimple_call_noreturn_p (call))
    2388              :         break;
    2389              : 
    2390              :       /* Internal calls are ok, to bypass
    2391              :          check first since fndecl will be null. */
    2392          647 :       if (gimple_call_internal_p (call))
    2393            1 :         continue;
    2394              : 
    2395          646 :       callee = gimple_call_fndecl (call);
    2396              :       /* Non-builtin calls are not ok. */
    2397          646 :       if (!callee
    2398          646 :           || !fndecl_built_in_p (callee))
    2399              :         return false;
    2400              : 
    2401              :       /* Do not remove stack updates before strub leave.  */
    2402          570 :       if (fndecl_built_in_p (callee, BUILT_IN___STRUB_LEAVE)
    2403              :           /* Alloca calls are not ok either. */
    2404          570 :           || fndecl_builtin_alloc_p (callee))
    2405              :         return false;
    2406              : 
    2407          355 :       if (fndecl_built_in_p (callee, BUILT_IN_STACK_RESTORE))
    2408           52 :         goto second_stack_restore;
    2409              : 
    2410              :       /* If not a simple or inexpensive builtin, then it is not ok either. */
    2411          303 :       if (!is_simple_builtin (callee)
    2412          303 :           && !is_inexpensive_builtin (callee))
    2413              :         return false;
    2414              :     }
    2415              : 
    2416              :   /* Allow one successor of the exit block, or zero successors.  */
    2417         2086 :   switch (EDGE_COUNT (bb->succs))
    2418              :     {
    2419              :     case 0:
    2420              :       break;
    2421         1999 :     case 1:
    2422         1999 :       if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
    2423              :         return false;
    2424              :       break;
    2425              :     default:
    2426              :       return false;
    2427              :     }
    2428         1729 :  second_stack_restore:
    2429              : 
    2430              :   /* If there's exactly one use, then zap the call to __builtin_stack_save.
    2431              :      If there are multiple uses, then the last one should remove the call.
    2432              :      In any case, whether the call to __builtin_stack_save can be removed
    2433              :      or not is irrelevant to removing the call to __builtin_stack_restore.  */
    2434         1729 :   if (has_single_use (gimple_call_arg (call, 0)))
    2435              :     {
    2436         1559 :       gimple *stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
    2437         1559 :       if (is_gimple_call (stack_save))
    2438              :         {
    2439         1557 :           callee = gimple_call_fndecl (stack_save);
    2440         1557 :           if (callee && fndecl_built_in_p (callee, BUILT_IN_STACK_SAVE))
    2441              :             {
    2442         1557 :               gimple_stmt_iterator stack_save_gsi;
    2443         1557 :               tree rhs;
    2444              : 
    2445         1557 :               stack_save_gsi = gsi_for_stmt (stack_save);
    2446         1557 :               rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
    2447         1557 :               replace_call_with_value (&stack_save_gsi, rhs);
    2448              :             }
    2449              :         }
    2450              :     }
    2451              : 
    2452              :   /* No effect, so the statement will be deleted.  */
    2453         1729 :   replace_call_with_value (gsi, NULL_TREE);
    2454         1729 :   return true;
    2455              : }
    2456              : 
    2457              : /* *GSI_P is a GIMPLE_CALL to a builtin function.
    2458              :    Optimize
    2459              :    memcpy (p, "abcd", 4);
    2460              :    memset (p + 4, ' ', 3);
    2461              :    into
    2462              :    memcpy (p, "abcd   ", 7);
    2463              :    call if the latter can be stored by pieces during expansion.
    2464              : 
    2465              :    Optimize
    2466              :    memchr ("abcd", a, 4) == 0;
    2467              :    or
    2468              :    memchr ("abcd", a, 4) != 0;
    2469              :    to
    2470              :    (a == 'a' || a == 'b' || a == 'c' || a == 'd') == 0
    2471              :    or
    2472              :    (a == 'a' || a == 'b' || a == 'c' || a == 'd') != 0
    2473              : 
    2474              :    Also canonicalize __atomic_fetch_op (p, x, y) op x
    2475              :    to __atomic_op_fetch (p, x, y) or
    2476              :    __atomic_op_fetch (p, x, y) iop x
    2477              :    to __atomic_fetch_op (p, x, y) when possible (also __sync).  */
    2478              : 
    2479              : static bool
    2480      6293190 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2, bool full_walk)
    2481              : {
    2482      6293190 :   gimple *stmt2 = gsi_stmt (*gsi_p);
    2483      6293190 :   enum built_in_function other_atomic = END_BUILTINS;
    2484      6293190 :   enum tree_code atomic_op = ERROR_MARK;
    2485              : 
    2486      6293190 :   switch (DECL_FUNCTION_CODE (callee2))
    2487              :     {
    2488        10369 :     case BUILT_IN_STACK_RESTORE:
    2489        10369 :       return optimize_stack_restore (gsi_p, as_a<gcall*>(stmt2));
    2490       470836 :     case BUILT_IN_MEMCMP:
    2491       470836 :     case BUILT_IN_MEMCMP_EQ:
    2492       470836 :       return simplify_builtin_memcmp (gsi_p, as_a<gcall*>(stmt2));
    2493        14509 :     case BUILT_IN_MEMCHR:
    2494        14509 :       return simplify_builtin_memchr (gsi_p, as_a<gcall*>(stmt2));
    2495              : 
    2496       112011 :     case BUILT_IN_MEMSET:
    2497       112011 :       if (gimple_call_num_args (stmt2) == 3)
    2498              :         {
    2499              :           /* Try to prop the zeroing/value of the memset to memcpy
    2500              :              if the dest is an address and the value is a constant. */
    2501       112011 :           optimize_aggr_zeroprop (stmt2, full_walk);
    2502              :         }
    2503       112011 :       return simplify_builtin_memcpy_memset (gsi_p, as_a<gcall*>(stmt2));
    2504              : 
    2505              :  #define CASE_ATOMIC(NAME, OTHER, OP) \
    2506              :     case BUILT_IN_##NAME##_1:                                           \
    2507              :     case BUILT_IN_##NAME##_2:                                           \
    2508              :     case BUILT_IN_##NAME##_4:                                           \
    2509              :     case BUILT_IN_##NAME##_8:                                           \
    2510              :     case BUILT_IN_##NAME##_16:                                          \
    2511              :       atomic_op = OP;                                                   \
    2512              :       other_atomic                                                      \
    2513              :         = (enum built_in_function) (BUILT_IN_##OTHER##_1                \
    2514              :                                     + (DECL_FUNCTION_CODE (callee2)     \
    2515              :                                        - BUILT_IN_##NAME##_1));         \
    2516              :       goto handle_atomic_fetch_op;
    2517              : 
    2518        48548 :     CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
    2519         7125 :     CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
    2520         2876 :     CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
    2521         2895 :     CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
    2522         3819 :     CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR)
    2523              : 
    2524         2365 :     CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR)
    2525         2004 :     CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR)
    2526         1876 :     CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR)
    2527         2144 :     CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR)
    2528         1987 :     CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR)
    2529              : 
    2530        14400 :     CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
    2531         8552 :     CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
    2532         2380 :     CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR)
    2533              : 
    2534          846 :     CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR)
    2535          732 :     CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR)
    2536          800 :     CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR)
    2537              : 
    2538              : #undef CASE_ATOMIC
    2539              : 
    2540       103349 :     handle_atomic_fetch_op:
    2541       103349 :       if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
    2542              :         {
    2543        60088 :           tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
    2544        60088 :           tree arg = gimple_call_arg (stmt2, 1);
    2545        60088 :           gimple *use_stmt, *cast_stmt = NULL;
    2546        60088 :           use_operand_p use_p;
    2547        60088 :           tree ndecl = builtin_decl_explicit (other_atomic);
    2548              : 
    2549        60088 :           if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
    2550              :             break;
    2551              : 
    2552        58959 :           if (gimple_assign_cast_p (use_stmt))
    2553              :             {
    2554        31403 :               cast_stmt = use_stmt;
    2555        31403 :               lhsc = gimple_assign_lhs (cast_stmt);
    2556        31403 :               if (lhsc == NULL_TREE
    2557        31403 :                   || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
    2558        30852 :                   || (TYPE_PRECISION (TREE_TYPE (lhsc))
    2559        30852 :                       != TYPE_PRECISION (TREE_TYPE (lhs2)))
    2560        60729 :                   || !single_imm_use (lhsc, &use_p, &use_stmt))
    2561              :                 {
    2562         2605 :                   use_stmt = cast_stmt;
    2563         2605 :                   cast_stmt = NULL;
    2564         2605 :                   lhsc = lhs2;
    2565              :                 }
    2566              :             }
    2567              : 
    2568        58959 :           bool ok = false;
    2569        58959 :           tree oarg = NULL_TREE;
    2570        58959 :           enum tree_code ccode = ERROR_MARK;
    2571        58959 :           tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
    2572        58959 :           if (is_gimple_assign (use_stmt)
    2573        58959 :               && gimple_assign_rhs_code (use_stmt) == atomic_op)
    2574              :             {
    2575         1416 :               if (gimple_assign_rhs1 (use_stmt) == lhsc)
    2576         1016 :                 oarg = gimple_assign_rhs2 (use_stmt);
    2577          400 :               else if (atomic_op != MINUS_EXPR)
    2578              :                 oarg = gimple_assign_rhs1 (use_stmt);
    2579              :             }
    2580        57543 :           else if (atomic_op == MINUS_EXPR
    2581        13246 :                    && is_gimple_assign (use_stmt)
    2582         3606 :                    && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
    2583          199 :                    && TREE_CODE (arg) == INTEGER_CST
    2584        57742 :                    && (TREE_CODE (gimple_assign_rhs2 (use_stmt))
    2585              :                        == INTEGER_CST))
    2586              :             {
    2587          183 :               tree a = fold_convert (TREE_TYPE (lhs2), arg);
    2588          183 :               tree o = fold_convert (TREE_TYPE (lhs2),
    2589              :                                      gimple_assign_rhs2 (use_stmt));
    2590          183 :               if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
    2591              :                 ok = true;
    2592              :             }
    2593        57360 :           else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
    2594              :             ;
    2595        52118 :           else if (gimple_code (use_stmt) == GIMPLE_COND)
    2596              :             {
    2597        19580 :               ccode = gimple_cond_code (use_stmt);
    2598        19580 :               crhs1 = gimple_cond_lhs (use_stmt);
    2599        19580 :               crhs2 = gimple_cond_rhs (use_stmt);
    2600              :             }
    2601        32538 :           else if (is_gimple_assign (use_stmt))
    2602              :             {
    2603         9519 :               if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
    2604              :                 {
    2605         3935 :                   ccode = gimple_assign_rhs_code (use_stmt);
    2606         3935 :                   crhs1 = gimple_assign_rhs1 (use_stmt);
    2607         3935 :                   crhs2 = gimple_assign_rhs2 (use_stmt);
    2608              :                 }
    2609         5584 :               else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR)
    2610              :                 {
    2611            0 :                   tree cond = gimple_assign_rhs1 (use_stmt);
    2612            0 :                   if (COMPARISON_CLASS_P (cond))
    2613              :                     {
    2614            0 :                       ccode = TREE_CODE (cond);
    2615            0 :                       crhs1 = TREE_OPERAND (cond, 0);
    2616            0 :                       crhs2 = TREE_OPERAND (cond, 1);
    2617              :                     }
    2618              :                 }
    2619              :             }
    2620        24531 :           if (ccode == EQ_EXPR || ccode == NE_EXPR)
    2621              :             {
    2622              :               /* Deal with x - y == 0 or x ^ y == 0
    2623              :                  being optimized into x == y and x + cst == 0
    2624              :                  into x == -cst.  */
    2625        22331 :               tree o = NULL_TREE;
    2626        22331 :               if (crhs1 == lhsc)
    2627              :                 o = crhs2;
    2628          133 :               else if (crhs2 == lhsc)
    2629          133 :                 o = crhs1;
    2630        22331 :               if (o && atomic_op != PLUS_EXPR)
    2631              :                 oarg = o;
    2632        10116 :               else if (o
    2633        10116 :                        && TREE_CODE (o) == INTEGER_CST
    2634        10116 :                        && TREE_CODE (arg) == INTEGER_CST)
    2635              :                 {
    2636         9406 :                   tree a = fold_convert (TREE_TYPE (lhs2), arg);
    2637         9406 :                   o = fold_convert (TREE_TYPE (lhs2), o);
    2638         9406 :                   if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
    2639        58959 :                     ok = true;
    2640              :                 }
    2641              :             }
    2642        58959 :           if (oarg && !ok)
    2643              :             {
    2644        13631 :               if (operand_equal_p (arg, oarg, 0))
    2645              :                 ok = true;
    2646        12302 :               else if (TREE_CODE (arg) == SSA_NAME
    2647         2203 :                        && TREE_CODE (oarg) == SSA_NAME)
    2648              :                 {
    2649          745 :                   tree oarg2 = oarg;
    2650          745 :                   if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg)))
    2651              :                     {
    2652          104 :                       gimple *g = SSA_NAME_DEF_STMT (oarg);
    2653          104 :                       oarg2 = gimple_assign_rhs1 (g);
    2654          104 :                       if (TREE_CODE (oarg2) != SSA_NAME
    2655          104 :                           || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2))
    2656          208 :                           || (TYPE_PRECISION (TREE_TYPE (oarg2))
    2657          104 :                               != TYPE_PRECISION (TREE_TYPE (oarg))))
    2658              :                         oarg2 = oarg;
    2659              :                     }
    2660          745 :                   if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg)))
    2661              :                     {
    2662          544 :                       gimple *g = SSA_NAME_DEF_STMT (arg);
    2663          544 :                       tree rhs1 = gimple_assign_rhs1 (g);
    2664              :                       /* Handle e.g.
    2665              :                          x.0_1 = (long unsigned int) x_4(D);
    2666              :                          _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0);
    2667              :                          _3 = (long int) _2;
    2668              :                          _7 = x_4(D) + _3;  */
    2669          544 :                       if (rhs1 == oarg || rhs1 == oarg2)
    2670              :                         ok = true;
    2671              :                       /* Handle e.g.
    2672              :                          x.18_1 = (short unsigned int) x_5(D);
    2673              :                          _2 = (int) x.18_1;
    2674              :                          _3 = __atomic_fetch_xor_2 (&vshort, _2, 0);
    2675              :                          _4 = (short int) _3;
    2676              :                          _8 = x_5(D) ^ _4;
    2677              :                          This happens only for char/short.  */
    2678          160 :                       else if (TREE_CODE (rhs1) == SSA_NAME
    2679          160 :                                && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
    2680          320 :                                && (TYPE_PRECISION (TREE_TYPE (rhs1))
    2681          160 :                                    == TYPE_PRECISION (TREE_TYPE (lhs2))))
    2682              :                         {
    2683          160 :                           g = SSA_NAME_DEF_STMT (rhs1);
    2684          160 :                           if (gimple_assign_cast_p (g)
    2685          160 :                               && (gimple_assign_rhs1 (g) == oarg
    2686            0 :                                   || gimple_assign_rhs1 (g) == oarg2))
    2687              :                             ok = true;
    2688              :                         }
    2689              :                     }
    2690          745 :                   if (!ok && arg == oarg2)
    2691              :                     /* Handle e.g.
    2692              :                        _1 = __sync_fetch_and_add_4 (&v, x_5(D));
    2693              :                        _2 = (int) _1;
    2694              :                        x.0_3 = (int) x_5(D);
    2695              :                        _7 = _2 + x.0_3;  */
    2696              :                     ok = true;
    2697              :                 }
    2698              :             }
    2699              : 
    2700        57630 :           if (ok)
    2701              :             {
    2702         2605 :               tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
    2703         2605 :               gimple_call_set_lhs (stmt2, new_lhs);
    2704         2605 :               gimple_call_set_fndecl (stmt2, ndecl);
    2705         2605 :               gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
    2706         2605 :               if (ccode == ERROR_MARK)
    2707         2000 :                 gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
    2708              :                                                 ? NOP_EXPR : SSA_NAME,
    2709              :                                                 new_lhs);
    2710              :               else
    2711              :                 {
    2712         1382 :                   crhs1 = new_lhs;
    2713         1382 :                   crhs2 = build_zero_cst (TREE_TYPE (lhs2));
    2714         1382 :                   if (gimple_code (use_stmt) == GIMPLE_COND)
    2715              :                     {
    2716         1043 :                       gcond *cond_stmt = as_a <gcond *> (use_stmt);
    2717         1043 :                       gimple_cond_set_lhs (cond_stmt, crhs1);
    2718         1043 :                       gimple_cond_set_rhs (cond_stmt, crhs2);
    2719              :                     }
    2720          339 :                   else if (gimple_assign_rhs_class (use_stmt)
    2721              :                            == GIMPLE_BINARY_RHS)
    2722              :                     {
    2723          339 :                       gimple_assign_set_rhs1 (use_stmt, crhs1);
    2724          339 :                       gimple_assign_set_rhs2 (use_stmt, crhs2);
    2725              :                     }
    2726              :                   else
    2727              :                     {
    2728            0 :                       gcc_checking_assert (gimple_assign_rhs_code (use_stmt)
    2729              :                                            == COND_EXPR);
    2730            0 :                       tree cond = build2 (ccode, boolean_type_node,
    2731              :                                           crhs1, crhs2);
    2732            0 :                       gimple_assign_set_rhs1 (use_stmt, cond);
    2733              :                     }
    2734              :                 }
    2735         2605 :               update_stmt (use_stmt);
    2736         2605 :               if (atomic_op != BIT_AND_EXPR
    2737         2605 :                   && atomic_op != BIT_IOR_EXPR
    2738         2605 :                   && !stmt_ends_bb_p (stmt2))
    2739              :                 {
    2740              :                   /* For the benefit of debug stmts, emit stmt(s) to set
    2741              :                      lhs2 to the value it had from the new builtin.
    2742              :                      E.g. if it was previously:
    2743              :                      lhs2 = __atomic_fetch_add_8 (ptr, arg, 0);
    2744              :                      emit:
    2745              :                      new_lhs = __atomic_add_fetch_8 (ptr, arg, 0);
    2746              :                      lhs2 = new_lhs - arg;
    2747              :                      We also keep cast_stmt if any in the IL for
    2748              :                      the same reasons.
    2749              :                      These stmts will be DCEd later and proper debug info
    2750              :                      will be emitted.
    2751              :                      This is only possible for reversible operations
    2752              :                      (+/-/^) and without -fnon-call-exceptions.  */
    2753         2264 :                   gsi = gsi_for_stmt (stmt2);
    2754         2264 :                   tree type = TREE_TYPE (lhs2);
    2755         2264 :                   if (TREE_CODE (arg) == INTEGER_CST)
    2756         1682 :                     arg = fold_convert (type, arg);
    2757          582 :                   else if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
    2758              :                     {
    2759            0 :                       tree narg = make_ssa_name (type);
    2760            0 :                       gimple *g = gimple_build_assign (narg, NOP_EXPR, arg);
    2761            0 :                       gsi_insert_after (&gsi, g, GSI_NEW_STMT);
    2762            0 :                       arg = narg;
    2763              :                     }
    2764         2264 :                   enum tree_code rcode;
    2765         2264 :                   switch (atomic_op)
    2766              :                     {
    2767              :                     case PLUS_EXPR: rcode = MINUS_EXPR; break;
    2768          727 :                     case MINUS_EXPR: rcode = PLUS_EXPR; break;
    2769          492 :                     case BIT_XOR_EXPR: rcode = atomic_op; break;
    2770            0 :                     default: gcc_unreachable ();
    2771              :                     }
    2772         2264 :                   gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
    2773         2264 :                   gsi_insert_after (&gsi, g, GSI_NEW_STMT);
    2774         2264 :                   update_stmt (stmt2);
    2775              :                 }
    2776              :               else
    2777              :                 {
    2778              :                   /* For e.g.
    2779              :                      lhs2 = __atomic_fetch_or_8 (ptr, arg, 0);
    2780              :                      after we change it to
    2781              :                      new_lhs = __atomic_or_fetch_8 (ptr, arg, 0);
    2782              :                      there is no way to find out the lhs2 value (i.e.
    2783              :                      what the atomic memory contained before the operation),
    2784              :                      values of some bits are lost.  We have checked earlier
    2785              :                      that we don't have any non-debug users except for what
    2786              :                      we are already changing, so we need to reset the
    2787              :                      debug stmts and remove the cast_stmt if any.  */
    2788          341 :                   imm_use_iterator iter;
    2789          676 :                   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2)
    2790          335 :                     if (use_stmt != cast_stmt)
    2791              :                       {
    2792          168 :                         gcc_assert (is_gimple_debug (use_stmt));
    2793          168 :                         gimple_debug_bind_reset_value (use_stmt);
    2794          168 :                         update_stmt (use_stmt);
    2795          341 :                       }
    2796          341 :                   if (cast_stmt)
    2797              :                     {
    2798          167 :                       gsi = gsi_for_stmt (cast_stmt);
    2799          167 :                       gsi_remove (&gsi, true);
    2800              :                     }
    2801          341 :                   update_stmt (stmt2);
    2802          341 :                   release_ssa_name (lhs2);
    2803              :                 }
    2804              :             }
    2805              :         }
    2806              :       break;
    2807              : 
    2808              :     default:
    2809              :       break;
    2810              :     }
    2811              :   return false;
    2812              : }
    2813              : 
    2814              : /* Given a ssa_name in NAME see if it was defined by an assignment and
    2815              :    set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
    2816              :    to the second operand on the rhs. */
    2817              : 
    2818              : static inline void
    2819     17566123 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
    2820              : {
    2821     17566123 :   gimple *def;
    2822     17566123 :   enum tree_code code1;
    2823     17566123 :   tree arg11;
    2824     17566123 :   tree arg21;
    2825     17566123 :   tree arg31;
    2826     17566123 :   enum gimple_rhs_class grhs_class;
    2827              : 
    2828     17566123 :   code1 = TREE_CODE (name);
    2829     17566123 :   arg11 = name;
    2830     17566123 :   arg21 = NULL_TREE;
    2831     17566123 :   arg31 = NULL_TREE;
    2832     17566123 :   grhs_class = get_gimple_rhs_class (code1);
    2833              : 
    2834     17566123 :   if (code1 == SSA_NAME)
    2835              :     {
    2836     11703723 :       def = SSA_NAME_DEF_STMT (name);
    2837              : 
    2838     11703723 :       if (def && is_gimple_assign (def)
    2839     19012879 :           && can_propagate_from (def))
    2840              :         {
    2841      5017144 :           code1 = gimple_assign_rhs_code (def);
    2842      5017144 :           arg11 = gimple_assign_rhs1 (def);
    2843      5017144 :           arg21 = gimple_assign_rhs2 (def);
    2844      5017144 :           arg31 = gimple_assign_rhs3 (def);
    2845              :         }
    2846              :     }
    2847      5862400 :   else if (grhs_class != GIMPLE_SINGLE_RHS)
    2848            0 :     code1 = ERROR_MARK;
    2849              : 
    2850     17566123 :   *code = code1;
    2851     17566123 :   *arg1 = arg11;
    2852     17566123 :   if (arg2)
    2853     17548863 :     *arg2 = arg21;
    2854     17566123 :   if (arg31)
    2855         2343 :     *code = ERROR_MARK;
    2856     17566123 : }
    2857              : 
    2858              : 
    2859              : /* Recognize rotation patterns.  Return true if a transformation
    2860              :    applied, otherwise return false.
    2861              : 
    2862              :    We are looking for X with unsigned type T with bitsize B, OP being
    2863              :    +, | or ^, some type T2 wider than T.  For:
    2864              :    (X << CNT1) OP (X >> CNT2)                               iff CNT1 + CNT2 == B
    2865              :    ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
    2866              : 
    2867              :    transform these into:
    2868              :    X r<< CNT1
    2869              : 
    2870              :    Or for:
    2871              :    (X << Y) OP (X >> (B - Y))
    2872              :    (X << (int) Y) OP (X >> (int) (B - Y))
    2873              :    ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
    2874              :    ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
    2875              :    (X << Y) | (X >> ((-Y) & (B - 1)))
    2876              :    (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
    2877              :    ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
    2878              :    ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
    2879              : 
    2880              :    transform these into (last 2 only if ranger can prove Y < B
    2881              :    or Y = N * B):
    2882              :    X r<< Y
    2883              :    or
    2884              :    X r<< (& & (B - 1))
    2885              :    The latter for the forms with T2 wider than T if ranger can't prove Y < B.
    2886              : 
    2887              :    Or for:
    2888              :    (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1)))
    2889              :    (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1)))
    2890              :    ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1))))
    2891              :    ((T) ((T2) X << (int) (Y & (B - 1)))) \
    2892              :      | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
    2893              : 
    2894              :    transform these into:
    2895              :    X r<< (Y & (B - 1))
    2896              : 
    2897              :    Note, in the patterns with T2 type, the type of OP operands
    2898              :    might be even a signed type, but should have precision B.
    2899              :    Expressions with & (B - 1) should be recognized only if B is
    2900              :    a power of 2.  */
    2901              : 
    2902              : static bool
    2903     10312220 : simplify_rotate (gimple_stmt_iterator *gsi)
    2904              : {
    2905     10312220 :   gimple *stmt = gsi_stmt (*gsi);
    2906     10312220 :   tree arg[2], rtype, rotcnt = NULL_TREE;
    2907     10312220 :   tree def_arg1[2], def_arg2[2];
    2908     10312220 :   enum tree_code def_code[2];
    2909     10312220 :   tree lhs;
    2910     10312220 :   int i;
    2911     10312220 :   bool swapped_p = false;
    2912     10312220 :   gimple *g;
    2913     10312220 :   gimple *def_arg_stmt[2] = { NULL, NULL };
    2914     10312220 :   int wider_prec = 0;
    2915     10312220 :   bool add_masking = false;
    2916              : 
    2917     10312220 :   arg[0] = gimple_assign_rhs1 (stmt);
    2918     10312220 :   arg[1] = gimple_assign_rhs2 (stmt);
    2919     10312220 :   rtype = TREE_TYPE (arg[0]);
    2920              : 
    2921              :   /* Only create rotates in complete modes.  Other cases are not
    2922              :      expanded properly.  */
    2923     10312220 :   if (!INTEGRAL_TYPE_P (rtype)
    2924     10312220 :       || !type_has_mode_precision_p (rtype))
    2925      1579065 :     return false;
    2926              : 
    2927     26199465 :   for (i = 0; i < 2; i++)
    2928              :     {
    2929     17466310 :       defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2930     17466310 :       if (TREE_CODE (arg[i]) == SSA_NAME)
    2931     11603910 :         def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
    2932              :     }
    2933              : 
    2934              :   /* Look through narrowing (or same precision) conversions.  */
    2935      7766183 :   if (CONVERT_EXPR_CODE_P (def_code[0])
    2936       966972 :       && CONVERT_EXPR_CODE_P (def_code[1])
    2937       140889 :       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
    2938       117207 :       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
    2939       109663 :       && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
    2940       109663 :          == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
    2941        63714 :       && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
    2942        44690 :       && has_single_use (arg[0])
    2943      8766261 :       && has_single_use (arg[1]))
    2944              :     {
    2945        28621 :       wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
    2946        85863 :       for (i = 0; i < 2; i++)
    2947              :         {
    2948        57242 :           arg[i] = def_arg1[i];
    2949        57242 :           defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2950        57242 :           if (TREE_CODE (arg[i]) == SSA_NAME)
    2951        57242 :             def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
    2952              :         }
    2953              :     }
    2954              :   else
    2955              :     {
    2956              :       /* Handle signed rotate; the RSHIFT_EXPR has to be done
    2957              :          in unsigned type but LSHIFT_EXPR could be signed.  */
    2958      8704534 :       i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
    2959      7748643 :       if (CONVERT_EXPR_CODE_P (def_code[i])
    2960       955891 :           && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
    2961        30654 :           && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
    2962        29391 :           && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
    2963      8709436 :           && has_single_use (arg[i]))
    2964              :         {
    2965         2051 :           arg[i] = def_arg1[i];
    2966         2051 :           defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
    2967         2051 :           if (TREE_CODE (arg[i]) == SSA_NAME)
    2968         2051 :             def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
    2969              :         }
    2970              :     }
    2971              : 
    2972              :   /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR.  */
    2973      8931969 :   for (i = 0; i < 2; i++)
    2974      8907233 :     if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
    2975              :       return false;
    2976       239439 :     else if (!has_single_use (arg[i]))
    2977              :       return false;
    2978        24736 :   if (def_code[0] == def_code[1])
    2979              :     return false;
    2980              : 
    2981              :   /* If we've looked through narrowing conversions before, look through
    2982              :      widening conversions from unsigned type with the same precision
    2983              :      as rtype here.  */
    2984        20366 :   if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype))
    2985        19348 :     for (i = 0; i < 2; i++)
    2986              :       {
    2987        12900 :         tree tem;
    2988        12900 :         enum tree_code code;
    2989        12900 :         defcodefor_name (def_arg1[i], &code, &tem, NULL);
    2990            4 :         if (!CONVERT_EXPR_CODE_P (code)
    2991        12896 :             || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
    2992        25796 :             || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
    2993            4 :           return false;
    2994        12896 :         def_arg1[i] = tem;
    2995              :       }
    2996              :   /* Both shifts have to use the same first operand.  */
    2997        20362 :   if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
    2998        32299 :       || !types_compatible_p (TREE_TYPE (def_arg1[0]),
    2999        11937 :                               TREE_TYPE (def_arg1[1])))
    3000              :     {
    3001         8425 :       if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
    3002         8425 :            != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
    3003         8425 :           || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
    3004         8425 :               == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
    3005         8401 :         return false;
    3006              : 
    3007              :       /* Handle signed rotate; the RSHIFT_EXPR has to be done
    3008              :          in unsigned type but LSHIFT_EXPR could be signed.  */
    3009          546 :       i = def_code[0] != RSHIFT_EXPR;
    3010          546 :       if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
    3011              :         return false;
    3012              : 
    3013          507 :       tree tem;
    3014          507 :       enum tree_code code;
    3015          507 :       defcodefor_name (def_arg1[i], &code, &tem, NULL);
    3016          304 :       if (!CONVERT_EXPR_CODE_P (code)
    3017          203 :           || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
    3018          710 :           || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
    3019              :         return false;
    3020          194 :       def_arg1[i] = tem;
    3021          194 :       if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
    3022          218 :           || !types_compatible_p (TREE_TYPE (def_arg1[0]),
    3023           24 :                                   TREE_TYPE (def_arg1[1])))
    3024          170 :         return false;
    3025              :     }
    3026        11937 :   else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
    3027              :     return false;
    3028              : 
    3029              :   /* CNT1 + CNT2 == B case above.  */
    3030        10706 :   if (tree_fits_uhwi_p (def_arg2[0])
    3031         1210 :       && tree_fits_uhwi_p (def_arg2[1])
    3032        10706 :       && tree_to_uhwi (def_arg2[0])
    3033         1210 :          + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype))
    3034              :     rotcnt = def_arg2[0];
    3035         9776 :   else if (TREE_CODE (def_arg2[0]) != SSA_NAME
    3036         9496 :            || TREE_CODE (def_arg2[1]) != SSA_NAME)
    3037              :     return false;
    3038              :   else
    3039              :     {
    3040         9496 :       tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2];
    3041         9496 :       enum tree_code cdef_code[2];
    3042         9496 :       gimple *def_arg_alt_stmt[2] = { NULL, NULL };
    3043         9496 :       int check_range = 0;
    3044         9496 :       gimple *check_range_stmt = NULL;
    3045              :       /* Look through conversion of the shift count argument.
    3046              :          The C/C++ FE cast any shift count argument to integer_type_node.
    3047              :          The only problem might be if the shift count type maximum value
    3048              :          is equal or smaller than number of bits in rtype.  */
    3049        28488 :       for (i = 0; i < 2; i++)
    3050              :         {
    3051        18992 :           def_arg2_alt[i] = def_arg2[i];
    3052        18992 :           defcodefor_name (def_arg2[i], &cdef_code[i],
    3053              :                            &cdef_arg1[i], &cdef_arg2[i]);
    3054        14724 :           if (CONVERT_EXPR_CODE_P (cdef_code[i])
    3055         4268 :               && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i]))
    3056         4268 :               && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i]))
    3057         8536 :                  > floor_log2 (TYPE_PRECISION (rtype))
    3058        23260 :               && type_has_mode_precision_p (TREE_TYPE (cdef_arg1[i])))
    3059              :             {
    3060         4268 :               def_arg2_alt[i] = cdef_arg1[i];
    3061         4268 :               if (TREE_CODE (def_arg2[i]) == SSA_NAME)
    3062         4268 :                 def_arg_alt_stmt[i] = SSA_NAME_DEF_STMT (def_arg2[i]);
    3063         4268 :               defcodefor_name (def_arg2_alt[i], &cdef_code[i],
    3064              :                                &cdef_arg1[i], &cdef_arg2[i]);
    3065              :             }
    3066              :           else
    3067        14724 :             def_arg_alt_stmt[i] = def_arg_stmt[i];
    3068              :         }
    3069        25812 :       for (i = 0; i < 2; i++)
    3070              :         /* Check for one shift count being Y and the other B - Y,
    3071              :            with optional casts.  */
    3072        18641 :         if (cdef_code[i] == MINUS_EXPR
    3073          862 :             && tree_fits_shwi_p (cdef_arg1[i])
    3074          862 :             && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype)
    3075        19463 :             && TREE_CODE (cdef_arg2[i]) == SSA_NAME)
    3076              :           {
    3077          822 :             tree tem;
    3078          822 :             enum tree_code code;
    3079              : 
    3080          822 :             if (cdef_arg2[i] == def_arg2[1 - i]
    3081          472 :                 || cdef_arg2[i] == def_arg2_alt[1 - i])
    3082              :               {
    3083          350 :                 rotcnt = cdef_arg2[i];
    3084          350 :                 check_range = -1;
    3085          350 :                 if (cdef_arg2[i] == def_arg2[1 - i])
    3086          350 :                   check_range_stmt = def_arg_stmt[1 - i];
    3087              :                 else
    3088            0 :                   check_range_stmt = def_arg_alt_stmt[1 - i];
    3089          806 :                 break;
    3090              :               }
    3091          472 :             defcodefor_name (cdef_arg2[i], &code, &tem, NULL);
    3092           16 :             if (CONVERT_EXPR_CODE_P (code)
    3093          456 :                 && INTEGRAL_TYPE_P (TREE_TYPE (tem))
    3094          456 :                 && TYPE_PRECISION (TREE_TYPE (tem))
    3095          912 :                    > floor_log2 (TYPE_PRECISION (rtype))
    3096          456 :                 && type_has_mode_precision_p (TREE_TYPE (tem))
    3097          928 :                 && (tem == def_arg2[1 - i]
    3098          288 :                     || tem == def_arg2_alt[1 - i]))
    3099              :               {
    3100          456 :                 rotcnt = tem;
    3101          456 :                 check_range = -1;
    3102          456 :                 if (tem == def_arg2[1 - i])
    3103          168 :                   check_range_stmt = def_arg_stmt[1 - i];
    3104              :                 else
    3105          288 :                   check_range_stmt = def_arg_alt_stmt[1 - i];
    3106              :                 break;
    3107              :               }
    3108              :           }
    3109              :         /* The above sequence isn't safe for Y being 0,
    3110              :            because then one of the shifts triggers undefined behavior.
    3111              :            This alternative is safe even for rotation count of 0.
    3112              :            One shift count is Y and the other (-Y) & (B - 1).
    3113              :            Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1).  */
    3114        17819 :         else if (cdef_code[i] == BIT_AND_EXPR
    3115        28748 :                  && pow2p_hwi (TYPE_PRECISION (rtype))
    3116        12432 :                  && tree_fits_shwi_p (cdef_arg2[i])
    3117        24864 :                  && tree_to_shwi (cdef_arg2[i])
    3118        12432 :                     == TYPE_PRECISION (rtype) - 1
    3119        12372 :                  && TREE_CODE (cdef_arg1[i]) == SSA_NAME
    3120        30191 :                  && gimple_assign_rhs_code (stmt) == BIT_IOR_EXPR)
    3121              :           {
    3122         2312 :             tree tem;
    3123         2312 :             enum tree_code code;
    3124              : 
    3125         2312 :             defcodefor_name (cdef_arg1[i], &code, &tem, NULL);
    3126         2115 :             if (CONVERT_EXPR_CODE_P (code)
    3127          197 :                 && INTEGRAL_TYPE_P (TREE_TYPE (tem))
    3128          197 :                 && TYPE_PRECISION (TREE_TYPE (tem))
    3129          394 :                    > floor_log2 (TYPE_PRECISION (rtype))
    3130         2509 :                 && type_has_mode_precision_p (TREE_TYPE (tem)))
    3131          197 :               defcodefor_name (tem, &code, &tem, NULL);
    3132              : 
    3133         2312 :             if (code == NEGATE_EXPR)
    3134              :               {
    3135         1533 :                 if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i])
    3136              :                   {
    3137          854 :                     rotcnt = tem;
    3138          854 :                     check_range = 1;
    3139          854 :                     if (tem == def_arg2[1 - i])
    3140          846 :                       check_range_stmt = def_arg_stmt[1 - i];
    3141              :                     else
    3142            8 :                       check_range_stmt = def_arg_alt_stmt[1 - i];
    3143         1519 :                     break;
    3144              :                   }
    3145          679 :                 tree tem2;
    3146          679 :                 defcodefor_name (tem, &code, &tem2, NULL);
    3147          237 :                 if (CONVERT_EXPR_CODE_P (code)
    3148          442 :                     && INTEGRAL_TYPE_P (TREE_TYPE (tem2))
    3149          442 :                     && TYPE_PRECISION (TREE_TYPE (tem2))
    3150          884 :                        > floor_log2 (TYPE_PRECISION (rtype))
    3151         1121 :                     && type_has_mode_precision_p (TREE_TYPE (tem2)))
    3152              :                   {
    3153          442 :                     if (tem2 == def_arg2[1 - i]
    3154          442 :                         || tem2 == def_arg2_alt[1 - i])
    3155              :                       {
    3156          228 :                         rotcnt = tem2;
    3157          228 :                         check_range = 1;
    3158          228 :                         if (tem2 == def_arg2[1 - i])
    3159            0 :                           check_range_stmt = def_arg_stmt[1 - i];
    3160              :                         else
    3161          228 :                           check_range_stmt = def_arg_alt_stmt[1 - i];
    3162              :                         break;
    3163              :                       }
    3164              :                   }
    3165              :                 else
    3166          237 :                   tem2 = NULL_TREE;
    3167              : 
    3168          451 :                 if (cdef_code[1 - i] == BIT_AND_EXPR
    3169          438 :                     && tree_fits_shwi_p (cdef_arg2[1 - i])
    3170          876 :                     && tree_to_shwi (cdef_arg2[1 - i])
    3171          438 :                        == TYPE_PRECISION (rtype) - 1
    3172          889 :                     && TREE_CODE (cdef_arg1[1 - i]) == SSA_NAME)
    3173              :                   {
    3174          438 :                     if (tem == cdef_arg1[1 - i]
    3175          213 :                         || tem2 == cdef_arg1[1 - i])
    3176              :                       {
    3177              :                         rotcnt = def_arg2[1 - i];
    3178          437 :                         break;
    3179              :                       }
    3180          193 :                     tree tem3;
    3181          193 :                     defcodefor_name (cdef_arg1[1 - i], &code, &tem3, NULL);
    3182            0 :                     if (CONVERT_EXPR_CODE_P (code)
    3183          193 :                         && INTEGRAL_TYPE_P (TREE_TYPE (tem3))
    3184          193 :                         && TYPE_PRECISION (TREE_TYPE (tem3))
    3185          386 :                            > floor_log2 (TYPE_PRECISION (rtype))
    3186          386 :                         && type_has_mode_precision_p (TREE_TYPE (tem3)))
    3187              :                       {
    3188          193 :                         if (tem == tem3 || tem2 == tem3)
    3189              :                           {
    3190              :                             rotcnt = def_arg2[1 - i];
    3191              :                             break;
    3192              :                           }
    3193              :                       }
    3194              :                   }
    3195              :               }
    3196              :           }
    3197         2325 :       if (check_range && wider_prec > TYPE_PRECISION (rtype))
    3198              :         {
    3199         1533 :           if (TREE_CODE (rotcnt) != SSA_NAME)
    3200          573 :             return false;
    3201         1533 :           int_range_max r;
    3202         1533 :           range_query *q = get_range_query (cfun);
    3203         1533 :           if (q == get_global_range_query ())
    3204         1522 :             q = enable_ranger (cfun);
    3205         1533 :           if (!q->range_of_expr (r, rotcnt, check_range_stmt))
    3206              :             {
    3207            0 :               if (check_range > 0)
    3208              :                 return false;
    3209            0 :               r.set_varying (TREE_TYPE (rotcnt));
    3210              :             }
    3211         1533 :           int prec = TYPE_PRECISION (TREE_TYPE (rotcnt));
    3212         1533 :           signop sign = TYPE_SIGN (TREE_TYPE (rotcnt));
    3213         1533 :           wide_int min = wide_int::from (TYPE_PRECISION (rtype), prec, sign);
    3214         1533 :           wide_int max = wide_int::from (wider_prec - 1, prec, sign);
    3215         1533 :           if (check_range < 0)
    3216          616 :             max = min;
    3217         1533 :           int_range<1> r2 (TREE_TYPE (rotcnt), min, max);
    3218         1533 :           r.intersect (r2);
    3219         1533 :           if (!r.undefined_p ())
    3220              :             {
    3221         1181 :               if (check_range > 0)
    3222              :                 {
    3223          589 :                   int_range_max r3;
    3224         1844 :                   for (int i = TYPE_PRECISION (rtype) + 1; i < wider_prec;
    3225         1255 :                        i += TYPE_PRECISION (rtype))
    3226              :                     {
    3227         1255 :                       int j = i + TYPE_PRECISION (rtype) - 2;
    3228         1255 :                       min = wide_int::from (i, prec, sign);
    3229         1255 :                       max = wide_int::from (MIN (j, wider_prec - 1),
    3230         1255 :                                             prec, sign);
    3231         1255 :                       int_range<1> r4 (TREE_TYPE (rotcnt), min, max);
    3232         1255 :                       r3.union_ (r4);
    3233         1255 :                     }
    3234          589 :                   r.intersect (r3);
    3235          589 :                   if (!r.undefined_p ())
    3236          573 :                     return false;
    3237          589 :                 }
    3238              :               add_masking = true;
    3239              :             }
    3240         1533 :         }
    3241         8923 :       if (rotcnt == NULL_TREE)
    3242              :         return false;
    3243         1752 :       swapped_p = i != 1;
    3244              :     }
    3245              : 
    3246         2682 :   if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]),
    3247         2682 :                                   TREE_TYPE (rotcnt)))
    3248              :     {
    3249          496 :       g = gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2[0])),
    3250              :                                NOP_EXPR, rotcnt);
    3251          496 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    3252          496 :       rotcnt = gimple_assign_lhs (g);
    3253              :     }
    3254         2682 :   if (add_masking)
    3255              :     {
    3256          608 :       g = gimple_build_assign (make_ssa_name (TREE_TYPE (rotcnt)),
    3257              :                                BIT_AND_EXPR, rotcnt,
    3258          608 :                                build_int_cst (TREE_TYPE (rotcnt),
    3259          608 :                                               TYPE_PRECISION (rtype) - 1));
    3260          608 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    3261          608 :       rotcnt = gimple_assign_lhs (g);
    3262              :     }
    3263         2682 :   lhs = gimple_assign_lhs (stmt);
    3264         2682 :   if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
    3265         1010 :     lhs = make_ssa_name (TREE_TYPE (def_arg1[0]));
    3266         2682 :   g = gimple_build_assign (lhs,
    3267         2682 :                            ((def_code[0] == LSHIFT_EXPR) ^ swapped_p)
    3268              :                            ? LROTATE_EXPR : RROTATE_EXPR, def_arg1[0], rotcnt);
    3269         2682 :   if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
    3270              :     {
    3271         1010 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    3272         1010 :       g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, lhs);
    3273              :     }
    3274         2682 :   gsi_replace (gsi, g, false);
    3275         2682 :   return true;
    3276              : }
    3277              : 
    3278              : 
    3279              : /* Check whether an array contains a valid table according to VALIDATE_FN.  */
    3280              : template<typename ValidateFn>
    3281              : static bool
    3282           17 : check_table_array (tree ctor, HOST_WIDE_INT &zero_val, unsigned bits,
    3283              :                   ValidateFn validate_fn)
    3284              : {
    3285              :   tree elt, idx;
    3286           17 :   unsigned HOST_WIDE_INT i, raw_idx = 0;
    3287           17 :   unsigned matched = 0;
    3288              : 
    3289           17 :   zero_val = 0;
    3290              : 
    3291          734 :   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
    3292              :     {
    3293          734 :       if (!tree_fits_shwi_p (idx))
    3294              :         return false;
    3295          734 :       if (!tree_fits_shwi_p (elt) && TREE_CODE (elt) != RAW_DATA_CST)
    3296              :         return false;
    3297              : 
    3298          734 :       unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
    3299              :       HOST_WIDE_INT val;
    3300              : 
    3301          734 :       if (TREE_CODE (elt) == INTEGER_CST)
    3302          670 :         val = tree_to_shwi (elt);
    3303              :       else
    3304              :         {
    3305           64 :           if (raw_idx == (unsigned) RAW_DATA_LENGTH (elt))
    3306              :             {
    3307            0 :               raw_idx = 0;
    3308            0 :               continue;
    3309              :             }
    3310           64 :           if (TYPE_UNSIGNED (TREE_TYPE (elt)))
    3311            0 :             val = RAW_DATA_UCHAR_ELT (elt, raw_idx);
    3312              :           else
    3313           64 :             val = RAW_DATA_SCHAR_ELT (elt, raw_idx);
    3314           64 :           index += raw_idx;
    3315           64 :           raw_idx++;
    3316           64 :           i--;
    3317              :         }
    3318              : 
    3319          734 :       if (index > bits * 2)
    3320              :         return false;
    3321              : 
    3322          734 :       if (index == 0)
    3323              :         {
    3324           17 :           zero_val = val;
    3325           17 :           matched++;
    3326              :         }
    3327              : 
    3328          734 :       if (val >= 0 && val < bits && validate_fn (val, index))
    3329          672 :         matched++;
    3330              : 
    3331          734 :       if (matched > bits)
    3332              :         return true;
    3333              :     }
    3334              : 
    3335              :   return false;
    3336              : }
    3337              : 
    3338              : /* Check whether a string contains a valid table according to VALIDATE_FN.  */
    3339              : template<typename ValidateFn>
    3340              : static bool
    3341            4 : check_table_string (tree string, HOST_WIDE_INT &zero_val,unsigned bits,
    3342              :                     ValidateFn validate_fn)
    3343              : {
    3344            4 :   unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
    3345            4 :   unsigned matched = 0;
    3346            4 :   const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
    3347              : 
    3348            4 :   if (len < bits || len > bits * 2)
    3349              :     return false;
    3350              : 
    3351            4 :   zero_val = p[0];
    3352              : 
    3353          164 :   for (unsigned i = 0; i < len; i++)
    3354          160 :     if (p[i] < bits && validate_fn (p[i], i))
    3355          160 :       matched++;
    3356              : 
    3357            4 :   return matched == bits;
    3358              : }
    3359              : 
    3360              : /* Check whether CTOR contains a valid table according to VALIDATE_FN.  */
    3361              : template<typename ValidateFn>
    3362              : static bool
    3363           29 : check_table (tree ctor, tree type, HOST_WIDE_INT &zero_val, unsigned bits,
    3364              :              ValidateFn validate_fn)
    3365              : {
    3366           29 :   if (TREE_CODE (ctor) == CONSTRUCTOR)
    3367           17 :     return check_table_array (ctor, zero_val, bits, validate_fn);
    3368              :   else if (TREE_CODE (ctor) == STRING_CST
    3369           12 :            && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
    3370            4 :     return check_table_string (ctor, zero_val, bits, validate_fn);
    3371              :   return false;
    3372              : }
    3373              : 
    3374              : /* Match.pd function to match the ctz expression.  */
    3375              : extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree));
    3376              : extern bool gimple_clz_table_index (tree, tree *, tree (*)(tree));
    3377              : extern bool gimple_clz_msb_iso_table_index (tree, tree *, tree (*)(tree));
    3378              : 
    3379              : /* Recognize count leading and trailing zeroes idioms.
    3380              :    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
    3381              :    constant which when multiplied by a power of 2 creates a unique value
    3382              :    in the top 5 or 6 bits.  This is then indexed into a table which maps it
    3383              :    to the number of trailing zeroes.  Array[0] is returned so the caller can
    3384              :    emit an appropriate sequence depending on whether ctz (0) is defined on
    3385              :    the target.  */
    3386              : 
    3387              : static bool
    3388      1996254 : simplify_count_zeroes (gimple_stmt_iterator *gsi)
    3389              : {
    3390      1996254 :   gimple *stmt = gsi_stmt (*gsi);
    3391      1996254 :   tree array_ref = gimple_assign_rhs1 (stmt);
    3392      1996254 :   tree res_ops[3];
    3393              : 
    3394      1996254 :   gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
    3395              : 
    3396      1996254 :   internal_fn fn = IFN_LAST;
    3397              :   /* When true, the matched idiom is a CLZ using DeBruijn CTZ on the
    3398              :      isolated MSB -- see clz_msb_iso_table_index in match.pd.  The
    3399              :      table stores MSB positions and must satisfy the direct CTZ
    3400              :      DeBruijn property, so we validate it with the CTZ checkfn even
    3401              :      though we emit IFN_CLZ code.  */
    3402      1996254 :   bool clz_via_ctz = false;
    3403              :   /* For CTZ we recognize ((x & -x) * C) >> SHIFT where the array data
    3404              :      represents the number of trailing zeros.  */
    3405      1996254 :   if (gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL))
    3406              :     fn = IFN_CTZ;
    3407              :   /* For CLZ we recognize
    3408              :        x |= x >> 1;
    3409              :        x |= x >> 2;
    3410              :        x |= x >> 4;
    3411              :        x |= x >> 8;
    3412              :        x |= x >> 16;
    3413              :        (x * C) >> SHIFT
    3414              :      where 31 minus the array data represents the number of leading zeros.  */
    3415      1996231 :   else if (gimple_clz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0],
    3416              :                                    NULL))
    3417              :     fn = IFN_CLZ;
    3418              :   /* Variant CLZ idiom: after the OR-cascade sets all bits from 0 to
    3419              :      the original MSB, (value - (value >> 1)) isolates the MSB as a
    3420              :      power of two (2^k), and the subsequent DeBruijn multiply-and-shift
    3421              :      is a CTZ-style lookup on 2^k.  The table stores MSB positions
    3422              :      directly.  */
    3423      1996221 :   else if (gimple_clz_msb_iso_table_index (TREE_OPERAND (array_ref, 1),
    3424              :                                            &res_ops[0], NULL))
    3425              :     {
    3426              :       fn = IFN_CLZ;
    3427              :       clz_via_ctz = true;
    3428              :     }
    3429              :   else
    3430              :     return false;
    3431              : 
    3432           34 :   HOST_WIDE_INT zero_val;
    3433           34 :   tree type = TREE_TYPE (array_ref);
    3434           34 :   tree array = TREE_OPERAND (array_ref, 0);
    3435           34 :   tree input_type = TREE_TYPE (res_ops[0]);
    3436           34 :   unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type));
    3437              : 
    3438              :   /* Check the array element type is integral and not wider than 64 bits,
    3439              :      and the input is an unsigned 32-bit or 64-bit type.  The table values
    3440              :      are bit positions in [0, input_bits - 1], so any integer element type
    3441              :      with at least 6 bits of precision suffices; the cap is just to keep
    3442              :      the transformation simple.  */
    3443           34 :   if (!INTEGRAL_TYPE_P (type) || TYPE_PRECISION (type) > 64
    3444           68 :       || !TYPE_UNSIGNED (input_type))
    3445              :     return false;
    3446           30 :   if (input_bits != 32 && input_bits != 64)
    3447              :     return false;
    3448              : 
    3449           30 :   if (!direct_internal_fn_supported_p (fn, input_type, OPTIMIZE_FOR_BOTH))
    3450              :     return false;
    3451              : 
    3452              :   /* Check the lower bound of the array is zero.  */
    3453           30 :   tree low = array_ref_low_bound (array_ref);
    3454           30 :   if (!low || !integer_zerop (low))
    3455            0 :     return false;
    3456              : 
    3457              :   /* Check the shift extracts the top 5..7 bits.  */
    3458           30 :   unsigned shiftval = tree_to_shwi (res_ops[2]);
    3459           30 :   if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
    3460              :     return false;
    3461              : 
    3462           29 :   tree ctor = ctor_for_folding (array);
    3463           29 :   if (!ctor)
    3464              :     return false;
    3465           29 :   unsigned HOST_WIDE_INT mulval = tree_to_uhwi (res_ops[1]);
    3466              :   /* CTZ and the MSB-isolation CLZ variant both use the direct CTZ
    3467              :      DeBruijn check (table[(magic << data) >> shift] == data).  */
    3468           29 :   if (fn == IFN_CTZ || clz_via_ctz)
    3469              :     {
    3470          559 :       auto checkfn = [&](unsigned data, unsigned i) -> bool
    3471              :         {
    3472          540 :           unsigned HOST_WIDE_INT mask
    3473          540 :             = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
    3474          540 :           return (((mulval << data) & mask) >> shiftval) == i;
    3475           19 :         };
    3476           19 :       if (!check_table (ctor, type, zero_val, input_bits, checkfn))
    3477            8 :         return false;
    3478              :     }
    3479           10 :   else if (fn == IFN_CLZ)
    3480              :     {
    3481          362 :       auto checkfn = [&](unsigned data, unsigned i) -> bool
    3482              :         {
    3483          352 :           unsigned HOST_WIDE_INT mask
    3484          352 :             = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
    3485              :           /* The OR-cascade produces a value with all bits from 0 to the
    3486              :              original MSB set.  Compute (1 << (data + 1)) - 1 to simulate
    3487              :              that value.  When data + 1 equals HOST_BITS_PER_WIDE_INT
    3488              :              (i.e. data is the MSB position of a 64-bit input) the shift
    3489              :              is undefined behavior, so handle that case explicitly using
    3490              :              all-ones.  Without this, any well-formed 64-bit DeBruijn CLZ
    3491              :              table is rejected because its entry for the all-ones input
    3492              :              correctly maps to the MSB (e.g. table[...] == 63).
    3493              :              PR tree-optimization/122569.  */
    3494          703 :           unsigned HOST_WIDE_INT all_bits_below
    3495              :             = (data + 1 == HOST_BITS_PER_WIDE_INT)
    3496          352 :               ? HOST_WIDE_INT_M1U
    3497          351 :               : ((HOST_WIDE_INT_1U << (data + 1)) - 1);
    3498          352 :           return (((all_bits_below * mulval) & mask) >> shiftval) == i;
    3499           10 :         };
    3500           10 :     if (!check_table (ctor, type, zero_val, input_bits, checkfn))
    3501            0 :       return false;
    3502              :     }
    3503              : 
    3504           21 :   HOST_WIDE_INT ctz_val = -1;
    3505           21 :   bool zero_ok;
    3506           21 :   if (fn == IFN_CTZ)
    3507              :     {
    3508           10 :       ctz_val = 0;
    3509           20 :       zero_ok = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
    3510              :                                            ctz_val) == 2;
    3511              :     }
    3512           11 :   else if (fn == IFN_CLZ)
    3513              :     {
    3514           11 :       ctz_val = 32;
    3515           11 :       zero_ok = CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
    3516              :                                            ctz_val) == 2;
    3517           11 :       zero_val = input_bits - 1 - zero_val;
    3518              :     }
    3519           21 :   int nargs = 2;
    3520              : 
    3521              :   /* If the input value can't be zero, don't special case ctz (0).  */
    3522           21 :   range_query *q = get_range_query (cfun);
    3523           21 :   if (q == get_global_range_query ())
    3524           21 :     q = enable_ranger (cfun);
    3525           21 :   int_range_max vr;
    3526           21 :   if (q->range_of_expr (vr, res_ops[0], stmt)
    3527           21 :       && !range_includes_zero_p (vr))
    3528              :     {
    3529            4 :       zero_ok = true;
    3530            4 :       zero_val = 0;
    3531            4 :       ctz_val = 0;
    3532            4 :       nargs = 1;
    3533              :     }
    3534              : 
    3535           21 :   gimple_seq seq = NULL;
    3536           21 :   gimple *g;
    3537           21 :   gcall *call = gimple_build_call_internal (fn, nargs, res_ops[0],
    3538              :                                             nargs == 1 ? NULL_TREE
    3539           38 :                                             : build_int_cst (integer_type_node,
    3540           17 :                                                              ctz_val));
    3541           21 :   gimple_set_location (call, gimple_location (stmt));
    3542           21 :   gimple_set_lhs (call, make_ssa_name (integer_type_node));
    3543           21 :   gimple_seq_add_stmt (&seq, call);
    3544              : 
    3545           21 :   tree prev_lhs = gimple_call_lhs (call);
    3546              : 
    3547           21 :   if (zero_ok && zero_val == ctz_val)
    3548              :     ;
    3549              :   /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0.  */
    3550            6 :   else if (zero_ok && zero_val == 0 && ctz_val == input_bits)
    3551              :     {
    3552            5 :       g = gimple_build_assign (make_ssa_name (integer_type_node),
    3553              :                                BIT_AND_EXPR, prev_lhs,
    3554              :                                build_int_cst (integer_type_node,
    3555            5 :                                               input_bits - 1));
    3556            5 :       gimple_set_location (g, gimple_location (stmt));
    3557            5 :       gimple_seq_add_stmt (&seq, g);
    3558            5 :       prev_lhs = gimple_assign_lhs (g);
    3559              :     }
    3560              :   /* As fallback emit a conditional move.  */
    3561              :   else
    3562              :     {
    3563           10 :       g = gimple_build_assign (make_ssa_name (boolean_type_node), EQ_EXPR,
    3564              :                                res_ops[0], build_zero_cst (input_type));
    3565           10 :       gimple_set_location (g, gimple_location (stmt));
    3566           10 :       gimple_seq_add_stmt (&seq, g);
    3567           10 :       tree cond = gimple_assign_lhs (g);
    3568           10 :       g = gimple_build_assign (make_ssa_name (integer_type_node),
    3569              :                                COND_EXPR, cond,
    3570           10 :                                build_int_cst (integer_type_node, zero_val),
    3571              :                                prev_lhs);
    3572           10 :       gimple_set_location (g, gimple_location (stmt));
    3573           10 :       gimple_seq_add_stmt (&seq, g);
    3574           10 :       prev_lhs = gimple_assign_lhs (g);
    3575              :     }
    3576              : 
    3577           21 :   if (fn == IFN_CLZ)
    3578              :     {
    3579           11 :       g = gimple_build_assign (make_ssa_name (integer_type_node),
    3580              :                                MINUS_EXPR,
    3581              :                                build_int_cst (integer_type_node,
    3582           11 :                                               input_bits - 1),
    3583              :                                prev_lhs);
    3584           11 :       gimple_set_location (g, gimple_location (stmt));
    3585           11 :       gimple_seq_add_stmt (&seq, g);
    3586           11 :       prev_lhs = gimple_assign_lhs (g);
    3587              :     }
    3588              : 
    3589           21 :   g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
    3590           21 :   gimple_seq_add_stmt (&seq, g);
    3591           21 :   gsi_replace_with_seq (gsi, seq, true);
    3592           21 :   return true;
    3593           21 : }
    3594              : 
    3595              : 
    3596              : /* Determine whether applying the 2 permutations (mask1 then mask2)
    3597              :    gives back one of the input.  */
    3598              : 
    3599              : static int
    3600           42 : is_combined_permutation_identity (tree mask1, tree mask2)
    3601              : {
    3602           42 :   tree mask;
    3603           42 :   unsigned HOST_WIDE_INT nelts, i, j;
    3604           42 :   bool maybe_identity1 = true;
    3605           42 :   bool maybe_identity2 = true;
    3606              : 
    3607           42 :   gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
    3608              :                        && TREE_CODE (mask2) == VECTOR_CST);
    3609              : 
    3610              :   /* For VLA masks, check for the following pattern:
    3611              :      v1 = VEC_PERM_EXPR (v0, ..., mask1)
    3612              :      v2 = VEC_PERM_EXPR (v1, ..., mask2)
    3613              :      -->
    3614              :      v2 = v0
    3615              :      if mask1 == mask2 == {nelts - 1, nelts - 2, ...}.  */
    3616              : 
    3617           42 :   if (operand_equal_p (mask1, mask2, 0)
    3618           42 :       && !VECTOR_CST_NELTS (mask1).is_constant ())
    3619              :     {
    3620              :       vec_perm_builder builder;
    3621              :       if (tree_to_vec_perm_builder (&builder, mask1))
    3622              :         {
    3623              :           poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1));
    3624              :           vec_perm_indices sel (builder, 1, nelts);
    3625              :           if (sel.series_p (0, 1, nelts - 1, -1))
    3626              :             return 1;
    3627              :         }
    3628              :     }
    3629              : 
    3630           42 :   mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
    3631           42 :   if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
    3632              :     return 0;
    3633              : 
    3634           42 :   if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
    3635              :     return 0;
    3636           72 :   for (i = 0; i < nelts; i++)
    3637              :     {
    3638           72 :       tree val = VECTOR_CST_ELT (mask, i);
    3639           72 :       gcc_assert (TREE_CODE (val) == INTEGER_CST);
    3640           72 :       j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
    3641           72 :       if (j == i)
    3642              :         maybe_identity2 = false;
    3643           55 :       else if (j == i + nelts)
    3644              :         maybe_identity1 = false;
    3645              :       else
    3646              :         return 0;
    3647              :     }
    3648            0 :   return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
    3649              : }
    3650              : 
    3651              : /* Combine a shuffle with its arguments.  Returns true if there were any
    3652              :    changes made.  */
    3653              : 
    3654              : static bool
    3655       190530 : simplify_permutation (gimple_stmt_iterator *gsi)
    3656              : {
    3657       190530 :   gimple *stmt = gsi_stmt (*gsi);
    3658       190530 :   gimple *def_stmt = NULL;
    3659       190530 :   tree op0, op1, op2, op3, arg0, arg1;
    3660       190530 :   enum tree_code code, code2 = ERROR_MARK;
    3661       190530 :   bool single_use_op0 = false;
    3662              : 
    3663       190530 :   gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
    3664              : 
    3665       190530 :   op0 = gimple_assign_rhs1 (stmt);
    3666       190530 :   op1 = gimple_assign_rhs2 (stmt);
    3667       190530 :   op2 = gimple_assign_rhs3 (stmt);
    3668              : 
    3669       190530 :   if (TREE_CODE (op2) != VECTOR_CST)
    3670              :     return false;
    3671              : 
    3672       187761 :   if (TREE_CODE (op0) == VECTOR_CST)
    3673              :     {
    3674              :       code = VECTOR_CST;
    3675              :       arg0 = op0;
    3676              :     }
    3677       185895 :   else if (TREE_CODE (op0) == SSA_NAME)
    3678              :     {
    3679       185895 :       def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
    3680       185895 :       if (!def_stmt)
    3681              :         return false;
    3682       177555 :       code = gimple_assign_rhs_code (def_stmt);
    3683       177555 :       if (code == VIEW_CONVERT_EXPR)
    3684              :         {
    3685         1621 :           tree rhs = gimple_assign_rhs1 (def_stmt);
    3686         1621 :           tree name = TREE_OPERAND (rhs, 0);
    3687         1621 :           if (TREE_CODE (name) != SSA_NAME)
    3688              :             return false;
    3689         1621 :           if (!has_single_use (name))
    3690          250 :             single_use_op0 = false;
    3691              :           /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
    3692              :              but still keep the code to indicate it comes from
    3693              :              VIEW_CONVERT_EXPR.  */
    3694         1621 :           def_stmt = SSA_NAME_DEF_STMT (name);
    3695         1621 :           if (!def_stmt || !is_gimple_assign (def_stmt))
    3696              :             return false;
    3697          834 :           if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
    3698              :             return false;
    3699              :         }
    3700       176200 :       if (!can_propagate_from (def_stmt))
    3701              :         return false;
    3702        24440 :       arg0 = gimple_assign_rhs1 (def_stmt);
    3703              :     }
    3704              :   else
    3705              :     return false;
    3706              : 
    3707              :   /* Two consecutive shuffles.  */
    3708        24440 :   if (code == VEC_PERM_EXPR)
    3709              :     {
    3710         6657 :       tree orig;
    3711         6657 :       int ident;
    3712              : 
    3713         6657 :       if (op0 != op1)
    3714              :         return false;
    3715           42 :       op3 = gimple_assign_rhs3 (def_stmt);
    3716           42 :       if (TREE_CODE (op3) != VECTOR_CST)
    3717              :         return false;
    3718           42 :       ident = is_combined_permutation_identity (op3, op2);
    3719           42 :       if (!ident)
    3720              :         return false;
    3721            0 :       orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
    3722            0 :                           : gimple_assign_rhs2 (def_stmt);
    3723            0 :       gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
    3724            0 :       gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
    3725            0 :       gimple_set_num_ops (stmt, 2);
    3726            0 :       update_stmt (stmt);
    3727            0 :       remove_prop_source_from_use (op0);
    3728            0 :       return true;
    3729              :     }
    3730        19649 :   else if (code == CONSTRUCTOR
    3731        19649 :            || code == VECTOR_CST
    3732              :            || code == VIEW_CONVERT_EXPR)
    3733              :     {
    3734         4658 :       if (op0 != op1)
    3735              :         {
    3736         4474 :           if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
    3737              :             return false;
    3738              : 
    3739         3819 :           if (TREE_CODE (op1) == VECTOR_CST)
    3740              :             arg1 = op1;
    3741         3186 :           else if (TREE_CODE (op1) == SSA_NAME)
    3742              :             {
    3743         3186 :               gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
    3744         3186 :               if (!def_stmt2)
    3745              :                 return false;
    3746         1678 :               code2 = gimple_assign_rhs_code (def_stmt2);
    3747         1678 :               if (code2 == VIEW_CONVERT_EXPR)
    3748              :                 {
    3749            4 :                   tree rhs = gimple_assign_rhs1 (def_stmt2);
    3750            4 :                   tree name = TREE_OPERAND (rhs, 0);
    3751            4 :                   if (TREE_CODE (name) != SSA_NAME)
    3752              :                     return false;
    3753            4 :                   if (!has_single_use (name))
    3754              :                     return false;
    3755            3 :                   def_stmt2 = SSA_NAME_DEF_STMT (name);
    3756            3 :                   if (!def_stmt2 || !is_gimple_assign (def_stmt2))
    3757              :                     return false;
    3758            0 :                   if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
    3759              :                     return false;
    3760              :                 }
    3761         1674 :               else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
    3762              :                 return false;
    3763         1485 :               if (!can_propagate_from (def_stmt2))
    3764              :                 return false;
    3765         1485 :               arg1 = gimple_assign_rhs1 (def_stmt2);
    3766              :             }
    3767              :           else
    3768              :             return false;
    3769              :         }
    3770              :       else
    3771              :         {
    3772              :           /* Already used twice in this statement.  */
    3773          184 :           if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
    3774              :             return false;
    3775              :           arg1 = arg0;
    3776              :         }
    3777              : 
    3778              :       /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
    3779              :          operands source, check whether it's valid to transform and prepare
    3780              :          the required new operands.  */
    3781         2234 :       if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
    3782              :         {
    3783              :           /* Figure out the target vector type to which operands should be
    3784              :              converted.  If both are CONSTRUCTOR, the types should be the
    3785              :              same, otherwise, use the one of CONSTRUCTOR.  */
    3786           24 :           tree tgt_type = NULL_TREE;
    3787           24 :           if (code == VIEW_CONVERT_EXPR)
    3788              :             {
    3789           24 :               gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
    3790           24 :               code = CONSTRUCTOR;
    3791           24 :               tgt_type = TREE_TYPE (arg0);
    3792              :             }
    3793           24 :           if (code2 == VIEW_CONVERT_EXPR)
    3794              :             {
    3795            0 :               tree arg1_type = TREE_TYPE (arg1);
    3796            0 :               if (tgt_type == NULL_TREE)
    3797              :                 tgt_type = arg1_type;
    3798            0 :               else if (tgt_type != arg1_type)
    3799           23 :                 return false;
    3800              :             }
    3801              : 
    3802           24 :           if (!VECTOR_TYPE_P (tgt_type))
    3803              :             return false;
    3804           24 :           tree op2_type = TREE_TYPE (op2);
    3805              : 
    3806              :           /* Figure out the shrunk factor.  */
    3807           24 :           poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
    3808           24 :           poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
    3809           24 :           if (maybe_gt (tgt_units, op2_units))
    3810              :             return false;
    3811           24 :           unsigned int factor;
    3812           47 :           if (!constant_multiple_p (op2_units, tgt_units, &factor))
    3813              :             return false;
    3814              : 
    3815              :           /* Build the new permutation control vector as target vector.  */
    3816           24 :           vec_perm_builder builder;
    3817           24 :           if (!tree_to_vec_perm_builder (&builder, op2))
    3818              :             return false;
    3819           24 :           vec_perm_indices indices (builder, 2, op2_units);
    3820           24 :           vec_perm_indices new_indices;
    3821           24 :           if (new_indices.new_shrunk_vector (indices, factor))
    3822              :             {
    3823            1 :               tree mask_type = tgt_type;
    3824            1 :               if (!VECTOR_INTEGER_TYPE_P (mask_type))
    3825              :                 {
    3826            0 :                   tree elem_type = TREE_TYPE (mask_type);
    3827            0 :                   unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
    3828            0 :                   tree int_type = build_nonstandard_integer_type (elem_size, 0);
    3829            0 :                   mask_type = build_vector_type (int_type, tgt_units);
    3830              :                 }
    3831            1 :               op2 = vec_perm_indices_to_tree (mask_type, new_indices);
    3832              :             }
    3833              :           else
    3834           23 :             return false;
    3835              : 
    3836              :           /* Convert the VECTOR_CST to the appropriate vector type.  */
    3837            1 :           if (tgt_type != TREE_TYPE (arg0))
    3838            0 :             arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
    3839            1 :           else if (tgt_type != TREE_TYPE (arg1))
    3840            0 :             arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
    3841           47 :         }
    3842              : 
    3843              :       /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before.  */
    3844         2211 :       gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
    3845              : 
    3846              :       /* Shuffle of a constructor.  */
    3847         2211 :       tree res_type
    3848         2211 :         = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)),
    3849         2211 :                              TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)));
    3850         2211 :       tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
    3851         2211 :       if (!opt
    3852          280 :           || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
    3853              :         return false;
    3854              :       /* Found VIEW_CONVERT_EXPR before, need one explicit conversion.  */
    3855          280 :       if (res_type != TREE_TYPE (op0))
    3856              :         {
    3857            1 :           tree name = make_ssa_name (TREE_TYPE (opt));
    3858            1 :           gimple *ass_stmt = gimple_build_assign (name, opt);
    3859            1 :           gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
    3860            1 :           opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
    3861              :         }
    3862          280 :       gimple_assign_set_rhs_from_tree (gsi, opt);
    3863          280 :       update_stmt (gsi_stmt (*gsi));
    3864          280 :       if (TREE_CODE (op0) == SSA_NAME)
    3865            1 :         remove_prop_source_from_use (op0);
    3866          280 :       if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
    3867            0 :         remove_prop_source_from_use (op1);
    3868          280 :       return true;
    3869              :     }
    3870              : 
    3871              :   return false;
    3872              : }
    3873              : 
    3874              : /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
    3875              :    conversions with code CONV_CODE or update it if still ERROR_MARK.
    3876              :    Return NULL_TREE if no such matching def was found.  */
    3877              : 
    3878              : static tree
    3879       426781 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
    3880              : {
    3881       426781 :   if (TREE_CODE (val) != SSA_NAME)
    3882              :     return NULL_TREE ;
    3883       399850 :   gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
    3884       399850 :   if (!def_stmt)
    3885              :     return NULL_TREE;
    3886       320999 :   enum tree_code code = gimple_assign_rhs_code (def_stmt);
    3887       320999 :   if (code == FLOAT_EXPR
    3888       320999 :       || code == FIX_TRUNC_EXPR
    3889              :       || CONVERT_EXPR_CODE_P (code))
    3890              :     {
    3891       186937 :       tree op1 = gimple_assign_rhs1 (def_stmt);
    3892       186937 :       if (conv_code == ERROR_MARK)
    3893        89509 :         conv_code = code;
    3894        97428 :       else if (conv_code != code)
    3895              :         return NULL_TREE;
    3896       186912 :       if (TREE_CODE (op1) != SSA_NAME)
    3897              :         return NULL_TREE;
    3898        78799 :       def_stmt = SSA_NAME_DEF_STMT (op1);
    3899        78799 :       if (! is_gimple_assign (def_stmt))
    3900              :         return NULL_TREE;
    3901        63056 :       code = gimple_assign_rhs_code (def_stmt);
    3902              :     }
    3903       197118 :   if (code != BIT_FIELD_REF)
    3904              :     return NULL_TREE;
    3905        24393 :   return gimple_assign_rhs1 (def_stmt);
    3906              : }
    3907              : 
    3908              : /* Recognize a VEC_PERM_EXPR.  Returns true if there were any changes.  */
    3909              : 
    3910              : static bool
    3911       164380 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
    3912              : {
    3913       164380 :   gimple *stmt = gsi_stmt (*gsi);
    3914       164380 :   tree op, orig[2], type;
    3915       164380 :   unsigned i;
    3916       164380 :   unsigned HOST_WIDE_INT nelts;
    3917       164380 :   unsigned HOST_WIDE_INT refnelts;
    3918       164380 :   enum tree_code conv_code;
    3919       164380 :   constructor_elt *elt;
    3920              : 
    3921       164380 :   op = gimple_assign_rhs1 (stmt);
    3922       164380 :   type = TREE_TYPE (op);
    3923       164380 :   gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
    3924              :                        && TREE_CODE (type) == VECTOR_TYPE);
    3925              : 
    3926       164380 :   if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
    3927              :     return false;
    3928              : 
    3929       164380 :   orig[0] = NULL;
    3930       164380 :   orig[1] = NULL;
    3931       164380 :   tree orig_elem_type[2] = {};
    3932       164380 :   conv_code = ERROR_MARK;
    3933       164380 :   bool maybe_ident = true;
    3934       164380 :   bool maybe_blend[2] = { true, true };
    3935       164380 :   tree one_constant = NULL_TREE;
    3936       164380 :   tree one_nonconstant = NULL_TREE;
    3937       164380 :   tree subelt;
    3938       164380 :   auto_vec<tree> constants;
    3939       164380 :   constants.safe_grow_cleared (nelts, true);
    3940       164380 :   auto_vec<std::pair<unsigned, unsigned>, 64> elts;
    3941       164380 :   unsigned int tsubelts = 0;
    3942       460197 :   FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
    3943              :     {
    3944       426781 :       tree ref, op1;
    3945       426781 :       unsigned int elem, src_elem_size;
    3946       426781 :       unsigned HOST_WIDE_INT nsubelts = 1;
    3947              : 
    3948       426781 :       if (i >= nelts)
    3949       164380 :         return false;
    3950              : 
    3951              :       /* Look for elements extracted and possibly converted from
    3952              :          another vector.  */
    3953       426781 :       op1 = get_bit_field_ref_def (elt->value, conv_code);
    3954       426781 :       if (op1
    3955        24393 :           && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
    3956         6028 :           && VECTOR_TYPE_P (TREE_TYPE (ref))
    3957         6017 :           && (tree_nop_conversion_p (TREE_TYPE (op1),
    3958         6017 :                                      TREE_TYPE (TREE_TYPE (ref)))
    3959          893 :               || (VECTOR_TYPE_P (TREE_TYPE (op1))
    3960          133 :                   && tree_nop_conversion_p (TREE_TYPE (TREE_TYPE (op1)),
    3961          133 :                                             TREE_TYPE (TREE_TYPE (ref)))
    3962          133 :                   && TYPE_VECTOR_SUBPARTS (TREE_TYPE (op1))
    3963          133 :                         .is_constant (&nsubelts)))
    3964         5257 :           && constant_multiple_p (bit_field_size (op1), nsubelts,
    3965              :                                   &src_elem_size)
    3966       432038 :           && constant_multiple_p (bit_field_offset (op1), src_elem_size, &elem)
    3967       432038 :           && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
    3968              :         {
    3969              :           unsigned int j;
    3970         5626 :           for (j = 0; j < 2; ++j)
    3971              :             {
    3972         5597 :               if (!orig[j])
    3973              :                 {
    3974         2401 :                   if (j == 0
    3975         2602 :                       || useless_type_conversion_p (TREE_TYPE (orig[0]),
    3976          201 :                                                     TREE_TYPE (ref)))
    3977              :                     break;
    3978              :                 }
    3979         3196 :               else if (ref == orig[j])
    3980              :                 break;
    3981              :             }
    3982              :           /* Found a suitable vector element.  */
    3983         5257 :           if (j < 2)
    3984              :             {
    3985         5228 :               orig[j] = ref;
    3986              :               /* Track what element type was actually extracted (which may
    3987              :                  differ in signedness from the vector's element type due to
    3988              :                  tree_nop_conversion_p).  */
    3989         5228 :               if (!orig_elem_type[j])
    3990         2397 :                 orig_elem_type[j] = TREE_TYPE (op1);
    3991         5228 :               if (elem != i || j != 0)
    3992         2154 :                 maybe_ident = false;
    3993         5228 :               if (elem != i)
    3994         2094 :                 maybe_blend[j] = false;
    3995        10715 :               for (unsigned int k = 0; k < nsubelts; ++k)
    3996         5487 :                 elts.safe_push (std::make_pair (j, elem + k));
    3997         5228 :               tsubelts += nsubelts;
    3998         5228 :               continue;
    3999         5228 :             }
    4000              :           /* Else fallthru.  */
    4001              :         }
    4002              :       /* Handle elements not extracted from a vector.
    4003              :           1. constants by permuting with constant vector
    4004              :           2. a unique non-constant element by permuting with a splat vector  */
    4005       421553 :       if (orig[1]
    4006       258303 :           && orig[1] != error_mark_node)
    4007              :         return false;
    4008       421524 :       orig[1] = error_mark_node;
    4009       421524 :       if (VECTOR_TYPE_P (TREE_TYPE (elt->value))
    4010       421524 :           && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (elt->value))
    4011         5713 :                         .is_constant (&nsubelts))
    4012              :         return false;
    4013       421524 :       if (CONSTANT_CLASS_P (elt->value))
    4014              :         {
    4015        26927 :           if (one_nonconstant)
    4016              :             return false;
    4017        18350 :           if (!one_constant)
    4018         8942 :             one_constant = TREE_CODE (elt->value) == VECTOR_CST
    4019         8942 :                            ? VECTOR_CST_ELT (elt->value, 0)
    4020              :                            : elt->value;
    4021        18350 :           if (TREE_CODE (elt->value) == VECTOR_CST)
    4022              :             {
    4023          687 :               for (unsigned int k = 0; k < nsubelts; k++)
    4024          507 :                 constants[tsubelts + k] = VECTOR_CST_ELT (elt->value, k);
    4025              :             }
    4026              :           else
    4027        18170 :             constants[tsubelts] = elt->value;
    4028              :         }
    4029              :       else
    4030              :         {
    4031       394597 :           if (one_constant)
    4032              :             return false;
    4033       386120 :           subelt = VECTOR_TYPE_P (TREE_TYPE (elt->value))
    4034       386120 :                    ? ssa_uniform_vector_p (elt->value)
    4035              :                    : elt->value;
    4036       386120 :           if (!subelt)
    4037              :             return false;
    4038       380829 :           if (!one_nonconstant)
    4039              :             one_nonconstant = subelt;
    4040       231812 :           else if (!operand_equal_p (one_nonconstant, subelt, 0))
    4041              :             return false;
    4042              :         }
    4043       581507 :       for (unsigned int k = 0; k < nsubelts; ++k)
    4044       290918 :         elts.safe_push (std::make_pair (1, tsubelts + k));
    4045       290589 :       tsubelts += nsubelts;
    4046       290589 :       maybe_ident = false;
    4047              :     }
    4048              : 
    4049        66832 :   if (elts.length () < nelts)
    4050              :     return false;
    4051              : 
    4052        32165 :   if (! orig[0]
    4053        32165 :       || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
    4054              :     return false;
    4055         1577 :   refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
    4056              :   /* We currently do not handle larger destination vectors.  */
    4057         1577 :   if (refnelts < nelts)
    4058              :     return false;
    4059              : 
    4060              :   /* Determine the element type for the conversion source.
    4061              :      As orig_elem_type keeps track of the original type, check
    4062              :      if we need to perform a sign swap after permuting.
    4063              :      We need to be able to construct a vector type from the element
    4064              :      type which is not possible for e.g. BitInt or pointers
    4065              :      so pun with an integer type if needed.   */
    4066         1337 :   tree perm_eltype = TREE_TYPE (TREE_TYPE (orig[0]));
    4067         1337 :   bool sign_change_p = false;
    4068         1337 :   if (conv_code != ERROR_MARK
    4069          349 :       && orig_elem_type[0]
    4070         1686 :       && TYPE_SIGN (orig_elem_type[0]) != TYPE_SIGN (perm_eltype))
    4071              :     {
    4072           38 :       perm_eltype = signed_or_unsigned_type_for
    4073           38 :         (TYPE_UNSIGNED (orig_elem_type[0]), perm_eltype);
    4074           38 :       sign_change_p = true;
    4075              :     }
    4076         1337 :   tree conv_src_type = build_vector_type (perm_eltype, nelts);
    4077              : 
    4078         1337 :   if (maybe_ident)
    4079              :     {
    4080              :       /* When there is no conversion, use the target type directly.  */
    4081          495 :       if (conv_code == ERROR_MARK && nelts != refnelts)
    4082          495 :         conv_src_type = type;
    4083          495 :       if (conv_code != ERROR_MARK
    4084          495 :           && !supportable_convert_operation (conv_code, type, conv_src_type,
    4085              :                                              &conv_code))
    4086              :         {
    4087              :           /* Only few targets implement direct conversion patterns so try
    4088              :              some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR.  */
    4089           93 :           optab optab;
    4090           93 :           insn_code icode;
    4091           93 :           tree halfvectype, dblvectype;
    4092           93 :           enum tree_code unpack_op;
    4093              : 
    4094           93 :           if (!BYTES_BIG_ENDIAN)
    4095          169 :             unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
    4096           93 :                          ? VEC_UNPACK_FLOAT_LO_EXPR
    4097              :                          : VEC_UNPACK_LO_EXPR);
    4098              :           else
    4099              :             unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
    4100              :                          ? VEC_UNPACK_FLOAT_HI_EXPR
    4101              :                          : VEC_UNPACK_HI_EXPR);
    4102              : 
    4103              :           /* Conversions between DFP and FP have no special tree code
    4104              :              but we cannot handle those since all relevant vector conversion
    4105              :              optabs only have a single mode.  */
    4106           15 :           if (CONVERT_EXPR_CODE_P (conv_code)
    4107           78 :               && FLOAT_TYPE_P (TREE_TYPE (type))
    4108           97 :               && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
    4109            2 :                   != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
    4110              :             return false;
    4111              : 
    4112           15 :           if (CONVERT_EXPR_CODE_P (conv_code)
    4113           77 :               && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
    4114           77 :                   == TYPE_PRECISION (TREE_TYPE (type)))
    4115            0 :               && orig_elem_type[0]
    4116            0 :               && useless_type_conversion_p (orig_elem_type[0],
    4117            0 :                                             TREE_TYPE (TREE_TYPE (orig[0])))
    4118            0 :               && mode_for_vector (as_a <scalar_mode>
    4119            0 :                                   (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
    4120            0 :                                   nelts * 2).exists ()
    4121            0 :               && (dblvectype
    4122            0 :                   = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
    4123            0 :                                        nelts * 2))
    4124              :               /* Only use it for vector modes or for vector booleans
    4125              :                  represented as scalar bitmasks.  See PR95528.  */
    4126            0 :               && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
    4127            0 :                   || VECTOR_BOOLEAN_TYPE_P (dblvectype))
    4128            0 :               && (optab = optab_for_tree_code (unpack_op,
    4129              :                                                dblvectype,
    4130              :                                                optab_default))
    4131            0 :               && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
    4132              :                   != CODE_FOR_nothing)
    4133           92 :               && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
    4134              :             {
    4135            0 :               gimple_seq stmts = NULL;
    4136            0 :               tree dbl;
    4137            0 :               if (refnelts == nelts)
    4138              :                 {
    4139              :                   /* ???  Paradoxical subregs don't exist, so insert into
    4140              :                      the lower half of a wider zero vector.  */
    4141            0 :                   dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
    4142              :                                       build_zero_cst (dblvectype), orig[0],
    4143            0 :                                       bitsize_zero_node);
    4144              :                 }
    4145            0 :               else if (refnelts == 2 * nelts)
    4146              :                 dbl = orig[0];
    4147              :               else
    4148            0 :                 dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
    4149            0 :                                     orig[0], TYPE_SIZE (dblvectype),
    4150            0 :                                     bitsize_zero_node);
    4151            0 :               gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    4152            0 :               gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
    4153              :             }
    4154           15 :           else if (CONVERT_EXPR_CODE_P (conv_code)
    4155           77 :                    && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
    4156           77 :                        == 2 * TYPE_PRECISION (TREE_TYPE (type)))
    4157            1 :                    && orig_elem_type[0]
    4158            1 :                    && useless_type_conversion_p (orig_elem_type[0],
    4159            1 :                                                  TREE_TYPE (TREE_TYPE (orig[0])))
    4160            1 :                    && mode_for_vector (as_a <scalar_mode>
    4161            1 :                                          (TYPE_MODE
    4162              :                                            (TREE_TYPE (TREE_TYPE (orig[0])))),
    4163            2 :                                        nelts / 2).exists ()
    4164            1 :                    && (halfvectype
    4165            1 :                          = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
    4166            1 :                                               nelts / 2))
    4167              :                    /* Only use it for vector modes or for vector booleans
    4168              :                       represented as scalar bitmasks.  See PR95528.  */
    4169            1 :                    && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
    4170            0 :                        || VECTOR_BOOLEAN_TYPE_P (halfvectype))
    4171            1 :                    && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
    4172              :                                                     halfvectype,
    4173              :                                                     optab_default))
    4174            1 :                    && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
    4175              :                        != CODE_FOR_nothing)
    4176           93 :                    && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
    4177              :             {
    4178            0 :               gimple_seq stmts = NULL;
    4179            0 :               tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
    4180            0 :                                        orig[0], TYPE_SIZE (halfvectype),
    4181            0 :                                        bitsize_zero_node);
    4182            0 :               tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
    4183            0 :                                        orig[0], TYPE_SIZE (halfvectype),
    4184            0 :                                        TYPE_SIZE (halfvectype));
    4185            0 :               gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    4186            0 :               gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
    4187              :                                               low, hig);
    4188              :             }
    4189              :           else
    4190           92 :             return false;
    4191            0 :           update_stmt (gsi_stmt (*gsi));
    4192            0 :           return true;
    4193              :         }
    4194          402 :       if (nelts != refnelts)
    4195              :         {
    4196           14 :           gassign *lowpart
    4197           14 :             = gimple_build_assign (make_ssa_name (conv_src_type),
    4198              :                                    build3 (BIT_FIELD_REF, conv_src_type,
    4199           14 :                                            orig[0], TYPE_SIZE (conv_src_type),
    4200              :                                            bitsize_zero_node));
    4201           14 :           gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
    4202           14 :           orig[0] = gimple_assign_lhs (lowpart);
    4203              :         }
    4204          388 :       else if (sign_change_p)
    4205              :         {
    4206            0 :           gassign *conv
    4207            0 :             = gimple_build_assign (make_ssa_name (conv_src_type),
    4208              :                                    build1 (VIEW_CONVERT_EXPR, conv_src_type,
    4209              :                                            orig[0]));
    4210            0 :           gsi_insert_before (gsi, conv, GSI_SAME_STMT);
    4211            0 :           orig[0] = gimple_assign_lhs (conv);
    4212              :         }
    4213          402 :       if (conv_code == ERROR_MARK)
    4214              :         {
    4215          385 :           tree src_type = TREE_TYPE (orig[0]);
    4216          385 :           if (!useless_type_conversion_p (type, src_type))
    4217              :             {
    4218            0 :               gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
    4219              :                                     TYPE_VECTOR_SUBPARTS (src_type))
    4220              :                           && tree_nop_conversion_p (TREE_TYPE (type),
    4221              :                                                     TREE_TYPE (src_type)));
    4222            0 :               tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
    4223            0 :               orig[0] = make_ssa_name (type);
    4224            0 :               gassign *assign = gimple_build_assign (orig[0], rhs);
    4225            0 :               gsi_insert_before (gsi, assign, GSI_SAME_STMT);
    4226              :             }
    4227          385 :           gimple_assign_set_rhs_from_tree (gsi, orig[0]);
    4228              :         }
    4229              :       else
    4230           17 :         gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
    4231              :                                         NULL_TREE, NULL_TREE);
    4232              :     }
    4233              :   else
    4234              :     {
    4235              :       /* If we combine a vector with a non-vector avoid cases where
    4236              :          we'll obviously end up with more GIMPLE stmts which is when
    4237              :          we'll later not fold this to a single insert into the vector
    4238              :          and we had a single extract originally.  See PR92819.  */
    4239          842 :       if (nelts == 2
    4240          482 :           && refnelts > 2
    4241          102 :           && orig[1] == error_mark_node
    4242           31 :           && !maybe_blend[0])
    4243          256 :         return false;
    4244          817 :       tree mask_type, perm_type;
    4245          817 :       perm_type = TREE_TYPE (orig[0]);
    4246          817 :       if (conv_code != ERROR_MARK
    4247          817 :           && !supportable_convert_operation (conv_code, type, conv_src_type,
    4248              :                                              &conv_code))
    4249              :         return false;
    4250              : 
    4251              :       /* Now that we know the number of elements of the source build the
    4252              :          permute vector.
    4253              :          ???  When the second vector has constant values we can shuffle
    4254              :          it and its source indexes to make the permutation supported.
    4255              :          For now it mimics a blend.  */
    4256          643 :       vec_perm_builder sel (refnelts, refnelts, 1);
    4257          643 :       bool all_same_p = true;
    4258         7302 :       for (i = 0; i < elts.length (); ++i)
    4259              :         {
    4260         3008 :           sel.quick_push (elts[i].second + elts[i].first * refnelts);
    4261         3008 :           all_same_p &= known_eq (sel[i], sel[0]);
    4262              :         }
    4263              :       /* And fill the tail with "something".  It's really don't care,
    4264              :          and ideally we'd allow VEC_PERM to have a smaller destination
    4265              :          vector.  As a heuristic:
    4266              : 
    4267              :          (a) if what we have so far duplicates a single element, make the
    4268              :              tail do the same
    4269              : 
    4270              :          (b) otherwise preserve a uniform orig[0].  This facilitates
    4271              :              later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR.  */
    4272         1151 :       for (; i < refnelts; ++i)
    4273         1016 :         sel.quick_push (all_same_p
    4274         1524 :                         ? sel[0]
    4275          140 :                         : (elts[0].second == 0 && elts[0].first == 0
    4276          700 :                            ? 0 : refnelts) + i);
    4277          815 :       vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
    4278          643 :       machine_mode vmode = TYPE_MODE (perm_type);
    4279          643 :       if ((cfun->curr_properties & PROP_gimple_lvec)
    4280          643 :           && !can_vec_perm_const_p (vmode, vmode, indices))
    4281              :         return false;
    4282          586 :       mask_type = build_vector_type (ssizetype, refnelts);
    4283          586 :       tree op2 = vec_perm_indices_to_tree (mask_type, indices);
    4284          586 :       bool converted_orig1 = false;
    4285          586 :       gimple_seq stmts = NULL;
    4286          586 :       if (!orig[1])
    4287          153 :         orig[1] = orig[0];
    4288          433 :       else if (orig[1] == error_mark_node
    4289          314 :                && one_nonconstant)
    4290              :         {
    4291              :           /* ???  We can see if we can safely convert to the original
    4292              :              element type.  */
    4293           90 :           converted_orig1 = conv_code != ERROR_MARK;
    4294           90 :           tree target_type = converted_orig1 ? type : perm_type;
    4295           90 :           tree nonconstant_for_splat = one_nonconstant;
    4296              :           /* If there's a nop conversion between the target element type and
    4297              :              the nonconstant's type, convert it.  */
    4298           90 :           if (!useless_type_conversion_p (TREE_TYPE (target_type),
    4299           90 :                                           TREE_TYPE (one_nonconstant)))
    4300            0 :             nonconstant_for_splat
    4301            0 :               = gimple_build (&stmts, NOP_EXPR, TREE_TYPE (target_type),
    4302              :                               one_nonconstant);
    4303           90 :           orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
    4304              :                                                   target_type,
    4305              :                                                   nonconstant_for_splat);
    4306           90 :         }
    4307          343 :       else if (orig[1] == error_mark_node)
    4308              :         {
    4309              :           /* ???  See if we can convert the vector to the original type.  */
    4310          224 :           converted_orig1 = conv_code != ERROR_MARK;
    4311          224 :           unsigned n = converted_orig1 ? nelts : refnelts;
    4312          207 :           tree target_type = converted_orig1 ? type : perm_type;
    4313          224 :           tree_vector_builder vec (target_type, n, 1);
    4314         1748 :           for (unsigned i = 0; i < n; ++i)
    4315         2924 :             if (i < nelts && constants[i])
    4316              :               {
    4317          778 :                 tree constant = constants[i];
    4318              :                 /* If there's a nop conversion, convert the constant.  */
    4319          778 :                 if (!useless_type_conversion_p (TREE_TYPE (target_type),
    4320          778 :                                                 TREE_TYPE (constant)))
    4321            2 :                   constant = fold_convert (TREE_TYPE (target_type), constant);
    4322          778 :                 vec.quick_push (constant);
    4323              :               }
    4324              :             else
    4325              :               {
    4326              :                 /* ??? Push a don't-care value.  */
    4327          746 :                 tree constant = one_constant;
    4328          746 :                 if (!useless_type_conversion_p (TREE_TYPE (target_type),
    4329          746 :                                                 TREE_TYPE (constant)))
    4330            2 :                   constant = fold_convert (TREE_TYPE (target_type), constant);
    4331          746 :                 vec.quick_push (constant);
    4332              :               }
    4333          224 :           orig[1] = vec.build ();
    4334          224 :         }
    4335          467 :       tree blend_op2 = NULL_TREE;
    4336          467 :       if (converted_orig1)
    4337              :         {
    4338              :           /* Make sure we can do a blend in the target type.  */
    4339           19 :           vec_perm_builder sel (nelts, nelts, 1);
    4340           87 :           for (i = 0; i < elts.length (); ++i)
    4341           68 :             sel.quick_push (elts[i].first
    4342           68 :                             ? elts[i].second + nelts : i);
    4343           19 :           vec_perm_indices indices (sel, 2, nelts);
    4344           19 :           machine_mode vmode = TYPE_MODE (type);
    4345           19 :           if ((cfun->curr_properties & PROP_gimple_lvec)
    4346           19 :               && !can_vec_perm_const_p (vmode, vmode, indices))
    4347            0 :             return false;
    4348           19 :           mask_type = build_vector_type (ssizetype, nelts);
    4349           19 :           blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
    4350           19 :         }
    4351              : 
    4352              :       /* For a real orig[1] (no splat, constant etc.) we might need to
    4353              :          nop-convert it.  Do so here.  */
    4354          586 :       if (orig[1] && orig[1] != error_mark_node
    4355          586 :           && !useless_type_conversion_p (perm_type, TREE_TYPE (orig[1]))
    4356          605 :           && tree_nop_conversion_p (TREE_TYPE (perm_type),
    4357           19 :                                     TREE_TYPE (TREE_TYPE (orig[1]))))
    4358            0 :         orig[1] = gimple_build (&stmts, VIEW_CONVERT_EXPR, perm_type,
    4359              :                                 orig[1]);
    4360              : 
    4361          586 :       tree orig1_for_perm
    4362          586 :         = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
    4363          586 :       tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
    4364              :                                orig[0], orig1_for_perm, op2);
    4365              :       /* If we're building a smaller vector, extract the element
    4366              :          with the proper type.  */
    4367          586 :       if (nelts != refnelts)
    4368          208 :         res = gimple_build (&stmts, BIT_FIELD_REF,
    4369              :                             conv_code != ERROR_MARK ? conv_src_type : type,
    4370              :                             res,
    4371          104 :                             TYPE_SIZE (conv_code != ERROR_MARK ? conv_src_type
    4372              :                                                                : type),
    4373          104 :                             bitsize_zero_node);
    4374              :       /* Otherwise, we can still have an intermediate sign change.
    4375              :          ??? In that case we have two subsequent conversions.
    4376              :          We should be able to merge them.  */
    4377          482 :       else if (sign_change_p)
    4378           14 :         res = gimple_build (&stmts, VIEW_CONVERT_EXPR, conv_src_type, res);
    4379              :       /* Finally, apply the conversion.  */
    4380          586 :       if (conv_code != ERROR_MARK)
    4381           52 :         res = gimple_build (&stmts, conv_code, type, res);
    4382          534 :       else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
    4383              :         {
    4384            3 :           gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
    4385              :                                 TYPE_VECTOR_SUBPARTS (perm_type))
    4386              :                       && tree_nop_conversion_p (TREE_TYPE (type),
    4387              :                                                 TREE_TYPE (perm_type)));
    4388            3 :           res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
    4389              :         }
    4390              :       /* Blend in the actual constant.  */
    4391          586 :       if (converted_orig1)
    4392           19 :         res = gimple_build (&stmts, VEC_PERM_EXPR, type,
    4393           19 :                             res, orig[1], blend_op2);
    4394          586 :       gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    4395          586 :       gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
    4396          643 :     }
    4397          988 :   update_stmt (gsi_stmt (*gsi));
    4398          988 :   return true;
    4399       164380 : }
    4400              : 
    4401              : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
    4402              :    lvalue.  This splits out an address computation stmt before *GSI
    4403              :    and returns a MEM_REF wrapping the address.  */
    4404              : 
    4405              : static tree
    4406         1104 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
    4407              : {
    4408         1104 :   if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
    4409          218 :     mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
    4410         1104 :   tree ptrtype = build_pointer_type (TREE_TYPE (ref));
    4411         1104 :   tree tem = make_ssa_name (ptrtype);
    4412         1104 :   gimple *new_stmt
    4413         1104 :     = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
    4414              :                                         unshare_expr (ref)));
    4415         1104 :   gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    4416         2208 :   ref = build2_loc (EXPR_LOCATION (ref),
    4417         1104 :                     MEM_REF, TREE_TYPE (ref), tem,
    4418         1104 :                     build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
    4419         1104 :   return ref;
    4420              : }
    4421              : 
    4422              : /* Rewrite the vector load at *GSI to component-wise loads if the load
    4423              :    is only used in BIT_FIELD_REF extractions with eventual intermediate
    4424              :    widening.  */
    4425              : 
    4426              : static void
    4427       293226 : optimize_vector_load (gimple_stmt_iterator *gsi)
    4428              : {
    4429       293226 :   gimple *stmt = gsi_stmt (*gsi);
    4430       293226 :   tree lhs = gimple_assign_lhs (stmt);
    4431       293226 :   tree rhs = gimple_assign_rhs1 (stmt);
    4432       293226 :   tree vuse = gimple_vuse (stmt);
    4433              : 
    4434              :   /* Gather BIT_FIELD_REFs to rewrite, looking through
    4435              :      VEC_UNPACK_{LO,HI}_EXPR.  */
    4436       293226 :   use_operand_p use_p;
    4437       293226 :   imm_use_iterator iter;
    4438       293226 :   bool rewrite = true;
    4439       293226 :   bool scalar_use = false;
    4440       293226 :   bool unpack_use = false;
    4441       293226 :   auto_vec<gimple *, 8> bf_stmts;
    4442       293226 :   auto_vec<tree, 8> worklist;
    4443       293226 :   worklist.quick_push (lhs);
    4444       295168 :   do
    4445              :     {
    4446       295168 :       tree def = worklist.pop ();
    4447       295168 :       unsigned HOST_WIDE_INT def_eltsize
    4448       295168 :         = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
    4449       667815 :       FOR_EACH_IMM_USE_FAST (use_p, iter, def)
    4450              :         {
    4451       352071 :           gimple *use_stmt = USE_STMT (use_p);
    4452       352071 :           if (is_gimple_debug (use_stmt))
    4453        77479 :             continue;
    4454       350683 :           tree use_lhs;
    4455       350683 :           if (!is_gimple_assign (use_stmt)
    4456              :               /* For alias reasons we move the use to the place of the
    4457              :                  load.  Avoid this when abnormals are involved.  */
    4458       350683 :               || ((TREE_CODE ((use_lhs = gimple_assign_lhs (use_stmt)))
    4459              :                    == SSA_NAME)
    4460       239381 :                   && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs)))
    4461              :             {
    4462              :               rewrite = false;
    4463       274592 :               break;
    4464              :             }
    4465       314713 :           enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
    4466       314713 :           tree use_rhs = gimple_assign_rhs1 (use_stmt);
    4467       386939 :           if (use_code == BIT_FIELD_REF
    4468        72227 :               && TREE_OPERAND (use_rhs, 0) == def
    4469              :               /* If its on the VEC_UNPACK_{HI,LO}_EXPR
    4470              :                  def need to verify it is element aligned.  */
    4471       386940 :               && (def == lhs
    4472          153 :                   || (known_eq (bit_field_size (use_rhs), def_eltsize)
    4473          153 :                       && constant_multiple_p (bit_field_offset (use_rhs),
    4474              :                                               def_eltsize)
    4475              :                       /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
    4476              :                          via a NOP_EXPR only for integral types.
    4477              :                          ???  Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR.  */
    4478          153 :                       && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
    4479              :             {
    4480        72226 :               if (!VECTOR_TYPE_P (TREE_TYPE (gimple_assign_lhs (use_stmt))))
    4481        70067 :                 scalar_use = true;
    4482        72226 :               bf_stmts.safe_push (use_stmt);
    4483        72226 :               continue;
    4484              :             }
    4485              :           /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR.  */
    4486       242487 :           if (def == lhs
    4487       240626 :               && (use_code == VEC_UNPACK_HI_EXPR
    4488       240626 :                   || use_code == VEC_UNPACK_LO_EXPR)
    4489         3865 :               && use_rhs == lhs)
    4490              :             {
    4491         3865 :               unpack_use = true;
    4492         3865 :               worklist.safe_push (gimple_assign_lhs (use_stmt));
    4493         3865 :               continue;
    4494              :             }
    4495              :           rewrite = false;
    4496              :           break;
    4497       295168 :         }
    4498       295168 :       if (!rewrite)
    4499              :         break;
    4500              :     }
    4501        41152 :   while (!worklist.is_empty ());
    4502              : 
    4503       293226 :   rewrite = rewrite && (scalar_use
    4504        18634 :                         || unpack_use
    4505          616 :                         || !can_implement_p (mov_optab,
    4506          616 :                                              TYPE_MODE (TREE_TYPE (lhs))));
    4507       293226 :   if (!rewrite)
    4508              :     {
    4509       274774 :       gsi_next (gsi);
    4510       274774 :       return;
    4511              :     }
    4512              :   /* We now have all ultimate uses of the load to rewrite in bf_stmts.  */
    4513              : 
    4514              :   /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
    4515              :      For TARGET_MEM_REFs we have to separate the LEA from the reference.  */
    4516        18452 :   tree load_rhs = rhs;
    4517        18452 :   if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
    4518         1103 :     load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
    4519              : 
    4520              :   /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
    4521              :      the place of the original load.  */
    4522       121414 :   for (gimple *use_stmt : bf_stmts)
    4523              :     {
    4524        66058 :       tree bfr = gimple_assign_rhs1 (use_stmt);
    4525        66058 :       tree new_rhs = unshare_expr (load_rhs);
    4526        66058 :       if (TREE_OPERAND (bfr, 0) != lhs)
    4527              :         {
    4528              :           /* When the BIT_FIELD_REF is on the promoted vector we have to
    4529              :              adjust it and emit a conversion afterwards.  */
    4530          152 :           gimple *def_stmt
    4531          152 :               = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
    4532          152 :           enum tree_code def_code
    4533          152 :               = gimple_assign_rhs_code (def_stmt);
    4534              : 
    4535              :           /* The adjusted BIT_FIELD_REF is of the promotion source
    4536              :              vector size and at half of the offset...  */
    4537          152 :           new_rhs = fold_build3 (BIT_FIELD_REF,
    4538              :                                  TREE_TYPE (TREE_TYPE (lhs)),
    4539              :                                  new_rhs,
    4540              :                                  TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
    4541              :                                  size_binop (EXACT_DIV_EXPR,
    4542              :                                              TREE_OPERAND (bfr, 2),
    4543              :                                              bitsize_int (2)));
    4544              :           /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR.  */
    4545          152 :           if (def_code == (!BYTES_BIG_ENDIAN
    4546              :                            ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
    4547           76 :             TREE_OPERAND (new_rhs, 2)
    4548          152 :               = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
    4549              :                             size_binop (EXACT_DIV_EXPR,
    4550              :                                         TYPE_SIZE (TREE_TYPE (lhs)),
    4551              :                                         bitsize_int (2)));
    4552          152 :           tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
    4553          152 :           gimple *new_stmt = gimple_build_assign (tem, new_rhs);
    4554          152 :           location_t loc = gimple_location (use_stmt);
    4555          152 :           gimple_set_location (new_stmt, loc);
    4556          152 :           gimple_set_vuse (new_stmt, vuse);
    4557          152 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    4558              :           /* Perform scalar promotion.  */
    4559          152 :           new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
    4560              :                                           NOP_EXPR, tem);
    4561          152 :           gimple_set_location (new_stmt, loc);
    4562          152 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    4563              :         }
    4564              :       else
    4565              :         {
    4566              :           /* When the BIT_FIELD_REF is on the original load result
    4567              :              we can just wrap that.  */
    4568        65906 :           tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
    4569              :                                       unshare_expr (load_rhs),
    4570              :                                       TREE_OPERAND (bfr, 1),
    4571              :                                       TREE_OPERAND (bfr, 2));
    4572        65906 :           gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
    4573              :                                                   new_rhs);
    4574        65906 :           location_t loc = gimple_location (use_stmt);
    4575        65906 :           gimple_set_location (new_stmt, loc);
    4576        65906 :           gimple_set_vuse (new_stmt, vuse);
    4577        65906 :           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    4578              :         }
    4579        66058 :       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    4580        66058 :       unlink_stmt_vdef (use_stmt);
    4581        66058 :       gsi_remove (&gsi2, true);
    4582              :     }
    4583              : 
    4584              :   /* Finally get rid of the intermediate stmts.  */
    4585        18452 :   gimple *use_stmt;
    4586        37508 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    4587              :     {
    4588          604 :       if (is_gimple_debug (use_stmt))
    4589              :         {
    4590          542 :           if (gimple_debug_bind_p (use_stmt))
    4591              :             {
    4592          542 :               gimple_debug_bind_reset_value (use_stmt);
    4593          542 :               update_stmt (use_stmt);
    4594              :             }
    4595          542 :           continue;
    4596              :         }
    4597           62 :       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    4598           62 :       unlink_stmt_vdef (use_stmt);
    4599           62 :       release_defs (use_stmt);
    4600           62 :       gsi_remove (&gsi2, true);
    4601        18452 :     }
    4602              :   /* And the original load.  */
    4603        18452 :   release_defs (stmt);
    4604        18452 :   gsi_remove (gsi, true);
    4605       293226 : }
    4606              : 
    4607              : 
    4608              : /* Primitive "lattice" function for gimple_simplify.  */
    4609              : 
    4610              : static tree
    4611   1659491741 : fwprop_ssa_val (tree name)
    4612              : {
    4613              :   /* First valueize NAME.  */
    4614   1659491741 :   if (TREE_CODE (name) == SSA_NAME
    4615   1659491741 :       && SSA_NAME_VERSION (name) < lattice.length ())
    4616              :     {
    4617   1658646299 :       tree val = lattice[SSA_NAME_VERSION (name)];
    4618   1658646299 :       if (val)
    4619   1659491741 :         name = val;
    4620              :     }
    4621              :   /* We continue matching along SSA use-def edges for SSA names
    4622              :      that are not single-use.  Currently there are no patterns
    4623              :      that would cause any issues with that.  */
    4624   1659491741 :   return name;
    4625              : }
    4626              : 
    4627              : /* Search for opportunities to free half of the lanes in the following pattern:
    4628              : 
    4629              :      v_in = {e0, e1, e2, e3}
    4630              :      v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
    4631              :      // v_1 = {e0, e2, e0, e2}
    4632              :      v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
    4633              :      // v_2 = {e1, e3, e1, e3}
    4634              : 
    4635              :      v_x = v_1 + v_2
    4636              :      // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
    4637              :      v_y = v_1 - v_2
    4638              :      // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
    4639              : 
    4640              :      v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
    4641              :      // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
    4642              : 
    4643              :    The last statement could be simplified to:
    4644              :      v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
    4645              :      // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
    4646              : 
    4647              :    Characteristic properties:
    4648              :    - v_1 and v_2 are created from the same input vector v_in and introduce the
    4649              :      lane duplication (in the selection operand) that we can eliminate.
    4650              :    - v_x and v_y are results from lane-preserving operations that use v_1 and
    4651              :      v_2 as inputs.
    4652              :    - v_out is created by selecting from duplicated lanes.  */
    4653              : 
    4654              : static bool
    4655       188381 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
    4656              : {
    4657       188381 :   unsigned HOST_WIDE_INT nelts;
    4658              : 
    4659       188381 :   gcc_checking_assert (stmt);
    4660       188381 :   gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
    4661       188381 :   basic_block bb = gimple_bb (stmt);
    4662              : 
    4663              :   /* Decompose the final vec permute statement.  */
    4664       188381 :   tree v_x = gimple_assign_rhs1 (stmt);
    4665       188381 :   tree v_y = gimple_assign_rhs2 (stmt);
    4666       188381 :   tree sel = gimple_assign_rhs3 (stmt);
    4667              : 
    4668       188381 :   if (TREE_CODE (sel) != VECTOR_CST
    4669       185612 :       || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
    4670       185612 :       || TREE_CODE (v_x) != SSA_NAME
    4671       183756 :       || TREE_CODE (v_y) != SSA_NAME
    4672       178487 :       || !has_single_use (v_x)
    4673       299540 :       || !has_single_use (v_y))
    4674        79026 :     return false;
    4675              : 
    4676              :   /* Don't analyse sequences with many lanes.  */
    4677       109355 :   if (nelts > 4)
    4678              :     return false;
    4679              : 
    4680              :   /* Lookup the definition of v_x and v_y.  */
    4681       105743 :   gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
    4682       105743 :   gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
    4683       105366 :   if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
    4684       211109 :       || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
    4685              :     return false;
    4686              : 
    4687              :   /* Check the operations that define v_x and v_y.  */
    4688       105359 :   if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
    4689       107437 :       || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
    4690              :     return false;
    4691              : 
    4692         2078 :   tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
    4693         2078 :   tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
    4694         2078 :   tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
    4695         2078 :   tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
    4696              : 
    4697         2078 :   if (v_x_stmt == v_y_stmt
    4698         2078 :       || TREE_CODE (v_x_1) != SSA_NAME
    4699         2075 :       || TREE_CODE (v_x_2) != SSA_NAME
    4700         2051 :       || num_imm_uses (v_x_1) != 2
    4701         3971 :       || num_imm_uses (v_x_2) != 2)
    4702              :     return false;
    4703              : 
    4704         1857 :   if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
    4705              :     {
    4706              :       /* Allow operands of commutative operators to swap.  */
    4707          651 :       if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
    4708              :         {
    4709              :           /* Keep v_x_1 the first operand for non-commutative operators.  */
    4710          244 :           std::swap (v_x_1, v_x_2);
    4711          244 :           if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
    4712              :             return false;
    4713              :         }
    4714          407 :       else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
    4715              :         {
    4716          407 :           if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
    4717              :             return false;
    4718              :         }
    4719              :       else
    4720              :         return false;
    4721              :     }
    4722         1857 :   gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
    4723         1857 :   gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
    4724         1793 :   if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
    4725         3650 :       || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
    4726              :     return false;
    4727              : 
    4728         1789 :   if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
    4729         1911 :       || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
    4730              :     return false;
    4731              : 
    4732              :   /* Decompose initial VEC_PERM_EXPRs.  */
    4733          108 :   tree v_in = gimple_assign_rhs1 (v_1_stmt);
    4734          108 :   tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
    4735          108 :   tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
    4736          108 :   if (v_in != gimple_assign_rhs2 (v_1_stmt)
    4737          103 :       || v_in != gimple_assign_rhs1 (v_2_stmt)
    4738          209 :       || v_in != gimple_assign_rhs2 (v_2_stmt))
    4739              :     return false;
    4740              : 
    4741          101 :   unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
    4742          101 :   if (TREE_CODE (v_1_sel) != VECTOR_CST
    4743          101 :       || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
    4744          101 :       || TREE_CODE (v_2_sel) != VECTOR_CST
    4745          202 :       || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
    4746            0 :     return false;
    4747              : 
    4748          101 :   if (nelts != v_1_nelts || nelts != v_2_nelts)
    4749              :     return false;
    4750              : 
    4751              :   /* Create the new selector.  */
    4752          101 :   vec_perm_builder new_sel_perm (nelts, nelts, 1);
    4753          101 :   auto_vec<bool> lanes (nelts);
    4754          101 :   lanes.quick_grow_cleared (nelts);
    4755          505 :   for (unsigned int i = 0; i < nelts; i++)
    4756              :     {
    4757              :       /* Extract the i-th value from the selector.  */
    4758          404 :       unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
    4759          404 :       unsigned int lane = sel_cst % nelts;
    4760          404 :       unsigned int offs = sel_cst / nelts;
    4761              : 
    4762              :       /* Check what's in the lane.  */
    4763          404 :       unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
    4764          404 :       unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
    4765              : 
    4766              :       /* Reuse previous lane (if any).  */
    4767          404 :       unsigned int l = 0;
    4768          687 :       for (; l < lane; l++)
    4769              :         {
    4770          481 :           if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
    4771          481 :               && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
    4772              :             break;
    4773              :         }
    4774              : 
    4775              :       /* Add to narrowed selector.  */
    4776          404 :       new_sel_perm.quick_push (l + offs * nelts);
    4777              : 
    4778              :       /* Mark lane as used.  */
    4779          404 :       lanes[l] = true;
    4780              :     }
    4781              : 
    4782              :   /* Count how many lanes are need.  */
    4783              :   unsigned int cnt = 0;
    4784          505 :   for (unsigned int i = 0; i < nelts; i++)
    4785          404 :     cnt += lanes[i];
    4786              : 
    4787              :   /* If more than (nelts/2) lanes are needed, skip the sequence.  */
    4788          101 :   if (cnt > nelts / 2)
    4789              :     return false;
    4790              : 
    4791              :   /* Check if the resulting permutation is cheap.  */
    4792          101 :   vec_perm_indices new_indices (new_sel_perm, 2, nelts);
    4793          101 :   tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
    4794          101 :   machine_mode vmode = TYPE_MODE (vectype);
    4795          101 :   if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
    4796              :     return false;
    4797              : 
    4798          101 :   *seq = XNEW (struct _vec_perm_simplify_seq);
    4799          101 :   (*seq)->stmt = stmt;
    4800          101 :   (*seq)->v_1_stmt = v_1_stmt;
    4801          101 :   (*seq)->v_2_stmt = v_2_stmt;
    4802          101 :   (*seq)->v_x_stmt = v_x_stmt;
    4803          101 :   (*seq)->v_y_stmt = v_y_stmt;
    4804          101 :   (*seq)->nelts = nelts;
    4805          101 :   (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
    4806              : 
    4807          101 :   if (dump_file)
    4808              :     {
    4809           28 :       fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
    4810           28 :       print_gimple_stmt (dump_file, stmt, 0);
    4811              : 
    4812           28 :       if (dump_flags & TDF_DETAILS)
    4813              :         {
    4814           28 :           fprintf (dump_file, "\tNarrowed vec_perm selector: ");
    4815           28 :           print_generic_expr (dump_file, (*seq)->new_sel);
    4816           28 :           fprintf (dump_file, "\n");
    4817              :         }
    4818              :     }
    4819              : 
    4820              :   return true;
    4821          202 : }
    4822              : 
    4823              : /* Reduce the lane consumption of a simplifiable vec perm sequence.  */
    4824              : 
    4825              : static void
    4826           74 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
    4827              : {
    4828           74 :   gassign *stmt = seq->stmt;
    4829           74 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4830              :     {
    4831           22 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    4832           22 :       fprintf (dump_file, "Old stmt: ");
    4833           22 :       print_gimple_stmt (dump_file, stmt, 0);
    4834              :     }
    4835              : 
    4836              :   /* Update the last VEC_PERM statement.  */
    4837           74 :   gimple_assign_set_rhs3 (stmt, seq->new_sel);
    4838           74 :   update_stmt (stmt);
    4839              : 
    4840           74 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4841              :     {
    4842           22 :       fprintf (dump_file, "New stmt: ");
    4843           22 :       print_gimple_stmt (dump_file, stmt, 0);
    4844              :     }
    4845           74 : }
    4846              : 
    4847              : /* Test if we can blend two simplifiable vec permute sequences.
    4848              :    NEED_SWAP will be set, if sequences must be swapped for blending.  */
    4849              : 
    4850              : static bool
    4851           47 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
    4852              :                                     vec_perm_simplify_seq seq2,
    4853              :                                     bool *need_swap)
    4854              : {
    4855           47 :   unsigned int nelts = seq1->nelts;
    4856           47 :   basic_block bb = gimple_bb (seq1->stmt);
    4857              : 
    4858           47 :   gcc_assert (gimple_bb (seq2->stmt) == bb);
    4859              : 
    4860              :   /* BBs and number of elements must be equal.  */
    4861           47 :   if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
    4862              :     return false;
    4863              : 
    4864              :   /* We need vectors of the same type.  */
    4865           47 :   if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
    4866           47 :       != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
    4867              :     return false;
    4868              : 
    4869              :   /* We require isomorphic operators.  */
    4870           41 :   if (((gimple_assign_rhs_code (seq1->v_x_stmt)
    4871           41 :         != gimple_assign_rhs_code (seq2->v_x_stmt))
    4872           41 :        || (gimple_assign_rhs_code (seq1->v_y_stmt)
    4873           41 :            != gimple_assign_rhs_code (seq2->v_y_stmt))))
    4874              :     return false;
    4875              : 
    4876              :   /* We cannot have any dependencies between the sequences.
    4877              : 
    4878              :      For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
    4879              :      seq1's v_in is defined before these statements, but we need
    4880              :      to check if seq2's v_in is defined before them as well.
    4881              : 
    4882              :      Further, we will reuse seq2->stmt.  We need to ensure that
    4883              :      seq1->v_x_stmt and seq1->v_y_stmt are before it.
    4884              : 
    4885              :      Note, that we don't need to check the BBs here, because all
    4886              :      statements of both sequences have to be in the same BB.  */
    4887              : 
    4888           41 :   tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
    4889           41 :   if (TREE_CODE (seq2_v_in) != SSA_NAME)
    4890              :     return false;
    4891              : 
    4892           41 :   gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
    4893           41 :   if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
    4894           41 :       || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
    4895           37 :       || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
    4896           37 :       || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
    4897              :     {
    4898            4 :       tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
    4899            4 :       if (TREE_CODE (seq1_v_in) != SSA_NAME)
    4900              :         return false;
    4901              : 
    4902            4 :       gassign *seq1_v_in_stmt
    4903            4 :         = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
    4904              :       /* Let's try to see if we succeed when swapping the sequences.  */
    4905            4 :       if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
    4906            0 :           || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
    4907            0 :           || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
    4908            0 :           || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
    4909              :         return false;
    4910            0 :       *need_swap = true;
    4911              :     }
    4912              :   else
    4913           37 :     *need_swap = false;
    4914              : 
    4915           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4916           11 :     fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
    4917              : 
    4918              :   return true;
    4919              : }
    4920              : 
    4921              : /* Calculate the permutations for blending the two given vec permute
    4922              :    sequences.  This may fail if the resulting permutation is not
    4923              :    supported.  */
    4924              : 
    4925              : static bool
    4926           37 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
    4927              :                                   vec_perm_simplify_seq seq2,
    4928              :                                   vec_perm_indices *seq2_stmt_indices,
    4929              :                                   vec_perm_indices *seq1_v_1_stmt_indices,
    4930              :                                   vec_perm_indices *seq1_v_2_stmt_indices)
    4931              : {
    4932           37 :   unsigned int i;
    4933           37 :   unsigned int nelts = seq1->nelts;
    4934           37 :   auto_vec<unsigned int> lane_assignment;
    4935           37 :   lane_assignment.create (nelts);
    4936              : 
    4937              :   /* Mark all lanes as free.  */
    4938           37 :   lane_assignment.quick_grow_cleared (nelts);
    4939              : 
    4940              :   /* Allocate lanes for seq1.  */
    4941          185 :   for (i = 0; i < nelts; i++)
    4942              :     {
    4943          148 :       unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
    4944          148 :       l %= nelts;
    4945          148 :       lane_assignment[l] = 1;
    4946              :     }
    4947              : 
    4948              :   /* Allocate lanes for seq2 and calculate selector for seq2->stmt.  */
    4949           37 :   vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
    4950          185 :   for (i = 0; i < nelts; i++)
    4951              :     {
    4952          148 :       unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
    4953          148 :       unsigned int lane = sel % nelts;
    4954          148 :       unsigned int offs = sel / nelts;
    4955          148 :       unsigned int new_sel;
    4956              : 
    4957              :       /* Check if we already allocated the lane for seq2.  */
    4958          148 :       unsigned int j = 0;
    4959          263 :       for (; j < i; j++)
    4960              :         {
    4961          189 :           unsigned int sel_old;
    4962          189 :           sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
    4963          189 :           unsigned int lane_old = sel_old % nelts;
    4964          189 :           if (lane == lane_old)
    4965              :             {
    4966           74 :               new_sel = seq2_stmt_sel_perm[j].to_constant ();
    4967           74 :               new_sel = (new_sel % nelts) + offs * nelts;
    4968           74 :               break;
    4969              :             }
    4970              :         }
    4971              : 
    4972              :       /* If the lane is not allocated, we need to do that now.  */
    4973          148 :       if (j == i)
    4974              :         {
    4975              :           unsigned int l_orig = lane;
    4976          182 :           while (lane_assignment[lane] != 0)
    4977              :             {
    4978          108 :               lane = (lane + 1) % nelts;
    4979              : 
    4980              :               /* This should not happen if both sequences utilize no more than
    4981              :                  half of the lanes.  Test anyway to guarantee termination.  */
    4982          108 :               if (lane == l_orig)
    4983            0 :                 return false;
    4984              :             }
    4985              : 
    4986              :           /* Allocate lane.  */
    4987           74 :           lane_assignment[lane] = 2 + l_orig;
    4988           74 :           new_sel = lane + offs * nelts;
    4989              :         }
    4990              : 
    4991          148 :       seq2_stmt_sel_perm.quick_push (new_sel);
    4992              :     }
    4993              : 
    4994              :   /* Check if the resulting permutation is cheap.  */
    4995           37 :   seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
    4996           37 :   tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
    4997           37 :   machine_mode vmode = TYPE_MODE (vectype);
    4998           37 :   if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
    4999              :     return false;
    5000              : 
    5001              :   /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt.  */
    5002           37 :   vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
    5003           37 :   vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
    5004          185 :   for (i = 0; i < nelts; i++)
    5005              :     {
    5006          148 :       bool use_seq1 = lane_assignment[i] < 2;
    5007          148 :       unsigned int l1, l2;
    5008              : 
    5009          148 :       if (use_seq1)
    5010              :         {
    5011              :           /* Just reuse the selector indices.  */
    5012           74 :           tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
    5013           74 :           tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
    5014           74 :           l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
    5015           74 :           l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
    5016              :         }
    5017              :       else
    5018              :         {
    5019              :           /* We moved the lanes for seq2, so we need to adjust for that.  */
    5020           74 :           tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
    5021           74 :           tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
    5022           74 :           l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, lane_assignment[i] - 2));
    5023           74 :           l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, lane_assignment[i] - 2));
    5024              :         }
    5025              : 
    5026          148 :       l1 %= nelts;
    5027          148 :       l2 %= nelts;
    5028          222 :       seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
    5029          148 :       seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
    5030              :     }
    5031              : 
    5032           37 :   seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
    5033           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
    5034           37 :   vmode = TYPE_MODE (vectype);
    5035           37 :   if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
    5036              :     return false;
    5037              : 
    5038           37 :   seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
    5039           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
    5040           37 :   vmode = TYPE_MODE (vectype);
    5041           37 :   if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
    5042              :     return false;
    5043              : 
    5044              :   return true;
    5045           74 : }
    5046              : 
    5047              : /* Blend the two given simplifiable vec permute sequences using the
    5048              :    given permutations.  */
    5049              : 
    5050              : static void
    5051           37 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
    5052              :                               vec_perm_simplify_seq seq2,
    5053              :                               const vec_perm_indices &seq2_stmt_indices,
    5054              :                               const vec_perm_indices &seq1_v_1_stmt_indices,
    5055              :                               const vec_perm_indices &seq1_v_2_stmt_indices)
    5056              : {
    5057              :   /* We don't need to adjust seq1->stmt because its lanes consumption
    5058              :      was already narrowed before entering this function.  */
    5059              : 
    5060              :   /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel.  */
    5061           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5062              :     {
    5063           11 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    5064           11 :       fprintf (dump_file, "Old stmt: ");
    5065           11 :       print_gimple_stmt (dump_file, seq2->stmt, 0);
    5066              :     }
    5067              : 
    5068           37 :   gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
    5069           74 :   gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
    5070           37 :   tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
    5071           37 :   tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
    5072           37 :   gimple_assign_set_rhs3 (seq2->stmt, sel);
    5073           37 :   update_stmt (seq2->stmt);
    5074              : 
    5075           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5076              :     {
    5077           11 :       fprintf (dump_file, "New stmt: ");
    5078           11 :       print_gimple_stmt (dump_file, seq2->stmt, 0);
    5079              :     }
    5080              : 
    5081              :   /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel.  */
    5082           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5083              :     {
    5084           11 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    5085           11 :       fprintf (dump_file, "Old stmt: ");
    5086           11 :       print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
    5087              :     }
    5088              : 
    5089           37 :   gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
    5090           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
    5091           37 :   sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
    5092           37 :   gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
    5093           37 :   update_stmt (seq1->v_1_stmt);
    5094              : 
    5095           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5096              :     {
    5097           11 :       fprintf (dump_file, "New stmt: ");
    5098           11 :       print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
    5099              :     }
    5100              : 
    5101              :   /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel.  */
    5102           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5103              :     {
    5104           11 :       fprintf (dump_file, "Updating VEC_PERM statement:\n");
    5105           11 :       fprintf (dump_file, "Old stmt: ");
    5106           11 :       print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
    5107              :     }
    5108              : 
    5109           37 :   gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
    5110           37 :   vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
    5111           37 :   sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
    5112           37 :   gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
    5113           37 :   update_stmt (seq1->v_2_stmt);
    5114              : 
    5115           37 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5116              :     {
    5117           11 :       fprintf (dump_file, "New stmt: ");
    5118           11 :       print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
    5119              :     }
    5120              : 
    5121              :   /* At this point, we have four unmodified seq2 stmts, which will be
    5122              :      eliminated by DCE.  */
    5123              : 
    5124           37 :   if (dump_file)
    5125           11 :     fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
    5126           37 : }
    5127              : 
    5128              : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
    5129              :    The provided list will be empty after this call.  */
    5130              : 
    5131              : static void
    5132    330805910 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
    5133              : {
    5134    330805910 :   unsigned int i, j;
    5135    330805910 :   vec_perm_simplify_seq seq1, seq2;
    5136              : 
    5137    330805910 :   if (l->is_empty ())
    5138    330805865 :     return;
    5139              : 
    5140           45 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5141           13 :     fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
    5142              :              l->length ());
    5143              : 
    5144          109 :   FOR_EACH_VEC_ELT (*l, i, seq1)
    5145              :     {
    5146           64 :       if (i + 1 < l->length ())
    5147              :         {
    5148           51 :           FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
    5149              :             {
    5150           47 :               bool swap = false;
    5151           47 :               if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
    5152              :                 {
    5153           37 :                   vec_perm_indices seq2_stmt_indices;
    5154           37 :                   vec_perm_indices seq1_v_1_stmt_indices;
    5155           37 :                   vec_perm_indices seq1_v_2_stmt_indices;
    5156          111 :                   if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
    5157              :                                                         swap ? seq1 : seq2,
    5158              :                                                         &seq2_stmt_indices,
    5159              :                                                         &seq1_v_1_stmt_indices,
    5160              :                                                         &seq1_v_2_stmt_indices))
    5161              :                     {
    5162              :                       /* Narrow lane usage.  */
    5163           37 :                       narrow_vec_perm_simplify_seq (seq1);
    5164           37 :                       narrow_vec_perm_simplify_seq (seq2);
    5165              : 
    5166              :                       /* Blend sequences.  */
    5167           37 :                       blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
    5168              :                                                     swap ? seq1 : seq2,
    5169              :                                                     seq2_stmt_indices,
    5170              :                                                     seq1_v_1_stmt_indices,
    5171              :                                                     seq1_v_2_stmt_indices);
    5172              : 
    5173              :                       /* We can use unordered_remove as we break the loop.  */
    5174           37 :                       l->unordered_remove (j);
    5175           37 :                       XDELETE (seq2);
    5176           37 :                       break;
    5177              :                     }
    5178           37 :                 }
    5179              :             }
    5180              :         }
    5181              : 
    5182              :       /* We don't need to call l->remove for seq1.  */
    5183           64 :       XDELETE (seq1);
    5184              :     }
    5185              : 
    5186           45 :   l->truncate (0);
    5187              : }
    5188              : 
    5189              : static void
    5190          101 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
    5191              :                                    const vec_perm_simplify_seq &seq)
    5192              : {
    5193              :   /* If no space on list left, then process the list.  */
    5194          101 :   if (!l->space (1))
    5195            0 :       process_vec_perm_simplify_seq_list (l);
    5196              : 
    5197          101 :   l->quick_push (seq);
    5198          101 : }
    5199              : 
    5200              : /* Main entry point for the forward propagation and statement combine
    5201              :    optimizer.  */
    5202              : 
    5203              : namespace {
    5204              : 
    5205              : const pass_data pass_data_forwprop =
    5206              : {
    5207              :   GIMPLE_PASS, /* type */
    5208              :   "forwprop", /* name */
    5209              :   OPTGROUP_NONE, /* optinfo_flags */
    5210              :   TV_TREE_FORWPROP, /* tv_id */
    5211              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    5212              :   0, /* properties_provided */
    5213              :   0, /* properties_destroyed */
    5214              :   0, /* todo_flags_start */
    5215              :   0, /* todo_flags_finish */
    5216              : };
    5217              : 
    5218              : class pass_forwprop : public gimple_opt_pass
    5219              : {
    5220              : public:
    5221      1461855 :   pass_forwprop (gcc::context *ctxt)
    5222      2923710 :     : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
    5223              :   {}
    5224              : 
    5225              :   /* opt_pass methods: */
    5226      1169484 :   opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
    5227      1754226 :   void set_pass_param (unsigned int n, bool param) final override
    5228              :     {
    5229      1754226 :       switch (n)
    5230              :         {
    5231      1169484 :           case 0:
    5232      1169484 :             m_full_walk = param;
    5233      1169484 :             break;
    5234       584742 :           case 1:
    5235       584742 :             last_p = param;
    5236       584742 :             break;
    5237            0 :           default:
    5238            0 :           gcc_unreachable();
    5239              :         }
    5240      1754226 :     }
    5241      5695907 :   bool gate (function *) final override { return flag_tree_forwprop; }
    5242              :   unsigned int execute (function *) final override;
    5243              : 
    5244              :  private:
    5245              :   /* Determines whether the pass instance should set PROP_last_full_fold.  */
    5246              :   bool last_p;
    5247              : 
    5248              :   /* True if the aggregate props are doing a full walk or not.  */
    5249              :   bool m_full_walk = false;
    5250              : }; // class pass_forwprop
    5251              : 
    5252              : /* Attempt to make the BB block of __builtin_unreachable unreachable by changing
    5253              :    the incoming jumps.  Return true if at least one jump was changed.  */
    5254              : 
    5255              : static bool
    5256         1131 : optimize_unreachable (basic_block bb)
    5257              : {
    5258         1131 :   gimple_stmt_iterator gsi;
    5259         1131 :   gimple *stmt;
    5260         1131 :   edge_iterator ei;
    5261         1131 :   edge e;
    5262         1131 :   bool ret;
    5263              : 
    5264         1131 :   ret = false;
    5265         2331 :   FOR_EACH_EDGE (e, ei, bb->preds)
    5266              :     {
    5267         1200 :       gsi = gsi_last_bb (e->src);
    5268         1200 :       if (gsi_end_p (gsi))
    5269          328 :         continue;
    5270              : 
    5271          872 :       stmt = gsi_stmt (gsi);
    5272          872 :       if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
    5273              :         {
    5274              :           /* If the condition is already true/false
    5275              :              ignore it. This can happen during copy prop of forwprop. */
    5276          686 :           if (gimple_cond_true_p (cond_stmt)
    5277          678 :               || gimple_cond_false_p (cond_stmt))
    5278            8 :             continue;
    5279          670 :           else if (e->flags & EDGE_TRUE_VALUE)
    5280          582 :             gimple_cond_make_false (cond_stmt);
    5281           88 :           else if (e->flags & EDGE_FALSE_VALUE)
    5282           88 :             gimple_cond_make_true (cond_stmt);
    5283              :           else
    5284            0 :             gcc_unreachable ();
    5285          670 :           update_stmt (cond_stmt);
    5286              :         }
    5287              :       else
    5288              :         {
    5289              :           /* Todo: handle other cases.  e.g. switch.  */
    5290          194 :           continue;
    5291              :         }
    5292              : 
    5293          670 :       ret = true;
    5294              :     }
    5295              : 
    5296         1131 :   return ret;
    5297              : }
    5298              : 
    5299              : unsigned int
    5300      5693299 : pass_forwprop::execute (function *fun)
    5301              : {
    5302      5693299 :   unsigned int todoflags = 0;
    5303              :   /* Handle a full walk only when expensive optimizations are on.  */
    5304      5693299 :   bool full_walk = m_full_walk && flag_expensive_optimizations;
    5305              : 
    5306      5693299 :   cfg_changed = false;
    5307      5693299 :   if (last_p)
    5308      1056998 :     fun->curr_properties |= PROP_last_full_fold;
    5309              : 
    5310      5693299 :   calculate_dominance_info (CDI_DOMINATORS);
    5311              : 
    5312              :   /* Combine stmts with the stmts defining their operands.  Do that
    5313              :      in an order that guarantees visiting SSA defs before SSA uses.  */
    5314     11386598 :   lattice.create (num_ssa_names);
    5315     11386598 :   lattice.quick_grow_cleared (num_ssa_names);
    5316      5693299 :   int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
    5317      5693299 :   int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
    5318              :                                                          postorder, false);
    5319      5693299 :   int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
    5320     51311617 :   for (int i = 0; i < postorder_num; ++i)
    5321              :     {
    5322     45618318 :       bb_to_rpo[postorder[i]] = i;
    5323     45618318 :       edge_iterator ei;
    5324     45618318 :       edge e;
    5325    109755600 :       FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
    5326     64137282 :         e->flags &= ~EDGE_EXECUTABLE;
    5327              :     }
    5328      5693299 :   single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
    5329      5693299 :     |= EDGE_EXECUTABLE;
    5330      5693299 :   auto_vec<gimple *, 4> to_fixup;
    5331      5693299 :   auto_vec<gimple *, 32> to_remove;
    5332      5693299 :   auto_vec<unsigned, 32> to_remove_defs;
    5333      5693299 :   auto_vec<std::pair<int, int>, 10> edges_to_remove;
    5334      5693299 :   auto_bitmap simple_dce_worklist;
    5335      5693299 :   auto_bitmap need_ab_cleanup;
    5336      5693299 :   to_purge = BITMAP_ALLOC (NULL);
    5337      5693299 :   auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
    5338     51311617 :   for (int i = 0; i < postorder_num; ++i)
    5339              :     {
    5340     45618318 :       gimple_stmt_iterator gsi;
    5341     45618318 :       basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
    5342     45618318 :       edge_iterator ei;
    5343     45618318 :       edge e;
    5344              : 
    5345              :       /* Skip processing not executable blocks.  We could improve
    5346              :          single_use tracking by at least unlinking uses from unreachable
    5347              :          blocks but since blocks with uses are not processed in a
    5348              :          meaningful order this is probably not worth it.  */
    5349     45618318 :       bool any = false;
    5350     46766058 :       FOR_EACH_EDGE (e, ei, bb->preds)
    5351              :         {
    5352     46753092 :           if ((e->flags & EDGE_EXECUTABLE)
    5353              :               /* We can handle backedges in natural loops correctly but
    5354              :                  for irreducible regions we have to take all backedges
    5355              :                  conservatively when we did not visit the source yet.  */
    5356     46753092 :               || (bb_to_rpo[e->src->index] > i
    5357       674007 :                   && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
    5358              :             {
    5359              :               any = true;
    5360              :               break;
    5361              :             }
    5362              :         }
    5363     45618318 :       if (!any)
    5364        13603 :         continue;
    5365              : 
    5366              :       /* Remove conditions that go directly to unreachable when this is the last forwprop.  */
    5367     45605352 :       if (last_p
    5368      9934221 :           && !(flag_sanitize & SANITIZE_UNREACHABLE))
    5369              :         {
    5370      9929246 :           gimple_stmt_iterator gsi;
    5371      9929246 :           gsi = gsi_start_nondebug_after_labels_bb (bb);
    5372      9929883 :           if (!gsi_end_p (gsi)
    5373      9077333 :               && gimple_call_builtin_p (*gsi, BUILT_IN_UNREACHABLE)
    5374      9930377 :               && optimize_unreachable (bb))
    5375              :             {
    5376          637 :               cfg_changed = true;
    5377          637 :               continue;
    5378              :             }
    5379              :         }
    5380              : 
    5381              :       /* Record degenerate PHIs in the lattice.  */
    5382     61620861 :       for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
    5383     16016146 :            gsi_next (&si))
    5384              :         {
    5385     16016146 :           gphi *phi = si.phi ();
    5386     16016146 :           tree res = gimple_phi_result (phi);
    5387     32032292 :           if (virtual_operand_p (res))
    5388      7359050 :             continue;
    5389              : 
    5390      8657096 :           tree first = NULL_TREE;
    5391      8657096 :           bool all_same = true;
    5392      8657096 :           edge_iterator ei;
    5393      8657096 :           edge e;
    5394     17812132 :           FOR_EACH_EDGE (e, ei, bb->preds)
    5395              :             {
    5396              :               /* Ignore not executable forward edges.  */
    5397     17599711 :               if (!(e->flags & EDGE_EXECUTABLE))
    5398              :                 {
    5399      4059499 :                   if (bb_to_rpo[e->src->index] < i)
    5400         5589 :                     continue;
    5401              :                   /* Avoid equivalences from backedges - while we might
    5402              :                      be able to make irreducible regions reducible and
    5403              :                      thus turning a back into a forward edge we do not
    5404              :                      want to deal with the intermediate SSA issues that
    5405              :                      exposes.  */
    5406              :                   all_same = false;
    5407              :                 }
    5408     17594122 :               tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
    5409     17594122 :               if (use == res)
    5410              :                 /* The PHI result can also appear on a backedge, if so
    5411              :                    we can ignore this case for the purpose of determining
    5412              :                    the singular value.  */
    5413              :                 ;
    5414     17581375 :               else if (! first)
    5415              :                 first = use;
    5416      8924279 :               else if (! operand_equal_p (first, use, 0))
    5417              :                 {
    5418              :                   all_same = false;
    5419              :                   break;
    5420              :                 }
    5421              :             }
    5422      8657096 :           if (all_same)
    5423              :             {
    5424       207819 :               if (may_propagate_copy (res, first))
    5425       207264 :                 to_remove_defs.safe_push (SSA_NAME_VERSION (res));
    5426       207819 :               fwprop_set_lattice_val (res, first);
    5427              :             }
    5428              :         }
    5429              : 
    5430              :       /* Apply forward propagation to all stmts in the basic-block.
    5431              :          Note we update GSI within the loop as necessary.  */
    5432     45604715 :       unsigned int uid = 1;
    5433    444224632 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
    5434              :         {
    5435    353015202 :           gimple *stmt = gsi_stmt (gsi);
    5436    353015202 :           tree lhs, rhs;
    5437    353015202 :           enum tree_code code;
    5438              : 
    5439    353015202 :           gimple_set_uid (stmt, uid++);
    5440              : 
    5441    353015202 :           if (!is_gimple_assign (stmt))
    5442              :             {
    5443    245900527 :               process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    5444    245900527 :               gsi_next (&gsi);
    5445    245900527 :               continue;
    5446              :             }
    5447              : 
    5448    107114675 :           lhs = gimple_assign_lhs (stmt);
    5449    107114675 :           rhs = gimple_assign_rhs1 (stmt);
    5450    107114675 :           code = gimple_assign_rhs_code (stmt);
    5451              : 
    5452    146415343 :           if (TREE_CODE (lhs) != SSA_NAME
    5453    107114675 :               || has_zero_uses (lhs))
    5454              :             {
    5455     39300668 :               process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    5456     39300668 :               gsi_next (&gsi);
    5457     39300668 :               continue;
    5458              :             }
    5459              : 
    5460              :           /* If this statement sets an SSA_NAME to an address,
    5461              :              try to propagate the address into the uses of the SSA_NAME.  */
    5462     67814007 :           if ((code == ADDR_EXPR
    5463              :                /* Handle pointer conversions on invariant addresses
    5464              :                   as well, as this is valid gimple.  */
    5465     65489243 :                || (CONVERT_EXPR_CODE_P (code)
    5466      9010003 :                    && TREE_CODE (rhs) == ADDR_EXPR
    5467       357130 :                    && POINTER_TYPE_P (TREE_TYPE (lhs))))
    5468     67814231 :               && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
    5469              :             {
    5470      2324389 :               tree base = get_base_address (TREE_OPERAND (rhs, 0));
    5471      2324389 :               if ((!base
    5472      2324389 :                    || !DECL_P (base)
    5473       134124 :                    || decl_address_invariant_p (base))
    5474      2324389 :                   && !stmt_references_abnormal_ssa_name (stmt)
    5475      4648762 :                   && forward_propagate_addr_expr (lhs, rhs, true))
    5476              :                 {
    5477       475635 :                   fwprop_invalidate_lattice (gimple_get_lhs (stmt));
    5478       475635 :                   release_defs (stmt);
    5479       475635 :                   gsi_remove (&gsi, true);
    5480              :                 }
    5481              :               else
    5482      1848754 :                 gsi_next (&gsi);
    5483              :             }
    5484     65489618 :           else if (code == POINTER_PLUS_EXPR)
    5485              :             {
    5486      3660843 :               tree off = gimple_assign_rhs2 (stmt);
    5487      3660843 :               if (TREE_CODE (off) == INTEGER_CST
    5488      1125808 :                   && can_propagate_from (stmt)
    5489      1125455 :                   && !simple_iv_increment_p (stmt)
    5490              :                   /* ???  Better adjust the interface to that function
    5491              :                      instead of building new trees here.  */
    5492      4490893 :                   && forward_propagate_addr_expr
    5493      2490150 :                        (lhs,
    5494              :                         build1_loc (gimple_location (stmt),
    5495       830050 :                                     ADDR_EXPR, TREE_TYPE (rhs),
    5496       830050 :                                     fold_build2 (MEM_REF,
    5497              :                                                  TREE_TYPE (TREE_TYPE (rhs)),
    5498              :                                                  rhs,
    5499              :                                                  fold_convert (ptr_type_node,
    5500              :                                                                off))), true))
    5501              :                 {
    5502       316833 :                   fwprop_invalidate_lattice (gimple_get_lhs (stmt));
    5503       316833 :                   release_defs (stmt);
    5504       316833 :                   gsi_remove (&gsi, true);
    5505              :                 }
    5506      3344010 :               else if (is_gimple_min_invariant (rhs))
    5507              :                 {
    5508              :                   /* Make sure to fold &a[0] + off_1 here.  */
    5509       416717 :                   fold_stmt_inplace (&gsi);
    5510       416717 :                   update_stmt (stmt);
    5511       416717 :                   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
    5512       416699 :                     gsi_next (&gsi);
    5513              :                 }
    5514              :               else
    5515      2927293 :                 gsi_next (&gsi);
    5516              :             }
    5517     61828775 :           else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
    5518       214034 :                    && gimple_assign_load_p (stmt)
    5519       134964 :                    && !gimple_has_volatile_ops (stmt)
    5520        40889 :                    && TREE_CODE (rhs) != TARGET_MEM_REF
    5521        40860 :                    && TREE_CODE (rhs) != BIT_FIELD_REF
    5522     61869631 :                    && !stmt_can_throw_internal (fun, stmt))
    5523              :             {
    5524              :               /* Rewrite loads used only in real/imagpart extractions to
    5525              :                  component-wise loads.  */
    5526        40731 :               use_operand_p use_p;
    5527        40731 :               imm_use_iterator iter;
    5528        40731 :               tree vuse = gimple_vuse (stmt);
    5529        40731 :               bool rewrite = true;
    5530        86645 :               FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
    5531              :                 {
    5532        43788 :                   gimple *use_stmt = USE_STMT (use_p);
    5533        43788 :                   if (is_gimple_debug (use_stmt))
    5534         1011 :                     continue;
    5535        42777 :                   if (!is_gimple_assign (use_stmt)
    5536        28092 :                       || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
    5537        25991 :                           && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
    5538        46949 :                       || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
    5539              :                     {
    5540              :                       rewrite = false;
    5541              :                       break;
    5542              :                     }
    5543        40731 :                 }
    5544        40731 :               if (rewrite)
    5545              :                 {
    5546         2126 :                   gimple *use_stmt;
    5547         8889 :                   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    5548              :                     {
    5549         4637 :                       if (is_gimple_debug (use_stmt))
    5550              :                         {
    5551          498 :                           if (gimple_debug_bind_p (use_stmt))
    5552              :                             {
    5553          498 :                               gimple_debug_bind_reset_value (use_stmt);
    5554          498 :                               update_stmt (use_stmt);
    5555              :                             }
    5556          498 :                           continue;
    5557              :                         }
    5558              : 
    5559         8278 :                       tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
    5560         4139 :                                              TREE_TYPE (TREE_TYPE (rhs)),
    5561              :                                              unshare_expr (rhs));
    5562         4139 :                       gimple *new_stmt
    5563         4139 :                         = gimple_build_assign (gimple_assign_lhs (use_stmt),
    5564              :                                                new_rhs);
    5565              : 
    5566         4139 :                       location_t loc = gimple_location (use_stmt);
    5567         4139 :                       gimple_set_location (new_stmt, loc);
    5568         4139 :                       gimple_set_vuse (new_stmt, vuse);
    5569         4139 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    5570         4139 :                       unlink_stmt_vdef (use_stmt);
    5571         4139 :                       gsi_remove (&gsi2, true);
    5572              : 
    5573         4139 :                       gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
    5574         2126 :                     }
    5575              : 
    5576         2126 :                   release_defs (stmt);
    5577         2126 :                   gsi_remove (&gsi, true);
    5578              :                 }
    5579              :               else
    5580        38605 :                 gsi_next (&gsi);
    5581              :             }
    5582     61788044 :           else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
    5583      1804584 :                    && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
    5584              :                        /* After vector lowering rewrite all loads, but
    5585              :                           initially do not since this conflicts with
    5586              :                           vector CONSTRUCTOR to shuffle optimization.  */
    5587      1780532 :                        || (fun->curr_properties & PROP_gimple_lvec))
    5588       925086 :                    && gimple_assign_load_p (stmt)
    5589       307597 :                    && !gimple_has_volatile_ops (stmt)
    5590       293728 :                    && !stmt_can_throw_internal (fun, stmt)
    5591     62081772 :                    && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
    5592       293226 :             optimize_vector_load (&gsi);
    5593              : 
    5594     61494818 :           else if (code == COMPLEX_EXPR)
    5595              :             {
    5596              :               /* Rewrite stores of a single-use complex build expression
    5597              :                  to component-wise stores.  */
    5598        37861 :               use_operand_p use_p;
    5599        37861 :               gimple *use_stmt, *def1, *def2;
    5600        37861 :               tree rhs2;
    5601        37861 :               if (single_imm_use (lhs, &use_p, &use_stmt)
    5602        35678 :                   && gimple_store_p (use_stmt)
    5603        42088 :                   && !gimple_has_volatile_ops (use_stmt)
    5604         3128 :                   && is_gimple_assign (use_stmt)
    5605         3124 :                   && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (use_stmt)))
    5606              :                       == COMPLEX_TYPE)
    5607        40980 :                   && (TREE_CODE (gimple_assign_lhs (use_stmt))
    5608              :                       != TARGET_MEM_REF))
    5609              :                 {
    5610         3115 :                   tree use_lhs = gimple_assign_lhs (use_stmt);
    5611         3115 :                   if (auto_var_p (use_lhs))
    5612          601 :                     DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
    5613         6230 :                   tree new_lhs = build1 (REALPART_EXPR,
    5614         3115 :                                          TREE_TYPE (TREE_TYPE (use_lhs)),
    5615              :                                          unshare_expr (use_lhs));
    5616         3115 :                   gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
    5617         3115 :                   location_t loc = gimple_location (use_stmt);
    5618         3115 :                   gimple_set_location (new_stmt, loc);
    5619         6230 :                   gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
    5620         3115 :                   gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
    5621         6230 :                   SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
    5622         6230 :                   gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
    5623         3115 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    5624         3115 :                   gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
    5625              : 
    5626         6230 :                   new_lhs = build1 (IMAGPART_EXPR,
    5627         3115 :                                     TREE_TYPE (TREE_TYPE (use_lhs)),
    5628              :                                     unshare_expr (use_lhs));
    5629         3115 :                   gimple_assign_set_lhs (use_stmt, new_lhs);
    5630         3115 :                   gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
    5631         3115 :                   update_stmt (use_stmt);
    5632              : 
    5633         3115 :                   release_defs (stmt);
    5634         3115 :                   gsi_remove (&gsi, true);
    5635              :                 }
    5636              :               /* Rewrite a component-wise load of a complex to a complex
    5637              :                  load if the components are not used separately.  */
    5638        34746 :               else if (TREE_CODE (rhs) == SSA_NAME
    5639        34305 :                        && has_single_use (rhs)
    5640        30769 :                        && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
    5641        30769 :                        && TREE_CODE (rhs2) == SSA_NAME
    5642        28958 :                        && has_single_use (rhs2)
    5643        28538 :                        && (def1 = SSA_NAME_DEF_STMT (rhs),
    5644        28538 :                            gimple_assign_load_p (def1))
    5645         1088 :                        && (def2 = SSA_NAME_DEF_STMT (rhs2),
    5646         1088 :                            gimple_assign_load_p (def2))
    5647         1588 :                        && (gimple_vuse (def1) == gimple_vuse (def2))
    5648          791 :                        && !gimple_has_volatile_ops (def1)
    5649          791 :                        && !gimple_has_volatile_ops (def2)
    5650          791 :                        && !stmt_can_throw_internal (fun, def1)
    5651          791 :                        && !stmt_can_throw_internal (fun, def2)
    5652          791 :                        && gimple_assign_rhs_code (def1) == REALPART_EXPR
    5653          545 :                        && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
    5654        35291 :                        && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
    5655              :                                                                  (def1), 0),
    5656          545 :                                            TREE_OPERAND (gimple_assign_rhs1
    5657              :                                                                  (def2), 0)))
    5658              :                 {
    5659          545 :                   tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
    5660          545 :                   gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
    5661          545 :                   gcc_assert (gsi_stmt (gsi) == stmt);
    5662         1090 :                   gimple_set_vuse (stmt, gimple_vuse (def1));
    5663          545 :                   gimple_set_modified (stmt, true);
    5664          545 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
    5665          545 :                   gsi_remove (&gsi, false);
    5666          545 :                   gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
    5667              :                 }
    5668              :               else
    5669        34201 :                 gsi_next (&gsi);
    5670              :             }
    5671     61456957 :           else if (code == CONSTRUCTOR
    5672       166586 :                    && VECTOR_TYPE_P (TREE_TYPE (rhs))
    5673       166586 :                    && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
    5674         4492 :                    && CONSTRUCTOR_NELTS (rhs) > 0
    5675     61461449 :                    && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
    5676         2049 :                        || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
    5677              :                            != BLKmode)))
    5678              :             {
    5679              :               /* Rewrite stores of a single-use vector constructors
    5680              :                  to component-wise stores if the mode isn't supported.  */
    5681         4135 :               use_operand_p use_p;
    5682         4135 :               gimple *use_stmt;
    5683         4135 :               if (single_imm_use (lhs, &use_p, &use_stmt)
    5684         3692 :                   && gimple_store_p (use_stmt)
    5685         3024 :                   && !gimple_has_volatile_ops (use_stmt)
    5686         1506 :                   && !stmt_can_throw_internal (fun, use_stmt)
    5687         5634 :                   && is_gimple_assign (use_stmt))
    5688              :                 {
    5689         1499 :                   tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
    5690         1499 :                   unsigned HOST_WIDE_INT elt_w
    5691         1499 :                     = tree_to_uhwi (TYPE_SIZE (elt_t));
    5692         1499 :                   unsigned HOST_WIDE_INT n
    5693         1499 :                     = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
    5694         1499 :                   tree use_lhs = gimple_assign_lhs (use_stmt);
    5695         1499 :                   if (auto_var_p (use_lhs))
    5696          567 :                     DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
    5697          932 :                   else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
    5698              :                     {
    5699            1 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    5700            1 :                       use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
    5701              :                     }
    5702        33388 :                   for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
    5703              :                     {
    5704        31889 :                       unsigned HOST_WIDE_INT ci = bi / elt_w;
    5705        31889 :                       tree new_rhs;
    5706        31889 :                       if (ci < CONSTRUCTOR_NELTS (rhs))
    5707        31271 :                         new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
    5708              :                       else
    5709          618 :                         new_rhs = build_zero_cst (elt_t);
    5710        31889 :                       tree new_lhs = build3 (BIT_FIELD_REF,
    5711              :                                              elt_t,
    5712              :                                              unshare_expr (use_lhs),
    5713        31889 :                                              bitsize_int (elt_w),
    5714        31889 :                                              bitsize_int (bi));
    5715        31889 :                       gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
    5716        31889 :                       location_t loc = gimple_location (use_stmt);
    5717        31889 :                       gimple_set_location (new_stmt, loc);
    5718        63778 :                       gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
    5719        31889 :                       gimple_set_vdef (new_stmt,
    5720              :                                        make_ssa_name (gimple_vop (fun)));
    5721        63778 :                       SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
    5722        63778 :                       gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
    5723        31889 :                       gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    5724        31889 :                       gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
    5725              :                     }
    5726         1499 :                   gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
    5727         1499 :                   unlink_stmt_vdef (use_stmt);
    5728         1499 :                   release_defs (use_stmt);
    5729         1499 :                   gsi_remove (&gsi2, true);
    5730         1499 :                   release_defs (stmt);
    5731         1499 :                   gsi_remove (&gsi, true);
    5732              :                 }
    5733              :               else
    5734         2636 :                 gsi_next (&gsi);
    5735              :             }
    5736     61452822 :           else if (code == VEC_PERM_EXPR)
    5737              :             {
    5738              :               /* Find vectorized sequences, where we can reduce the lane
    5739              :                  utilization.  The narrowing will be donw later and only
    5740              :                  if we find a pair of sequences that can be blended.  */
    5741       188381 :               gassign *assign = dyn_cast <gassign *> (stmt);
    5742       188381 :               vec_perm_simplify_seq seq;
    5743       188381 :               if (recognise_vec_perm_simplify_seq (assign, &seq))
    5744          101 :                 append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
    5745              :                                                    seq);
    5746              : 
    5747       188381 :               gsi_next (&gsi);
    5748              :           }
    5749              :           else
    5750     61264441 :             gsi_next (&gsi);
    5751              :         }
    5752              : 
    5753     45604715 :       process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
    5754              : 
    5755              :       /* Combine stmts with the stmts defining their operands.
    5756              :          Note we update GSI within the loop as necessary.  */
    5757    443864940 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    5758              :         {
    5759    352655510 :           gimple *stmt = gsi_stmt (gsi);
    5760              : 
    5761              :           /* Mark stmt as potentially needing revisiting.  */
    5762    352655510 :           gimple_set_plf (stmt, GF_PLF_1, false);
    5763              : 
    5764    352655510 :           bool can_make_abnormal_goto = (is_gimple_call (stmt)
    5765    352655510 :                                          && stmt_can_make_abnormal_goto (stmt));
    5766              : 
    5767              :           /* Substitute from our lattice.  We need to do so only once.  */
    5768    352655510 :           bool substituted_p = false;
    5769    352655510 :           use_operand_p usep;
    5770    352655510 :           ssa_op_iter iter;
    5771    517911638 :           FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
    5772              :             {
    5773    165256128 :               tree use = USE_FROM_PTR (usep);
    5774    165256128 :               tree val = fwprop_ssa_val (use);
    5775    165256128 :               if (val && val != use)
    5776              :                 {
    5777      1849228 :                   if (!is_gimple_debug (stmt))
    5778      1542182 :                     bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
    5779      1849228 :                   if (may_propagate_copy (use, val))
    5780              :                     {
    5781      1846000 :                       propagate_value (usep, val);
    5782      1846000 :                       substituted_p = true;
    5783              :                     }
    5784              :                 }
    5785              :             }
    5786    352655510 :           if (substituted_p)
    5787      1791734 :             update_stmt (stmt);
    5788      1791734 :           if (substituted_p
    5789      1791734 :               && is_gimple_assign (stmt)
    5790      1076470 :               && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
    5791        20012 :             recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
    5792    352655510 :           if (substituted_p
    5793    352655510 :               && can_make_abnormal_goto
    5794    352655510 :               && !stmt_can_make_abnormal_goto (stmt))
    5795            3 :             bitmap_set_bit (need_ab_cleanup, bb->index);
    5796              : 
    5797    355482704 :           bool changed;
    5798    710965408 :           do
    5799              :             {
    5800    355482704 :               gimple *orig_stmt = stmt = gsi_stmt (gsi);
    5801    355482704 :               bool was_call = is_gimple_call (stmt);
    5802    355482704 :               bool was_noreturn = (was_call
    5803    355482704 :                                    && gimple_call_noreturn_p (stmt));
    5804    355482704 :               changed = false;
    5805              : 
    5806    355482704 :               auto_vec<tree, 8> uses;
    5807    523752888 :               FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
    5808    168270184 :                 if (uses.space (1))
    5809    167879317 :                   uses.quick_push (USE_FROM_PTR (usep));
    5810              : 
    5811    355482704 :               if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
    5812              :                 {
    5813      2506212 :                   changed = true;
    5814              :                   /* There is no updating of the address
    5815              :                      taken after the last forwprop so update
    5816              :                      the addresses when a folding happened to a call.
    5817              :                      The va_* builtins can remove taking of the address so
    5818              :                      can the sincos->cexpi transformation.  See PR 39643 and PR 20983. */
    5819      2506212 :                   if (was_call && last_p)
    5820      2506212 :                     todoflags |= TODO_update_address_taken;
    5821      2506212 :                   stmt = gsi_stmt (gsi);
    5822              :                   /* Cleanup the CFG if we simplified a condition to
    5823              :                      true or false.  */
    5824      2506212 :                   if (gcond *cond = dyn_cast <gcond *> (stmt))
    5825      1001117 :                     if (gimple_cond_true_p (cond)
    5826      1001117 :                         || gimple_cond_false_p (cond))
    5827        13956 :                       cfg_changed = true;
    5828              :                   /* Queue old uses for simple DCE if not debug statement.  */
    5829      2506212 :                   if (!is_gimple_debug (stmt))
    5830     10599242 :                     for (tree use : uses)
    5831      3101984 :                       if (TREE_CODE (use) == SSA_NAME
    5832      3101984 :                           && !SSA_NAME_IS_DEFAULT_DEF (use))
    5833      2904271 :                         bitmap_set_bit (simple_dce_worklist,
    5834      2904271 :                                         SSA_NAME_VERSION (use));
    5835      2506212 :                   update_stmt (stmt);
    5836              :                 }
    5837              : 
    5838    355482704 :               switch (gimple_code (stmt))
    5839              :                 {
    5840    108094092 :                 case GIMPLE_ASSIGN:
    5841    108094092 :                   {
    5842    108094092 :                     tree rhs1 = gimple_assign_rhs1 (stmt);
    5843    108094092 :                     enum tree_code code = gimple_assign_rhs_code (stmt);
    5844    108094092 :                     if (gimple_clobber_p (stmt))
    5845      7400238 :                       do_simple_agr_dse (as_a<gassign*>(stmt), full_walk);
    5846    100693854 :                     else if (gimple_store_p (stmt))
    5847              :                       {
    5848     31277562 :                         optimize_aggr_zeroprop (stmt, full_walk);
    5849     31277562 :                         if (gimple_assign_load_p (stmt))
    5850      3886465 :                           optimize_agr_copyprop (stmt);
    5851              :                       }
    5852     69416292 :                     else if (TREE_CODE_CLASS (code) == tcc_comparison)
    5853      2662715 :                       changed |= forward_propagate_into_comparison (&gsi);
    5854     66753577 :                     else if ((code == PLUS_EXPR
    5855     66753577 :                               || code == BIT_IOR_EXPR
    5856     56579243 :                               || code == BIT_XOR_EXPR)
    5857     66891463 :                              && simplify_rotate (&gsi))
    5858              :                       changed = true;
    5859     66750895 :                     else if (code == VEC_PERM_EXPR)
    5860       190530 :                       changed |= simplify_permutation (&gsi);
    5861     66560365 :                     else if (code == CONSTRUCTOR
    5862     66560365 :                              && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
    5863       164380 :                       changed |= simplify_vector_constructor (&gsi);
    5864     66395985 :                     else if (code == ARRAY_REF)
    5865      1996254 :                       changed |= simplify_count_zeroes (&gsi);
    5866              :                     break;
    5867              :                   }
    5868              : 
    5869       107050 :                 case GIMPLE_SWITCH:
    5870       107050 :                   changed |= simplify_gimple_switch (as_a <gswitch *> (stmt),
    5871              :                                                      edges_to_remove,
    5872              :                                                      simple_dce_worklist);
    5873       107050 :                   break;
    5874              : 
    5875     19670771 :                 case GIMPLE_COND:
    5876     19670771 :                   {
    5877     19670771 :                     int did_something = forward_propagate_into_gimple_cond
    5878     19670771 :                                                         (as_a <gcond *> (stmt));
    5879     19670771 :                     if (did_something == 2)
    5880         1653 :                       cfg_changed = true;
    5881     19670771 :                     changed |= did_something != 0;
    5882     19670771 :                     break;
    5883              :                   }
    5884              : 
    5885     23765488 :                 case GIMPLE_CALL:
    5886     23765488 :                   {
    5887     23765488 :                     tree callee = gimple_call_fndecl (stmt);
    5888     23765488 :                     if (callee != NULL_TREE
    5889     23765488 :                         && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
    5890      6293190 :                       changed |= simplify_builtin_call (&gsi, callee, full_walk);
    5891              :                     break;
    5892              :                   }
    5893              : 
    5894    355480022 :                 default:;
    5895              :                 }
    5896              : 
    5897    355480022 :               if (changed || substituted_p)
    5898              :                 {
    5899      4097687 :                   substituted_p = false;
    5900      4097687 :                   stmt = gsi_stmt (gsi);
    5901      4097687 :                   if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
    5902           70 :                     bitmap_set_bit (to_purge, bb->index);
    5903      4097687 :                   if (!was_noreturn
    5904      4097687 :                       && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
    5905           12 :                     to_fixup.safe_push (stmt);
    5906              :                 }
    5907      4097687 :               if (changed)
    5908              :                 {
    5909              :                   /* If the stmt changed then re-visit it and the statements
    5910              :                      inserted before it.  */
    5911      8845892 :                   for (; !gsi_end_p (gsi); gsi_prev (&gsi))
    5912      5615289 :                     if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
    5913              :                       break;
    5914      2827194 :                   if (gsi_end_p (gsi))
    5915       442508 :                     gsi = gsi_start_bb (bb);
    5916              :                   else
    5917      2605940 :                     gsi_next (&gsi);
    5918              :                 }
    5919    355482704 :             }
    5920              :           while (changed);
    5921              : 
    5922              :           /* Stmt no longer needs to be revisited.  */
    5923    352655510 :           stmt = gsi_stmt (gsi);
    5924    352655510 :           gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
    5925    352655510 :           gimple_set_plf (stmt, GF_PLF_1, true);
    5926              : 
    5927              :           /* Fill up the lattice.  */
    5928    352655510 :           if (gimple_assign_single_p (stmt))
    5929              :             {
    5930     71590327 :               tree lhs = gimple_assign_lhs (stmt);
    5931     71590327 :               tree rhs = gimple_assign_rhs1 (stmt);
    5932     71590327 :               if (TREE_CODE (lhs) == SSA_NAME)
    5933              :                 {
    5934     32924352 :                   tree val = lhs;
    5935     32924352 :                   if (TREE_CODE (rhs) == SSA_NAME)
    5936       787453 :                     val = fwprop_ssa_val (rhs);
    5937     32136899 :                   else if (is_gimple_min_invariant (rhs))
    5938       428087 :                     val = rhs;
    5939              :                   /* If we can propagate the lattice-value mark the
    5940              :                      stmt for removal.  */
    5941     32924352 :                   if (val != lhs
    5942     32924352 :                       && may_propagate_copy (lhs, val))
    5943      1212110 :                     to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
    5944     32924352 :                   fwprop_set_lattice_val (lhs, val);
    5945              :                 }
    5946              :             }
    5947    281065183 :           else if (gimple_nop_p (stmt))
    5948        89584 :             to_remove.safe_push (stmt);
    5949              :         }
    5950              : 
    5951              :       /* Substitute in destination PHI arguments.  */
    5952    109730910 :       FOR_EACH_EDGE (e, ei, bb->succs)
    5953     64126195 :         for (gphi_iterator gsi = gsi_start_phis (e->dest);
    5954    106467047 :              !gsi_end_p (gsi); gsi_next (&gsi))
    5955              :           {
    5956     42340852 :             gphi *phi = gsi.phi ();
    5957     42340852 :             use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
    5958     42340852 :             tree arg = USE_FROM_PTR (use_p);
    5959     69793774 :             if (TREE_CODE (arg) != SSA_NAME
    5960     42340852 :                 || virtual_operand_p (arg))
    5961     27452922 :               continue;
    5962     14887930 :             tree val = fwprop_ssa_val (arg);
    5963     14887930 :             if (val != arg
    5964     14887930 :                 && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
    5965       244029 :               propagate_value (use_p, val);
    5966              :           }
    5967              : 
    5968              :       /* Mark outgoing executable edges.  */
    5969     45604715 :       if (edge e = find_taken_edge (bb, NULL))
    5970              :         {
    5971     19364628 :           e->flags |= EDGE_EXECUTABLE;
    5972     45624653 :           if (EDGE_COUNT (bb->succs) > 1)
    5973        19938 :             cfg_changed = true;
    5974              :         }
    5975              :       else
    5976              :         {
    5977     70981715 :           FOR_EACH_EDGE (e, ei, bb->succs)
    5978     44741628 :             e->flags |= EDGE_EXECUTABLE;
    5979              :         }
    5980              :     }
    5981      5693299 :   free (postorder);
    5982      5693299 :   free (bb_to_rpo);
    5983      5693299 :   lattice.release ();
    5984              : 
    5985              :   /* First remove chains of stmts where we check no uses remain.  */
    5986      5693299 :   simple_dce_from_worklist (simple_dce_worklist, to_purge);
    5987              : 
    5988      6036201 :   auto remove = [](gimple *stmt)
    5989              :     {
    5990       342902 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5991              :         {
    5992            1 :           fprintf (dump_file, "Removing dead stmt ");
    5993            1 :           print_gimple_stmt (dump_file, stmt, 0);
    5994            1 :           fprintf (dump_file, "\n");
    5995              :         }
    5996       342902 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    5997       342902 :       if (gimple_code (stmt) == GIMPLE_PHI)
    5998        92699 :         remove_phi_node (&gsi, true);
    5999              :       else
    6000              :         {
    6001       250203 :           unlink_stmt_vdef (stmt);
    6002       250203 :           gsi_remove (&gsi, true);
    6003       250203 :           release_defs (stmt);
    6004              :         }
    6005       342902 :     };
    6006              : 
    6007              :   /* Then remove stmts we know we can remove even though we did not
    6008              :      substitute in dead code regions, so uses can remain.  Do so in reverse
    6009              :      order to make debug stmt creation possible.  */
    6010     12805972 :   while (!to_remove_defs.is_empty())
    6011              :     {
    6012      1419374 :       tree def = ssa_name (to_remove_defs.pop ());
    6013              :       /* For example remove_prop_source_from_use can remove stmts queued
    6014              :          for removal.  Deal with this gracefully.  */
    6015      1419374 :       if (!def)
    6016      1166056 :         continue;
    6017       253318 :       gimple *stmt = SSA_NAME_DEF_STMT (def);
    6018       253318 :       remove (stmt);
    6019              :     }
    6020              : 
    6021              :   /* Wipe other queued stmts that do not have SSA defs.  */
    6022      5782883 :   while (!to_remove.is_empty())
    6023              :     {
    6024        89584 :       gimple *stmt = to_remove.pop ();
    6025        89584 :       remove (stmt);
    6026              :     }
    6027              : 
    6028              :   /* Fixup stmts that became noreturn calls.  This may require splitting
    6029              :      blocks and thus isn't possible during the walk.  Do this
    6030              :      in reverse order so we don't inadvertently remove a stmt we want to
    6031              :      fixup by visiting a dominating now noreturn call first.  */
    6032      5693311 :   while (!to_fixup.is_empty ())
    6033              :     {
    6034           12 :       gimple *stmt = to_fixup.pop ();
    6035           12 :       if (dump_file && dump_flags & TDF_DETAILS)
    6036              :         {
    6037            0 :           fprintf (dump_file, "Fixing up noreturn call ");
    6038            0 :           print_gimple_stmt (dump_file, stmt, 0);
    6039            0 :           fprintf (dump_file, "\n");
    6040              :         }
    6041           12 :       cfg_changed |= fixup_noreturn_call (stmt);
    6042              :     }
    6043              : 
    6044      5693299 :   cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
    6045      5693299 :   cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
    6046      5693299 :   BITMAP_FREE (to_purge);
    6047              : 
    6048              :   /* Remove edges queued from switch stmt simplification.  */
    6049     17079897 :   for (auto ep : edges_to_remove)
    6050              :     {
    6051            0 :       basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
    6052            0 :       basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
    6053            0 :       edge e;
    6054            0 :       if (src && dest && (e = find_edge (src, dest)))
    6055              :         {
    6056            0 :           free_dominance_info (CDI_DOMINATORS);
    6057            0 :           remove_edge (e);
    6058            0 :           cfg_changed = true;
    6059              :         }
    6060              :     }
    6061              : 
    6062     11385055 :   if (get_range_query (fun) != get_global_range_query ())
    6063         1543 :     disable_ranger (fun);
    6064              : 
    6065      5693299 :   if (cfg_changed)
    6066         9409 :     todoflags |= TODO_cleanup_cfg;
    6067              : 
    6068      5693299 :   return todoflags;
    6069      5693299 : }
    6070              : 
    6071              : } // anon namespace
    6072              : 
    6073              : gimple_opt_pass *
    6074       292371 : make_pass_forwprop (gcc::context *ctxt)
    6075              : {
    6076       292371 :   return new pass_forwprop (ctxt);
    6077              : }
        

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.