LCOV - code coverage report
Current view: top level - gcc - gimple-fold.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.9 % 5390 4955
Test Date: 2025-05-10 15:51:31 Functions: 94.6 % 147 139
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Statement simplification on GIMPLE.
       2                 :             :    Copyright (C) 2010-2025 Free Software Foundation, Inc.
       3                 :             :    Split out from tree-ssa-ccp.cc.
       4                 :             : 
       5                 :             : This file is part of GCC.
       6                 :             : 
       7                 :             : GCC is free software; you can redistribute it and/or modify it
       8                 :             : under the terms of the GNU General Public License as published by the
       9                 :             : Free Software Foundation; either version 3, or (at your option) any
      10                 :             : later version.
      11                 :             : 
      12                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT
      13                 :             : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15                 :             : for more details.
      16                 :             : 
      17                 :             : You should have received a copy of the GNU General Public License
      18                 :             : along with GCC; see the file COPYING3.  If not see
      19                 :             : <http://www.gnu.org/licenses/>.  */
      20                 :             : 
      21                 :             : #include "config.h"
      22                 :             : #include "system.h"
      23                 :             : #include "coretypes.h"
      24                 :             : #include "backend.h"
      25                 :             : #include "target.h"
      26                 :             : #include "rtl.h"
      27                 :             : #include "tree.h"
      28                 :             : #include "gimple.h"
      29                 :             : #include "predict.h"
      30                 :             : #include "ssa.h"
      31                 :             : #include "cgraph.h"
      32                 :             : #include "gimple-pretty-print.h"
      33                 :             : #include "gimple-ssa-warn-access.h"
      34                 :             : #include "gimple-ssa-warn-restrict.h"
      35                 :             : #include "fold-const.h"
      36                 :             : #include "stmt.h"
      37                 :             : #include "expr.h"
      38                 :             : #include "stor-layout.h"
      39                 :             : #include "dumpfile.h"
      40                 :             : #include "gimple-iterator.h"
      41                 :             : #include "gimple-fold.h"
      42                 :             : #include "gimplify.h"
      43                 :             : #include "tree-into-ssa.h"
      44                 :             : #include "tree-dfa.h"
      45                 :             : #include "tree-object-size.h"
      46                 :             : #include "tree-ssa.h"
      47                 :             : #include "tree-ssa-propagate.h"
      48                 :             : #include "ipa-utils.h"
      49                 :             : #include "tree-ssa-address.h"
      50                 :             : #include "langhooks.h"
      51                 :             : #include "gimplify-me.h"
      52                 :             : #include "dbgcnt.h"
      53                 :             : #include "builtins.h"
      54                 :             : #include "tree-eh.h"
      55                 :             : #include "gimple-match.h"
      56                 :             : #include "gomp-constants.h"
      57                 :             : #include "optabs-query.h"
      58                 :             : #include "omp-general.h"
      59                 :             : #include "tree-cfg.h"
      60                 :             : #include "fold-const-call.h"
      61                 :             : #include "stringpool.h"
      62                 :             : #include "attribs.h"
      63                 :             : #include "asan.h"
      64                 :             : #include "diagnostic-core.h"
      65                 :             : #include "intl.h"
      66                 :             : #include "calls.h"
      67                 :             : #include "tree-vector-builder.h"
      68                 :             : #include "tree-ssa-strlen.h"
      69                 :             : #include "varasm.h"
      70                 :             : #include "internal-fn.h"
      71                 :             : #include "gimple-range.h"
      72                 :             : 
      73                 :             : enum strlen_range_kind {
      74                 :             :   /* Compute the exact constant string length.  */
      75                 :             :   SRK_STRLEN,
      76                 :             :   /* Compute the maximum constant string length.  */
      77                 :             :   SRK_STRLENMAX,
      78                 :             :   /* Compute a range of string lengths bounded by object sizes.  When
      79                 :             :      the length of a string cannot be determined, consider as the upper
      80                 :             :      bound the size of the enclosing object the string may be a member
      81                 :             :      or element of.  Also determine the size of the largest character
      82                 :             :      array the string may refer to.  */
      83                 :             :   SRK_LENRANGE,
      84                 :             :   /* Determine the integer value of the argument (not string length).  */
      85                 :             :   SRK_INT_VALUE
      86                 :             : };
      87                 :             : 
      88                 :             : static bool
      89                 :             : get_range_strlen (tree, bitmap, strlen_range_kind, c_strlen_data *, unsigned);
      90                 :             : 
      91                 :             : /* Return true when DECL can be referenced from current unit.
      92                 :             :    FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
      93                 :             :    We can get declarations that are not possible to reference for various
      94                 :             :    reasons:
      95                 :             : 
      96                 :             :      1) When analyzing C++ virtual tables.
      97                 :             :         C++ virtual tables do have known constructors even
      98                 :             :         when they are keyed to other compilation unit.
      99                 :             :         Those tables can contain pointers to methods and vars
     100                 :             :         in other units.  Those methods have both STATIC and EXTERNAL
     101                 :             :         set.
     102                 :             :      2) In WHOPR mode devirtualization might lead to reference
     103                 :             :         to method that was partitioned elsehwere.
     104                 :             :         In this case we have static VAR_DECL or FUNCTION_DECL
     105                 :             :         that has no corresponding callgraph/varpool node
     106                 :             :         declaring the body.
     107                 :             :      3) COMDAT functions referred by external vtables that
     108                 :             :         we devirtualize only during final compilation stage.
     109                 :             :         At this time we already decided that we will not output
     110                 :             :         the function body and thus we can't reference the symbol
     111                 :             :         directly.  */
     112                 :             : 
     113                 :             : static bool
     114                 :     4529677 : can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
     115                 :             : {
     116                 :     4529677 :   varpool_node *vnode;
     117                 :     4529677 :   struct cgraph_node *node;
     118                 :     4529677 :   symtab_node *snode;
     119                 :             : 
     120                 :     4529677 :   if (DECL_ABSTRACT_P (decl))
     121                 :             :     return false;
     122                 :             : 
     123                 :             :   /* We are concerned only about static/external vars and functions.  */
     124                 :     1508795 :   if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
     125                 :     5634934 :       || !VAR_OR_FUNCTION_DECL_P (decl))
     126                 :             :     return true;
     127                 :             : 
     128                 :             :   /* Static objects can be referred only if they are defined and not optimized
     129                 :             :      out yet.  */
     130                 :     4126139 :   if (!TREE_PUBLIC (decl))
     131                 :             :     {
     132                 :     1089915 :       if (DECL_EXTERNAL (decl))
     133                 :             :         return false;
     134                 :             :       /* Before we start optimizing unreachable code we can be sure all
     135                 :             :          static objects are defined.  */
     136                 :     1089870 :       if (symtab->function_flags_ready)
     137                 :             :         return true;
     138                 :     1054828 :       snode = symtab_node::get (decl);
     139                 :     1054828 :       if (!snode || !snode->definition)
     140                 :             :         return false;
     141                 :     1054787 :       node = dyn_cast <cgraph_node *> (snode);
     142                 :     1063546 :       return !node || !node->inlined_to;
     143                 :             :     }
     144                 :             : 
     145                 :             :   /* We will later output the initializer, so we can refer to it.
     146                 :             :      So we are concerned only when DECL comes from initializer of
     147                 :             :      external var or var that has been optimized out.  */
     148                 :     3036224 :   if (!from_decl
     149                 :      592099 :       || !VAR_P (from_decl)
     150                 :      590972 :       || (!DECL_EXTERNAL (from_decl)
     151                 :      283288 :           && (vnode = varpool_node::get (from_decl)) != NULL
     152                 :      176875 :           && vnode->definition)
     153                 :     3450360 :       || (flag_ltrans
     154                 :           2 :           && (vnode = varpool_node::get (from_decl)) != NULL
     155                 :           2 :           && vnode->in_other_partition))
     156                 :             :     return true;
     157                 :             :   /* We are folding reference from external vtable.  The vtable may reffer
     158                 :             :      to a symbol keyed to other compilation unit.  The other compilation
     159                 :             :      unit may be in separate DSO and the symbol may be hidden.  */
     160                 :      414134 :   if (DECL_VISIBILITY_SPECIFIED (decl)
     161                 :      407124 :       && DECL_EXTERNAL (decl)
     162                 :      303178 :       && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
     163                 :      619071 :       && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition))
     164                 :             :     return false;
     165                 :             :   /* When function is public, we always can introduce new reference.
     166                 :             :      Exception are the COMDAT functions where introducing a direct
     167                 :             :      reference imply need to include function body in the curren tunit.  */
     168                 :      209197 :   if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
     169                 :             :     return true;
     170                 :             :   /* We have COMDAT.  We are going to check if we still have definition
     171                 :             :      or if the definition is going to be output in other partition.
     172                 :             :      Bypass this when gimplifying; all needed functions will be produced.
     173                 :             : 
     174                 :             :      As observed in PR20991 for already optimized out comdat virtual functions
     175                 :             :      it may be tempting to not necessarily give up because the copy will be
     176                 :             :      output elsewhere when corresponding vtable is output.
     177                 :             :      This is however not possible - ABI specify that COMDATs are output in
     178                 :             :      units where they are used and when the other unit was compiled with LTO
     179                 :             :      it is possible that vtable was kept public while the function itself
     180                 :             :      was privatized. */
     181                 :      158109 :   if (!symtab->function_flags_ready)
     182                 :             :     return true;
     183                 :             : 
     184                 :      143807 :   snode = symtab_node::get (decl);
     185                 :      143807 :   if (!snode
     186                 :      143807 :       || ((!snode->definition || DECL_EXTERNAL (decl))
     187                 :       17517 :           && (!snode->in_other_partition
     188                 :           0 :               || (!snode->forced_by_abi && !snode->force_output))))
     189                 :             :     return false;
     190                 :       90506 :   node = dyn_cast <cgraph_node *> (snode);
     191                 :       90506 :   return !node || !node->inlined_to;
     192                 :             : }
     193                 :             : 
     194                 :             : /* Create a temporary for TYPE for a statement STMT.  If the current function
     195                 :             :    is in SSA form, a SSA name is created.  Otherwise a temporary register
     196                 :             :    is made.  */
     197                 :             : 
     198                 :             : tree
     199                 :      957335 : create_tmp_reg_or_ssa_name (tree type, gimple *stmt)
     200                 :             : {
     201                 :      957335 :   if (gimple_in_ssa_p (cfun))
     202                 :      941911 :     return make_ssa_name (type, stmt);
     203                 :             :   else
     204                 :       15424 :     return create_tmp_reg (type);
     205                 :             : }
     206                 :             : 
     207                 :             : /* CVAL is value taken from DECL_INITIAL of variable.  Try to transform it into
     208                 :             :    acceptable form for is_gimple_min_invariant.
     209                 :             :    FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL.  */
     210                 :             : 
     211                 :             : tree
     212                 :    15492880 : canonicalize_constructor_val (tree cval, tree from_decl)
     213                 :             : {
     214                 :    15492880 :   if (CONSTANT_CLASS_P (cval))
     215                 :             :     return cval;
     216                 :             : 
     217                 :     9332893 :   tree orig_cval = cval;
     218                 :     9332893 :   STRIP_NOPS (cval);
     219                 :     9332893 :   if (TREE_CODE (cval) == POINTER_PLUS_EXPR
     220                 :     9332893 :       && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
     221                 :             :     {
     222                 :       68695 :       tree ptr = TREE_OPERAND (cval, 0);
     223                 :       68695 :       if (is_gimple_min_invariant (ptr))
     224                 :      205731 :         cval = build1_loc (EXPR_LOCATION (cval),
     225                 :       68577 :                            ADDR_EXPR, TREE_TYPE (ptr),
     226                 :      137154 :                            fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
     227                 :             :                                         ptr,
     228                 :             :                                         fold_convert (ptr_type_node,
     229                 :             :                                                       TREE_OPERAND (cval, 1))));
     230                 :             :     }
     231                 :     9332893 :   if (TREE_CODE (cval) == ADDR_EXPR)
     232                 :             :     {
     233                 :     5135405 :       tree base = NULL_TREE;
     234                 :     5135405 :       if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
     235                 :             :         {
     236                 :         189 :           base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
     237                 :         189 :           if (base)
     238                 :         189 :             TREE_OPERAND (cval, 0) = base;
     239                 :             :         }
     240                 :             :       else
     241                 :     5135216 :         base = get_base_address (TREE_OPERAND (cval, 0));
     242                 :     5135405 :       if (!base)
     243                 :           0 :         return NULL_TREE;
     244                 :             : 
     245                 :     2261243 :       if (VAR_OR_FUNCTION_DECL_P (base)
     246                 :     6428767 :           && !can_refer_decl_in_current_unit_p (base, from_decl))
     247                 :             :         return NULL_TREE;
     248                 :     4929563 :       if (TREE_TYPE (base) == error_mark_node)
     249                 :             :         return NULL_TREE;
     250                 :     4929563 :       if (VAR_P (base))
     251                 :             :         /* ???  We should be able to assert that TREE_ADDRESSABLE is set,
     252                 :             :            but since the use can be in a debug stmt we can't.  */
     253                 :             :         ;
     254                 :     2260240 :       else if (TREE_CODE (base) == FUNCTION_DECL)
     255                 :             :         {
     256                 :             :           /* Make sure we create a cgraph node for functions we'll reference.
     257                 :             :              They can be non-existent if the reference comes from an entry
     258                 :             :              of an external vtable for example.  */
     259                 :     1292359 :           cgraph_node::get_create (base);
     260                 :             :         }
     261                 :             :       /* Fixup types in global initializers.  */
     262                 :     4929563 :       if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
     263                 :       37537 :         cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
     264                 :             : 
     265                 :     4929563 :       if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
     266                 :      211318 :         cval = fold_convert (TREE_TYPE (orig_cval), cval);
     267                 :     4929563 :       return cval;
     268                 :             :     }
     269                 :             :   /* In CONSTRUCTORs we may see unfolded constants like (int (*) ()) 0.  */
     270                 :     4197488 :   if (TREE_CODE (cval) == INTEGER_CST)
     271                 :             :     {
     272                 :       60455 :       if (TREE_OVERFLOW_P (cval))
     273                 :           0 :         cval = drop_tree_overflow (cval);
     274                 :       60455 :       if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
     275                 :       60454 :         cval = fold_convert (TREE_TYPE (orig_cval), cval);
     276                 :       60455 :       return cval;
     277                 :             :     }
     278                 :             :   return orig_cval;
     279                 :             : }
     280                 :             : 
     281                 :             : /* If SYM is a constant variable with known value, return the value.
     282                 :             :    NULL_TREE is returned otherwise.  */
     283                 :             : 
     284                 :             : tree
     285                 :    20801546 : get_symbol_constant_value (tree sym)
     286                 :             : {
     287                 :    20801546 :   tree val = ctor_for_folding (sym);
     288                 :    20801546 :   if (val != error_mark_node)
     289                 :             :     {
     290                 :       38695 :       if (val)
     291                 :             :         {
     292                 :       36516 :           val = canonicalize_constructor_val (unshare_expr (val), sym);
     293                 :       36516 :           if (val
     294                 :       36516 :               && is_gimple_min_invariant (val)
     295                 :       65927 :               && useless_type_conversion_p (TREE_TYPE (sym), TREE_TYPE (val)))
     296                 :             :             return val;
     297                 :             :           else
     298                 :        7212 :             return NULL_TREE;
     299                 :             :         }
     300                 :             :       /* Variables declared 'const' without an initializer
     301                 :             :          have zero as the initializer if they may not be
     302                 :             :          overridden at link or run time.  */
     303                 :        2179 :       if (!val
     304                 :        2179 :           && is_gimple_reg_type (TREE_TYPE (sym)))
     305                 :        1907 :         return build_zero_cst (TREE_TYPE (sym));
     306                 :             :     }
     307                 :             : 
     308                 :             :   return NULL_TREE;
     309                 :             : }
     310                 :             : 
     311                 :             : 
     312                 :             : 
     313                 :             : /* Subroutine of fold_stmt.  We perform constant folding of the
     314                 :             :    memory reference tree EXPR.  */
     315                 :             : 
     316                 :             : static tree
     317                 :    62153580 : maybe_fold_reference (tree expr)
     318                 :             : {
     319                 :    62153580 :   tree result = NULL_TREE;
     320                 :             : 
     321                 :    62153580 :   if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
     322                 :    60222815 :        || TREE_CODE (expr) == REALPART_EXPR
     323                 :    59531608 :        || TREE_CODE (expr) == IMAGPART_EXPR)
     324                 :    63670731 :       && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
     325                 :         287 :     result = fold_unary_loc (EXPR_LOCATION (expr),
     326                 :             :                              TREE_CODE (expr),
     327                 :         287 :                              TREE_TYPE (expr),
     328                 :         287 :                              TREE_OPERAND (expr, 0));
     329                 :    62153293 :   else if (TREE_CODE (expr) == BIT_FIELD_REF
     330                 :    62153293 :            && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
     331                 :           4 :     result = fold_ternary_loc (EXPR_LOCATION (expr),
     332                 :             :                                TREE_CODE (expr),
     333                 :           4 :                                TREE_TYPE (expr),
     334                 :           4 :                                TREE_OPERAND (expr, 0),
     335                 :           4 :                                TREE_OPERAND (expr, 1),
     336                 :           4 :                                TREE_OPERAND (expr, 2));
     337                 :             :   else
     338                 :    62153289 :     result = fold_const_aggregate_ref (expr);
     339                 :             : 
     340                 :    62153580 :   if (result && is_gimple_min_invariant (result))
     341                 :             :     return result;
     342                 :             : 
     343                 :             :   return NULL_TREE;
     344                 :             : }
     345                 :             : 
     346                 :             : /* Return true if EXPR is an acceptable right-hand-side for a
     347                 :             :    GIMPLE assignment.  We validate the entire tree, not just
     348                 :             :    the root node, thus catching expressions that embed complex
     349                 :             :    operands that are not permitted in GIMPLE.  This function
     350                 :             :    is needed because the folding routines in fold-const.cc
     351                 :             :    may return such expressions in some cases, e.g., an array
     352                 :             :    access with an embedded index addition.  It may make more
     353                 :             :    sense to have folding routines that are sensitive to the
     354                 :             :    constraints on GIMPLE operands, rather than abandoning any
     355                 :             :    any attempt to fold if the usual folding turns out to be too
     356                 :             :    aggressive.  */
     357                 :             : 
     358                 :             : bool
     359                 :           0 : valid_gimple_rhs_p (tree expr)
     360                 :             : {
     361                 :           0 :   enum tree_code code = TREE_CODE (expr);
     362                 :             : 
     363                 :           0 :   switch (TREE_CODE_CLASS (code))
     364                 :             :     {
     365                 :           0 :     case tcc_declaration:
     366                 :           0 :       if (!is_gimple_variable (expr))
     367                 :             :         return false;
     368                 :             :       break;
     369                 :             : 
     370                 :             :     case tcc_constant:
     371                 :             :       /* All constants are ok.  */
     372                 :             :       break;
     373                 :             : 
     374                 :           0 :     case tcc_comparison:
     375                 :             :       /* GENERIC allows comparisons with non-boolean types, reject
     376                 :             :          those for GIMPLE.  Let vector-typed comparisons pass - rules
     377                 :             :          for GENERIC and GIMPLE are the same here.  */
     378                 :           0 :       if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
     379                 :           0 :             && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
     380                 :           0 :                 || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
     381                 :           0 :           && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
     382                 :             :         return false;
     383                 :             : 
     384                 :             :       /* Fallthru.  */
     385                 :           0 :     case tcc_binary:
     386                 :           0 :       if (!is_gimple_val (TREE_OPERAND (expr, 0))
     387                 :           0 :           || !is_gimple_val (TREE_OPERAND (expr, 1)))
     388                 :           0 :         return false;
     389                 :             :       break;
     390                 :             : 
     391                 :           0 :     case tcc_unary:
     392                 :           0 :       if (!is_gimple_val (TREE_OPERAND (expr, 0)))
     393                 :             :         return false;
     394                 :             :       break;
     395                 :             : 
     396                 :           0 :     case tcc_expression:
     397                 :           0 :       switch (code)
     398                 :             :         {
     399                 :           0 :         case ADDR_EXPR:
     400                 :           0 :           {
     401                 :           0 :             tree t;
     402                 :           0 :             if (is_gimple_min_invariant (expr))
     403                 :             :               return true;
     404                 :           0 :             t = TREE_OPERAND (expr, 0);
     405                 :           0 :             while (handled_component_p (t))
     406                 :             :               {
     407                 :             :                 /* ??? More checks needed, see the GIMPLE verifier.  */
     408                 :           0 :                 if ((TREE_CODE (t) == ARRAY_REF
     409                 :           0 :                      || TREE_CODE (t) == ARRAY_RANGE_REF)
     410                 :           0 :                     && !is_gimple_val (TREE_OPERAND (t, 1)))
     411                 :             :                   return false;
     412                 :           0 :                 t = TREE_OPERAND (t, 0);
     413                 :             :               }
     414                 :           0 :             if (!is_gimple_id (t))
     415                 :             :               return false;
     416                 :             :           }
     417                 :             :           break;
     418                 :             : 
     419                 :           0 :         default:
     420                 :           0 :           if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
     421                 :             :             {
     422                 :           0 :               if (!is_gimple_val (TREE_OPERAND (expr, 0))
     423                 :           0 :                   || !is_gimple_val (TREE_OPERAND (expr, 1))
     424                 :           0 :                   || !is_gimple_val (TREE_OPERAND (expr, 2)))
     425                 :           0 :                 return false;
     426                 :             :               break;
     427                 :             :             }
     428                 :             :           return false;
     429                 :             :         }
     430                 :             :       break;
     431                 :             : 
     432                 :             :     case tcc_vl_exp:
     433                 :             :       return false;
     434                 :             : 
     435                 :           0 :     case tcc_exceptional:
     436                 :           0 :       if (code == CONSTRUCTOR)
     437                 :             :         {
     438                 :             :           unsigned i;
     439                 :             :           tree elt;
     440                 :           0 :           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
     441                 :           0 :             if (!is_gimple_val (elt))
     442                 :             :               return false;
     443                 :             :           return true;
     444                 :             :         }
     445                 :           0 :       if (code != SSA_NAME)
     446                 :             :         return false;
     447                 :             :       break;
     448                 :             : 
     449                 :           0 :     case tcc_reference:
     450                 :           0 :       if (code == BIT_FIELD_REF)
     451                 :           0 :         return is_gimple_val (TREE_OPERAND (expr, 0));
     452                 :             :       return false;
     453                 :             : 
     454                 :             :     default:
     455                 :             :       return false;
     456                 :             :     }
     457                 :             : 
     458                 :             :   return true;
     459                 :             : }
     460                 :             : 
     461                 :             : 
     462                 :             : /* Attempt to fold an assignment statement pointed-to by SI.  Returns a
     463                 :             :    replacement rhs for the statement or NULL_TREE if no simplification
     464                 :             :    could be made.  It is assumed that the operands have been previously
     465                 :             :    folded.  */
     466                 :             : 
     467                 :             : static tree
     468                 :   249459800 : fold_gimple_assign (gimple_stmt_iterator *si)
     469                 :             : {
     470                 :   249459800 :   gimple *stmt = gsi_stmt (*si);
     471                 :   249459800 :   enum tree_code subcode = gimple_assign_rhs_code (stmt);
     472                 :   249459800 :   location_t loc = gimple_location (stmt);
     473                 :             : 
     474                 :   249459800 :   tree result = NULL_TREE;
     475                 :             : 
     476                 :   249459800 :   switch (get_gimple_rhs_class (subcode))
     477                 :             :     {
     478                 :   166477935 :     case GIMPLE_SINGLE_RHS:
     479                 :   166477935 :       {
     480                 :   166477935 :         tree rhs = gimple_assign_rhs1 (stmt);
     481                 :             : 
     482                 :   166477935 :         if (TREE_CLOBBER_P (rhs))
     483                 :             :           return NULL_TREE;
     484                 :             : 
     485                 :   151106648 :         if (REFERENCE_CLASS_P (rhs))
     486                 :    59889615 :           return maybe_fold_reference (rhs);
     487                 :             : 
     488                 :    91217033 :         else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
     489                 :             :           {
     490                 :       65023 :             tree val = OBJ_TYPE_REF_EXPR (rhs);
     491                 :       65023 :             if (is_gimple_min_invariant (val))
     492                 :             :               return val;
     493                 :       65021 :             else if (flag_devirtualize && virtual_method_call_p (rhs))
     494                 :             :               {
     495                 :       64981 :                 bool final;
     496                 :       64981 :                 vec <cgraph_node *>targets
     497                 :       64981 :                   = possible_polymorphic_call_targets (rhs, stmt, &final);
     498                 :       65004 :                 if (final && targets.length () <= 1 && dbg_cnt (devirt))
     499                 :             :                   {
     500                 :          23 :                     if (dump_enabled_p ())
     501                 :             :                       {
     502                 :           0 :                         dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
     503                 :             :                                          "resolving virtual function address "
     504                 :             :                                          "reference to function %s\n",
     505                 :           0 :                                          targets.length () == 1
     506                 :           0 :                                          ? targets[0]->name ()
     507                 :             :                                          : "NULL");
     508                 :             :                       }
     509                 :          23 :                     if (targets.length () == 1)
     510                 :             :                       {
     511                 :          16 :                         val = fold_convert (TREE_TYPE (val),
     512                 :             :                                             build_fold_addr_expr_loc
     513                 :             :                                               (loc, targets[0]->decl));
     514                 :          16 :                         STRIP_USELESS_TYPE_CONVERSION (val);
     515                 :             :                       }
     516                 :             :                     else
     517                 :             :                       /* We cannot use __builtin_unreachable here because it
     518                 :             :                          cannot have address taken.  */
     519                 :           7 :                       val = build_int_cst (TREE_TYPE (val), 0);
     520                 :          23 :                     return val;
     521                 :             :                   }
     522                 :             :               }
     523                 :             :           }
     524                 :             : 
     525                 :    91152010 :         else if (TREE_CODE (rhs) == ADDR_EXPR)
     526                 :             :           {
     527                 :    14401186 :             tree ref = TREE_OPERAND (rhs, 0);
     528                 :    14401186 :             if (TREE_CODE (ref) == MEM_REF
     529                 :    14401186 :                 && integer_zerop (TREE_OPERAND (ref, 1)))
     530                 :             :               {
     531                 :        2422 :                 result = TREE_OPERAND (ref, 0);
     532                 :        2422 :                 if (!useless_type_conversion_p (TREE_TYPE (rhs),
     533                 :        2422 :                                                 TREE_TYPE (result)))
     534                 :           0 :                   result = build1 (NOP_EXPR, TREE_TYPE (rhs), result);
     535                 :        2422 :                 return result;
     536                 :             :               }
     537                 :             :           }
     538                 :             : 
     539                 :    76750824 :         else if (TREE_CODE (rhs) == CONSTRUCTOR
     540                 :    76750824 :                  && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
     541                 :             :           {
     542                 :             :             /* Fold a constant vector CONSTRUCTOR to VECTOR_CST.  */
     543                 :             :             unsigned i;
     544                 :             :             tree val;
     545                 :             : 
     546                 :      505867 :             FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
     547                 :      501468 :               if (! CONSTANT_CLASS_P (val))
     548                 :             :                 return NULL_TREE;
     549                 :             : 
     550                 :        4399 :             return build_vector_from_ctor (TREE_TYPE (rhs),
     551                 :        8798 :                                            CONSTRUCTOR_ELTS (rhs));
     552                 :             :           }
     553                 :             : 
     554                 :    76340267 :         else if (DECL_P (rhs)
     555                 :    76340267 :                  && is_gimple_reg_type (TREE_TYPE (rhs)))
     556                 :    11974899 :           return get_symbol_constant_value (rhs);
     557                 :             :       }
     558                 :             :       break;
     559                 :             : 
     560                 :             :     case GIMPLE_UNARY_RHS:
     561                 :             :       break;
     562                 :             : 
     563                 :             :     case GIMPLE_BINARY_RHS:
     564                 :             :       break;
     565                 :             : 
     566                 :      428332 :     case GIMPLE_TERNARY_RHS:
     567                 :      856664 :       result = fold_ternary_loc (loc, subcode,
     568                 :      428332 :                                  TREE_TYPE (gimple_assign_lhs (stmt)),
     569                 :             :                                  gimple_assign_rhs1 (stmt),
     570                 :             :                                  gimple_assign_rhs2 (stmt),
     571                 :             :                                  gimple_assign_rhs3 (stmt));
     572                 :             : 
     573                 :      428332 :       if (result)
     574                 :             :         {
     575                 :           0 :           STRIP_USELESS_TYPE_CONVERSION (result);
     576                 :           0 :           if (valid_gimple_rhs_p (result))
     577                 :             :             return result;
     578                 :             :         }
     579                 :             :       break;
     580                 :             : 
     581                 :           0 :     case GIMPLE_INVALID_RHS:
     582                 :           0 :       gcc_unreachable ();
     583                 :             :     }
     584                 :             : 
     585                 :             :   return NULL_TREE;
     586                 :             : }
     587                 :             : 
     588                 :             : 
     589                 :             : /* Replace a statement at *SI_P with a sequence of statements in STMTS,
     590                 :             :    adjusting the replacement stmts location and virtual operands.
     591                 :             :    If the statement has a lhs the last stmt in the sequence is expected
     592                 :             :    to assign to that lhs.  */
     593                 :             : 
     594                 :             : void
     595                 :      112314 : gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
     596                 :             : {
     597                 :      112314 :   gimple *stmt = gsi_stmt (*si_p);
     598                 :             : 
     599                 :      112314 :   if (gimple_has_location (stmt))
     600                 :       90982 :     annotate_all_with_location (stmts, gimple_location (stmt));
     601                 :             : 
     602                 :             :   /* First iterate over the replacement statements backward, assigning
     603                 :             :      virtual operands to their defining statements.  */
     604                 :      112314 :   gimple *laststore = NULL;
     605                 :      224628 :   for (gimple_stmt_iterator i = gsi_last (stmts);
     606                 :      420924 :        !gsi_end_p (i); gsi_prev (&i))
     607                 :             :     {
     608                 :      154305 :       gimple *new_stmt = gsi_stmt (i);
     609                 :      154305 :       if ((gimple_assign_single_p (new_stmt)
     610                 :      101881 :            && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
     611                 :      255725 :           || (is_gimple_call (new_stmt)
     612                 :       15346 :               && (gimple_call_flags (new_stmt)
     613                 :       15346 :                   & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
     614                 :             :         {
     615                 :        2563 :           tree vdef;
     616                 :        2563 :           if (!laststore)
     617                 :        2410 :             vdef = gimple_vdef (stmt);
     618                 :             :           else
     619                 :         153 :             vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
     620                 :        2563 :           gimple_set_vdef (new_stmt, vdef);
     621                 :        2563 :           if (vdef && TREE_CODE (vdef) == SSA_NAME)
     622                 :        1636 :             SSA_NAME_DEF_STMT (vdef) = new_stmt;
     623                 :             :           laststore = new_stmt;
     624                 :             :         }
     625                 :             :     }
     626                 :             : 
     627                 :             :   /* Second iterate over the statements forward, assigning virtual
     628                 :             :      operands to their uses.  */
     629                 :      112314 :   tree reaching_vuse = gimple_vuse (stmt);
     630                 :      112314 :   for (gimple_stmt_iterator i = gsi_start (stmts);
     631                 :      266619 :        !gsi_end_p (i); gsi_next (&i))
     632                 :             :     {
     633                 :      154305 :       gimple *new_stmt = gsi_stmt (i);
     634                 :             :       /* If the new statement possibly has a VUSE, update it with exact SSA
     635                 :             :          name we know will reach this one.  */
     636                 :      154305 :       if (gimple_has_mem_ops (new_stmt))
     637                 :      154303 :         gimple_set_vuse (new_stmt, reaching_vuse);
     638                 :      154305 :       gimple_set_modified (new_stmt, true);
     639                 :      461272 :       if (gimple_vdef (new_stmt))
     640                 :      154305 :         reaching_vuse = gimple_vdef (new_stmt);
     641                 :             :     }
     642                 :             : 
     643                 :             :   /* If the new sequence does not do a store release the virtual
     644                 :             :      definition of the original statement.  */
     645                 :      112314 :   if (reaching_vuse
     646                 :      183756 :       && reaching_vuse == gimple_vuse (stmt))
     647                 :             :     {
     648                 :       69959 :       tree vdef = gimple_vdef (stmt);
     649                 :       69959 :       if (vdef
     650                 :        1373 :           && TREE_CODE (vdef) == SSA_NAME)
     651                 :             :         {
     652                 :        1318 :           unlink_stmt_vdef (stmt);
     653                 :        1318 :           release_ssa_name (vdef);
     654                 :             :         }
     655                 :             :     }
     656                 :             : 
     657                 :             :   /* Finally replace the original statement with the sequence.  */
     658                 :      112314 :   gsi_replace_with_seq (si_p, stmts, false);
     659                 :      112314 : }
     660                 :             : 
     661                 :             : /* Helper function for update_gimple_call and
     662                 :             :    gimplify_and_update_call_from_tree.  A GIMPLE_CALL STMT is being replaced
     663                 :             :    with GIMPLE_CALL NEW_STMT.  */
     664                 :             : 
     665                 :             : static void
     666                 :        2284 : finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple *new_stmt,
     667                 :             :                            gimple *stmt)
     668                 :             : {
     669                 :        2284 :   tree lhs = gimple_call_lhs (stmt);
     670                 :        2284 :   gimple_call_set_lhs (new_stmt, lhs);
     671                 :        2284 :   if (lhs && TREE_CODE (lhs) == SSA_NAME)
     672                 :         835 :     SSA_NAME_DEF_STMT (lhs) = new_stmt;
     673                 :        2284 :   gimple_move_vops (new_stmt, stmt);
     674                 :        2284 :   gimple_set_location (new_stmt, gimple_location (stmt));
     675                 :        2284 :   if (gimple_block (new_stmt) == NULL_TREE)
     676                 :           1 :     gimple_set_block (new_stmt, gimple_block (stmt));
     677                 :        2284 :   gsi_replace (si_p, new_stmt, false);
     678                 :        2284 : }
     679                 :             : 
     680                 :             : /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
     681                 :             :    with number of arguments NARGS, where the arguments in GIMPLE form
     682                 :             :    follow NARGS argument.  */
     683                 :             : 
     684                 :             : bool
     685                 :        2279 : update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
     686                 :             : {
     687                 :        2279 :   va_list ap;
     688                 :        2279 :   gcall *new_stmt, *stmt = as_a <gcall *> (gsi_stmt (*si_p));
     689                 :             : 
     690                 :        2279 :   gcc_assert (is_gimple_call (stmt));
     691                 :        2279 :   va_start (ap, nargs);
     692                 :        2279 :   new_stmt = gimple_build_call_valist (fn, nargs, ap);
     693                 :        2279 :   finish_update_gimple_call (si_p, new_stmt, stmt);
     694                 :        2279 :   va_end (ap);
     695                 :        2279 :   return true;
     696                 :             : }
     697                 :             : 
     698                 :             : /* Return true if EXPR is a CALL_EXPR suitable for representation
     699                 :             :    as a single GIMPLE_CALL statement.  If the arguments require
     700                 :             :    further gimplification, return false.  */
     701                 :             : 
     702                 :             : static bool
     703                 :       51979 : valid_gimple_call_p (tree expr)
     704                 :             : {
     705                 :       51979 :   unsigned i, nargs;
     706                 :             : 
     707                 :       51979 :   if (TREE_CODE (expr) != CALL_EXPR)
     708                 :             :     return false;
     709                 :             : 
     710                 :           5 :   nargs = call_expr_nargs (expr);
     711                 :          10 :   for (i = 0; i < nargs; i++)
     712                 :             :     {
     713                 :           5 :       tree arg = CALL_EXPR_ARG (expr, i);
     714                 :           5 :       if (is_gimple_reg_type (TREE_TYPE (arg)))
     715                 :             :         {
     716                 :           5 :           if (!is_gimple_val (arg))
     717                 :             :             return false;
     718                 :             :         }
     719                 :             :       else
     720                 :           0 :         if (!is_gimple_lvalue (arg))
     721                 :             :           return false;
     722                 :             :     }
     723                 :             : 
     724                 :             :   return true;
     725                 :             : }
     726                 :             : 
     727                 :             : /* Convert EXPR into a GIMPLE value suitable for substitution on the
     728                 :             :    RHS of an assignment.  Insert the necessary statements before
     729                 :             :    iterator *SI_P.  The statement at *SI_P, which must be a GIMPLE_CALL
     730                 :             :    is replaced.  If the call is expected to produces a result, then it
     731                 :             :    is replaced by an assignment of the new RHS to the result variable.
     732                 :             :    If the result is to be ignored, then the call is replaced by a
     733                 :             :    GIMPLE_NOP.  A proper VDEF chain is retained by making the first
     734                 :             :    VUSE and the last VDEF of the whole sequence be the same as the replaced
     735                 :             :    statement and using new SSA names for stores in between.  */
     736                 :             : 
     737                 :             : void
     738                 :       51979 : gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
     739                 :             : {
     740                 :       51979 :   tree lhs;
     741                 :       51979 :   gimple *stmt, *new_stmt;
     742                 :       51979 :   gimple_stmt_iterator i;
     743                 :       51979 :   gimple_seq stmts = NULL;
     744                 :             : 
     745                 :       51979 :   stmt = gsi_stmt (*si_p);
     746                 :             : 
     747                 :       51979 :   gcc_assert (is_gimple_call (stmt));
     748                 :             : 
     749                 :       51979 :   if (valid_gimple_call_p (expr))
     750                 :             :     {
     751                 :             :       /* The call has simplified to another call.  */
     752                 :           5 :       tree fn = CALL_EXPR_FN (expr);
     753                 :           5 :       unsigned i;
     754                 :           5 :       unsigned nargs = call_expr_nargs (expr);
     755                 :           5 :       vec<tree> args = vNULL;
     756                 :           5 :       gcall *new_stmt;
     757                 :             : 
     758                 :           5 :       if (nargs > 0)
     759                 :             :         {
     760                 :           5 :           args.create (nargs);
     761                 :           5 :           args.safe_grow_cleared (nargs, true);
     762                 :             : 
     763                 :          15 :           for (i = 0; i < nargs; i++)
     764                 :           5 :             args[i] = CALL_EXPR_ARG (expr, i);
     765                 :             :         }
     766                 :             : 
     767                 :           5 :       new_stmt = gimple_build_call_vec (fn, args);
     768                 :           5 :       finish_update_gimple_call (si_p, new_stmt, stmt);
     769                 :           5 :       args.release ();
     770                 :           5 :       return;
     771                 :             :     }
     772                 :             : 
     773                 :       51974 :   lhs = gimple_call_lhs (stmt);
     774                 :       51974 :   if (lhs == NULL_TREE)
     775                 :             :     {
     776                 :       13096 :       push_gimplify_context (gimple_in_ssa_p (cfun));
     777                 :        6548 :       gimplify_and_add (expr, &stmts);
     778                 :        6548 :       pop_gimplify_context (NULL);
     779                 :             : 
     780                 :             :       /* We can end up with folding a memcpy of an empty class assignment
     781                 :             :          which gets optimized away by C++ gimplification.  */
     782                 :        6548 :       if (gimple_seq_empty_p (stmts))
     783                 :             :         {
     784                 :        6219 :           if (gimple_in_ssa_p (cfun))
     785                 :             :             {
     786                 :        6219 :               unlink_stmt_vdef (stmt);
     787                 :        6219 :               release_defs (stmt);
     788                 :             :             }
     789                 :        6219 :           gsi_replace (si_p, gimple_build_nop (), false);
     790                 :        6219 :           return;
     791                 :             :         }
     792                 :             :     }
     793                 :             :   else
     794                 :             :     {
     795                 :       45426 :       tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
     796                 :       45426 :       new_stmt = gimple_build_assign (lhs, tmp);
     797                 :       45426 :       i = gsi_last (stmts);
     798                 :       45426 :       gsi_insert_after_without_update (&i, new_stmt,
     799                 :             :                                        GSI_CONTINUE_LINKING);
     800                 :             :     }
     801                 :             : 
     802                 :       45755 :   gsi_replace_with_seq_vops (si_p, stmts);
     803                 :             : }
     804                 :             : 
     805                 :             : /* Print a message in the dump file recording transformation of FROM to TO.  */
     806                 :             : 
     807                 :             : static void
     808                 :       39598 : dump_transformation (gcall *from, gcall *to)
     809                 :             : {
     810                 :       39598 :   if (dump_enabled_p ())
     811                 :          11 :     dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, from, "simplified %T to %T\n",
     812                 :             :                      gimple_call_fn (from), gimple_call_fn (to));
     813                 :       39598 : }
     814                 :             : 
     815                 :             : /* Replace the call at *GSI with the gimple value VAL.  */
     816                 :             : 
     817                 :             : void
     818                 :       74138 : replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
     819                 :             : {
     820                 :       74138 :   gimple *stmt = gsi_stmt (*gsi);
     821                 :       74138 :   tree lhs = gimple_call_lhs (stmt);
     822                 :       74138 :   gimple *repl;
     823                 :       74138 :   if (lhs)
     824                 :             :     {
     825                 :       71037 :       if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
     826                 :        4022 :         val = fold_convert (TREE_TYPE (lhs), val);
     827                 :       71037 :       repl = gimple_build_assign (lhs, val);
     828                 :             :     }
     829                 :             :   else
     830                 :        3101 :     repl = gimple_build_nop ();
     831                 :       74138 :   tree vdef = gimple_vdef (stmt);
     832                 :       74138 :   if (vdef && TREE_CODE (vdef) == SSA_NAME)
     833                 :             :     {
     834                 :        3762 :       unlink_stmt_vdef (stmt);
     835                 :        3762 :       release_ssa_name (vdef);
     836                 :             :     }
     837                 :       74138 :   gsi_replace (gsi, repl, false);
     838                 :       74138 : }
     839                 :             : 
     840                 :             : /* Replace the call at *GSI with the new call REPL and fold that
     841                 :             :    again.  */
     842                 :             : 
     843                 :             : static void
     844                 :       39598 : replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
     845                 :             : {
     846                 :       39598 :   gimple *stmt = gsi_stmt (*gsi);
     847                 :       39598 :   dump_transformation (as_a <gcall *> (stmt), as_a <gcall *> (repl));
     848                 :       39598 :   gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
     849                 :       39598 :   gimple_set_location (repl, gimple_location (stmt));
     850                 :       39598 :   gimple_move_vops (repl, stmt);
     851                 :       39598 :   gsi_replace (gsi, repl, false);
     852                 :       39598 :   fold_stmt (gsi);
     853                 :       39598 : }
     854                 :             : 
     855                 :             : /* Return true if VAR is a VAR_DECL or a component thereof.  */
     856                 :             : 
     857                 :             : static bool
     858                 :      430619 : var_decl_component_p (tree var)
     859                 :             : {
     860                 :      430619 :   tree inner = var;
     861                 :      626934 :   while (handled_component_p (inner))
     862                 :      196315 :     inner = TREE_OPERAND (inner, 0);
     863                 :      430619 :   return (DECL_P (inner)
     864                 :      430619 :           || (TREE_CODE (inner) == MEM_REF
     865                 :       54023 :               && TREE_CODE (TREE_OPERAND (inner, 0)) == ADDR_EXPR));
     866                 :             : }
     867                 :             : 
     868                 :             : /* Return TRUE if the SIZE argument, representing the size of an
     869                 :             :    object, is in a range of values of which exactly zero is valid.  */
     870                 :             : 
     871                 :             : static bool
     872                 :      950206 : size_must_be_zero_p (tree size)
     873                 :             : {
     874                 :      950206 :   if (integer_zerop (size))
     875                 :             :     return true;
     876                 :             : 
     877                 :      947602 :   if (TREE_CODE (size) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (size)))
     878                 :             :     return false;
     879                 :             : 
     880                 :      544983 :   tree type = TREE_TYPE (size);
     881                 :      544983 :   int prec = TYPE_PRECISION (type);
     882                 :             : 
     883                 :             :   /* Compute the value of SSIZE_MAX, the largest positive value that
     884                 :             :      can be stored in ssize_t, the signed counterpart of size_t.  */
     885                 :      544983 :   wide_int ssize_max = wi::lshift (wi::one (prec), prec - 1) - 1;
     886                 :      544983 :   wide_int zero = wi::zero (TYPE_PRECISION (type));
     887                 :      544983 :   int_range_max valid_range (type, zero, ssize_max);
     888                 :      544983 :   int_range_max vr;
     889                 :     1089966 :   get_range_query (cfun)->range_of_expr (vr, size);
     890                 :             : 
     891                 :      544983 :   if (vr.undefined_p ())
     892                 :          40 :     vr.set_varying (TREE_TYPE (size));
     893                 :      544983 :   vr.intersect (valid_range);
     894                 :      544983 :   return vr.zero_p ();
     895                 :      544983 : }
     896                 :             : 
     897                 :             : /* Optimize
     898                 :             :    a = {};
     899                 :             :    b = a;
     900                 :             :    into
     901                 :             :    a = {};
     902                 :             :    b = {};
     903                 :             :    Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
     904                 :             :    and/or memcpy (&b, &a, sizeof (a)); instead of b = a;  */
     905                 :             : 
     906                 :             : static bool
     907                 :     9054358 : optimize_memcpy_to_memset (gimple_stmt_iterator *gsip, tree dest, tree src, tree len)
     908                 :             : {
     909                 :     9054358 :   ao_ref read;
     910                 :     9054358 :   gimple *stmt = gsi_stmt (*gsip);
     911                 :    18108716 :   if (gimple_has_volatile_ops (stmt))
     912                 :             :     return false;
     913                 :             : 
     914                 :             : 
     915                 :     9052624 :   tree src2 = NULL_TREE, len2 = NULL_TREE;
     916                 :     9052624 :   poly_int64 offset, offset2;
     917                 :     9052624 :   tree val = integer_zero_node;
     918                 :     9052624 :   bool len_was_null = len == NULL_TREE;
     919                 :     9052624 :   if (len == NULL_TREE)
     920                 :     8943305 :     len = (TREE_CODE (src) == COMPONENT_REF
     921                 :    16826924 :            ? DECL_SIZE_UNIT (TREE_OPERAND (src, 1))
     922                 :     7883619 :            : TYPE_SIZE_UNIT (TREE_TYPE (src)));
     923                 :     8943305 :   if (len == NULL_TREE
     924                 :     9052624 :       || !poly_int_tree_p (len))
     925                 :             :     return false;
     926                 :             : 
     927                 :     9052623 :   ao_ref_init (&read, src);
     928                 :     9052623 :   tree vuse = gimple_vuse (stmt);
     929                 :    33095725 :   gimple *defstmt;
     930                 :    33095725 :   do {
     931                 :    29927388 :     if (vuse == NULL || TREE_CODE (vuse) != SSA_NAME)
     932                 :             :       return false;
     933                 :    29359600 :     defstmt = SSA_NAME_DEF_STMT (vuse);
     934                 :    29359600 :     if (is_a <gphi*>(defstmt))
     935                 :             :       return false;
     936                 :             : 
     937                 :             :     /* If the len was null, then we can use TBBA. */
     938                 :    28398703 :     if (stmt_may_clobber_ref_p_1 (defstmt, &read,
     939                 :             :                                   /* tbaa_p = */ len_was_null))
     940                 :             :       break;
     941                 :    57138827 :     vuse = gimple_vuse (defstmt);
     942                 :             :   } while (true);
     943                 :             : 
     944                 :     4355601 :   if (gimple_store_p (defstmt)
     945                 :     3968813 :       && gimple_assign_single_p (defstmt)
     946                 :     2885087 :       && TREE_CODE (gimple_assign_rhs1 (defstmt)) == STRING_CST
     947                 :     4357756 :       && !gimple_clobber_p (defstmt))
     948                 :             :     {
     949                 :        2155 :       tree str = gimple_assign_rhs1 (defstmt);
     950                 :        2155 :       src2 = gimple_assign_lhs (defstmt);
     951                 :             :       /* The string must contain all null char's for now.  */
     952                 :        2494 :       for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
     953                 :             :         {
     954                 :        2485 :           if (TREE_STRING_POINTER (str)[i] != 0)
     955                 :             :             {
     956                 :             :               src2 = NULL_TREE;
     957                 :             :               break;
     958                 :             :             }
     959                 :             :         }
     960                 :             :     }
     961                 :     4353446 :   else if (gimple_store_p (defstmt)
     962                 :     3966658 :       && gimple_assign_single_p (defstmt)
     963                 :     2882932 :       && TREE_CODE (gimple_assign_rhs1 (defstmt)) == CONSTRUCTOR
     964                 :     4407651 :       && !gimple_clobber_p (defstmt))
     965                 :       19473 :     src2 = gimple_assign_lhs (defstmt);
     966                 :     4333973 :   else if (gimple_call_builtin_p (defstmt, BUILT_IN_MEMSET)
     967                 :         887 :            && TREE_CODE (gimple_call_arg (defstmt, 0)) == ADDR_EXPR
     968                 :     4334664 :            && TREE_CODE (gimple_call_arg (defstmt, 1)) == INTEGER_CST)
     969                 :             :     {
     970                 :         691 :       src2 = TREE_OPERAND (gimple_call_arg (defstmt, 0), 0);
     971                 :         691 :       len2 = gimple_call_arg (defstmt, 2);
     972                 :         691 :       val = gimple_call_arg (defstmt, 1);
     973                 :             :       /* For non-0 val, we'd have to transform stmt from assignment
     974                 :             :          into memset (only if dest is addressable).  */
     975                 :         691 :       if (!integer_zerop (val) && is_gimple_assign (stmt))
     976                 :             :         src2 = NULL_TREE;
     977                 :             :     }
     978                 :             : 
     979                 :       21963 :   if (src2 == NULL_TREE)
     980                 :     4335784 :     return false;
     981                 :             : 
     982                 :       19817 :   if (len2 == NULL_TREE)
     983                 :       19482 :     len2 = (TREE_CODE (src2) == COMPONENT_REF
     984                 :       35040 :             ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
     985                 :       15558 :             : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
     986                 :       19482 :   if (len2 == NULL_TREE
     987                 :       19817 :       || !poly_int_tree_p (len2))
     988                 :             :     return false;
     989                 :             : 
     990                 :       19817 :   src = get_addr_base_and_unit_offset (src, &offset);
     991                 :       19817 :   src2 = get_addr_base_and_unit_offset (src2, &offset2);
     992                 :       19817 :   if (src == NULL_TREE
     993                 :       19817 :       || src2 == NULL_TREE
     994                 :       19817 :       || maybe_lt (offset, offset2))
     995                 :             :     return false;
     996                 :             : 
     997                 :       19359 :   if (!operand_equal_p (src, src2, 0))
     998                 :             :     return false;
     999                 :             : 
    1000                 :             :   /* [ src + offset2, src + offset2 + len2 - 1 ] is set to val.
    1001                 :             :      Make sure that
    1002                 :             :      [ src + offset, src + offset + len - 1 ] is a subset of that.  */
    1003                 :       10441 :   if (maybe_gt (wi::to_poly_offset (len) + (offset - offset2),
    1004                 :             :                 wi::to_poly_offset (len2)))
    1005                 :             :     return false;
    1006                 :             : 
    1007                 :       10123 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1008                 :             :     {
    1009                 :          19 :       fprintf (dump_file, "Simplified\n  ");
    1010                 :          19 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1011                 :          19 :       fprintf (dump_file, "after previous\n  ");
    1012                 :          19 :       print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
    1013                 :             :     }
    1014                 :             : 
    1015                 :             :   /* For simplicity, don't change the kind of the stmt,
    1016                 :             :      turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
    1017                 :             :      into memset (&dest, val, len);
    1018                 :             :      In theory we could change dest = src into memset if dest
    1019                 :             :      is addressable (maybe beneficial if val is not 0), or
    1020                 :             :      memcpy (&dest, &src, len) into dest = {} if len is the size
    1021                 :             :      of dest, dest isn't volatile.  */
    1022                 :       10123 :   if (is_gimple_assign (stmt))
    1023                 :             :     {
    1024                 :       10121 :       tree ctor = build_constructor (TREE_TYPE (dest), NULL);
    1025                 :       10121 :       gimple_assign_set_rhs_from_tree (gsip, ctor);
    1026                 :       10121 :       update_stmt (stmt);
    1027                 :             :     }
    1028                 :             :   else /* If stmt is memcpy, transform it into memset.  */
    1029                 :             :     {
    1030                 :           2 :       gcall *call = as_a <gcall *> (stmt);
    1031                 :           2 :       tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET);
    1032                 :           2 :       gimple_call_set_fndecl (call, fndecl);
    1033                 :           2 :       gimple_call_set_fntype (call, TREE_TYPE (fndecl));
    1034                 :           2 :       gimple_call_set_arg (call, 1, val);
    1035                 :           2 :       update_stmt (stmt);
    1036                 :             :     }
    1037                 :             : 
    1038                 :       10123 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1039                 :             :     {
    1040                 :          19 :       fprintf (dump_file, "into\n  ");
    1041                 :          19 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1042                 :             :     }
    1043                 :             :   return true;
    1044                 :             : }
    1045                 :             : 
    1046                 :             : /* Fold function call to builtin mem{{,p}cpy,move}.  Try to detect and
    1047                 :             :    diagnose (otherwise undefined) overlapping copies without preventing
    1048                 :             :    folding.  When folded, GCC guarantees that overlapping memcpy has
    1049                 :             :    the same semantics as memmove.  Call to the library memcpy need not
    1050                 :             :    provide the same guarantee.  Return false if no simplification can
    1051                 :             :    be made.  */
    1052                 :             : 
    1053                 :             : static bool
    1054                 :      950206 : gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
    1055                 :             :                                tree dest, tree src, enum built_in_function code)
    1056                 :             : {
    1057                 :      950206 :   gimple *stmt = gsi_stmt (*gsi);
    1058                 :      950206 :   tree lhs = gimple_call_lhs (stmt);
    1059                 :      950206 :   tree len = gimple_call_arg (stmt, 2);
    1060                 :      950206 :   location_t loc = gimple_location (stmt);
    1061                 :             : 
    1062                 :             :   /* If the LEN parameter is a constant zero or in range where
    1063                 :             :      the only valid value is zero, return DEST.  */
    1064                 :      950206 :   if (size_must_be_zero_p (len))
    1065                 :             :     {
    1066                 :        2636 :       gimple *repl;
    1067                 :        2636 :       if (gimple_call_lhs (stmt))
    1068                 :          58 :         repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
    1069                 :             :       else
    1070                 :        2578 :         repl = gimple_build_nop ();
    1071                 :        2636 :       tree vdef = gimple_vdef (stmt);
    1072                 :        2636 :       if (vdef && TREE_CODE (vdef) == SSA_NAME)
    1073                 :             :         {
    1074                 :         563 :           unlink_stmt_vdef (stmt);
    1075                 :         563 :           release_ssa_name (vdef);
    1076                 :             :         }
    1077                 :        2636 :       gsi_replace (gsi, repl, false);
    1078                 :        2636 :       return true;
    1079                 :             :     }
    1080                 :             : 
    1081                 :             :   /* If SRC and DEST are the same (and not volatile), return
    1082                 :             :      DEST{,+LEN,+LEN-1}.  */
    1083                 :      947570 :   if (operand_equal_p (src, dest, 0))
    1084                 :             :     {
    1085                 :             :       /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
    1086                 :             :          It's safe and may even be emitted by GCC itself (see bug
    1087                 :             :          32667).  */
    1088                 :          68 :       unlink_stmt_vdef (stmt);
    1089                 :         136 :       if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
    1090                 :          28 :         release_ssa_name (gimple_vdef (stmt));
    1091                 :          68 :       if (!lhs)
    1092                 :             :         {
    1093                 :          47 :           gsi_replace (gsi, gimple_build_nop (), false);
    1094                 :          47 :           return true;
    1095                 :             :         }
    1096                 :          21 :       goto done;
    1097                 :             :     }
    1098                 :     1895004 :   else if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1099                 :             :     return false;
    1100                 :             :   else
    1101                 :             :     {
    1102                 :             :       /* We cannot (easily) change the type of the copy if it is a storage
    1103                 :             :          order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that can
    1104                 :             :          modify the storage order of objects (see storage_order_barrier_p).  */
    1105                 :      947502 :       tree srctype
    1106                 :      960502 :         = POINTER_TYPE_P (TREE_TYPE (src))
    1107                 :      960502 :           ? TREE_TYPE (TREE_TYPE (src)) : NULL_TREE;
    1108                 :      947502 :       tree desttype
    1109                 :      968784 :         = POINTER_TYPE_P (TREE_TYPE (dest))
    1110                 :      968784 :           ? TREE_TYPE (TREE_TYPE (dest)) : NULL_TREE;
    1111                 :      947502 :       tree destvar, srcvar, srcoff;
    1112                 :      947502 :       unsigned int src_align, dest_align;
    1113                 :      947502 :       unsigned HOST_WIDE_INT tmp_len;
    1114                 :      947502 :       const char *tmp_str;
    1115                 :             : 
    1116                 :             :       /* Build accesses at offset zero with a ref-all character type.  */
    1117                 :      947502 :       tree off0
    1118                 :      947502 :         = build_int_cst (build_pointer_type_for_mode (char_type_node,
    1119                 :      947502 :                                                       ptr_mode, true), 0);
    1120                 :             : 
    1121                 :             :       /* If we can perform the copy efficiently with first doing all loads
    1122                 :             :          and then all stores inline it that way.  Currently efficiently
    1123                 :             :          means that we can load all the memory into a single integer
    1124                 :             :          register which is what MOVE_MAX gives us.  */
    1125                 :      947502 :       src_align = get_pointer_alignment (src);
    1126                 :      947502 :       dest_align = get_pointer_alignment (dest);
    1127                 :      947502 :       if (tree_fits_uhwi_p (len)
    1128                 :      393895 :           && compare_tree_int (len, MOVE_MAX) <= 0
    1129                 :             :           /* FIXME: Don't transform copies from strings with known length.
    1130                 :             :              Until GCC 9 this prevented a case in gcc.dg/strlenopt-8.c
    1131                 :             :              from being handled, and the case was XFAILed for that reason.
    1132                 :             :              Now that it is handled and the XFAIL removed, as soon as other
    1133                 :             :              strlenopt tests that rely on it for passing are adjusted, this
    1134                 :             :              hack can be removed.  */
    1135                 :      298702 :           && !c_strlen (src, 1)
    1136                 :      182962 :           && !((tmp_str = getbyterep (src, &tmp_len)) != NULL
    1137                 :       78274 :                && memchr (tmp_str, 0, tmp_len) == NULL)
    1138                 :      116844 :           && !(srctype
    1139                 :      116844 :                && AGGREGATE_TYPE_P (srctype)
    1140                 :       57303 :                && TYPE_REVERSE_STORAGE_ORDER (srctype))
    1141                 :     1064197 :           && !(desttype
    1142                 :      116695 :                && AGGREGATE_TYPE_P (desttype)
    1143                 :       66244 :                && TYPE_REVERSE_STORAGE_ORDER (desttype)))
    1144                 :             :         {
    1145                 :      116658 :           unsigned ilen = tree_to_uhwi (len);
    1146                 :      116658 :           if (pow2p_hwi (ilen))
    1147                 :             :             {
    1148                 :             :               /* Detect out-of-bounds accesses without issuing warnings.
    1149                 :             :                  Avoid folding out-of-bounds copies but to avoid false
    1150                 :             :                  positives for unreachable code defer warning until after
    1151                 :             :                  DCE has worked its magic.
    1152                 :             :                  -Wrestrict is still diagnosed.  */
    1153                 :       20901 :               if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
    1154                 :             :                                                          dest, src, len, len,
    1155                 :       20901 :                                                          false, false))
    1156                 :        1066 :                 if (warning != OPT_Wrestrict)
    1157                 :       18653 :                   return false;
    1158                 :             : 
    1159                 :       19893 :               scalar_int_mode imode;
    1160                 :       19893 :               machine_mode mode;
    1161                 :       19893 :               if (int_mode_for_size (ilen * BITS_PER_UNIT, 0).exists (&imode)
    1162                 :       19893 :                   && bitwise_mode_for_size (ilen
    1163                 :       19893 :                                             * BITS_PER_UNIT).exists (&mode)
    1164                 :       39786 :                   && known_eq (GET_MODE_BITSIZE (mode), ilen * BITS_PER_UNIT)
    1165                 :             :                   /* If the destination pointer is not aligned we must be able
    1166                 :             :                      to emit an unaligned store.  */
    1167                 :       19893 :                   && (dest_align >= GET_MODE_ALIGNMENT (mode)
    1168                 :       11544 :                       || !targetm.slow_unaligned_access (mode, dest_align)
    1169                 :           0 :                       || (optab_handler (movmisalign_optab, mode)
    1170                 :             :                           != CODE_FOR_nothing)))
    1171                 :             :                 {
    1172                 :       19893 :                   tree type = bitwise_type_for_mode (mode);
    1173                 :       19893 :                   tree srctype = type;
    1174                 :       19893 :                   tree desttype = type;
    1175                 :       19893 :                   if (src_align < GET_MODE_ALIGNMENT (mode))
    1176                 :       10842 :                     srctype = build_aligned_type (type, src_align);
    1177                 :       19893 :                   tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
    1178                 :       19893 :                   tree tem = fold_const_aggregate_ref (srcmem);
    1179                 :       19893 :                   if (tem)
    1180                 :             :                     srcmem = tem;
    1181                 :       19049 :                   else if (src_align < GET_MODE_ALIGNMENT (mode)
    1182                 :       10590 :                            && targetm.slow_unaligned_access (mode, src_align)
    1183                 :       19049 :                            && (optab_handler (movmisalign_optab, mode)
    1184                 :             :                                == CODE_FOR_nothing))
    1185                 :             :                     srcmem = NULL_TREE;
    1186                 :       19049 :                   if (srcmem)
    1187                 :             :                     {
    1188                 :       19893 :                       gimple *new_stmt;
    1189                 :       19893 :                       if (is_gimple_reg_type (TREE_TYPE (srcmem)))
    1190                 :             :                         {
    1191                 :       19893 :                           new_stmt = gimple_build_assign (NULL_TREE, srcmem);
    1192                 :       19893 :                           srcmem
    1193                 :       19893 :                             = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem),
    1194                 :             :                                                           new_stmt);
    1195                 :       19893 :                           gimple_assign_set_lhs (new_stmt, srcmem);
    1196                 :       39786 :                           gimple_set_vuse (new_stmt, gimple_vuse (stmt));
    1197                 :       19893 :                           gimple_set_location (new_stmt, loc);
    1198                 :       19893 :                           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1199                 :             :                         }
    1200                 :       19893 :                       if (dest_align < GET_MODE_ALIGNMENT (mode))
    1201                 :       11544 :                         desttype = build_aligned_type (type, dest_align);
    1202                 :       19893 :                       new_stmt
    1203                 :       19893 :                         = gimple_build_assign (fold_build2 (MEM_REF, desttype,
    1204                 :             :                                                             dest, off0),
    1205                 :             :                                                srcmem);
    1206                 :       19893 :                       gimple_move_vops (new_stmt, stmt);
    1207                 :       19893 :                       if (!lhs)
    1208                 :             :                         {
    1209                 :       17645 :                           gsi_replace (gsi, new_stmt, false);
    1210                 :       17645 :                           return true;
    1211                 :             :                         }
    1212                 :        2248 :                       gimple_set_location (new_stmt, loc);
    1213                 :        2248 :                       gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1214                 :        2248 :                       goto done;
    1215                 :             :                     }
    1216                 :             :                 }
    1217                 :             :             }
    1218                 :             :         }
    1219                 :             : 
    1220                 :      926601 :       if (code == BUILT_IN_MEMMOVE)
    1221                 :             :         {
    1222                 :             :           /* Both DEST and SRC must be pointer types.
    1223                 :             :              ??? This is what old code did.  Is the testing for pointer types
    1224                 :             :              really mandatory?
    1225                 :             : 
    1226                 :             :              If either SRC is readonly or length is 1, we can use memcpy.  */
    1227                 :      195784 :           if (!dest_align || !src_align)
    1228                 :             :             return false;
    1229                 :      195784 :           if (readonly_data_expr (src)
    1230                 :      195784 :               || (tree_fits_uhwi_p (len)
    1231                 :       32369 :                   && (MIN (src_align, dest_align) / BITS_PER_UNIT
    1232                 :       32369 :                       >= tree_to_uhwi (len))))
    1233                 :             :             {
    1234                 :      894945 :               tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    1235                 :       18199 :               if (!fn)
    1236                 :             :                 return false;
    1237                 :       18199 :               gimple_call_set_fndecl (stmt, fn);
    1238                 :       18199 :               gimple_call_set_arg (stmt, 0, dest);
    1239                 :       18199 :               gimple_call_set_arg (stmt, 1, src);
    1240                 :       18199 :               fold_stmt (gsi);
    1241                 :       18199 :               return true;
    1242                 :             :             }
    1243                 :             : 
    1244                 :             :           /* If *src and *dest can't overlap, optimize into memcpy as well.  */
    1245                 :      177585 :           if (TREE_CODE (src) == ADDR_EXPR
    1246                 :        5542 :               && TREE_CODE (dest) == ADDR_EXPR)
    1247                 :             :             {
    1248                 :        1752 :               tree src_base, dest_base, fn;
    1249                 :        1752 :               poly_int64 src_offset = 0, dest_offset = 0;
    1250                 :        1752 :               poly_uint64 maxsize;
    1251                 :             : 
    1252                 :        1752 :               srcvar = TREE_OPERAND (src, 0);
    1253                 :        1752 :               src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
    1254                 :        1752 :               if (src_base == NULL)
    1255                 :           0 :                 src_base = srcvar;
    1256                 :        1752 :               destvar = TREE_OPERAND (dest, 0);
    1257                 :        1752 :               dest_base = get_addr_base_and_unit_offset (destvar,
    1258                 :             :                                                          &dest_offset);
    1259                 :        1752 :               if (dest_base == NULL)
    1260                 :           0 :                 dest_base = destvar;
    1261                 :        1752 :               if (!poly_int_tree_p (len, &maxsize))
    1262                 :         224 :                 maxsize = -1;
    1263                 :        1752 :               if (SSA_VAR_P (src_base)
    1264                 :        1742 :                   && SSA_VAR_P (dest_base))
    1265                 :             :                 {
    1266                 :        1742 :                   if (operand_equal_p (src_base, dest_base, 0)
    1267                 :        1742 :                       && ranges_maybe_overlap_p (src_offset, maxsize,
    1268                 :             :                                                  dest_offset, maxsize))
    1269                 :             :                     return false;
    1270                 :             :                 }
    1271                 :          10 :               else if (TREE_CODE (src_base) == MEM_REF
    1272                 :           0 :                        && TREE_CODE (dest_base) == MEM_REF)
    1273                 :             :                 {
    1274                 :           0 :                   if (! operand_equal_p (TREE_OPERAND (src_base, 0),
    1275                 :           0 :                                          TREE_OPERAND (dest_base, 0), 0))
    1276                 :           0 :                     return false;
    1277                 :           0 :                   poly_offset_int full_src_offset
    1278                 :           0 :                     = mem_ref_offset (src_base) + src_offset;
    1279                 :           0 :                   poly_offset_int full_dest_offset
    1280                 :           0 :                     = mem_ref_offset (dest_base) + dest_offset;
    1281                 :           0 :                   if (ranges_maybe_overlap_p (full_src_offset, maxsize,
    1282                 :             :                                               full_dest_offset, maxsize))
    1283                 :             :                     return false;
    1284                 :           0 :                 }
    1285                 :             :               else
    1286                 :             :                 return false;
    1287                 :             : 
    1288                 :        1752 :               fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    1289                 :        1307 :               if (!fn)
    1290                 :             :                 return false;
    1291                 :        1307 :               gimple_call_set_fndecl (stmt, fn);
    1292                 :        1307 :               gimple_call_set_arg (stmt, 0, dest);
    1293                 :        1307 :               gimple_call_set_arg (stmt, 1, src);
    1294                 :        1307 :               fold_stmt (gsi);
    1295                 :        1307 :               return true;
    1296                 :             :             }
    1297                 :             : 
    1298                 :             :           /* If the destination and source do not alias optimize into
    1299                 :             :              memcpy as well.  */
    1300                 :      175833 :           if ((is_gimple_min_invariant (dest)
    1301                 :      172256 :                || TREE_CODE (dest) == SSA_NAME)
    1302                 :      331395 :               && (is_gimple_min_invariant (src)
    1303                 :      155361 :                   || TREE_CODE (src) == SSA_NAME))
    1304                 :             :             {
    1305                 :      158728 :               ao_ref destr, srcr;
    1306                 :      158728 :               ao_ref_init_from_ptr_and_size (&destr, dest, len);
    1307                 :      158728 :               ao_ref_init_from_ptr_and_size (&srcr, src, len);
    1308                 :      158728 :               if (!refs_may_alias_p_1 (&destr, &srcr, false))
    1309                 :             :                 {
    1310                 :        9799 :                   tree fn;
    1311                 :        9799 :                   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    1312                 :        9799 :                   if (!fn)
    1313                 :        9799 :                     return false;
    1314                 :        9799 :                   gimple_call_set_fndecl (stmt, fn);
    1315                 :        9799 :                   gimple_call_set_arg (stmt, 0, dest);
    1316                 :        9799 :                   gimple_call_set_arg (stmt, 1, src);
    1317                 :        9799 :                   fold_stmt (gsi);
    1318                 :        9799 :                   return true;
    1319                 :             :                 }
    1320                 :             :             }
    1321                 :             : 
    1322                 :      166034 :           return false;
    1323                 :             :         }
    1324                 :             : 
    1325                 :             :      /* Try to optimize the memcpy to memset if src and dest are addresses. */
    1326                 :      730817 :      if (code != BUILT_IN_MEMPCPY
    1327                 :      718300 :          && TREE_CODE (dest) == ADDR_EXPR
    1328                 :      146528 :          && TREE_CODE (src) == ADDR_EXPR
    1329                 :      124758 :          && TREE_CODE (len) == INTEGER_CST
    1330                 :      840136 :          && optimize_memcpy_to_memset (gsi, TREE_OPERAND (dest, 0),
    1331                 :      109319 :                                        TREE_OPERAND (src, 0), len))
    1332                 :             :         return true;
    1333                 :             : 
    1334                 :      730815 :       if (!tree_fits_shwi_p (len))
    1335                 :             :         return false;
    1336                 :      325322 :       if (!srctype
    1337                 :      325322 :           || (AGGREGATE_TYPE_P (srctype)
    1338                 :      207125 :               && TYPE_REVERSE_STORAGE_ORDER (srctype)))
    1339                 :             :         return false;
    1340                 :      325173 :       if (!desttype
    1341                 :      325173 :           || (AGGREGATE_TYPE_P (desttype)
    1342                 :      199990 :               && TYPE_REVERSE_STORAGE_ORDER (desttype)))
    1343                 :             :         return false;
    1344                 :             :       /* In the following try to find a type that is most natural to be
    1345                 :             :          used for the memcpy source and destination and that allows
    1346                 :             :          the most optimization when memcpy is turned into a plain assignment
    1347                 :             :          using that type.  In theory we could always use a char[len] type
    1348                 :             :          but that only gains us that the destination and source possibly
    1349                 :             :          no longer will have their address taken.  */
    1350                 :      325136 :       if (TREE_CODE (srctype) == ARRAY_TYPE
    1351                 :      325136 :           && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
    1352                 :      130056 :         srctype = TREE_TYPE (srctype);
    1353                 :      325136 :       if (TREE_CODE (desttype) == ARRAY_TYPE
    1354                 :      325136 :           && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
    1355                 :      117383 :         desttype = TREE_TYPE (desttype);
    1356                 :      325136 :       if (TREE_ADDRESSABLE (srctype)
    1357                 :      325124 :           || TREE_ADDRESSABLE (desttype))
    1358                 :             :         return false;
    1359                 :             : 
    1360                 :             :       /* Make sure we are not copying using a floating-point mode or
    1361                 :             :          a type whose size possibly does not match its precision.  */
    1362                 :      649490 :       if (FLOAT_MODE_P (TYPE_MODE (desttype))
    1363                 :      324109 :           || TREE_CODE (desttype) == BOOLEAN_TYPE
    1364                 :      649193 :           || TREE_CODE (desttype) == ENUMERAL_TYPE)
    1365                 :        1023 :         desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
    1366                 :      649722 :       if (FLOAT_MODE_P (TYPE_MODE (srctype))
    1367                 :      324486 :           || TREE_CODE (srctype) == BOOLEAN_TYPE
    1368                 :      649574 :           || TREE_CODE (srctype) == ENUMERAL_TYPE)
    1369                 :         642 :         srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
    1370                 :      325108 :       if (!srctype)
    1371                 :         137 :         srctype = desttype;
    1372                 :      325108 :       if (!desttype)
    1373                 :           0 :         desttype = srctype;
    1374                 :      325108 :       if (!srctype)
    1375                 :             :         return false;
    1376                 :             : 
    1377                 :      325108 :       src_align = get_pointer_alignment (src);
    1378                 :      325108 :       dest_align = get_pointer_alignment (dest);
    1379                 :             : 
    1380                 :             :       /* Choose between src and destination type for the access based
    1381                 :             :          on alignment, whether the access constitutes a register access
    1382                 :             :          and whether it may actually expose a declaration for SSA rewrite
    1383                 :             :          or SRA decomposition.  Also try to expose a string constant, we
    1384                 :             :          might be able to concatenate several of them later into a single
    1385                 :             :          string store.  */
    1386                 :      325108 :       destvar = NULL_TREE;
    1387                 :      325108 :       srcvar = NULL_TREE;
    1388                 :      325108 :       if (TREE_CODE (dest) == ADDR_EXPR
    1389                 :      125194 :           && var_decl_component_p (TREE_OPERAND (dest, 0))
    1390                 :      125190 :           && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)
    1391                 :       22410 :           && dest_align >= TYPE_ALIGN (desttype)
    1392                 :      347518 :           && (is_gimple_reg_type (desttype)
    1393                 :       21690 :               || src_align >= TYPE_ALIGN (desttype)))
    1394                 :       17418 :         destvar = fold_build2 (MEM_REF, desttype, dest, off0);
    1395                 :      307690 :       else if (TREE_CODE (src) == ADDR_EXPR
    1396                 :      238910 :                && var_decl_component_p (TREE_OPERAND (src, 0))
    1397                 :       47733 :                && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
    1398                 :        8740 :                && src_align >= TYPE_ALIGN (srctype)
    1399                 :      316412 :                && (is_gimple_reg_type (srctype)
    1400                 :        8554 :                    || dest_align >= TYPE_ALIGN (srctype)))
    1401                 :        2807 :         srcvar = fold_build2 (MEM_REF, srctype, src, off0);
    1402                 :             :       /* FIXME: Don't transform copies from strings with known original length.
    1403                 :             :          As soon as strlenopt tests that rely on it for passing are adjusted,
    1404                 :             :          this hack can be removed.  */
    1405                 :      304883 :       else if (gimple_call_alloca_for_var_p (stmt)
    1406                 :           3 :                && (srcvar = string_constant (src, &srcoff, NULL, NULL))
    1407                 :           3 :                && integer_zerop (srcoff)
    1408                 :           3 :                && tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (srcvar)), len)
    1409                 :      304886 :                && dest_align >= TYPE_ALIGN (TREE_TYPE (srcvar)))
    1410                 :           3 :         srctype = TREE_TYPE (srcvar);
    1411                 :             :       else
    1412                 :      304880 :         return false;
    1413                 :             : 
    1414                 :             :       /* Now that we chose an access type express the other side in
    1415                 :             :          terms of it if the target allows that with respect to alignment
    1416                 :             :          constraints.  */
    1417                 :       20228 :       if (srcvar == NULL_TREE)
    1418                 :             :         {
    1419                 :       17418 :           if (src_align >= TYPE_ALIGN (desttype))
    1420                 :       17404 :             srcvar = fold_build2 (MEM_REF, desttype, src, off0);
    1421                 :             :           else
    1422                 :             :             {
    1423                 :          14 :               enum machine_mode mode = TYPE_MODE (desttype);
    1424                 :          14 :               if ((mode == BLKmode && STRICT_ALIGNMENT)
    1425                 :          14 :                   || (targetm.slow_unaligned_access (mode, src_align)
    1426                 :          14 :                       && (optab_handler (movmisalign_optab, mode)
    1427                 :             :                           == CODE_FOR_nothing)))
    1428                 :             :                 return false;
    1429                 :          14 :               srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
    1430                 :             :                                             src_align);
    1431                 :          14 :               srcvar = fold_build2 (MEM_REF, srctype, src, off0);
    1432                 :             :             }
    1433                 :             :         }
    1434                 :        2810 :       else if (destvar == NULL_TREE)
    1435                 :             :         {
    1436                 :        2810 :           if (dest_align >= TYPE_ALIGN (srctype))
    1437                 :        2810 :             destvar = fold_build2 (MEM_REF, srctype, dest, off0);
    1438                 :             :           else
    1439                 :             :             {
    1440                 :           0 :               enum machine_mode mode = TYPE_MODE (srctype);
    1441                 :           0 :               if ((mode == BLKmode && STRICT_ALIGNMENT)
    1442                 :           0 :                   || (targetm.slow_unaligned_access (mode, dest_align)
    1443                 :           0 :                       && (optab_handler (movmisalign_optab, mode)
    1444                 :             :                           == CODE_FOR_nothing)))
    1445                 :             :                 return false;
    1446                 :           0 :               desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
    1447                 :             :                                              dest_align);
    1448                 :           0 :               destvar = fold_build2 (MEM_REF, desttype, dest, off0);
    1449                 :             :             }
    1450                 :             :         }
    1451                 :             : 
    1452                 :             :       /* Same as above, detect out-of-bounds accesses without issuing
    1453                 :             :          warnings.  Avoid folding out-of-bounds copies but to avoid
    1454                 :             :          false positives for unreachable code defer warning until
    1455                 :             :          after DCE has worked its magic.
    1456                 :             :          -Wrestrict is still diagnosed.  */
    1457                 :       20228 :       if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
    1458                 :             :                                                  dest, src, len, len,
    1459                 :       20228 :                                                  false, false))
    1460                 :         133 :         if (warning != OPT_Wrestrict)
    1461                 :             :           return false;
    1462                 :             : 
    1463                 :       20103 :       gimple *new_stmt;
    1464                 :       20103 :       if (is_gimple_reg_type (TREE_TYPE (srcvar)))
    1465                 :             :         {
    1466                 :         843 :           tree tem = fold_const_aggregate_ref (srcvar);
    1467                 :         843 :           if (tem)
    1468                 :         826 :             srcvar = tem;
    1469                 :         843 :           if (! is_gimple_min_invariant (srcvar))
    1470                 :             :             {
    1471                 :          17 :               new_stmt = gimple_build_assign (NULL_TREE, srcvar);
    1472                 :          17 :               srcvar = create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar),
    1473                 :             :                                                    new_stmt);
    1474                 :          17 :               gimple_assign_set_lhs (new_stmt, srcvar);
    1475                 :          34 :               gimple_set_vuse (new_stmt, gimple_vuse (stmt));
    1476                 :          17 :               gimple_set_location (new_stmt, loc);
    1477                 :          17 :               gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1478                 :             :             }
    1479                 :         843 :           new_stmt = gimple_build_assign (destvar, srcvar);
    1480                 :         843 :           goto set_vop_and_replace;
    1481                 :             :         }
    1482                 :             : 
    1483                 :             :       /* We get an aggregate copy.  If the source is a STRING_CST, then
    1484                 :             :          directly use its type to perform the copy.  */
    1485                 :       19260 :       if (TREE_CODE (srcvar) == STRING_CST)
    1486                 :             :           desttype = srctype;
    1487                 :             : 
    1488                 :             :       /* Or else, use an unsigned char[] type to perform the copy in order
    1489                 :             :          to preserve padding and to avoid any issues with TREE_ADDRESSABLE
    1490                 :             :          types or float modes behavior on copying.  */
    1491                 :             :       else
    1492                 :             :         {
    1493                 :       38514 :           desttype = build_array_type_nelts (unsigned_char_type_node,
    1494                 :       19257 :                                              tree_to_uhwi (len));
    1495                 :       19257 :           srctype = desttype;
    1496                 :       19257 :           if (src_align > TYPE_ALIGN (srctype))
    1497                 :       11903 :             srctype = build_aligned_type (srctype, src_align);
    1498                 :       19257 :           srcvar = fold_build2 (MEM_REF, srctype, src, off0);
    1499                 :             :         }
    1500                 :             : 
    1501                 :       19260 :       if (dest_align > TYPE_ALIGN (desttype))
    1502                 :       12540 :         desttype = build_aligned_type (desttype, dest_align);
    1503                 :       19260 :       destvar = fold_build2 (MEM_REF, desttype, dest, off0);
    1504                 :       19260 :       new_stmt = gimple_build_assign (destvar, srcvar);
    1505                 :             : 
    1506                 :       20103 : set_vop_and_replace:
    1507                 :       20103 :       gimple_move_vops (new_stmt, stmt);
    1508                 :       20103 :       if (!lhs)
    1509                 :             :         {
    1510                 :       19628 :           gsi_replace (gsi, new_stmt, false);
    1511                 :       19628 :           return true;
    1512                 :             :         }
    1513                 :         475 :       gimple_set_location (new_stmt, loc);
    1514                 :         475 :       gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1515                 :             :     }
    1516                 :             : 
    1517                 :        2744 : done:
    1518                 :        2744 :   gimple_seq stmts = NULL;
    1519                 :        2744 :   if (code == BUILT_IN_MEMCPY || code == BUILT_IN_MEMMOVE)
    1520                 :        2744 :     len = NULL_TREE;
    1521                 :         197 :   else if (code == BUILT_IN_MEMPCPY)
    1522                 :             :     {
    1523                 :         197 :       len = gimple_convert_to_ptrofftype (&stmts, loc, len);
    1524                 :         197 :       dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
    1525                 :         197 :                            TREE_TYPE (dest), dest, len);
    1526                 :             :     }
    1527                 :             :   else
    1528                 :           0 :     gcc_unreachable ();
    1529                 :             : 
    1530                 :        2744 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    1531                 :        2744 :   gimple *repl = gimple_build_assign (lhs, dest);
    1532                 :        2744 :   gsi_replace (gsi, repl, false);
    1533                 :        2744 :   return true;
    1534                 :             : }
    1535                 :             : 
    1536                 :             : /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
    1537                 :             :    to built-in memcmp (a, b, len).  */
    1538                 :             : 
    1539                 :             : static bool
    1540                 :         148 : gimple_fold_builtin_bcmp (gimple_stmt_iterator *gsi)
    1541                 :             : {
    1542                 :         148 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMCMP);
    1543                 :             : 
    1544                 :         148 :   if (!fn)
    1545                 :             :     return false;
    1546                 :             : 
    1547                 :             :   /* Transform bcmp (a, b, len) into memcmp (a, b, len).  */
    1548                 :             : 
    1549                 :         148 :   gimple *stmt = gsi_stmt (*gsi);
    1550                 :         296 :   if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
    1551                 :             :     return false;
    1552                 :         148 :   tree a = gimple_call_arg (stmt, 0);
    1553                 :         148 :   tree b = gimple_call_arg (stmt, 1);
    1554                 :         148 :   tree len = gimple_call_arg (stmt, 2);
    1555                 :             : 
    1556                 :         148 :   gimple *repl = gimple_build_call (fn, 3, a, b, len);
    1557                 :         148 :   replace_call_with_call_and_fold (gsi, repl);
    1558                 :             : 
    1559                 :         148 :   return true;
    1560                 :             : }
    1561                 :             : 
    1562                 :             : /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
    1563                 :             :    to built-in memmove (dest, src, len).  */
    1564                 :             : 
    1565                 :             : static bool
    1566                 :         367 : gimple_fold_builtin_bcopy (gimple_stmt_iterator *gsi)
    1567                 :             : {
    1568                 :         367 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMMOVE);
    1569                 :             : 
    1570                 :         367 :   if (!fn)
    1571                 :             :     return false;
    1572                 :             : 
    1573                 :             :   /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
    1574                 :             :      it's quivalent to memmove (not memcpy).  Transform bcopy (src, dest,
    1575                 :             :      len) into memmove (dest, src, len).  */
    1576                 :             : 
    1577                 :         367 :   gimple *stmt = gsi_stmt (*gsi);
    1578                 :         734 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1579                 :             :     return false;
    1580                 :         367 :   tree src = gimple_call_arg (stmt, 0);
    1581                 :         367 :   tree dest = gimple_call_arg (stmt, 1);
    1582                 :         367 :   tree len = gimple_call_arg (stmt, 2);
    1583                 :             : 
    1584                 :         367 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    1585                 :         367 :   gimple_call_set_fntype (as_a <gcall *> (stmt), TREE_TYPE (fn));
    1586                 :         367 :   replace_call_with_call_and_fold (gsi, repl);
    1587                 :             : 
    1588                 :         367 :   return true;
    1589                 :             : }
    1590                 :             : 
    1591                 :             : /* Transform a call to built-in bzero (dest, len) at *GSI into one
    1592                 :             :    to built-in memset (dest, 0, len).  */
    1593                 :             : 
    1594                 :             : static bool
    1595                 :         250 : gimple_fold_builtin_bzero (gimple_stmt_iterator *gsi)
    1596                 :             : {
    1597                 :         250 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
    1598                 :             : 
    1599                 :         250 :   if (!fn)
    1600                 :             :     return false;
    1601                 :             : 
    1602                 :             :   /* Transform bzero (dest, len) into memset (dest, 0, len).  */
    1603                 :             : 
    1604                 :         250 :   gimple *stmt = gsi_stmt (*gsi);
    1605                 :         500 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1606                 :             :     return false;
    1607                 :         250 :   tree dest = gimple_call_arg (stmt, 0);
    1608                 :         250 :   tree len = gimple_call_arg (stmt, 1);
    1609                 :             : 
    1610                 :         250 :   gimple_seq seq = NULL;
    1611                 :         250 :   gimple *repl = gimple_build_call (fn, 3, dest, integer_zero_node, len);
    1612                 :         250 :   gimple_seq_add_stmt_without_update (&seq, repl);
    1613                 :         250 :   gsi_replace_with_seq_vops (gsi, seq);
    1614                 :         250 :   fold_stmt (gsi);
    1615                 :             : 
    1616                 :         250 :   return true;
    1617                 :             : }
    1618                 :             : 
    1619                 :             : /* Fold function call to builtin memset or bzero at *GSI setting the
    1620                 :             :    memory of size LEN to VAL.  Return whether a simplification was made.  */
    1621                 :             : 
    1622                 :             : static bool
    1623                 :      329646 : gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
    1624                 :             : {
    1625                 :      329646 :   gimple *stmt = gsi_stmt (*gsi);
    1626                 :      329646 :   tree etype;
    1627                 :      329646 :   unsigned HOST_WIDE_INT length, cval;
    1628                 :             : 
    1629                 :             :   /* If the LEN parameter is zero, return DEST.  */
    1630                 :      329646 :   if (integer_zerop (len))
    1631                 :             :     {
    1632                 :         811 :       replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
    1633                 :         811 :       return true;
    1634                 :             :     }
    1635                 :             : 
    1636                 :      984749 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1637                 :             :     return false;
    1638                 :             : 
    1639                 :      328835 :   if (! tree_fits_uhwi_p (len))
    1640                 :             :     return false;
    1641                 :             : 
    1642                 :      213515 :   if (TREE_CODE (c) != INTEGER_CST)
    1643                 :             :     return false;
    1644                 :             : 
    1645                 :      207401 :   tree dest = gimple_call_arg (stmt, 0);
    1646                 :      207401 :   tree var = dest;
    1647                 :      207401 :   if (TREE_CODE (var) != ADDR_EXPR)
    1648                 :             :     return false;
    1649                 :             : 
    1650                 :      166752 :   var = TREE_OPERAND (var, 0);
    1651                 :      166752 :   if (TREE_THIS_VOLATILE (var))
    1652                 :             :     return false;
    1653                 :             : 
    1654                 :      166709 :   etype = TREE_TYPE (var);
    1655                 :      166709 :   if (TREE_CODE (etype) == ARRAY_TYPE)
    1656                 :       89095 :     etype = TREE_TYPE (etype);
    1657                 :             : 
    1658                 :      166709 :   if ((!INTEGRAL_TYPE_P (etype)
    1659                 :      100763 :        && !POINTER_TYPE_P (etype))
    1660                 :       66560 :       || TREE_CODE (etype) == BITINT_TYPE)
    1661                 :             :     return false;
    1662                 :             : 
    1663                 :       66515 :   if (! var_decl_component_p (var))
    1664                 :             :     return false;
    1665                 :             : 
    1666                 :       66515 :   length = tree_to_uhwi (len);
    1667                 :       66515 :   if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype)) != length
    1668                 :        1756 :       || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (etype))
    1669                 :        3512 :           != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (etype)))
    1670                 :       68271 :       || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
    1671                 :       64759 :     return false;
    1672                 :             : 
    1673                 :        1756 :   if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
    1674                 :             :     return false;
    1675                 :             : 
    1676                 :        1756 :   if (!type_has_mode_precision_p (etype))
    1677                 :           7 :     etype = lang_hooks.types.type_for_mode (SCALAR_INT_TYPE_MODE (etype),
    1678                 :           7 :                                             TYPE_UNSIGNED (etype));
    1679                 :             : 
    1680                 :        1756 :   if (integer_zerop (c))
    1681                 :             :     cval = 0;
    1682                 :             :   else
    1683                 :             :     {
    1684                 :         334 :       if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
    1685                 :             :         return NULL_TREE;
    1686                 :             : 
    1687                 :         334 :       cval = TREE_INT_CST_LOW (c);
    1688                 :         334 :       cval &= 0xff;
    1689                 :         334 :       cval |= cval << 8;
    1690                 :         334 :       cval |= cval << 16;
    1691                 :         334 :       cval |= (cval << 31) << 1;
    1692                 :             :     }
    1693                 :             : 
    1694                 :        1756 :   var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
    1695                 :        1756 :   gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
    1696                 :        1756 :   gimple_move_vops (store, stmt);
    1697                 :        1756 :   gimple_set_location (store, gimple_location (stmt));
    1698                 :        1756 :   gsi_insert_before (gsi, store, GSI_SAME_STMT);
    1699                 :        1756 :   if (gimple_call_lhs (stmt))
    1700                 :             :     {
    1701                 :           2 :       gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
    1702                 :           2 :       gsi_replace (gsi, asgn, false);
    1703                 :             :     }
    1704                 :             :   else
    1705                 :             :     {
    1706                 :        1754 :       gimple_stmt_iterator gsi2 = *gsi;
    1707                 :        1754 :       gsi_prev (gsi);
    1708                 :        1754 :       gsi_remove (&gsi2, true);
    1709                 :             :     }
    1710                 :             : 
    1711                 :             :   return true;
    1712                 :             : }
    1713                 :             : 
    1714                 :             : /* Helper of get_range_strlen for ARG that is not an SSA_NAME.  */
    1715                 :             : 
    1716                 :             : static bool
    1717                 :      621592 : get_range_strlen_tree (tree arg, bitmap visited, strlen_range_kind rkind,
    1718                 :             :                        c_strlen_data *pdata, unsigned eltsize)
    1719                 :             : {
    1720                 :      621592 :   gcc_assert (TREE_CODE (arg) != SSA_NAME);
    1721                 :             : 
    1722                 :             :   /* The length computed by this invocation of the function.  */
    1723                 :      621592 :   tree val = NULL_TREE;
    1724                 :             : 
    1725                 :             :   /* True if VAL is an optimistic (tight) bound determined from
    1726                 :             :      the size of the character array in which the string may be
    1727                 :             :      stored.  In that case, the computed VAL is used to set
    1728                 :             :      PDATA->MAXBOUND.  */
    1729                 :      621592 :   bool tight_bound = false;
    1730                 :             : 
    1731                 :             :   /* We can end up with &(*iftmp_1)[0] here as well, so handle it.  */
    1732                 :      621592 :   if (TREE_CODE (arg) == ADDR_EXPR
    1733                 :      621592 :       && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
    1734                 :             :     {
    1735                 :       34231 :       tree op = TREE_OPERAND (arg, 0);
    1736                 :       34231 :       if (integer_zerop (TREE_OPERAND (op, 1)))
    1737                 :             :         {
    1738                 :       13162 :           tree aop0 = TREE_OPERAND (op, 0);
    1739                 :       13162 :           if (TREE_CODE (aop0) == INDIRECT_REF
    1740                 :       13162 :               && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
    1741                 :           0 :             return get_range_strlen (TREE_OPERAND (aop0, 0), visited, rkind,
    1742                 :           0 :                                      pdata, eltsize);
    1743                 :             :         }
    1744                 :       21069 :       else if (TREE_CODE (TREE_OPERAND (op, 0)) == COMPONENT_REF
    1745                 :       21069 :                && rkind == SRK_LENRANGE)
    1746                 :             :         {
    1747                 :             :           /* Fail if an array is the last member of a struct object
    1748                 :             :              since it could be treated as a (fake) flexible array
    1749                 :             :              member.  */
    1750                 :        5082 :           tree idx = TREE_OPERAND (op, 1);
    1751                 :             : 
    1752                 :        5082 :           arg = TREE_OPERAND (op, 0);
    1753                 :        5082 :           tree optype = TREE_TYPE (arg);
    1754                 :        5082 :           if (tree dom = TYPE_DOMAIN (optype))
    1755                 :        5082 :             if (tree bound = TYPE_MAX_VALUE (dom))
    1756                 :        5082 :               if (TREE_CODE (bound) == INTEGER_CST
    1757                 :        5082 :                   && TREE_CODE (idx) == INTEGER_CST
    1758                 :        8594 :                   && tree_int_cst_lt (bound, idx))
    1759                 :             :                 return false;
    1760                 :             :         }
    1761                 :             :     }
    1762                 :             : 
    1763                 :      621376 :   if (rkind == SRK_INT_VALUE)
    1764                 :             :     {
    1765                 :             :       /* We are computing the maximum value (not string length).  */
    1766                 :       26290 :       val = arg;
    1767                 :       26290 :       if (TREE_CODE (val) != INTEGER_CST
    1768                 :       26290 :           || tree_int_cst_sgn (val) < 0)
    1769                 :        2785 :         return false;
    1770                 :             :     }
    1771                 :             :   else
    1772                 :             :     {
    1773                 :      595086 :       c_strlen_data lendata = { };
    1774                 :      595086 :       val = c_strlen (arg, 1, &lendata, eltsize);
    1775                 :             : 
    1776                 :      595086 :       if (!val && lendata.decl)
    1777                 :             :         {
    1778                 :             :           /* ARG refers to an unterminated const character array.
    1779                 :             :              DATA.DECL with size DATA.LEN.  */
    1780                 :        4559 :           val = lendata.minlen;
    1781                 :        4559 :           pdata->decl = lendata.decl;
    1782                 :             :         }
    1783                 :             :     }
    1784                 :             : 
    1785                 :             :   /* Set if VAL represents the maximum length based on array size (set
    1786                 :             :      when exact length cannot be determined).  */
    1787                 :      618591 :   bool maxbound = false;
    1788                 :             : 
    1789                 :      618591 :   if (!val && rkind == SRK_LENRANGE)
    1790                 :             :     {
    1791                 :      387987 :       if (TREE_CODE (arg) == ADDR_EXPR)
    1792                 :      156162 :         return get_range_strlen (TREE_OPERAND (arg, 0), visited, rkind,
    1793                 :      156162 :                                  pdata, eltsize);
    1794                 :             : 
    1795                 :      231825 :       if (TREE_CODE (arg) == ARRAY_REF)
    1796                 :             :         {
    1797                 :       29510 :           tree optype = TREE_TYPE (TREE_OPERAND (arg, 0));
    1798                 :             : 
    1799                 :             :           /* Determine the "innermost" array type.  */
    1800                 :       29510 :           while (TREE_CODE (optype) == ARRAY_TYPE
    1801                 :       36907 :                  && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
    1802                 :        7397 :             optype = TREE_TYPE (optype);
    1803                 :             : 
    1804                 :             :           /* Avoid arrays of pointers.  */
    1805                 :       29510 :           tree eltype = TREE_TYPE (optype);
    1806                 :       29510 :           if (TREE_CODE (optype) != ARRAY_TYPE
    1807                 :       29510 :               || !INTEGRAL_TYPE_P (eltype))
    1808                 :             :             return false;
    1809                 :             : 
    1810                 :             :           /* Fail when the array bound is unknown or zero.  */
    1811                 :       16136 :           val = TYPE_SIZE_UNIT (optype);
    1812                 :       16136 :           if (!val
    1813                 :       16064 :               || TREE_CODE (val) != INTEGER_CST
    1814                 :       32168 :               || integer_zerop (val))
    1815                 :         113 :             return false;
    1816                 :             : 
    1817                 :       16023 :           val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
    1818                 :             :                               integer_one_node);
    1819                 :             : 
    1820                 :             :           /* Set the minimum size to zero since the string in
    1821                 :             :              the array could have zero length.  */
    1822                 :       16023 :           pdata->minlen = ssize_int (0);
    1823                 :             : 
    1824                 :       16023 :           tight_bound = true;
    1825                 :             :         }
    1826                 :      202315 :       else if (TREE_CODE (arg) == COMPONENT_REF
    1827                 :      202315 :                && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1)))
    1828                 :             :                    == ARRAY_TYPE))
    1829                 :             :         {
    1830                 :             :           /* Use the type of the member array to determine the upper
    1831                 :             :              bound on the length of the array.  This may be overly
    1832                 :             :              optimistic if the array itself isn't NUL-terminated and
    1833                 :             :              the caller relies on the subsequent member to contain
    1834                 :             :              the NUL but that would only be considered valid if
    1835                 :             :              the array were the last member of a struct.  */
    1836                 :             : 
    1837                 :       10486 :           tree fld = TREE_OPERAND (arg, 1);
    1838                 :             : 
    1839                 :       10486 :           tree optype = TREE_TYPE (fld);
    1840                 :             : 
    1841                 :             :           /* Determine the "innermost" array type.  */
    1842                 :       10486 :           while (TREE_CODE (optype) == ARRAY_TYPE
    1843                 :       11134 :                  && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
    1844                 :         648 :             optype = TREE_TYPE (optype);
    1845                 :             : 
    1846                 :             :           /* Fail when the array bound is unknown or zero.  */
    1847                 :       10486 :           val = TYPE_SIZE_UNIT (optype);
    1848                 :       10486 :           if (!val
    1849                 :       10230 :               || TREE_CODE (val) != INTEGER_CST
    1850                 :       20681 :               || integer_zerop (val))
    1851                 :         376 :             return false;
    1852                 :       10110 :           val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
    1853                 :             :                              integer_one_node);
    1854                 :             : 
    1855                 :             :           /* Set the minimum size to zero since the string in
    1856                 :             :              the array could have zero length.  */
    1857                 :       10110 :           pdata->minlen = ssize_int (0);
    1858                 :             : 
    1859                 :             :           /* The array size determined above is an optimistic bound
    1860                 :             :              on the length.  If the array isn't nul-terminated the
    1861                 :             :              length computed by the library function would be greater.
    1862                 :             :              Even though using strlen to cross the subobject boundary
    1863                 :             :              is undefined, avoid drawing conclusions from the member
    1864                 :             :              type about the length here.  */
    1865                 :       10110 :           tight_bound = true;
    1866                 :             :         }
    1867                 :      191829 :       else if (TREE_CODE (arg) == MEM_REF
    1868                 :       28262 :                && TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
    1869                 :        4318 :                && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == INTEGER_TYPE
    1870                 :      195625 :                && TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
    1871                 :             :         {
    1872                 :             :           /* Handle a MEM_REF into a DECL accessing an array of integers,
    1873                 :             :              being conservative about references to extern structures with
    1874                 :             :              flexible array members that can be initialized to arbitrary
    1875                 :             :              numbers of elements as an extension (static structs are okay).  */
    1876                 :        3796 :           tree ref = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
    1877                 :        3796 :           if ((TREE_CODE (ref) == PARM_DECL || VAR_P (ref))
    1878                 :        7578 :               && (decl_binds_to_current_def_p (ref)
    1879                 :         457 :                   || !array_ref_flexible_size_p (arg)))
    1880                 :             :             {
    1881                 :             :               /* Fail if the offset is out of bounds.  Such accesses
    1882                 :             :                  should be diagnosed at some point.  */
    1883                 :        3657 :               val = DECL_SIZE_UNIT (ref);
    1884                 :        3657 :               if (!val
    1885                 :        3485 :                   || TREE_CODE (val) != INTEGER_CST
    1886                 :        7142 :                   || integer_zerop (val))
    1887                 :         386 :                 return false;
    1888                 :             : 
    1889                 :        3483 :               poly_offset_int psiz = wi::to_offset (val);
    1890                 :        3483 :               poly_offset_int poff = mem_ref_offset (arg);
    1891                 :        3483 :               if (known_le (psiz, poff))
    1892                 :             :                 return false;
    1893                 :             : 
    1894                 :        3271 :               pdata->minlen = ssize_int (0);
    1895                 :             : 
    1896                 :             :               /* Subtract the offset and one for the terminating nul.  */
    1897                 :        3271 :               psiz -= poff;
    1898                 :        3271 :               psiz -= 1;
    1899                 :        3271 :               val = wide_int_to_tree (TREE_TYPE (val), psiz);
    1900                 :             :               /* Since VAL reflects the size of a declared object
    1901                 :             :                  rather the type of the access it is not a tight bound.  */
    1902                 :             :             }
    1903                 :             :         }
    1904                 :      188033 :       else if (TREE_CODE (arg) == PARM_DECL || VAR_P (arg))
    1905                 :             :         {
    1906                 :             :           /* Avoid handling pointers to arrays.  GCC might misuse
    1907                 :             :              a pointer to an array of one bound to point to an array
    1908                 :             :              object of a greater bound.  */
    1909                 :      135595 :           tree argtype = TREE_TYPE (arg);
    1910                 :      135595 :           if (TREE_CODE (argtype) == ARRAY_TYPE)
    1911                 :             :             {
    1912                 :       46604 :               val = TYPE_SIZE_UNIT (argtype);
    1913                 :       46604 :               if (!val
    1914                 :       45810 :                   || TREE_CODE (val) != INTEGER_CST
    1915                 :       92414 :                   || integer_zerop (val))
    1916                 :         911 :                 return false;
    1917                 :       45693 :               val = wide_int_to_tree (TREE_TYPE (val),
    1918                 :       45693 :                                       wi::sub (wi::to_wide (val), 1));
    1919                 :             : 
    1920                 :             :               /* Set the minimum size to zero since the string in
    1921                 :             :                  the array could have zero length.  */
    1922                 :       45693 :               pdata->minlen = ssize_int (0);
    1923                 :             :             }
    1924                 :             :         }
    1925                 :             :       maxbound = true;
    1926                 :             :     }
    1927                 :             : 
    1928                 :      447269 :   if (!val)
    1929                 :             :     return false;
    1930                 :             : 
    1931                 :             :   /* Adjust the lower bound on the string length as necessary.  */
    1932                 :      279901 :   if (!pdata->minlen
    1933                 :      279901 :       || (rkind != SRK_STRLEN
    1934                 :       80989 :           && TREE_CODE (pdata->minlen) == INTEGER_CST
    1935                 :       80989 :           && TREE_CODE (val) == INTEGER_CST
    1936                 :       80984 :           && tree_int_cst_lt (val, pdata->minlen)))
    1937                 :      198620 :     pdata->minlen = val;
    1938                 :             : 
    1939                 :      279901 :   if (pdata->maxbound && TREE_CODE (pdata->maxbound) == INTEGER_CST)
    1940                 :             :     {
    1941                 :             :       /* Adjust the tighter (more optimistic) string length bound
    1942                 :             :          if necessary and proceed to adjust the more conservative
    1943                 :             :          bound.  */
    1944                 :        1552 :       if (TREE_CODE (val) == INTEGER_CST)
    1945                 :             :         {
    1946                 :        1552 :           if (tree_int_cst_lt (pdata->maxbound, val))
    1947                 :         612 :             pdata->maxbound = val;
    1948                 :             :         }
    1949                 :             :       else
    1950                 :           0 :         pdata->maxbound = val;
    1951                 :             :     }
    1952                 :      278349 :   else if (pdata->maxbound || maxbound)
    1953                 :             :     /* Set PDATA->MAXBOUND only if it either isn't INTEGER_CST or
    1954                 :             :        if VAL corresponds to the maximum length determined based
    1955                 :             :        on the type of the object.  */
    1956                 :       78351 :     pdata->maxbound = val;
    1957                 :             : 
    1958                 :      279901 :   if (tight_bound)
    1959                 :             :     {
    1960                 :             :       /* VAL computed above represents an optimistically tight bound
    1961                 :             :          on the length of the string based on the referenced object's
    1962                 :             :          or subobject's type.  Determine the conservative upper bound
    1963                 :             :          based on the enclosing object's size if possible.  */
    1964                 :       26133 :       if (rkind == SRK_LENRANGE)
    1965                 :             :         {
    1966                 :       26133 :           poly_int64 offset;
    1967                 :       26133 :           tree base = get_addr_base_and_unit_offset (arg, &offset);
    1968                 :       26133 :           if (!base)
    1969                 :             :             {
    1970                 :             :               /* When the call above fails due to a non-constant offset
    1971                 :             :                  assume the offset is zero and use the size of the whole
    1972                 :             :                  enclosing object instead.  */
    1973                 :        7832 :               base = get_base_address (arg);
    1974                 :        7832 :               offset = 0;
    1975                 :             :             }
    1976                 :             :           /* If the base object is a pointer no upper bound on the length
    1977                 :             :              can be determined.  Otherwise the maximum length is equal to
    1978                 :             :              the size of the enclosing object minus the offset of
    1979                 :             :              the referenced subobject minus 1 (for the terminating nul).  */
    1980                 :       26133 :           tree type = TREE_TYPE (base);
    1981                 :       26133 :           if (TREE_CODE (type) == POINTER_TYPE
    1982                 :       26129 :               || (TREE_CODE (base) != PARM_DECL && !VAR_P (base))
    1983                 :       45898 :               || !(val = DECL_SIZE_UNIT (base)))
    1984                 :        7621 :             val = build_all_ones_cst (size_type_node);
    1985                 :             :           else
    1986                 :             :             {
    1987                 :       18512 :               val = DECL_SIZE_UNIT (base);
    1988                 :       18512 :               val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
    1989                 :             :                                  size_int (offset + 1));
    1990                 :             :             }
    1991                 :             :         }
    1992                 :             :       else
    1993                 :             :         return false;
    1994                 :             :     }
    1995                 :             : 
    1996                 :      279901 :   if (pdata->maxlen)
    1997                 :             :     {
    1998                 :             :       /* Adjust the more conservative bound if possible/necessary
    1999                 :             :          and fail otherwise.  */
    2000                 :        9587 :       if (rkind != SRK_STRLEN)
    2001                 :             :         {
    2002                 :        8557 :           if (TREE_CODE (pdata->maxlen) != INTEGER_CST
    2003                 :        8557 :               || TREE_CODE (val) != INTEGER_CST)
    2004                 :             :             return false;
    2005                 :             : 
    2006                 :        8552 :           if (tree_int_cst_lt (pdata->maxlen, val))
    2007                 :        1507 :             pdata->maxlen = val;
    2008                 :        8552 :           return true;
    2009                 :             :         }
    2010                 :        1030 :       else if (simple_cst_equal (val, pdata->maxlen) != 1)
    2011                 :             :         {
    2012                 :             :           /* Fail if the length of this ARG is different from that
    2013                 :             :              previously determined from another ARG.  */
    2014                 :             :           return false;
    2015                 :             :         }
    2016                 :             :     }
    2017                 :             : 
    2018                 :      270440 :   pdata->maxlen = val;
    2019                 :      270440 :   return rkind == SRK_LENRANGE || !integer_all_onesp (val);
    2020                 :             : }
    2021                 :             : 
    2022                 :             : /* For an ARG referencing one or more strings, try to obtain the range
    2023                 :             :    of their lengths, or the size of the largest array ARG referes to if
    2024                 :             :    the range of lengths cannot be determined, and store all in *PDATA.
    2025                 :             :    For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
    2026                 :             :    the maximum constant value.
    2027                 :             :    If ARG is an SSA_NAME, follow its use-def chains.  When RKIND ==
    2028                 :             :    SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
    2029                 :             :    length or if we are unable to determine the length, return false.
    2030                 :             :    VISITED is a bitmap of visited variables.
    2031                 :             :    RKIND determines the kind of value or range to obtain (see
    2032                 :             :    strlen_range_kind).
    2033                 :             :    Set PDATA->DECL if ARG refers to an unterminated constant array.
    2034                 :             :    On input, set ELTSIZE to 1 for normal single byte character strings,
    2035                 :             :    and either 2 or 4 for wide characer strings (the size of wchar_t).
    2036                 :             :    Return true if *PDATA was successfully populated and false otherwise.  */
    2037                 :             : 
    2038                 :             : static bool
    2039                 :     1532270 : get_range_strlen (tree arg, bitmap visited,
    2040                 :             :                   strlen_range_kind rkind,
    2041                 :             :                   c_strlen_data *pdata, unsigned eltsize)
    2042                 :             : {
    2043                 :             : 
    2044                 :     1626750 :   if (TREE_CODE (arg) != SSA_NAME)
    2045                 :      621592 :     return get_range_strlen_tree (arg, visited, rkind, pdata, eltsize);
    2046                 :             : 
    2047                 :             :   /* If ARG is registered for SSA update we cannot look at its defining
    2048                 :             :      statement.  */
    2049                 :     1005158 :   if (name_registered_for_update_p (arg))
    2050                 :             :     return false;
    2051                 :             : 
    2052                 :             :   /* If we were already here, break the infinite cycle.  */
    2053                 :     1005158 :   if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg)))
    2054                 :             :     return true;
    2055                 :             : 
    2056                 :     1002688 :   tree var = arg;
    2057                 :     1002688 :   gimple *def_stmt = SSA_NAME_DEF_STMT (var);
    2058                 :             : 
    2059                 :     1002688 :   switch (gimple_code (def_stmt))
    2060                 :             :     {
    2061                 :      142527 :       case GIMPLE_ASSIGN:
    2062                 :             :         /* The RHS of the statement defining VAR must either have a
    2063                 :             :            constant length or come from another SSA_NAME with a constant
    2064                 :             :            length.  */
    2065                 :      142527 :         if (gimple_assign_single_p (def_stmt)
    2066                 :      142527 :             || gimple_assign_unary_nop_p (def_stmt))
    2067                 :             :           {
    2068                 :       94480 :             tree rhs = gimple_assign_rhs1 (def_stmt);
    2069                 :       94480 :             return get_range_strlen (rhs, visited, rkind, pdata, eltsize);
    2070                 :             :           }
    2071                 :       48047 :         else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
    2072                 :             :           {
    2073                 :         246 :             tree ops[2] = { gimple_assign_rhs2 (def_stmt),
    2074                 :         246 :                             gimple_assign_rhs3 (def_stmt) };
    2075                 :             : 
    2076                 :         738 :             for (unsigned int i = 0; i < 2; i++)
    2077                 :         492 :               if (!get_range_strlen (ops[i], visited, rkind, pdata, eltsize))
    2078                 :             :                 {
    2079                 :          28 :                   if (rkind != SRK_LENRANGE)
    2080                 :             :                     return false;
    2081                 :             :                   /* Set the upper bound to the maximum to prevent
    2082                 :             :                      it from being adjusted in the next iteration but
    2083                 :             :                      leave MINLEN and the more conservative MAXBOUND
    2084                 :             :                      determined so far alone (or leave them null if
    2085                 :             :                      they haven't been set yet).  That the MINLEN is
    2086                 :             :                      in fact zero can be determined from MAXLEN being
    2087                 :             :                      unbounded but the discovered minimum is used for
    2088                 :             :                      diagnostics.  */
    2089                 :          28 :                   pdata->maxlen = build_all_ones_cst (size_type_node);
    2090                 :             :                 }
    2091                 :             :             return true;
    2092                 :             :           }
    2093                 :             :         return false;
    2094                 :             : 
    2095                 :             :       case GIMPLE_PHI:
    2096                 :             :         /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
    2097                 :             :            must have a constant length.  */
    2098                 :       71990 :         for (unsigned i = 0; i < gimple_phi_num_args (def_stmt); i++)
    2099                 :             :           {
    2100                 :       50002 :             tree arg = gimple_phi_arg (def_stmt, i)->def;
    2101                 :             : 
    2102                 :             :             /* If this PHI has itself as an argument, we cannot
    2103                 :             :                determine the string length of this argument.  However,
    2104                 :             :                if we can find a constant string length for the other
    2105                 :             :                PHI args then we can still be sure that this is a
    2106                 :             :                constant string length.  So be optimistic and just
    2107                 :             :                continue with the next argument.  */
    2108                 :       50002 :             if (arg == gimple_phi_result (def_stmt))
    2109                 :           0 :               continue;
    2110                 :             : 
    2111                 :       50002 :             if (!get_range_strlen (arg, visited, rkind, pdata, eltsize))
    2112                 :             :               {
    2113                 :       26752 :                 if (rkind != SRK_LENRANGE)
    2114                 :             :                   return false;
    2115                 :             :                 /* Set the upper bound to the maximum to prevent
    2116                 :             :                    it from being adjusted in the next iteration but
    2117                 :             :                    leave MINLEN and the more conservative MAXBOUND
    2118                 :             :                    determined so far alone (or leave them null if
    2119                 :             :                    they haven't been set yet).  That the MINLEN is
    2120                 :             :                    in fact zero can be determined from MAXLEN being
    2121                 :             :                    unbounded but the discovered minimum is used for
    2122                 :             :                    diagnostics.  */
    2123                 :       24803 :                 pdata->maxlen = build_all_ones_cst (size_type_node);
    2124                 :             :               }
    2125                 :             :           }
    2126                 :             :         return true;
    2127                 :             : 
    2128                 :             :       default:
    2129                 :             :         return false;
    2130                 :             :     }
    2131                 :             : }
    2132                 :             : 
    2133                 :             : /* Try to obtain the range of the lengths of the string(s) referenced
    2134                 :             :    by ARG, or the size of the largest array ARG refers to if the range
    2135                 :             :    of lengths cannot be determined, and store all in *PDATA which must
    2136                 :             :    be zero-initialized on input except PDATA->MAXBOUND may be set to
    2137                 :             :    a non-null tree node other than INTEGER_CST to request to have it
    2138                 :             :    set to the length of the longest string in a PHI.  ELTSIZE is
    2139                 :             :    the expected size of the string element in bytes: 1 for char and
    2140                 :             :    some power of 2 for wide characters.
    2141                 :             :    Return true if the range [PDATA->MINLEN, PDATA->MAXLEN] is suitable
    2142                 :             :    for optimization.  Returning false means that a nonzero PDATA->MINLEN
    2143                 :             :    doesn't reflect the true lower bound of the range when PDATA->MAXLEN
    2144                 :             :    is -1 (in that case, the actual range is indeterminate, i.e.,
    2145                 :             :    [0, PTRDIFF_MAX - 2].  */
    2146                 :             : 
    2147                 :             : bool
    2148                 :     1224290 : get_range_strlen (tree arg, c_strlen_data *pdata, unsigned eltsize)
    2149                 :             : {
    2150                 :     1224290 :   auto_bitmap visited;
    2151                 :     1224290 :   tree maxbound = pdata->maxbound;
    2152                 :             : 
    2153                 :     1224290 :   if (!get_range_strlen (arg, visited, SRK_LENRANGE, pdata, eltsize))
    2154                 :             :     {
    2155                 :             :       /* On failure extend the length range to an impossible maximum
    2156                 :             :          (a valid MAXLEN must be less than PTRDIFF_MAX - 1).  Other
    2157                 :             :          members can stay unchanged regardless.  */
    2158                 :      991233 :       pdata->minlen = ssize_int (0);
    2159                 :      991233 :       pdata->maxlen = build_all_ones_cst (size_type_node);
    2160                 :             :     }
    2161                 :      233057 :   else if (!pdata->minlen)
    2162                 :        8470 :     pdata->minlen = ssize_int (0);
    2163                 :             : 
    2164                 :             :   /* If it's unchanged from it initial non-null value, set the conservative
    2165                 :             :      MAXBOUND to SIZE_MAX.  Otherwise leave it null (if it is null).  */
    2166                 :     1224290 :   if (maxbound && pdata->maxbound == maxbound)
    2167                 :      650585 :     pdata->maxbound = build_all_ones_cst (size_type_node);
    2168                 :             : 
    2169                 :     1224290 :   return !integer_all_onesp (pdata->maxlen);
    2170                 :     1224290 : }
    2171                 :             : 
    2172                 :             : /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
    2173                 :             :    For ARG of pointer types, NONSTR indicates if the caller is prepared
    2174                 :             :    to handle unterminated strings.   For integer ARG and when RKIND ==
    2175                 :             :    SRK_INT_VALUE, NONSTR must be null.
    2176                 :             : 
    2177                 :             :    If an unterminated array is discovered and our caller handles
    2178                 :             :    unterminated arrays, then bubble up the offending DECL and
    2179                 :             :    return the maximum size.  Otherwise return NULL.  */
    2180                 :             : 
    2181                 :             : static tree
    2182                 :      101324 : get_maxval_strlen (tree arg, strlen_range_kind rkind, tree *nonstr = NULL)
    2183                 :             : {
    2184                 :             :   /* A non-null NONSTR is meaningless when determining the maximum
    2185                 :             :      value of an integer ARG.  */
    2186                 :      101324 :   gcc_assert (rkind != SRK_INT_VALUE || nonstr == NULL);
    2187                 :             :   /* ARG must have an integral type when RKIND says so.  */
    2188                 :      101324 :   gcc_assert (rkind != SRK_INT_VALUE || INTEGRAL_TYPE_P (TREE_TYPE (arg)));
    2189                 :             : 
    2190                 :      101324 :   auto_bitmap visited;
    2191                 :             : 
    2192                 :             :   /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
    2193                 :             :      is unbounded.  */
    2194                 :      101324 :   c_strlen_data lendata = { };
    2195                 :      101324 :   if (!get_range_strlen (arg, visited, rkind, &lendata, /* eltsize = */1))
    2196                 :       54401 :     lendata.maxlen = NULL_TREE;
    2197                 :       46923 :   else if (lendata.maxlen && integer_all_onesp (lendata.maxlen))
    2198                 :           0 :     lendata.maxlen = NULL_TREE;
    2199                 :             : 
    2200                 :      101324 :   if (nonstr)
    2201                 :             :     {
    2202                 :             :       /* For callers prepared to handle unterminated arrays set
    2203                 :             :          *NONSTR to point to the declaration of the array and return
    2204                 :             :          the maximum length/size. */
    2205                 :       25353 :       *nonstr = lendata.decl;
    2206                 :       25353 :       return lendata.maxlen;
    2207                 :             :     }
    2208                 :             : 
    2209                 :             :   /* Fail if the constant array isn't nul-terminated.  */
    2210                 :       75971 :   return lendata.decl ? NULL_TREE : lendata.maxlen;
    2211                 :      101324 : }
    2212                 :             : 
    2213                 :             : /* Return true if LEN is known to be less than or equal to (or if STRICT is
    2214                 :             :    true, strictly less than) the lower bound of SIZE at compile time and false
    2215                 :             :    otherwise.  */
    2216                 :             : 
    2217                 :             : static bool
    2218                 :       66488 : known_lower (gimple *stmt, tree len, tree size, bool strict = false)
    2219                 :             : {
    2220                 :       66488 :   if (len == NULL_TREE)
    2221                 :             :     return false;
    2222                 :             : 
    2223                 :      243710 :   wide_int size_range[2];
    2224                 :      243710 :   wide_int len_range[2];
    2225                 :       48742 :   if (get_range (len, stmt, len_range) && get_range (size, stmt, size_range))
    2226                 :             :     {
    2227                 :       17585 :       if (strict)
    2228                 :        2053 :         return wi::ltu_p (len_range[1], size_range[0]);
    2229                 :             :       else
    2230                 :       15532 :        return wi::leu_p (len_range[1], size_range[0]);
    2231                 :             :     }
    2232                 :             : 
    2233                 :             :   return false;
    2234                 :      292452 : }
    2235                 :             : 
    2236                 :             : /* Fold function call to builtin strcpy with arguments DEST and SRC.
    2237                 :             :    If LEN is not NULL, it represents the length of the string to be
    2238                 :             :    copied.  Return NULL_TREE if no simplification can be made.  */
    2239                 :             : 
    2240                 :             : static bool
    2241                 :       27680 : gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
    2242                 :             :                             tree dest, tree src)
    2243                 :             : {
    2244                 :       27680 :   gimple *stmt = gsi_stmt (*gsi);
    2245                 :       27680 :   location_t loc = gimple_location (stmt);
    2246                 :       27680 :   tree fn;
    2247                 :             : 
    2248                 :             :   /* If SRC and DEST are the same (and not volatile), return DEST.  */
    2249                 :       27680 :   if (operand_equal_p (src, dest, 0))
    2250                 :             :     {
    2251                 :             :       /* Issue -Wrestrict unless the pointers are null (those do
    2252                 :             :          not point to objects and so do not indicate an overlap;
    2253                 :             :          such calls could be the result of sanitization and jump
    2254                 :             :          threading).  */
    2255                 :          86 :       if (!integer_zerop (dest) && !warning_suppressed_p (stmt, OPT_Wrestrict))
    2256                 :             :         {
    2257                 :          51 :           tree func = gimple_call_fndecl (stmt);
    2258                 :             : 
    2259                 :          51 :           warning_at (loc, OPT_Wrestrict,
    2260                 :             :                       "%qD source argument is the same as destination",
    2261                 :             :                       func);
    2262                 :             :         }
    2263                 :             : 
    2264                 :          86 :       replace_call_with_value (gsi, dest);
    2265                 :          86 :       return true;
    2266                 :             :     }
    2267                 :             : 
    2268                 :       27594 :   if (optimize_function_for_size_p (cfun))
    2269                 :             :     return false;
    2270                 :             : 
    2271                 :       25353 :   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    2272                 :       25353 :   if (!fn)
    2273                 :             :     return false;
    2274                 :             : 
    2275                 :             :   /* Set to non-null if ARG refers to an unterminated array.  */
    2276                 :       25353 :   tree nonstr = NULL;
    2277                 :       25353 :   tree len = get_maxval_strlen (src, SRK_STRLEN, &nonstr);
    2278                 :             : 
    2279                 :       25353 :   if (nonstr)
    2280                 :             :     {
    2281                 :             :       /* Avoid folding calls with unterminated arrays.  */
    2282                 :         596 :       if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
    2283                 :          69 :         warn_string_no_nul (loc, stmt, "strcpy", src, nonstr);
    2284                 :         596 :       suppress_warning (stmt, OPT_Wstringop_overread);
    2285                 :         596 :       return false;
    2286                 :             :     }
    2287                 :             : 
    2288                 :       30409 :   if (!len || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    2289                 :             :     return false;
    2290                 :             : 
    2291                 :        2815 :   len = fold_convert_loc (loc, size_type_node, len);
    2292                 :        2815 :   len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
    2293                 :        2815 :   len = force_gimple_operand_gsi (gsi, len, true,
    2294                 :             :                                   NULL_TREE, true, GSI_SAME_STMT);
    2295                 :        2815 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    2296                 :        2815 :   replace_call_with_call_and_fold (gsi, repl);
    2297                 :        2815 :   return true;
    2298                 :             : }
    2299                 :             : 
    2300                 :             : /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
    2301                 :             :    If SLEN is not NULL, it represents the length of the source string.
    2302                 :             :    Return NULL_TREE if no simplification can be made.  */
    2303                 :             : 
    2304                 :             : static bool
    2305                 :       19113 : gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
    2306                 :             :                              tree dest, tree src, tree len)
    2307                 :             : {
    2308                 :       19113 :   gimple *stmt = gsi_stmt (*gsi);
    2309                 :       19113 :   location_t loc = gimple_location (stmt);
    2310                 :       19113 :   bool nonstring = get_attr_nonstring_decl (dest) != NULL_TREE;
    2311                 :             : 
    2312                 :             :   /* If the LEN parameter is zero, return DEST.  */
    2313                 :       19113 :   if (integer_zerop (len))
    2314                 :             :     {
    2315                 :             :       /* Avoid warning if the destination refers to an array/pointer
    2316                 :             :          decorate with attribute nonstring.  */
    2317                 :         167 :       if (!nonstring)
    2318                 :             :         {
    2319                 :         155 :           tree fndecl = gimple_call_fndecl (stmt);
    2320                 :             : 
    2321                 :             :           /* Warn about the lack of nul termination: the result is not
    2322                 :             :              a (nul-terminated) string.  */
    2323                 :         155 :           tree slen = get_maxval_strlen (src, SRK_STRLEN);
    2324                 :         155 :           if (slen && !integer_zerop (slen))
    2325                 :          24 :             warning_at (loc, OPT_Wstringop_truncation,
    2326                 :             :                         "%qD destination unchanged after copying no bytes "
    2327                 :             :                         "from a string of length %E",
    2328                 :             :                         fndecl, slen);
    2329                 :             :           else
    2330                 :         131 :             warning_at (loc, OPT_Wstringop_truncation,
    2331                 :             :                         "%qD destination unchanged after copying no bytes",
    2332                 :             :                         fndecl);
    2333                 :             :         }
    2334                 :             : 
    2335                 :         167 :       replace_call_with_value (gsi, dest);
    2336                 :         167 :       return true;
    2337                 :             :     }
    2338                 :             : 
    2339                 :             :   /* We can't compare slen with len as constants below if len is not a
    2340                 :             :      constant.  */
    2341                 :       18946 :   if (TREE_CODE (len) != INTEGER_CST)
    2342                 :             :     return false;
    2343                 :             : 
    2344                 :             :   /* Now, we must be passed a constant src ptr parameter.  */
    2345                 :       11901 :   tree slen = get_maxval_strlen (src, SRK_STRLEN);
    2346                 :       11901 :   if (!slen || TREE_CODE (slen) != INTEGER_CST)
    2347                 :             :     return false;
    2348                 :             : 
    2349                 :             :   /* The size of the source string including the terminating nul.  */
    2350                 :        1917 :   tree ssize = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
    2351                 :             : 
    2352                 :             :   /* We do not support simplification of this case, though we do
    2353                 :             :      support it when expanding trees into RTL.  */
    2354                 :             :   /* FIXME: generate a call to __builtin_memset.  */
    2355                 :        1917 :   if (tree_int_cst_lt (ssize, len))
    2356                 :             :     return false;
    2357                 :             : 
    2358                 :             :   /* Diagnose truncation that leaves the copy unterminated.  */
    2359                 :         695 :   maybe_diag_stxncpy_trunc (*gsi, src, len);
    2360                 :             : 
    2361                 :             :   /* OK transform into builtin memcpy.  */
    2362                 :         695 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    2363                 :       19641 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    2364                 :             :     return false;
    2365                 :             : 
    2366                 :         695 :   len = fold_convert_loc (loc, size_type_node, len);
    2367                 :         695 :   len = force_gimple_operand_gsi (gsi, len, true,
    2368                 :             :                                   NULL_TREE, true, GSI_SAME_STMT);
    2369                 :         695 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    2370                 :         695 :   replace_call_with_call_and_fold (gsi, repl);
    2371                 :             : 
    2372                 :         695 :   return true;
    2373                 :             : }
    2374                 :             : 
    2375                 :             : /* Fold function call to builtin strchr or strrchr.
    2376                 :             :    If both arguments are constant, evaluate and fold the result,
    2377                 :             :    otherwise simplify str(r)chr (str, 0) into str + strlen (str).
    2378                 :             :    In general strlen is significantly faster than strchr
    2379                 :             :    due to being a simpler operation.  */
    2380                 :             : static bool
    2381                 :        6445 : gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
    2382                 :             : {
    2383                 :        6445 :   gimple *stmt = gsi_stmt (*gsi);
    2384                 :        6445 :   tree str = gimple_call_arg (stmt, 0);
    2385                 :        6445 :   tree c = gimple_call_arg (stmt, 1);
    2386                 :        6445 :   location_t loc = gimple_location (stmt);
    2387                 :        6445 :   const char *p;
    2388                 :        6445 :   char ch;
    2389                 :             : 
    2390                 :        6445 :   if (!gimple_call_lhs (stmt))
    2391                 :             :     return false;
    2392                 :             : 
    2393                 :             :   /* Avoid folding if the first argument is not a nul-terminated array.
    2394                 :             :      Defer warning until later.  */
    2395                 :        6435 :   if (!check_nul_terminated_array (NULL_TREE, str))
    2396                 :             :     return false;
    2397                 :             : 
    2398                 :        6341 :   if ((p = c_getstr (str)) && target_char_cst_p (c, &ch))
    2399                 :             :     {
    2400                 :          41 :       const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch);
    2401                 :             : 
    2402                 :          41 :       if (p1 == NULL)
    2403                 :             :         {
    2404                 :           1 :           replace_call_with_value (gsi, integer_zero_node);
    2405                 :           1 :           return true;
    2406                 :             :         }
    2407                 :             : 
    2408                 :          40 :       tree len = build_int_cst (size_type_node, p1 - p);
    2409                 :          40 :       gimple_seq stmts = NULL;
    2410                 :          40 :       gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
    2411                 :             :                                               POINTER_PLUS_EXPR, str, len);
    2412                 :          40 :       gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2413                 :          40 :       gsi_replace_with_seq_vops (gsi, stmts);
    2414                 :          40 :       return true;
    2415                 :             :     }
    2416                 :             : 
    2417                 :        6382 :   if (!integer_zerop (c) || (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun)))
    2418                 :             :     return false;
    2419                 :             : 
    2420                 :             :   /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size.  */
    2421                 :          82 :   if (is_strrchr && optimize_function_for_size_p (cfun))
    2422                 :             :     {
    2423                 :           3 :       tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
    2424                 :             : 
    2425                 :           3 :       if (strchr_fn)
    2426                 :             :         {
    2427                 :           3 :           gimple *repl = gimple_build_call (strchr_fn, 2, str, c);
    2428                 :           3 :           replace_call_with_call_and_fold (gsi, repl);
    2429                 :           3 :           return true;
    2430                 :             :         }
    2431                 :             : 
    2432                 :             :       return false;
    2433                 :             :     }
    2434                 :             : 
    2435                 :          79 :   tree len;
    2436                 :        6401 :   tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
    2437                 :             : 
    2438                 :          79 :   if (!strlen_fn)
    2439                 :             :     return false;
    2440                 :             : 
    2441                 :             :   /* Create newstr = strlen (str).  */
    2442                 :          79 :   gimple_seq stmts = NULL;
    2443                 :          79 :   gimple *new_stmt = gimple_build_call (strlen_fn, 1, str);
    2444                 :          79 :   gimple_set_location (new_stmt, loc);
    2445                 :          79 :   len = create_tmp_reg_or_ssa_name (size_type_node);
    2446                 :          79 :   gimple_call_set_lhs (new_stmt, len);
    2447                 :          79 :   gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2448                 :             : 
    2449                 :             :   /* Create (str p+ strlen (str)).  */
    2450                 :          79 :   new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
    2451                 :             :                                   POINTER_PLUS_EXPR, str, len);
    2452                 :          79 :   gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2453                 :          79 :   gsi_replace_with_seq_vops (gsi, stmts);
    2454                 :             :   /* gsi now points at the assignment to the lhs, get a
    2455                 :             :      stmt iterator to the strlen.
    2456                 :             :      ???  We can't use gsi_for_stmt as that doesn't work when the
    2457                 :             :      CFG isn't built yet.  */
    2458                 :          79 :   gimple_stmt_iterator gsi2 = *gsi;
    2459                 :          79 :   gsi_prev (&gsi2);
    2460                 :          79 :   fold_stmt (&gsi2);
    2461                 :          79 :   return true;
    2462                 :             : }
    2463                 :             : 
    2464                 :             : /* Fold function call to builtin strstr.
    2465                 :             :    If both arguments are constant, evaluate and fold the result,
    2466                 :             :    additionally fold strstr (x, "") into x and strstr (x, "c")
    2467                 :             :    into strchr (x, 'c').  */
    2468                 :             : static bool
    2469                 :        4619 : gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
    2470                 :             : {
    2471                 :        4619 :   gimple *stmt = gsi_stmt (*gsi);
    2472                 :        4619 :   if (!gimple_call_lhs (stmt))
    2473                 :             :     return false;
    2474                 :             : 
    2475                 :        4616 :   tree haystack = gimple_call_arg (stmt, 0);
    2476                 :        4616 :   tree needle = gimple_call_arg (stmt, 1);
    2477                 :             : 
    2478                 :             :   /* Avoid folding if either argument is not a nul-terminated array.
    2479                 :             :      Defer warning until later.  */
    2480                 :        4616 :   if (!check_nul_terminated_array (NULL_TREE, haystack)
    2481                 :        4616 :       || !check_nul_terminated_array (NULL_TREE, needle))
    2482                 :          21 :     return false;
    2483                 :             : 
    2484                 :        4595 :   const char *q = c_getstr (needle);
    2485                 :        4595 :   if (q == NULL)
    2486                 :             :     return false;
    2487                 :             : 
    2488                 :        3425 :   if (const char *p = c_getstr (haystack))
    2489                 :             :     {
    2490                 :          14 :       const char *r = strstr (p, q);
    2491                 :             : 
    2492                 :          14 :       if (r == NULL)
    2493                 :             :         {
    2494                 :           1 :           replace_call_with_value (gsi, integer_zero_node);
    2495                 :           1 :           return true;
    2496                 :             :         }
    2497                 :             : 
    2498                 :          13 :       tree len = build_int_cst (size_type_node, r - p);
    2499                 :          13 :       gimple_seq stmts = NULL;
    2500                 :          13 :       gimple *new_stmt
    2501                 :          13 :         = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR,
    2502                 :             :                                haystack, len);
    2503                 :          13 :       gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2504                 :          13 :       gsi_replace_with_seq_vops (gsi, stmts);
    2505                 :          13 :       return true;
    2506                 :             :     }
    2507                 :             : 
    2508                 :             :   /* For strstr (x, "") return x.  */
    2509                 :        3411 :   if (q[0] == '\0')
    2510                 :             :     {
    2511                 :           6 :       replace_call_with_value (gsi, haystack);
    2512                 :           6 :       return true;
    2513                 :             :     }
    2514                 :             : 
    2515                 :       11387 :   if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
    2516                 :             :     return false;
    2517                 :             : 
    2518                 :             :   /* Transform strstr (x, "c") into strchr (x, 'c').  */
    2519                 :        3405 :   if (q[1] == '\0')
    2520                 :             :     {
    2521                 :          22 :       tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
    2522                 :          22 :       if (strchr_fn)
    2523                 :             :         {
    2524                 :          22 :           tree c = build_int_cst (integer_type_node, q[0]);
    2525                 :          22 :           gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c);
    2526                 :          22 :           replace_call_with_call_and_fold (gsi, repl);
    2527                 :          22 :           return true;
    2528                 :             :         }
    2529                 :             :     }
    2530                 :             : 
    2531                 :             :   return false;
    2532                 :             : }
    2533                 :             : 
    2534                 :             : /* Simplify a call to the strcat builtin.  DST and SRC are the arguments
    2535                 :             :    to the call.
    2536                 :             : 
    2537                 :             :    Return NULL_TREE if no simplification was possible, otherwise return the
    2538                 :             :    simplified form of the call as a tree.
    2539                 :             : 
    2540                 :             :    The simplified form may be a constant or other expression which
    2541                 :             :    computes the same value, but in a more efficient manner (including
    2542                 :             :    calls to other builtin functions).
    2543                 :             : 
    2544                 :             :    The call may contain arguments which need to be evaluated, but
    2545                 :             :    which are not useful to determine the result of the call.  In
    2546                 :             :    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
    2547                 :             :    COMPOUND_EXPR will be an argument which must be evaluated.
    2548                 :             :    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
    2549                 :             :    COMPOUND_EXPR in the chain will contain the tree for the simplified
    2550                 :             :    form of the builtin function call.  */
    2551                 :             : 
    2552                 :             : static bool
    2553                 :        7934 : gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
    2554                 :             : {
    2555                 :        7934 :   gimple *stmt = gsi_stmt (*gsi);
    2556                 :        7934 :   location_t loc = gimple_location (stmt);
    2557                 :             : 
    2558                 :        7934 :   const char *p = c_getstr (src);
    2559                 :             : 
    2560                 :             :   /* If the string length is zero, return the dst parameter.  */
    2561                 :        7934 :   if (p && *p == '\0')
    2562                 :             :     {
    2563                 :          72 :       replace_call_with_value (gsi, dst);
    2564                 :          72 :       return true;
    2565                 :             :     }
    2566                 :             : 
    2567                 :        7862 :   if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
    2568                 :             :     return false;
    2569                 :             : 
    2570                 :       21553 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    2571                 :             :     return false;
    2572                 :             : 
    2573                 :             :   /* See if we can store by pieces into (dst + strlen(dst)).  */
    2574                 :        7217 :   tree newdst;
    2575                 :        7217 :   tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
    2576                 :        7217 :   tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    2577                 :             : 
    2578                 :        7217 :   if (!strlen_fn || !memcpy_fn)
    2579                 :             :     return false;
    2580                 :             : 
    2581                 :             :   /* If the length of the source string isn't computable don't
    2582                 :             :      split strcat into strlen and memcpy.  */
    2583                 :        7217 :   tree len = get_maxval_strlen (src, SRK_STRLEN);
    2584                 :        7217 :   if (! len)
    2585                 :             :     return false;
    2586                 :             : 
    2587                 :             :   /* Create strlen (dst).  */
    2588                 :         743 :   gimple_seq stmts = NULL, stmts2;
    2589                 :         743 :   gimple *repl = gimple_build_call (strlen_fn, 1, dst);
    2590                 :         743 :   gimple_set_location (repl, loc);
    2591                 :         743 :   newdst = create_tmp_reg_or_ssa_name (size_type_node);
    2592                 :         743 :   gimple_call_set_lhs (repl, newdst);
    2593                 :         743 :   gimple_seq_add_stmt_without_update (&stmts, repl);
    2594                 :             : 
    2595                 :             :   /* Create (dst p+ strlen (dst)).  */
    2596                 :         743 :   newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
    2597                 :         743 :   newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
    2598                 :         743 :   gimple_seq_add_seq_without_update (&stmts, stmts2);
    2599                 :             : 
    2600                 :         743 :   len = fold_convert_loc (loc, size_type_node, len);
    2601                 :         743 :   len = size_binop_loc (loc, PLUS_EXPR, len,
    2602                 :         743 :                         build_int_cst (size_type_node, 1));
    2603                 :         743 :   len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
    2604                 :         743 :   gimple_seq_add_seq_without_update (&stmts, stmts2);
    2605                 :             : 
    2606                 :         743 :   repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
    2607                 :         743 :   gimple_seq_add_stmt_without_update (&stmts, repl);
    2608                 :         743 :   if (gimple_call_lhs (stmt))
    2609                 :             :     {
    2610                 :         165 :       repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
    2611                 :         165 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    2612                 :         165 :       gsi_replace_with_seq_vops (gsi, stmts);
    2613                 :             :       /* gsi now points at the assignment to the lhs, get a
    2614                 :             :          stmt iterator to the memcpy call.
    2615                 :             :          ???  We can't use gsi_for_stmt as that doesn't work when the
    2616                 :             :          CFG isn't built yet.  */
    2617                 :         165 :       gimple_stmt_iterator gsi2 = *gsi;
    2618                 :         165 :       gsi_prev (&gsi2);
    2619                 :         165 :       fold_stmt (&gsi2);
    2620                 :             :     }
    2621                 :             :   else
    2622                 :             :     {
    2623                 :         578 :       gsi_replace_with_seq_vops (gsi, stmts);
    2624                 :         578 :       fold_stmt (gsi);
    2625                 :             :     }
    2626                 :             :   return true;
    2627                 :             : }
    2628                 :             : 
    2629                 :             : /* Fold a call to the __strcat_chk builtin FNDECL.  DEST, SRC, and SIZE
    2630                 :             :    are the arguments to the call.  */
    2631                 :             : 
    2632                 :             : static bool
    2633                 :        1854 : gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
    2634                 :             : {
    2635                 :        1854 :   gimple *stmt = gsi_stmt (*gsi);
    2636                 :        1854 :   tree dest = gimple_call_arg (stmt, 0);
    2637                 :        1854 :   tree src = gimple_call_arg (stmt, 1);
    2638                 :        1854 :   tree size = gimple_call_arg (stmt, 2);
    2639                 :        1854 :   tree fn;
    2640                 :        1854 :   const char *p;
    2641                 :             : 
    2642                 :        1854 :   p = c_getstr (src);
    2643                 :             :   /* If the SRC parameter is "", return DEST.  */
    2644                 :        1854 :   if (p && *p == '\0')
    2645                 :             :     {
    2646                 :          60 :       replace_call_with_value (gsi, dest);
    2647                 :          60 :       return true;
    2648                 :             :     }
    2649                 :             : 
    2650                 :        1794 :   if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
    2651                 :        1712 :     return false;
    2652                 :             : 
    2653                 :        1876 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    2654                 :             :     return false;
    2655                 :             : 
    2656                 :             :   /* If __builtin_strcat_chk is used, assume strcat is available.  */
    2657                 :          82 :   fn = builtin_decl_explicit (BUILT_IN_STRCAT);
    2658                 :          82 :   if (!fn)
    2659                 :             :     return false;
    2660                 :             : 
    2661                 :          82 :   gimple *repl = gimple_build_call (fn, 2, dest, src);
    2662                 :          82 :   replace_call_with_call_and_fold (gsi, repl);
    2663                 :          82 :   return true;
    2664                 :             : }
    2665                 :             : 
    2666                 :             : /* Simplify a call to the strncat builtin.  */
    2667                 :             : 
    2668                 :             : static bool
    2669                 :        7557 : gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
    2670                 :             : {
    2671                 :        7557 :   gimple *stmt = gsi_stmt (*gsi);
    2672                 :        7557 :   tree dst = gimple_call_arg (stmt, 0);
    2673                 :        7557 :   tree src = gimple_call_arg (stmt, 1);
    2674                 :        7557 :   tree len = gimple_call_arg (stmt, 2);
    2675                 :        7557 :   tree src_len = c_strlen (src, 1);
    2676                 :             : 
    2677                 :             :   /* If the requested length is zero, or the src parameter string
    2678                 :             :      length is zero, return the dst parameter.  */
    2679                 :        7557 :   if (integer_zerop (len) || (src_len && integer_zerop (src_len)))
    2680                 :             :     {
    2681                 :         119 :       replace_call_with_value (gsi, dst);
    2682                 :         119 :       return true;
    2683                 :             :     }
    2684                 :             : 
    2685                 :             :   /* Return early if the requested len is less than the string length.
    2686                 :             :      Warnings will be issued elsewhere later.  */
    2687                 :        7438 :   if (!src_len || known_lower (stmt, len, src_len, true))
    2688                 :        6833 :     return false;
    2689                 :             : 
    2690                 :             :   /* Warn on constant LEN.  */
    2691                 :         605 :   if (TREE_CODE (len) == INTEGER_CST)
    2692                 :             :     {
    2693                 :         131 :       bool nowarn = warning_suppressed_p (stmt, OPT_Wstringop_overflow_);
    2694                 :         131 :       tree dstsize;
    2695                 :             : 
    2696                 :         131 :       if (!nowarn && compute_builtin_object_size (dst, 1, &dstsize)
    2697                 :         175 :           && TREE_CODE (dstsize) == INTEGER_CST)
    2698                 :             :         {
    2699                 :          44 :           int cmpdst = tree_int_cst_compare (len, dstsize);
    2700                 :             : 
    2701                 :          44 :           if (cmpdst >= 0)
    2702                 :             :             {
    2703                 :          19 :               tree fndecl = gimple_call_fndecl (stmt);
    2704                 :             : 
    2705                 :             :               /* Strncat copies (at most) LEN bytes and always appends
    2706                 :             :                  the terminating NUL so the specified bound should never
    2707                 :             :                  be equal to (or greater than) the size of the destination.
    2708                 :             :                  If it is, the copy could overflow.  */
    2709                 :          19 :               location_t loc = gimple_location (stmt);
    2710                 :          37 :               nowarn = warning_at (loc, OPT_Wstringop_overflow_,
    2711                 :             :                                    cmpdst == 0
    2712                 :             :                                    ? G_("%qD specified bound %E equals "
    2713                 :             :                                         "destination size")
    2714                 :             :                                    : G_("%qD specified bound %E exceeds "
    2715                 :             :                                         "destination size %E"),
    2716                 :             :                                    fndecl, len, dstsize);
    2717                 :          19 :               if (nowarn)
    2718                 :           0 :                 suppress_warning (stmt, OPT_Wstringop_overflow_);
    2719                 :             :             }
    2720                 :             :         }
    2721                 :             : 
    2722                 :         131 :       if (!nowarn && TREE_CODE (src_len) == INTEGER_CST
    2723                 :         243 :           && tree_int_cst_compare (src_len, len) == 0)
    2724                 :             :         {
    2725                 :          20 :           tree fndecl = gimple_call_fndecl (stmt);
    2726                 :          20 :           location_t loc = gimple_location (stmt);
    2727                 :             : 
    2728                 :             :           /* To avoid possible overflow the specified bound should also
    2729                 :             :              not be equal to the length of the source, even when the size
    2730                 :             :              of the destination is unknown (it's not an uncommon mistake
    2731                 :             :              to specify as the bound to strncpy the length of the source).  */
    2732                 :          20 :           if (warning_at (loc, OPT_Wstringop_overflow_,
    2733                 :             :                           "%qD specified bound %E equals source length",
    2734                 :             :                           fndecl, len))
    2735                 :           6 :             suppress_warning (stmt, OPT_Wstringop_overflow_);
    2736                 :             :         }
    2737                 :             :     }
    2738                 :             : 
    2739                 :         605 :   if (!known_lower (stmt, src_len, len))
    2740                 :             :     return false;
    2741                 :             : 
    2742                 :         136 :   tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
    2743                 :             : 
    2744                 :             :   /* If the replacement _DECL isn't initialized, don't do the
    2745                 :             :      transformation.  */
    2746                 :        7574 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    2747                 :             :     return false;
    2748                 :             : 
    2749                 :             :   /* Otherwise, emit a call to strcat.  */
    2750                 :         136 :   gcall *repl = gimple_build_call (fn, 2, dst, src);
    2751                 :         136 :   replace_call_with_call_and_fold (gsi, repl);
    2752                 :         136 :   return true;
    2753                 :             : }
    2754                 :             : 
    2755                 :             : /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
    2756                 :             :    LEN, and SIZE.  */
    2757                 :             : 
    2758                 :             : static bool
    2759                 :        1225 : gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
    2760                 :             : {
    2761                 :        1225 :   gimple *stmt = gsi_stmt (*gsi);
    2762                 :        1225 :   tree dest = gimple_call_arg (stmt, 0);
    2763                 :        1225 :   tree src = gimple_call_arg (stmt, 1);
    2764                 :        1225 :   tree len = gimple_call_arg (stmt, 2);
    2765                 :        1225 :   tree size = gimple_call_arg (stmt, 3);
    2766                 :        1225 :   tree fn;
    2767                 :        1225 :   const char *p;
    2768                 :             : 
    2769                 :        1225 :   p = c_getstr (src);
    2770                 :             :   /* If the SRC parameter is "" or if LEN is 0, return DEST.  */
    2771                 :         312 :   if ((p && *p == '\0')
    2772                 :        1486 :       || integer_zerop (len))
    2773                 :             :     {
    2774                 :          78 :       replace_call_with_value (gsi, dest);
    2775                 :          78 :       return true;
    2776                 :             :     }
    2777                 :             : 
    2778                 :        3289 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    2779                 :             :     return false;
    2780                 :             : 
    2781                 :        1147 :   if (! integer_all_onesp (size))
    2782                 :             :     {
    2783                 :        1060 :       tree src_len = c_strlen (src, 1);
    2784                 :        1060 :       if (known_lower (stmt, src_len, len))
    2785                 :             :         {
    2786                 :             :           /* If LEN >= strlen (SRC), optimize into __strcat_chk.  */
    2787                 :          65 :           fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
    2788                 :          65 :           if (!fn)
    2789                 :             :             return false;
    2790                 :             : 
    2791                 :          65 :           gimple *repl = gimple_build_call (fn, 3, dest, src, size);
    2792                 :          65 :           replace_call_with_call_and_fold (gsi, repl);
    2793                 :          65 :           return true;
    2794                 :             :         }
    2795                 :             :       return false;
    2796                 :             :     }
    2797                 :             : 
    2798                 :             :   /* If __builtin_strncat_chk is used, assume strncat is available.  */
    2799                 :          87 :   fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
    2800                 :          87 :   if (!fn)
    2801                 :             :     return false;
    2802                 :             : 
    2803                 :          87 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    2804                 :          87 :   replace_call_with_call_and_fold (gsi, repl);
    2805                 :          87 :   return true;
    2806                 :             : }
    2807                 :             : 
    2808                 :             : /* Build and append gimple statements to STMTS that would load a first
    2809                 :             :    character of a memory location identified by STR.  LOC is location
    2810                 :             :    of the statement.  */
    2811                 :             : 
    2812                 :             : static tree
    2813                 :         461 : gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts)
    2814                 :             : {
    2815                 :         461 :   tree var;
    2816                 :             : 
    2817                 :         461 :   tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
    2818                 :         461 :   tree cst_uchar_ptr_node
    2819                 :         461 :     = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
    2820                 :         461 :   tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
    2821                 :             : 
    2822                 :         461 :   tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0);
    2823                 :         461 :   gassign *stmt = gimple_build_assign (NULL_TREE, temp);
    2824                 :         461 :   var = create_tmp_reg_or_ssa_name (cst_uchar_node, stmt);
    2825                 :             : 
    2826                 :         461 :   gimple_assign_set_lhs (stmt, var);
    2827                 :         461 :   gimple_seq_add_stmt_without_update (stmts, stmt);
    2828                 :             : 
    2829                 :         461 :   return var;
    2830                 :             : }
    2831                 :             : 
    2832                 :             : /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.  */
    2833                 :             : 
    2834                 :             : static bool
    2835                 :     1377759 : gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
    2836                 :             : {
    2837                 :     1377759 :   gimple *stmt = gsi_stmt (*gsi);
    2838                 :     1377759 :   tree callee = gimple_call_fndecl (stmt);
    2839                 :     1377759 :   enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
    2840                 :             : 
    2841                 :     1377759 :   tree type = integer_type_node;
    2842                 :     1377759 :   tree str1 = gimple_call_arg (stmt, 0);
    2843                 :     1377759 :   tree str2 = gimple_call_arg (stmt, 1);
    2844                 :     1377759 :   tree lhs = gimple_call_lhs (stmt);
    2845                 :             : 
    2846                 :     1377759 :   tree bound_node = NULL_TREE;
    2847                 :     1377759 :   unsigned HOST_WIDE_INT bound = HOST_WIDE_INT_M1U;
    2848                 :             : 
    2849                 :             :   /* Handle strncmp and strncasecmp functions.  */
    2850                 :     1377759 :   if (gimple_call_num_args (stmt) == 3)
    2851                 :             :     {
    2852                 :       25566 :       bound_node = gimple_call_arg (stmt, 2);
    2853                 :       25566 :       if (tree_fits_uhwi_p (bound_node))
    2854                 :       19317 :         bound = tree_to_uhwi (bound_node);
    2855                 :             :     }
    2856                 :             : 
    2857                 :             :   /* If the BOUND parameter is zero, return zero.  */
    2858                 :       19317 :   if (bound == 0)
    2859                 :             :     {
    2860                 :           4 :       replace_call_with_value (gsi, integer_zero_node);
    2861                 :           4 :       return true;
    2862                 :             :     }
    2863                 :             : 
    2864                 :             :   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
    2865                 :     1377755 :   if (operand_equal_p (str1, str2, 0))
    2866                 :             :     {
    2867                 :          41 :       replace_call_with_value (gsi, integer_zero_node);
    2868                 :          41 :       return true;
    2869                 :             :     }
    2870                 :             : 
    2871                 :     2755428 :   if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
    2872                 :             :     return false;
    2873                 :             : 
    2874                 :             :   /* Initially set to the number of characters, including the terminating
    2875                 :             :      nul if each array has one.   LENx == strnlen (Sx, LENx) implies that
    2876                 :             :      the array Sx is not terminated by a nul.
    2877                 :             :      For nul-terminated strings then adjusted to their length so that
    2878                 :             :      LENx == NULPOSx holds.  */
    2879                 :     1377714 :   unsigned HOST_WIDE_INT len1 = HOST_WIDE_INT_MAX, len2 = len1;
    2880                 :     1377714 :   const char *p1 = getbyterep (str1, &len1);
    2881                 :     1377714 :   const char *p2 = getbyterep (str2, &len2);
    2882                 :             : 
    2883                 :             :   /* The position of the terminating nul character if one exists, otherwise
    2884                 :             :      a value greater than LENx.  */
    2885                 :     1377714 :   unsigned HOST_WIDE_INT nulpos1 = HOST_WIDE_INT_MAX, nulpos2 = nulpos1;
    2886                 :             : 
    2887                 :     1377714 :   if (p1)
    2888                 :             :     {
    2889                 :       43321 :       size_t n = strnlen (p1, len1);
    2890                 :       43321 :       if (n < len1)
    2891                 :       43206 :         len1 = nulpos1 = n;
    2892                 :             :     }
    2893                 :             : 
    2894                 :     1377714 :   if (p2)
    2895                 :             :     {
    2896                 :     1345876 :       size_t n = strnlen (p2, len2);
    2897                 :     1345876 :       if (n < len2)
    2898                 :     1345812 :         len2 = nulpos2 = n;
    2899                 :             :     }
    2900                 :             : 
    2901                 :             :   /* For known strings, return an immediate value.  */
    2902                 :     1377714 :   if (p1 && p2)
    2903                 :             :     {
    2904                 :       39371 :       int r = 0;
    2905                 :       39371 :       bool known_result = false;
    2906                 :             : 
    2907                 :       39371 :       switch (fcode)
    2908                 :             :         {
    2909                 :       38172 :         case BUILT_IN_STRCMP:
    2910                 :       38172 :         case BUILT_IN_STRCMP_EQ:
    2911                 :       38172 :           if (len1 != nulpos1 || len2 != nulpos2)
    2912                 :             :             break;
    2913                 :             : 
    2914                 :       38144 :           r = strcmp (p1, p2);
    2915                 :       38144 :           known_result = true;
    2916                 :       38144 :           break;
    2917                 :             : 
    2918                 :        1118 :         case BUILT_IN_STRNCMP:
    2919                 :        1118 :         case BUILT_IN_STRNCMP_EQ:
    2920                 :        1118 :           {
    2921                 :        1118 :             if (bound == HOST_WIDE_INT_M1U)
    2922                 :             :               break;
    2923                 :             : 
    2924                 :             :             /* Reduce the bound to be no more than the length
    2925                 :             :                of the shorter of the two strings, or the sizes
    2926                 :             :                of the unterminated arrays.  */
    2927                 :          38 :             unsigned HOST_WIDE_INT n = bound;
    2928                 :             : 
    2929                 :          38 :             if (len1 == nulpos1 && len1 < n)
    2930                 :           4 :               n = len1 + 1;
    2931                 :          38 :             if (len2 == nulpos2 && len2 < n)
    2932                 :          11 :               n = len2 + 1;
    2933                 :             : 
    2934                 :          38 :             if (MIN (nulpos1, nulpos2) + 1 < n)
    2935                 :             :               break;
    2936                 :             : 
    2937                 :          38 :             r = strncmp (p1, p2, n);
    2938                 :          38 :             known_result = true;
    2939                 :          38 :             break;
    2940                 :             :           }
    2941                 :             :         /* Only handleable situation is where the string are equal (result 0),
    2942                 :             :            which is already handled by operand_equal_p case.  */
    2943                 :             :         case BUILT_IN_STRCASECMP:
    2944                 :             :           break;
    2945                 :          41 :         case BUILT_IN_STRNCASECMP:
    2946                 :          41 :           {
    2947                 :          41 :             if (bound == HOST_WIDE_INT_M1U)
    2948                 :             :               break;
    2949                 :          41 :             r = strncmp (p1, p2, bound);
    2950                 :          41 :             if (r == 0)
    2951                 :             :               known_result = true;
    2952                 :             :             break;
    2953                 :             :           }
    2954                 :           0 :         default:
    2955                 :           0 :           gcc_unreachable ();
    2956                 :             :         }
    2957                 :             : 
    2958                 :       38182 :       if (known_result)
    2959                 :             :         {
    2960                 :       38182 :           replace_call_with_value (gsi, build_cmp_result (type, r));
    2961                 :       38182 :           return true;
    2962                 :             :         }
    2963                 :             :     }
    2964                 :             : 
    2965                 :     2679064 :   bool nonzero_bound = (bound >= 1 && bound < HOST_WIDE_INT_M1U)
    2966                 :     1320370 :     || fcode == BUILT_IN_STRCMP
    2967                 :     1320370 :     || fcode == BUILT_IN_STRCMP_EQ
    2968                 :     1345973 :     || fcode == BUILT_IN_STRCASECMP;
    2969                 :             : 
    2970                 :     1339532 :   location_t loc = gimple_location (stmt);
    2971                 :             : 
    2972                 :             :   /* If the second arg is "", return *(const unsigned char*)arg1.  */
    2973                 :     1339532 :   if (p2 && *p2 == '\0' && nonzero_bound)
    2974                 :             :     {
    2975                 :         150 :       gimple_seq stmts = NULL;
    2976                 :         150 :       tree var = gimple_load_first_char (loc, str1, &stmts);
    2977                 :         150 :       if (lhs)
    2978                 :             :         {
    2979                 :         150 :           stmt = gimple_build_assign (lhs, NOP_EXPR, var);
    2980                 :         150 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2981                 :             :         }
    2982                 :             : 
    2983                 :         150 :       gsi_replace_with_seq_vops (gsi, stmts);
    2984                 :         150 :       return true;
    2985                 :             :     }
    2986                 :             : 
    2987                 :             :   /* If the first arg is "", return -*(const unsigned char*)arg2.  */
    2988                 :     1339382 :   if (p1 && *p1 == '\0' && nonzero_bound)
    2989                 :             :     {
    2990                 :          91 :       gimple_seq stmts = NULL;
    2991                 :          91 :       tree var = gimple_load_first_char (loc, str2, &stmts);
    2992                 :             : 
    2993                 :          91 :       if (lhs)
    2994                 :             :         {
    2995                 :          91 :           tree c = create_tmp_reg_or_ssa_name (integer_type_node);
    2996                 :          91 :           stmt = gimple_build_assign (c, NOP_EXPR, var);
    2997                 :          91 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2998                 :             : 
    2999                 :          91 :           stmt = gimple_build_assign (lhs, NEGATE_EXPR, c);
    3000                 :          91 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    3001                 :             :         }
    3002                 :             : 
    3003                 :          91 :       gsi_replace_with_seq_vops (gsi, stmts);
    3004                 :          91 :       return true;
    3005                 :             :     }
    3006                 :             : 
    3007                 :             :   /* If BOUND is one, return an expression corresponding to
    3008                 :             :      (*(const unsigned char*)arg2 - *(const unsigned char*)arg1).  */
    3009                 :     1339291 :   if (fcode == BUILT_IN_STRNCMP && bound == 1)
    3010                 :             :     {
    3011                 :         110 :       gimple_seq stmts = NULL;
    3012                 :         110 :       tree temp1 = gimple_load_first_char (loc, str1, &stmts);
    3013                 :         110 :       tree temp2 = gimple_load_first_char (loc, str2, &stmts);
    3014                 :             : 
    3015                 :         110 :       if (lhs)
    3016                 :             :         {
    3017                 :         107 :           tree c1 = create_tmp_reg_or_ssa_name (integer_type_node);
    3018                 :         107 :           gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1);
    3019                 :         107 :           gimple_seq_add_stmt_without_update (&stmts, convert1);
    3020                 :             : 
    3021                 :         107 :           tree c2 = create_tmp_reg_or_ssa_name (integer_type_node);
    3022                 :         107 :           gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2);
    3023                 :         107 :           gimple_seq_add_stmt_without_update (&stmts, convert2);
    3024                 :             : 
    3025                 :         107 :           stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2);
    3026                 :         107 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    3027                 :             :         }
    3028                 :             : 
    3029                 :         110 :       gsi_replace_with_seq_vops (gsi, stmts);
    3030                 :         110 :       return true;
    3031                 :             :     }
    3032                 :             : 
    3033                 :             :   /* If BOUND is greater than the length of one constant string,
    3034                 :             :      and the other argument is also a nul-terminated string, replace
    3035                 :             :      strncmp with strcmp.  */
    3036                 :     1339181 :   if (fcode == BUILT_IN_STRNCMP
    3037                 :       19568 :       && bound > 0 && bound < HOST_WIDE_INT_M1U
    3038                 :       13453 :       && ((p2 && len2 < bound && len2 == nulpos2)
    3039                 :       13223 :           || (p1 && len1 < bound && len1 == nulpos1)))
    3040                 :             :     {
    3041                 :     1339181 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCMP);
    3042                 :         308 :       if (!fn)
    3043                 :             :         return false;
    3044                 :         308 :       gimple *repl = gimple_build_call (fn, 2, str1, str2);
    3045                 :         308 :       replace_call_with_call_and_fold (gsi, repl);
    3046                 :         308 :       return true;
    3047                 :             :     }
    3048                 :             : 
    3049                 :             :   return false;
    3050                 :             : }
    3051                 :             : 
    3052                 :             : /* Fold a call to the memchr pointed by GSI iterator.  */
    3053                 :             : 
    3054                 :             : static bool
    3055                 :       25754 : gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
    3056                 :             : {
    3057                 :       25754 :   gimple *stmt = gsi_stmt (*gsi);
    3058                 :       25754 :   tree lhs = gimple_call_lhs (stmt);
    3059                 :       25754 :   tree arg1 = gimple_call_arg (stmt, 0);
    3060                 :       25754 :   tree arg2 = gimple_call_arg (stmt, 1);
    3061                 :       25754 :   tree len = gimple_call_arg (stmt, 2);
    3062                 :             : 
    3063                 :             :   /* If the LEN parameter is zero, return zero.  */
    3064                 :       25754 :   if (integer_zerop (len))
    3065                 :             :     {
    3066                 :           1 :       replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
    3067                 :           1 :       return true;
    3068                 :             :     }
    3069                 :             : 
    3070                 :       25753 :   char c;
    3071                 :       25753 :   if (TREE_CODE (arg2) != INTEGER_CST
    3072                 :       14902 :       || !tree_fits_uhwi_p (len)
    3073                 :       26489 :       || !target_char_cst_p (arg2, &c))
    3074                 :       25017 :     return false;
    3075                 :             : 
    3076                 :         736 :   unsigned HOST_WIDE_INT length = tree_to_uhwi (len);
    3077                 :         736 :   unsigned HOST_WIDE_INT string_length;
    3078                 :         736 :   const char *p1 = getbyterep (arg1, &string_length);
    3079                 :             : 
    3080                 :         736 :   if (p1)
    3081                 :             :     {
    3082                 :         100 :       const char *r = (const char *)memchr (p1, c, MIN (length, string_length));
    3083                 :         100 :       if (r == NULL)
    3084                 :             :         {
    3085                 :          17 :           tree mem_size, offset_node;
    3086                 :          17 :           byte_representation (arg1, &offset_node, &mem_size, NULL);
    3087                 :          34 :           unsigned HOST_WIDE_INT offset = (offset_node == NULL_TREE)
    3088                 :          17 :                                           ? 0 : tree_to_uhwi (offset_node);
    3089                 :             :           /* MEM_SIZE is the size of the array the string literal
    3090                 :             :              is stored in.  */
    3091                 :          17 :           unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size) - offset;
    3092                 :          17 :           gcc_checking_assert (string_length <= string_size);
    3093                 :          17 :           if (length <= string_size)
    3094                 :             :             {
    3095                 :           5 :               replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
    3096                 :           5 :               return true;
    3097                 :             :             }
    3098                 :             :         }
    3099                 :             :       else
    3100                 :             :         {
    3101                 :          83 :           unsigned HOST_WIDE_INT offset = r - p1;
    3102                 :          83 :           gimple_seq stmts = NULL;
    3103                 :          83 :           if (lhs != NULL_TREE)
    3104                 :             :             {
    3105                 :          81 :               tree offset_cst = build_int_cst (sizetype, offset);
    3106                 :          81 :               gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR,
    3107                 :             :                                                    arg1, offset_cst);
    3108                 :          81 :               gimple_seq_add_stmt_without_update (&stmts, stmt);
    3109                 :             :             }
    3110                 :             :           else
    3111                 :           2 :             gimple_seq_add_stmt_without_update (&stmts,
    3112                 :             :                                                 gimple_build_nop ());
    3113                 :             : 
    3114                 :          83 :           gsi_replace_with_seq_vops (gsi, stmts);
    3115                 :          83 :           return true;
    3116                 :             :         }
    3117                 :             :     }
    3118                 :             : 
    3119                 :             :   return false;
    3120                 :             : }
    3121                 :             : 
    3122                 :             : /* Fold a call to the fputs builtin.  ARG0 and ARG1 are the arguments
    3123                 :             :    to the call.  IGNORE is true if the value returned
    3124                 :             :    by the builtin will be ignored.  UNLOCKED is true is true if this
    3125                 :             :    actually a call to fputs_unlocked.  If LEN in non-NULL, it represents
    3126                 :             :    the known length of the string.  Return NULL_TREE if no simplification
    3127                 :             :    was possible.  */
    3128                 :             : 
    3129                 :             : static bool
    3130                 :       21962 : gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
    3131                 :             :                            tree arg0, tree arg1,
    3132                 :             :                            bool unlocked)
    3133                 :             : {
    3134                 :       21962 :   gimple *stmt = gsi_stmt (*gsi);
    3135                 :             : 
    3136                 :             :   /* If we're using an unlocked function, assume the other unlocked
    3137                 :             :      functions exist explicitly.  */
    3138                 :       21962 :   tree const fn_fputc = (unlocked
    3139                 :       21962 :                          ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
    3140                 :       21962 :                          : builtin_decl_implicit (BUILT_IN_FPUTC));
    3141                 :       21918 :   tree const fn_fwrite = (unlocked
    3142                 :          44 :                           ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
    3143                 :       21962 :                           : builtin_decl_implicit (BUILT_IN_FWRITE));
    3144                 :             : 
    3145                 :             :   /* If the return value is used, don't do the transformation.  */
    3146                 :       21962 :   if (gimple_call_lhs (stmt))
    3147                 :             :     return false;
    3148                 :             : 
    3149                 :             :   /* Get the length of the string passed to fputs.  If the length
    3150                 :             :      can't be determined, punt.  */
    3151                 :       21882 :   tree len = get_maxval_strlen (arg0, SRK_STRLEN);
    3152                 :       21882 :   if (!len || TREE_CODE (len) != INTEGER_CST)
    3153                 :             :     return false;
    3154                 :             : 
    3155                 :       16900 :   switch (compare_tree_int (len, 1))
    3156                 :             :     {
    3157                 :          91 :     case -1: /* length is 0, delete the call entirely .  */
    3158                 :          91 :       replace_call_with_value (gsi, integer_zero_node);
    3159                 :          91 :       return true;
    3160                 :             : 
    3161                 :        1045 :     case 0: /* length is 1, call fputc.  */
    3162                 :        1045 :       {
    3163                 :        1045 :         const char *p = c_getstr (arg0);
    3164                 :        1045 :         if (p != NULL)
    3165                 :             :           {
    3166                 :        2060 :             if (!fn_fputc || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3167                 :             :               return false;
    3168                 :             : 
    3169                 :        1030 :             gimple *repl
    3170                 :        1030 :               = gimple_build_call (fn_fputc, 2,
    3171                 :        1030 :                                    build_int_cst (integer_type_node, p[0]),
    3172                 :             :                                    arg1);
    3173                 :        1030 :             replace_call_with_call_and_fold (gsi, repl);
    3174                 :        1030 :             return true;
    3175                 :             :           }
    3176                 :             :       }
    3177                 :             :       /* FALLTHROUGH */
    3178                 :       15779 :     case 1: /* length is greater than 1, call fwrite.  */
    3179                 :       15779 :       {
    3180                 :             :         /* If optimizing for size keep fputs.  */
    3181                 :       15779 :         if (optimize_function_for_size_p (cfun))
    3182                 :             :           return false;
    3183                 :             :         /* New argument list transforming fputs(string, stream) to
    3184                 :             :            fwrite(string, 1, len, stream).  */
    3185                 :       28957 :         if (!fn_fwrite || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3186                 :             :           return false;
    3187                 :             : 
    3188                 :        8116 :         gimple *repl
    3189                 :        8116 :           = gimple_build_call (fn_fwrite, 4, arg0, size_one_node,
    3190                 :             :                                fold_convert (size_type_node, len), arg1);
    3191                 :        8116 :         replace_call_with_call_and_fold (gsi, repl);
    3192                 :        8116 :         return true;
    3193                 :             :       }
    3194                 :           0 :     default:
    3195                 :           0 :       gcc_unreachable ();
    3196                 :             :     }
    3197                 :             : }
    3198                 :             : 
    3199                 :             : /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
    3200                 :             :    DEST, SRC, LEN, and SIZE are the arguments to the call.
    3201                 :             :    IGNORE is true, if return value can be ignored.  FCODE is the BUILT_IN_*
    3202                 :             :    code of the builtin.  If MAXLEN is not NULL, it is maximum length
    3203                 :             :    passed as third argument.  */
    3204                 :             : 
    3205                 :             : static bool
    3206                 :       26052 : gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
    3207                 :             :                                 tree dest, tree src, tree len, tree size,
    3208                 :             :                                 enum built_in_function fcode)
    3209                 :             : {
    3210                 :       26052 :   gimple *stmt = gsi_stmt (*gsi);
    3211                 :       26052 :   location_t loc = gimple_location (stmt);
    3212                 :       26052 :   bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
    3213                 :       26052 :   tree fn;
    3214                 :             : 
    3215                 :             :   /* If SRC and DEST are the same (and not volatile), return DEST
    3216                 :             :      (resp. DEST+LEN for __mempcpy_chk).  */
    3217                 :       26052 :   if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
    3218                 :             :     {
    3219                 :          13 :       if (fcode != BUILT_IN_MEMPCPY_CHK)
    3220                 :             :         {
    3221                 :           7 :           replace_call_with_value (gsi, dest);
    3222                 :           7 :           return true;
    3223                 :             :         }
    3224                 :             :       else
    3225                 :             :         {
    3226                 :           6 :           gimple_seq stmts = NULL;
    3227                 :           6 :           len = gimple_convert_to_ptrofftype (&stmts, loc, len);
    3228                 :           6 :           tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
    3229                 :           6 :                                     TREE_TYPE (dest), dest, len);
    3230                 :           6 :           gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3231                 :           6 :           replace_call_with_value (gsi, temp);
    3232                 :           6 :           return true;
    3233                 :             :         }
    3234                 :             :     }
    3235                 :             : 
    3236                 :       69565 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3237                 :             :     return false;
    3238                 :             : 
    3239                 :       26039 :   tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
    3240                 :       26039 :   if (! integer_all_onesp (size)
    3241                 :       24975 :       && !known_lower (stmt, len, size)
    3242                 :       43643 :       && !known_lower (stmt, maxlen, size))
    3243                 :             :     {
    3244                 :             :       /* MAXLEN and LEN both cannot be proved to be less than SIZE, at
    3245                 :             :          least try to optimize (void) __mempcpy_chk () into
    3246                 :             :          (void) __memcpy_chk () */
    3247                 :       17527 :       if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
    3248                 :             :         {
    3249                 :          40 :           fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
    3250                 :          40 :           if (!fn)
    3251                 :             :             return false;
    3252                 :             : 
    3253                 :          40 :           gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
    3254                 :          40 :           replace_call_with_call_and_fold (gsi, repl);
    3255                 :          40 :           return true;
    3256                 :             :         }
    3257                 :             :       return false;
    3258                 :             :     }
    3259                 :             : 
    3260                 :        8512 :   fn = NULL_TREE;
    3261                 :             :   /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
    3262                 :             :      mem{cpy,pcpy,move,set} is available.  */
    3263                 :        8512 :   switch (fcode)
    3264                 :             :     {
    3265                 :        1764 :     case BUILT_IN_MEMCPY_CHK:
    3266                 :        1764 :       fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
    3267                 :        1764 :       break;
    3268                 :        1071 :     case BUILT_IN_MEMPCPY_CHK:
    3269                 :        1071 :       fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
    3270                 :        1071 :       break;
    3271                 :        1657 :     case BUILT_IN_MEMMOVE_CHK:
    3272                 :        1657 :       fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
    3273                 :        1657 :       break;
    3274                 :        4020 :     case BUILT_IN_MEMSET_CHK:
    3275                 :        4020 :       fn = builtin_decl_explicit (BUILT_IN_MEMSET);
    3276                 :        4020 :       break;
    3277                 :             :     default:
    3278                 :             :       break;
    3279                 :             :     }
    3280                 :             : 
    3281                 :        8512 :   if (!fn)
    3282                 :             :     return false;
    3283                 :             : 
    3284                 :        8512 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    3285                 :        8512 :   replace_call_with_call_and_fold (gsi, repl);
    3286                 :        8512 :   return true;
    3287                 :             : }
    3288                 :             : 
    3289                 :             : /* Fold a call to the __st[rp]cpy_chk builtin.
    3290                 :             :    DEST, SRC, and SIZE are the arguments to the call.
    3291                 :             :    IGNORE is true if return value can be ignored.  FCODE is the BUILT_IN_*
    3292                 :             :    code of the builtin.  If MAXLEN is not NULL, it is maximum length of
    3293                 :             :    strings passed as second argument.  */
    3294                 :             : 
    3295                 :             : static bool
    3296                 :        2791 : gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
    3297                 :             :                                 tree dest,
    3298                 :             :                                 tree src, tree size,
    3299                 :             :                                 enum built_in_function fcode)
    3300                 :             : {
    3301                 :        2791 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3302                 :        2791 :   location_t loc = gimple_location (stmt);
    3303                 :        2791 :   bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
    3304                 :        2791 :   tree len, fn;
    3305                 :             : 
    3306                 :             :   /* If SRC and DEST are the same (and not volatile), return DEST.  */
    3307                 :        2791 :   if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
    3308                 :             :     {
    3309                 :             :       /* Issue -Wrestrict unless the pointers are null (those do
    3310                 :             :          not point to objects and so do not indicate an overlap;
    3311                 :             :          such calls could be the result of sanitization and jump
    3312                 :             :          threading).  */
    3313                 :           0 :       if (!integer_zerop (dest)
    3314                 :           0 :           && !warning_suppressed_p (stmt, OPT_Wrestrict))
    3315                 :             :         {
    3316                 :           0 :           tree func = gimple_call_fndecl (stmt);
    3317                 :             : 
    3318                 :           0 :           warning_at (loc, OPT_Wrestrict,
    3319                 :             :                       "%qD source argument is the same as destination",
    3320                 :             :                       func);
    3321                 :             :         }
    3322                 :             : 
    3323                 :           0 :       replace_call_with_value (gsi, dest);
    3324                 :           0 :       return true;
    3325                 :             :     }
    3326                 :             : 
    3327                 :        5582 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3328                 :             :     return false;
    3329                 :             : 
    3330                 :        2791 :   tree maxlen = get_maxval_strlen (src, SRK_STRLENMAX);
    3331                 :        2791 :   if (! integer_all_onesp (size))
    3332                 :             :     {
    3333                 :        2722 :       len = c_strlen (src, 1);
    3334                 :        2722 :       if (!known_lower (stmt, len, size, true)
    3335                 :        2722 :           && !known_lower (stmt, maxlen, size, true))
    3336                 :             :         {
    3337                 :        2386 :           if (fcode == BUILT_IN_STPCPY_CHK)
    3338                 :             :             {
    3339                 :        1193 :               if (! ignore)
    3340                 :             :                 return false;
    3341                 :             : 
    3342                 :             :               /* If return value of __stpcpy_chk is ignored,
    3343                 :             :                  optimize into __strcpy_chk.  */
    3344                 :          37 :               fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
    3345                 :          37 :               if (!fn)
    3346                 :             :                 return false;
    3347                 :             : 
    3348                 :          37 :               gimple *repl = gimple_build_call (fn, 3, dest, src, size);
    3349                 :          37 :               replace_call_with_call_and_fold (gsi, repl);
    3350                 :          37 :               return true;
    3351                 :             :             }
    3352                 :             : 
    3353                 :        1193 :           if (! len || TREE_SIDE_EFFECTS (len))
    3354                 :             :             return false;
    3355                 :             : 
    3356                 :             :           /* If c_strlen returned something, but not provably less than size,
    3357                 :             :              transform __strcpy_chk into __memcpy_chk.  */
    3358                 :         106 :           fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
    3359                 :         106 :           if (!fn)
    3360                 :             :             return false;
    3361                 :             : 
    3362                 :         106 :           gimple_seq stmts = NULL;
    3363                 :         106 :           len = force_gimple_operand (len, &stmts, true, NULL_TREE);
    3364                 :         106 :           len = gimple_convert (&stmts, loc, size_type_node, len);
    3365                 :         106 :           len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len,
    3366                 :         106 :                               build_int_cst (size_type_node, 1));
    3367                 :         106 :           gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3368                 :         106 :           gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
    3369                 :         106 :           replace_call_with_call_and_fold (gsi, repl);
    3370                 :         106 :           return true;
    3371                 :             :         }
    3372                 :             :     }
    3373                 :             : 
    3374                 :             :   /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available.  */
    3375                 :         662 :   fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK && !ignore
    3376                 :             :                               ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
    3377                 :         405 :   if (!fn)
    3378                 :             :     return false;
    3379                 :             : 
    3380                 :         405 :   gcall *repl = gimple_build_call (fn, 2, dest, src);
    3381                 :         405 :   replace_call_with_call_and_fold (gsi, repl);
    3382                 :         405 :   return true;
    3383                 :             : }
    3384                 :             : 
    3385                 :             : /* Fold a call to the __st{r,p}ncpy_chk builtin.  DEST, SRC, LEN, and SIZE
    3386                 :             :    are the arguments to the call.  If MAXLEN is not NULL, it is maximum
    3387                 :             :    length passed as third argument. IGNORE is true if return value can be
    3388                 :             :    ignored. FCODE is the BUILT_IN_* code of the builtin. */
    3389                 :             : 
    3390                 :             : static bool
    3391                 :        2878 : gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
    3392                 :             :                                  tree dest, tree src,
    3393                 :             :                                  tree len, tree size,
    3394                 :             :                                  enum built_in_function fcode)
    3395                 :             : {
    3396                 :        2878 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3397                 :        2878 :   bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
    3398                 :        2878 :   tree fn;
    3399                 :             : 
    3400                 :        2878 :   tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
    3401                 :        2878 :   if (! integer_all_onesp (size)
    3402                 :        2878 :       && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
    3403                 :             :     {
    3404                 :        2421 :       if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
    3405                 :             :         {
    3406                 :             :           /* If return value of __stpncpy_chk is ignored,
    3407                 :             :              optimize into __strncpy_chk.  */
    3408                 :          39 :           fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
    3409                 :          39 :           if (fn)
    3410                 :             :             {
    3411                 :          39 :               gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
    3412                 :          39 :               replace_call_with_call_and_fold (gsi, repl);
    3413                 :          39 :               return true;
    3414                 :             :             }
    3415                 :             :         }
    3416                 :             :       return false;
    3417                 :             :     }
    3418                 :             : 
    3419                 :             :   /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available.  */
    3420                 :         717 :   fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK && !ignore
    3421                 :             :                               ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
    3422                 :        3296 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3423                 :             :     return false;
    3424                 :             : 
    3425                 :         457 :   gcall *repl = gimple_build_call (fn, 3, dest, src, len);
    3426                 :         457 :   replace_call_with_call_and_fold (gsi, repl);
    3427                 :         457 :   return true;
    3428                 :             : }
    3429                 :             : 
    3430                 :             : /* Fold function call to builtin stpcpy with arguments DEST and SRC.
    3431                 :             :    Return NULL_TREE if no simplification can be made.  */
    3432                 :             : 
    3433                 :             : static bool
    3434                 :        4623 : gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
    3435                 :             : {
    3436                 :        4623 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3437                 :        4623 :   location_t loc = gimple_location (stmt);
    3438                 :        4623 :   tree dest = gimple_call_arg (stmt, 0);
    3439                 :        4623 :   tree src = gimple_call_arg (stmt, 1);
    3440                 :        4623 :   tree fn, lenp1;
    3441                 :             : 
    3442                 :        9246 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3443                 :             :     return false;
    3444                 :             : 
    3445                 :             :   /* If the result is unused, replace stpcpy with strcpy.  */
    3446                 :        4623 :   if (gimple_call_lhs (stmt) == NULL_TREE)
    3447                 :             :     {
    3448                 :          30 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3449                 :          30 :       if (!fn)
    3450                 :             :         return false;
    3451                 :          30 :       gimple_call_set_fndecl (stmt, fn);
    3452                 :          30 :       fold_stmt (gsi);
    3453                 :          30 :       return true;
    3454                 :             :     }
    3455                 :             : 
    3456                 :             :   /* Set to non-null if ARG refers to an unterminated array.  */
    3457                 :        4593 :   c_strlen_data data = { };
    3458                 :             :   /* The size of the unterminated array if SRC referes to one.  */
    3459                 :        4593 :   tree size;
    3460                 :             :   /* True if the size is exact/constant, false if it's the lower bound
    3461                 :             :      of a range.  */
    3462                 :        4593 :   bool exact;
    3463                 :        4593 :   tree len = c_strlen (src, 1, &data, 1);
    3464                 :        4593 :   if (!len
    3465                 :         757 :       || TREE_CODE (len) != INTEGER_CST)
    3466                 :             :     {
    3467                 :        4096 :       data.decl = unterminated_array (src, &size, &exact);
    3468                 :        4096 :       if (!data.decl)
    3469                 :             :         return false;
    3470                 :             :     }
    3471                 :             : 
    3472                 :        1172 :   if (data.decl)
    3473                 :             :     {
    3474                 :             :       /* Avoid folding calls with unterminated arrays.  */
    3475                 :         675 :       if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
    3476                 :          75 :         warn_string_no_nul (loc, stmt, "stpcpy", src, data.decl, size,
    3477                 :             :                             exact);
    3478                 :         675 :       suppress_warning (stmt, OPT_Wstringop_overread);
    3479                 :         675 :       return false;
    3480                 :             :     }
    3481                 :             : 
    3482                 :         497 :   if (optimize_function_for_size_p (cfun)
    3483                 :             :       /* If length is zero it's small enough.  */
    3484                 :         497 :       && !integer_zerop (len))
    3485                 :             :     return false;
    3486                 :             : 
    3487                 :             :   /* If the source has a known length replace stpcpy with memcpy.  */
    3488                 :        4593 :   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    3489                 :         290 :   if (!fn)
    3490                 :             :     return false;
    3491                 :             : 
    3492                 :         290 :   gimple_seq stmts = NULL;
    3493                 :         290 :   tree tem = gimple_convert (&stmts, loc, size_type_node, len);
    3494                 :         290 :   lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
    3495                 :         290 :                         tem, build_int_cst (size_type_node, 1));
    3496                 :         290 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3497                 :         290 :   gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
    3498                 :         290 :   gimple_move_vops (repl, stmt);
    3499                 :         290 :   gsi_insert_before (gsi, repl, GSI_SAME_STMT);
    3500                 :             :   /* Replace the result with dest + len.  */
    3501                 :         290 :   stmts = NULL;
    3502                 :         290 :   tem = gimple_convert (&stmts, loc, sizetype, len);
    3503                 :         290 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3504                 :         290 :   gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
    3505                 :             :                                       POINTER_PLUS_EXPR, dest, tem);
    3506                 :         290 :   gsi_replace (gsi, ret, false);
    3507                 :             :   /* Finally fold the memcpy call.  */
    3508                 :         290 :   gimple_stmt_iterator gsi2 = *gsi;
    3509                 :         290 :   gsi_prev (&gsi2);
    3510                 :         290 :   fold_stmt (&gsi2);
    3511                 :         290 :   return true;
    3512                 :             : }
    3513                 :             : 
    3514                 :             : /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS.  Return
    3515                 :             :    NULL_TREE if a normal call should be emitted rather than expanding
    3516                 :             :    the function inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
    3517                 :             :    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
    3518                 :             :    passed as second argument.  */
    3519                 :             : 
    3520                 :             : static bool
    3521                 :        2917 : gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
    3522                 :             :                                   enum built_in_function fcode)
    3523                 :             : {
    3524                 :        2917 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3525                 :        2917 :   tree dest, size, len, fn, fmt, flag;
    3526                 :        2917 :   const char *fmt_str;
    3527                 :             : 
    3528                 :             :   /* Verify the required arguments in the original call.  */
    3529                 :        2917 :   if (gimple_call_num_args (stmt) < 5)
    3530                 :             :     return false;
    3531                 :             : 
    3532                 :        2917 :   dest = gimple_call_arg (stmt, 0);
    3533                 :        2917 :   len = gimple_call_arg (stmt, 1);
    3534                 :        2917 :   flag = gimple_call_arg (stmt, 2);
    3535                 :        2917 :   size = gimple_call_arg (stmt, 3);
    3536                 :        2917 :   fmt = gimple_call_arg (stmt, 4);
    3537                 :             : 
    3538                 :        2917 :   tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
    3539                 :        2917 :   if (! integer_all_onesp (size)
    3540                 :        2917 :       && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
    3541                 :             :     return false;
    3542                 :             : 
    3543                 :         314 :   if (!init_target_chars ())
    3544                 :             :     return false;
    3545                 :             : 
    3546                 :             :   /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
    3547                 :             :      or if format doesn't contain % chars or is "%s".  */
    3548                 :         314 :   if (! integer_zerop (flag))
    3549                 :             :     {
    3550                 :          58 :       fmt_str = c_getstr (fmt);
    3551                 :          58 :       if (fmt_str == NULL)
    3552                 :             :         return false;
    3553                 :          58 :       if (strchr (fmt_str, target_percent) != NULL
    3554                 :          57 :           && strcmp (fmt_str, target_percent_s))
    3555                 :             :         return false;
    3556                 :             :     }
    3557                 :             : 
    3558                 :             :   /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
    3559                 :             :      available.  */
    3560                 :         415 :   fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
    3561                 :             :                               ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
    3562                 :        3176 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3563                 :             :     return false;
    3564                 :             : 
    3565                 :             :   /* Replace the called function and the first 5 argument by 3 retaining
    3566                 :             :      trailing varargs.  */
    3567                 :         259 :   gimple_call_set_fndecl (stmt, fn);
    3568                 :         259 :   gimple_call_set_fntype (stmt, TREE_TYPE (fn));
    3569                 :         259 :   gimple_call_set_arg (stmt, 0, dest);
    3570                 :         259 :   gimple_call_set_arg (stmt, 1, len);
    3571                 :         259 :   gimple_call_set_arg (stmt, 2, fmt);
    3572                 :         546 :   for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
    3573                 :         287 :     gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
    3574                 :         259 :   gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
    3575                 :         259 :   fold_stmt (gsi);
    3576                 :         259 :   return true;
    3577                 :             : }
    3578                 :             : 
    3579                 :             : /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
    3580                 :             :    Return NULL_TREE if a normal call should be emitted rather than
    3581                 :             :    expanding the function inline.  FCODE is either BUILT_IN_SPRINTF_CHK
    3582                 :             :    or BUILT_IN_VSPRINTF_CHK.  */
    3583                 :             : 
    3584                 :             : static bool
    3585                 :        5105 : gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
    3586                 :             :                                  enum built_in_function fcode)
    3587                 :             : {
    3588                 :        5105 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3589                 :        5105 :   tree dest, size, len, fn, fmt, flag;
    3590                 :        5105 :   const char *fmt_str;
    3591                 :        5105 :   unsigned nargs = gimple_call_num_args (stmt);
    3592                 :             : 
    3593                 :             :   /* Verify the required arguments in the original call.  */
    3594                 :        5105 :   if (nargs < 4)
    3595                 :             :     return false;
    3596                 :        5105 :   dest = gimple_call_arg (stmt, 0);
    3597                 :        5105 :   flag = gimple_call_arg (stmt, 1);
    3598                 :        5105 :   size = gimple_call_arg (stmt, 2);
    3599                 :        5105 :   fmt = gimple_call_arg (stmt, 3);
    3600                 :             : 
    3601                 :        5105 :   len = NULL_TREE;
    3602                 :             : 
    3603                 :        5105 :   if (!init_target_chars ())
    3604                 :             :     return false;
    3605                 :             : 
    3606                 :             :   /* Check whether the format is a literal string constant.  */
    3607                 :        5105 :   fmt_str = c_getstr (fmt);
    3608                 :        5105 :   if (fmt_str != NULL)
    3609                 :             :     {
    3610                 :             :       /* If the format doesn't contain % args or %%, we know the size.  */
    3611                 :        4681 :       if (strchr (fmt_str, target_percent) == 0)
    3612                 :             :         {
    3613                 :         322 :           if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
    3614                 :         322 :             len = build_int_cstu (size_type_node, strlen (fmt_str));
    3615                 :             :         }
    3616                 :             :       /* If the format is "%s" and first ... argument is a string literal,
    3617                 :             :          we know the size too.  */
    3618                 :        4359 :       else if (fcode == BUILT_IN_SPRINTF_CHK
    3619                 :        3400 :                && strcmp (fmt_str, target_percent_s) == 0)
    3620                 :             :         {
    3621                 :         490 :           tree arg;
    3622                 :             : 
    3623                 :         490 :           if (nargs == 5)
    3624                 :             :             {
    3625                 :         490 :               arg = gimple_call_arg (stmt, 4);
    3626                 :         490 :               if (POINTER_TYPE_P (TREE_TYPE (arg)))
    3627                 :         454 :                 len = c_strlen (arg, 1);
    3628                 :             :             }
    3629                 :             :         }
    3630                 :             :     }
    3631                 :             : 
    3632                 :        5105 :   if (! integer_all_onesp (size) && !known_lower (stmt, len, size, true))
    3633                 :             :     return false;
    3634                 :             : 
    3635                 :             :   /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
    3636                 :             :      or if format doesn't contain % chars or is "%s".  */
    3637                 :         202 :   if (! integer_zerop (flag))
    3638                 :             :     {
    3639                 :           1 :       if (fmt_str == NULL)
    3640                 :             :         return false;
    3641                 :           1 :       if (strchr (fmt_str, target_percent) != NULL
    3642                 :           0 :           && strcmp (fmt_str, target_percent_s))
    3643                 :             :         return false;
    3644                 :             :     }
    3645                 :             : 
    3646                 :             :   /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available.  */
    3647                 :         347 :   fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
    3648                 :             :                               ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
    3649                 :        5307 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3650                 :             :     return false;
    3651                 :             : 
    3652                 :             :   /* Replace the called function and the first 4 argument by 2 retaining
    3653                 :             :      trailing varargs.  */
    3654                 :         202 :   gimple_call_set_fndecl (stmt, fn);
    3655                 :         202 :   gimple_call_set_fntype (stmt, TREE_TYPE (fn));
    3656                 :         202 :   gimple_call_set_arg (stmt, 0, dest);
    3657                 :         202 :   gimple_call_set_arg (stmt, 1, fmt);
    3658                 :         400 :   for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
    3659                 :         198 :     gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
    3660                 :         202 :   gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
    3661                 :         202 :   fold_stmt (gsi);
    3662                 :         202 :   return true;
    3663                 :             : }
    3664                 :             : 
    3665                 :             : /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
    3666                 :             :    ORIG may be null if this is a 2-argument call.  We don't attempt to
    3667                 :             :    simplify calls with more than 3 arguments.
    3668                 :             : 
    3669                 :             :    Return true if simplification was possible, otherwise false.  */
    3670                 :             : 
    3671                 :             : bool
    3672                 :        2263 : gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
    3673                 :             : {
    3674                 :        2263 :   gimple *stmt = gsi_stmt (*gsi);
    3675                 :             : 
    3676                 :             :   /* Verify the required arguments in the original call.  We deal with two
    3677                 :             :      types of sprintf() calls: 'sprintf (str, fmt)' and
    3678                 :             :      'sprintf (dest, "%s", orig)'.  */
    3679                 :        2263 :   if (gimple_call_num_args (stmt) > 3)
    3680                 :             :     return false;
    3681                 :             : 
    3682                 :        1862 :   tree orig = NULL_TREE;
    3683                 :        1862 :   if (gimple_call_num_args (stmt) == 3)
    3684                 :        1766 :     orig = gimple_call_arg (stmt, 2);
    3685                 :             : 
    3686                 :             :   /* Check whether the format is a literal string constant.  */
    3687                 :        1862 :   tree fmt = gimple_call_arg (stmt, 1);
    3688                 :        1862 :   const char *fmt_str = c_getstr (fmt);
    3689                 :        1862 :   if (fmt_str == NULL)
    3690                 :             :     return false;
    3691                 :             : 
    3692                 :        1862 :   tree dest = gimple_call_arg (stmt, 0);
    3693                 :             : 
    3694                 :        1862 :   if (!init_target_chars ())
    3695                 :             :     return false;
    3696                 :             : 
    3697                 :        1862 :   tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3698                 :        5133 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3699                 :             :     return false;
    3700                 :             : 
    3701                 :             :   /* If the format doesn't contain % args or %%, use strcpy.  */
    3702                 :        1862 :   if (strchr (fmt_str, target_percent) == NULL)
    3703                 :             :     {
    3704                 :             :       /* Don't optimize sprintf (buf, "abc", ptr++).  */
    3705                 :         108 :       if (orig)
    3706                 :             :         return false;
    3707                 :             : 
    3708                 :             :       /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
    3709                 :             :          'format' is known to contain no % formats.  */
    3710                 :          95 :       gimple_seq stmts = NULL;
    3711                 :          95 :       gimple *repl = gimple_build_call (fn, 2, dest, fmt);
    3712                 :             : 
    3713                 :             :       /* Propagate the NO_WARNING bit to avoid issuing the same
    3714                 :             :          warning more than once.  */
    3715                 :          95 :       copy_warning (repl, stmt);
    3716                 :             : 
    3717                 :          95 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3718                 :          95 :       if (tree lhs = gimple_call_lhs (stmt))
    3719                 :             :         {
    3720                 :           0 :           repl = gimple_build_assign (lhs, build_int_cst (TREE_TYPE (lhs),
    3721                 :           0 :                                                           strlen (fmt_str)));
    3722                 :           0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3723                 :           0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3724                 :             :           /* gsi now points at the assignment to the lhs, get a
    3725                 :             :              stmt iterator to the memcpy call.
    3726                 :             :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3727                 :             :              CFG isn't built yet.  */
    3728                 :           0 :           gimple_stmt_iterator gsi2 = *gsi;
    3729                 :           0 :           gsi_prev (&gsi2);
    3730                 :           0 :           fold_stmt (&gsi2);
    3731                 :             :         }
    3732                 :             :       else
    3733                 :             :         {
    3734                 :          95 :           gsi_replace_with_seq_vops (gsi, stmts);
    3735                 :          95 :           fold_stmt (gsi);
    3736                 :             :         }
    3737                 :          95 :       return true;
    3738                 :             :     }
    3739                 :             : 
    3740                 :             :   /* If the format is "%s", use strcpy if the result isn't used.  */
    3741                 :        1754 :   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
    3742                 :             :     {
    3743                 :             :       /* Don't crash on sprintf (str1, "%s").  */
    3744                 :         746 :       if (!orig)
    3745                 :             :         return false;
    3746                 :             : 
    3747                 :             :       /* Don't fold calls with source arguments of invalid (nonpointer)
    3748                 :             :          types.  */
    3749                 :         745 :       if (!POINTER_TYPE_P (TREE_TYPE (orig)))
    3750                 :             :         return false;
    3751                 :             : 
    3752                 :         739 :       tree orig_len = NULL_TREE;
    3753                 :         739 :       if (gimple_call_lhs (stmt))
    3754                 :             :         {
    3755                 :          17 :           orig_len = get_maxval_strlen (orig, SRK_STRLEN);
    3756                 :          17 :           if (!orig_len)
    3757                 :             :             return false;
    3758                 :             :         }
    3759                 :             : 
    3760                 :             :       /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2).  */
    3761                 :         722 :       gimple_seq stmts = NULL;
    3762                 :         722 :       gimple *repl = gimple_build_call (fn, 2, dest, orig);
    3763                 :             : 
    3764                 :             :       /* Propagate the NO_WARNING bit to avoid issuing the same
    3765                 :             :          warning more than once.  */
    3766                 :         722 :       copy_warning (repl, stmt);
    3767                 :             : 
    3768                 :         722 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3769                 :         722 :       if (tree lhs = gimple_call_lhs (stmt))
    3770                 :             :         {
    3771                 :           0 :           if (!useless_type_conversion_p (TREE_TYPE (lhs),
    3772                 :           0 :                                           TREE_TYPE (orig_len)))
    3773                 :           0 :             orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
    3774                 :           0 :           repl = gimple_build_assign (lhs, orig_len);
    3775                 :           0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3776                 :           0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3777                 :             :           /* gsi now points at the assignment to the lhs, get a
    3778                 :             :              stmt iterator to the memcpy call.
    3779                 :             :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3780                 :             :              CFG isn't built yet.  */
    3781                 :           0 :           gimple_stmt_iterator gsi2 = *gsi;
    3782                 :           0 :           gsi_prev (&gsi2);
    3783                 :           0 :           fold_stmt (&gsi2);
    3784                 :             :         }
    3785                 :             :       else
    3786                 :             :         {
    3787                 :         722 :           gsi_replace_with_seq_vops (gsi, stmts);
    3788                 :         722 :           fold_stmt (gsi);
    3789                 :             :         }
    3790                 :         722 :       return true;
    3791                 :             :     }
    3792                 :             :   return false;
    3793                 :             : }
    3794                 :             : 
    3795                 :             : /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
    3796                 :             :    FMT, and ORIG.  ORIG may be null if this is a 3-argument call.  We don't
    3797                 :             :    attempt to simplify calls with more than 4 arguments.
    3798                 :             : 
    3799                 :             :    Return true if simplification was possible, otherwise false.  */
    3800                 :             : 
    3801                 :             : bool
    3802                 :        1580 : gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
    3803                 :             : {
    3804                 :        1580 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3805                 :        1580 :   tree dest = gimple_call_arg (stmt, 0);
    3806                 :        1580 :   tree destsize = gimple_call_arg (stmt, 1);
    3807                 :        1580 :   tree fmt = gimple_call_arg (stmt, 2);
    3808                 :        1580 :   tree orig = NULL_TREE;
    3809                 :        1580 :   const char *fmt_str = NULL;
    3810                 :             : 
    3811                 :        1580 :   if (gimple_call_num_args (stmt) > 4
    3812                 :        2708 :       || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3813                 :             :     return false;
    3814                 :             : 
    3815                 :         707 :   if (gimple_call_num_args (stmt) == 4)
    3816                 :         588 :     orig = gimple_call_arg (stmt, 3);
    3817                 :             : 
    3818                 :             :   /* Check whether the format is a literal string constant.  */
    3819                 :         707 :   fmt_str = c_getstr (fmt);
    3820                 :         707 :   if (fmt_str == NULL)
    3821                 :             :     return false;
    3822                 :             : 
    3823                 :         707 :   if (!init_target_chars ())
    3824                 :             :     return false;
    3825                 :             : 
    3826                 :             :   /* If the format doesn't contain % args or %%, use strcpy.  */
    3827                 :         707 :   if (strchr (fmt_str, target_percent) == NULL)
    3828                 :             :     {
    3829                 :         148 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3830                 :         118 :       if (!fn)
    3831                 :             :         return false;
    3832                 :             : 
    3833                 :             :       /* Don't optimize snprintf (buf, 4, "abc", ptr++).  */
    3834                 :         118 :       if (orig)
    3835                 :             :         return false;
    3836                 :             : 
    3837                 :         118 :       tree len = build_int_cstu (TREE_TYPE (destsize), strlen (fmt_str));
    3838                 :             : 
    3839                 :             :       /* We could expand this as
    3840                 :             :          memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
    3841                 :             :          or to
    3842                 :             :          memcpy (str, fmt_with_nul_at_cstm1, cst);
    3843                 :             :          but in the former case that might increase code size
    3844                 :             :          and in the latter case grow .rodata section too much.
    3845                 :             :          So punt for now.  */
    3846                 :         118 :       if (!known_lower (stmt, len, destsize, true))
    3847                 :             :         return false;
    3848                 :             : 
    3849                 :          88 :       gimple_seq stmts = NULL;
    3850                 :          88 :       gimple *repl = gimple_build_call (fn, 2, dest, fmt);
    3851                 :          88 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3852                 :          88 :       if (tree lhs = gimple_call_lhs (stmt))
    3853                 :             :         {
    3854                 :           0 :           repl = gimple_build_assign (lhs,
    3855                 :           0 :                                       fold_convert (TREE_TYPE (lhs), len));
    3856                 :           0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3857                 :           0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3858                 :             :           /* gsi now points at the assignment to the lhs, get a
    3859                 :             :              stmt iterator to the memcpy call.
    3860                 :             :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3861                 :             :              CFG isn't built yet.  */
    3862                 :           0 :           gimple_stmt_iterator gsi2 = *gsi;
    3863                 :           0 :           gsi_prev (&gsi2);
    3864                 :           0 :           fold_stmt (&gsi2);
    3865                 :             :         }
    3866                 :             :       else
    3867                 :             :         {
    3868                 :          88 :           gsi_replace_with_seq_vops (gsi, stmts);
    3869                 :          88 :           fold_stmt (gsi);
    3870                 :             :         }
    3871                 :          88 :       return true;
    3872                 :             :     }
    3873                 :             : 
    3874                 :             :   /* If the format is "%s", use strcpy if the result isn't used.  */
    3875                 :         589 :   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
    3876                 :             :     {
    3877                 :         292 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3878                 :         174 :       if (!fn)
    3879                 :             :         return false;
    3880                 :             : 
    3881                 :             :       /* Don't crash on snprintf (str1, cst, "%s").  */
    3882                 :         174 :       if (!orig)
    3883                 :             :         return false;
    3884                 :             : 
    3885                 :         174 :       tree orig_len = get_maxval_strlen (orig, SRK_STRLEN);
    3886                 :             : 
    3887                 :             :       /* We could expand this as
    3888                 :             :          memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
    3889                 :             :          or to
    3890                 :             :          memcpy (str1, str2_with_nul_at_cstm1, cst);
    3891                 :             :          but in the former case that might increase code size
    3892                 :             :          and in the latter case grow .rodata section too much.
    3893                 :             :          So punt for now.  */
    3894                 :         174 :       if (!known_lower (stmt, orig_len, destsize, true))
    3895                 :             :         return false;
    3896                 :             : 
    3897                 :             :       /* Convert snprintf (str1, cst, "%s", str2) into
    3898                 :             :          strcpy (str1, str2) if strlen (str2) < cst.  */
    3899                 :          56 :       gimple_seq stmts = NULL;
    3900                 :          56 :       gimple *repl = gimple_build_call (fn, 2, dest, orig);
    3901                 :          56 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3902                 :          56 :       if (tree lhs = gimple_call_lhs (stmt))
    3903                 :             :         {
    3904                 :           0 :           if (!useless_type_conversion_p (TREE_TYPE (lhs),
    3905                 :           0 :                                           TREE_TYPE (orig_len)))
    3906                 :           0 :             orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
    3907                 :           0 :           repl = gimple_build_assign (lhs, orig_len);
    3908                 :           0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3909                 :           0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3910                 :             :           /* gsi now points at the assignment to the lhs, get a
    3911                 :             :              stmt iterator to the memcpy call.
    3912                 :             :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3913                 :             :              CFG isn't built yet.  */
    3914                 :           0 :           gimple_stmt_iterator gsi2 = *gsi;
    3915                 :           0 :           gsi_prev (&gsi2);
    3916                 :           0 :           fold_stmt (&gsi2);
    3917                 :             :         }
    3918                 :             :       else
    3919                 :             :         {
    3920                 :          56 :           gsi_replace_with_seq_vops (gsi, stmts);
    3921                 :          56 :           fold_stmt (gsi);
    3922                 :             :         }
    3923                 :          56 :       return true;
    3924                 :             :     }
    3925                 :             :   return false;
    3926                 :             : }
    3927                 :             : 
    3928                 :             : /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
    3929                 :             :    FP, FMT, and ARG are the arguments to the call.  We don't fold calls with
    3930                 :             :    more than 3 arguments, and ARG may be null in the 2-argument case.
    3931                 :             : 
    3932                 :             :    Return NULL_TREE if no simplification was possible, otherwise return the
    3933                 :             :    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
    3934                 :             :    code of the function to be simplified.  */
    3935                 :             : 
    3936                 :             : static bool
    3937                 :       59443 : gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
    3938                 :             :                              tree fp, tree fmt, tree arg,
    3939                 :             :                              enum built_in_function fcode)
    3940                 :             : {
    3941                 :       59443 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3942                 :       59443 :   tree fn_fputc, fn_fputs;
    3943                 :       59443 :   const char *fmt_str = NULL;
    3944                 :             : 
    3945                 :             :   /* If the return value is used, don't do the transformation.  */
    3946                 :       59443 :   if (gimple_call_lhs (stmt) != NULL_TREE)
    3947                 :             :     return false;
    3948                 :             : 
    3949                 :             :   /* Check whether the format is a literal string constant.  */
    3950                 :       54749 :   fmt_str = c_getstr (fmt);
    3951                 :       54749 :   if (fmt_str == NULL)
    3952                 :             :     return false;
    3953                 :             : 
    3954                 :       54469 :   if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
    3955                 :             :     {
    3956                 :             :       /* If we're using an unlocked function, assume the other
    3957                 :             :          unlocked functions exist explicitly.  */
    3958                 :          86 :       fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
    3959                 :          86 :       fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
    3960                 :             :     }
    3961                 :             :   else
    3962                 :             :     {
    3963                 :       54383 :       fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
    3964                 :       54383 :       fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
    3965                 :             :     }
    3966                 :             : 
    3967                 :       54469 :   if (!init_target_chars ())
    3968                 :             :     return false;
    3969                 :             : 
    3970                 :      158192 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3971                 :             :     return false;
    3972                 :             : 
    3973                 :             :   /* If the format doesn't contain % args or %%, use strcpy.  */
    3974                 :       54469 :   if (strchr (fmt_str, target_percent) == NULL)
    3975                 :             :     {
    3976                 :        9501 :       if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
    3977                 :        9431 :           && arg)
    3978                 :             :         return false;
    3979                 :             : 
    3980                 :             :       /* If the format specifier was "", fprintf does nothing.  */
    3981                 :        9501 :       if (fmt_str[0] == '\0')
    3982                 :             :         {
    3983                 :          58 :           replace_call_with_value (gsi, NULL_TREE);
    3984                 :          58 :           return true;
    3985                 :             :         }
    3986                 :             : 
    3987                 :             :       /* When "string" doesn't contain %, replace all cases of
    3988                 :             :          fprintf (fp, string) with fputs (string, fp).  The fputs
    3989                 :             :          builtin will take care of special cases like length == 1.  */
    3990                 :        9443 :       if (fn_fputs)
    3991                 :             :         {
    3992                 :        9443 :           gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
    3993                 :        9443 :           replace_call_with_call_and_fold (gsi, repl);
    3994                 :        9443 :           return true;
    3995                 :             :         }
    3996                 :             :     }
    3997                 :             : 
    3998                 :             :   /* The other optimizations can be done only on the non-va_list variants.  */
    3999                 :       44968 :   else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
    4000                 :             :     return false;
    4001                 :             : 
    4002                 :             :   /* If the format specifier was "%s", call __builtin_fputs (arg, fp).  */
    4003                 :       43761 :   else if (strcmp (fmt_str, target_percent_s) == 0)
    4004                 :             :     {
    4005                 :         639 :       if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
    4006                 :             :         return false;
    4007                 :         639 :       if (fn_fputs)
    4008                 :             :         {
    4009                 :         639 :           gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
    4010                 :         639 :           replace_call_with_call_and_fold (gsi, repl);
    4011                 :         639 :           return true;
    4012                 :             :         }
    4013                 :             :     }
    4014                 :             : 
    4015                 :             :   /* If the format specifier was "%c", call __builtin_fputc (arg, fp).  */
    4016                 :       43122 :   else if (strcmp (fmt_str, target_percent_c) == 0)
    4017                 :             :     {
    4018                 :          49 :       if (!arg
    4019                 :          49 :           || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
    4020                 :           0 :         return false;
    4021                 :          49 :       if (fn_fputc)
    4022                 :             :         {
    4023                 :          49 :           gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
    4024                 :          49 :           replace_call_with_call_and_fold (gsi, repl);
    4025                 :          49 :           return true;
    4026                 :             :         }
    4027                 :             :     }
    4028                 :             : 
    4029                 :             :   return false;
    4030                 :             : }
    4031                 :             : 
    4032                 :             : /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
    4033                 :             :    FMT and ARG are the arguments to the call; we don't fold cases with
    4034                 :             :    more than 2 arguments, and ARG may be null if this is a 1-argument case.
    4035                 :             : 
    4036                 :             :    Return NULL_TREE if no simplification was possible, otherwise return the
    4037                 :             :    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
    4038                 :             :    code of the function to be simplified.  */
    4039                 :             : 
    4040                 :             : static bool
    4041                 :      116112 : gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
    4042                 :             :                             tree arg, enum built_in_function fcode)
    4043                 :             : {
    4044                 :      116112 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    4045                 :      116112 :   tree fn_putchar, fn_puts, newarg;
    4046                 :      116112 :   const char *fmt_str = NULL;
    4047                 :             : 
    4048                 :             :   /* If the return value is used, don't do the transformation.  */
    4049                 :      116112 :   if (gimple_call_lhs (stmt) != NULL_TREE)
    4050                 :             :     return false;
    4051                 :             : 
    4052                 :      336151 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    4053                 :             :     return false;
    4054                 :             : 
    4055                 :             :   /* Check whether the format is a literal string constant.  */
    4056                 :      112463 :   fmt_str = c_getstr (fmt);
    4057                 :      112463 :   if (fmt_str == NULL)
    4058                 :             :     return false;
    4059                 :             : 
    4060                 :      109001 :   if (fcode == BUILT_IN_PRINTF_UNLOCKED)
    4061                 :             :     {
    4062                 :             :       /* If we're using an unlocked function, assume the other
    4063                 :             :          unlocked functions exist explicitly.  */
    4064                 :          86 :       fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
    4065                 :          86 :       fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
    4066                 :             :     }
    4067                 :             :   else
    4068                 :             :     {
    4069                 :      108915 :       fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
    4070                 :      108915 :       fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
    4071                 :             :     }
    4072                 :             : 
    4073                 :      109001 :   if (!init_target_chars ())
    4074                 :             :     return false;
    4075                 :             : 
    4076                 :      109001 :   if (strcmp (fmt_str, target_percent_s) == 0
    4077                 :      101920 :       || strchr (fmt_str, target_percent) == NULL)
    4078                 :             :     {
    4079                 :       15048 :       const char *str;
    4080                 :             : 
    4081                 :       15048 :       if (strcmp (fmt_str, target_percent_s) == 0)
    4082                 :             :         {
    4083                 :        7081 :           if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
    4084                 :             :             return false;
    4085                 :             : 
    4086                 :        6757 :           if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
    4087                 :             :             return false;
    4088                 :             : 
    4089                 :        6752 :           str = c_getstr (arg);
    4090                 :        6752 :           if (str == NULL)
    4091                 :             :             return false;
    4092                 :             :         }
    4093                 :             :       else
    4094                 :             :         {
    4095                 :             :           /* The format specifier doesn't contain any '%' characters.  */
    4096                 :        7967 :           if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
    4097                 :        7825 :               && arg)
    4098                 :             :             return false;
    4099                 :             :           str = fmt_str;
    4100                 :             :         }
    4101                 :             : 
    4102                 :             :       /* If the string was "", printf does nothing.  */
    4103                 :        5759 :       if (str[0] == '\0')
    4104                 :             :         {
    4105                 :         109 :           replace_call_with_value (gsi, NULL_TREE);
    4106                 :         109 :           return true;
    4107                 :             :         }
    4108                 :             : 
    4109                 :             :       /* If the string has length of 1, call putchar.  */
    4110                 :        5650 :       if (str[1] == '\0')
    4111                 :             :         {
    4112                 :             :           /* Given printf("c"), (where c is any one character,)
    4113                 :             :              convert "c"[0] to an int and pass that to the replacement
    4114                 :             :              function.  */
    4115                 :         559 :           newarg = build_int_cst (integer_type_node, str[0]);
    4116                 :         559 :           if (fn_putchar)
    4117                 :             :             {
    4118                 :         559 :               gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
    4119                 :         559 :               replace_call_with_call_and_fold (gsi, repl);
    4120                 :         559 :               return true;
    4121                 :             :             }
    4122                 :             :         }
    4123                 :             :       else
    4124                 :             :         {
    4125                 :             :           /* If the string was "string\n", call puts("string").  */
    4126                 :        5091 :           size_t len = strlen (str);
    4127                 :        5091 :           if ((unsigned char)str[len - 1] == target_newline
    4128                 :        3993 :               && (size_t) (int) len == len
    4129                 :        3993 :               && (int) len > 0)
    4130                 :             :             {
    4131                 :        3993 :               char *newstr;
    4132                 :             : 
    4133                 :             :               /* Create a NUL-terminated string that's one char shorter
    4134                 :             :                  than the original, stripping off the trailing '\n'.  */
    4135                 :        3993 :               newstr = xstrdup (str);
    4136                 :        3993 :               newstr[len - 1] = '\0';
    4137                 :        3993 :               newarg = build_string_literal (len, newstr);
    4138                 :        3993 :               free (newstr);
    4139                 :        3993 :               if (fn_puts)
    4140                 :             :                 {
    4141                 :        3993 :                   gcall *repl = gimple_build_call (fn_puts, 1, newarg);
    4142                 :        3993 :                   replace_call_with_call_and_fold (gsi, repl);
    4143                 :        3993 :                   return true;
    4144                 :             :                 }
    4145                 :             :             }
    4146                 :             :           else
    4147                 :             :             /* We'd like to arrange to call fputs(string,stdout) here,
    4148                 :             :                but we need stdout and don't have a way to get it yet.  */
    4149                 :             :             return false;
    4150                 :             :         }
    4151                 :             :     }
    4152                 :             : 
    4153                 :             :   /* The other optimizations can be done only on the non-va_list variants.  */
    4154                 :       93953 :   else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
    4155                 :             :     return false;
    4156                 :             : 
    4157                 :             :   /* If the format specifier was "%s\n", call __builtin_puts(arg).  */
    4158                 :       93675 :   else if (strcmp (fmt_str, target_percent_s_newline) == 0)
    4159                 :             :     {
    4160                 :         179 :       if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
    4161                 :             :         return false;
    4162                 :         179 :       if (fn_puts)
    4163                 :             :         {
    4164                 :         179 :           gcall *repl = gimple_build_call (fn_puts, 1, arg);
    4165                 :         179 :           replace_call_with_call_and_fold (gsi, repl);
    4166                 :         179 :           return true;
    4167                 :             :         }
    4168                 :             :     }
    4169                 :             : 
    4170                 :             :   /* If the format specifier was "%c", call __builtin_putchar(arg).  */
    4171                 :       93496 :   else if (strcmp (fmt_str, target_percent_c) == 0)
    4172                 :             :     {
    4173                 :          94 :       if (!arg || ! useless_type_conversion_p (integer_type_node,
    4174                 :          47 :                                                TREE_TYPE (arg)))
    4175                 :           0 :         return false;
    4176                 :          47 :       if (fn_putchar)
    4177                 :             :         {
    4178                 :          47 :           gcall *repl = gimple_build_call (fn_putchar, 1, arg);
    4179                 :          47 :           replace_call_with_call_and_fold (gsi, repl);
    4180                 :          47 :           return true;
    4181                 :             :         }
    4182                 :             :     }
    4183                 :             : 
    4184                 :             :   return false;
    4185                 :             : }
    4186                 :             : 
    4187                 :             : 
    4188                 :             : 
    4189                 :             : /* Fold a call to __builtin_strlen with known length LEN.  */
    4190                 :             : 
    4191                 :             : static bool
    4192                 :      151682 : gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
    4193                 :             : {
    4194                 :      151682 :   gimple *stmt = gsi_stmt (*gsi);
    4195                 :      151682 :   tree arg = gimple_call_arg (stmt, 0);
    4196                 :             : 
    4197                 :      151682 :   wide_int minlen;
    4198                 :      151682 :   wide_int maxlen;
    4199                 :             : 
    4200                 :      151682 :   c_strlen_data lendata = { };
    4201                 :      151682 :   if (get_range_strlen (arg, &lendata, /* eltsize = */ 1)
    4202                 :       39001 :       && !lendata.decl
    4203                 :       35748 :       && lendata.minlen && TREE_CODE (lendata.minlen) == INTEGER_CST
    4204                 :      187325 :       && lendata.maxlen && TREE_CODE (lendata.maxlen) == INTEGER_CST)
    4205                 :             :     {
    4206                 :             :       /* The range of lengths refers to either a single constant
    4207                 :             :          string or to the longest and shortest constant string
    4208                 :             :          referenced by the argument of the strlen() call, or to
    4209                 :             :          the strings that can possibly be stored in the arrays
    4210                 :             :          the argument refers to.  */
    4211                 :       35643 :       minlen = wi::to_wide (lendata.minlen);
    4212                 :       35643 :       maxlen = wi::to_wide (lendata.maxlen);
    4213                 :             :     }
    4214                 :             :   else
    4215                 :             :     {
    4216                 :      116039 :       unsigned prec = TYPE_PRECISION (sizetype);
    4217                 :             : 
    4218                 :      116039 :       minlen = wi::shwi (0, prec);
    4219                 :      116039 :       maxlen = wi::to_wide (max_object_size (), prec) - 2;
    4220                 :             :     }
    4221                 :             : 
    4222                 :             :   /* For -fsanitize=address, don't optimize the upper bound of the
    4223                 :             :      length to be able to diagnose UB on non-zero terminated arrays.  */
    4224                 :      151682 :   if (sanitize_flags_p (SANITIZE_ADDRESS))
    4225                 :         287 :     maxlen = wi::max_value (TYPE_PRECISION (sizetype), UNSIGNED);
    4226                 :             : 
    4227                 :      151682 :   if (minlen == maxlen)
    4228                 :             :     {
    4229                 :             :       /* Fold the strlen call to a constant.  */
    4230                 :        3466 :       tree type = TREE_TYPE (lendata.minlen);
    4231                 :        6932 :       tree len = force_gimple_operand_gsi (gsi,
    4232                 :        3466 :                                            wide_int_to_tree (type, minlen),
    4233                 :             :                                            true, NULL, true, GSI_SAME_STMT);
    4234                 :        3466 :       replace_call_with_value (gsi, len);
    4235                 :        3466 :       return true;
    4236                 :             :     }
    4237                 :             : 
    4238                 :             :   /* Set the strlen() range to [0, MAXLEN].  */
    4239                 :      148216 :   if (tree lhs = gimple_call_lhs (stmt))
    4240                 :      148211 :     set_strlen_range (lhs, minlen, maxlen);
    4241                 :             : 
    4242                 :             :   return false;
    4243                 :      151682 : }
    4244                 :             : 
    4245                 :             : static bool
    4246                 :         203 : gimple_fold_builtin_omp_is_initial_device (gimple_stmt_iterator *gsi)
    4247                 :             : {
    4248                 :             : #if ACCEL_COMPILER
    4249                 :             :   replace_call_with_value (gsi, integer_zero_node);
    4250                 :             :   return true;
    4251                 :             : #else
    4252                 :         203 :   if (!ENABLE_OFFLOADING || symtab->state == EXPANSION)
    4253                 :             :     {
    4254                 :           0 :       replace_call_with_value (gsi, integer_one_node);
    4255                 :         203 :       return true;
    4256                 :             :     }
    4257                 :             : #endif
    4258                 :             :   return false;
    4259                 :             : }
    4260                 :             : 
    4261                 :             : 
    4262                 :             : /* Fold a call to __builtin_acc_on_device.  */
    4263                 :             : 
    4264                 :             : static bool
    4265                 :        2866 : gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
    4266                 :             : {
    4267                 :             :   /* Defer folding until we know which compiler we're in.  */
    4268                 :        2866 :   if (symtab->state != EXPANSION)
    4269                 :             :     return false;
    4270                 :             : 
    4271                 :         554 :   unsigned val_host = GOMP_DEVICE_HOST;
    4272                 :         554 :   unsigned val_dev = GOMP_DEVICE_NONE;
    4273                 :             : 
    4274                 :             : #ifdef ACCEL_COMPILER
    4275                 :             :   val_host = GOMP_DEVICE_NOT_HOST;
    4276                 :             :   val_dev = ACCEL_COMPILER_acc_device;
    4277                 :             : #endif
    4278                 :             : 
    4279                 :         554 :   location_t loc = gimple_location (gsi_stmt (*gsi));
    4280                 :             : 
    4281                 :         554 :   tree host_eq = make_ssa_name (boolean_type_node);
    4282                 :         554 :   gimple *host_ass = gimple_build_assign
    4283                 :         554 :     (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host));
    4284                 :         554 :   gimple_set_location (host_ass, loc);
    4285                 :         554 :   gsi_insert_before (gsi, host_ass, GSI_SAME_STMT);
    4286                 :             : 
    4287                 :         554 :   tree dev_eq = make_ssa_name (boolean_type_node);
    4288                 :         554 :   gimple *dev_ass = gimple_build_assign
    4289                 :         554 :     (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev));
    4290                 :         554 :   gimple_set_location (dev_ass, loc);
    4291                 :         554 :   gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT);
    4292                 :             : 
    4293                 :         554 :   tree result = make_ssa_name (boolean_type_node);
    4294                 :         554 :   gimple *result_ass = gimple_build_assign
    4295                 :         554 :     (result, BIT_IOR_EXPR, host_eq, dev_eq);
    4296                 :         554 :   gimple_set_location (result_ass, loc);
    4297                 :         554 :   gsi_insert_before (gsi, result_ass, GSI_SAME_STMT);
    4298                 :             : 
    4299                 :         554 :   replace_call_with_value (gsi, result);
    4300                 :             : 
    4301                 :         554 :   return true;
    4302                 :             : }
    4303                 :             : 
    4304                 :             : /* Fold realloc (0, n) -> malloc (n).  */
    4305                 :             : 
    4306                 :             : static bool
    4307                 :       47915 : gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
    4308                 :             : {
    4309                 :       47915 :   gimple *stmt = gsi_stmt (*gsi);
    4310                 :       47915 :   tree arg = gimple_call_arg (stmt, 0);
    4311                 :       47915 :   tree size = gimple_call_arg (stmt, 1);
    4312                 :             : 
    4313                 :      142526 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    4314                 :             :     return false;
    4315                 :             : 
    4316                 :       47915 :   if (operand_equal_p (arg, null_pointer_node, 0))
    4317                 :             :     {
    4318                 :        1219 :       tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
    4319                 :        1219 :       if (fn_malloc)
    4320                 :             :         {
    4321                 :        1219 :           gcall *repl = gimple_build_call (fn_malloc, 1, size);
    4322                 :        1219 :           replace_call_with_call_and_fold (gsi, repl);
    4323                 :        1219 :           return true;
    4324                 :             :         }
    4325                 :             :     }
    4326                 :             :   return false;
    4327                 :             : }
    4328                 :             : 
    4329                 :             : /* Number of bytes into which any type but aggregate, vector or
    4330                 :             :    _BitInt types should fit.  */
    4331                 :             : static constexpr size_t clear_padding_unit
    4332                 :             :   = MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT;
    4333                 :             : /* Buffer size on which __builtin_clear_padding folding code works.  */
    4334                 :             : static const size_t clear_padding_buf_size = 32 * clear_padding_unit;
    4335                 :             : 
    4336                 :             : /* Data passed through __builtin_clear_padding folding.  */
    4337                 :             : struct clear_padding_struct {
    4338                 :             :   location_t loc;
    4339                 :             :   /* 0 during __builtin_clear_padding folding, nonzero during
    4340                 :             :      clear_type_padding_in_mask.  In that case, instead of clearing the
    4341                 :             :      non-padding bits in union_ptr array clear the padding bits in there.  */
    4342                 :             :   bool clear_in_mask;
    4343                 :             :   tree base;
    4344                 :             :   tree alias_type;
    4345                 :             :   gimple_stmt_iterator *gsi;
    4346                 :             :   /* Alignment of buf->base + 0.  */
    4347                 :             :   unsigned align;
    4348                 :             :   /* Offset from buf->base.  Should be always a multiple of UNITS_PER_WORD.  */
    4349                 :             :   HOST_WIDE_INT off;
    4350                 :             :   /* Number of padding bytes before buf->off that don't have padding clear
    4351                 :             :      code emitted yet.  */
    4352                 :             :   HOST_WIDE_INT padding_bytes;
    4353                 :             :   /* The size of the whole object.  Never emit code to touch
    4354                 :             :      buf->base + buf->sz or following bytes.  */
    4355                 :             :   HOST_WIDE_INT sz;
    4356                 :             :   /* Number of bytes recorded in buf->buf.  */
    4357                 :             :   size_t size;
    4358                 :             :   /* When inside union, instead of emitting code we and bits inside of
    4359                 :             :      the union_ptr array.  */
    4360                 :             :   unsigned char *union_ptr;
    4361                 :             :   /* Set bits mean padding bits that need to be cleared by the builtin.  */
    4362                 :             :   unsigned char buf[clear_padding_buf_size + clear_padding_unit];
    4363                 :             : };
    4364                 :             : 
    4365                 :             : /* Emit code to clear padding requested in BUF->buf - set bits
    4366                 :             :    in there stand for padding that should be cleared.  FULL is true
    4367                 :             :    if everything from the buffer should be flushed, otherwise
    4368                 :             :    it can leave up to 2 * clear_padding_unit bytes for further
    4369                 :             :    processing.  */
    4370                 :             : 
    4371                 :             : static void
    4372                 :       30929 : clear_padding_flush (clear_padding_struct *buf, bool full)
    4373                 :             : {
    4374                 :       30929 :   gcc_assert ((clear_padding_unit % UNITS_PER_WORD) == 0);
    4375                 :       30929 :   if (!full && buf->size < 2 * clear_padding_unit)
    4376                 :       30929 :     return;
    4377                 :       32003 :   gcc_assert ((buf->off % UNITS_PER_WORD) == 0);
    4378                 :       30887 :   size_t end = buf->size;
    4379                 :       30887 :   if (!full)
    4380                 :          42 :     end = ((end - clear_padding_unit - 1) / clear_padding_unit
    4381                 :             :            * clear_padding_unit);
    4382                 :       30887 :   size_t padding_bytes = buf->padding_bytes;
    4383                 :       30887 :   if (buf->union_ptr)
    4384                 :             :     {
    4385                 :       30135 :       if (buf->clear_in_mask)
    4386                 :             :         {
    4387                 :             :           /* During clear_type_padding_in_mask, clear the padding
    4388                 :             :              bits set in buf->buf in the buf->union_ptr mask.  */
    4389                 :      204459 :           for (size_t i = 0; i < end; i++)
    4390                 :             :             {
    4391                 :      174717 :               if (buf->buf[i] == (unsigned char) ~0)
    4392                 :        6420 :                 padding_bytes++;
    4393                 :             :               else
    4394                 :             :                 {
    4395                 :      168297 :                   memset (&buf->union_ptr[buf->off + i - padding_bytes],
    4396                 :             :                           0, padding_bytes);
    4397                 :      168297 :                   padding_bytes = 0;
    4398                 :      168297 :                   buf->union_ptr[buf->off + i] &= ~buf->buf[i];
    4399                 :             :                 }
    4400                 :             :             }
    4401                 :       29742 :           if (full)
    4402                 :             :             {
    4403                 :       29742 :               memset (&buf->union_ptr[buf->off + end - padding_bytes],
    4404                 :             :                       0, padding_bytes);
    4405                 :       29742 :               buf->off = 0;
    4406                 :       29742 :               buf->size = 0;
    4407                 :       29742 :               buf->padding_bytes = 0;
    4408                 :             :             }
    4409                 :             :           else
    4410                 :             :             {
    4411                 :           0 :               memmove (buf->buf, buf->buf + end, buf->size - end);
    4412                 :           0 :               buf->off += end;
    4413                 :           0 :               buf->size -= end;
    4414                 :           0 :               buf->padding_bytes = padding_bytes;
    4415                 :             :             }
    4416                 :       29742 :           return;
    4417                 :             :         }
    4418                 :             :       /* Inside of a union, instead of emitting any code, instead
    4419                 :             :          clear all bits in the union_ptr buffer that are clear
    4420                 :             :          in buf.  Whole padding bytes don't clear anything.  */
    4421                 :        3017 :       for (size_t i = 0; i < end; i++)
    4422                 :             :         {
    4423                 :        2624 :           if (buf->buf[i] == (unsigned char) ~0)
    4424                 :        1424 :             padding_bytes++;
    4425                 :             :           else
    4426                 :             :             {
    4427                 :        1200 :               padding_bytes = 0;
    4428                 :        1200 :               buf->union_ptr[buf->off + i] &= buf->buf[i];
    4429                 :             :             }
    4430                 :             :         }
    4431                 :         393 :       if (full)
    4432                 :             :         {
    4433                 :         393 :           buf->off = 0;
    4434                 :         393 :           buf->size = 0;
    4435                 :         393 :           buf->padding_bytes = 0;
    4436                 :             :         }
    4437                 :             :       else
    4438                 :             :         {
    4439                 :           0 :           memmove (buf->buf, buf->buf + end, buf->size - end);
    4440                 :           0 :           buf->off += end;
    4441                 :           0 :           buf->size -= end;
    4442                 :           0 :           buf->padding_bytes = padding_bytes;
    4443                 :             :         }
    4444                 :         393 :       return;
    4445                 :             :     }
    4446                 :         752 :   size_t wordsize = UNITS_PER_WORD;
    4447                 :       43621 :   for (size_t i = 0; i < end; i += wordsize)
    4448                 :             :     {
    4449                 :       42869 :       size_t nonzero_first = wordsize;
    4450                 :       42869 :       size_t nonzero_last = 0;
    4451                 :       42869 :       size_t zero_first = wordsize;
    4452                 :       42869 :       size_t zero_last = 0;
    4453                 :       42869 :       bool all_ones = true, bytes_only = true;
    4454                 :       43150 :       if ((unsigned HOST_WIDE_INT) (buf->off + i + wordsize)
    4455                 :       42869 :           > (unsigned HOST_WIDE_INT) buf->sz)
    4456                 :             :         {
    4457                 :         281 :           gcc_assert (wordsize > 1);
    4458                 :         281 :           wordsize /= 2;
    4459                 :         281 :           i -= wordsize;
    4460                 :         281 :           continue;
    4461                 :             :         }
    4462                 :       42588 :       size_t endsize = end - i > wordsize ? wordsize : end - i;
    4463                 :      382067 :       for (size_t j = i; j < i + endsize; j++)
    4464                 :             :         {
    4465                 :      339479 :           if (buf->buf[j])
    4466                 :             :             {
    4467                 :      329618 :               if (nonzero_first == wordsize)
    4468                 :             :                 {
    4469                 :       41660 :                   nonzero_first = j - i;
    4470                 :       41660 :                   nonzero_last = j - i;
    4471                 :             :                 }
    4472                 :      329618 :               if (nonzero_last != j - i)
    4473                 :         158 :                 all_ones = false;
    4474                 :      329618 :               nonzero_last = j + 1 - i;
    4475                 :             :             }
    4476                 :             :           else
    4477                 :             :             {
    4478                 :        9861 :               if (zero_first == wordsize)
    4479                 :        1915 :                 zero_first = j - i;
    4480                 :        9861 :               zero_last = j + 1 - i;
    4481                 :             :             }
    4482                 :      339479 :           if (buf->buf[j] != 0 && buf->buf[j] != (unsigned char) ~0)
    4483                 :             :             {
    4484                 :          79 :               all_ones = false;
    4485                 :          79 :               bytes_only = false;
    4486                 :             :             }
    4487                 :             :         }
    4488                 :       42588 :       size_t padding_end = i;
    4489                 :       42588 :       if (padding_bytes)
    4490                 :             :         {
    4491                 :       41008 :           if (nonzero_first == 0
    4492                 :       41008 :               && nonzero_last == endsize
    4493                 :       40560 :               && all_ones)
    4494                 :             :             {
    4495                 :             :               /* All bits are padding and we had some padding
    4496                 :             :                  before too.  Just extend it.  */
    4497                 :       40560 :               padding_bytes += endsize;
    4498                 :       40560 :               continue;
    4499                 :             :             }
    4500                 :         448 :           if (all_ones && nonzero_first == 0)
    4501                 :             :             {
    4502                 :           4 :               padding_bytes += nonzero_last;
    4503                 :           4 :               padding_end += nonzero_last;
    4504                 :           4 :               nonzero_first = wordsize;
    4505                 :           4 :               nonzero_last = 0;
    4506                 :             :             }
    4507                 :         444 :           else if (bytes_only && nonzero_first == 0)
    4508                 :             :             {
    4509                 :           0 :               gcc_assert (zero_first && zero_first != wordsize);
    4510                 :           0 :               padding_bytes += zero_first;
    4511                 :           0 :               padding_end += zero_first;
    4512                 :             :             }
    4513                 :         448 :           tree atype, src;
    4514                 :         448 :           if (padding_bytes == 1)
    4515                 :             :             {
    4516                 :          33 :               atype = char_type_node;
    4517                 :          33 :               src = build_zero_cst (char_type_node);
    4518                 :             :             }
    4519                 :             :           else
    4520                 :             :             {
    4521                 :         415 :               atype = build_array_type_nelts (char_type_node, padding_bytes);
    4522                 :         415 :               src = build_constructor (atype, NULL);
    4523                 :             :             }
    4524                 :         448 :           tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
    4525                 :         448 :                                  build_int_cst (buf->alias_type,
    4526                 :         448 :                                                 buf->off + padding_end
    4527                 :         448 :                                                 - padding_bytes));
    4528                 :         448 :           gimple *g = gimple_build_assign (dst, src);
    4529                 :         448 :           gimple_set_location (g, buf->loc);
    4530                 :         448 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4531                 :         448 :           padding_bytes = 0;
    4532                 :         448 :           buf->padding_bytes = 0;
    4533                 :             :         }
    4534                 :        2028 :       if (nonzero_first == wordsize)
    4535                 :             :         /* All bits in a word are 0, there are no padding bits.  */
    4536                 :         932 :         continue;
    4537                 :        1096 :       if (all_ones && nonzero_last == endsize)
    4538                 :             :         {
    4539                 :             :           /* All bits between nonzero_first and end of word are padding
    4540                 :             :              bits, start counting padding_bytes.  */
    4541                 :         833 :           padding_bytes = nonzero_last - nonzero_first;
    4542                 :         833 :           continue;
    4543                 :             :         }
    4544                 :         263 :       if (bytes_only)
    4545                 :             :         {
    4546                 :             :           /* If bitfields aren't involved in this word, prefer storing
    4547                 :             :              individual bytes or groups of them over performing a RMW
    4548                 :             :              operation on the whole word.  */
    4549                 :         226 :           gcc_assert (i + zero_last <= end);
    4550                 :        1113 :           for (size_t j = padding_end; j < i + zero_last; j++)
    4551                 :             :             {
    4552                 :         887 :               if (buf->buf[j])
    4553                 :             :                 {
    4554                 :             :                   size_t k;
    4555                 :         604 :                   for (k = j; k < i + zero_last; k++)
    4556                 :         604 :                     if (buf->buf[k] == 0)
    4557                 :             :                       break;
    4558                 :         258 :                   HOST_WIDE_INT off = buf->off + j;
    4559                 :         258 :                   tree atype, src;
    4560                 :         258 :                   if (k - j == 1)
    4561                 :             :                     {
    4562                 :         214 :                       atype = char_type_node;
    4563                 :         214 :                       src = build_zero_cst (char_type_node);
    4564                 :             :                     }
    4565                 :             :                   else
    4566                 :             :                     {
    4567                 :          44 :                       atype = build_array_type_nelts (char_type_node, k - j);
    4568                 :          44 :                       src = build_constructor (atype, NULL);
    4569                 :             :                     }
    4570                 :         258 :                   tree dst = build2_loc (buf->loc, MEM_REF, atype,
    4571                 :             :                                          buf->base,
    4572                 :         258 :                                          build_int_cst (buf->alias_type, off));
    4573                 :         258 :                   gimple *g = gimple_build_assign (dst, src);
    4574                 :         258 :                   gimple_set_location (g, buf->loc);
    4575                 :         258 :                   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4576                 :         258 :                   j = k;
    4577                 :             :                 }
    4578                 :             :             }
    4579                 :         226 :           if (nonzero_last == endsize)
    4580                 :          98 :             padding_bytes = nonzero_last - zero_last;
    4581                 :         226 :           continue;
    4582                 :         226 :         }
    4583                 :         120 :       for (size_t eltsz = 1; eltsz <= wordsize; eltsz <<= 1)
    4584                 :             :         {
    4585                 :         120 :           if (nonzero_last - nonzero_first <= eltsz
    4586                 :          37 :               && ((nonzero_first & ~(eltsz - 1))
    4587                 :          37 :                   == ((nonzero_last - 1) & ~(eltsz - 1))))
    4588                 :             :             {
    4589                 :          37 :               tree type;
    4590                 :          37 :               if (eltsz == 1)
    4591                 :           2 :                 type = char_type_node;
    4592                 :             :               else
    4593                 :          35 :                 type = lang_hooks.types.type_for_size (eltsz * BITS_PER_UNIT,
    4594                 :             :                                                        0);
    4595                 :          37 :               size_t start = nonzero_first & ~(eltsz - 1);
    4596                 :          37 :               HOST_WIDE_INT off = buf->off + i + start;
    4597                 :          37 :               tree atype = type;
    4598                 :          37 :               if (eltsz > 1 && buf->align < TYPE_ALIGN (type))
    4599                 :           0 :                 atype = build_aligned_type (type, buf->align);
    4600                 :          37 :               tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
    4601                 :          37 :                                      build_int_cst (buf->alias_type, off));
    4602                 :          37 :               tree src;
    4603                 :          37 :               gimple *g;
    4604                 :          37 :               if (all_ones
    4605                 :          37 :                   && nonzero_first == start
    4606                 :           0 :                   && nonzero_last == start + eltsz)
    4607                 :           0 :                 src = build_zero_cst (type);
    4608                 :             :               else
    4609                 :             :                 {
    4610                 :          37 :                   src = make_ssa_name (type);
    4611                 :          37 :                   tree tmp_dst = unshare_expr (dst);
    4612                 :             :                   /* The folding introduces a read from the tmp_dst, we should
    4613                 :             :                      prevent uninitialized warning analysis from issuing warning
    4614                 :             :                      for such fake read.  In order to suppress warning only for
    4615                 :             :                      this expr, we should set the location of tmp_dst to
    4616                 :             :                      UNKNOWN_LOCATION first, then suppress_warning will call
    4617                 :             :                      set_no_warning_bit to set the no_warning flag only for
    4618                 :             :                      tmp_dst.  */
    4619                 :          37 :                   SET_EXPR_LOCATION (tmp_dst, UNKNOWN_LOCATION);
    4620                 :          37 :                   suppress_warning (tmp_dst, OPT_Wuninitialized);
    4621                 :          37 :                   g = gimple_build_assign (src, tmp_dst);
    4622                 :          37 :                   gimple_set_location (g, buf->loc);
    4623                 :          37 :                   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4624                 :          74 :                   tree mask = native_interpret_expr (type,
    4625                 :          37 :                                                      buf->buf + i + start,
    4626                 :             :                                                      eltsz);
    4627                 :          37 :                   gcc_assert (mask && TREE_CODE (mask) == INTEGER_CST);
    4628                 :          37 :                   mask = fold_build1 (BIT_NOT_EXPR, type, mask);
    4629                 :          37 :                   tree src_masked = make_ssa_name (type);
    4630                 :          37 :                   g = gimple_build_assign (src_masked, BIT_AND_EXPR,
    4631                 :             :                                            src, mask);
    4632                 :          37 :                   gimple_set_location (g, buf->loc);
    4633                 :          37 :                   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4634                 :          37 :                   src = src_masked;
    4635                 :             :                 }
    4636                 :          37 :               g = gimple_build_assign (dst, src);
    4637                 :          37 :               gimple_set_location (g, buf->loc);
    4638                 :          37 :               gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4639                 :          37 :               break;
    4640                 :             :             }
    4641                 :             :         }
    4642                 :             :     }
    4643                 :         752 :   if (full)
    4644                 :             :     {
    4645                 :         710 :       if (padding_bytes)
    4646                 :             :         {
    4647                 :         483 :           tree atype, src;
    4648                 :         483 :           if (padding_bytes == 1)
    4649                 :             :             {
    4650                 :         108 :               atype = char_type_node;
    4651                 :         108 :               src = build_zero_cst (char_type_node);
    4652                 :             :             }
    4653                 :             :           else
    4654                 :             :             {
    4655                 :         375 :               atype = build_array_type_nelts (char_type_node, padding_bytes);
    4656                 :         375 :               src = build_constructor (atype, NULL);
    4657                 :             :             }
    4658                 :         483 :           tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
    4659                 :         483 :                                  build_int_cst (buf->alias_type,
    4660                 :         483 :                                                 buf->off + end
    4661                 :         483 :                                                 - padding_bytes));
    4662                 :         483 :           gimple *g = gimple_build_assign (dst, src);
    4663                 :         483 :           gimple_set_location (g, buf->loc);
    4664                 :         483 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4665                 :             :         }
    4666                 :         710 :       size_t end_rem = end % UNITS_PER_WORD;
    4667                 :         710 :       buf->off += end - end_rem;
    4668                 :         710 :       buf->size = end_rem;
    4669                 :         710 :       memset (buf->buf, 0, buf->size);
    4670                 :         710 :       buf->padding_bytes = 0;
    4671                 :             :     }
    4672                 :             :   else
    4673                 :             :     {
    4674                 :          42 :       memmove (buf->buf, buf->buf + end, buf->size - end);
    4675                 :          42 :       buf->off += end;
    4676                 :          42 :       buf->size -= end;
    4677                 :          42 :       buf->padding_bytes = padding_bytes;
    4678                 :             :     }
    4679                 :             : }
    4680                 :             : 
    4681                 :             : /* Append PADDING_BYTES padding bytes.  */
    4682                 :             : 
    4683                 :             : static void
    4684                 :        4977 : clear_padding_add_padding (clear_padding_struct *buf,
    4685                 :             :                            HOST_WIDE_INT padding_bytes)
    4686                 :             : {
    4687                 :        4977 :   if (padding_bytes == 0)
    4688                 :             :     return;
    4689                 :        1667 :   if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
    4690                 :             :       > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
    4691                 :          42 :     clear_padding_flush (buf, false);
    4692                 :        1667 :   if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
    4693                 :             :       > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
    4694                 :             :     {
    4695                 :          42 :       memset (buf->buf + buf->size, ~0, clear_padding_buf_size - buf->size);
    4696                 :          42 :       padding_bytes -= clear_padding_buf_size - buf->size;
    4697                 :          42 :       buf->size = clear_padding_buf_size;
    4698                 :          42 :       clear_padding_flush (buf, false);
    4699                 :          42 :       gcc_assert (buf->padding_bytes);
    4700                 :             :       /* At this point buf->buf[0] through buf->buf[buf->size - 1]
    4701                 :             :          is guaranteed to be all ones.  */
    4702                 :          42 :       padding_bytes += buf->size;
    4703                 :          42 :       buf->size = padding_bytes % UNITS_PER_WORD;
    4704                 :          42 :       memset (buf->buf, ~0, buf->size);
    4705                 :          42 :       buf->off += padding_bytes - buf->size;
    4706                 :          42 :       buf->padding_bytes += padding_bytes - buf->size;
    4707                 :             :     }
    4708                 :             :   else
    4709                 :             :     {
    4710                 :        1625 :       memset (buf->buf + buf->size, ~0, padding_bytes);
    4711                 :        1625 :       buf->size += padding_bytes;
    4712                 :             :     }
    4713                 :             : }
    4714                 :             : 
    4715                 :             : static void clear_padding_type (clear_padding_struct *, tree,
    4716                 :             :                                 HOST_WIDE_INT, bool);
    4717                 :             : 
    4718                 :             : /* Clear padding bits of union type TYPE.  */
    4719                 :             : 
    4720                 :             : static void
    4721                 :         128 : clear_padding_union (clear_padding_struct *buf, tree type,
    4722                 :             :                      HOST_WIDE_INT sz, bool for_auto_init)
    4723                 :             : {
    4724                 :         128 :   clear_padding_struct *union_buf;
    4725                 :         128 :   HOST_WIDE_INT start_off = 0, next_off = 0;
    4726                 :         128 :   size_t start_size = 0;
    4727                 :         128 :   if (buf->union_ptr)
    4728                 :             :     {
    4729                 :          42 :       start_off = buf->off + buf->size;
    4730                 :          42 :       next_off = start_off + sz;
    4731                 :          42 :       start_size = start_off % UNITS_PER_WORD;
    4732                 :          42 :       start_off -= start_size;
    4733                 :          42 :       clear_padding_flush (buf, true);
    4734                 :          42 :       union_buf = buf;
    4735                 :             :     }
    4736                 :             :   else
    4737                 :             :     {
    4738                 :          86 :       if (sz + buf->size > clear_padding_buf_size)
    4739                 :           0 :         clear_padding_flush (buf, false);
    4740                 :          86 :       union_buf = XALLOCA (clear_padding_struct);
    4741                 :          86 :       union_buf->loc = buf->loc;
    4742                 :          86 :       union_buf->clear_in_mask = buf->clear_in_mask;
    4743                 :          86 :       union_buf->base = NULL_TREE;
    4744                 :          86 :       union_buf->alias_type = NULL_TREE;
    4745                 :          86 :       union_buf->gsi = NULL;
    4746                 :          86 :       union_buf->align = 0;
    4747                 :          86 :       union_buf->off = 0;
    4748                 :          86 :       union_buf->padding_bytes = 0;
    4749                 :          86 :       union_buf->sz = sz;
    4750                 :          86 :       union_buf->size = 0;
    4751                 :          86 :       if (sz + buf->size <= clear_padding_buf_size)
    4752                 :          86 :         union_buf->union_ptr = buf->buf + buf->size;
    4753                 :             :       else
    4754                 :           0 :         union_buf->union_ptr = XNEWVEC (unsigned char, sz);
    4755                 :          86 :       memset (union_buf->union_ptr, ~0, sz);
    4756                 :             :     }
    4757                 :             : 
    4758                 :        1193 :   for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    4759                 :        1065 :     if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
    4760                 :             :       {
    4761                 :         359 :         if (DECL_SIZE_UNIT (field) == NULL_TREE)
    4762                 :             :           {
    4763                 :           8 :             if (TREE_TYPE (field) == error_mark_node)
    4764                 :           0 :               continue;
    4765                 :           8 :             gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
    4766                 :             :                         && !COMPLETE_TYPE_P (TREE_TYPE (field)));
    4767                 :           8 :             if (!buf->clear_in_mask && !for_auto_init)
    4768                 :           8 :               error_at (buf->loc, "flexible array member %qD does not have "
    4769                 :             :                                   "well defined padding bits for %qs",
    4770                 :             :                         field, "__builtin_clear_padding");
    4771                 :           8 :             continue;
    4772                 :             :           }
    4773                 :         351 :         HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
    4774                 :         351 :         gcc_assert (union_buf->size == 0);
    4775                 :         351 :         union_buf->off = start_off;
    4776                 :         351 :         union_buf->size = start_size;
    4777                 :         351 :         memset (union_buf->buf, ~0, start_size);
    4778                 :         351 :         clear_padding_type (union_buf, TREE_TYPE (field), fldsz, for_auto_init);
    4779                 :         351 :         clear_padding_add_padding (union_buf, sz - fldsz);
    4780                 :         351 :         clear_padding_flush (union_buf, true);
    4781                 :             :       }
    4782                 :             : 
    4783                 :         128 :   if (buf == union_buf)
    4784                 :             :     {
    4785                 :          42 :       buf->off = next_off;
    4786                 :          42 :       buf->size = next_off % UNITS_PER_WORD;
    4787                 :          42 :       buf->off -= buf->size;
    4788                 :          42 :       memset (buf->buf, ~0, buf->size);
    4789                 :             :     }
    4790                 :          86 :   else if (sz + buf->size <= clear_padding_buf_size)
    4791                 :          86 :     buf->size += sz;
    4792                 :             :   else
    4793                 :             :     {
    4794                 :           0 :       unsigned char *union_ptr = union_buf->union_ptr;
    4795                 :           0 :       while (sz)
    4796                 :             :         {
    4797                 :           0 :           clear_padding_flush (buf, false);
    4798                 :           0 :           HOST_WIDE_INT this_sz
    4799                 :           0 :             = MIN ((unsigned HOST_WIDE_INT) sz,
    4800                 :             :                    clear_padding_buf_size - buf->size);
    4801                 :           0 :           memcpy (buf->buf + buf->size, union_ptr, this_sz);
    4802                 :           0 :           buf->size += this_sz;
    4803                 :           0 :           union_ptr += this_sz;
    4804                 :           0 :           sz -= this_sz;
    4805                 :             :         }
    4806                 :           0 :       XDELETE (union_buf->union_ptr);
    4807                 :             :     }
    4808                 :         128 : }
    4809                 :             : 
    4810                 :             : /* The only known floating point formats with padding bits are the
    4811                 :             :    IEEE extended ones.  */
    4812                 :             : 
    4813                 :             : static bool
    4814                 :       31382 : clear_padding_real_needs_padding_p (tree type)
    4815                 :             : {
    4816                 :       31382 :   const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
    4817                 :       31382 :   return (fmt->b == 2
    4818                 :       31063 :           && fmt->signbit_ro == fmt->signbit_rw
    4819                 :       62445 :           && (fmt->signbit_ro == 79 || fmt->signbit_ro == 95));
    4820                 :             : }
    4821                 :             : 
    4822                 :             : /* _BitInt has padding bits if it isn't extended in the ABI and has smaller
    4823                 :             :    precision than bits in limb or corresponding number of limbs.  */
    4824                 :             : 
    4825                 :             : static bool
    4826                 :           6 : clear_padding_bitint_needs_padding_p (tree type)
    4827                 :             : {
    4828                 :           6 :   struct bitint_info info;
    4829                 :           6 :   bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
    4830                 :           6 :   gcc_assert (ok);
    4831                 :           6 :   if (info.extended)
    4832                 :             :     return false;
    4833                 :           6 :   scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.abi_limb_mode);
    4834                 :           6 :   if (TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
    4835                 :             :     return true;
    4836                 :           4 :   else if (TYPE_PRECISION (type) == GET_MODE_PRECISION (limb_mode))
    4837                 :             :     return false;
    4838                 :             :   else
    4839                 :           4 :     return (((unsigned) TYPE_PRECISION (type))
    4840                 :           4 :             % GET_MODE_PRECISION (limb_mode)) != 0;
    4841                 :             : }
    4842                 :             : 
    4843                 :             : /* Return true if TYPE might contain any padding bits.  */
    4844                 :             : 
    4845                 :             : bool
    4846                 :      901232 : clear_padding_type_may_have_padding_p (tree type)
    4847                 :             : {
    4848                 :     1033308 :   switch (TREE_CODE (type))
    4849                 :             :     {
    4850                 :             :     case RECORD_TYPE:
    4851                 :             :     case UNION_TYPE:
    4852                 :             :       return true;
    4853                 :      132076 :     case ARRAY_TYPE:
    4854                 :      132076 :     case COMPLEX_TYPE:
    4855                 :      132076 :     case VECTOR_TYPE:
    4856                 :      132076 :       return clear_padding_type_may_have_padding_p (TREE_TYPE (type));
    4857                 :        1657 :     case REAL_TYPE:
    4858                 :        1657 :       return clear_padding_real_needs_padding_p (type);
    4859                 :           6 :     case BITINT_TYPE:
    4860                 :           6 :       return clear_padding_bitint_needs_padding_p (type);
    4861                 :       36752 :     default:
    4862                 :       36752 :       return false;
    4863                 :             :     }
    4864                 :             : }
    4865                 :             : 
    4866                 :             : /* Return true if TYPE has padding bits aside from those in fields,
    4867                 :             :    elements, etc.  */
    4868                 :             : 
    4869                 :             : bool
    4870                 :     1171882 : type_has_padding_at_level_p (tree type)
    4871                 :             : {
    4872                 :     1171882 :   switch (TREE_CODE (type))
    4873                 :             :     {
    4874                 :     1037985 :     case RECORD_TYPE:
    4875                 :     1037985 :       {
    4876                 :     1037985 :         tree bitpos = size_zero_node;
    4877                 :             :         /* Expect fields to be sorted by bit position.  */
    4878                 :     8217152 :         for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
    4879                 :     7183837 :           if (TREE_CODE (f) == FIELD_DECL)
    4880                 :             :             {
    4881                 :     2370843 :               if (DECL_PADDING_P (f))
    4882                 :             :                 return true;
    4883                 :     2370840 :               tree pos = bit_position (f);
    4884                 :     2370840 :               if (simple_cst_equal (bitpos, pos) != 1)
    4885                 :             :                 return true;
    4886                 :     2366193 :               if (!DECL_SIZE (f))
    4887                 :             :                 return true;
    4888                 :     2366173 :               bitpos = int_const_binop (PLUS_EXPR, pos, DECL_SIZE (f));
    4889                 :             :             }
    4890                 :     1033315 :         if (simple_cst_equal (bitpos, TYPE_SIZE (type)) != 1)
    4891                 :             :           return true;
    4892                 :             :         return false;
    4893                 :             :       }
    4894                 :           3 :     case UNION_TYPE:
    4895                 :           3 :     case QUAL_UNION_TYPE:
    4896                 :           3 :       bool any_fields;
    4897                 :           3 :       any_fields = false;
    4898                 :             :       /* If any of the fields is smaller than the whole, there is padding.  */
    4899                 :           6 :       for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
    4900                 :           3 :         if (TREE_CODE (f) != FIELD_DECL || TREE_TYPE (f) == error_mark_node)
    4901                 :           3 :           continue;
    4902                 :           0 :         else if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
    4903                 :           0 :                                    TYPE_SIZE (type)) != 1)
    4904                 :             :           return true;
    4905                 :             :         else
    4906                 :             :           any_fields = true;
    4907                 :             :       /* If the union doesn't have any fields and still has non-zero size,
    4908                 :             :          all of it is padding.  */
    4909                 :           3 :       if (!any_fields && !integer_zerop (TYPE_SIZE (type)))
    4910                 :             :         return true;
    4911                 :             :       return false;
    4912                 :             :     case ARRAY_TYPE:
    4913                 :             :     case COMPLEX_TYPE:
    4914                 :             :     case VECTOR_TYPE:
    4915                 :             :       /* No recursing here, no padding at this level.  */
    4916                 :             :       return false;
    4917                 :           0 :     case REAL_TYPE:
    4918                 :           0 :       return clear_padding_real_needs_padding_p (type);
    4919                 :           0 :     case BITINT_TYPE:
    4920                 :           0 :       return clear_padding_bitint_needs_padding_p (type);
    4921                 :             :     default:
    4922                 :             :       return false;
    4923                 :             :     }
    4924                 :             : }
    4925                 :             : 
    4926                 :             : /* Emit a runtime loop:
    4927                 :             :    for (; buf.base != end; buf.base += sz)
    4928                 :             :      __builtin_clear_padding (buf.base);  */
    4929                 :             : 
    4930                 :             : static void
    4931                 :         114 : clear_padding_emit_loop (clear_padding_struct *buf, tree type,
    4932                 :             :                          tree end, bool for_auto_init)
    4933                 :             : {
    4934                 :         114 :   tree l1 = create_artificial_label (buf->loc);
    4935                 :         114 :   tree l2 = create_artificial_label (buf->loc);
    4936                 :         114 :   tree l3 = create_artificial_label (buf->loc);
    4937                 :         114 :   gimple *g = gimple_build_goto (l2);
    4938                 :         114 :   gimple_set_location (g, buf->loc);
    4939                 :         114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4940                 :         114 :   g = gimple_build_label (l1);
    4941                 :         114 :   gimple_set_location (g, buf->loc);
    4942                 :         114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4943                 :         114 :   clear_padding_type (buf, type, buf->sz, for_auto_init);
    4944                 :         114 :   clear_padding_flush (buf, true);
    4945                 :         114 :   g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR, buf->base,
    4946                 :         114 :                            size_int (buf->sz));
    4947                 :         114 :   gimple_set_location (g, buf->loc);
    4948                 :         114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4949                 :         114 :   g = gimple_build_label (l2);
    4950                 :         114 :   gimple_set_location (g, buf->loc);
    4951                 :         114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4952                 :         114 :   g = gimple_build_cond (NE_EXPR, buf->base, end, l1, l3);
    4953                 :         114 :   gimple_set_location (g, buf->loc);
    4954                 :         114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4955                 :         114 :   g = gimple_build_label (l3);
    4956                 :         114 :   gimple_set_location (g, buf->loc);
    4957                 :         114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4958                 :         114 : }
    4959                 :             : 
    4960                 :             : /* Clear padding bits for TYPE.  Called recursively from
    4961                 :             :    gimple_fold_builtin_clear_padding.  If FOR_AUTO_INIT is true,
    4962                 :             :    the __builtin_clear_padding is not called by the end user,
    4963                 :             :    instead, it's inserted by the compiler to initialize the
    4964                 :             :    paddings of automatic variable.  Therefore, we should not
    4965                 :             :    emit the error messages for flexible array members to confuse
    4966                 :             :    the end user.  */
    4967                 :             : 
    4968                 :             : static void
    4969                 :       34848 : clear_padding_type (clear_padding_struct *buf, tree type,
    4970                 :             :                     HOST_WIDE_INT sz, bool for_auto_init)
    4971                 :             : {
    4972                 :       34848 :   switch (TREE_CODE (type))
    4973                 :             :     {
    4974                 :        1247 :     case RECORD_TYPE:
    4975                 :        1247 :       HOST_WIDE_INT cur_pos;
    4976                 :        1247 :       cur_pos = 0;
    4977                 :       16011 :       for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    4978                 :       14764 :         if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
    4979                 :             :           {
    4980                 :        3684 :             tree ftype = TREE_TYPE (field);
    4981                 :        3684 :             if (DECL_BIT_FIELD (field))
    4982                 :             :               {
    4983                 :         256 :                 HOST_WIDE_INT fldsz = TYPE_PRECISION (ftype);
    4984                 :         256 :                 if (fldsz == 0)
    4985                 :           0 :                   continue;
    4986                 :         256 :                 HOST_WIDE_INT pos = int_byte_position (field);
    4987                 :         256 :                 if (pos >= sz)
    4988                 :           0 :                   continue;
    4989                 :         256 :                 HOST_WIDE_INT bpos
    4990                 :         256 :                   = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
    4991                 :         256 :                 bpos %= BITS_PER_UNIT;
    4992                 :         256 :                 HOST_WIDE_INT end
    4993                 :         256 :                   = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
    4994                 :         256 :                 if (pos + end > cur_pos)
    4995                 :             :                   {
    4996                 :         195 :                     clear_padding_add_padding (buf, pos + end - cur_pos);
    4997                 :         195 :                     cur_pos = pos + end;
    4998                 :             :                   }
    4999                 :         256 :                 gcc_assert (cur_pos > pos
    5000                 :             :                             && ((unsigned HOST_WIDE_INT) buf->size
    5001                 :             :                                 >= (unsigned HOST_WIDE_INT) cur_pos - pos));
    5002                 :         256 :                 unsigned char *p = buf->buf + buf->size - (cur_pos - pos);
    5003                 :         256 :                 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
    5004                 :             :                   sorry_at (buf->loc, "PDP11 bit-field handling unsupported"
    5005                 :             :                                       " in %qs", "__builtin_clear_padding");
    5006                 :         256 :                 else if (BYTES_BIG_ENDIAN)
    5007                 :             :                   {
    5008                 :             :                     /* Big endian.  */
    5009                 :             :                     if (bpos + fldsz <= BITS_PER_UNIT)
    5010                 :             :                       *p &= ~(((1 << fldsz) - 1)
    5011                 :             :                               << (BITS_PER_UNIT - bpos - fldsz));
    5012                 :             :                     else
    5013                 :             :                       {
    5014                 :             :                         if (bpos)
    5015                 :             :                           {
    5016                 :             :                             *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
    5017                 :             :                             p++;
    5018                 :             :                             fldsz -= BITS_PER_UNIT - bpos;
    5019                 :             :                           }
    5020                 :             :                         memset (p, 0, fldsz / BITS_PER_UNIT);
    5021                 :             :                         p += fldsz / BITS_PER_UNIT;
    5022                 :             :                         fldsz %= BITS_PER_UNIT;
    5023                 :             :                         if (fldsz)
    5024                 :             :                           *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
    5025                 :             :                       }
    5026                 :             :                   }
    5027                 :             :                 else
    5028                 :             :                   {
    5029                 :             :                     /* Little endian.  */
    5030                 :         256 :                     if (bpos + fldsz <= BITS_PER_UNIT)
    5031                 :         159 :                       *p &= ~(((1 << fldsz) - 1) << bpos);
    5032                 :             :                     else
    5033                 :             :                       {
    5034                 :          97 :                         if (bpos)
    5035                 :             :                           {
    5036                 :          29 :                             *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
    5037                 :          29 :                             p++;
    5038                 :          29 :                             fldsz -= BITS_PER_UNIT - bpos;
    5039                 :             :                           }
    5040                 :          97 :                         memset (p, 0, fldsz / BITS_PER_UNIT);
    5041                 :          97 :                         p += fldsz / BITS_PER_UNIT;
    5042                 :          97 :                         fldsz %= BITS_PER_UNIT;
    5043                 :          97 :                         if (fldsz)
    5044                 :          52 :                           *p &= ~((1 << fldsz) - 1);
    5045                 :             :                       }
    5046                 :             :                   }
    5047                 :             :               }
    5048                 :        3428 :             else if (DECL_SIZE_UNIT (field) == NULL_TREE)
    5049                 :             :               {
    5050                 :          32 :                 if (ftype == error_mark_node)
    5051                 :           0 :                   continue;
    5052                 :          32 :                 gcc_assert (TREE_CODE (ftype) == ARRAY_TYPE
    5053                 :             :                             && !COMPLETE_TYPE_P (ftype));
    5054                 :          32 :                 if (!buf->clear_in_mask && !for_auto_init)
    5055                 :          24 :                   error_at (buf->loc, "flexible array member %qD does not "
    5056                 :             :                                       "have well defined padding bits for %qs",
    5057                 :             :                             field, "__builtin_clear_padding");
    5058                 :             :               }
    5059                 :        3396 :             else if (is_empty_type (ftype))
    5060                 :         212 :               continue;
    5061                 :             :             else
    5062                 :             :               {
    5063                 :        3184 :                 HOST_WIDE_INT pos = int_byte_position (field);
    5064                 :        3184 :                 if (pos >= sz)
    5065                 :           0 :                   continue;
    5066                 :        3184 :                 HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
    5067                 :        3184 :                 gcc_assert (pos >= 0 && fldsz >= 0 && pos >= cur_pos);
    5068                 :        3184 :                 clear_padding_add_padding (buf, pos - cur_pos);
    5069                 :        3184 :                 cur_pos = pos;
    5070                 :        3184 :                 if (tree asbase = lang_hooks.types.classtype_as_base (field))
    5071                 :         188 :                   ftype = asbase;
    5072                 :        3184 :                 clear_padding_type (buf, ftype, fldsz, for_auto_init);
    5073                 :        3184 :                 cur_pos += fldsz;
    5074                 :             :               }
    5075                 :             :           }
    5076                 :        1247 :       gcc_assert (sz >= cur_pos);
    5077                 :        1247 :       clear_padding_add_padding (buf, sz - cur_pos);
    5078                 :        1247 :       break;
    5079                 :         325 :     case ARRAY_TYPE:
    5080                 :         325 :       HOST_WIDE_INT nelts, fldsz;
    5081                 :         325 :       fldsz = int_size_in_bytes (TREE_TYPE (type));
    5082                 :         325 :       if (fldsz == 0)
    5083                 :             :         break;
    5084                 :         311 :       nelts = sz / fldsz;
    5085                 :         311 :       if (nelts > 1
    5086                 :         304 :           && sz > 8 * UNITS_PER_WORD
    5087                 :          78 :           && buf->union_ptr == NULL
    5088                 :         389 :           && clear_padding_type_may_have_padding_p (TREE_TYPE (type)))
    5089                 :             :         {
    5090                 :             :           /* For sufficiently large array of more than one elements,
    5091                 :             :              emit a runtime loop to keep code size manageable.  */
    5092                 :          66 :           tree base = buf->base;
    5093                 :          66 :           unsigned int prev_align = buf->align;
    5094                 :          66 :           HOST_WIDE_INT off = buf->off + buf->size;
    5095                 :          66 :           HOST_WIDE_INT prev_sz = buf->sz;
    5096                 :          66 :           clear_padding_flush (buf, true);
    5097                 :          66 :           tree elttype = TREE_TYPE (type);
    5098                 :          66 :           buf->base = create_tmp_var (build_pointer_type (elttype));
    5099                 :          66 :           tree end = make_ssa_name (TREE_TYPE (buf->base));
    5100                 :          66 :           gimple *g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR,
    5101                 :          66 :                                            base, size_int (off));
    5102                 :          66 :           gimple_set_location (g, buf->loc);
    5103                 :          66 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5104                 :          66 :           g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf->base,
    5105                 :          66 :                                    size_int (sz));
    5106                 :          66 :           gimple_set_location (g, buf->loc);
    5107                 :          66 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5108                 :          66 :           buf->sz = fldsz;
    5109                 :          66 :           buf->align = TYPE_ALIGN (elttype);
    5110                 :          66 :           buf->off = 0;
    5111                 :          66 :           buf->size = 0;
    5112                 :          66 :           clear_padding_emit_loop (buf, elttype, end, for_auto_init);
    5113                 :          66 :           off += sz;
    5114                 :          66 :           buf->base = base;
    5115                 :          66 :           buf->sz = prev_sz;
    5116                 :          66 :           buf->align = prev_align;
    5117                 :          66 :           buf->size = off % UNITS_PER_WORD;
    5118                 :          66 :           buf->off = off - buf->size;
    5119                 :          66 :           memset (buf->buf, 0, buf->size);
    5120                 :          66 :           break;
    5121                 :             :         }
    5122                 :        1163 :       for (HOST_WIDE_INT i = 0; i < nelts; i++)
    5123                 :         918 :         clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5124                 :             :       break;
    5125                 :         128 :     case UNION_TYPE:
    5126                 :         128 :       clear_padding_union (buf, type, sz, for_auto_init);
    5127                 :         128 :       break;
    5128                 :       29725 :     case REAL_TYPE:
    5129                 :       29725 :       gcc_assert ((size_t) sz <= clear_padding_unit);
    5130                 :       29725 :       if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
    5131                 :           0 :         clear_padding_flush (buf, false);
    5132                 :       29725 :       if (clear_padding_real_needs_padding_p (type))
    5133                 :             :         {
    5134                 :             :           /* Use native_interpret_real + native_encode_expr to figure out
    5135                 :             :              which bits are padding.  */
    5136                 :        1148 :           memset (buf->buf + buf->size, ~0, sz);
    5137                 :        1148 :           tree cst = native_interpret_real (type, buf->buf + buf->size, sz);
    5138                 :        1148 :           gcc_assert (cst && TREE_CODE (cst) == REAL_CST);
    5139                 :        1148 :           int len = native_encode_expr (cst, buf->buf + buf->size, sz);
    5140                 :        1148 :           gcc_assert (len > 0 && (size_t) len == (size_t) sz);
    5141                 :       19516 :           for (size_t i = 0; i < (size_t) sz; i++)
    5142                 :       18368 :             buf->buf[buf->size + i] ^= ~0;
    5143                 :             :         }
    5144                 :             :       else
    5145                 :       28577 :         memset (buf->buf + buf->size, 0, sz);
    5146                 :       29725 :       buf->size += sz;
    5147                 :       29725 :       break;
    5148                 :           0 :     case COMPLEX_TYPE:
    5149                 :           0 :       fldsz = int_size_in_bytes (TREE_TYPE (type));
    5150                 :           0 :       clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5151                 :           0 :       clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5152                 :           0 :       break;
    5153                 :           0 :     case VECTOR_TYPE:
    5154                 :           0 :       nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
    5155                 :           0 :       fldsz = int_size_in_bytes (TREE_TYPE (type));
    5156                 :           0 :       for (HOST_WIDE_INT i = 0; i < nelts; i++)
    5157                 :           0 :         clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5158                 :             :       break;
    5159                 :           7 :     case NULLPTR_TYPE:
    5160                 :           7 :       gcc_assert ((size_t) sz <= clear_padding_unit);
    5161                 :           7 :       if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
    5162                 :           0 :         clear_padding_flush (buf, false);
    5163                 :           7 :       memset (buf->buf + buf->size, ~0, sz);
    5164                 :           7 :       buf->size += sz;
    5165                 :           7 :       break;
    5166                 :           4 :     case BITINT_TYPE:
    5167                 :           4 :       {
    5168                 :           4 :         struct bitint_info info;
    5169                 :           4 :         bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
    5170                 :           4 :         gcc_assert (ok);
    5171                 :           4 :         scalar_int_mode limb_mode
    5172                 :           4 :           = as_a <scalar_int_mode> (info.abi_limb_mode);
    5173                 :           4 :         if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode))
    5174                 :             :           {
    5175                 :           2 :             gcc_assert ((size_t) sz <= clear_padding_unit);
    5176                 :           2 :             if ((unsigned HOST_WIDE_INT) sz + buf->size
    5177                 :             :                 > clear_padding_buf_size)
    5178                 :           0 :               clear_padding_flush (buf, false);
    5179                 :           2 :             if (!info.extended
    5180                 :           2 :                 && TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
    5181                 :             :               {
    5182                 :           2 :                 int tprec = GET_MODE_PRECISION (limb_mode);
    5183                 :           2 :                 int prec = TYPE_PRECISION (type);
    5184                 :           2 :                 tree t = build_nonstandard_integer_type (tprec, 1);
    5185                 :           2 :                 tree cst = wide_int_to_tree (t, wi::mask (prec, true, tprec));
    5186                 :           2 :                 int len = native_encode_expr (cst, buf->buf + buf->size, sz);
    5187                 :           2 :                 gcc_assert (len > 0 && (size_t) len == (size_t) sz);
    5188                 :             :               }
    5189                 :             :             else
    5190                 :           0 :               memset (buf->buf + buf->size, 0, sz);
    5191                 :           2 :             buf->size += sz;
    5192                 :           2 :             break;
    5193                 :             :           }
    5194                 :           2 :         tree limbtype
    5195                 :           2 :           = build_nonstandard_integer_type (GET_MODE_PRECISION (limb_mode), 1);
    5196                 :           2 :         fldsz = int_size_in_bytes (limbtype);
    5197                 :           2 :         nelts = int_size_in_bytes (type) / fldsz;
    5198                 :          13 :         for (HOST_WIDE_INT i = 0; i < nelts; i++)
    5199                 :             :           {
    5200                 :          11 :             if (!info.extended
    5201                 :          11 :                 && i == (info.big_endian ? 0 : nelts - 1)
    5202                 :          13 :                 && (((unsigned) TYPE_PRECISION (type))
    5203                 :           2 :                     % TYPE_PRECISION (limbtype)) != 0)
    5204                 :             :               {
    5205                 :           2 :                 int tprec = GET_MODE_PRECISION (limb_mode);
    5206                 :           2 :                 int prec = (((unsigned) TYPE_PRECISION (type)) % tprec);
    5207                 :           2 :                 tree cst = wide_int_to_tree (limbtype,
    5208                 :           2 :                                              wi::mask (prec, true, tprec));
    5209                 :           2 :                 int len = native_encode_expr (cst, buf->buf + buf->size,
    5210                 :             :                                               fldsz);
    5211                 :           2 :                 gcc_assert (len > 0 && (size_t) len == (size_t) fldsz);
    5212                 :           2 :                 buf->size += fldsz;
    5213                 :             :               }
    5214                 :             :             else
    5215                 :           9 :               clear_padding_type (buf, limbtype, fldsz, for_auto_init);
    5216                 :             :           }
    5217                 :             :         break;
    5218                 :             :       }
    5219                 :        3412 :     default:
    5220                 :        3412 :       gcc_assert ((size_t) sz <= clear_padding_unit);
    5221                 :        3412 :       if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
    5222                 :           0 :         clear_padding_flush (buf, false);
    5223                 :        3412 :       memset (buf->buf + buf->size, 0, sz);
    5224                 :        3412 :       buf->size += sz;
    5225                 :        3412 :       break;
    5226                 :             :     }
    5227                 :       34848 : }
    5228                 :             : 
    5229                 :             : /* Clear padding bits of TYPE in MASK.  */
    5230                 :             : 
    5231                 :             : void
    5232                 :       29742 : clear_type_padding_in_mask (tree type, unsigned char *mask)
    5233                 :             : {
    5234                 :       29742 :   clear_padding_struct buf;
    5235                 :       29742 :   buf.loc = UNKNOWN_LOCATION;
    5236                 :       29742 :   buf.clear_in_mask = true;
    5237                 :       29742 :   buf.base = NULL_TREE;
    5238                 :       29742 :   buf.alias_type = NULL_TREE;
    5239                 :       29742 :   buf.gsi = NULL;
    5240                 :       29742 :   buf.align = 0;
    5241                 :       29742 :   buf.off = 0;
    5242                 :       29742 :   buf.padding_bytes = 0;
    5243                 :       29742 :   buf.sz = int_size_in_bytes (type);
    5244                 :       29742 :   buf.size = 0;
    5245                 :       29742 :   buf.union_ptr = mask;
    5246                 :       29742 :   clear_padding_type (&buf, type, buf.sz, false);
    5247                 :       29742 :   clear_padding_flush (&buf, true);
    5248                 :       29742 : }
    5249                 :             : 
    5250                 :             : /* Fold __builtin_clear_padding builtin.  */
    5251                 :             : 
    5252                 :             : static bool
    5253                 :         616 : gimple_fold_builtin_clear_padding (gimple_stmt_iterator *gsi)
    5254                 :             : {
    5255                 :         616 :   gimple *stmt = gsi_stmt (*gsi);
    5256                 :         616 :   gcc_assert (gimple_call_num_args (stmt) == 2);
    5257                 :         616 :   tree ptr = gimple_call_arg (stmt, 0);
    5258                 :         616 :   tree typearg = gimple_call_arg (stmt, 1);
    5259                 :             :   /* The 2nd argument of __builtin_clear_padding's value is used to
    5260                 :             :      distinguish whether this call is made by the user or by the compiler
    5261                 :             :      for automatic variable initialization.  */
    5262                 :         616 :   bool for_auto_init = (bool) TREE_INT_CST_LOW (typearg);
    5263                 :         616 :   tree type = TREE_TYPE (TREE_TYPE (typearg));
    5264                 :         616 :   location_t loc = gimple_location (stmt);
    5265                 :         616 :   clear_padding_struct buf;
    5266                 :         616 :   gimple_stmt_iterator gsiprev = *gsi;
    5267                 :             :   /* This should be folded during the lower pass.  */
    5268                 :        1232 :   gcc_assert (!gimple_in_ssa_p (cfun) && cfun->cfg == NULL);
    5269                 :         616 :   gcc_assert (COMPLETE_TYPE_P (type));
    5270                 :         616 :   gsi_prev (&gsiprev);
    5271                 :             : 
    5272                 :         616 :   buf.loc = loc;
    5273                 :         616 :   buf.clear_in_mask = false;
    5274                 :         616 :   buf.base = ptr;
    5275                 :         616 :   buf.alias_type = NULL_TREE;
    5276                 :         616 :   buf.gsi = gsi;
    5277                 :         616 :   buf.align = get_pointer_alignment (ptr);
    5278                 :         616 :   unsigned int talign = min_align_of_type (type) * BITS_PER_UNIT;
    5279                 :         616 :   buf.align = MAX (buf.align, talign);
    5280                 :         616 :   buf.off = 0;
    5281                 :         616 :   buf.padding_bytes = 0;
    5282                 :         616 :   buf.size = 0;
    5283                 :         616 :   buf.sz = int_size_in_bytes (type);
    5284                 :         616 :   buf.union_ptr = NULL;
    5285                 :         616 :   if (buf.sz < 0 && int_size_in_bytes (strip_array_types (type)) < 0)
    5286                 :           1 :     sorry_at (loc, "%s not supported for variable length aggregates",
    5287                 :             :               "__builtin_clear_padding");
    5288                 :             :   /* The implementation currently assumes 8-bit host and target
    5289                 :             :      chars which is the case for all currently supported targets
    5290                 :             :      and hosts and is required e.g. for native_{encode,interpret}* APIs.  */
    5291                 :         615 :   else if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
    5292                 :             :     sorry_at (loc, "%s not supported on this target",
    5293                 :             :               "__builtin_clear_padding");
    5294                 :         615 :   else if (!clear_padding_type_may_have_padding_p (type))
    5295                 :             :     ;
    5296                 :         578 :   else if (TREE_CODE (type) == ARRAY_TYPE && buf.sz < 0)
    5297                 :             :     {
    5298                 :          48 :       tree sz = TYPE_SIZE_UNIT (type);
    5299                 :          48 :       tree elttype = type;
    5300                 :             :       /* Only supports C/C++ VLAs and flattens all the VLA levels.  */
    5301                 :          48 :       while (TREE_CODE (elttype) == ARRAY_TYPE
    5302                 :         144 :              && int_size_in_bytes (elttype) < 0)
    5303                 :          96 :         elttype = TREE_TYPE (elttype);
    5304                 :          48 :       HOST_WIDE_INT eltsz = int_size_in_bytes (elttype);
    5305                 :          48 :       gcc_assert (eltsz >= 0);
    5306                 :          48 :       if (eltsz)
    5307                 :             :         {
    5308                 :          48 :           buf.base = create_tmp_var (build_pointer_type (elttype));
    5309                 :          48 :           tree end = make_ssa_name (TREE_TYPE (buf.base));
    5310                 :          48 :           gimple *g = gimple_build_assign (buf.base, ptr);
    5311                 :          48 :           gimple_set_location (g, loc);
    5312                 :          48 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5313                 :          48 :           g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf.base, sz);
    5314                 :          48 :           gimple_set_location (g, loc);
    5315                 :          48 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5316                 :          48 :           buf.sz = eltsz;
    5317                 :          48 :           buf.align = TYPE_ALIGN (elttype);
    5318                 :          48 :           buf.alias_type = build_pointer_type (elttype);
    5319                 :          48 :           clear_padding_emit_loop (&buf, elttype, end, for_auto_init);
    5320                 :             :         }
    5321                 :             :     }
    5322                 :             :   else
    5323                 :             :     {
    5324                 :         530 :       if (!is_gimple_mem_ref_addr (buf.base))
    5325                 :             :         {
    5326                 :          28 :           buf.base = make_ssa_name (TREE_TYPE (ptr));
    5327                 :          28 :           gimple *g = gimple_build_assign (buf.base, ptr);
    5328                 :          28 :           gimple_set_location (g, loc);
    5329                 :          28 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5330                 :             :         }
    5331                 :         530 :       buf.alias_type = build_pointer_type (type);
    5332                 :         530 :       clear_padding_type (&buf, type, buf.sz, for_auto_init);
    5333                 :         530 :       clear_padding_flush (&buf, true);
    5334                 :             :     }
    5335                 :             : 
    5336                 :         616 :   gimple_stmt_iterator gsiprev2 = *gsi;
    5337                 :         616 :   gsi_prev (&gsiprev2);
    5338                 :         616 :   if (gsi_stmt (gsiprev) == gsi_stmt (gsiprev2))
    5339                 :         122 :     gsi_replace (gsi, gimple_build_nop (), true);
    5340                 :             :   else
    5341                 :             :     {
    5342                 :         494 :       gsi_remove (gsi, true);
    5343                 :         494 :       *gsi = gsiprev2;
    5344                 :             :     }
    5345                 :         616 :   return true;
    5346                 :             : }
    5347                 :             : 
    5348                 :             : /* Fold the non-target builtin at *GSI and return whether any simplification
    5349                 :             :    was made.  */
    5350                 :             : 
    5351                 :             : static bool
    5352                 :    13253352 : gimple_fold_builtin (gimple_stmt_iterator *gsi)
    5353                 :             : {
    5354                 :    13253352 :   gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
    5355                 :    13253352 :   tree callee = gimple_call_fndecl (stmt);
    5356                 :             : 
    5357                 :             :   /* Give up for always_inline inline builtins until they are
    5358                 :             :      inlined.  */
    5359                 :    13253352 :   if (avoid_folding_inline_builtin (callee))
    5360                 :             :     return false;
    5361                 :             : 
    5362                 :    13252176 :   unsigned n = gimple_call_num_args (stmt);
    5363                 :    13252176 :   enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
    5364                 :    13252176 :   switch (fcode)
    5365                 :             :     {
    5366                 :         148 :     case BUILT_IN_BCMP:
    5367                 :         148 :       return gimple_fold_builtin_bcmp (gsi);
    5368                 :         367 :     case BUILT_IN_BCOPY:
    5369                 :         367 :       return gimple_fold_builtin_bcopy (gsi);
    5370                 :         250 :     case BUILT_IN_BZERO:
    5371                 :         250 :       return gimple_fold_builtin_bzero (gsi);
    5372                 :             : 
    5373                 :      329646 :     case BUILT_IN_MEMSET:
    5374                 :      329646 :       return gimple_fold_builtin_memset (gsi,
    5375                 :             :                                          gimple_call_arg (stmt, 1),
    5376                 :      329646 :                                          gimple_call_arg (stmt, 2));
    5377                 :      950206 :     case BUILT_IN_MEMCPY:
    5378                 :      950206 :     case BUILT_IN_MEMPCPY:
    5379                 :      950206 :     case BUILT_IN_MEMMOVE:
    5380                 :      950206 :       return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
    5381                 :      950206 :                                             gimple_call_arg (stmt, 1), fcode);
    5382                 :        5105 :     case BUILT_IN_SPRINTF_CHK:
    5383                 :        5105 :     case BUILT_IN_VSPRINTF_CHK:
    5384                 :        5105 :       return gimple_fold_builtin_sprintf_chk (gsi, fcode);
    5385                 :        1854 :     case BUILT_IN_STRCAT_CHK:
    5386                 :        1854 :       return gimple_fold_builtin_strcat_chk (gsi);
    5387                 :        1225 :     case BUILT_IN_STRNCAT_CHK:
    5388                 :        1225 :       return gimple_fold_builtin_strncat_chk (gsi);
    5389                 :      151682 :     case BUILT_IN_STRLEN:
    5390                 :      151682 :       return gimple_fold_builtin_strlen (gsi);
    5391                 :       27680 :     case BUILT_IN_STRCPY:
    5392                 :       27680 :       return gimple_fold_builtin_strcpy (gsi,
    5393                 :             :                                          gimple_call_arg (stmt, 0),
    5394                 :       27680 :                                          gimple_call_arg (stmt, 1));
    5395                 :       19113 :     case BUILT_IN_STRNCPY:
    5396                 :       19113 :       return gimple_fold_builtin_strncpy (gsi,
    5397                 :             :                                           gimple_call_arg (stmt, 0),
    5398                 :             :                                           gimple_call_arg (stmt, 1),
    5399                 :       19113 :                                           gimple_call_arg (stmt, 2));
    5400                 :        7934 :     case BUILT_IN_STRCAT:
    5401                 :        7934 :       return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
    5402                 :        7934 :                                          gimple_call_arg (stmt, 1));
    5403                 :        7557 :     case BUILT_IN_STRNCAT:
    5404                 :        7557 :       return gimple_fold_builtin_strncat (gsi);
    5405                 :        5653 :     case BUILT_IN_INDEX:
    5406                 :        5653 :     case BUILT_IN_STRCHR:
    5407                 :        5653 :       return gimple_fold_builtin_strchr (gsi, false);
    5408                 :         792 :     case BUILT_IN_RINDEX:
    5409                 :         792 :     case BUILT_IN_STRRCHR:
    5410                 :         792 :       return gimple_fold_builtin_strchr (gsi, true);
    5411                 :        4619 :     case BUILT_IN_STRSTR:
    5412                 :        4619 :       return gimple_fold_builtin_strstr (gsi);
    5413                 :     1377759 :     case BUILT_IN_STRCMP:
    5414                 :     1377759 :     case BUILT_IN_STRCMP_EQ:
    5415                 :     1377759 :     case BUILT_IN_STRCASECMP:
    5416                 :     1377759 :     case BUILT_IN_STRNCMP:
    5417                 :     1377759 :     case BUILT_IN_STRNCMP_EQ:
    5418                 :     1377759 :     case BUILT_IN_STRNCASECMP:
    5419                 :     1377759 :       return gimple_fold_builtin_string_compare (gsi);
    5420                 :       25754 :     case BUILT_IN_MEMCHR:
    5421                 :       25754 :       return gimple_fold_builtin_memchr (gsi);
    5422                 :       21918 :     case BUILT_IN_FPUTS:
    5423                 :       21918 :       return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
    5424                 :       21918 :                                         gimple_call_arg (stmt, 1), false);
    5425                 :          44 :     case BUILT_IN_FPUTS_UNLOCKED:
    5426                 :          44 :       return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
    5427                 :          44 :                                         gimple_call_arg (stmt, 1), true);
    5428                 :       26052 :     case BUILT_IN_MEMCPY_CHK:
    5429                 :       26052 :     case BUILT_IN_MEMPCPY_CHK:
    5430                 :       26052 :     case BUILT_IN_MEMMOVE_CHK:
    5431                 :       26052 :     case BUILT_IN_MEMSET_CHK:
    5432                 :       26052 :       return gimple_fold_builtin_memory_chk (gsi,
    5433                 :             :                                              gimple_call_arg (stmt, 0),
    5434                 :             :                                              gimple_call_arg (stmt, 1),
    5435                 :             :                                              gimple_call_arg (stmt, 2),
    5436                 :             :                                              gimple_call_arg (stmt, 3),
    5437                 :       26052 :                                              fcode);
    5438                 :        4623 :     case BUILT_IN_STPCPY:
    5439                 :        4623 :       return gimple_fold_builtin_stpcpy (gsi);
    5440                 :        2791 :     case BUILT_IN_STRCPY_CHK:
    5441                 :        2791 :     case BUILT_IN_STPCPY_CHK:
    5442                 :        2791 :       return gimple_fold_builtin_stxcpy_chk (gsi,
    5443                 :             :                                              gimple_call_arg (stmt, 0),
    5444                 :             :                                              gimple_call_arg (stmt, 1),
    5445                 :             :                                              gimple_call_arg (stmt, 2),
    5446                 :        2791 :                                              fcode);
    5447                 :        2878 :     case BUILT_IN_STRNCPY_CHK:
    5448                 :        2878 :     case BUILT_IN_STPNCPY_CHK:
    5449                 :        2878 :       return gimple_fold_builtin_stxncpy_chk (gsi,
    5450                 :             :                                               gimple_call_arg (stmt, 0),
    5451                 :             :                                               gimple_call_arg (stmt, 1),
    5452                 :             :                                               gimple_call_arg (stmt, 2),
    5453                 :             :                                               gimple_call_arg (stmt, 3),
    5454                 :        2878 :                                               fcode);
    5455                 :        2917 :     case BUILT_IN_SNPRINTF_CHK:
    5456                 :        2917 :     case BUILT_IN_VSNPRINTF_CHK:
    5457                 :        2917 :       return gimple_fold_builtin_snprintf_chk (gsi, fcode);
    5458                 :             : 
    5459                 :      864953 :     case BUILT_IN_FPRINTF:
    5460                 :      864953 :     case BUILT_IN_FPRINTF_UNLOCKED:
    5461                 :      864953 :     case BUILT_IN_VFPRINTF:
    5462                 :      864953 :       if (n == 2 || n == 3)
    5463                 :      104359 :         return gimple_fold_builtin_fprintf (gsi,
    5464                 :             :                                             gimple_call_arg (stmt, 0),
    5465                 :             :                                             gimple_call_arg (stmt, 1),
    5466                 :             :                                             n == 3
    5467                 :       47248 :                                             ? gimple_call_arg (stmt, 2)
    5468                 :             :                                             : NULL_TREE,
    5469                 :       57111 :                                             fcode);
    5470                 :             :       break;
    5471                 :        2514 :     case BUILT_IN_FPRINTF_CHK:
    5472                 :        2514 :     case BUILT_IN_VFPRINTF_CHK:
    5473                 :        2514 :       if (n == 3 || n == 4)
    5474                 :        4303 :         return gimple_fold_builtin_fprintf (gsi,
    5475                 :             :                                             gimple_call_arg (stmt, 0),
    5476                 :             :                                             gimple_call_arg (stmt, 2),
    5477                 :             :                                             n == 4
    5478                 :        1971 :                                             ? gimple_call_arg (stmt, 3)
    5479                 :             :                                             : NULL_TREE,
    5480                 :        2332 :                                             fcode);
    5481                 :             :       break;
    5482                 :      198175 :     case BUILT_IN_PRINTF:
    5483                 :      198175 :     case BUILT_IN_PRINTF_UNLOCKED:
    5484                 :      198175 :     case BUILT_IN_VPRINTF:
    5485                 :      198175 :       if (n == 1 || n == 2)
    5486                 :      217861 :         return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
    5487                 :             :                                            n == 2
    5488                 :      104147 :                                            ? gimple_call_arg (stmt, 1)
    5489                 :      113714 :                                            : NULL_TREE, fcode);
    5490                 :             :       break;
    5491                 :        2571 :     case BUILT_IN_PRINTF_CHK:
    5492                 :        2571 :     case BUILT_IN_VPRINTF_CHK:
    5493                 :        2571 :       if (n == 2 || n == 3)
    5494                 :        4401 :         return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
    5495                 :             :                                            n == 3
    5496                 :        2003 :                                            ? gimple_call_arg (stmt, 2)
    5497                 :        2398 :                                            : NULL_TREE, fcode);
    5498                 :             :       break;
    5499                 :        2866 :     case BUILT_IN_ACC_ON_DEVICE:
    5500                 :        2866 :       return gimple_fold_builtin_acc_on_device (gsi,
    5501                 :        2866 :                                                 gimple_call_arg (stmt, 0));
    5502                 :         203 :     case BUILT_IN_OMP_IS_INITIAL_DEVICE:
    5503                 :         203 :       return gimple_fold_builtin_omp_is_initial_device (gsi);
    5504                 :             : 
    5505                 :       47915 :     case BUILT_IN_REALLOC:
    5506                 :       47915 :       return gimple_fold_builtin_realloc (gsi);
    5507                 :             : 
    5508                 :         616 :     case BUILT_IN_CLEAR_PADDING:
    5509                 :         616 :       return gimple_fold_builtin_clear_padding (gsi);
    5510                 :             : 
    5511                 :    10046454 :     default:;
    5512                 :             :     }
    5513                 :             : 
    5514                 :             :   /* Try the generic builtin folder.  */
    5515                 :    10046454 :   bool ignore = (gimple_call_lhs (stmt) == NULL);
    5516                 :    10046454 :   tree result = fold_call_stmt (stmt, ignore);
    5517                 :    10046454 :   if (result)
    5518                 :             :     {
    5519                 :        5678 :       if (ignore)
    5520                 :        1195 :         STRIP_NOPS (result);
    5521                 :             :       else
    5522                 :        4483 :         result = fold_convert (gimple_call_return_type (stmt), result);
    5523                 :        5678 :       gimplify_and_update_call_from_tree (gsi, result);
    5524                 :        5678 :       return true;
    5525                 :             :     }
    5526                 :             : 
    5527                 :             :   return false;
    5528                 :             : }
    5529                 :             : 
    5530                 :             : /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
    5531                 :             :    function calls to constants, where possible.  */
    5532                 :             : 
    5533                 :             : static tree
    5534                 :       19721 : fold_internal_goacc_dim (const gimple *call)
    5535                 :             : {
    5536                 :       19721 :   int axis = oacc_get_ifn_dim_arg (call);
    5537                 :       19721 :   int size = oacc_get_fn_dim_size (current_function_decl, axis);
    5538                 :       19721 :   tree result = NULL_TREE;
    5539                 :       19721 :   tree type = TREE_TYPE (gimple_call_lhs (call));
    5540                 :             : 
    5541                 :       19721 :   switch (gimple_call_internal_fn (call))
    5542                 :             :     {
    5543                 :        8570 :     case IFN_GOACC_DIM_POS:
    5544                 :             :       /* If the size is 1, we know the answer.  */
    5545                 :        8570 :       if (size == 1)
    5546                 :        8570 :         result = build_int_cst (type, 0);
    5547                 :             :       break;
    5548                 :       11151 :     case IFN_GOACC_DIM_SIZE:
    5549                 :             :       /* If the size is not dynamic, we know the answer.  */
    5550                 :       11151 :       if (size)
    5551                 :       11151 :         result = build_int_cst (type, size);
    5552                 :             :       break;
    5553                 :             :     default:
    5554                 :             :       break;
    5555                 :             :     }
    5556                 :             : 
    5557                 :       19721 :   return result;
    5558                 :             : }
    5559                 :             : 
    5560                 :             : /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
    5561                 :             :    for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
    5562                 :             :    &var where var is only addressable because of such calls.  */
    5563                 :             : 
    5564                 :             : bool
    5565                 :    57143744 : optimize_atomic_compare_exchange_p (gimple *stmt)
    5566                 :             : {
    5567                 :    57143744 :   if (gimple_call_num_args (stmt) != 6
    5568                 :     1568254 :       || !flag_inline_atomics
    5569                 :     1568254 :       || !optimize
    5570                 :     1568254 :       || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS)
    5571                 :     1568193 :       || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
    5572                 :     1025261 :       || !gimple_vdef (stmt)
    5573                 :    58059281 :       || !gimple_vuse (stmt))
    5574                 :    56228207 :     return false;
    5575                 :             : 
    5576                 :      915537 :   tree fndecl = gimple_call_fndecl (stmt);
    5577                 :      915537 :   switch (DECL_FUNCTION_CODE (fndecl))
    5578                 :             :     {
    5579                 :       52605 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
    5580                 :       52605 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
    5581                 :       52605 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
    5582                 :       52605 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
    5583                 :       52605 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
    5584                 :       52605 :       break;
    5585                 :             :     default:
    5586                 :             :       return false;
    5587                 :             :     }
    5588                 :             : 
    5589                 :       52605 :   tree expected = gimple_call_arg (stmt, 1);
    5590                 :       52605 :   if (TREE_CODE (expected) != ADDR_EXPR
    5591                 :       52605 :       || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
    5592                 :             :     return false;
    5593                 :             : 
    5594                 :       50347 :   tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
    5595                 :       50347 :   if (!is_gimple_reg_type (etype)
    5596                 :       49961 :       || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
    5597                 :       47570 :       || TREE_THIS_VOLATILE (etype)
    5598                 :       47570 :       || VECTOR_TYPE_P (etype)
    5599                 :             :       || TREE_CODE (etype) == COMPLEX_TYPE
    5600                 :             :       /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
    5601                 :             :          might not preserve all the bits.  See PR71716.  */
    5602                 :             :       || SCALAR_FLOAT_TYPE_P (etype)
    5603                 :       68441 :       || maybe_ne (TYPE_PRECISION (etype),
    5604                 :       36188 :                    GET_MODE_BITSIZE (TYPE_MODE (etype))))
    5605                 :       38595 :     return false;
    5606                 :             : 
    5607                 :       11752 :   tree weak = gimple_call_arg (stmt, 3);
    5608                 :       11752 :   if (!integer_zerop (weak) && !integer_onep (weak))
    5609                 :             :     return false;
    5610                 :             : 
    5611                 :       11752 :   tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    5612                 :       11752 :   tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
    5613                 :       11752 :   machine_mode mode = TYPE_MODE (itype);
    5614                 :             : 
    5615                 :       11752 :   if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
    5616                 :             :       == CODE_FOR_nothing
    5617                 :       11752 :       && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
    5618                 :             :     return false;
    5619                 :             : 
    5620                 :       23504 :   if (maybe_ne (int_size_in_bytes (etype), GET_MODE_SIZE (mode)))
    5621                 :             :     return false;
    5622                 :             : 
    5623                 :             :   return true;
    5624                 :             : }
    5625                 :             : 
    5626                 :             : /* Fold
    5627                 :             :      r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
    5628                 :             :    into
    5629                 :             :      _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
    5630                 :             :      i = IMAGPART_EXPR <t>;
    5631                 :             :      r = (_Bool) i;
    5632                 :             :      e = REALPART_EXPR <t>;  */
    5633                 :             : 
    5634                 :             : void
    5635                 :        5769 : fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
    5636                 :             : {
    5637                 :        5769 :   gimple *stmt = gsi_stmt (*gsi);
    5638                 :        5769 :   tree fndecl = gimple_call_fndecl (stmt);
    5639                 :        5769 :   tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    5640                 :        5769 :   tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
    5641                 :        5769 :   tree ctype = build_complex_type (itype);
    5642                 :        5769 :   tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
    5643                 :        5769 :   bool throws = false;
    5644                 :        5769 :   edge e = NULL;
    5645                 :        5769 :   gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
    5646                 :             :                                    expected);
    5647                 :        5769 :   gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5648                 :        5769 :   gimple_stmt_iterator gsiret = gsi_for_stmt (g);
    5649                 :        5769 :   if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
    5650                 :             :     {
    5651                 :        2580 :       g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
    5652                 :             :                                build1 (VIEW_CONVERT_EXPR, itype,
    5653                 :             :                                        gimple_assign_lhs (g)));
    5654                 :        2580 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5655                 :             :     }
    5656                 :        5769 :   int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
    5657                 :       11077 :              + int_size_in_bytes (itype);
    5658                 :        5769 :   g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
    5659                 :             :                                   gimple_call_arg (stmt, 0),
    5660                 :             :                                   gimple_assign_lhs (g),
    5661                 :             :                                   gimple_call_arg (stmt, 2),
    5662                 :        5769 :                                   build_int_cst (integer_type_node, flag),
    5663                 :             :                                   gimple_call_arg (stmt, 4),
    5664                 :             :                                   gimple_call_arg (stmt, 5));
    5665                 :        5769 :   tree lhs = make_ssa_name (ctype);
    5666                 :        5769 :   gimple_call_set_lhs (g, lhs);
    5667                 :        5769 :   gimple_move_vops (g, stmt);
    5668                 :        5769 :   tree oldlhs = gimple_call_lhs (stmt);
    5669                 :        5769 :   if (stmt_can_throw_internal (cfun, stmt))
    5670                 :             :     {
    5671                 :         203 :       throws = true;
    5672                 :         203 :       e = find_fallthru_edge (gsi_bb (*gsi)->succs);
    5673                 :             :     }
    5674                 :        5769 :   gimple_call_set_nothrow (as_a <gcall *> (g),
    5675                 :        5769 :                            gimple_call_nothrow_p (as_a <gcall *> (stmt)));
    5676                 :        5769 :   gimple_call_set_lhs (stmt, NULL_TREE);
    5677                 :        5769 :   gsi_replace (gsi, g, true);
    5678                 :        5769 :   if (oldlhs)
    5679                 :             :     {
    5680                 :        5722 :       g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
    5681                 :             :                                build1 (IMAGPART_EXPR, itype, lhs));
    5682                 :        5722 :       if (throws)
    5683                 :             :         {
    5684                 :         197 :           gsi_insert_on_edge_immediate (e, g);
    5685                 :         197 :           *gsi = gsi_for_stmt (g);
    5686                 :             :         }
    5687                 :             :       else
    5688                 :        5525 :         gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5689                 :        5722 :       g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
    5690                 :        5722 :       gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5691                 :             :     }
    5692                 :        5769 :   g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
    5693                 :             :                            build1 (REALPART_EXPR, itype, lhs));
    5694                 :        5769 :   if (throws && oldlhs == NULL_TREE)
    5695                 :             :     {
    5696                 :           6 :       gsi_insert_on_edge_immediate (e, g);
    5697                 :           6 :       *gsi = gsi_for_stmt (g);
    5698                 :             :     }
    5699                 :             :   else
    5700                 :        5763 :     gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5701                 :        5769 :   if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
    5702                 :             :     {
    5703                 :        5160 :       g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
    5704                 :             :                                VIEW_CONVERT_EXPR,
    5705                 :        2580 :                                build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
    5706                 :             :                                        gimple_assign_lhs (g)));
    5707                 :        2580 :       gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5708                 :             :     }
    5709                 :        5769 :   g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
    5710                 :        5769 :   gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5711                 :        5769 :   *gsi = gsiret;
    5712                 :        5769 : }
    5713                 :             : 
    5714                 :             : /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
    5715                 :             :    doesn't fit into TYPE.  The test for overflow should be regardless of
    5716                 :             :    -fwrapv, and even for unsigned types.  */
    5717                 :             : 
    5718                 :             : bool
    5719                 :      368471 : arith_overflowed_p (enum tree_code code, const_tree type,
    5720                 :             :                     const_tree arg0, const_tree arg1)
    5721                 :             : {
    5722                 :      368471 :   widest2_int warg0 = widest2_int_cst (arg0);
    5723                 :      368471 :   widest2_int warg1 = widest2_int_cst (arg1);
    5724                 :      368471 :   widest2_int wres;
    5725                 :      368471 :   switch (code)
    5726                 :             :     {
    5727                 :       96713 :     case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
    5728                 :      114919 :     case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
    5729                 :      158018 :     case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
    5730                 :           0 :     default: gcc_unreachable ();
    5731                 :             :     }
    5732                 :      368471 :   signop sign = TYPE_SIGN (type);
    5733                 :      368471 :   if (sign == UNSIGNED && wi::neg_p (wres))
    5734                 :             :     return true;
    5735                 :      296729 :   return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
    5736                 :      368483 : }
    5737                 :             : 
    5738                 :             : /* If IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL is unconditional,
    5739                 :             :    return a MEM_REF for the memory it references, otherwise return null.
    5740                 :             :    VECTYPE is the type of the memory vector.  MASK_P indicates it's for
    5741                 :             :    MASK if true, otherwise it's for LEN.  */
    5742                 :             : 
    5743                 :             : static tree
    5744                 :        3149 : gimple_fold_partial_load_store_mem_ref (gcall *call, tree vectype, bool mask_p)
    5745                 :             : {
    5746                 :        3149 :   tree ptr = gimple_call_arg (call, 0);
    5747                 :        3149 :   tree alias_align = gimple_call_arg (call, 1);
    5748                 :        3149 :   if (!tree_fits_uhwi_p (alias_align))
    5749                 :             :     return NULL_TREE;
    5750                 :             : 
    5751                 :        3149 :   if (mask_p)
    5752                 :             :     {
    5753                 :        3149 :       tree mask = gimple_call_arg (call, 2);
    5754                 :        3149 :       if (!integer_all_onesp (mask))
    5755                 :             :         return NULL_TREE;
    5756                 :             :     }
    5757                 :             :   else
    5758                 :             :     {
    5759                 :           0 :       internal_fn ifn = gimple_call_internal_fn (call);
    5760                 :           0 :       int len_index = internal_fn_len_index (ifn);
    5761                 :           0 :       tree basic_len = gimple_call_arg (call, len_index);
    5762                 :           0 :       if (!poly_int_tree_p (basic_len))
    5763                 :             :         return NULL_TREE;
    5764                 :           0 :       tree bias = gimple_call_arg (call, len_index + 1);
    5765                 :           0 :       gcc_assert (TREE_CODE (bias) == INTEGER_CST);
    5766                 :             :       /* For LEN_LOAD/LEN_STORE/MASK_LEN_LOAD/MASK_LEN_STORE,
    5767                 :             :          we don't fold when (bias + len) != VF.  */
    5768                 :           0 :       if (maybe_ne (wi::to_poly_widest (basic_len) + wi::to_widest (bias),
    5769                 :           0 :                     GET_MODE_NUNITS (TYPE_MODE (vectype))))
    5770                 :             :         return NULL_TREE;
    5771                 :             : 
    5772                 :             :       /* For MASK_LEN_{LOAD,STORE}, we should also check whether
    5773                 :             :           the mask is all ones mask.  */
    5774                 :           0 :       if (ifn == IFN_MASK_LEN_LOAD || ifn == IFN_MASK_LEN_STORE)
    5775                 :             :         {
    5776                 :           0 :           tree mask = gimple_call_arg (call, internal_fn_mask_index (ifn));
    5777                 :           0 :           if (!integer_all_onesp (mask))
    5778                 :             :             return NULL_TREE;
    5779                 :             :         }
    5780                 :             :     }
    5781                 :             : 
    5782                 :          31 :   unsigned HOST_WIDE_INT align = tree_to_uhwi (alias_align);
    5783                 :          31 :   if (TYPE_ALIGN (vectype) != align)
    5784                 :          14 :     vectype = build_aligned_type (vectype, align);
    5785                 :          31 :   tree offset = build_zero_cst (TREE_TYPE (alias_align));
    5786                 :          31 :   return fold_build2 (MEM_REF, vectype, ptr, offset);
    5787                 :             : }
    5788                 :             : 
    5789                 :             : /* Try to fold IFN_{MASK,LEN}_LOAD call CALL.  Return true on success.
    5790                 :             :    MASK_P indicates it's for MASK if true, otherwise it's for LEN.  */
    5791                 :             : 
    5792                 :             : static bool
    5793                 :        1600 : gimple_fold_partial_load (gimple_stmt_iterator *gsi, gcall *call, bool mask_p)
    5794                 :             : {
    5795                 :        1600 :   tree lhs = gimple_call_lhs (call);
    5796                 :        1600 :   if (!lhs)
    5797                 :             :     return false;
    5798                 :             : 
    5799                 :        3200 :   if (tree rhs
    5800                 :        1600 :       = gimple_fold_partial_load_store_mem_ref (call, TREE_TYPE (lhs), mask_p))
    5801                 :             :     {
    5802                 :          17 :       gassign *new_stmt = gimple_build_assign (lhs, rhs);
    5803                 :          17 :       gimple_set_location (new_stmt, gimple_location (call));
    5804                 :          17 :       gimple_move_vops (new_stmt, call);
    5805                 :          17 :       gsi_replace (gsi, new_stmt, false);
    5806                 :          17 :       return true;
    5807                 :             :     }
    5808                 :             :   return false;
    5809                 :             : }
    5810                 :             : 
    5811                 :             : /* Try to fold IFN_{MASK,LEN}_STORE call CALL.  Return true on success.
    5812                 :             :    MASK_P indicates it's for MASK if true, otherwise it's for LEN.  */
    5813                 :             : 
    5814                 :             : static bool
    5815                 :        1549 : gimple_fold_partial_store (gimple_stmt_iterator *gsi, gcall *call,
    5816                 :             :                            bool mask_p)
    5817                 :             : {
    5818                 :        1549 :   internal_fn ifn = gimple_call_internal_fn (call);
    5819                 :        1549 :   tree rhs = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
    5820                 :        3098 :   if (tree lhs
    5821                 :        1549 :       = gimple_fold_partial_load_store_mem_ref (call, TREE_TYPE (rhs), mask_p))
    5822                 :             :     {
    5823                 :          14 :       gassign *new_stmt = gimple_build_assign (lhs, rhs);
    5824                 :          14 :       gimple_set_location (new_stmt, gimple_location (call));
    5825                 :          14 :       gimple_move_vops (new_stmt, call);
    5826                 :          14 :       gsi_replace (gsi, new_stmt, false);
    5827                 :          14 :       return true;
    5828                 :             :     }
    5829                 :             :   return false;
    5830                 :             : }
    5831                 :             : 
    5832                 :             : /* Attempt to fold a call statement referenced by the statement iterator GSI.
    5833                 :             :    The statement may be replaced by another statement, e.g., if the call
    5834                 :             :    simplifies to a constant value. Return true if any changes were made.
    5835                 :             :    It is assumed that the operands have been previously folded.  */
    5836                 :             : 
    5837                 :             : static bool
    5838                 :    54832118 : gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
    5839                 :             : {
    5840                 :    54832118 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    5841                 :    54832118 :   tree callee;
    5842                 :    54832118 :   bool changed = false;
    5843                 :             : 
    5844                 :             :   /* Check for virtual calls that became direct calls.  */
    5845                 :    54832118 :   callee = gimple_call_fn (stmt);
    5846                 :    54832118 :   if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
    5847                 :             :     {
    5848                 :      467824 :       if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
    5849                 :             :         {
    5850                 :           6 :           if (dump_file && virtual_method_call_p (callee)
    5851                 :         351 :               && !possible_polymorphic_call_target_p
    5852                 :           6 :                     (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
    5853                 :           6 :                                                      (OBJ_TYPE_REF_EXPR (callee)))))
    5854                 :             :             {
    5855                 :           0 :               fprintf (dump_file,
    5856                 :             :                        "Type inheritance inconsistent devirtualization of ");
    5857                 :           0 :               print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    5858                 :           0 :               fprintf (dump_file, " to ");
    5859                 :           0 :               print_generic_expr (dump_file, callee, TDF_SLIM);
    5860                 :           0 :               fprintf (dump_file, "\n");
    5861                 :             :             }
    5862                 :             : 
    5863                 :         345 :           gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
    5864                 :         345 :           changed = true;
    5865                 :             :         }
    5866                 :      467479 :       else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
    5867                 :             :         {
    5868                 :      462724 :           bool final;
    5869                 :      462724 :           vec <cgraph_node *>targets
    5870                 :      462724 :             = possible_polymorphic_call_targets (callee, stmt, &final);
    5871                 :      465345 :           if (final && targets.length () <= 1 && dbg_cnt (devirt))
    5872                 :             :             {
    5873                 :        2030 :               tree lhs = gimple_call_lhs (stmt);
    5874                 :        2030 :               if (dump_enabled_p ())
    5875                 :             :                 {
    5876                 :          34 :                   dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
    5877                 :             :                                    "folding virtual function call to %s\n",
    5878                 :          34 :                                    targets.length () == 1
    5879                 :          17 :                                    ? targets[0]->name ()
    5880                 :             :                                    : "__builtin_unreachable");
    5881                 :             :                 }
    5882                 :        2030 :               if (targets.length () == 1)
    5883                 :             :                 {
    5884                 :        1987 :                   tree fndecl = targets[0]->decl;
    5885                 :        1987 :                   gimple_call_set_fndecl (stmt, fndecl);
    5886                 :        1987 :                   changed = true;
    5887                 :             :                   /* If changing the call to __cxa_pure_virtual
    5888                 :             :                      or similar noreturn function, adjust gimple_call_fntype
    5889                 :             :                      too.  */
    5890                 :        1987 :                   if (gimple_call_noreturn_p (stmt)
    5891                 :          28 :                       && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
    5892                 :          13 :                       && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
    5893                 :        2000 :                       && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
    5894                 :          13 :                           == void_type_node))
    5895                 :          13 :                     gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
    5896                 :             :                   /* If the call becomes noreturn, remove the lhs.  */
    5897                 :        1987 :                   if (lhs
    5898                 :        1682 :                       && gimple_call_noreturn_p (stmt)
    5899                 :        2002 :                       && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
    5900                 :           6 :                           || should_remove_lhs_p (lhs)))
    5901                 :             :                     {
    5902                 :          12 :                       if (TREE_CODE (lhs) == SSA_NAME)
    5903                 :             :                         {
    5904                 :           0 :                           tree var = create_tmp_var (TREE_TYPE (lhs));
    5905                 :           0 :                           tree def = get_or_create_ssa_default_def (cfun, var);
    5906                 :           0 :                           gimple *new_stmt = gimple_build_assign (lhs, def);
    5907                 :           0 :                           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    5908                 :             :                         }
    5909                 :          12 :                       gimple_call_set_lhs (stmt, NULL_TREE);
    5910                 :             :                     }
    5911                 :        1987 :                   maybe_remove_unused_call_args (cfun, stmt);
    5912                 :             :                 }
    5913                 :             :               else
    5914                 :             :                 {
    5915                 :          43 :                   location_t loc = gimple_location (stmt);
    5916                 :          43 :                   gimple *new_stmt = gimple_build_builtin_unreachable (loc);
    5917                 :          43 :                   gimple_call_set_ctrl_altering (new_stmt, false);
    5918                 :             :                   /* If the call had a SSA name as lhs morph that into
    5919                 :             :                      an uninitialized value.  */
    5920                 :          43 :                   if (lhs && TREE_CODE (lhs) == SSA_NAME)
    5921                 :             :                     {
    5922                 :          13 :                       tree var = create_tmp_var (TREE_TYPE (lhs));
    5923                 :          13 :                       SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
    5924                 :          13 :                       SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
    5925                 :          13 :                       set_ssa_default_def (cfun, var, lhs);
    5926                 :             :                     }
    5927                 :          43 :                   gimple_move_vops (new_stmt, stmt);
    5928                 :          43 :                   gsi_replace (gsi, new_stmt, false);
    5929                 :          43 :                   return true;
    5930                 :             :                 }
    5931                 :             :             }
    5932                 :             :         }
    5933                 :             :     }
    5934                 :             : 
    5935                 :             :   /* Check for indirect calls that became direct calls, and then
    5936                 :             :      no longer require a static chain.  */
    5937                 :    54832075 :   if (gimple_call_chain (stmt))
    5938                 :             :     {
    5939                 :      245191 :       tree fn = gimple_call_fndecl (stmt);
    5940                 :      293508 :       if (fn && !DECL_STATIC_CHAIN (fn))
    5941                 :             :         {
    5942                 :        2024 :           gimple_call_set_chain (stmt, NULL);
    5943                 :        2024 :           changed = true;
    5944                 :             :         }
    5945                 :             :     }
    5946                 :             : 
    5947                 :    54832075 :   if (inplace)
    5948                 :             :     return changed;
    5949                 :             : 
    5950                 :             :   /* Check for builtins that CCP can handle using information not
    5951                 :             :      available in the generic fold routines.  */
    5952                 :    54829723 :   if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
    5953                 :             :     {
    5954                 :    13253352 :       if (gimple_fold_builtin (gsi))
    5955                 :      193926 :         changed = true;
    5956                 :             :     }
    5957                 :    41576371 :   else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
    5958                 :             :     {
    5959                 :     1074400 :         changed |= targetm.gimple_fold_builtin (gsi);
    5960                 :             :     }
    5961                 :    40501971 :   else if (gimple_call_internal_p (stmt))
    5962                 :             :     {
    5963                 :     1380051 :       enum tree_code subcode = ERROR_MARK;
    5964                 :     1380051 :       tree result = NULL_TREE;
    5965                 :     1380051 :       bool cplx_result = false;
    5966                 :     1380051 :       bool uaddc_usubc = false;
    5967                 :     1380051 :       tree overflow = NULL_TREE;
    5968                 :     1380051 :       switch (gimple_call_internal_fn (stmt))
    5969                 :             :         {
    5970                 :      147315 :         case IFN_BUILTIN_EXPECT:
    5971                 :      147315 :           result = fold_builtin_expect (gimple_location (stmt),
    5972                 :             :                                         gimple_call_arg (stmt, 0),
    5973                 :             :                                         gimple_call_arg (stmt, 1),
    5974                 :             :                                         gimple_call_arg (stmt, 2),
    5975                 :             :                                         NULL_TREE);
    5976                 :      147315 :           break;
    5977                 :        8450 :         case IFN_UBSAN_OBJECT_SIZE:
    5978                 :        8450 :           {
    5979                 :        8450 :             tree offset = gimple_call_arg (stmt, 1);
    5980                 :        8450 :             tree objsize = gimple_call_arg (stmt, 2);
    5981                 :        8450 :             if (integer_all_onesp (objsize)
    5982                 :        8450 :                 || (TREE_CODE (offset) == INTEGER_CST
    5983                 :        4648 :                     && TREE_CODE (objsize) == INTEGER_CST
    5984                 :        1163 :                     && tree_int_cst_le (offset, objsize)))
    5985                 :             :               {
    5986                 :        1502 :                 replace_call_with_value (gsi, NULL_TREE);
    5987                 :        1502 :                 return true;
    5988                 :             :               }
    5989                 :             :           }
    5990                 :             :           break;
    5991                 :       11211 :         case IFN_UBSAN_PTR:
    5992                 :       11211 :           if (integer_zerop (gimple_call_arg (stmt, 1)))
    5993                 :             :             {
    5994                 :          31 :               replace_call_with_value (gsi, NULL_TREE);
    5995                 :          31 :               return true;
    5996                 :             :             }
    5997                 :             :           break;
    5998                 :        6770 :         case IFN_UBSAN_BOUNDS:
    5999                 :        6770 :           {
    6000                 :        6770 :             tree index = gimple_call_arg (stmt, 1);
    6001                 :        6770 :             tree bound = gimple_call_arg (stmt, 2);
    6002                 :        6770 :             if (TREE_CODE (index) == INTEGER_CST
    6003                 :        4225 :                 && TREE_CODE (bound) == INTEGER_CST)
    6004                 :             :               {
    6005                 :        4053 :                 index = fold_convert (TREE_TYPE (bound), index);
    6006                 :        4053 :                 if (TREE_CODE (index) == INTEGER_CST
    6007                 :        4053 :                     && tree_int_cst_lt (index, bound))
    6008                 :             :                   {
    6009                 :         283 :                     replace_call_with_value (gsi, NULL_TREE);
    6010                 :         283 :                     return true;
    6011                 :             :                   }
    6012                 :             :               }
    6013                 :             :           }
    6014                 :             :           break;
    6015                 :       19721 :         case IFN_GOACC_DIM_SIZE:
    6016                 :       19721 :         case IFN_GOACC_DIM_POS:
    6017                 :       19721 :           result = fold_internal_goacc_dim (stmt);
    6018                 :       19721 :           break;
    6019                 :             :         case IFN_UBSAN_CHECK_ADD:
    6020                 :             :           subcode = PLUS_EXPR;
    6021                 :             :           break;
    6022                 :             :         case IFN_UBSAN_CHECK_SUB:
    6023                 :             :           subcode = MINUS_EXPR;
    6024                 :             :           break;
    6025                 :             :         case IFN_UBSAN_CHECK_MUL:
    6026                 :             :           subcode = MULT_EXPR;
    6027                 :             :           break;
    6028                 :             :         case IFN_ADD_OVERFLOW:
    6029                 :             :           subcode = PLUS_EXPR;
    6030                 :             :           cplx_result = true;
    6031                 :             :           break;
    6032                 :             :         case IFN_SUB_OVERFLOW:
    6033                 :             :           subcode = MINUS_EXPR;
    6034                 :             :           cplx_result = true;
    6035                 :             :           break;
    6036                 :             :         case IFN_MUL_OVERFLOW:
    6037                 :             :           subcode = MULT_EXPR;
    6038                 :             :           cplx_result = true;
    6039                 :             :           break;
    6040                 :             :         case IFN_UADDC:
    6041                 :             :           subcode = PLUS_EXPR;
    6042                 :             :           cplx_result = true;
    6043                 :             :           uaddc_usubc = true;
    6044                 :             :           break;
    6045                 :             :         case IFN_USUBC:
    6046                 :             :           subcode = MINUS_EXPR;
    6047                 :             :           cplx_result = true;
    6048                 :             :           uaddc_usubc = true;
    6049                 :             :           break;
    6050                 :        1600 :         case IFN_MASK_LOAD:
    6051                 :        1600 :           changed |= gimple_fold_partial_load (gsi, stmt, true);
    6052                 :        1600 :           break;
    6053                 :        1549 :         case IFN_MASK_STORE:
    6054                 :        1549 :           changed |= gimple_fold_partial_store (gsi, stmt, true);
    6055                 :        1549 :           break;
    6056                 :           0 :         case IFN_LEN_LOAD:
    6057                 :           0 :         case IFN_MASK_LEN_LOAD:
    6058                 :           0 :           changed |= gimple_fold_partial_load (gsi, stmt, false);
    6059                 :           0 :           break;
    6060                 :           0 :         case IFN_LEN_STORE:
    6061                 :           0 :         case IFN_MASK_LEN_STORE:
    6062                 :           0 :           changed |= gimple_fold_partial_store (gsi, stmt, false);
    6063                 :           0 :           break;
    6064                 :             :         default:
    6065                 :             :           break;
    6066                 :             :         }
    6067                 :      170185 :       if (subcode != ERROR_MARK)
    6068                 :             :         {
    6069                 :      485305 :           tree arg0 = gimple_call_arg (stmt, 0);
    6070                 :      485305 :           tree arg1 = gimple_call_arg (stmt, 1);
    6071                 :      485305 :           tree arg2 = NULL_TREE;
    6072                 :      485305 :           tree type = TREE_TYPE (arg0);
    6073                 :      485305 :           if (cplx_result)
    6074                 :             :             {
    6075                 :      466294 :               tree lhs = gimple_call_lhs (stmt);
    6076                 :      466294 :               if (lhs == NULL_TREE)
    6077                 :             :                 type = NULL_TREE;
    6078                 :             :               else
    6079                 :      466294 :                 type = TREE_TYPE (TREE_TYPE (lhs));
    6080                 :      466294 :               if (uaddc_usubc)
    6081                 :       30777 :                 arg2 = gimple_call_arg (stmt, 2);
    6082                 :             :             }
    6083                 :      485305 :           if (type == NULL_TREE)
    6084                 :             :             ;
    6085                 :      485305 :           else if (uaddc_usubc)
    6086                 :             :             {
    6087                 :       30777 :               if (!integer_zerop (arg2))
    6088                 :             :                 ;
    6089                 :             :               /* x = y + 0 + 0; x = y - 0 - 0; */
    6090                 :        4824 :               else if (integer_zerop (arg1))
    6091                 :             :                 result = arg0;
    6092                 :             :               /* x = 0 + y + 0; */
    6093                 :        4203 :               else if (subcode != MINUS_EXPR && integer_zerop (arg0))
    6094                 :             :                 result = arg1;
    6095                 :             :               /* x = y - y - 0; */
    6096                 :        4203 :               else if (subcode == MINUS_EXPR
    6097                 :        4203 :                        && operand_equal_p (arg0, arg1, 0))
    6098                 :           0 :                 result = integer_zero_node;
    6099                 :             :             }
    6100                 :             :           /* x = y + 0; x = y - 0; x = y * 0; */
    6101                 :      454528 :           else if (integer_zerop (arg1))
    6102                 :       10088 :             result = subcode == MULT_EXPR ? integer_zero_node : arg0;
    6103                 :             :           /* x = 0 + y; x = 0 * y; */
    6104                 :      444440 :           else if (subcode != MINUS_EXPR && integer_zerop (arg0))
    6105                 :           0 :             result = subcode == MULT_EXPR ? integer_zero_node : arg1;
    6106                 :             :           /* x = y - y; */
    6107                 :      444440 :           else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
    6108                 :           6 :             result = integer_zero_node;
    6109                 :             :           /* x = y * 1; x = 1 * y; */
    6110                 :      444434 :           else if (subcode == MULT_EXPR && integer_onep (arg1))
    6111                 :             :             result = arg0;
    6112                 :      441127 :           else if (subcode == MULT_EXPR && integer_onep (arg0))
    6113                 :             :             result = arg1;
    6114                 :      485305 :           if (result)
    6115                 :             :             {
    6116                 :       14022 :               if (result == integer_zero_node)
    6117                 :        2139 :                 result = build_zero_cst (type);
    6118                 :       11883 :               else if (cplx_result && TREE_TYPE (result) != type)
    6119                 :             :                 {
    6120                 :        9600 :                   if (TREE_CODE (result) == INTEGER_CST)
    6121                 :             :                     {
    6122                 :           0 :                       if (arith_overflowed_p (PLUS_EXPR, type, result,
    6123                 :             :                                               integer_zero_node))
    6124                 :           0 :                         overflow = build_one_cst (type);
    6125                 :             :                     }
    6126                 :        9600 :                   else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
    6127                 :        6919 :                             && TYPE_UNSIGNED (type))
    6128                 :        9745 :                            || (TYPE_PRECISION (type)
    6129                 :        2826 :                                < (TYPE_PRECISION (TREE_TYPE (result))
    6130                 :        2826 :                                   + (TYPE_UNSIGNED (TREE_TYPE (result))
    6131                 :        3145 :                                      && !TYPE_UNSIGNED (type)))))
    6132                 :             :                     result = NULL_TREE;
    6133                 :          58 :                   if (result)
    6134                 :          58 :                     result = fold_convert (type, result);
    6135                 :             :                 }
    6136                 :             :             }
    6137                 :             :         }
    6138                 :             : 
    6139                 :      897410 :       if (result)
    6140                 :             :         {
    6141                 :       25944 :           if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
    6142                 :           0 :             result = drop_tree_overflow (result);
    6143                 :       25944 :           if (cplx_result)
    6144                 :             :             {
    6145                 :        4465 :               if (overflow == NULL_TREE)
    6146                 :        4465 :                 overflow = build_zero_cst (TREE_TYPE (result));
    6147                 :        4465 :               tree ctype = build_complex_type (TREE_TYPE (result));
    6148                 :        4465 :               if (TREE_CODE (result) == INTEGER_CST
    6149                 :        2139 :                   && TREE_CODE (overflow) == INTEGER_CST)
    6150                 :        2139 :                 result = build_complex (ctype, result, overflow);
    6151                 :             :               else
    6152                 :        2326 :                 result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
    6153                 :             :                                      ctype, result, overflow);
    6154                 :             :             }
    6155                 :       25944 :           gimplify_and_update_call_from_tree (gsi, result);
    6156                 :       25944 :           changed = true;
    6157                 :             :         }
    6158                 :             :     }
    6159                 :             : 
    6160                 :             :   return changed;
    6161                 :             : }
    6162                 :             : 
    6163                 :             : 
    6164                 :             : /* Return true whether NAME has a use on STMT.  Note this can return
    6165                 :             :    false even though there's a use on STMT if SSA operands are not
    6166                 :             :    up-to-date.  */
    6167                 :             : 
    6168                 :             : static bool
    6169                 :        1605 : has_use_on_stmt (tree name, gimple *stmt)
    6170                 :             : {
    6171                 :        1605 :   ssa_op_iter iter;
    6172                 :        1605 :   tree op;
    6173                 :        3245 :   FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
    6174                 :        1686 :     if (op == name)
    6175                 :             :       return true;
    6176                 :             :   return false;
    6177                 :             : }
    6178                 :             : 
    6179                 :             : /* Add the lhs of each statement of SEQ to DCE_WORKLIST. */
    6180                 :             : 
    6181                 :             : static void
    6182                 :     4395237 : mark_lhs_in_seq_for_dce (bitmap dce_worklist, gimple_seq seq)
    6183                 :             : {
    6184                 :     4395237 :   if (!dce_worklist)
    6185                 :             :     return;
    6186                 :             : 
    6187                 :     1526962 :   for (gimple_stmt_iterator i = gsi_start (seq);
    6188                 :     1676518 :        !gsi_end_p (i); gsi_next (&i))
    6189                 :             :     {
    6190                 :      149556 :       gimple *stmt = gsi_stmt (i);
    6191                 :      149556 :       tree name = gimple_get_lhs (stmt);
    6192                 :      149556 :       if (name && TREE_CODE (name) == SSA_NAME)
    6193                 :      149556 :         bitmap_set_bit (dce_worklist, SSA_NAME_VERSION (name));
    6194                 :             :     }
    6195                 :             : }
    6196                 :             : 
    6197                 :             : /* Worker for fold_stmt_1 dispatch to pattern based folding with
    6198                 :             :    gimple_simplify.
    6199                 :             : 
    6200                 :             :    Replaces *GSI with the simplification result in RCODE and OPS
    6201                 :             :    and the associated statements in *SEQ.  Does the replacement
    6202                 :             :    according to INPLACE and returns true if the operation succeeded.  */
    6203                 :             : 
    6204                 :             : static bool
    6205                 :     8679855 : replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
    6206                 :             :                                   gimple_match_op *res_op,
    6207                 :             :                                   gimple_seq *seq, bool inplace,
    6208                 :             :                                   bitmap dce_worklist)
    6209                 :             : {
    6210                 :     8679855 :   gimple *stmt = gsi_stmt (*gsi);
    6211                 :     8679855 :   tree *ops = res_op->ops;
    6212                 :     8679855 :   unsigned int num_ops = res_op->num_ops;
    6213                 :             : 
    6214                 :             :   /* Play safe and do not allow abnormals to be mentioned in
    6215                 :             :      newly created statements.  See also maybe_push_res_to_seq.
    6216                 :             :      As an exception allow such uses if there was a use of the
    6217                 :             :      same SSA name on the old stmt.  */
    6218                 :    19216465 :   for (unsigned int i = 0; i < num_ops; ++i)
    6219                 :    10538169 :     if (TREE_CODE (ops[i]) == SSA_NAME
    6220                 :     6495967 :         && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[i])
    6221                 :    10539774 :         && !has_use_on_stmt (ops[i], stmt))
    6222                 :             :       return false;
    6223                 :             : 
    6224                 :     8678296 :   if (num_ops > 0 && COMPARISON_CLASS_P (ops[0]))
    6225                 :           0 :     for (unsigned int i = 0; i < 2; ++i)
    6226                 :           0 :       if (TREE_CODE (TREE_OPERAND (ops[0], i)) == SSA_NAME
    6227                 :           0 :           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], i))
    6228                 :           0 :           && !has_use_on_stmt (TREE_OPERAND (ops[0], i), stmt))
    6229                 :             :         return false;
    6230                 :             : 
    6231                 :             :   /* Don't insert new statements when INPLACE is true, even if we could
    6232                 :             :      reuse STMT for the final statement.  */
    6233                 :     8678296 :   if (inplace && !gimple_seq_empty_p (*seq))
    6234                 :             :     return false;
    6235                 :             : 
    6236                 :     8678296 :   if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
    6237                 :             :     {
    6238                 :     6598756 :       gcc_assert (res_op->code.is_tree_code ());
    6239                 :     6598756 :       auto code = tree_code (res_op->code);
    6240                 :     6598756 :       if (TREE_CODE_CLASS (code) == tcc_comparison
    6241                 :             :           /* GIMPLE_CONDs condition may not throw.  */
    6242                 :     6598756 :           && (!flag_exceptions
    6243                 :      754418 :               || !cfun->can_throw_non_call_exceptions
    6244                 :      286855 :               || !operation_could_trap_p (code,
    6245                 :      286855 :                                           FLOAT_TYPE_P (TREE_TYPE (ops[0])),
    6246                 :             :                                           false, NULL_TREE)))
    6247                 :     1093295 :         gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
    6248                 :     5505461 :       else if (code == SSA_NAME)
    6249                 :             :         {
    6250                 :             :           /* If setting the gimple cond to the same thing,
    6251                 :             :              return false as nothing changed.  */
    6252                 :     3863987 :           if (gimple_cond_code (cond_stmt) == NE_EXPR
    6253                 :     3839691 :               && operand_equal_p (gimple_cond_lhs (cond_stmt), ops[0])
    6254                 :     7700283 :               && integer_zerop (gimple_cond_rhs (cond_stmt)))
    6255                 :             :             return false;
    6256                 :       27691 :           gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
    6257                 :       27691 :                                      build_zero_cst (TREE_TYPE (ops[0])));
    6258                 :             :         }
    6259                 :     1641474 :       else if (code == INTEGER_CST)
    6260                 :             :         {
    6261                 :             :           /* Make into the canonical form `1 != 0` and `0 != 0`.
    6262                 :             :              If already in the canonical form return false
    6263                 :             :              saying nothing has been done.  */
    6264                 :     1050895 :           if (integer_zerop (ops[0]))
    6265                 :             :             {
    6266                 :     5031789 :               if (gimple_cond_false_canonical_p (cond_stmt))
    6267                 :             :                 return false;
    6268                 :      444620 :               gimple_cond_make_false (cond_stmt);
    6269                 :             :             }
    6270                 :             :           else
    6271                 :             :             {
    6272                 :      303724 :               if (gimple_cond_true_canonical_p (cond_stmt))
    6273                 :             :                 return false;
    6274                 :      160339 :               gimple_cond_make_true (cond_stmt);
    6275                 :             :             }
    6276                 :             :         }
    6277                 :      590579 :       else if (!inplace)
    6278                 :             :         {
    6279                 :      590579 :           tree res = maybe_push_res_to_seq (res_op, seq);
    6280                 :      590579 :           if (!res)
    6281                 :             :             return false;
    6282                 :      590579 :           gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
    6283                 :      590579 :                                      build_zero_cst (TREE_TYPE (res)));
    6284                 :             :         }
    6285                 :             :       else
    6286                 :             :         return false;
    6287                 :     2316524 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6288                 :             :         {
    6289                 :         859 :           fprintf (dump_file, "gimple_simplified to ");
    6290                 :         859 :           if (!gimple_seq_empty_p (*seq))
    6291                 :           0 :             print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6292                 :         859 :           print_gimple_stmt (dump_file, gsi_stmt (*gsi),
    6293                 :             :                              0, TDF_SLIM);
    6294                 :             :         }
    6295                 :             :       // Mark the lhs of the new statements maybe for dce
    6296                 :     2316524 :       mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6297                 :     2316524 :       gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
    6298                 :     2316524 :       return true;
    6299                 :             :     }
    6300                 :     2079540 :   else if (is_gimple_assign (stmt)
    6301                 :     2079540 :            && res_op->code.is_tree_code ())
    6302                 :             :     {
    6303                 :     2006915 :       auto code = tree_code (res_op->code);
    6304                 :     2006915 :       if (!inplace
    6305                 :     2006915 :           || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (code))
    6306                 :             :         {
    6307                 :     2006915 :           maybe_build_generic_op (res_op);
    6308                 :     4720765 :           gimple_assign_set_rhs_with_ops (gsi, code,
    6309                 :             :                                           res_op->op_or_null (0),
    6310                 :             :                                           res_op->op_or_null (1),
    6311                 :             :                                           res_op->op_or_null (2));
    6312                 :     2006915 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6313                 :             :             {
    6314                 :       11298 :               fprintf (dump_file, "gimple_simplified to ");
    6315                 :       11298 :               if (!gimple_seq_empty_p (*seq))
    6316                 :          41 :                 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6317                 :       11298 :               print_gimple_stmt (dump_file, gsi_stmt (*gsi),
    6318                 :             :                                  0, TDF_SLIM);
    6319                 :             :             }
    6320                 :             :           // Mark the lhs of the new statements maybe for dce
    6321                 :     2006915 :           mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6322                 :     2006915 :           gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
    6323                 :     2006915 :           return true;
    6324                 :             :         }
    6325                 :             :     }
    6326                 :       72625 :   else if (res_op->code.is_fn_code ()
    6327                 :       72625 :            && gimple_call_combined_fn (stmt) == combined_fn (res_op->code))
    6328                 :             :     {
    6329                 :        7759 :       gcc_assert (num_ops == gimple_call_num_args (stmt));
    6330                 :       23085 :       for (unsigned int i = 0; i < num_ops; ++i)
    6331                 :       15326 :         gimple_call_set_arg (stmt, i, ops[i]);
    6332                 :        7759 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6333                 :             :         {
    6334                 :           0 :           fprintf (dump_file, "gimple_simplified to ");
    6335                 :           0 :           if (!gimple_seq_empty_p (*seq))
    6336                 :           0 :             print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6337                 :           0 :           print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
    6338                 :             :         }
    6339                 :             :       // Mark the lhs of the new statements maybe for dce
    6340                 :        7759 :       mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6341                 :        7759 :       gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
    6342                 :        7759 :       return true;
    6343                 :             :     }
    6344                 :       64866 :   else if (!inplace)
    6345                 :             :     {
    6346                 :      127608 :       if (gimple_has_lhs (stmt))
    6347                 :             :         {
    6348                 :       64866 :           tree lhs = gimple_get_lhs (stmt);
    6349                 :       64866 :           if (!maybe_push_res_to_seq (res_op, seq, lhs))
    6350                 :             :             return false;
    6351                 :       64039 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6352                 :             :             {
    6353                 :          10 :               fprintf (dump_file, "gimple_simplified to ");
    6354                 :          10 :               print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6355                 :             :             }
    6356                 :             :           // Mark the lhs of the new statements maybe for dce
    6357                 :       64039 :           mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6358                 :       64039 :           gsi_replace_with_seq_vops (gsi, *seq);
    6359                 :       64039 :           return true;
    6360                 :             :         }
    6361                 :             :       else
    6362                 :           0 :         gcc_unreachable ();
    6363                 :             :     }
    6364                 :             : 
    6365                 :             :   return false;
    6366                 :             : }
    6367                 :             : 
    6368                 :             : /* Canonicalize MEM_REFs invariant address operand after propagation.  */
    6369                 :             : 
    6370                 :             : static bool
    6371                 :   186731174 : maybe_canonicalize_mem_ref_addr (tree *t, bool is_debug = false)
    6372                 :             : {
    6373                 :   186731174 :   bool res = false;
    6374                 :   186731174 :   tree *orig_t = t;
    6375                 :             : 
    6376                 :   186731174 :   if (TREE_CODE (*t) == ADDR_EXPR)
    6377                 :    60118005 :     t = &TREE_OPERAND (*t, 0);
    6378                 :             : 
    6379                 :             :   /* The C and C++ frontends use an ARRAY_REF for indexing with their
    6380                 :             :      generic vector extension.  The actual vector referenced is
    6381                 :             :      view-converted to an array type for this purpose.  If the index
    6382                 :             :      is constant the canonical representation in the middle-end is a
    6383                 :             :      BIT_FIELD_REF so re-write the former to the latter here.  */
    6384                 :   186731174 :   if (TREE_CODE (*t) == ARRAY_REF
    6385                 :    10146387 :       && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
    6386                 :      145850 :       && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
    6387                 :   186774319 :       && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
    6388                 :             :     {
    6389                 :       15552 :       tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
    6390                 :       15552 :       if (VECTOR_TYPE_P (vtype))
    6391                 :             :         {
    6392                 :       15552 :           tree low = array_ref_low_bound (*t);
    6393                 :       15552 :           if (TREE_CODE (low) == INTEGER_CST)
    6394                 :             :             {
    6395                 :       15552 :               if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
    6396                 :             :                 {
    6397                 :       31060 :                   widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
    6398                 :       31060 :                                             wi::to_widest (low));
    6399                 :       15530 :                   idx = wi::mul (idx, wi::to_widest
    6400                 :       31060 :                                          (TYPE_SIZE (TREE_TYPE (*t))));
    6401                 :       15530 :                   widest_int ext
    6402                 :       15530 :                     = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
    6403                 :       15530 :                   if (maybe_le (ext, wi::to_poly_widest (TYPE_SIZE (vtype))))
    6404                 :             :                     {
    6405                 :       30464 :                       *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
    6406                 :       15232 :                                        TREE_TYPE (*t),
    6407                 :       15232 :                                        TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
    6408                 :       15232 :                                        TYPE_SIZE (TREE_TYPE (*t)),
    6409                 :       15232 :                                        wide_int_to_tree (bitsizetype, idx));
    6410                 :       15232 :                       res = true;
    6411                 :             :                     }
    6412                 :       15530 :                 }
    6413                 :             :             }
    6414                 :             :         }
    6415                 :             :     }
    6416                 :             : 
    6417                 :   347355422 :   while (handled_component_p (*t))
    6418                 :   160624248 :     t = &TREE_OPERAND (*t, 0);
    6419                 :             : 
    6420                 :             :   /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
    6421                 :             :      of invariant addresses into a SSA name MEM_REF address.  */
    6422                 :   186731174 :   if (TREE_CODE (*t) == MEM_REF
    6423                 :   186731174 :       || TREE_CODE (*t) == TARGET_MEM_REF)
    6424                 :             :     {
    6425                 :   101544756 :       tree addr = TREE_OPERAND (*t, 0);
    6426                 :   101544756 :       if (TREE_CODE (addr) == ADDR_EXPR
    6427                 :   101544756 :           && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
    6428                 :    33720442 :               || handled_component_p (TREE_OPERAND (addr, 0))))
    6429                 :             :         {
    6430                 :      538203 :           tree base;
    6431                 :      538203 :           poly_int64 coffset;
    6432                 :      538203 :           base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
    6433                 :             :                                                 &coffset);
    6434                 :      538203 :           if (!base)
    6435                 :             :             {
    6436                 :          18 :               if (is_debug)
    6437                 :          18 :                 return false;
    6438                 :           0 :               gcc_unreachable ();
    6439                 :             :             }
    6440                 :             : 
    6441                 :      538185 :           TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
    6442                 :      538185 :           TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
    6443                 :      538185 :                                                   TREE_OPERAND (*t, 1),
    6444                 :      538185 :                                                   size_int (coffset));
    6445                 :      538185 :           res = true;
    6446                 :             :         }
    6447                 :   101544738 :       gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
    6448                 :             :                            || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
    6449                 :             :     }
    6450                 :             : 
    6451                 :             :   /* Canonicalize back MEM_REFs to plain reference trees if the object
    6452                 :             :      accessed is a decl that has the same access semantics as the MEM_REF.  */
    6453                 :   186731156 :   if (TREE_CODE (*t) == MEM_REF
    6454                 :    99683305 :       && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
    6455                 :    33414562 :       && integer_zerop (TREE_OPERAND (*t, 1))
    6456                 :   205100876 :       && MR_DEPENDENCE_CLIQUE (*t) == 0)
    6457                 :             :     {
    6458                 :    11707003 :       tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
    6459                 :    11707003 :       tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
    6460                 :    11707003 :       if (/* Same volatile qualification.  */
    6461                 :    11707003 :           TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
    6462                 :             :           /* Same TBAA behavior with -fstrict-aliasing.  */
    6463                 :    11703901 :           && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
    6464                 :    11475358 :           && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
    6465                 :    11475358 :               == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
    6466                 :             :           /* Same alignment.  */
    6467                 :     5372024 :           && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
    6468                 :             :           /* We have to look out here to not drop a required conversion
    6469                 :             :              from the rhs to the lhs if *t appears on the lhs or vice-versa
    6470                 :             :              if it appears on the rhs.  Thus require strict type
    6471                 :             :              compatibility.  */
    6472                 :    16656511 :           && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
    6473                 :             :         {
    6474                 :     2379817 :           *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
    6475                 :     2379817 :           res = true;
    6476                 :             :         }
    6477                 :             :     }
    6478                 :             : 
    6479                 :   175024153 :   else if (TREE_CODE (*orig_t) == ADDR_EXPR
    6480                 :    57767381 :            && TREE_CODE (*t) == MEM_REF
    6481                 :   195360616 :            && TREE_CODE (TREE_OPERAND (*t, 0)) == INTEGER_CST)
    6482                 :             :     {
    6483                 :         774 :       tree base;
    6484                 :         774 :       poly_int64 coffset;
    6485                 :         774 :       base = get_addr_base_and_unit_offset (TREE_OPERAND (*orig_t, 0),
    6486                 :             :                                             &coffset);
    6487                 :         774 :       if (base)
    6488                 :             :         {
    6489                 :         696 :           gcc_assert (TREE_CODE (base) == MEM_REF);
    6490                 :         696 :           poly_int64 moffset;
    6491                 :         696 :           if (mem_ref_offset (base).to_shwi (&moffset))
    6492                 :             :             {
    6493                 :         696 :               coffset += moffset;
    6494                 :         696 :               if (wi::to_poly_wide (TREE_OPERAND (base, 0)).to_shwi (&moffset))
    6495                 :             :                 {
    6496                 :         696 :                   coffset += moffset;
    6497                 :         696 :                   *orig_t = build_int_cst (TREE_TYPE (*orig_t), coffset);
    6498                 :         696 :                   return true;
    6499                 :             :                 }
    6500                 :             :             }
    6501                 :             :         }
    6502                 :             :     }
    6503                 :             : 
    6504                 :             :   /* Canonicalize TARGET_MEM_REF in particular with respect to
    6505                 :             :      the indexes becoming constant.  */
    6506                 :   175023379 :   else if (TREE_CODE (*t) == TARGET_MEM_REF)
    6507                 :             :     {
    6508                 :     1861433 :       tree tem = maybe_fold_tmr (*t);
    6509                 :     1861433 :       if (tem)
    6510                 :             :         {
    6511                 :        1570 :           *t = tem;
    6512                 :        1570 :           if (TREE_CODE (*orig_t) == ADDR_EXPR)
    6513                 :           0 :             recompute_tree_invariant_for_addr_expr (*orig_t);
    6514                 :             :           res = true;
    6515                 :             :         }
    6516                 :             :     }
    6517                 :             : 
    6518                 :             :   return res;
    6519                 :             : }
    6520                 :             : 
    6521                 :             : /* Worker for both fold_stmt and fold_stmt_inplace.  The INPLACE argument
    6522                 :             :    distinguishes both cases.  */
    6523                 :             : 
    6524                 :             : static bool
    6525                 :   719646698 : fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree),
    6526                 :             :              bitmap dce_worklist = nullptr)
    6527                 :             : {
    6528                 :   719646698 :   bool changed = false;
    6529                 :   719646698 :   gimple *stmt = gsi_stmt (*gsi);
    6530                 :   719646698 :   bool nowarning = warning_suppressed_p (stmt, OPT_Wstrict_overflow);
    6531                 :   719646698 :   unsigned i;
    6532                 :   719646698 :   fold_defer_overflow_warnings ();
    6533                 :             : 
    6534                 :             :   /* First do required canonicalization of [TARGET_]MEM_REF addresses
    6535                 :             :      after propagation.
    6536                 :             :      ???  This shouldn't be done in generic folding but in the
    6537                 :             :      propagation helpers which also know whether an address was
    6538                 :             :      propagated.
    6539                 :             :      Also canonicalize operand order.  */
    6540                 :   719646698 :   switch (gimple_code (stmt))
    6541                 :             :     {
    6542                 :   249424128 :     case GIMPLE_ASSIGN:
    6543                 :   249424128 :       if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
    6544                 :             :         {
    6545                 :   165821994 :           tree *rhs = gimple_assign_rhs1_ptr (stmt);
    6546                 :   165821994 :           if ((REFERENCE_CLASS_P (*rhs)
    6547                 :   105434084 :                || TREE_CODE (*rhs) == ADDR_EXPR)
    6548                 :   180276203 :               && maybe_canonicalize_mem_ref_addr (rhs))
    6549                 :             :             changed = true;
    6550                 :   165821994 :           tree *lhs = gimple_assign_lhs_ptr (stmt);
    6551                 :   165821994 :           if (REFERENCE_CLASS_P (*lhs)
    6552                 :   165821994 :               && maybe_canonicalize_mem_ref_addr (lhs))
    6553                 :             :             changed = true;
    6554                 :             :           /* Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST.
    6555                 :             :              This cannot be done in maybe_canonicalize_mem_ref_addr
    6556                 :             :              as the gimple now has two operands rather than one.
    6557                 :             :              The same reason why this can't be done in
    6558                 :             :              maybe_canonicalize_mem_ref_addr is the same reason why
    6559                 :             :              this can't be done inplace.  */
    6560                 :   165821994 :           if (!inplace && TREE_CODE (*rhs) == ADDR_EXPR)
    6561                 :             :             {
    6562                 :    14260942 :               tree inner = TREE_OPERAND (*rhs, 0);
    6563                 :    14260942 :               if (TREE_CODE (inner) == MEM_REF
    6564                 :     1011953 :                   && TREE_CODE (TREE_OPERAND (inner, 0)) == SSA_NAME
    6565                 :    14318471 :                   && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
    6566                 :             :                 {
    6567                 :       57529 :                   tree ptr = TREE_OPERAND (inner, 0);
    6568                 :       57529 :                   tree addon = TREE_OPERAND (inner, 1);
    6569                 :       57529 :                   addon = fold_convert (sizetype, addon);
    6570                 :       57529 :                   gimple_assign_set_rhs_with_ops (gsi, POINTER_PLUS_EXPR,
    6571                 :             :                                                   ptr, addon);
    6572                 :       57529 :                   changed = true;
    6573                 :       57529 :                   stmt = gsi_stmt (*gsi);
    6574                 :             :                 }
    6575                 :             :             }
    6576                 :             :         }
    6577                 :             :       else
    6578                 :             :         {
    6579                 :             :           /* Canonicalize operand order.  */
    6580                 :    83602134 :           enum tree_code code = gimple_assign_rhs_code (stmt);
    6581                 :    83602134 :           if (TREE_CODE_CLASS (code) == tcc_comparison
    6582                 :    77891022 :               || commutative_tree_code (code)
    6583                 :   125691421 :               || commutative_ternary_tree_code (code))
    6584                 :             :             {
    6585                 :    41513394 :               tree rhs1 = gimple_assign_rhs1 (stmt);
    6586                 :    41513394 :               tree rhs2 = gimple_assign_rhs2 (stmt);
    6587                 :    41513394 :               if (tree_swap_operands_p (rhs1, rhs2))
    6588                 :             :                 {
    6589                 :     3173373 :                   gimple_assign_set_rhs1 (stmt, rhs2);
    6590                 :     3173373 :                   gimple_assign_set_rhs2 (stmt, rhs1);
    6591                 :     3173373 :                   if (TREE_CODE_CLASS (code) == tcc_comparison)
    6592                 :      293242 :                     gimple_assign_set_rhs_code (stmt,
    6593                 :             :                                                 swap_tree_comparison (code));
    6594                 :             :                   changed = true;
    6595                 :             :                 }
    6596                 :             :             }
    6597                 :             :         }
    6598                 :             :       break;
    6599                 :    54882629 :     case GIMPLE_CALL:
    6600                 :    54882629 :       {
    6601                 :    54882629 :         gcall *call = as_a<gcall *> (stmt);
    6602                 :   165389894 :         for (i = 0; i < gimple_call_num_args (call); ++i)
    6603                 :             :           {
    6604                 :   110507265 :             tree *arg = gimple_call_arg_ptr (call, i);
    6605                 :   110507265 :             if (REFERENCE_CLASS_P (*arg)
    6606                 :   110507265 :                 && maybe_canonicalize_mem_ref_addr (arg))
    6607                 :             :               changed = true;
    6608                 :             :           }
    6609                 :    54882629 :         tree *lhs = gimple_call_lhs_ptr (call);
    6610                 :    54882629 :         if (*lhs
    6611                 :    22196977 :             && REFERENCE_CLASS_P (*lhs)
    6612                 :    55025770 :             && maybe_canonicalize_mem_ref_addr (lhs))
    6613                 :             :           changed = true;
    6614                 :    54882629 :         if (*lhs)
    6615                 :             :           {
    6616                 :    22196977 :             combined_fn cfn = gimple_call_combined_fn (call);
    6617                 :    22196977 :             internal_fn ifn = associated_internal_fn (cfn, TREE_TYPE (*lhs));
    6618                 :    22196977 :             int opno = first_commutative_argument (ifn);
    6619                 :    22196977 :             if (opno >= 0)
    6620                 :             :               {
    6621                 :      347808 :                 tree arg1 = gimple_call_arg (call, opno);
    6622                 :      347808 :                 tree arg2 = gimple_call_arg (call, opno + 1);
    6623                 :      347808 :                 if (tree_swap_operands_p (arg1, arg2))
    6624                 :             :                   {
    6625                 :       22874 :                     gimple_call_set_arg (call, opno, arg2);
    6626                 :       22874 :                     gimple_call_set_arg (call, opno + 1, arg1);
    6627                 :       22874 :                     changed = true;
    6628                 :             :                   }
    6629                 :             :               }
    6630                 :             :           }
    6631                 :             :         break;
    6632                 :             :       }
    6633                 :      554845 :     case GIMPLE_ASM:
    6634                 :      554845 :       {
    6635                 :      554845 :         gasm *asm_stmt = as_a <gasm *> (stmt);
    6636                 :     1148783 :         for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
    6637                 :             :           {
    6638                 :      593938 :             tree link = gimple_asm_output_op (asm_stmt, i);
    6639                 :      593938 :             tree op = TREE_VALUE (link);
    6640                 :      593938 :             if (REFERENCE_CLASS_P (op)
    6641                 :      593938 :                 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
    6642                 :             :               changed = true;
    6643                 :             :           }
    6644                 :      920477 :         for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
    6645                 :             :           {
    6646                 :      365632 :             tree link = gimple_asm_input_op (asm_stmt, i);
    6647                 :      365632 :             tree op = TREE_VALUE (link);
    6648                 :      365632 :             if ((REFERENCE_CLASS_P (op)
    6649                 :      359867 :                  || TREE_CODE (op) == ADDR_EXPR)
    6650                 :      396390 :                 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
    6651                 :             :               changed = true;
    6652                 :             :           }
    6653                 :             :       }
    6654                 :             :       break;
    6655                 :   352964628 :     case GIMPLE_DEBUG:
    6656                 :   352964628 :       if (gimple_debug_bind_p (stmt))
    6657                 :             :         {
    6658                 :   267274212 :           tree *val = gimple_debug_bind_get_value_ptr (stmt);
    6659                 :   267274212 :           if (*val
    6660                 :   153651446 :               && (REFERENCE_CLASS_P (*val)
    6661                 :   151385731 :                   || TREE_CODE (*val) == ADDR_EXPR)
    6662                 :   315172965 :               && maybe_canonicalize_mem_ref_addr (val, true))
    6663                 :             :             changed = true;
    6664                 :             :         }
    6665                 :             :       break;
    6666                 :    41904857 :     case GIMPLE_COND:
    6667                 :    41904857 :       {
    6668                 :             :         /* Canonicalize operand order.  */
    6669                 :    41904857 :         tree lhs = gimple_cond_lhs (stmt);
    6670                 :    41904857 :         tree rhs = gimple_cond_rhs (stmt);
    6671                 :    41904857 :         if (tree_swap_operands_p (lhs, rhs))
    6672                 :             :           {
    6673                 :     1333534 :             gcond *gc = as_a <gcond *> (stmt);
    6674                 :     1333534 :             gimple_cond_set_lhs (gc, rhs);
    6675                 :     1333534 :             gimple_cond_set_rhs (gc, lhs);
    6676                 :     1333534 :             gimple_cond_set_code (gc,
    6677                 :             :                                   swap_tree_comparison (gimple_cond_code (gc)));
    6678                 :     1333534 :             changed = true;
    6679                 :             :           }
    6680                 :             :       }
    6681                 :   718180215 :     default:;
    6682                 :             :     }
    6683                 :             : 
    6684                 :             :   /* Dispatch to pattern-based folding.  */
    6685                 :   718180215 :   if (!inplace
    6686                 :     3086022 :       || is_gimple_assign (stmt)
    6687                 :   719038880 :       || gimple_code (stmt) == GIMPLE_COND)
    6688                 :             :     {
    6689                 :   718788033 :       gimple_seq seq = NULL;
    6690                 :   718788033 :       gimple_match_op res_op;
    6691                 :  1435348709 :       if (gimple_simplify (stmt, &res_op, inplace ? NULL : &seq,
    6692                 :             :                            valueize, valueize))
    6693                 :             :         {
    6694                 :     8679855 :           if (replace_stmt_with_simplification (gsi, &res_op, &seq, inplace,
    6695                 :             :                                                 dce_worklist))
    6696                 :             :             changed = true;
    6697                 :             :           else
    6698                 :     4284618 :             gimple_seq_discard (seq);
    6699                 :             :         }
    6700                 :             :     }
    6701                 :             : 
    6702                 :   719646698 :   stmt = gsi_stmt (*gsi);
    6703                 :             : 
    6704                 :             :   /* Fold the main computation performed by the statement.  */
    6705                 :   719646698 :   switch (gimple_code (stmt))
    6706                 :             :     {
    6707                 :   249474639 :     case GIMPLE_ASSIGN:
    6708                 :   249474639 :       {
    6709                 :   249474639 :         if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
    6710                 :             :           {
    6711                 :     8945039 :             if (optimize_memcpy_to_memset (gsi, gimple_assign_lhs (stmt),
    6712                 :             :                                            gimple_assign_rhs1 (stmt),
    6713                 :             :                                            /* len = */NULL_TREE))
    6714                 :             :               {
    6715                 :             :                 changed = true;
    6716                 :             :                 break;
    6717                 :             :               }
    6718                 :             :           }
    6719                 :             :         /* Try to canonicalize for boolean-typed X the comparisons
    6720                 :             :            X == 0, X == 1, X != 0, and X != 1.  */
    6721                 :   249464518 :         if (gimple_assign_rhs_code (stmt) == EQ_EXPR
    6722                 :   249464518 :             || gimple_assign_rhs_code (stmt) == NE_EXPR)
    6723                 :             :           {
    6724                 :     3155982 :             tree lhs = gimple_assign_lhs (stmt);
    6725                 :     3155982 :             tree op1 = gimple_assign_rhs1 (stmt);
    6726                 :     3155982 :             tree op2 = gimple_assign_rhs2 (stmt);
    6727                 :     3155982 :             tree type = TREE_TYPE (op1);
    6728                 :             : 
    6729                 :             :             /* Check whether the comparison operands are of the same boolean
    6730                 :             :                type as the result type is.
    6731                 :             :                Check that second operand is an integer-constant with value
    6732                 :             :                one or zero.  */
    6733                 :     3155982 :             if (TREE_CODE (op2) == INTEGER_CST
    6734                 :     2160393 :                 && (integer_zerop (op2) || integer_onep (op2))
    6735                 :     4726973 :                 && useless_type_conversion_p (TREE_TYPE (lhs), type))
    6736                 :             :               {
    6737                 :        4718 :                 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
    6738                 :        4718 :                 bool is_logical_not = false;
    6739                 :             : 
    6740                 :             :                 /* X == 0 and X != 1 is a logical-not.of X
    6741                 :             :                    X == 1 and X != 0 is X  */
    6742                 :        3918 :                 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
    6743                 :        4718 :                     || (cmp_code == NE_EXPR && integer_onep (op2)))
    6744                 :        4676 :                   is_logical_not = true;
    6745                 :             : 
    6746                 :        4718 :                 if (is_logical_not == false)
    6747                 :          42 :                   gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
    6748                 :             :                 /* Only for one-bit precision typed X the transformation
    6749                 :             :                    !X -> ~X is valied.  */
    6750                 :        4676 :                 else if (TYPE_PRECISION (type) == 1)
    6751                 :        4676 :                   gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
    6752                 :             :                 /* Otherwise we use !X -> X ^ 1.  */
    6753                 :             :                 else
    6754                 :           0 :                   gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
    6755                 :           0 :                                                   build_int_cst (type, 1));
    6756                 :             :                 changed = true;
    6757                 :             :                 break;
    6758                 :             :               }
    6759                 :             :           }
    6760                 :             : 
    6761                 :   249459800 :         unsigned old_num_ops = gimple_num_ops (stmt);
    6762                 :   249459800 :         tree lhs = gimple_assign_lhs (stmt);
    6763                 :   249459800 :         tree new_rhs = fold_gimple_assign (gsi);
    6764                 :   249459800 :         if (new_rhs
    6765                 :   249576510 :             && !useless_type_conversion_p (TREE_TYPE (lhs),
    6766                 :      116710 :                                            TREE_TYPE (new_rhs)))
    6767                 :           0 :           new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
    6768                 :   249459800 :         if (new_rhs
    6769                 :   249459800 :             && (!inplace
    6770                 :         768 :                 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
    6771                 :             :           {
    6772                 :      116710 :             gimple_assign_set_rhs_from_tree (gsi, new_rhs);
    6773                 :      116710 :             changed = true;
    6774                 :             :           }
    6775                 :             :         break;
    6776                 :             :       }
    6777                 :             : 
    6778                 :    54832118 :     case GIMPLE_CALL:
    6779                 :    54832118 :       changed |= gimple_fold_call (gsi, inplace);
    6780                 :    54832118 :       break;
    6781                 :             : 
    6782                 :   352964628 :     case GIMPLE_DEBUG:
    6783                 :   352964628 :       if (gimple_debug_bind_p (stmt))
    6784                 :             :         {
    6785                 :   267274212 :           tree val = gimple_debug_bind_get_value (stmt);
    6786                 :   267274212 :           if (val && REFERENCE_CLASS_P (val))
    6787                 :             :             {
    6788                 :     2263965 :               tree tem = maybe_fold_reference (val);
    6789                 :     2263965 :               if (tem)
    6790                 :             :                 {
    6791                 :         288 :                   gimple_debug_bind_set_value (stmt, tem);
    6792                 :         288 :                   changed = true;
    6793                 :             :                 }
    6794                 :             :             }
    6795                 :             :         }
    6796                 :             :       break;
    6797                 :             : 
    6798                 :    10336981 :     case GIMPLE_RETURN:
    6799                 :    10336981 :       {
    6800                 :    10336981 :         greturn *ret_stmt = as_a<greturn *> (stmt);
    6801                 :    10336981 :         tree ret = gimple_return_retval(ret_stmt);
    6802                 :             : 
    6803                 :    10336981 :         if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
    6804                 :             :           {
    6805                 :     4317887 :             tree val = valueize (ret);
    6806                 :     4317887 :             if (val && val != ret
    6807                 :     4317887 :                 && may_propagate_copy (ret, val))
    6808                 :             :               {
    6809                 :           0 :                 gimple_return_set_retval (ret_stmt, val);
    6810                 :           0 :                 changed = true;
    6811                 :             :               }
    6812                 :             :           }
    6813                 :             :       }
    6814                 :             :       break;
    6815                 :             : 
    6816                 :   719646698 :     default:;
    6817                 :             :     }
    6818                 :             : 
    6819                 :   719646698 :   stmt = gsi_stmt (*gsi);
    6820                 :             : 
    6821                 :   719646698 :   fold_undefer_overflow_warnings (changed && !nowarning, stmt, 0);
    6822                 :   719646698 :   return changed;
    6823                 :             : }
    6824                 :             : 
    6825                 :             : /* Valueziation callback that ends up not following SSA edges.  */
    6826                 :             : 
    6827                 :             : tree
    6828                 :  5134849214 : no_follow_ssa_edges (tree)
    6829                 :             : {
    6830                 :  5134849214 :   return NULL_TREE;
    6831                 :             : }
    6832                 :             : 
    6833                 :             : /* Valueization callback that ends up following single-use SSA edges only.  */
    6834                 :             : 
    6835                 :             : tree
    6836                 :   769191112 : follow_single_use_edges (tree val)
    6837                 :             : {
    6838                 :   769191112 :   if (TREE_CODE (val) == SSA_NAME
    6839                 :   769191112 :       && !has_single_use (val))
    6840                 :   390050385 :     return NULL_TREE;
    6841                 :             :   return val;
    6842                 :             : }
    6843                 :             : 
    6844                 :             : /* Valueization callback that follows all SSA edges.  */
    6845                 :             : 
    6846                 :             : tree
    6847                 :   168878295 : follow_all_ssa_edges (tree val)
    6848                 :             : {
    6849                 :   168878295 :   return val;
    6850                 :             : }
    6851                 :             : 
    6852                 :             : /* Fold the statement pointed to by GSI.  In some cases, this function may
    6853                 :             :    replace the whole statement with a new one.  Returns true iff folding
    6854                 :             :    makes any changes.
    6855                 :             :    The statement pointed to by GSI should be in valid gimple form but may
    6856                 :             :    be in unfolded state as resulting from for example constant propagation
    6857                 :             :    which can produce *&x = 0.  */
    6858                 :             : 
    6859                 :             : bool
    6860                 :   140393051 : fold_stmt (gimple_stmt_iterator *gsi, bitmap dce_bitmap)
    6861                 :             : {
    6862                 :   140393051 :   return fold_stmt_1 (gsi, false, no_follow_ssa_edges, dce_bitmap);
    6863                 :             : }
    6864                 :             : 
    6865                 :             : bool
    6866                 :   576167625 : fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree), bitmap dce_bitmap)
    6867                 :             : {
    6868                 :   576167625 :   return fold_stmt_1 (gsi, false, valueize, dce_bitmap);
    6869                 :             : }
    6870                 :             : 
    6871                 :             : /* Perform the minimal folding on statement *GSI.  Only operations like
    6872                 :             :    *&x created by constant propagation are handled.  The statement cannot
    6873                 :             :    be replaced with a new one.  Return true if the statement was
    6874                 :             :    changed, false otherwise.
    6875                 :             :    The statement *GSI should be in valid gimple form but may
    6876                 :             :    be in unfolded state as resulting from for example constant propagation
    6877                 :             :    which can produce *&x = 0.  */
    6878                 :             : 
    6879                 :             : bool
    6880                 :     3086022 : fold_stmt_inplace (gimple_stmt_iterator *gsi)
    6881                 :             : {
    6882                 :     3086022 :   gimple *stmt = gsi_stmt (*gsi);
    6883                 :     3086022 :   bool changed = fold_stmt_1 (gsi, true, no_follow_ssa_edges);
    6884                 :     3086022 :   gcc_assert (gsi_stmt (*gsi) == stmt);
    6885                 :     3086022 :   return changed;
    6886                 :             : }
    6887                 :             : 
    6888                 :             : /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
    6889                 :             :    if EXPR is null or we don't know how.
    6890                 :             :    If non-null, the result always has boolean type.  */
    6891                 :             : 
    6892                 :             : static tree
    6893                 :      284589 : canonicalize_bool (tree expr, bool invert)
    6894                 :             : {
    6895                 :      284589 :   if (!expr)
    6896                 :             :     return NULL_TREE;
    6897                 :          56 :   else if (invert)
    6898                 :             :     {
    6899                 :          42 :       if (integer_nonzerop (expr))
    6900                 :           0 :         return boolean_false_node;
    6901                 :          42 :       else if (integer_zerop (expr))
    6902                 :           0 :         return boolean_true_node;
    6903                 :          42 :       else if (TREE_CODE (expr) == SSA_NAME)
    6904                 :           0 :         return fold_build2 (EQ_EXPR, boolean_type_node, expr,
    6905                 :             :                             build_int_cst (TREE_TYPE (expr), 0));
    6906                 :          42 :       else if (COMPARISON_CLASS_P (expr))
    6907                 :          42 :         return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
    6908                 :             :                             boolean_type_node,
    6909                 :             :                             TREE_OPERAND (expr, 0),
    6910                 :             :                             TREE_OPERAND (expr, 1));
    6911                 :             :       else
    6912                 :             :         return NULL_TREE;
    6913                 :             :     }
    6914                 :             :   else
    6915                 :             :     {
    6916                 :          14 :       if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
    6917                 :             :         return expr;
    6918                 :           0 :       if (integer_nonzerop (expr))
    6919                 :           0 :         return boolean_true_node;
    6920                 :           0 :       else if (integer_zerop (expr))
    6921                 :           0 :         return boolean_false_node;
    6922                 :           0 :       else if (TREE_CODE (expr) == SSA_NAME)
    6923                 :           0 :         return fold_build2 (NE_EXPR, boolean_type_node, expr,
    6924                 :             :                             build_int_cst (TREE_TYPE (expr), 0));
    6925                 :           0 :       else if (COMPARISON_CLASS_P (expr))
    6926                 :           0 :         return fold_build2 (TREE_CODE (expr),
    6927                 :             :                             boolean_type_node,
    6928                 :             :                             TREE_OPERAND (expr, 0),
    6929                 :             :                             TREE_OPERAND (expr, 1));
    6930                 :             :       else
    6931                 :             :         return NULL_TREE;
    6932                 :             :     }
    6933                 :             : }
    6934                 :             : 
    6935                 :             : /* Check to see if a boolean expression EXPR is logically equivalent to the
    6936                 :             :    comparison (OP1 CODE OP2).  Check for various identities involving
    6937                 :             :    SSA_NAMEs.  */
    6938                 :             : 
    6939                 :             : static bool
    6940                 :        1967 : same_bool_comparison_p (const_tree expr, enum tree_code code,
    6941                 :             :                         const_tree op1, const_tree op2)
    6942                 :             : {
    6943                 :        1967 :   gimple *s;
    6944                 :             : 
    6945                 :             :   /* The obvious case.  */
    6946                 :        1967 :   if (TREE_CODE (expr) == code
    6947                 :          48 :       && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
    6948                 :        2015 :       && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
    6949                 :             :     return true;
    6950                 :             : 
    6951                 :             :   /* Check for comparing (name, name != 0) and the case where expr
    6952                 :             :      is an SSA_NAME with a definition matching the comparison.  */
    6953                 :        1951 :   if (TREE_CODE (expr) == SSA_NAME
    6954                 :        1951 :       && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
    6955                 :             :     {
    6956                 :           0 :       if (operand_equal_p (expr, op1, 0))
    6957                 :           0 :         return ((code == NE_EXPR && integer_zerop (op2))
    6958                 :           0 :                 || (code == EQ_EXPR && integer_nonzerop (op2)));
    6959                 :           0 :       s = SSA_NAME_DEF_STMT (expr);
    6960                 :           0 :       if (is_gimple_assign (s)
    6961                 :           0 :           && gimple_assign_rhs_code (s) == code
    6962                 :           0 :           && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
    6963                 :           0 :           && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
    6964                 :             :         return true;
    6965                 :             :     }
    6966                 :             : 
    6967                 :             :   /* If op1 is of the form (name != 0) or (name == 0), and the definition
    6968                 :             :      of name is a comparison, recurse.  */
    6969                 :        1951 :   if (TREE_CODE (op1) == SSA_NAME
    6970                 :        1951 :       && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
    6971                 :             :     {
    6972                 :         455 :       s = SSA_NAME_DEF_STMT (op1);
    6973                 :         455 :       if (is_gimple_assign (s)
    6974                 :         455 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
    6975                 :             :         {
    6976                 :           0 :           enum tree_code c = gimple_assign_rhs_code (s);
    6977                 :           0 :           if ((c == NE_EXPR && integer_zerop (op2))
    6978                 :           0 :               || (c == EQ_EXPR && integer_nonzerop (op2)))
    6979                 :           0 :             return same_bool_comparison_p (expr, c,
    6980                 :           0 :                                            gimple_assign_rhs1 (s),
    6981                 :           0 :                                            gimple_assign_rhs2 (s));
    6982                 :           0 :           if ((c == EQ_EXPR && integer_zerop (op2))
    6983                 :           0 :               || (c == NE_EXPR && integer_nonzerop (op2)))
    6984                 :           0 :             return same_bool_comparison_p (expr,
    6985                 :             :                                            invert_tree_comparison (c, false),
    6986                 :           0 :                                            gimple_assign_rhs1 (s),
    6987                 :           0 :                                            gimple_assign_rhs2 (s));
    6988                 :             :         }
    6989                 :             :     }
    6990                 :             :   return false;
    6991                 :             : }
    6992                 :             : 
    6993                 :             : /* Check to see if two boolean expressions OP1 and OP2 are logically
    6994                 :             :    equivalent.  */
    6995                 :             : 
    6996                 :             : static bool
    6997                 :          23 : same_bool_result_p (const_tree op1, const_tree op2)
    6998                 :             : {
    6999                 :             :   /* Simple cases first.  */
    7000                 :          23 :   if (operand_equal_p (op1, op2, 0))
    7001                 :             :     return true;
    7002                 :             : 
    7003                 :             :   /* Check the cases where at least one of the operands is a comparison.
    7004                 :             :      These are a bit smarter than operand_equal_p in that they apply some
    7005                 :             :      identifies on SSA_NAMEs.  */
    7006                 :          16 :   if (COMPARISON_CLASS_P (op2)
    7007                 :          32 :       && same_bool_comparison_p (op1, TREE_CODE (op2),
    7008                 :          16 :                                  TREE_OPERAND (op2, 0),
    7009                 :          16 :                                  TREE_OPERAND (op2, 1)))
    7010                 :             :     return true;
    7011                 :          16 :   if (COMPARISON_CLASS_P (op1)
    7012                 :          32 :       && same_bool_comparison_p (op2, TREE_CODE (op1),
    7013                 :          16 :                                  TREE_OPERAND (op1, 0),
    7014                 :          16 :                                  TREE_OPERAND (op1, 1)))
    7015                 :             :     return true;
    7016                 :             : 
    7017                 :             :   /* Default case.  */
    7018                 :             :   return false;
    7019                 :             : }
    7020                 :             : 
    7021                 :             : /* Forward declarations for some mutually recursive functions.  */
    7022                 :             : 
    7023                 :             : static tree
    7024                 :             : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
    7025                 :             :                    enum tree_code code2, tree op2a, tree op2b, basic_block);
    7026                 :             : static tree
    7027                 :             : and_var_with_comparison (tree type, tree var, bool invert,
    7028                 :             :                          enum tree_code code2, tree op2a, tree op2b,
    7029                 :             :                          basic_block);
    7030                 :             : static tree
    7031                 :             : and_var_with_comparison_1 (tree type, gimple *stmt,
    7032                 :             :                            enum tree_code code2, tree op2a, tree op2b,
    7033                 :             :                            basic_block);
    7034                 :             : static tree
    7035                 :             : or_comparisons_1 (tree, enum tree_code code1, tree op1a, tree op1b,
    7036                 :             :                   enum tree_code code2, tree op2a, tree op2b,
    7037                 :             :                   basic_block);
    7038                 :             : static tree
    7039                 :             : or_var_with_comparison (tree, tree var, bool invert,
    7040                 :             :                         enum tree_code code2, tree op2a, tree op2b,
    7041                 :             :                         basic_block);
    7042                 :             : static tree
    7043                 :             : or_var_with_comparison_1 (tree, gimple *stmt,
    7044                 :             :                           enum tree_code code2, tree op2a, tree op2b,
    7045                 :             :                           basic_block);
    7046                 :             : 
    7047                 :             : /* Helper function for and_comparisons_1:  try to simplify the AND of the
    7048                 :             :    ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
    7049                 :             :    If INVERT is true, invert the value of the VAR before doing the AND.
    7050                 :             :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    7051                 :             : 
    7052                 :             : static tree
    7053                 :      242096 : and_var_with_comparison (tree type, tree var, bool invert,
    7054                 :             :                          enum tree_code code2, tree op2a, tree op2b,
    7055                 :             :                          basic_block outer_cond_bb)
    7056                 :             : {
    7057                 :      242096 :   tree t;
    7058                 :      242096 :   gimple *stmt = SSA_NAME_DEF_STMT (var);
    7059                 :             : 
    7060                 :             :   /* We can only deal with variables whose definitions are assignments.  */
    7061                 :      242096 :   if (!is_gimple_assign (stmt))
    7062                 :             :     return NULL_TREE;
    7063                 :             : 
    7064                 :             :   /* If we have an inverted comparison, apply DeMorgan's law and rewrite
    7065                 :             :      !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
    7066                 :             :      Then we only have to consider the simpler non-inverted cases.  */
    7067                 :      241517 :   if (invert)
    7068                 :      147714 :     t = or_var_with_comparison_1 (type, stmt,
    7069                 :             :                                   invert_tree_comparison (code2, false),
    7070                 :             :                                   op2a, op2b, outer_cond_bb);
    7071                 :             :   else
    7072                 :       93803 :     t = and_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
    7073                 :             :                                    outer_cond_bb);
    7074                 :      241517 :   return canonicalize_bool (t, invert);
    7075                 :             : }
    7076                 :             : 
    7077                 :             : /* Try to simplify the AND of the ssa variable defined by the assignment
    7078                 :             :    STMT with the comparison specified by (OP2A CODE2 OP2B).
    7079                 :             :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    7080                 :             : 
    7081                 :             : static tree
    7082                 :      117774 : and_var_with_comparison_1 (tree type, gimple *stmt,
    7083                 :             :                            enum tree_code code2, tree op2a, tree op2b,
    7084                 :             :                            basic_block outer_cond_bb)
    7085                 :             : {
    7086                 :      117774 :   tree var = gimple_assign_lhs (stmt);
    7087                 :      117774 :   tree true_test_var = NULL_TREE;
    7088                 :      117774 :   tree false_test_var = NULL_TREE;
    7089                 :      117774 :   enum tree_code innercode = gimple_assign_rhs_code (stmt);
    7090                 :             : 
    7091                 :             :   /* Check for identities like (var AND (var == 0)) => false.  */
    7092                 :      117774 :   if (TREE_CODE (op2a) == SSA_NAME
    7093                 :      117774 :       && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
    7094                 :             :     {
    7095                 :        8436 :       if ((code2 == NE_EXPR && integer_zerop (op2b))
    7096                 :       24868 :           || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
    7097                 :             :         {
    7098                 :        7357 :           true_test_var = op2a;
    7099                 :        7357 :           if (var == true_test_var)
    7100                 :             :             return var;
    7101                 :             :         }
    7102                 :        5876 :       else if ((code2 == EQ_EXPR && integer_zerop (op2b))
    7103                 :       19523 :                || (code2 == NE_EXPR && integer_nonzerop (op2b)))
    7104                 :             :         {
    7105                 :        3405 :           false_test_var = op2a;
    7106                 :        3405 :           if (var == false_test_var)
    7107                 :           0 :             return boolean_false_node;
    7108                 :             :         }
    7109                 :             :     }
    7110                 :             : 
    7111                 :             :   /* If the definition is a comparison, recurse on it.  */
    7112                 :      117774 :   if (TREE_CODE_CLASS (innercode) == tcc_comparison)
    7113                 :             :     {
    7114                 :        4039 :       tree t = and_comparisons_1 (type, innercode,
    7115                 :             :                                   gimple_assign_rhs1 (stmt),
    7116                 :             :                                   gimple_assign_rhs2 (stmt),
    7117                 :             :                                   code2,
    7118                 :             :                                   op2a,
    7119                 :             :                                   op2b, outer_cond_bb);
    7120                 :        4039 :       if (t)
    7121                 :             :         return t;
    7122                 :             :     }
    7123                 :             : 
    7124                 :             :   /* If the definition is an AND or OR expression, we may be able to
    7125                 :             :      simplify by reassociating.  */
    7126                 :      117768 :   if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
    7127                 :      117768 :       && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
    7128                 :             :     {
    7129                 :       14383 :       tree inner1 = gimple_assign_rhs1 (stmt);
    7130                 :       14383 :       tree inner2 = gimple_assign_rhs2 (stmt);
    7131                 :       14383 :       gimple *s;
    7132                 :       14383 :       tree t;
    7133                 :       14383 :       tree partial = NULL_TREE;
    7134                 :       14383 :       bool is_and = (innercode == BIT_AND_EXPR);
    7135                 :             : 
    7136                 :             :       /* Check for boolean identities that don't require recursive examination
    7137                 :             :          of inner1/inner2:
    7138                 :             :          inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
    7139                 :             :          inner1 AND (inner1 OR inner2) => inner1
    7140                 :             :          !inner1 AND (inner1 AND inner2) => false
    7141                 :             :          !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
    7142                 :             :          Likewise for similar cases involving inner2.  */
    7143                 :       14383 :       if (inner1 == true_test_var)
    7144                 :           0 :         return (is_and ? var : inner1);
    7145                 :       14383 :       else if (inner2 == true_test_var)
    7146                 :           0 :         return (is_and ? var : inner2);
    7147                 :       14383 :       else if (inner1 == false_test_var)
    7148                 :           0 :         return (is_and
    7149                 :           0 :                 ? boolean_false_node
    7150                 :           0 :                 : and_var_with_comparison (type, inner2, false, code2, op2a,
    7151                 :             :                                            op2b, outer_cond_bb));
    7152                 :       14383 :       else if (inner2 == false_test_var)
    7153                 :           0 :         return (is_and
    7154                 :           0 :                 ? boolean_false_node
    7155                 :           0 :                 : and_var_with_comparison (type, inner1, false, code2, op2a,
    7156                 :             :                                            op2b, outer_cond_bb));
    7157                 :             : 
    7158                 :             :       /* Next, redistribute/reassociate the AND across the inner tests.
    7159                 :             :          Compute the first partial result, (inner1 AND (op2a code op2b))  */
    7160                 :       14383 :       if (TREE_CODE (inner1) == SSA_NAME
    7161                 :       14383 :           && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
    7162                 :       13600 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    7163                 :       27270 :           && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
    7164                 :             :                                               gimple_assign_rhs1 (s),
    7165                 :             :                                               gimple_assign_rhs2 (s),
    7166                 :             :                                               code2, op2a, op2b,
    7167                 :             :                                               outer_cond_bb)))
    7168                 :             :         {
    7169                 :             :           /* Handle the AND case, where we are reassociating:
    7170                 :             :              (inner1 AND inner2) AND (op2a code2 op2b)
    7171                 :             :              => (t AND inner2)
    7172                 :             :              If the partial result t is a constant, we win.  Otherwise
    7173                 :             :              continue on to try reassociating with the other inner test.  */
    7174                 :          60 :           if (is_and)
    7175                 :             :             {
    7176                 :           3 :               if (integer_onep (t))
    7177                 :             :                 return inner2;
    7178                 :           3 :               else if (integer_zerop (t))
    7179                 :           0 :                 return boolean_false_node;
    7180                 :             :             }
    7181                 :             : 
    7182                 :             :           /* Handle the OR case, where we are redistributing:
    7183                 :             :              (inner1 OR inner2) AND (op2a code2 op2b)
    7184                 :             :              => (t OR (inner2 AND (op2a code2 op2b)))  */
    7185                 :          57 :           else if (integer_onep (t))
    7186                 :           0 :             return boolean_true_node;
    7187                 :             : 
    7188                 :             :           /* Save partial result for later.  */
    7189                 :             :           partial = t;
    7190                 :             :         }
    7191                 :             : 
    7192                 :             :       /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
    7193                 :       14383 :       if (TREE_CODE (inner2) == SSA_NAME
    7194                 :       14383 :           && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
    7195                 :       14103 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    7196                 :       27415 :           && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
    7197                 :             :                                               gimple_assign_rhs1 (s),
    7198                 :             :                                               gimple_assign_rhs2 (s),
    7199                 :             :                                               code2, op2a, op2b,
    7200                 :             :                                               outer_cond_bb)))
    7201                 :             :         {
    7202                 :             :           /* Handle the AND case, where we are reassociating:
    7203                 :             :              (inner1 AND inner2) AND (op2a code2 op2b)
    7204                 :             :              => (inner1 AND t)  */
    7205                 :          51 :           if (is_and)
    7206                 :             :             {
    7207                 :          14 :               if (integer_onep (t))
    7208                 :             :                 return inner1;
    7209                 :          14 :               else if (integer_zerop (t))
    7210                 :           1 :                 return boolean_false_node;
    7211                 :             :               /* If both are the same, we can apply the identity
    7212                 :             :                  (x AND x) == x.  */
    7213                 :          13 :               else if (partial && same_bool_result_p (t, partial))
    7214                 :             :                 return t;
    7215                 :             :             }
    7216                 :             : 
    7217                 :             :           /* Handle the OR case. where we are redistributing:
    7218                 :             :              (inner1 OR inner2) AND (op2a code2 op2b)
    7219                 :             :              => (t OR (inner1 AND (op2a code2 op2b)))
    7220                 :             :              => (t OR partial)  */
    7221                 :             :           else
    7222                 :             :             {
    7223                 :          37 :               if (integer_onep (t))
    7224                 :           0 :                 return boolean_true_node;
    7225                 :          37 :               else if (partial)
    7226                 :             :                 {
    7227                 :             :                   /* We already got a simplification for the other
    7228                 :             :                      operand to the redistributed OR expression.  The
    7229                 :             :                      interesting case is when at least one is false.
    7230                 :             :                      Or, if both are the same, we can apply the identity
    7231                 :             :                      (x OR x) == x.  */
    7232                 :          14 :                   if (integer_zerop (partial))
    7233                 :             :                     return t;
    7234                 :          14 :                   else if (integer_zerop (t))
    7235                 :             :                     return partial;
    7236                 :          12 :                   else if (same_bool_result_p (t, partial))
    7237                 :             :                     return t;
    7238                 :             :                 }
    7239                 :             :             }
    7240                 :             :         }
    7241                 :             :     }
    7242                 :             :   return NULL_TREE;
    7243                 :             : }
    7244                 :             : 
    7245                 :             : /* Try to simplify the AND of two comparisons defined by
    7246                 :             :    (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
    7247                 :             :    If this can be done without constructing an intermediate value,
    7248                 :             :    return the resulting tree; otherwise NULL_TREE is returned.
    7249                 :             :    This function is deliberately asymmetric as it recurses on SSA_DEFs
    7250                 :             :    in the first comparison but not the second.  */
    7251                 :             : 
    7252                 :             : static tree
    7253                 :      893082 : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
    7254                 :             :                    enum tree_code code2, tree op2a, tree op2b,
    7255                 :             :                    basic_block outer_cond_bb)
    7256                 :             : {
    7257                 :      893082 :   tree truth_type = truth_type_for (TREE_TYPE (op1a));
    7258                 :             : 
    7259                 :             :   /* First check for ((x CODE1 y) AND (x CODE2 y)).  */
    7260                 :      893082 :   if (operand_equal_p (op1a, op2a, 0)
    7261                 :      893082 :       && operand_equal_p (op1b, op2b, 0))
    7262                 :             :     {
    7263                 :             :       /* Result will be either NULL_TREE, or a combined comparison.  */
    7264                 :        3734 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    7265                 :             :                                     TRUTH_ANDIF_EXPR, code1, code2,
    7266                 :             :                                     truth_type, op1a, op1b);
    7267                 :        3734 :       if (t)
    7268                 :             :         return t;
    7269                 :             :     }
    7270                 :             : 
    7271                 :             :   /* Likewise the swapped case of the above.  */
    7272                 :      892164 :   if (operand_equal_p (op1a, op2b, 0)
    7273                 :      892164 :       && operand_equal_p (op1b, op2a, 0))
    7274                 :             :     {
    7275                 :             :       /* Result will be either NULL_TREE, or a combined comparison.  */
    7276                 :           0 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    7277                 :             :                                     TRUTH_ANDIF_EXPR, code1,
    7278                 :             :                                     swap_tree_comparison (code2),
    7279                 :             :                                     truth_type, op1a, op1b);
    7280                 :           0 :       if (t)
    7281                 :             :         return t;
    7282                 :             :     }
    7283                 :             : 
    7284                 :             :   /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
    7285                 :             :      NAME's definition is a truth value.  See if there are any simplifications
    7286                 :             :      that can be done against the NAME's definition.  */
    7287                 :      892164 :   if (TREE_CODE (op1a) == SSA_NAME
    7288                 :      891731 :       && (code1 == NE_EXPR || code1 == EQ_EXPR)
    7289                 :     1529730 :       && (integer_zerop (op1b) || integer_onep (op1b)))
    7290                 :             :     {
    7291                 :      184200 :       bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
    7292                 :      322184 :                      || (code1 == NE_EXPR && integer_onep (op1b)));
    7293                 :      297009 :       gimple *stmt = SSA_NAME_DEF_STMT (op1a);
    7294                 :      297009 :       switch (gimple_code (stmt))
    7295                 :             :         {
    7296                 :      237314 :         case GIMPLE_ASSIGN:
    7297                 :             :           /* Try to simplify by copy-propagating the definition.  */
    7298                 :      237314 :           return and_var_with_comparison (type, op1a, invert, code2, op2a,
    7299                 :      237314 :                                           op2b, outer_cond_bb);
    7300                 :             : 
    7301                 :       31150 :         case GIMPLE_PHI:
    7302                 :             :           /* If every argument to the PHI produces the same result when
    7303                 :             :              ANDed with the second comparison, we win.
    7304                 :             :              Do not do this unless the type is bool since we need a bool
    7305                 :             :              result here anyway.  */
    7306                 :       31150 :           if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
    7307                 :             :             {
    7308                 :             :               tree result = NULL_TREE;
    7309                 :             :               unsigned i;
    7310                 :       14961 :               for (i = 0; i < gimple_phi_num_args (stmt); i++)
    7311                 :             :                 {
    7312                 :       14961 :                   tree arg = gimple_phi_arg_def (stmt, i);
    7313                 :             : 
    7314                 :             :                   /* If this PHI has itself as an argument, ignore it.
    7315                 :             :                      If all the other args produce the same result,
    7316                 :             :                      we're still OK.  */
    7317                 :       14961 :                   if (arg == gimple_phi_result (stmt))
    7318                 :           0 :                     continue;
    7319                 :       14961 :                   else if (TREE_CODE (arg) == INTEGER_CST)
    7320                 :             :                     {
    7321                 :        9087 :                       if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
    7322                 :             :                         {
    7323                 :        5329 :                           if (!result)
    7324                 :        2265 :                             result = boolean_false_node;
    7325                 :        3064 :                           else if (!integer_zerop (result))
    7326                 :             :                             return NULL_TREE;
    7327                 :             :                         }
    7328                 :        3758 :                       else if (!result)
    7329                 :        2043 :                         result = fold_build2 (code2, boolean_type_node,
    7330                 :             :                                               op2a, op2b);
    7331                 :        1715 :                       else if (!same_bool_comparison_p (result,
    7332                 :             :                                                         code2, op2a, op2b))
    7333                 :             :                         return NULL_TREE;
    7334                 :             :                     }
    7335                 :        5874 :                   else if (TREE_CODE (arg) == SSA_NAME
    7336                 :        5874 :                            && !SSA_NAME_IS_DEFAULT_DEF (arg))
    7337                 :             :                     {
    7338                 :        5871 :                       tree temp;
    7339                 :        5871 :                       gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
    7340                 :             :                       /* In simple cases we can look through PHI nodes,
    7341                 :             :                          but we have to be careful with loops.
    7342                 :             :                          See PR49073.  */
    7343                 :        5871 :                       if (! dom_info_available_p (CDI_DOMINATORS)
    7344                 :        5871 :                           || gimple_bb (def_stmt) == gimple_bb (stmt)
    7345                 :       11742 :                           || dominated_by_p (CDI_DOMINATORS,
    7346                 :        5871 :                                              gimple_bb (def_stmt),
    7347                 :        5871 :                                              gimple_bb (stmt)))
    7348                 :        1089 :                         return NULL_TREE;
    7349                 :        4782 :                       temp = and_var_with_comparison (type, arg, invert, code2,
    7350                 :             :                                                       op2a, op2b,
    7351                 :             :                                                       outer_cond_bb);
    7352                 :        4782 :                       if (!temp)
    7353                 :             :                         return NULL_TREE;
    7354                 :           0 :                       else if (!result)
    7355                 :             :                         result = temp;
    7356                 :           0 :                       else if (!same_bool_result_p (result, temp))
    7357                 :             :                         return NULL_TREE;
    7358                 :             :                     }
    7359                 :             :                   else
    7360                 :             :                     return NULL_TREE;
    7361                 :             :                 }
    7362                 :             :               return result;
    7363                 :             :             }
    7364                 :             : 
    7365                 :             :         default:
    7366                 :             :           break;
    7367                 :             :         }
    7368                 :             :     }
    7369                 :             :   return NULL_TREE;
    7370                 :             : }
    7371                 :             : 
    7372                 :             : static basic_block fosa_bb;
    7373                 :             : static vec<std::pair<tree, flow_sensitive_info_storage> > *fosa_unwind;
    7374                 :             : static tree
    7375                 :    30975560 : follow_outer_ssa_edges (tree val)
    7376                 :             : {
    7377                 :    30975560 :   if (TREE_CODE (val) == SSA_NAME
    7378                 :    30975560 :       && !SSA_NAME_IS_DEFAULT_DEF (val))
    7379                 :             :     {
    7380                 :    30565596 :       basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
    7381                 :    30565596 :       if (!def_bb
    7382                 :     9852079 :           || def_bb == fosa_bb
    7383                 :    35839849 :           || (dom_info_available_p (CDI_DOMINATORS)
    7384                 :     5274253 :               && (def_bb == fosa_bb
    7385                 :     5274253 :                   || dominated_by_p (CDI_DOMINATORS, fosa_bb, def_bb))))
    7386                 :    27597430 :         return val;
    7387                 :             :       /* We cannot temporarily rewrite stmts with undefined overflow
    7388                 :             :          behavior, so avoid expanding them.  */
    7389                 :     5925970 :       if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (val))
    7390                 :      216213 :            || POINTER_TYPE_P (TREE_TYPE (val)))
    7391                 :     5840065 :           && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (val)))
    7392                 :             :         return NULL_TREE;
    7393                 :     1978751 :       flow_sensitive_info_storage storage;
    7394                 :     1978751 :       storage.save_and_clear (val);
    7395                 :             :       /* If the definition does not dominate fosa_bb temporarily reset
    7396                 :             :          flow-sensitive info.  */
    7397                 :     1978751 :       fosa_unwind->safe_push (std::make_pair (val, storage));
    7398                 :     1978751 :       return val;
    7399                 :             :     }
    7400                 :             :   return val;
    7401                 :             : }
    7402                 :             : 
    7403                 :             : /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
    7404                 :             :    : try to simplify the AND/OR of the ssa variable VAR with the comparison
    7405                 :             :    specified by (OP2A CODE2 OP2B) from match.pd.  Return NULL_EXPR if we can't
    7406                 :             :    simplify this to a single expression.  As we are going to lower the cost
    7407                 :             :    of building SSA names / gimple stmts significantly, we need to allocate
    7408                 :             :    them ont the stack.  This will cause the code to be a bit ugly.  */
    7409                 :             : 
    7410                 :             : static tree
    7411                 :      936756 : maybe_fold_comparisons_from_match_pd (tree type, enum tree_code code,
    7412                 :             :                                       enum tree_code code1,
    7413                 :             :                                       tree op1a, tree op1b,
    7414                 :             :                                       enum tree_code code2, tree op2a,
    7415                 :             :                                       tree op2b,
    7416                 :             :                                       basic_block outer_cond_bb)
    7417                 :             : {
    7418                 :             :   /* Allocate gimple stmt1 on the stack.  */
    7419                 :      936756 :   gassign *stmt1
    7420                 :      936756 :     = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
    7421                 :      936756 :   gimple_init (stmt1, GIMPLE_ASSIGN, 3);
    7422                 :      936756 :   gimple_assign_set_rhs_code (stmt1, code1);
    7423                 :      936756 :   gimple_assign_set_rhs1 (stmt1, op1a);
    7424                 :      936756 :   gimple_assign_set_rhs2 (stmt1, op1b);
    7425                 :      936756 :   gimple_set_bb (stmt1, NULL);
    7426                 :             : 
    7427                 :             :   /* Allocate gimple stmt2 on the stack.  */
    7428                 :      936756 :   gassign *stmt2
    7429                 :      936756 :     = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
    7430                 :      936756 :   gimple_init (stmt2, GIMPLE_ASSIGN, 3);
    7431                 :      936756 :   gimple_assign_set_rhs_code (stmt2, code2);
    7432                 :      936756 :   gimple_assign_set_rhs1 (stmt2, op2a);
    7433                 :      936756 :   gimple_assign_set_rhs2 (stmt2, op2b);
    7434                 :      936756 :   gimple_set_bb (stmt2, NULL);
    7435                 :             : 
    7436                 :             :   /* Allocate SSA names(lhs1) on the stack.  */
    7437                 :      936756 :   alignas (tree_node) unsigned char lhs1buf[sizeof (tree_ssa_name)];
    7438                 :      936756 :   tree lhs1 = (tree) &lhs1buf[0];
    7439                 :      936756 :   memset (lhs1, 0, sizeof (tree_ssa_name));
    7440                 :      936756 :   TREE_SET_CODE (lhs1, SSA_NAME);
    7441                 :      936756 :   TREE_TYPE (lhs1) = type;
    7442                 :      936756 :   init_ssa_name_imm_use (lhs1);
    7443                 :             : 
    7444                 :             :   /* Allocate SSA names(lhs2) on the stack.  */
    7445                 :      936756 :   alignas (tree_node) unsigned char lhs2buf[sizeof (tree_ssa_name)];
    7446                 :      936756 :   tree lhs2 = (tree) &lhs2buf[0];
    7447                 :      936756 :   memset (lhs2, 0, sizeof (tree_ssa_name));
    7448                 :      936756 :   TREE_SET_CODE (lhs2, SSA_NAME);
    7449                 :      936756 :   TREE_TYPE (lhs2) = type;
    7450                 :      936756 :   init_ssa_name_imm_use (lhs2);
    7451                 :             : 
    7452                 :      936756 :   gimple_assign_set_lhs (stmt1, lhs1);
    7453                 :      936756 :   gimple_assign_set_lhs (stmt2, lhs2);
    7454                 :             : 
    7455                 :      936756 :   gimple_match_op op (gimple_match_cond::UNCOND, code,
    7456                 :             :                       type, gimple_assign_lhs (stmt1),
    7457                 :      936756 :                       gimple_assign_lhs (stmt2));
    7458                 :      936756 :   fosa_bb = outer_cond_bb;
    7459                 :      936756 :   auto_vec<std::pair<tree, flow_sensitive_info_storage>, 8> unwind_stack;
    7460                 :      936756 :   fosa_unwind = &unwind_stack;
    7461                 :     1304076 :   if (op.resimplify (NULL, (!outer_cond_bb
    7462                 :             :                             ? follow_all_ssa_edges : follow_outer_ssa_edges)))
    7463                 :             :     {
    7464                 :        2031 :       fosa_unwind = NULL;
    7465                 :        7384 :       for (auto p : unwind_stack)
    7466                 :        1291 :         p.second.restore (p.first);
    7467                 :        2031 :       if (gimple_simplified_result_is_gimple_val (&op))
    7468                 :             :         {
    7469                 :        1434 :           tree res = op.ops[0];
    7470                 :        1434 :           if (res == lhs1)
    7471                 :        1076 :             return build2 (code1, type, op1a, op1b);
    7472                 :         358 :           else if (res == lhs2)
    7473                 :         330 :             return build2 (code2, type, op2a, op2b);
    7474                 :             :           else
    7475                 :             :             return res;
    7476                 :             :         }
    7477                 :         597 :       else if (op.code.is_tree_code ()
    7478                 :         597 :                && TREE_CODE_CLASS ((tree_code)op.code) == tcc_comparison)
    7479                 :             :         {
    7480                 :         597 :           tree op0 = op.ops[0];
    7481                 :         597 :           tree op1 = op.ops[1];
    7482                 :         597 :           if (op0 == lhs1 || op0 == lhs2 || op1 == lhs1 || op1 == lhs2)
    7483                 :             :             return NULL_TREE;  /* not simple */
    7484                 :             : 
    7485                 :         597 :           return build2 ((enum tree_code)op.code, op.type, op0, op1);
    7486                 :             :         }
    7487                 :             :     }
    7488                 :      934725 :   fosa_unwind = NULL;
    7489                 :     4781635 :   for (auto p : unwind_stack)
    7490                 :     1977460 :     p.second.restore (p.first);
    7491                 :             : 
    7492                 :      934725 :   return NULL_TREE;
    7493                 :      936756 : }
    7494                 :             : 
    7495                 :             : /* Return TRUE and set op[0] if T, following all SSA edges, is a type
    7496                 :             :    conversion.  Reject loads if LOAD is NULL, otherwise set *LOAD if a
    7497                 :             :    converting load is found.  */
    7498                 :             : 
    7499                 :             : static bool
    7500                 :     1945392 : gimple_convert_def_p (tree t, tree op[1], gimple **load = NULL)
    7501                 :             : {
    7502                 :     1945392 :   bool ret = false;
    7503                 :             : 
    7504                 :     1945392 :   if (TREE_CODE (t) == SSA_NAME
    7505                 :     1945392 :       && !SSA_NAME_IS_DEFAULT_DEF (t))
    7506                 :     1104732 :     if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
    7507                 :             :       {
    7508                 :     1025011 :         bool load_p = gimple_assign_load_p (def);
    7509                 :     1025011 :         if (load_p && !load)
    7510                 :             :           return false;
    7511                 :      754541 :         switch (gimple_assign_rhs_code (def))
    7512                 :             :           {
    7513                 :       11167 :           CASE_CONVERT:
    7514                 :       11167 :             op[0] = gimple_assign_rhs1 (def);
    7515                 :       11167 :             ret = true;
    7516                 :       11167 :             break;
    7517                 :             : 
    7518                 :        1653 :           case VIEW_CONVERT_EXPR:
    7519                 :        1653 :             op[0] = TREE_OPERAND (gimple_assign_rhs1 (def), 0);
    7520                 :        1653 :             ret = true;
    7521                 :        1653 :             break;
    7522                 :             : 
    7523                 :             :           default:
    7524                 :             :             break;
    7525                 :             :           }
    7526                 :             : 
    7527                 :       12820 :         if (ret && load_p)
    7528                 :           0 :           *load = def;
    7529                 :             :       }
    7530                 :             : 
    7531                 :             :   return ret;
    7532                 :             : }
    7533                 :             : 
    7534                 :             : /* Return TRUE and set op[*] if T, following all SSA edges, resolves to a
    7535                 :             :    binary expression with code CODE.  */
    7536                 :             : 
    7537                 :             : static bool
    7538                 :     1970772 : gimple_binop_def_p (enum tree_code code, tree t, tree op[2])
    7539                 :             : {
    7540                 :     1970772 :   if (TREE_CODE (t) == SSA_NAME
    7541                 :     1970772 :       && !SSA_NAME_IS_DEFAULT_DEF (t))
    7542                 :     1129147 :     if (gimple *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
    7543                 :     1045878 :       if (gimple_assign_rhs_code (def) == code)
    7544                 :             :         {
    7545                 :       37292 :           op[0] = gimple_assign_rhs1 (def);
    7546                 :       37292 :           op[1] = gimple_assign_rhs2 (def);
    7547                 :       37292 :           return true;
    7548                 :             :         }
    7549                 :             :   return false;
    7550                 :             : }
    7551                 :             : /* Subroutine for fold_truth_andor_1: decode a field reference.
    7552                 :             : 
    7553                 :             :    If *PEXP is a comparison reference, we return the innermost reference.
    7554                 :             : 
    7555                 :             :    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
    7556                 :             :    set to the starting bit number.
    7557                 :             : 
    7558                 :             :    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
    7559                 :             :    otherwise it is not changed.
    7560                 :             : 
    7561                 :             :    *PUNSIGNEDP is set to the signedness of the field.
    7562                 :             : 
    7563                 :             :    *PREVERSEP is set to the storage order of the field.
    7564                 :             : 
    7565                 :             :    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.  If
    7566                 :             :    *PAND_MASK is initially set to a mask with nonzero precision, that mask is
    7567                 :             :    combined with the found mask, or adjusted in precision to match.
    7568                 :             : 
    7569                 :             :    *PSIGNBIT is set to TRUE if, before clipping to *PBITSIZE, the mask
    7570                 :             :    encompassed bits that corresponded to extensions of the sign bit.
    7571                 :             : 
    7572                 :             :    *PXORP is to be FALSE if EXP might be a XOR used in a compare, in which
    7573                 :             :    case, if PXOR_CMP_OP is a zero constant, it will be overridden with *PEXP,
    7574                 :             :    *PXORP will be set to TRUE, *PXOR_AND_MASK will be copied from *PAND_MASK,
    7575                 :             :    and the left-hand operand of the XOR will be decoded.  If *PXORP is TRUE,
    7576                 :             :    PXOR_CMP_OP and PXOR_AND_MASK are supposed to be NULL, and then the
    7577                 :             :    right-hand operand of the XOR will be decoded.
    7578                 :             : 
    7579                 :             :    *LOAD is set to the load stmt of the innermost reference, if any,
    7580                 :             :    *and NULL otherwise.
    7581                 :             : 
    7582                 :             :    LOC[0..3] are filled in as conversion, masking, shifting and loading
    7583                 :             :    operations are located.
    7584                 :             : 
    7585                 :             :    Return 0 if this is not a component reference or is one that we can't
    7586                 :             :    do anything with.  */
    7587                 :             : 
    7588                 :             : static tree
    7589                 :      693966 : decode_field_reference (tree *pexp, HOST_WIDE_INT *pbitsize,
    7590                 :             :                         HOST_WIDE_INT *pbitpos,
    7591                 :             :                         bool *punsignedp, bool *preversep, bool *pvolatilep,
    7592                 :             :                         wide_int *pand_mask, bool *psignbit,
    7593                 :             :                         bool *pxorp, tree *pxor_cmp_op, wide_int *pxor_and_mask,
    7594                 :             :                         gimple **pload, location_t loc[4])
    7595                 :             : {
    7596                 :      693966 :   tree exp = *pexp;
    7597                 :      693966 :   tree outer_type = 0;
    7598                 :      693966 :   wide_int and_mask;
    7599                 :      693966 :   tree inner, offset;
    7600                 :      693966 :   int shiftrt = 0;
    7601                 :      693966 :   tree res_ops[2];
    7602                 :      693966 :   machine_mode mode;
    7603                 :      693966 :   bool convert_before_shift = false;
    7604                 :      693966 :   bool signbit = false;
    7605                 :      693966 :   bool xorp = false;
    7606                 :      693966 :   tree xor_cmp_op;
    7607                 :      693966 :   wide_int xor_and_mask;
    7608                 :      693966 :   gimple *load = NULL;
    7609                 :             : 
    7610                 :             :   /* All the optimizations using this function assume integer fields.
    7611                 :             :      There are problems with FP fields since the type_for_size call
    7612                 :             :      below can fail for, e.g., XFmode.  */
    7613                 :      693966 :   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
    7614                 :             :     return NULL_TREE;
    7615                 :             : 
    7616                 :             :   /* Drop casts, saving only the outermost type, effectively used in
    7617                 :             :      the compare.  We can deal with at most one conversion, and it may
    7618                 :             :      appear at various points in the chain of recognized preparation
    7619                 :             :      statements.  Earlier optimizers will often have already dropped
    7620                 :             :      unneeded extensions, but they may survive, as in PR118046.  ???
    7621                 :             :      Can we do better and allow multiple conversions, perhaps taking
    7622                 :             :      note of the narrowest intermediate type, sign extensions and
    7623                 :             :      whatnot?  */
    7624                 :      656928 :   if (!outer_type && gimple_convert_def_p (exp, res_ops))
    7625                 :             :     {
    7626                 :       12560 :       outer_type = TREE_TYPE (exp);
    7627                 :       12560 :       loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7628                 :       12560 :       exp = res_ops[0];
    7629                 :             :     }
    7630                 :             : 
    7631                 :             :   /* Recognize and save a masking operation.  Combine it with an
    7632                 :             :      incoming mask.  */
    7633                 :      656928 :   if (gimple_binop_def_p (BIT_AND_EXPR, exp, res_ops)
    7634                 :      656928 :       && TREE_CODE (res_ops[1]) == INTEGER_CST)
    7635                 :             :     {
    7636                 :       22936 :       loc[1] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7637                 :       22936 :       exp = res_ops[0];
    7638                 :       22936 :       and_mask = wi::to_wide (res_ops[1]);
    7639                 :       22936 :       unsigned prec_in = pand_mask->get_precision ();
    7640                 :       22936 :       if (prec_in)
    7641                 :             :         {
    7642                 :          52 :           unsigned prec_op = and_mask.get_precision ();
    7643                 :          52 :           if (prec_in >= prec_op)
    7644                 :             :             {
    7645                 :          52 :               if (prec_in > prec_op)
    7646                 :           0 :                 and_mask = wide_int::from (and_mask, prec_in, UNSIGNED);
    7647                 :          52 :               and_mask &= *pand_mask;
    7648                 :             :             }
    7649                 :             :           else
    7650                 :           0 :             and_mask &= wide_int::from (*pand_mask, prec_op, UNSIGNED);
    7651                 :             :         }
    7652                 :             :     }
    7653                 :             :   else
    7654                 :      633992 :     and_mask = *pand_mask;
    7655                 :             : 
    7656                 :             :   /* Turn (a ^ b) [!]= 0 into a [!]= b.  */
    7657                 :      656928 :   if (pxorp && gimple_binop_def_p (BIT_XOR_EXPR, exp, res_ops))
    7658                 :             :     {
    7659                 :             :       /* No location recorded for this one, it's entirely subsumed by the
    7660                 :             :          compare.  */
    7661                 :        7940 :       if (*pxorp)
    7662                 :             :         {
    7663                 :        3937 :           exp = res_ops[1];
    7664                 :        3937 :           gcc_checking_assert (!pxor_cmp_op && !pxor_and_mask);
    7665                 :             :         }
    7666                 :        4003 :       else if (!pxor_cmp_op)
    7667                 :             :         /* Not much we can do when xor appears in the right-hand compare
    7668                 :             :            operand.  */
    7669                 :             :         return NULL_TREE;
    7670                 :        3991 :       else if (integer_zerop (*pxor_cmp_op))
    7671                 :             :         {
    7672                 :        3937 :           xorp = true;
    7673                 :        3937 :           exp = res_ops[0];
    7674                 :        3937 :           xor_cmp_op = *pexp;
    7675                 :        3937 :           xor_and_mask = *pand_mask;
    7676                 :             :         }
    7677                 :             :     }
    7678                 :             : 
    7679                 :             :   /* Another chance to drop conversions.  */
    7680                 :      656916 :   if (!outer_type && gimple_convert_def_p (exp, res_ops))
    7681                 :             :     {
    7682                 :         248 :       outer_type = TREE_TYPE (exp);
    7683                 :         248 :       loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7684                 :         248 :       exp = res_ops[0];
    7685                 :             :     }
    7686                 :             : 
    7687                 :             :   /* Take note of shifts.  */
    7688                 :      656916 :   if (gimple_binop_def_p (RSHIFT_EXPR, exp, res_ops)
    7689                 :      656916 :       && TREE_CODE (res_ops[1]) == INTEGER_CST)
    7690                 :             :     {
    7691                 :         324 :       loc[2] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7692                 :         324 :       exp = res_ops[0];
    7693                 :         324 :       if (!tree_fits_shwi_p (res_ops[1]))
    7694                 :             :         return NULL_TREE;
    7695                 :         324 :       shiftrt = tree_to_shwi (res_ops[1]);
    7696                 :         324 :       if (shiftrt <= 0)
    7697                 :             :         return NULL_TREE;
    7698                 :             :     }
    7699                 :             : 
    7700                 :             :   /* Yet another chance to drop conversions.  This one is allowed to
    7701                 :             :      match a converting load, subsuming the load identification block
    7702                 :             :      below.  */
    7703                 :      656916 :   if (!outer_type && gimple_convert_def_p (exp, res_ops, &load))
    7704                 :             :     {
    7705                 :          12 :       outer_type = TREE_TYPE (exp);
    7706                 :          12 :       loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7707                 :          12 :       if (load)
    7708                 :           0 :         loc[3] = gimple_location (load);
    7709                 :          12 :       exp = res_ops[0];
    7710                 :             :       /* This looks backwards, but we're going back the def chain, so if we
    7711                 :             :          find the conversion here, after finding a shift, that's because the
    7712                 :             :          convert appears before the shift, and we should thus adjust the bit
    7713                 :             :          pos and size because of the shift after adjusting it due to type
    7714                 :             :          conversion.  */
    7715                 :          12 :       convert_before_shift = true;
    7716                 :             :     }
    7717                 :             : 
    7718                 :             :   /* Identify the load, if there is one.  */
    7719                 :      656916 :   if (!load && TREE_CODE (exp) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (exp))
    7720                 :             :     {
    7721                 :      375921 :       gimple *def = SSA_NAME_DEF_STMT (exp);
    7722                 :      375921 :       if (gimple_assign_load_p (def))
    7723                 :             :         {
    7724                 :      304478 :           loc[3] = gimple_location (def);
    7725                 :      304478 :           load = def;
    7726                 :      304478 :           exp = gimple_assign_rhs1 (def);
    7727                 :             :         }
    7728                 :             :     }
    7729                 :             : 
    7730                 :             :   /* Identify the relevant bits.  */
    7731                 :      656916 :   poly_int64 poly_bitsize, poly_bitpos;
    7732                 :      656916 :   int unsignedp, reversep = *preversep, volatilep = *pvolatilep;
    7733                 :      656916 :   inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
    7734                 :             :                                &mode, &unsignedp, &reversep, &volatilep);
    7735                 :             : 
    7736                 :      656916 :   HOST_WIDE_INT bs, bp;
    7737                 :      656916 :   if (!poly_bitsize.is_constant (&bs)
    7738                 :      656916 :       || !poly_bitpos.is_constant (&bp)
    7739                 :      656916 :       || bs <= shiftrt
    7740                 :      656916 :       || offset != 0
    7741                 :      655615 :       || TREE_CODE (inner) == PLACEHOLDER_EXPR
    7742                 :             :       /* Reject out-of-bound accesses (PR79731, PR118514).  */
    7743                 :      655615 :       || !access_in_bounds_of_type_p (TREE_TYPE (inner), bs, bp)
    7744                 :      655598 :       || (INTEGRAL_TYPE_P (TREE_TYPE (inner))
    7745                 :      377956 :           && !type_has_mode_precision_p (TREE_TYPE (inner))))
    7746                 :       33005 :     return NULL_TREE;
    7747                 :             : 
    7748                 :             :   /* Adjust shifts...  */
    7749                 :      623911 :   if (convert_before_shift
    7750                 :      623911 :       && outer_type && bs > TYPE_PRECISION (outer_type))
    7751                 :             :     {
    7752                 :           3 :       HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
    7753                 :           3 :       if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    7754                 :           0 :         bp += excess;
    7755                 :             :       bs -= excess;
    7756                 :             :     }
    7757                 :             : 
    7758                 :      623911 :   if (shiftrt)
    7759                 :             :     {
    7760                 :             :       /* Punt if we're shifting by more than the loaded bitfield (after
    7761                 :             :          adjustment), or if there's a shift after a change of signedness, punt.
    7762                 :             :          When comparing this field with a constant, we'll check that the
    7763                 :             :          constant is a proper sign- or zero-extension (depending on signedness)
    7764                 :             :          of a value that would fit in the selected portion of the bitfield.  A
    7765                 :             :          shift after a change of signedness would make the extension
    7766                 :             :          non-uniform, and we can't deal with that (yet ???).  See
    7767                 :             :          gcc.dg/field-merge-22.c for a test that would go wrong.  */
    7768                 :         318 :       if (bs <= shiftrt
    7769                 :         318 :           || (convert_before_shift
    7770                 :          12 :               && outer_type && unsignedp != TYPE_UNSIGNED (outer_type)))
    7771                 :             :         return NULL_TREE;
    7772                 :         308 :       if (!reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    7773                 :         308 :         bp += shiftrt;
    7774                 :         308 :       bs -= shiftrt;
    7775                 :             :     }
    7776                 :             : 
    7777                 :             :   /* ... and bit position.  */
    7778                 :      623901 :   if (!convert_before_shift
    7779                 :      623901 :       && outer_type && bs > TYPE_PRECISION (outer_type))
    7780                 :             :     {
    7781                 :        6211 :       HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
    7782                 :        6211 :       if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    7783                 :           0 :         bp += excess;
    7784                 :             :       bs -= excess;
    7785                 :             :     }
    7786                 :             : 
    7787                 :             :   /* If the number of bits in the reference is the same as the bitsize of
    7788                 :             :      the outer type, then the outer type gives the signedness. Otherwise
    7789                 :             :      (in case of a small bitfield) the signedness is unchanged.  */
    7790                 :      623901 :   if (outer_type && bs == TYPE_PRECISION (outer_type))
    7791                 :        8960 :     unsignedp = TYPE_UNSIGNED (outer_type);
    7792                 :             : 
    7793                 :             :   /* Make the mask the expected width.  */
    7794                 :      623901 :   if (and_mask.get_precision () != 0)
    7795                 :             :     {
    7796                 :             :       /* If the AND_MASK encompasses bits that would be extensions of
    7797                 :             :          the sign bit, set SIGNBIT.  */
    7798                 :       26556 :       if (!unsignedp
    7799                 :        2010 :           && and_mask.get_precision () > bs
    7800                 :       28608 :           && (and_mask & wi::mask (bs, true, and_mask.get_precision ())) != 0)
    7801                 :             :         signbit = true;
    7802                 :       26556 :       and_mask = wide_int::from (and_mask, bs, UNSIGNED);
    7803                 :             :     }
    7804                 :             : 
    7805                 :      623901 :   *pexp = exp;
    7806                 :      623901 :   *pload = load;
    7807                 :      623901 :   *pbitsize = bs;
    7808                 :      623901 :   *pbitpos = bp;
    7809                 :      623901 :   *punsignedp = unsignedp;
    7810                 :      623901 :   *preversep = reversep;
    7811                 :      623901 :   *pvolatilep = volatilep;
    7812                 :      623901 :   *psignbit = signbit;
    7813                 :      623901 :   *pand_mask = and_mask;
    7814                 :      623901 :   if (xorp)
    7815                 :             :     {
    7816                 :        3937 :       *pxorp = xorp;
    7817                 :        3937 :       *pxor_cmp_op = xor_cmp_op;
    7818                 :        3937 :       *pxor_and_mask = xor_and_mask;
    7819                 :             :     }
    7820                 :             : 
    7821                 :             :   return inner;
    7822                 :      693966 : }
    7823                 :             : 
    7824                 :             : /* Return the one bitpos within bit extents L or R that is at an
    7825                 :             :    ALIGN-bit alignment boundary, or -1 if there is more than one such
    7826                 :             :    boundary, if there isn't any, or if there is any such boundary
    7827                 :             :    between the extents.  L and R are given by bitpos and bitsize.  If
    7828                 :             :    it doesn't return -1, there are two consecutive ALIGN-bit words
    7829                 :             :    that contain both extents, and at least one of the extents
    7830                 :             :    straddles across the returned alignment boundary.  */
    7831                 :             : 
    7832                 :             : static inline HOST_WIDE_INT
    7833                 :       68207 : compute_split_boundary_from_align (HOST_WIDE_INT align,
    7834                 :             :                                    HOST_WIDE_INT l_bitpos,
    7835                 :             :                                    HOST_WIDE_INT l_bitsize,
    7836                 :             :                                    HOST_WIDE_INT r_bitpos,
    7837                 :             :                                    HOST_WIDE_INT r_bitsize)
    7838                 :             : {
    7839                 :       68207 :   HOST_WIDE_INT amask = ~(align - 1);
    7840                 :             : 
    7841                 :       68207 :   HOST_WIDE_INT first_bit = MIN (l_bitpos, r_bitpos);
    7842                 :       68207 :   HOST_WIDE_INT end_bit = MAX (l_bitpos + l_bitsize, r_bitpos + r_bitsize);
    7843                 :             : 
    7844                 :       68207 :   HOST_WIDE_INT boundary = (end_bit - 1) & amask;
    7845                 :             : 
    7846                 :             :   /* Make sure we're crossing no more than one alignment boundary.
    7847                 :             : 
    7848                 :             :      ??? We don't have logic to recombine loads of two adjacent
    7849                 :             :      fields that each crosses a different alignment boundary, so
    7850                 :             :      as to load the middle word only once, if other words can't be
    7851                 :             :      otherwise recombined.  */
    7852                 :       68207 :   if (boundary - first_bit > align)
    7853                 :             :     return -1;
    7854                 :             : 
    7855                 :       13865 :   HOST_WIDE_INT l_start_word = l_bitpos & amask;
    7856                 :       13865 :   HOST_WIDE_INT l_end_word = (l_bitpos + l_bitsize - 1) & amask;
    7857                 :             : 
    7858                 :       13865 :   HOST_WIDE_INT r_start_word = r_bitpos & amask;
    7859                 :       13865 :   HOST_WIDE_INT r_end_word = (r_bitpos + r_bitsize - 1) & amask;
    7860                 :             : 
    7861                 :             :   /* If neither field straddles across an alignment boundary, it's no
    7862                 :             :      use to even try to merge them.  */
    7863                 :       13865 :   if (l_start_word == l_end_word && r_start_word == r_end_word)
    7864                 :       13570 :     return -1;
    7865                 :             : 
    7866                 :             :   return boundary;
    7867                 :             : }
    7868                 :             : 
    7869                 :             : /* Make a bit_field_ref.  If POINT is NULL, return the BIT_FIELD_REF.
    7870                 :             :    Otherwise, build and insert a load stmt before POINT, and return
    7871                 :             :    the SSA_NAME.  ???  Rewrite LOAD in terms of the bitfield?  */
    7872                 :             : 
    7873                 :             : static tree
    7874                 :        4707 : make_bit_field_load (location_t loc, tree inner, tree orig_inner, tree type,
    7875                 :             :                      HOST_WIDE_INT bitsize, poly_int64 bitpos,
    7876                 :             :                      bool unsignedp, bool reversep, gimple *point)
    7877                 :             : {
    7878                 :        4707 :   if (point && loc == UNKNOWN_LOCATION)
    7879                 :          18 :     loc = gimple_location (point);
    7880                 :             : 
    7881                 :        4707 :   tree ref = make_bit_field_ref (loc, unshare_expr (inner),
    7882                 :             :                                  unshare_expr (orig_inner),
    7883                 :             :                                  type, bitsize, bitpos,
    7884                 :             :                                  unsignedp, reversep);
    7885                 :        4707 :   if (!point)
    7886                 :             :     return ref;
    7887                 :             : 
    7888                 :             :   /* If we're remaking the same load, reuse the SSA NAME it is already loaded
    7889                 :             :      into.  */
    7890                 :        4568 :   if (gimple_assign_load_p (point)
    7891                 :        4568 :       && operand_equal_p (ref, gimple_assign_rhs1 (point)))
    7892                 :             :     {
    7893                 :        1717 :       gcc_checking_assert (TREE_CODE (gimple_assign_lhs (point)) == SSA_NAME);
    7894                 :             :       return gimple_assign_lhs (point);
    7895                 :             :     }
    7896                 :             : 
    7897                 :        2851 :   gimple_seq stmts = NULL;
    7898                 :        2851 :   tree ret = force_gimple_operand (ref, &stmts, true, NULL_TREE);
    7899                 :             : 
    7900                 :             :   /* We know the vuse is supposed to end up being the same as that at the
    7901                 :             :      original load at the insertion point, but if we don't set it, it will be a
    7902                 :             :      generic placeholder that only the global SSA update at the end of the pass
    7903                 :             :      would make equal, too late for us to use in further combinations.  So go
    7904                 :             :      ahead and copy the vuse.  */
    7905                 :             : 
    7906                 :        2851 :   tree reaching_vuse = gimple_vuse (point);
    7907                 :        2851 :   for (gimple_stmt_iterator i = gsi_start (stmts);
    7908                 :        6088 :        !gsi_end_p (i); gsi_next (&i))
    7909                 :             :     {
    7910                 :        3237 :       gimple *new_stmt = gsi_stmt (i);
    7911                 :        6474 :       if (gimple_has_mem_ops (new_stmt))
    7912                 :        3237 :         gimple_set_vuse (new_stmt, reaching_vuse);
    7913                 :             :     }
    7914                 :             : 
    7915                 :        2851 :   gimple_stmt_iterator gsi = gsi_for_stmt (point);
    7916                 :        2851 :   gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
    7917                 :        2851 :   return ret;
    7918                 :             : }
    7919                 :             : 
    7920                 :             : /* Initialize ln_arg[0] and ln_arg[1] to a pair of newly-created (at
    7921                 :             :    LOC) loads from INNER (from ORIG_INNER), of modes MODE and MODE2,
    7922                 :             :    respectively, starting at BIT_POS, using reversed endianness if
    7923                 :             :    REVERSEP.  Also initialize BITPOS (the starting position of each
    7924                 :             :    part into INNER), BITSIZ (the bit count starting at BITPOS),
    7925                 :             :    TOSHIFT[1] (the amount by which the part and its mask are to be
    7926                 :             :    shifted right to bring its least-significant bit to bit zero) and
    7927                 :             :    SHIFTED (the amount by which the part, by separate loading, has
    7928                 :             :    already been shifted right, but that the mask needs shifting to
    7929                 :             :    match).  */
    7930                 :             : 
    7931                 :             : static inline void
    7932                 :         295 : build_split_load (tree /* out */ ln_arg[2],
    7933                 :             :                   HOST_WIDE_INT /* out */ bitpos[2],
    7934                 :             :                   HOST_WIDE_INT /* out */ bitsiz[2],
    7935                 :             :                   HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
    7936                 :             :                   HOST_WIDE_INT /* out */ shifted[2],
    7937                 :             :                   location_t loc, tree inner, tree orig_inner,
    7938                 :             :                   scalar_int_mode mode, scalar_int_mode mode2,
    7939                 :             :                   HOST_WIDE_INT bit_pos, bool reversep,
    7940                 :             :                   gimple *point[2])
    7941                 :             : {
    7942                 :         295 :   scalar_int_mode modes[2] = { mode, mode2 };
    7943                 :         295 :   bitsiz[0] = GET_MODE_BITSIZE (mode);
    7944                 :         295 :   bitsiz[1] = GET_MODE_BITSIZE (mode2);
    7945                 :             : 
    7946                 :         885 :   for (int i = 0; i < 2; i++)
    7947                 :             :     {
    7948                 :         590 :       tree type = lang_hooks.types.type_for_mode (modes[i], 1);
    7949                 :         590 :       if (!type)
    7950                 :             :         {
    7951                 :           0 :           type = build_nonstandard_integer_type (bitsiz[0], 1);
    7952                 :           0 :           gcc_assert (type);
    7953                 :             :         }
    7954                 :         590 :       bitpos[i] = bit_pos;
    7955                 :        1180 :       ln_arg[i] = make_bit_field_load (loc, inner, orig_inner,
    7956                 :         590 :                                        type, bitsiz[i],
    7957                 :         590 :                                        bit_pos, 1, reversep, point[i]);
    7958                 :         590 :       bit_pos += bitsiz[i];
    7959                 :             :     }
    7960                 :             : 
    7961                 :         295 :   toshift[1] = toshift[0];
    7962                 :         295 :   if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    7963                 :             :     {
    7964                 :           3 :       shifted[0] = bitsiz[1];
    7965                 :           3 :       shifted[1] = 0;
    7966                 :           3 :       toshift[0] = 0;
    7967                 :             :     }
    7968                 :             :   else
    7969                 :             :     {
    7970                 :         292 :       shifted[1] = bitsiz[0];
    7971                 :         292 :       shifted[0] = 0;
    7972                 :         292 :       toshift[1] = 0;
    7973                 :             :     }
    7974                 :         295 : }
    7975                 :             : 
    7976                 :             : /* Make arrangements to split at bit BOUNDARY a single loaded word
    7977                 :             :    (with REVERSEP bit order) LN_ARG[0], to be shifted right by
    7978                 :             :    TOSHIFT[0] to bring the field of interest to the least-significant
    7979                 :             :    bit.  The expectation is that the same loaded word will be
    7980                 :             :    propagated from part 0 to part 1, with just different shifting and
    7981                 :             :    masking to extract both parts.  MASK is not expected to do more
    7982                 :             :    than masking out the bits that belong to the other part.  See
    7983                 :             :    build_split_load for more information on the other fields.  */
    7984                 :             : 
    7985                 :             : static inline void
    7986                 :          51 : reuse_split_load (tree /* in[0] out[1] */ ln_arg[2],
    7987                 :             :                   HOST_WIDE_INT /* in[0] out[1] */ bitpos[2],
    7988                 :             :                   HOST_WIDE_INT /* in[0] out[1] */ bitsiz[2],
    7989                 :             :                   HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
    7990                 :             :                   HOST_WIDE_INT /* out */ shifted[2],
    7991                 :             :                   wide_int /* out */ mask[2],
    7992                 :             :                   HOST_WIDE_INT boundary, bool reversep)
    7993                 :             : {
    7994                 :          51 :   unsigned prec = TYPE_PRECISION (TREE_TYPE (ln_arg[0]));
    7995                 :             : 
    7996                 :          51 :   ln_arg[1] = ln_arg[0];
    7997                 :          51 :   bitpos[1] = bitpos[0];
    7998                 :          51 :   bitsiz[1] = bitsiz[0];
    7999                 :          51 :   shifted[1] = shifted[0] = 0;
    8000                 :             : 
    8001                 :          51 :   if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8002                 :             :     {
    8003                 :           3 :       toshift[1] = toshift[0];
    8004                 :           3 :       toshift[0] = bitpos[0] + bitsiz[0] - boundary;
    8005                 :           3 :       mask[0] = wi::mask (toshift[0], true, prec);
    8006                 :           3 :       mask[1] = wi::mask (toshift[0], false, prec);
    8007                 :             :     }
    8008                 :             :   else
    8009                 :             :     {
    8010                 :          48 :       toshift[1] = boundary - bitpos[1];
    8011                 :          48 :       mask[1] = wi::mask (toshift[1], true, prec);
    8012                 :          48 :       mask[0] = wi::mask (toshift[1], false, prec);
    8013                 :             :     }
    8014                 :          51 : }
    8015                 :             : 
    8016                 :             : /* Find ways of folding logical expressions of LHS and RHS:
    8017                 :             : 
    8018                 :             :    Try to merge two comparisons to nearby fields.
    8019                 :             : 
    8020                 :             :    For example, if we have p->a == 2 && p->b == 4 and we can load both A and B
    8021                 :             :    at once, we can do this with a comparison against the object ANDed with the
    8022                 :             :    a mask.
    8023                 :             : 
    8024                 :             :    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
    8025                 :             :    operations to do this with one comparison, loading both fields from P at
    8026                 :             :    once, and likewise from Q.
    8027                 :             : 
    8028                 :             :    Herein, loading at once means loading from within the same alignment
    8029                 :             :    boundary for the enclosing object.  If (packed) fields cross such alignment
    8030                 :             :    boundaries, we may still recombine the compares, so that loads do not cross
    8031                 :             :    the boundaries.
    8032                 :             : 
    8033                 :             :    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
    8034                 :             :    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
    8035                 :             : 
    8036                 :             :    TRUTH_TYPE is the type of the logical operand.
    8037                 :             : 
    8038                 :             :    LHS is denoted as LL_ARG LCODE LR_ARG.
    8039                 :             : 
    8040                 :             :    RHS is denoted as RL_ARG RCODE RR_ARG.
    8041                 :             : 
    8042                 :             :    LHS is assumed to dominate RHS.
    8043                 :             : 
    8044                 :             :    Combined loads are inserted next to preexisting loads, once we determine
    8045                 :             :    that the combination is viable, and the combined condition references new
    8046                 :             :    SSA_NAMEs that hold the loaded values.  Since the original loads are
    8047                 :             :    verified to have the same gimple_vuse, the insertion point doesn't matter
    8048                 :             :    for correctness.  ??? The loads may be a lot earlier than the compares, and
    8049                 :             :    it's conceivable that one or two loads for RHS appear before those for LHS.
    8050                 :             :    It could be advantageous to try to place the loads optimally, taking
    8051                 :             :    advantage of knowing whether RHS is accessed before LHS, or that both are
    8052                 :             :    accessed before both compares, but we don't do that (yet?).
    8053                 :             : 
    8054                 :             :    SEPARATEP should be NULL if the combined condition must be returned as a
    8055                 :             :    single expression, even if it is a compound condition.  This must only be
    8056                 :             :    done if LHS and RHS are adjacent, without intervening conditions, and the
    8057                 :             :    combined condition is to replace RHS, while LHS is dropped altogether.
    8058                 :             : 
    8059                 :             :    Otherwise, SEPARATEP must be a non-NULL pointer to a NULL_TREE, that may be
    8060                 :             :    replaced by a part of the compound condition that could replace RHS, while
    8061                 :             :    the returned expression replaces LHS.  This works whether or not LHS and RHS
    8062                 :             :    are adjacent, as long as there aren't VDEFs or other side effects between
    8063                 :             :    them.
    8064                 :             : 
    8065                 :             :    If the "words" accessed by RHS are already accessed by LHS, this won't
    8066                 :             :    matter, but if RHS accesses "words" that LHS doesn't, then *SEPARATEP will
    8067                 :             :    be set to the compares that should take RHS's place.  By "words" we mean
    8068                 :             :    contiguous bits that do not cross a an TYPE_ALIGN boundary of the accessed
    8069                 :             :    object's type.
    8070                 :             : 
    8071                 :             :    We return the simplified tree or 0 if no optimization is possible.  */
    8072                 :             : 
    8073                 :             : tree
    8074                 :      279162 : fold_truth_andor_for_ifcombine (enum tree_code code, tree truth_type,
    8075                 :             :                                 location_t lloc, enum tree_code lcode,
    8076                 :             :                                 tree ll_arg, tree lr_arg,
    8077                 :             :                                 location_t rloc, enum tree_code rcode,
    8078                 :             :                                 tree rl_arg, tree rr_arg,
    8079                 :             :                                 tree *separatep)
    8080                 :             : {
    8081                 :             :   /* If this is the "or" of two comparisons, we can do something if
    8082                 :             :      the comparisons are NE_EXPR.  If this is the "and", we can do something
    8083                 :             :      if the comparisons are EQ_EXPR.  I.e.,
    8084                 :             :         (a->b == 2 && a->c == 4) can become (a->new == NEW).
    8085                 :             : 
    8086                 :             :      WANTED_CODE is this operation code.  For single bit fields, we can
    8087                 :             :      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
    8088                 :             :      comparison for one-bit fields.  */
    8089                 :             : 
    8090                 :      279162 :   enum tree_code orig_code = code;
    8091                 :      279162 :   enum tree_code wanted_code;
    8092                 :      279162 :   tree ll_inner, lr_inner, rl_inner, rr_inner;
    8093                 :      279162 :   gimple *ll_load, *lr_load, *rl_load, *rr_load;
    8094                 :      279162 :   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
    8095                 :      279162 :   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
    8096                 :      279162 :   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
    8097                 :      279162 :   HOST_WIDE_INT lnbitsize, lnbitpos, lnprec;
    8098                 :      279162 :   HOST_WIDE_INT rnbitsize, rnbitpos, rnprec;
    8099                 :      279162 :   bool ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
    8100                 :      279162 :   bool ll_reversep, lr_reversep, rl_reversep, rr_reversep;
    8101                 :      279162 :   bool ll_signbit, lr_signbit, rl_signbit, rr_signbit;
    8102                 :      279162 :   scalar_int_mode lnmode, lnmode2, rnmode;
    8103                 :      279162 :   wide_int ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
    8104                 :      279162 :   wide_int l_const, r_const;
    8105                 :      279162 :   tree lntype, rntype, result;
    8106                 :      279162 :   HOST_WIDE_INT first_bit, end_bit;
    8107                 :      279162 :   bool volatilep;
    8108                 :      279162 :   bool l_split_load;
    8109                 :             : 
    8110                 :             :   /* These are indexed by: conv, mask, shft, load.  */
    8111                 :      279162 :   location_t ll_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
    8112                 :      279162 :   location_t lr_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
    8113                 :      279162 :   location_t rl_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
    8114                 :      279162 :   location_t rr_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
    8115                 :             : 
    8116                 :      279162 :   gcc_checking_assert (!separatep || !*separatep);
    8117                 :             : 
    8118                 :             :   /* Start by getting the comparison codes.  Fail if anything is volatile.
    8119                 :             :      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
    8120                 :             :      it were surrounded with a NE_EXPR.  */
    8121                 :             : 
    8122                 :      279162 :   if (TREE_CODE_CLASS (lcode) != tcc_comparison
    8123                 :      279162 :       || TREE_CODE_CLASS (rcode) != tcc_comparison)
    8124                 :             :     return 0;
    8125                 :             : 
    8126                 :             :   /* We don't normally find TRUTH_*IF_EXPR in gimple, but these codes may be
    8127                 :             :      given by our caller to denote conditions from different blocks.  */
    8128                 :      279162 :   switch (code)
    8129                 :             :     {
    8130                 :             :     case TRUTH_AND_EXPR:
    8131                 :             :     case TRUTH_ANDIF_EXPR:
    8132                 :             :       code = TRUTH_AND_EXPR;
    8133                 :             :       break;
    8134                 :             : 
    8135                 :           0 :     case TRUTH_OR_EXPR:
    8136                 :           0 :     case TRUTH_ORIF_EXPR:
    8137                 :           0 :       code = TRUTH_OR_EXPR;
    8138                 :           0 :       break;
    8139                 :             : 
    8140                 :             :     default:
    8141                 :             :       return 0;
    8142                 :             :     }
    8143                 :             : 
    8144                 :             :   /* Prepare to turn compares of signed quantities with zero into sign-bit
    8145                 :             :      tests.  We need not worry about *_reversep here for these compare
    8146                 :             :      rewrites: loads will have already been reversed before compares.  Save the
    8147                 :             :      precision, because [lr]l_arg may change and we won't be able to tell how
    8148                 :             :      wide it was originally.  */
    8149                 :      279162 :   unsigned lsignbit = 0, rsignbit = 0;
    8150                 :      279162 :   if ((lcode == LT_EXPR || lcode == GE_EXPR)
    8151                 :       12688 :       && integer_zerop (lr_arg)
    8152                 :        3658 :       && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
    8153                 :      282820 :       && !TYPE_UNSIGNED (TREE_TYPE (ll_arg)))
    8154                 :             :     {
    8155                 :        3658 :       lsignbit = TYPE_PRECISION (TREE_TYPE (ll_arg));
    8156                 :        3658 :       lcode = (lcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
    8157                 :             :     }
    8158                 :             :   /* Turn compares of unsigned quantities with powers of two into
    8159                 :             :      equality tests of masks.  */
    8160                 :      275504 :   else if ((lcode == LT_EXPR || lcode == GE_EXPR)
    8161                 :        9030 :            && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
    8162                 :        6702 :            && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
    8163                 :        4774 :            && TREE_CODE (lr_arg) == INTEGER_CST
    8164                 :      275504 :            && wi::popcount (wi::to_wide (lr_arg)) == 1)
    8165                 :             :     {
    8166                 :           0 :       ll_and_mask = ~(wi::to_wide (lr_arg) - 1);
    8167                 :           0 :       lcode = (lcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
    8168                 :           0 :       lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
    8169                 :             :     }
    8170                 :             :   /* Turn compares of unsigned quantities with powers of two minus one
    8171                 :             :      into equality tests of masks.  */
    8172                 :      551008 :   else if ((lcode == LE_EXPR || lcode == GT_EXPR)
    8173                 :       27958 :            && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
    8174                 :       27748 :            && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
    8175                 :       22176 :            && TREE_CODE (lr_arg) == INTEGER_CST
    8176                 :      578966 :            && wi::popcount (wi::to_wide (lr_arg) + 1) == 1)
    8177                 :             :     {
    8178                 :        3794 :       ll_and_mask = ~wi::to_wide (lr_arg);
    8179                 :        3794 :       lcode = (lcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
    8180                 :        3794 :       lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
    8181                 :             :     }
    8182                 :             :   /* Likewise for the second compare.  */
    8183                 :      279162 :   if ((rcode == LT_EXPR || rcode == GE_EXPR)
    8184                 :       21357 :       && integer_zerop (rr_arg)
    8185                 :        1725 :       && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
    8186                 :      280887 :       && !TYPE_UNSIGNED (TREE_TYPE (rl_arg)))
    8187                 :             :     {
    8188                 :        1725 :       rsignbit = TYPE_PRECISION (TREE_TYPE (rl_arg));
    8189                 :        1725 :       rcode = (rcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
    8190                 :             :     }
    8191                 :      277437 :   else if ((rcode == LT_EXPR || rcode == GE_EXPR)
    8192                 :       19632 :            && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
    8193                 :       17124 :            && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
    8194                 :        3439 :            && TREE_CODE (rr_arg) == INTEGER_CST
    8195                 :      277437 :            && wi::popcount (wi::to_wide (rr_arg)) == 1)
    8196                 :             :     {
    8197                 :           0 :       rl_and_mask = ~(wi::to_wide (rr_arg) - 1);
    8198                 :           0 :       rcode = (rcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
    8199                 :           0 :       rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
    8200                 :             :     }
    8201                 :      554874 :   else if ((rcode == LE_EXPR || rcode == GT_EXPR)
    8202                 :       36858 :            && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
    8203                 :       36367 :            && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
    8204                 :       25639 :            && TREE_CODE (rr_arg) == INTEGER_CST
    8205                 :      591732 :            && wi::popcount (wi::to_wide (rr_arg) + 1) == 1)
    8206                 :             :     {
    8207                 :        3545 :       rl_and_mask = ~wi::to_wide (rr_arg);
    8208                 :        3545 :       rcode = (rcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
    8209                 :        3545 :       rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
    8210                 :             :     }
    8211                 :             : 
    8212                 :             :   /* See if the comparisons can be merged.  Then get all the parameters for
    8213                 :             :      each side.  */
    8214                 :             : 
    8215                 :      279162 :   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
    8216                 :      245206 :       || (rcode != EQ_EXPR && rcode != NE_EXPR))
    8217                 :             :     return 0;
    8218                 :             : 
    8219                 :      219551 :   ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
    8220                 :      219551 :   volatilep = 0;
    8221                 :      219551 :   bool l_xor = false, r_xor = false;
    8222                 :      219551 :   ll_inner = decode_field_reference (&ll_arg, &ll_bitsize, &ll_bitpos,
    8223                 :             :                                      &ll_unsignedp, &ll_reversep, &volatilep,
    8224                 :             :                                      &ll_and_mask, &ll_signbit,
    8225                 :             :                                      &l_xor, &lr_arg, &lr_and_mask,
    8226                 :             :                                      &ll_load, ll_loc);
    8227                 :      219551 :   if (!ll_inner)
    8228                 :             :     return 0;
    8229                 :      163507 :   lr_inner = decode_field_reference (&lr_arg, &lr_bitsize, &lr_bitpos,
    8230                 :             :                                      &lr_unsignedp, &lr_reversep, &volatilep,
    8231                 :             :                                      &lr_and_mask, &lr_signbit, &l_xor, 0, 0,
    8232                 :             :                                      &lr_load, lr_loc);
    8233                 :      163507 :   if (!lr_inner)
    8234                 :             :     return 0;
    8235                 :      160712 :   rl_inner = decode_field_reference (&rl_arg, &rl_bitsize, &rl_bitpos,
    8236                 :             :                                      &rl_unsignedp, &rl_reversep, &volatilep,
    8237                 :             :                                      &rl_and_mask, &rl_signbit,
    8238                 :             :                                      &r_xor, &rr_arg, &rr_and_mask,
    8239                 :             :                                      &rl_load, rl_loc);
    8240                 :      160712 :   if (!rl_inner)
    8241                 :             :     return 0;
    8242                 :      150196 :   rr_inner = decode_field_reference (&rr_arg, &rr_bitsize, &rr_bitpos,
    8243                 :             :                                      &rr_unsignedp, &rr_reversep, &volatilep,
    8244                 :             :                                      &rr_and_mask, &rr_signbit, &r_xor, 0, 0,
    8245                 :             :                                      &rr_load, rr_loc);
    8246                 :      150196 :   if (!rr_inner)
    8247                 :             :     return 0;
    8248                 :             : 
    8249                 :             :   /* It must be true that the inner operation on the lhs of each
    8250                 :             :      comparison must be the same if we are to be able to do anything.
    8251                 :             :      Then see if we have constants.  If not, the same must be true for
    8252                 :             :      the rhs's.  If one is a load and the other isn't, we have to be
    8253                 :             :      conservative and avoid the optimization, otherwise we could get
    8254                 :             :      SRAed fields wrong.  */
    8255                 :      149486 :   if (volatilep)
    8256                 :             :     return 0;
    8257                 :             : 
    8258                 :      149486 :   if (ll_reversep != rl_reversep
    8259                 :      149486 :       || ! operand_equal_p (ll_inner, rl_inner, 0))
    8260                 :             :     {
    8261                 :             :       /* Try swapping the operands.  */
    8262                 :       74071 :       if (ll_reversep != rr_reversep || rsignbit
    8263                 :      147799 :           || !operand_equal_p (ll_inner, rr_inner, 0))
    8264                 :       73038 :         return 0;
    8265                 :             : 
    8266                 :        1060 :       rcode = swap_tree_comparison (rcode);
    8267                 :        1060 :       std::swap (rl_arg, rr_arg);
    8268                 :        1060 :       std::swap (rl_inner, rr_inner);
    8269                 :        1060 :       std::swap (rl_bitsize, rr_bitsize);
    8270                 :        1060 :       std::swap (rl_bitpos, rr_bitpos);
    8271                 :        1060 :       std::swap (rl_unsignedp, rr_unsignedp);
    8272                 :        1060 :       std::swap (rl_reversep, rr_reversep);
    8273                 :        1060 :       std::swap (rl_and_mask, rr_and_mask);
    8274                 :        1060 :       std::swap (rl_signbit, rr_signbit);
    8275                 :        1060 :       std::swap (rl_load, rr_load);
    8276                 :        1060 :       std::swap (rl_loc, rr_loc);
    8277                 :             :     }
    8278                 :             : 
    8279                 :      150883 :   if ((ll_load && rl_load)
    8280                 :      299753 :       ? gimple_vuse (ll_load) != gimple_vuse (rl_load)
    8281                 :        2013 :       : (!ll_load != !rl_load))
    8282                 :             :     return 0;
    8283                 :             : 
    8284                 :             :   /* ??? Can we do anything with these?  */
    8285                 :       76096 :   if (lr_signbit || rr_signbit)
    8286                 :             :     return 0;
    8287                 :             : 
    8288                 :             :   /* If the mask encompassed extensions of the sign bit before
    8289                 :             :      clipping, try to include the sign bit in the test.  If we're not
    8290                 :             :      comparing with zero, don't even try to deal with it (for now?).
    8291                 :             :      If we've already commited to a sign test, the extended (before
    8292                 :             :      clipping) mask could already be messing with it.  */
    8293                 :       76096 :   if (ll_signbit)
    8294                 :             :     {
    8295                 :           4 :       if (!integer_zerop (lr_arg) || lsignbit)
    8296                 :           0 :         return 0;
    8297                 :           4 :       wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
    8298                 :           4 :       if (!ll_and_mask.get_precision ())
    8299                 :           0 :         ll_and_mask = sign;
    8300                 :             :       else
    8301                 :           4 :         ll_and_mask |= sign;
    8302                 :           4 :     }
    8303                 :             : 
    8304                 :       76096 :   if (rl_signbit)
    8305                 :             :     {
    8306                 :           4 :       if (!integer_zerop (rr_arg) || rsignbit)
    8307                 :           1 :         return 0;
    8308                 :           3 :       wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
    8309                 :           3 :       if (!rl_and_mask.get_precision ())
    8310                 :           0 :         rl_and_mask = sign;
    8311                 :             :       else
    8312                 :           3 :         rl_and_mask |= sign;
    8313                 :           3 :     }
    8314                 :             : 
    8315                 :       76095 :   if (TREE_CODE (lr_arg) == INTEGER_CST
    8316                 :       64937 :       && TREE_CODE (rr_arg) == INTEGER_CST)
    8317                 :             :     {
    8318                 :       64524 :       l_const = wi::to_wide (lr_arg);
    8319                 :             :       /* We don't expect masks on constants, but if there are any, apply
    8320                 :             :          them now.  */
    8321                 :       64524 :       if (lr_and_mask.get_precision ())
    8322                 :           0 :         l_const &= wide_int::from (lr_and_mask,
    8323                 :           0 :                                    l_const.get_precision (), UNSIGNED);
    8324                 :       64524 :       r_const = wi::to_wide (rr_arg);
    8325                 :       64524 :       if (rr_and_mask.get_precision ())
    8326                 :           0 :         r_const &= wide_int::from (rr_and_mask,
    8327                 :           0 :                                    r_const.get_precision (), UNSIGNED);
    8328                 :       64524 :       lr_reversep = ll_reversep;
    8329                 :             :     }
    8330                 :       11571 :   else if (lr_reversep != rr_reversep
    8331                 :       11571 :            || ! operand_equal_p (lr_inner, rr_inner, 0)
    8332                 :       21969 :            || ((lr_load && rr_load)
    8333                 :       31113 :                ? gimple_vuse (lr_load) != gimple_vuse (rr_load)
    8334                 :          27 :                : (!lr_load != !rr_load)))
    8335                 :        1203 :     return 0;
    8336                 :             : 
    8337                 :             :   /* If we found sign tests, finish turning them into bit tests.  */
    8338                 :             : 
    8339                 :       74892 :   if (lsignbit)
    8340                 :             :     {
    8341                 :          59 :       wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
    8342                 :             :       /* If ll_arg is zero-extended and we're testing the sign bit, we know
    8343                 :             :          what the result should be.  Shifting the sign bit out of sign will get
    8344                 :             :          us to mask the entire field out, yielding zero, i.e., the sign bit of
    8345                 :             :          the zero-extended value.  We know the masked value is being compared
    8346                 :             :          with zero, so the compare will get us the result we're looking
    8347                 :             :          for: TRUE if EQ_EXPR, FALSE if NE_EXPR.  */
    8348                 :          59 :       if (lsignbit > ll_bitsize && ll_unsignedp)
    8349                 :           1 :         sign <<= 1;
    8350                 :          59 :       if (!ll_and_mask.get_precision ())
    8351                 :          58 :         ll_and_mask = sign;
    8352                 :             :       else
    8353                 :           1 :         ll_and_mask &= sign;
    8354                 :          59 :       if (l_xor)
    8355                 :             :         {
    8356                 :           1 :           if (ll_bitsize != lr_bitsize)
    8357                 :           1 :             return 0;
    8358                 :           0 :           if (!lr_and_mask.get_precision ())
    8359                 :           0 :             lr_and_mask = sign;
    8360                 :             :           else
    8361                 :           0 :             lr_and_mask &= sign;
    8362                 :           0 :           if (l_const.get_precision ())
    8363                 :           0 :             l_const &= wide_int::from (lr_and_mask,
    8364                 :           0 :                                        l_const.get_precision (), UNSIGNED);
    8365                 :             :         }
    8366                 :          59 :     }
    8367                 :             : 
    8368                 :       74891 :   if (rsignbit)
    8369                 :             :     {
    8370                 :         186 :       wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
    8371                 :         186 :       if (rsignbit > rl_bitsize && rl_unsignedp)
    8372                 :           0 :         sign <<= 1;
    8373                 :         186 :       if (!rl_and_mask.get_precision ())
    8374                 :         186 :         rl_and_mask = sign;
    8375                 :             :       else
    8376                 :           0 :         rl_and_mask &= sign;
    8377                 :         186 :       if (r_xor)
    8378                 :             :         {
    8379                 :           4 :           if (rl_bitsize != rr_bitsize)
    8380                 :           0 :             return 0;
    8381                 :           4 :           if (!rr_and_mask.get_precision ())
    8382                 :           4 :             rr_and_mask = sign;
    8383                 :             :           else
    8384                 :           0 :             rr_and_mask &= sign;
    8385                 :           4 :           if (r_const.get_precision ())
    8386                 :           0 :             r_const &= wide_int::from (rr_and_mask,
    8387                 :           0 :                                        r_const.get_precision (), UNSIGNED);
    8388                 :             :         }
    8389                 :         186 :     }
    8390                 :             : 
    8391                 :             :   /* If either comparison code is not correct for our logical operation,
    8392                 :             :      fail.  However, we can convert a one-bit comparison against zero into
    8393                 :             :      the opposite comparison against that bit being set in the field.  */
    8394                 :             : 
    8395                 :       74891 :   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
    8396                 :       74891 :   if (lcode != wanted_code)
    8397                 :             :     {
    8398                 :        4518 :       if (l_const.get_precision ()
    8399                 :        4400 :           && l_const == 0
    8400                 :        1182 :           && ll_and_mask.get_precision ()
    8401                 :        4795 :           && wi::popcount (ll_and_mask) == 1)
    8402                 :             :         {
    8403                 :             :           /* Make the left operand unsigned, since we are only interested
    8404                 :             :              in the value of one bit.  Otherwise we are doing the wrong
    8405                 :             :              thing below.  */
    8406                 :         152 :           ll_unsignedp = 1;
    8407                 :         152 :           l_const = ll_and_mask;
    8408                 :             :         }
    8409                 :             :       else
    8410                 :        4366 :         return 0;
    8411                 :             :     }
    8412                 :             : 
    8413                 :             :   /* This is analogous to the code for l_const above.  */
    8414                 :       70525 :   if (rcode != wanted_code)
    8415                 :             :     {
    8416                 :         703 :       if (r_const.get_precision ()
    8417                 :         703 :           && r_const == 0
    8418                 :         676 :           && rl_and_mask.get_precision ()
    8419                 :        1317 :           && wi::popcount (rl_and_mask) == 1)
    8420                 :             :         {
    8421                 :         447 :           rl_unsignedp = 1;
    8422                 :         447 :           r_const = rl_and_mask;
    8423                 :             :         }
    8424                 :             :       else
    8425                 :         256 :         return 0;
    8426                 :             :     }
    8427                 :             : 
    8428                 :             :   /* This will be bumped to 2 if any of the field pairs crosses an
    8429                 :             :      alignment boundary, so the merged compare has to be done in two
    8430                 :             :      parts.  */
    8431                 :      210807 :   int parts = 1;
    8432                 :             :   /* Set to true if the second combined compare should come first,
    8433                 :             :      e.g., because the second original compare accesses a word that
    8434                 :             :      the first one doesn't, and the combined compares access those in
    8435                 :             :      cmp[0].  */
    8436                 :      210807 :   bool first1 = false;
    8437                 :             :   /* Set to true if the first original compare is not the one being
    8438                 :             :      split.  */
    8439                 :      210807 :   bool maybe_separate = false;
    8440                 :             : 
    8441                 :             :   /* The following 2-dimensional arrays use the first index to
    8442                 :             :      identify left(0)- vs right(1)-hand compare operands, and the
    8443                 :             :      second one to identify merged compare parts.  */
    8444                 :             :   /* The memory loads or constants to be compared.  */
    8445                 :             :   tree ld_arg[2][2];
    8446                 :             :   /* The first bit of the corresponding inner object that the
    8447                 :             :      corresponding LD_ARG covers.  */
    8448                 :             :   HOST_WIDE_INT bitpos[2][2];
    8449                 :             :   /* The bit count starting at BITPOS that the corresponding LD_ARG
    8450                 :             :      covers.  */
    8451                 :             :   HOST_WIDE_INT bitsiz[2][2];
    8452                 :             :   /* The number of bits by which LD_ARG has already been shifted
    8453                 :             :      right, WRT mask.  */
    8454                 :             :   HOST_WIDE_INT shifted[2][2];
    8455                 :             :   /* The number of bits by which both LD_ARG and MASK need shifting to
    8456                 :             :      bring its least-significant bit to bit zero.  */
    8457                 :             :   HOST_WIDE_INT toshift[2][2];
    8458                 :             :   /* An additional mask to be applied to LD_ARG, to remove any bits
    8459                 :             :      that may have been loaded for use in another compare, but that
    8460                 :             :      don't belong in the corresponding compare.  */
    8461                 :      843228 :   wide_int xmask[2][2] = {};
    8462                 :             : 
    8463                 :             :   /* The combined compare or compares.  */
    8464                 :       70269 :   tree cmp[2];
    8465                 :             : 
    8466                 :             :   /* Consider we're comparing two non-contiguous fields of packed
    8467                 :             :      structs, both aligned at 32-bit boundaries:
    8468                 :             : 
    8469                 :             :      ll_arg: an 8-bit field at offset 0
    8470                 :             :      lr_arg: a 16-bit field at offset 2
    8471                 :             : 
    8472                 :             :      rl_arg: an 8-bit field at offset 1
    8473                 :             :      rr_arg: a 16-bit field at offset 3
    8474                 :             : 
    8475                 :             :      We'll have r_split_load, because rr_arg straddles across an
    8476                 :             :      alignment boundary.
    8477                 :             : 
    8478                 :             :      We'll want to have:
    8479                 :             : 
    8480                 :             :      bitpos  = { {  0,  0 }, {  0, 32 } }
    8481                 :             :      bitsiz  = { { 32, 32 }, { 32,  8 } }
    8482                 :             : 
    8483                 :             :      And, for little-endian:
    8484                 :             : 
    8485                 :             :      shifted = { {  0,  0 }, {  0, 32 } }
    8486                 :             :      toshift = { {  0, 24 }, {  0,  0 } }
    8487                 :             : 
    8488                 :             :      Or, for big-endian:
    8489                 :             : 
    8490                 :             :      shifted = { {  0,  0 }, {  8,  0 } }
    8491                 :             :      toshift = { {  8,  0 }, {  0,  0 } }
    8492                 :             :   */
    8493                 :             : 
    8494                 :             :   /* See if we can find a mode that contains both fields being compared on
    8495                 :             :      the left.  If we can't, fail.  Otherwise, update all constants and masks
    8496                 :             :      to be relative to a field of that size.  */
    8497                 :       70269 :   first_bit = MIN (ll_bitpos, rl_bitpos);
    8498                 :       70269 :   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
    8499                 :       70269 :   HOST_WIDE_INT ll_align = TYPE_ALIGN (TREE_TYPE (ll_inner));
    8500                 :       70269 :   poly_uint64 ll_end_region = 0;
    8501                 :       70269 :   if (TYPE_SIZE (TREE_TYPE (ll_inner))
    8502                 :       70269 :       && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (ll_inner))))
    8503                 :       70269 :     ll_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (ll_inner)));
    8504                 :       70269 :   if (get_best_mode (end_bit - first_bit, first_bit, 0, ll_end_region,
    8505                 :       70269 :                      ll_align, BITS_PER_WORD, volatilep, &lnmode))
    8506                 :             :     l_split_load = false;
    8507                 :             :   /* ??? If ll and rl share the same load, reuse that?
    8508                 :             :      See PR 118206 -> gcc.dg/field-merge-18.c  */
    8509                 :             :   else
    8510                 :             :     {
    8511                 :             :       /* Consider the possibility of recombining loads if any of the
    8512                 :             :          fields straddles across an alignment boundary, so that either
    8513                 :             :          part can be loaded along with the other field.  Since we
    8514                 :             :          limit access modes to BITS_PER_WORD, don't exceed that,
    8515                 :             :          otherwise on a 32-bit host and a 64-bit-aligned data
    8516                 :             :          structure, we'll fail the above for a field that straddles
    8517                 :             :          across two words, and would fail here for not even trying to
    8518                 :             :          split it at between 32-bit words.  */
    8519                 :       66862 :       HOST_WIDE_INT boundary = compute_split_boundary_from_align
    8520                 :       66862 :         (MIN (ll_align, BITS_PER_WORD),
    8521                 :             :          ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
    8522                 :             : 
    8523                 :       66862 :       if (boundary < 0
    8524                 :         210 :           || !get_best_mode (boundary - first_bit, first_bit, 0, ll_end_region,
    8525                 :             :                              ll_align, BITS_PER_WORD, volatilep, &lnmode)
    8526                 :       67033 :           || !get_best_mode (end_bit - boundary, boundary, 0, ll_end_region,
    8527                 :         171 :                              ll_align, BITS_PER_WORD, volatilep, &lnmode2))
    8528                 :             :         {
    8529                 :       68176 :           if (ll_align <= BITS_PER_WORD)
    8530                 :             :             return 0;
    8531                 :             : 
    8532                 :             :           /* As a last resort, try double-word access modes.  This
    8533                 :             :              enables us to deal with misaligned double-word fields
    8534                 :             :              that straddle across 3 separate words.  */
    8535                 :        1219 :           boundary = compute_split_boundary_from_align
    8536                 :        1219 :             (MIN (ll_align, 2 * BITS_PER_WORD),
    8537                 :             :              ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
    8538                 :        1219 :           if (boundary < 0
    8539                 :           0 :               || !get_best_mode (boundary - first_bit, first_bit,
    8540                 :           0 :                                  0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
    8541                 :             :                                  volatilep, &lnmode)
    8542                 :        1219 :               || !get_best_mode (end_bit - boundary, boundary,
    8543                 :           0 :                                  0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
    8544                 :             :                                  volatilep, &lnmode2))
    8545                 :        1219 :             return 0;
    8546                 :             :         }
    8547                 :             : 
    8548                 :             :       /* If we can't have a single load, but can with two, figure out whether
    8549                 :             :          the two compares can be separated, i.e., whether the entirety of the
    8550                 :             :          first original compare is encompassed by the entirety of the first
    8551                 :             :          combined compare.  If the first original compare is past the alignment
    8552                 :             :          boundary, arrange to compare that range first, by setting first1
    8553                 :             :          (meaning make cmp[1] first, instead of cmp[0]).  */
    8554                 :         171 :       l_split_load = true;
    8555                 :         171 :       parts = 2;
    8556                 :         171 :       if (ll_bitpos >= boundary)
    8557                 :             :         maybe_separate = first1 = true;
    8558                 :         126 :       else if (ll_bitpos + ll_bitsize <= boundary)
    8559                 :          26 :         maybe_separate = true;
    8560                 :             :     }
    8561                 :             : 
    8562                 :        3578 :   lnbitsize = GET_MODE_BITSIZE (lnmode);
    8563                 :        3578 :   lnbitpos = first_bit & ~ (lnbitsize - 1);
    8564                 :             :   /* Avoid situations that the code below can't handle.  */
    8565                 :        3578 :   if (lnbitpos < 0)
    8566                 :             :     return 0;
    8567                 :             : 
    8568                 :             :   /* Choose the type for the combined compare.  Even if we're splitting loads,
    8569                 :             :      make it wide enough to hold both.  */
    8570                 :        3578 :   if (l_split_load)
    8571                 :         342 :     lnbitsize += GET_MODE_BITSIZE (lnmode2);
    8572                 :        3578 :   lntype = build_nonstandard_integer_type (lnbitsize, 1);
    8573                 :        3578 :   if (!lntype)
    8574                 :             :     return NULL_TREE;
    8575                 :        3578 :   lnprec = TYPE_PRECISION (lntype);
    8576                 :        3578 :   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
    8577                 :             : 
    8578                 :             :   /* Adjust bit ranges for reverse endianness.  */
    8579                 :        3578 :   if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8580                 :             :     {
    8581                 :           6 :       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
    8582                 :           6 :       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
    8583                 :             :     }
    8584                 :             : 
    8585                 :             :   /* Adjust masks to match the positions in the combined lntype.  */
    8586                 :        7156 :   wide_int ll_mask, rl_mask, r_mask;
    8587                 :        3578 :   if (ll_and_mask.get_precision ())
    8588                 :        4826 :     ll_mask = wi::lshift (wide_int::from (ll_and_mask, lnprec, UNSIGNED),
    8589                 :        2413 :                           xll_bitpos);
    8590                 :             :   else
    8591                 :        1165 :     ll_mask = wi::shifted_mask (xll_bitpos, ll_bitsize, false, lnprec);
    8592                 :        3578 :   if (rl_and_mask.get_precision ())
    8593                 :        4576 :     rl_mask = wi::lshift (wide_int::from (rl_and_mask, lnprec, UNSIGNED),
    8594                 :        2288 :                           xrl_bitpos);
    8595                 :             :   else
    8596                 :        1290 :     rl_mask = wi::shifted_mask (xrl_bitpos, rl_bitsize, false, lnprec);
    8597                 :             : 
    8598                 :             :   /* When we set l_const, we also set r_const.  */
    8599                 :        3578 :   gcc_checking_assert (!l_const.get_precision () == !r_const.get_precision ());
    8600                 :             : 
    8601                 :             :   /* Adjust right-hand constants in both original comparisons to match width
    8602                 :             :      and bit position.  */
    8603                 :        3578 :   if (l_const.get_precision ())
    8604                 :             :     {
    8605                 :             :       /* Before clipping upper bits of the right-hand operand of the compare,
    8606                 :             :          check that they're sign or zero extensions, depending on how the
    8607                 :             :          left-hand operand would be extended.  If it is unsigned, or if there's
    8608                 :             :          a mask that zeroes out extension bits, whether because we've checked
    8609                 :             :          for upper bits in the mask and did not set ll_signbit, or because the
    8610                 :             :          sign bit itself is masked out, check that the right-hand operand is
    8611                 :             :          zero-extended.  */
    8612                 :        2154 :       bool l_non_ext_bits = false;
    8613                 :        2154 :       if (ll_bitsize < lr_bitsize)
    8614                 :             :         {
    8615                 :          34 :           wide_int zext = wi::zext (l_const, ll_bitsize);
    8616                 :          68 :           if ((ll_unsignedp
    8617                 :          26 :                || (ll_and_mask.get_precision ()
    8618                 :           4 :                    && (!ll_signbit
    8619                 :          42 :                        || ((ll_and_mask & wi::mask (ll_bitsize - 1, true, ll_bitsize))
    8620                 :           8 :                            == 0)))
    8621                 :         128 :                ? zext : wi::sext (l_const, ll_bitsize)) == l_const)
    8622                 :          34 :             l_const = zext;
    8623                 :             :           else
    8624                 :             :             l_non_ext_bits = true;
    8625                 :          34 :         }
    8626                 :             :       /* We're doing bitwise equality tests, so don't bother with sign
    8627                 :             :          extensions.  */
    8628                 :        2154 :       l_const = wide_int::from (l_const, lnprec, UNSIGNED);
    8629                 :        2154 :       if (ll_and_mask.get_precision ())
    8630                 :        1421 :         l_const &= wide_int::from (ll_and_mask, lnprec, UNSIGNED);
    8631                 :        2154 :       l_const <<= xll_bitpos;
    8632                 :        6462 :       if (l_non_ext_bits || (l_const & ~ll_mask) != 0)
    8633                 :             :         {
    8634                 :           0 :           warning_at (lloc, OPT_Wtautological_compare,
    8635                 :             :                       "comparison is always %d", wanted_code == NE_EXPR);
    8636                 :             : 
    8637                 :           0 :           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
    8638                 :             :         }
    8639                 :             : 
    8640                 :             :       /* Before clipping upper bits of the right-hand operand of the compare,
    8641                 :             :          check that they're sign or zero extensions, depending on how the
    8642                 :             :          left-hand operand would be extended.  */
    8643                 :        2154 :       bool r_non_ext_bits = false;
    8644                 :        2154 :       if (rl_bitsize < rr_bitsize)
    8645                 :             :         {
    8646                 :          34 :           wide_int zext = wi::zext (r_const, rl_bitsize);
    8647                 :          68 :           if ((rl_unsignedp
    8648                 :          17 :                || (rl_and_mask.get_precision ()
    8649                 :          10 :                    && (!rl_signbit
    8650                 :          40 :                        || ((rl_and_mask & wi::mask (rl_bitsize - 1, true, rl_bitsize))
    8651                 :           6 :                            == 0)))
    8652                 :         119 :                ? zext : wi::sext (r_const, rl_bitsize)) == r_const)
    8653                 :          34 :             r_const = zext;
    8654                 :             :           else
    8655                 :             :             r_non_ext_bits = true;
    8656                 :          34 :         }
    8657                 :        2154 :       r_const = wide_int::from (r_const, lnprec, UNSIGNED);
    8658                 :        2154 :       if (rl_and_mask.get_precision ())
    8659                 :        1341 :         r_const &= wide_int::from (rl_and_mask, lnprec, UNSIGNED);
    8660                 :        2154 :       r_const <<= xrl_bitpos;
    8661                 :        6462 :       if (r_non_ext_bits || (r_const & ~rl_mask) != 0)
    8662                 :             :         {
    8663                 :           0 :           warning_at (rloc, OPT_Wtautological_compare,
    8664                 :             :                       "comparison is always %d", wanted_code == NE_EXPR);
    8665                 :             : 
    8666                 :           0 :           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
    8667                 :             :         }
    8668                 :             : 
    8669                 :             :       /* If there is something in common between the masks, those bits of the
    8670                 :             :          constants must be the same.  If not, the combined condition cannot be
    8671                 :             :          met, and the result is known.  Test for this to avoid generating
    8672                 :             :          incorrect code below.  */
    8673                 :        2154 :       wide_int mask = ll_mask & rl_mask;
    8674                 :        2154 :       if (mask != 0
    8675                 :        2214 :           && (l_const & mask) != (r_const & mask))
    8676                 :             :         {
    8677                 :           0 :           if (wanted_code == NE_EXPR)
    8678                 :           0 :             return constant_boolean_node (true, truth_type);
    8679                 :             :           else
    8680                 :           0 :             return constant_boolean_node (false, truth_type);
    8681                 :             :         }
    8682                 :             : 
    8683                 :             :       /* The constants are combined so as to line up with the loaded field, so
    8684                 :             :          tentatively use the same parameters for the second combined
    8685                 :             :          compare.  */
    8686                 :        2154 :       ld_arg[1][0] = wide_int_to_tree (lntype, l_const | r_const);
    8687                 :        2154 :       toshift[1][0] = MIN (xll_bitpos, xrl_bitpos);
    8688                 :        2154 :       shifted[1][0] = 0;
    8689                 :        2154 :       bitpos[1][0] = lnbitpos;
    8690                 :        2154 :       bitsiz[1][0] = lnbitsize;
    8691                 :             : 
    8692                 :        2154 :       if (parts > 1)
    8693                 :          49 :         reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
    8694                 :             :                           shifted[1], xmask[1],
    8695                 :          49 :                           lnbitpos + GET_MODE_BITSIZE (lnmode),
    8696                 :             :                           lr_reversep);
    8697                 :             : 
    8698                 :             :       /* No masking needed, we know the full constants.  */
    8699                 :        2154 :       r_mask = wi::mask (0, true, lnprec);
    8700                 :             : 
    8701                 :             :       /* If the compiler thinks this is used uninitialized below, it's
    8702                 :             :          because it can't realize that parts can only be 2 when
    8703                 :             :          comparing with constants if l_split_load is also true.  This
    8704                 :             :          just silences the warning.  */
    8705                 :        2154 :       rnbitpos = 0;
    8706                 :        2154 :     }
    8707                 :             : 
    8708                 :             :   /* Likewise, if the right sides are not constant, align them for the combined
    8709                 :             :      compare.  Also, disallow this optimization if a size, signedness or
    8710                 :             :      storage order mismatch occurs between the left and right sides.  */
    8711                 :             :   else
    8712                 :             :     {
    8713                 :        1424 :       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
    8714                 :        1363 :           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
    8715                 :        1363 :           || ll_reversep != lr_reversep
    8716                 :             :           /* Make sure the two fields on the right
    8717                 :             :              correspond to the left without being swapped.  */
    8718                 :        1363 :           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
    8719                 :         295 :         return 0;
    8720                 :             : 
    8721                 :        1131 :       bool r_split_load;
    8722                 :        1131 :       scalar_int_mode rnmode2;
    8723                 :             : 
    8724                 :             :       /* Figure out how to load the bits for the right-hand size of the
    8725                 :             :          combined compare.  As in the left-hand size, we may have to split it,
    8726                 :             :          and then we use two separate compares.  */
    8727                 :        1131 :       first_bit = MIN (lr_bitpos, rr_bitpos);
    8728                 :        1131 :       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
    8729                 :        1131 :       HOST_WIDE_INT lr_align = TYPE_ALIGN (TREE_TYPE (lr_inner));
    8730                 :        1131 :       poly_uint64 lr_end_region = 0;
    8731                 :        1131 :       if (TYPE_SIZE (TREE_TYPE (lr_inner))
    8732                 :        1131 :           && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (lr_inner))))
    8733                 :        1131 :         lr_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (lr_inner)));
    8734                 :        1131 :       if (!get_best_mode (end_bit - first_bit, first_bit, 0, lr_end_region,
    8735                 :        1131 :                           lr_align, BITS_PER_WORD, volatilep, &rnmode))
    8736                 :             :         {
    8737                 :             :           /* Consider the possibility of recombining loads if any of the
    8738                 :             :              fields straddles across an alignment boundary, so that either
    8739                 :             :              part can be loaded along with the other field.  */
    8740                 :         126 :           HOST_WIDE_INT boundary = compute_split_boundary_from_align
    8741                 :         126 :             (lr_align, lr_bitpos, lr_bitsize, rr_bitpos, rr_bitsize);
    8742                 :             : 
    8743                 :         126 :           if (boundary < 0
    8744                 :             :               /* If we're to split both, make sure the split point is
    8745                 :             :                  the same.  */
    8746                 :         124 :               || (l_split_load
    8747                 :         122 :                   && (boundary - lr_bitpos
    8748                 :         122 :                       != (lnbitpos + GET_MODE_BITSIZE (lnmode)) - ll_bitpos))
    8749                 :         124 :               || !get_best_mode (boundary - first_bit, first_bit,
    8750                 :         124 :                                  0, lr_end_region,
    8751                 :         124 :                                  lr_align, BITS_PER_WORD, volatilep, &rnmode)
    8752                 :         250 :               || !get_best_mode (end_bit - boundary, boundary, 0, lr_end_region,
    8753                 :         124 :                                  lr_align, BITS_PER_WORD, volatilep, &rnmode2))
    8754                 :           2 :             return 0;
    8755                 :             : 
    8756                 :         124 :           r_split_load = true;
    8757                 :         124 :           parts = 2;
    8758                 :         124 :           if (lr_bitpos >= boundary)
    8759                 :             :             maybe_separate = first1 = true;
    8760                 :          82 :           else if (lr_bitpos + lr_bitsize <= boundary)
    8761                 :          23 :             maybe_separate = true;
    8762                 :             :         }
    8763                 :             :       else
    8764                 :             :         r_split_load = false;
    8765                 :             : 
    8766                 :             :       /* Find a type that can hold the entire right-hand operand.  */
    8767                 :        1129 :       rnbitsize = GET_MODE_BITSIZE (rnmode);
    8768                 :        1129 :       rnbitpos = first_bit & ~ (rnbitsize - 1);
    8769                 :        1129 :       if (r_split_load)
    8770                 :         248 :         rnbitsize += GET_MODE_BITSIZE (rnmode2);
    8771                 :        1129 :       rntype = build_nonstandard_integer_type (rnbitsize, 1);
    8772                 :        1129 :       if (!rntype)
    8773                 :             :         return  0;
    8774                 :        1129 :       rnprec = TYPE_PRECISION (rntype);
    8775                 :        1129 :       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
    8776                 :             : 
    8777                 :             :       /* Adjust for reversed endianness.  */
    8778                 :        1129 :       if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8779                 :             :         {
    8780                 :           0 :           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
    8781                 :           0 :           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
    8782                 :             :         }
    8783                 :             : 
    8784                 :             :       /* Adjust the masks to match the combined type, and combine them.  */
    8785                 :        1129 :       wide_int lr_mask, rr_mask;
    8786                 :        1129 :       if (lr_and_mask.get_precision ())
    8787                 :        1974 :         lr_mask = wi::lshift (wide_int::from (lr_and_mask, rnprec, UNSIGNED),
    8788                 :         987 :                               xlr_bitpos);
    8789                 :             :       else
    8790                 :         142 :         lr_mask = wi::shifted_mask (xlr_bitpos, lr_bitsize, false, rnprec);
    8791                 :        1129 :       if (rr_and_mask.get_precision ())
    8792                 :        1884 :         rr_mask = wi::lshift (wide_int::from (rr_and_mask, rnprec, UNSIGNED),
    8793                 :         942 :                               xrr_bitpos);
    8794                 :             :       else
    8795                 :         187 :         rr_mask = wi::shifted_mask (xrr_bitpos, rr_bitsize, false, rnprec);
    8796                 :        1129 :       r_mask = lr_mask | rr_mask;
    8797                 :             : 
    8798                 :             :       /* Load the right-hand operand of the combined compare.  */
    8799                 :        1129 :       toshift[1][0] = MIN (xlr_bitpos, xrr_bitpos);
    8800                 :        1129 :       shifted[1][0] = 0;
    8801                 :             : 
    8802                 :        1129 :       if (!r_split_load)
    8803                 :             :         {
    8804                 :        1005 :           bitpos[1][0] = rnbitpos;
    8805                 :        1005 :           bitsiz[1][0] = rnbitsize;
    8806                 :        1005 :           ld_arg[1][0] = make_bit_field_load (ll_loc[3], lr_inner, lr_arg,
    8807                 :             :                                               rntype, rnbitsize, rnbitpos,
    8808                 :        1005 :                                               lr_unsignedp || rr_unsignedp,
    8809                 :             :                                               lr_reversep, lr_load);
    8810                 :             :         }
    8811                 :             : 
    8812                 :             :       /* ... and the second part of the right-hand operand if needed.  */
    8813                 :        1129 :       if (parts > 1)
    8814                 :             :         {
    8815                 :         124 :           if (r_split_load)
    8816                 :             :             {
    8817                 :         124 :               gimple *point[2];
    8818                 :         124 :               point[0] = lr_load;
    8819                 :         124 :               point[1] = rr_load;
    8820                 :         124 :               build_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
    8821                 :             :                                 shifted[1], rl_loc[3], lr_inner, lr_arg,
    8822                 :             :                                 rnmode, rnmode2, rnbitpos, lr_reversep, point);
    8823                 :             :             }
    8824                 :             :           else
    8825                 :           0 :             reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
    8826                 :             :                               shifted[1], xmask[1],
    8827                 :           0 :                               lnbitpos + GET_MODE_BITSIZE (lnmode)
    8828                 :           0 :                               - ll_bitpos + lr_bitpos, lr_reversep);
    8829                 :             :         }
    8830                 :        1129 :     }
    8831                 :             : 
    8832                 :             :   /* Now issue the loads for the left-hand combined operand/s.  */
    8833                 :        6566 :   wide_int l_mask = ll_mask | rl_mask;
    8834                 :        3283 :   toshift[0][0] = MIN (xll_bitpos, xrl_bitpos);
    8835                 :        3283 :   shifted[0][0] = 0;
    8836                 :             : 
    8837                 :        3283 :   if (!l_split_load)
    8838                 :             :     {
    8839                 :        3112 :       bitpos[0][0] = lnbitpos;
    8840                 :        3112 :       bitsiz[0][0] = lnbitsize;
    8841                 :        3112 :       ld_arg[0][0] = make_bit_field_load (ll_loc[3], ll_inner, ll_arg,
    8842                 :             :                                           lntype, lnbitsize, lnbitpos,
    8843                 :        3112 :                                           ll_unsignedp || rl_unsignedp,
    8844                 :             :                                           ll_reversep, ll_load);
    8845                 :             :     }
    8846                 :             : 
    8847                 :        3283 :   if (parts > 1)
    8848                 :             :     {
    8849                 :         173 :       if (l_split_load)
    8850                 :             :             {
    8851                 :         171 :               gimple *point[2];
    8852                 :         171 :               point[0] = ll_load;
    8853                 :         171 :               point[1] = rl_load;
    8854                 :         171 :               build_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
    8855                 :             :                                 shifted[0], rl_loc[3], ll_inner, ll_arg,
    8856                 :             :                                 lnmode, lnmode2, lnbitpos, ll_reversep, point);
    8857                 :             :             }
    8858                 :             :       else
    8859                 :           2 :         reuse_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
    8860                 :             :                           shifted[0], xmask[0],
    8861                 :           2 :                           rnbitpos + GET_MODE_BITSIZE (rnmode)
    8862                 :           2 :                           - lr_bitpos + ll_bitpos, ll_reversep);
    8863                 :             :     }
    8864                 :             : 
    8865                 :             :   /* Compute the compares.  */
    8866                 :        6739 :   for (int i = 0; i < parts; i++)
    8867                 :             :     {
    8868                 :        3456 :       tree op[2] = { ld_arg[0][i], ld_arg[1][i] };
    8869                 :       10368 :       wide_int mask[2] = { l_mask, r_mask };
    8870                 :        3456 :       location_t *locs[2] = { i ? rl_loc : ll_loc, i ? rr_loc : lr_loc };
    8871                 :             : 
    8872                 :             :       /* Figure out the masks, and unshare the original operands.  */
    8873                 :       10368 :       for (int j = 0; j < 2; j++)
    8874                 :             :         {
    8875                 :        6912 :           unsigned prec = TYPE_PRECISION (TREE_TYPE (op[j]));
    8876                 :        6912 :           op[j] = unshare_expr (op[j]);
    8877                 :             : 
    8878                 :             :           /* Mask out the bits belonging to the other part.  */
    8879                 :        6912 :           if (xmask[j][i].get_precision ())
    8880                 :         102 :             mask[j] &= xmask[j][i];
    8881                 :             : 
    8882                 :        6912 :           if (shifted[j][i])
    8883                 :             :             {
    8884                 :         295 :               wide_int shift = wide_int::from (shifted[j][i], prec, UNSIGNED);
    8885                 :         295 :               mask[j] = wi::lrshift (mask[j], shift);
    8886                 :         295 :             }
    8887                 :        6912 :           mask[j] = wide_int::from (mask[j], prec, UNSIGNED);
    8888                 :             :         }
    8889                 :             : 
    8890                 :             :       /* Line up the operands for a compare.  */
    8891                 :        3456 :       HOST_WIDE_INT shift = (toshift[0][i] - toshift[1][i]);
    8892                 :             : 
    8893                 :        3456 :       if (shift)
    8894                 :             :         {
    8895                 :          54 :           int j;
    8896                 :          54 :           if (shift > 0)
    8897                 :             :             j = 0;
    8898                 :             :           else
    8899                 :             :             {
    8900                 :          52 :               j = 1;
    8901                 :          52 :               shift = -shift;
    8902                 :             :             }
    8903                 :             : 
    8904                 :          54 :           tree shiftsz = bitsize_int (shift);
    8905                 :          54 :           op[j] = fold_build2_loc (locs[j][1], RSHIFT_EXPR, TREE_TYPE (op[j]),
    8906                 :             :                                    op[j], shiftsz);
    8907                 :          54 :           mask[j] = wi::lrshift (mask[j], shift);
    8908                 :             :         }
    8909                 :             : 
    8910                 :             :       /* Convert to the smaller type before masking out unwanted
    8911                 :             :          bits.  */
    8912                 :        3456 :       tree type = TREE_TYPE (op[0]);
    8913                 :        3456 :       if (type != TREE_TYPE (op[1]))
    8914                 :             :         {
    8915                 :         236 :           int j = (TYPE_PRECISION (type)
    8916                 :         236 :                    < TYPE_PRECISION (TREE_TYPE (op[1])));
    8917                 :         236 :           if (!j)
    8918                 :         137 :             type = TREE_TYPE (op[1]);
    8919                 :         236 :           op[j] = fold_convert_loc (locs[j][0], type, op[j]);
    8920                 :         236 :           mask[j] = wide_int::from (mask[j], TYPE_PRECISION (type), UNSIGNED);
    8921                 :             :         }
    8922                 :             : 
    8923                 :             :       /* Apply masks.  */
    8924                 :       10368 :       for (int j = 0; j < 2; j++)
    8925                 :        6912 :         if (mask[j] != wi::mask (0, true, mask[j].get_precision ()))
    8926                 :        2873 :           op[j] = fold_build2_loc (locs[j][2], BIT_AND_EXPR, type,
    8927                 :        5746 :                                    op[j], wide_int_to_tree (type, mask[j]));
    8928                 :             : 
    8929                 :        6739 :       cmp[i] = fold_build2_loc (i ? rloc : lloc, wanted_code, truth_type,
    8930                 :             :                                 op[0], op[1]);
    8931                 :       10368 :     }
    8932                 :             : 
    8933                 :             :   /* Reorder the compares if needed.  */
    8934                 :        3283 :   if (first1)
    8935                 :          45 :     std::swap (cmp[0], cmp[1]);
    8936                 :             : 
    8937                 :             :   /* Prepare to return the resulting compares.  Combine two parts if
    8938                 :             :      needed.  */
    8939                 :        3283 :   if (parts == 1)
    8940                 :        3110 :     result = cmp[0];
    8941                 :         173 :   else if (!separatep || !maybe_separate)
    8942                 :             :     {
    8943                 :             :       /* Only fold if any of the cmp is known, otherwise we may lose the
    8944                 :             :          sequence point, and that may prevent further optimizations.  */
    8945                 :         167 :       if (TREE_CODE (cmp[0]) == INTEGER_CST
    8946                 :         131 :           || TREE_CODE (cmp[1]) == INTEGER_CST)
    8947                 :          37 :         result = fold_build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
    8948                 :             :       else
    8949                 :         130 :         result = build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
    8950                 :             :     }
    8951                 :             :   else
    8952                 :             :     {
    8953                 :           6 :       result = cmp[0];
    8954                 :           6 :       *separatep = cmp[1];
    8955                 :             :     }
    8956                 :             : 
    8957                 :        3283 :   return result;
    8958                 :      279162 : }
    8959                 :             : 
    8960                 :             : /* Try to simplify the AND of two comparisons, specified by
    8961                 :             :    (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
    8962                 :             :    If this can be simplified to a single expression (without requiring
    8963                 :             :    introducing more SSA variables to hold intermediate values),
    8964                 :             :    return the resulting tree.  Otherwise return NULL_TREE.
    8965                 :             :    If the result expression is non-null, it has boolean type.  */
    8966                 :             : 
    8967                 :             : tree
    8968                 :      444995 : maybe_fold_and_comparisons (tree type,
    8969                 :             :                             enum tree_code code1, tree op1a, tree op1b,
    8970                 :             :                             enum tree_code code2, tree op2a, tree op2b,
    8971                 :             :                             basic_block outer_cond_bb)
    8972                 :             : {
    8973                 :      444995 :   if (tree t = and_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
    8974                 :             :                                   outer_cond_bb))
    8975                 :             :     return t;
    8976                 :             : 
    8977                 :      444048 :   if (tree t = and_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
    8978                 :             :                                   outer_cond_bb))
    8979                 :             :     return t;
    8980                 :             : 
    8981                 :      444032 :   if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_AND_EXPR, code1,
    8982                 :             :                                                      op1a, op1b, code2, op2a,
    8983                 :             :                                                      op2b, outer_cond_bb))
    8984                 :             :     return t;
    8985                 :             : 
    8986                 :             :   return NULL_TREE;
    8987                 :             : }
    8988                 :             : 
    8989                 :             : /* Helper function for or_comparisons_1:  try to simplify the OR of the
    8990                 :             :    ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
    8991                 :             :    If INVERT is true, invert the value of VAR before doing the OR.
    8992                 :             :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    8993                 :             : 
    8994                 :             : static tree
    8995                 :       43191 : or_var_with_comparison (tree type, tree var, bool invert,
    8996                 :             :                         enum tree_code code2, tree op2a, tree op2b,
    8997                 :             :                         basic_block outer_cond_bb)
    8998                 :             : {
    8999                 :       43191 :   tree t;
    9000                 :       43191 :   gimple *stmt = SSA_NAME_DEF_STMT (var);
    9001                 :             : 
    9002                 :             :   /* We can only deal with variables whose definitions are assignments.  */
    9003                 :       43191 :   if (!is_gimple_assign (stmt))
    9004                 :             :     return NULL_TREE;
    9005                 :             : 
    9006                 :             :   /* If we have an inverted comparison, apply DeMorgan's law and rewrite
    9007                 :             :      !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
    9008                 :             :      Then we only have to consider the simpler non-inverted cases.  */
    9009                 :       43072 :   if (invert)
    9010                 :       23971 :     t = and_var_with_comparison_1 (type, stmt,
    9011                 :             :                                    invert_tree_comparison (code2, false),
    9012                 :             :                                    op2a, op2b, outer_cond_bb);
    9013                 :             :   else
    9014                 :       19101 :     t = or_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
    9015                 :             :                                   outer_cond_bb);
    9016                 :       43072 :   return canonicalize_bool (t, invert);
    9017                 :             : }
    9018                 :             : 
    9019                 :             : /* Try to simplify the OR of the ssa variable defined by the assignment
    9020                 :             :    STMT with the comparison specified by (OP2A CODE2 OP2B).
    9021                 :             :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    9022                 :             : 
    9023                 :             : static tree
    9024                 :      166815 : or_var_with_comparison_1 (tree type, gimple *stmt,
    9025                 :             :                           enum tree_code code2, tree op2a, tree op2b,
    9026                 :             :                           basic_block outer_cond_bb)
    9027                 :             : {
    9028                 :      166815 :   tree var = gimple_assign_lhs (stmt);
    9029                 :      166815 :   tree true_test_var = NULL_TREE;
    9030                 :      166815 :   tree false_test_var = NULL_TREE;
    9031                 :      166815 :   enum tree_code innercode = gimple_assign_rhs_code (stmt);
    9032                 :             : 
    9033                 :             :   /* Check for identities like (var OR (var != 0)) => true .  */
    9034                 :      166815 :   if (TREE_CODE (op2a) == SSA_NAME
    9035                 :      166815 :       && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
    9036                 :             :     {
    9037                 :       15870 :       if ((code2 == NE_EXPR && integer_zerop (op2b))
    9038                 :       51506 :           || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
    9039                 :             :         {
    9040                 :       14344 :           true_test_var = op2a;
    9041                 :       14344 :           if (var == true_test_var)
    9042                 :             :             return var;
    9043                 :             :         }
    9044                 :        3043 :       else if ((code2 == EQ_EXPR && integer_zerop (op2b))
    9045                 :       30828 :                || (code2 == NE_EXPR && integer_nonzerop (op2b)))
    9046                 :             :         {
    9047                 :        7280 :           false_test_var = op2a;
    9048                 :        7280 :           if (var == false_test_var)
    9049                 :           0 :             return boolean_true_node;
    9050                 :             :         }
    9051                 :             :     }
    9052                 :             : 
    9053                 :             :   /* If the definition is a comparison, recurse on it.  */
    9054                 :      166815 :   if (TREE_CODE_CLASS (innercode) == tcc_comparison)
    9055                 :             :     {
    9056                 :         848 :       tree t = or_comparisons_1 (type, innercode,
    9057                 :             :                                  gimple_assign_rhs1 (stmt),
    9058                 :             :                                  gimple_assign_rhs2 (stmt),
    9059                 :             :                                  code2, op2a, op2b, outer_cond_bb);
    9060                 :         848 :       if (t)
    9061                 :             :         return t;
    9062                 :             :     }
    9063                 :             : 
    9064                 :             :   /* If the definition is an AND or OR expression, we may be able to
    9065                 :             :      simplify by reassociating.  */
    9066                 :      166786 :   if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
    9067                 :      166786 :       && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
    9068                 :             :     {
    9069                 :       40269 :       tree inner1 = gimple_assign_rhs1 (stmt);
    9070                 :       40269 :       tree inner2 = gimple_assign_rhs2 (stmt);
    9071                 :       40269 :       gimple *s;
    9072                 :       40269 :       tree t;
    9073                 :       40269 :       tree partial = NULL_TREE;
    9074                 :       40269 :       bool is_or = (innercode == BIT_IOR_EXPR);
    9075                 :             : 
    9076                 :             :       /* Check for boolean identities that don't require recursive examination
    9077                 :             :          of inner1/inner2:
    9078                 :             :          inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
    9079                 :             :          inner1 OR (inner1 AND inner2) => inner1
    9080                 :             :          !inner1 OR (inner1 OR inner2) => true
    9081                 :             :          !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
    9082                 :             :       */
    9083                 :       40269 :       if (inner1 == true_test_var)
    9084                 :           0 :         return (is_or ? var : inner1);
    9085                 :       40269 :       else if (inner2 == true_test_var)
    9086                 :           0 :         return (is_or ? var : inner2);
    9087                 :       40269 :       else if (inner1 == false_test_var)
    9088                 :           0 :         return (is_or
    9089                 :           0 :                 ? boolean_true_node
    9090                 :           0 :                 : or_var_with_comparison (type, inner2, false, code2, op2a,
    9091                 :             :                                           op2b, outer_cond_bb));
    9092                 :       40269 :       else if (inner2 == false_test_var)
    9093                 :           0 :         return (is_or
    9094                 :           0 :                 ? boolean_true_node
    9095                 :           0 :                 : or_var_with_comparison (type, inner1, false, code2, op2a,
    9096                 :             :                                           op2b, outer_cond_bb));
    9097                 :             : 
    9098                 :             :       /* Next, redistribute/reassociate the OR across the inner tests.
    9099                 :             :          Compute the first partial result, (inner1 OR (op2a code op2b))  */
    9100                 :       40269 :       if (TREE_CODE (inner1) == SSA_NAME
    9101                 :       40269 :           && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
    9102                 :       39273 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    9103                 :       64654 :           && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
    9104                 :             :                                              gimple_assign_rhs1 (s),
    9105                 :             :                                              gimple_assign_rhs2 (s),
    9106                 :             :                                              code2, op2a, op2b,
    9107                 :             :                                              outer_cond_bb)))
    9108                 :             :         {
    9109                 :             :           /* Handle the OR case, where we are reassociating:
    9110                 :             :              (inner1 OR inner2) OR (op2a code2 op2b)
    9111                 :             :              => (t OR inner2)
    9112                 :             :              If the partial result t is a constant, we win.  Otherwise
    9113                 :             :              continue on to try reassociating with the other inner test.  */
    9114                 :         751 :           if (is_or)
    9115                 :             :             {
    9116                 :          39 :               if (integer_onep (t))
    9117                 :           0 :                 return boolean_true_node;
    9118                 :          39 :               else if (integer_zerop (t))
    9119                 :             :                 return inner2;
    9120                 :             :             }
    9121                 :             : 
    9122                 :             :           /* Handle the AND case, where we are redistributing:
    9123                 :             :              (inner1 AND inner2) OR (op2a code2 op2b)
    9124                 :             :              => (t AND (inner2 OR (op2a code op2b)))  */
    9125                 :         712 :           else if (integer_zerop (t))
    9126                 :           0 :             return boolean_false_node;
    9127                 :             : 
    9128                 :             :           /* Save partial result for later.  */
    9129                 :             :           partial = t;
    9130                 :             :         }
    9131                 :             : 
    9132                 :             :       /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
    9133                 :       40269 :       if (TREE_CODE (inner2) == SSA_NAME
    9134                 :       40269 :           && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
    9135                 :       39585 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    9136                 :       78066 :           && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
    9137                 :             :                                              gimple_assign_rhs1 (s),
    9138                 :             :                                              gimple_assign_rhs2 (s),
    9139                 :             :                                              code2, op2a, op2b,
    9140                 :             :                                              outer_cond_bb)))
    9141                 :             :         {
    9142                 :             :           /* Handle the OR case, where we are reassociating:
    9143                 :             :              (inner1 OR inner2) OR (op2a code2 op2b)
    9144                 :             :              => (inner1 OR t)
    9145                 :             :              => (t OR partial)  */
    9146                 :         509 :           if (is_or)
    9147                 :             :             {
    9148                 :          65 :               if (integer_zerop (t))
    9149                 :             :                 return inner1;
    9150                 :          65 :               else if (integer_onep (t))
    9151                 :           1 :                 return boolean_true_node;
    9152                 :             :               /* If both are the same, we can apply the identity
    9153                 :             :                  (x OR x) == x.  */
    9154                 :          64 :               else if (partial && same_bool_result_p (t, partial))
    9155                 :             :                 return t;
    9156                 :             :             }
    9157                 :             : 
    9158                 :             :           /* Handle the AND case, where we are redistributing:
    9159                 :             :              (inner1 AND inner2) OR (op2a code2 op2b)
    9160                 :             :              => (t AND (inner1 OR (op2a code2 op2b)))
    9161                 :             :              => (t AND partial)  */
    9162                 :             :           else
    9163                 :             :             {
    9164                 :         444 :               if (integer_zerop (t))
    9165                 :           0 :                 return boolean_false_node;
    9166                 :         444 :               else if (partial)
    9167                 :             :                 {
    9168                 :             :                   /* We already got a simplification for the other
    9169                 :             :                      operand to the redistributed AND expression.  The
    9170                 :             :                      interesting case is when at least one is true.
    9171                 :             :                      Or, if both are the same, we can apply the identity
    9172                 :             :                      (x AND x) == x.  */
    9173                 :          14 :                   if (integer_onep (partial))
    9174                 :             :                     return t;
    9175                 :          14 :                   else if (integer_onep (t))
    9176                 :             :                     return partial;
    9177                 :           4 :                   else if (same_bool_result_p (t, partial))
    9178                 :             :                     return t;
    9179                 :             :                 }
    9180                 :             :             }
    9181                 :             :         }
    9182                 :             :     }
    9183                 :             :   return NULL_TREE;
    9184                 :             : }
    9185                 :             : 
    9186                 :             : /* Try to simplify the OR of two comparisons defined by
    9187                 :             :    (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
    9188                 :             :    If this can be done without constructing an intermediate value,
    9189                 :             :    return the resulting tree; otherwise NULL_TREE is returned.
    9190                 :             :    This function is deliberately asymmetric as it recurses on SSA_DEFs
    9191                 :             :    in the first comparison but not the second.  */
    9192                 :             : 
    9193                 :             : static tree
    9194                 :      989345 : or_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
    9195                 :             :                   enum tree_code code2, tree op2a, tree op2b,
    9196                 :             :                   basic_block outer_cond_bb)
    9197                 :             : {
    9198                 :      989345 :   tree truth_type = truth_type_for (TREE_TYPE (op1a));
    9199                 :             : 
    9200                 :             :   /* First check for ((x CODE1 y) OR (x CODE2 y)).  */
    9201                 :      989345 :   if (operand_equal_p (op1a, op2a, 0)
    9202                 :      989345 :       && operand_equal_p (op1b, op2b, 0))
    9203                 :             :     {
    9204                 :             :       /* Result will be either NULL_TREE, or a combined comparison.  */
    9205                 :        3100 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    9206                 :             :                                     TRUTH_ORIF_EXPR, code1, code2,
    9207                 :             :                                     truth_type, op1a, op1b);
    9208                 :        3100 :       if (t)
    9209                 :             :         return t;
    9210                 :             :     }
    9211                 :             : 
    9212                 :             :   /* Likewise the swapped case of the above.  */
    9213                 :      986277 :   if (operand_equal_p (op1a, op2b, 0)
    9214                 :      986277 :       && operand_equal_p (op1b, op2a, 0))
    9215                 :             :     {
    9216                 :             :       /* Result will be either NULL_TREE, or a combined comparison.  */
    9217                 :           0 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    9218                 :             :                                     TRUTH_ORIF_EXPR, code1,
    9219                 :             :                                     swap_tree_comparison (code2),
    9220                 :             :                                     truth_type, op1a, op1b);
    9221                 :           0 :       if (t)
    9222                 :             :         return t;
    9223                 :             :     }
    9224                 :             : 
    9225                 :             :   /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
    9226                 :             :      NAME's definition is a truth value.  See if there are any simplifications
    9227                 :             :      that can be done against the NAME's definition.  */
    9228                 :      986277 :   if (TREE_CODE (op1a) == SSA_NAME
    9229                 :      985744 :       && (code1 == NE_EXPR || code1 == EQ_EXPR)
    9230                 :     1269848 :       && (integer_zerop (op1b) || integer_onep (op1b)))
    9231                 :             :     {
    9232                 :       38096 :       bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
    9233                 :       75426 :                      || (code1 == NE_EXPR && integer_onep (op1b)));
    9234                 :       71340 :       gimple *stmt = SSA_NAME_DEF_STMT (op1a);
    9235                 :       71340 :       switch (gimple_code (stmt))
    9236                 :             :         {
    9237                 :       43060 :         case GIMPLE_ASSIGN:
    9238                 :             :           /* Try to simplify by copy-propagating the definition.  */
    9239                 :       43060 :           return or_var_with_comparison (type, op1a, invert, code2, op2a,
    9240                 :       43060 :                                          op2b, outer_cond_bb);
    9241                 :             : 
    9242                 :       14765 :         case GIMPLE_PHI:
    9243                 :             :           /* If every argument to the PHI produces the same result when
    9244                 :             :              ORed with the second comparison, we win.
    9245                 :             :              Do not do this unless the type is bool since we need a bool
    9246                 :             :              result here anyway.  */
    9247                 :       14765 :           if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
    9248                 :             :             {
    9249                 :             :               tree result = NULL_TREE;
    9250                 :             :               unsigned i;
    9251                 :         934 :               for (i = 0; i < gimple_phi_num_args (stmt); i++)
    9252                 :             :                 {
    9253                 :         934 :                   tree arg = gimple_phi_arg_def (stmt, i);
    9254                 :             : 
    9255                 :             :                   /* If this PHI has itself as an argument, ignore it.
    9256                 :             :                      If all the other args produce the same result,
    9257                 :             :                      we're still OK.  */
    9258                 :         934 :                   if (arg == gimple_phi_result (stmt))
    9259                 :           0 :                     continue;
    9260                 :         934 :                   else if (TREE_CODE (arg) == INTEGER_CST)
    9261                 :             :                     {
    9262                 :         773 :                       if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
    9263                 :             :                         {
    9264                 :         353 :                           if (!result)
    9265                 :         213 :                             result = boolean_true_node;
    9266                 :         140 :                           else if (!integer_onep (result))
    9267                 :             :                             return NULL_TREE;
    9268                 :             :                         }
    9269                 :         420 :                       else if (!result)
    9270                 :         200 :                         result = fold_build2 (code2, boolean_type_node,
    9271                 :             :                                               op2a, op2b);
    9272                 :         220 :                       else if (!same_bool_comparison_p (result,
    9273                 :             :                                                         code2, op2a, op2b))
    9274                 :             :                         return NULL_TREE;
    9275                 :             :                     }
    9276                 :         161 :                   else if (TREE_CODE (arg) == SSA_NAME
    9277                 :         161 :                            && !SSA_NAME_IS_DEFAULT_DEF (arg))
    9278                 :             :                     {
    9279                 :         161 :                       tree temp;
    9280                 :         161 :                       gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
    9281                 :             :                       /* In simple cases we can look through PHI nodes,
    9282                 :             :                          but we have to be careful with loops.
    9283                 :             :                          See PR49073.  */
    9284                 :         161 :                       if (! dom_info_available_p (CDI_DOMINATORS)
    9285                 :         161 :                           || gimple_bb (def_stmt) == gimple_bb (stmt)
    9286                 :         322 :                           || dominated_by_p (CDI_DOMINATORS,
    9287                 :         161 :                                              gimple_bb (def_stmt),
    9288                 :         161 :                                              gimple_bb (stmt)))
    9289                 :          30 :                         return NULL_TREE;
    9290                 :         131 :                       temp = or_var_with_comparison (type, arg, invert, code2,
    9291                 :             :                                                      op2a, op2b, outer_cond_bb);
    9292                 :         131 :                       if (!temp)
    9293                 :             :                         return NULL_TREE;
    9294                 :           0 :                       else if (!result)
    9295                 :             :                         result = temp;
    9296                 :           0 :                       else if (!same_bool_result_p (result, temp))
    9297                 :             :                         return NULL_TREE;
    9298                 :             :                     }
    9299                 :             :                   else
    9300                 :             :                     return NULL_TREE;
    9301                 :             :                 }
    9302                 :             :               return result;
    9303                 :             :             }
    9304                 :             : 
    9305                 :             :         default:
    9306                 :             :           break;
    9307                 :             :         }
    9308                 :             :     }
    9309                 :             :   return NULL_TREE;
    9310                 :             : }
    9311                 :             : 
    9312                 :             : /* Try to simplify the OR of two comparisons, specified by
    9313                 :             :    (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
    9314                 :             :    If this can be simplified to a single expression (without requiring
    9315                 :             :    introducing more SSA variables to hold intermediate values),
    9316                 :             :    return the resulting tree.  Otherwise return NULL_TREE.
    9317                 :             :    If the result expression is non-null, it has boolean type.  */
    9318                 :             : 
    9319                 :             : tree
    9320                 :      495768 : maybe_fold_or_comparisons (tree type,
    9321                 :             :                            enum tree_code code1, tree op1a, tree op1b,
    9322                 :             :                            enum tree_code code2, tree op2a, tree op2b,
    9323                 :             :                            basic_block outer_cond_bb)
    9324                 :             : {
    9325                 :      495768 :   if (tree t = or_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
    9326                 :             :                                  outer_cond_bb))
    9327                 :             :     return t;
    9328                 :             : 
    9329                 :      492729 :   if (tree t = or_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
    9330                 :             :                                  outer_cond_bb))
    9331                 :             :     return t;
    9332                 :             : 
    9333                 :      492724 :   if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_IOR_EXPR, code1,
    9334                 :             :                                                      op1a, op1b, code2, op2a,
    9335                 :             :                                                      op2b, outer_cond_bb))
    9336                 :             :     return t;
    9337                 :             : 
    9338                 :             :   return NULL_TREE;
    9339                 :             : }
    9340                 :             : 
    9341                 :             : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
    9342                 :             : 
    9343                 :             :    Either NULL_TREE, a simplified but non-constant or a constant
    9344                 :             :    is returned.
    9345                 :             : 
    9346                 :             :    ???  This should go into a gimple-fold-inline.h file to be eventually
    9347                 :             :    privatized with the single valueize function used in the various TUs
    9348                 :             :    to avoid the indirect function call overhead.  */
    9349                 :             : 
    9350                 :             : tree
    9351                 :   406693442 : gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
    9352                 :             :                                 tree (*gvalueize) (tree))
    9353                 :             : {
    9354                 :   406693442 :   gimple_match_op res_op;
    9355                 :             :   /* ???  The SSA propagators do not correctly deal with following SSA use-def
    9356                 :             :      edges if there are intermediate VARYING defs.  For this reason
    9357                 :             :      do not follow SSA edges here even though SCCVN can technically
    9358                 :             :      just deal fine with that.  */
    9359                 :   406693442 :   if (gimple_simplify (stmt, &res_op, NULL, gvalueize, valueize))
    9360                 :             :     {
    9361                 :    53961323 :       tree res = NULL_TREE;
    9362                 :    53961323 :       if (gimple_simplified_result_is_gimple_val (&res_op))
    9363                 :    32335984 :         res = res_op.ops[0];
    9364                 :    21625339 :       else if (mprts_hook)
    9365                 :     7614305 :         res = mprts_hook (&res_op);
    9366                 :    39950289 :       if (res)
    9367                 :             :         {
    9368                 :    34106516 :           if (dump_file && dump_flags & TDF_DETAILS)
    9369                 :             :             {
    9370                 :        8742 :               fprintf (dump_file, "Match-and-simplified ");
    9371                 :        8742 :               print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
    9372                 :        8742 :               fprintf (dump_file, " to ");
    9373                 :        8742 :               print_generic_expr (dump_file, res);
    9374                 :        8742 :               fprintf (dump_file, "\n");
    9375                 :             :             }
    9376                 :    34106516 :           return res;
    9377                 :             :         }
    9378                 :             :     }
    9379                 :             : 
    9380                 :   372586926 :   location_t loc = gimple_location (stmt);
    9381                 :   372586926 :   switch (gimple_code (stmt))
    9382                 :             :     {
    9383                 :   322510647 :     case GIMPLE_ASSIGN:
    9384                 :   322510647 :       {
    9385                 :   322510647 :         enum tree_code subcode = gimple_assign_rhs_code (stmt);
    9386                 :             : 
    9387                 :   322510647 :         switch (get_gimple_rhs_class (subcode))
    9388                 :             :           {
    9389                 :   118231108 :           case GIMPLE_SINGLE_RHS:
    9390                 :   118231108 :             {
    9391                 :   118231108 :               tree rhs = gimple_assign_rhs1 (stmt);
    9392                 :   118231108 :               enum tree_code_class kind = TREE_CODE_CLASS (subcode);
    9393                 :             : 
    9394                 :   118231108 :               if (TREE_CODE (rhs) == SSA_NAME)
    9395                 :             :                 {
    9396                 :             :                   /* If the RHS is an SSA_NAME, return its known constant value,
    9397                 :             :                      if any.  */
    9398                 :     9157652 :                   return (*valueize) (rhs);
    9399                 :             :                 }
    9400                 :             :               /* Handle propagating invariant addresses into address
    9401                 :             :                  operations.  */
    9402                 :   109073456 :               else if (TREE_CODE (rhs) == ADDR_EXPR
    9403                 :   109073456 :                        && !is_gimple_min_invariant (rhs))
    9404                 :             :                 {
    9405                 :     7427812 :                   poly_int64 offset = 0;
    9406                 :     7427812 :                   tree base;
    9407                 :     7427812 :                   base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
    9408                 :             :                                                           &offset,
    9409                 :             :                                                           valueize);
    9410                 :     7427812 :                   if (base
    9411                 :     7427812 :                       && (CONSTANT_CLASS_P (base)
    9412                 :     6751349 :                           || decl_address_invariant_p (base)))
    9413                 :      187910 :                     return build_invariant_address (TREE_TYPE (rhs),
    9414                 :      187910 :                                                     base, offset);
    9415                 :             :                 }
    9416                 :   101645644 :               else if (TREE_CODE (rhs) == CONSTRUCTOR
    9417                 :     1120211 :                        && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
    9418                 :   103287961 :                        && known_eq (CONSTRUCTOR_NELTS (rhs),
    9419                 :             :                                     TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
    9420                 :             :                 {
    9421                 :      512568 :                   unsigned i, nelts;
    9422                 :      512568 :                   tree val;
    9423                 :             : 
    9424                 :      512568 :                   nelts = CONSTRUCTOR_NELTS (rhs);
    9425                 :      512568 :                   tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
    9426                 :     1141511 :                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
    9427                 :             :                     {
    9428                 :      610814 :                       val = (*valueize) (val);
    9429                 :      610814 :                       if (TREE_CODE (val) == INTEGER_CST
    9430                 :      514265 :                           || TREE_CODE (val) == REAL_CST
    9431                 :      494439 :                           || TREE_CODE (val) == FIXED_CST)
    9432                 :      116375 :                         vec.quick_push (val);
    9433                 :             :                       else
    9434                 :             :                         return NULL_TREE;
    9435                 :             :                     }
    9436                 :             : 
    9437                 :       18129 :                   return vec.build ();
    9438                 :      512568 :                 }
    9439                 :   108372978 :               if (subcode == OBJ_TYPE_REF)
    9440                 :             :                 {
    9441                 :      109045 :                   tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
    9442                 :             :                   /* If callee is constant, we can fold away the wrapper.  */
    9443                 :      109045 :                   if (is_gimple_min_invariant (val))
    9444                 :             :                     return val;
    9445                 :             :                 }
    9446                 :             : 
    9447                 :   108372923 :               if (kind == tcc_reference)
    9448                 :             :                 {
    9449                 :    72792033 :                   if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
    9450                 :    70635754 :                        || TREE_CODE (rhs) == REALPART_EXPR
    9451                 :    69794211 :                        || TREE_CODE (rhs) == IMAGPART_EXPR)
    9452                 :    74631071 :                       && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
    9453                 :             :                     {
    9454                 :     3078552 :                       tree val = (*valueize) (TREE_OPERAND (rhs, 0));
    9455                 :     3078552 :                       return fold_unary_loc (EXPR_LOCATION (rhs),
    9456                 :     3078552 :                                              TREE_CODE (rhs),
    9457                 :     6157104 :                                              TREE_TYPE (rhs), val);
    9458                 :             :                     }
    9459                 :    69713481 :                   else if (TREE_CODE (rhs) == BIT_FIELD_REF
    9460                 :    69713481 :                            && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
    9461                 :             :                     {
    9462                 :      483430 :                       tree val = (*valueize) (TREE_OPERAND (rhs, 0));
    9463                 :      483430 :                       return fold_ternary_loc (EXPR_LOCATION (rhs),
    9464                 :      483430 :                                                TREE_CODE (rhs),
    9465                 :      483430 :                                                TREE_TYPE (rhs), val,
    9466                 :      483430 :                                                TREE_OPERAND (rhs, 1),
    9467                 :      966860 :                                                TREE_OPERAND (rhs, 2));
    9468                 :             :                     }
    9469                 :    69230051 :                   else if (TREE_CODE (rhs) == MEM_REF
    9470                 :    69230051 :                            && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
    9471                 :             :                     {
    9472                 :    13804987 :                       tree val = (*valueize) (TREE_OPERAND (rhs, 0));
    9473                 :    13804987 :                       if (TREE_CODE (val) == ADDR_EXPR
    9474                 :    13804987 :                           && is_gimple_min_invariant (val))
    9475                 :             :                         {
    9476                 :      934926 :                           tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
    9477                 :             :                                                   unshare_expr (val),
    9478                 :             :                                                   TREE_OPERAND (rhs, 1));
    9479                 :      934926 :                           if (tem)
    9480                 :    69230051 :                             rhs = tem;
    9481                 :             :                         }
    9482                 :             :                     }
    9483                 :    69230051 :                   return fold_const_aggregate_ref_1 (rhs, valueize);
    9484                 :             :                 }
    9485                 :    35580890 :               else if (kind == tcc_declaration)
    9486                 :     8550734 :                 return get_symbol_constant_value (rhs);
    9487                 :             :               return rhs;
    9488                 :             :             }
    9489                 :             : 
    9490                 :             :           case GIMPLE_UNARY_RHS:
    9491                 :             :             return NULL_TREE;
    9492                 :             : 
    9493                 :   152827603 :           case GIMPLE_BINARY_RHS:
    9494                 :             :             /* Translate &x + CST into an invariant form suitable for
    9495                 :             :                further propagation.  */
    9496                 :   152827603 :             if (subcode == POINTER_PLUS_EXPR)
    9497                 :             :               {
    9498                 :    18276611 :                 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
    9499                 :    18276611 :                 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
    9500                 :    18276611 :                 if (TREE_CODE (op0) == ADDR_EXPR
    9501                 :     5332215 :                     && TREE_CODE (op1) == INTEGER_CST)
    9502                 :             :                   {
    9503                 :      555039 :                     tree off = fold_convert (ptr_type_node, op1);
    9504                 :      555039 :                     return build1_loc
    9505                 :     1110078 :                         (loc, ADDR_EXPR, TREE_TYPE (op0),
    9506                 :      555039 :                          fold_build2 (MEM_REF,
    9507                 :             :                                       TREE_TYPE (TREE_TYPE (op0)),
    9508                 :      555039 :                                       unshare_expr (op0), off));
    9509                 :             :                   }
    9510                 :             :               }
    9511                 :             :             /* Canonicalize bool != 0 and bool == 0 appearing after
    9512                 :             :                valueization.  While gimple_simplify handles this
    9513                 :             :                it can get confused by the ~X == 1 -> X == 0 transform
    9514                 :             :                which we cant reduce to a SSA name or a constant
    9515                 :             :                (and we have no way to tell gimple_simplify to not
    9516                 :             :                consider those transforms in the first place).  */
    9517                 :   134550992 :             else if (subcode == EQ_EXPR
    9518                 :   134550992 :                      || subcode == NE_EXPR)
    9519                 :             :               {
    9520                 :     3277039 :                 tree lhs = gimple_assign_lhs (stmt);
    9521                 :     3277039 :                 tree op0 = gimple_assign_rhs1 (stmt);
    9522                 :     3277039 :                 if (useless_type_conversion_p (TREE_TYPE (lhs),
    9523                 :     3277039 :                                                TREE_TYPE (op0)))
    9524                 :             :                   {
    9525                 :       26214 :                     tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
    9526                 :       26214 :                     op0 = (*valueize) (op0);
    9527                 :       26214 :                     if (TREE_CODE (op0) == INTEGER_CST)
    9528                 :         941 :                       std::swap (op0, op1);
    9529                 :       26214 :                     if (TREE_CODE (op1) == INTEGER_CST
    9530                 :       26214 :                         && ((subcode == NE_EXPR && integer_zerop (op1))
    9531                 :        2597 :                             || (subcode == EQ_EXPR && integer_onep (op1))))
    9532                 :         265 :                       return op0;
    9533                 :             :                   }
    9534                 :             :               }
    9535                 :             :             return NULL_TREE;
    9536                 :             : 
    9537                 :      616656 :           case GIMPLE_TERNARY_RHS:
    9538                 :      616656 :             {
    9539                 :             :               /* Handle ternary operators that can appear in GIMPLE form.  */
    9540                 :      616656 :               tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
    9541                 :      616656 :               tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
    9542                 :      616656 :               tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
    9543                 :      616656 :               return fold_ternary_loc (loc, subcode,
    9544                 :      616656 :                                        TREE_TYPE (gimple_assign_lhs (stmt)),
    9545                 :      616656 :                                        op0, op1, op2);
    9546                 :             :             }
    9547                 :             : 
    9548                 :           0 :           default:
    9549                 :           0 :             gcc_unreachable ();
    9550                 :             :           }
    9551                 :             :       }
    9552                 :             : 
    9553                 :    13847208 :     case GIMPLE_CALL:
    9554                 :    13847208 :       {
    9555                 :    13847208 :         tree fn;
    9556                 :    13847208 :         gcall *call_stmt = as_a <gcall *> (stmt);
    9557                 :             : 
    9558                 :    13847208 :         if (gimple_call_internal_p (stmt))
    9559                 :             :           {
    9560                 :     1168098 :             enum tree_code subcode = ERROR_MARK;
    9561                 :     1168098 :             switch (gimple_call_internal_fn (stmt))
    9562                 :             :               {
    9563                 :             :               case IFN_UBSAN_CHECK_ADD:
    9564                 :             :                 subcode = PLUS_EXPR;
    9565                 :             :                 break;
    9566                 :        7929 :               case IFN_UBSAN_CHECK_SUB:
    9567                 :        7929 :                 subcode = MINUS_EXPR;
    9568                 :        7929 :                 break;
    9569                 :        6815 :               case IFN_UBSAN_CHECK_MUL:
    9570                 :        6815 :                 subcode = MULT_EXPR;
    9571                 :        6815 :                 break;
    9572                 :      122514 :               case IFN_BUILTIN_EXPECT:
    9573                 :      122514 :                   {
    9574                 :      122514 :                     tree arg0 = gimple_call_arg (stmt, 0);
    9575                 :      122514 :                     tree op0 = (*valueize) (arg0);
    9576                 :      122514 :                     if (TREE_CODE (op0) == INTEGER_CST)
    9577                 :             :                       return op0;
    9578                 :             :                     return NULL_TREE;
    9579                 :             :                   }
    9580                 :             :               default:
    9581                 :             :                 return NULL_TREE;
    9582                 :             :               }
    9583                 :       22833 :             tree arg0 = gimple_call_arg (stmt, 0);
    9584                 :       22833 :             tree arg1 = gimple_call_arg (stmt, 1);
    9585                 :       22833 :             tree op0 = (*valueize) (arg0);
    9586                 :       22833 :             tree op1 = (*valueize) (arg1);
    9587                 :             : 
    9588                 :       22833 :             if (TREE_CODE (op0) != INTEGER_CST
    9589                 :        2482 :                 || TREE_CODE (op1) != INTEGER_CST)
    9590                 :             :               {
    9591                 :       22323 :                 switch (subcode)
    9592                 :             :                   {
    9593                 :        6715 :                   case MULT_EXPR:
    9594                 :             :                     /* x * 0 = 0 * x = 0 without overflow.  */
    9595                 :        6715 :                     if (integer_zerop (op0) || integer_zerop (op1))
    9596                 :          20 :                       return build_zero_cst (TREE_TYPE (arg0));
    9597                 :             :                     break;
    9598                 :        7587 :                   case MINUS_EXPR:
    9599                 :             :                     /* y - y = 0 without overflow.  */
    9600                 :        7587 :                     if (operand_equal_p (op0, op1, 0))
    9601                 :           0 :                       return build_zero_cst (TREE_TYPE (arg0));
    9602                 :             :                     break;
    9603                 :             :                   default:
    9604                 :             :                     break;
    9605                 :             :                   }
    9606                 :             :               }
    9607                 :       22813 :             tree res
    9608                 :       22813 :               = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
    9609                 :       22813 :             if (res
    9610                 :        2861 :                 && TREE_CODE (res) == INTEGER_CST
    9611                 :       23323 :                 && !TREE_OVERFLOW (res))
    9612                 :             :               return res;
    9613                 :             :             return NULL_TREE;
    9614                 :             :           }
    9615                 :             : 
    9616                 :    12679110 :         fn = (*valueize) (gimple_call_fn (stmt));
    9617                 :    12679110 :         if (TREE_CODE (fn) == ADDR_EXPR
    9618                 :    12057966 :             && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
    9619                 :    12057902 :             && fndecl_built_in_p (TREE_OPERAND (fn, 0))
    9620                 :    18588873 :             && gimple_builtin_call_types_compatible_p (stmt,
    9621                 :     5909763 :                                                        TREE_OPERAND (fn, 0)))
    9622                 :             :           {
    9623                 :     5817432 :             tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
    9624                 :     5817432 :             tree retval;
    9625                 :     5817432 :             unsigned i;
    9626                 :    18181830 :             for (i = 0; i < gimple_call_num_args (stmt); ++i)
    9627                 :    12364398 :               args[i] = (*valueize) (gimple_call_arg (stmt, i));
    9628                 :     5817432 :             retval = fold_builtin_call_array (loc,
    9629                 :             :                                          gimple_call_return_type (call_stmt),
    9630                 :             :                                          fn, gimple_call_num_args (stmt), args);
    9631                 :     5817432 :             if (retval)
    9632                 :             :               {
    9633                 :             :                 /* fold_call_expr wraps the result inside a NOP_EXPR.  */
    9634                 :       46075 :                 STRIP_NOPS (retval);
    9635                 :       46075 :                 retval = fold_convert (gimple_call_return_type (call_stmt),
    9636                 :             :                                        retval);
    9637                 :             :               }
    9638                 :     5817432 :             return retval;
    9639                 :             :           }
    9640                 :             :         return NULL_TREE;
    9641                 :             :       }
    9642                 :             : 
    9643                 :             :     default:
    9644                 :             :       return NULL_TREE;
    9645                 :             :     }
    9646                 :             : }
    9647                 :             : 
    9648                 :             : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
    9649                 :             :    Returns NULL_TREE if folding to a constant is not possible, otherwise
    9650                 :             :    returns a constant according to is_gimple_min_invariant.  */
    9651                 :             : 
    9652                 :             : tree
    9653                 :        4334 : gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
    9654                 :             : {
    9655                 :        4334 :   tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
    9656                 :        4334 :   if (res && is_gimple_min_invariant (res))
    9657                 :             :     return res;
    9658                 :             :   return NULL_TREE;
    9659                 :             : }
    9660                 :             : 
    9661                 :             : 
    9662                 :             : /* The following set of functions are supposed to fold references using
    9663                 :             :    their constant initializers.  */
    9664                 :             : 
    9665                 :             : /* See if we can find constructor defining value of BASE.
    9666                 :             :    When we know the consructor with constant offset (such as
    9667                 :             :    base is array[40] and we do know constructor of array), then
    9668                 :             :    BIT_OFFSET is adjusted accordingly.
    9669                 :             : 
    9670                 :             :    As a special case, return error_mark_node when constructor
    9671                 :             :    is not explicitly available, but it is known to be zero
    9672                 :             :    such as 'static const int a;'.  */
    9673                 :             : static tree
    9674                 :   127552234 : get_base_constructor (tree base, poly_int64 *bit_offset,
    9675                 :             :                       tree (*valueize)(tree))
    9676                 :             : {
    9677                 :   127643440 :   poly_int64 bit_offset2, size, max_size;
    9678                 :   127643440 :   bool reverse;
    9679                 :             : 
    9680                 :   127643440 :   if (TREE_CODE (base) == MEM_REF)
    9681                 :             :     {
    9682                 :   136174484 :       poly_offset_int boff = *bit_offset + mem_ref_offset (base) * BITS_PER_UNIT;
    9683                 :    68087242 :       if (!boff.to_shwi (bit_offset))
    9684                 :    67786626 :         return NULL_TREE;
    9685                 :             : 
    9686                 :    68086908 :       if (valueize
    9687                 :    68086908 :           && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
    9688                 :    36733853 :         base = valueize (TREE_OPERAND (base, 0));
    9689                 :    68086908 :       if (!base || TREE_CODE (base) != ADDR_EXPR)
    9690                 :             :         return NULL_TREE;
    9691                 :      300616 :       base = TREE_OPERAND (base, 0);
    9692                 :             :     }
    9693                 :    59556198 :   else if (valueize
    9694                 :    32065412 :            && TREE_CODE (base) == SSA_NAME)
    9695                 :           0 :     base = valueize (base);
    9696                 :             : 
    9697                 :             :   /* Get a CONSTRUCTOR.  If BASE is a VAR_DECL, get its
    9698                 :             :      DECL_INITIAL.  If BASE is a nested reference into another
    9699                 :             :      ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
    9700                 :             :      the inner reference.  */
    9701                 :    59856814 :   switch (TREE_CODE (base))
    9702                 :             :     {
    9703                 :    52465673 :     case VAR_DECL:
    9704                 :    52465673 :     case CONST_DECL:
    9705                 :    52465673 :       {
    9706                 :    52465673 :         tree init = ctor_for_folding (base);
    9707                 :             : 
    9708                 :             :         /* Our semantic is exact opposite of ctor_for_folding;
    9709                 :             :            NULL means unknown, while error_mark_node is 0.  */
    9710                 :    52465673 :         if (init == error_mark_node)
    9711                 :             :           return NULL_TREE;
    9712                 :     1095806 :         if (!init)
    9713                 :        1133 :           return error_mark_node;
    9714                 :             :         return init;
    9715                 :             :       }
    9716                 :             : 
    9717                 :       91206 :     case VIEW_CONVERT_EXPR:
    9718                 :       91206 :       return get_base_constructor (TREE_OPERAND (base, 0),
    9719                 :       91206 :                                    bit_offset, valueize);
    9720                 :             : 
    9721                 :      298354 :     case ARRAY_REF:
    9722                 :      298354 :     case COMPONENT_REF:
    9723                 :      298354 :       base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
    9724                 :             :                                       &reverse);
    9725                 :      298354 :       if (!known_size_p (max_size) || maybe_ne (size, max_size))
    9726                 :             :         return NULL_TREE;
    9727                 :      237581 :       *bit_offset +=  bit_offset2;
    9728                 :      237581 :       return get_base_constructor (base, bit_offset, valueize);
    9729                 :             : 
    9730                 :             :     case CONSTRUCTOR:
    9731                 :             :       return base;
    9732                 :             : 
    9733                 :     7001581 :     default:
    9734                 :     7001581 :       if (CONSTANT_CLASS_P (base))
    9735                 :             :         return base;
    9736                 :             : 
    9737                 :             :       return NULL_TREE;
    9738                 :             :     }
    9739                 :             : }
    9740                 :             : 
    9741                 :             : /* CTOR is a CONSTRUCTOR of an array or vector type.  Fold a reference of SIZE
    9742                 :             :    bits to the memory at bit OFFSET.  If non-null, TYPE is the expected type of
    9743                 :             :    the reference; otherwise the type of the referenced element is used instead.
    9744                 :             :    When SIZE is zero, attempt to fold a reference to the entire element OFFSET
    9745                 :             :    refers to.  Increment *SUBOFF by the bit offset of the accessed element.  */
    9746                 :             : 
    9747                 :             : static tree
    9748                 :      704035 : fold_array_ctor_reference (tree type, tree ctor,
    9749                 :             :                            unsigned HOST_WIDE_INT offset,
    9750                 :             :                            unsigned HOST_WIDE_INT size,
    9751                 :             :                            tree from_decl,
    9752                 :             :                            unsigned HOST_WIDE_INT *suboff)
    9753                 :             : {
    9754                 :      704035 :   offset_int low_bound;
    9755                 :      704035 :   offset_int elt_size;
    9756                 :      704035 :   offset_int access_index;
    9757                 :      704035 :   tree domain_type = NULL_TREE;
    9758                 :      704035 :   HOST_WIDE_INT inner_offset;
    9759                 :             : 
    9760                 :             :   /* Compute low bound and elt size.  */
    9761                 :      704035 :   if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
    9762                 :      704035 :     domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
    9763                 :      704035 :   if (domain_type && TYPE_MIN_VALUE (domain_type))
    9764                 :             :     {
    9765                 :             :       /* Static constructors for variably sized objects make no sense.  */
    9766                 :      704035 :       if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
    9767                 :             :         return NULL_TREE;
    9768                 :      704035 :       low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
    9769                 :             :     }
    9770                 :             :   else
    9771                 :           0 :     low_bound = 0;
    9772                 :             :   /* Static constructors for variably sized objects make no sense.  */
    9773                 :      704035 :   if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
    9774                 :             :     return NULL_TREE;
    9775                 :      704035 :   elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
    9776                 :             : 
    9777                 :             :   /* When TYPE is non-null, verify that it specifies a constant-sized
    9778                 :             :      access of a multiple of the array element size.  Avoid division
    9779                 :             :      by zero below when ELT_SIZE is zero, such as with the result of
    9780                 :             :      an initializer for a zero-length array or an empty struct.  */
    9781                 :      704035 :   if (elt_size == 0
    9782                 :      704035 :       || (type
    9783                 :      703996 :           && (!TYPE_SIZE_UNIT (type)
    9784                 :      703996 :               || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)))
    9785                 :          39 :     return NULL_TREE;
    9786                 :             : 
    9787                 :             :   /* Compute the array index we look for.  */
    9788                 :      703996 :   access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
    9789                 :             :                                  elt_size);
    9790                 :      703996 :   access_index += low_bound;
    9791                 :             : 
    9792                 :             :   /* And offset within the access.  */
    9793                 :      703996 :   inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
    9794                 :             : 
    9795                 :      703996 :   unsigned HOST_WIDE_INT elt_sz = elt_size.to_uhwi ();
    9796                 :      703996 :   if (size > elt_sz * BITS_PER_UNIT)
    9797                 :             :     {
    9798                 :             :       /* native_encode_expr constraints.  */
    9799                 :       47709 :       if (size > MAX_BITSIZE_MODE_ANY_MODE
    9800                 :       46218 :           || size % BITS_PER_UNIT != 0
    9801                 :       46218 :           || inner_offset % BITS_PER_UNIT != 0
    9802                 :       46218 :           || elt_sz > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
    9803                 :             :         return NULL_TREE;
    9804                 :             : 
    9805                 :       46218 :       unsigned ctor_idx;
    9806                 :       46218 :       tree val = get_array_ctor_element_at_index (ctor, access_index,
    9807                 :             :                                                   &ctor_idx);
    9808                 :       46242 :       if (!val && ctor_idx >= CONSTRUCTOR_NELTS (ctor))
    9809                 :          23 :         return build_zero_cst (type);
    9810                 :             : 
    9811                 :             :       /* native-encode adjacent ctor elements.  */
    9812                 :       46195 :       unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
    9813                 :       46195 :       unsigned bufoff = 0;
    9814                 :       46195 :       offset_int index = 0;
    9815                 :       46195 :       offset_int max_index = access_index;
    9816                 :       46195 :       constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
    9817                 :       46195 :       if (!val)
    9818                 :           1 :         val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
    9819                 :       46194 :       else if (!CONSTANT_CLASS_P (val))
    9820                 :             :         return NULL_TREE;
    9821                 :       45537 :       if (!elt->index)
    9822                 :             :         ;
    9823                 :       38731 :       else if (TREE_CODE (elt->index) == RANGE_EXPR)
    9824                 :             :         {
    9825                 :          20 :           index = wi::to_offset (TREE_OPERAND (elt->index, 0));
    9826                 :          20 :           max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
    9827                 :             :         }
    9828                 :             :       else
    9829                 :       38711 :         index = max_index = wi::to_offset (elt->index);
    9830                 :       45537 :       index = wi::umax (index, access_index);
    9831                 :      521614 :       do
    9832                 :             :         {
    9833                 :      521614 :           if (bufoff + elt_sz > sizeof (buf))
    9834                 :           0 :             elt_sz = sizeof (buf) - bufoff;
    9835                 :      521614 :           int len;
    9836                 :      521614 :           if (TREE_CODE (val) == RAW_DATA_CST)
    9837                 :             :             {
    9838                 :          20 :               gcc_assert (inner_offset == 0);
    9839                 :          20 :               if (!elt->index || TREE_CODE (elt->index) != INTEGER_CST)
    9840                 :             :                 return NULL_TREE;
    9841                 :          40 :               inner_offset = (access_index
    9842                 :          20 :                               - wi::to_offset (elt->index)).to_uhwi ();
    9843                 :          20 :               len = MIN (sizeof (buf) - bufoff,
    9844                 :             :                          (unsigned) (RAW_DATA_LENGTH (val) - inner_offset));
    9845                 :          20 :               memcpy (buf + bufoff, RAW_DATA_POINTER (val) + inner_offset,
    9846                 :             :                       len);
    9847                 :          20 :               access_index += len - 1;
    9848                 :             :             }
    9849                 :             :           else
    9850                 :             :             {
    9851                 :     1043188 :               len = native_encode_expr (val, buf + bufoff, elt_sz,
    9852                 :      521594 :                                         inner_offset / BITS_PER_UNIT);
    9853                 :      521594 :               if (len != (int) elt_sz - inner_offset / BITS_PER_UNIT)
    9854                 :             :                 return NULL_TREE;
    9855                 :             :             }
    9856                 :      521614 :           inner_offset = 0;
    9857                 :      521614 :           bufoff += len;
    9858                 :             : 
    9859                 :      521614 :           access_index += 1;
    9860                 :      521614 :           if (wi::cmpu (access_index, index) == 0)
    9861                 :           2 :             val = elt->value;
    9862                 :      521612 :           else if (wi::cmpu (access_index, max_index) > 0)
    9863                 :             :             {
    9864                 :      521372 :               ctor_idx++;
    9865                 :      521372 :               if (ctor_idx >= CONSTRUCTOR_NELTS (ctor))
    9866                 :             :                 {
    9867                 :       41785 :                   val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
    9868                 :       41785 :                   ++max_index;
    9869                 :             :                 }
    9870                 :             :               else
    9871                 :             :                 {
    9872                 :      479587 :                   elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
    9873                 :      479587 :                   index = 0;
    9874                 :      479587 :                   max_index = access_index;
    9875                 :      479587 :                   if (!elt->index)
    9876                 :             :                     ;
    9877                 :      478779 :                   else if (TREE_CODE (elt->index) == RANGE_EXPR)
    9878                 :             :                     {
    9879                 :           0 :                       index = wi::to_offset (TREE_OPERAND (elt->index, 0));
    9880                 :           0 :                       max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
    9881                 :             :                     }
    9882                 :             :                   else
    9883                 :      478779 :                     index = max_index = wi::to_offset (elt->index);
    9884                 :      479587 :                   index = wi::umax (index, access_index);
    9885                 :      479587 :                   if (wi::cmpu (access_index, index) == 0)
    9886                 :      479586 :                     val = elt->value;
    9887                 :             :                   else
    9888                 :           1 :                     val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
    9889                 :             :                 }
    9890                 :             :             }
    9891                 :             :         }
    9892                 :      521614 :       while (bufoff < size / BITS_PER_UNIT);
    9893                 :       45537 :       *suboff += size;
    9894                 :       45537 :       return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
    9895                 :             :     }
    9896                 :             : 
    9897                 :      656287 :   unsigned ctor_idx;
    9898                 :      656287 :   if (tree val = get_array_ctor_element_at_index (ctor, access_index,
    9899                 :             :                                                   &ctor_idx))
    9900                 :             :     {
    9901                 :      654724 :       if (TREE_CODE (val) == RAW_DATA_CST)
    9902                 :             :         {
    9903                 :        1642 :           if (size != BITS_PER_UNIT || elt_sz != 1 || inner_offset != 0)
    9904                 :             :             return NULL_TREE;
    9905                 :        1634 :           constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
    9906                 :        1634 :           if (elt->index == NULL_TREE || TREE_CODE (elt->index) != INTEGER_CST)
    9907                 :             :             return NULL_TREE;
    9908                 :        1634 :           unsigned o = (access_index - wi::to_offset (elt->index)).to_uhwi ();
    9909                 :        1634 :           val = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
    9910                 :             :         }
    9911                 :      654716 :       if (!size && TREE_CODE (val) != CONSTRUCTOR)
    9912                 :             :         {
    9913                 :             :           /* For the final reference to the entire accessed element
    9914                 :             :              (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
    9915                 :             :              may be null) in favor of the type of the element, and set
    9916                 :             :              SIZE to the size of the accessed element.  */
    9917                 :       23018 :           inner_offset = 0;
    9918                 :       23018 :           type = TREE_TYPE (val);
    9919                 :       23018 :           size = elt_sz * BITS_PER_UNIT;
    9920                 :             :         }
    9921                 :     1696939 :       else if (size && access_index < CONSTRUCTOR_NELTS (ctor) - 1
    9922                 :      472272 :                && TREE_CODE (val) == CONSTRUCTOR
    9923                 :        8412 :                && (elt_sz * BITS_PER_UNIT - inner_offset) < size)
    9924                 :             :         /* If this isn't the last element in the CTOR and a CTOR itself
    9925                 :             :            and it does not cover the whole object we are requesting give up
    9926                 :             :            since we're not set up for combining from multiple CTORs.  */
    9927                 :          20 :         return NULL_TREE;
    9928                 :             : 
    9929                 :      654696 :       *suboff += access_index.to_uhwi () * elt_sz * BITS_PER_UNIT;
    9930                 :      654696 :       return fold_ctor_reference (type, val, inner_offset, size, from_decl,
    9931                 :             :                                   suboff);
    9932                 :             :     }
    9933                 :             : 
    9934                 :             :   /* Memory not explicitly mentioned in constructor is 0 (or
    9935                 :             :      the reference is out of range).  */
    9936                 :        1563 :   return type ? build_zero_cst (type) : NULL_TREE;
    9937                 :             : }
    9938                 :             : 
    9939                 :             : /* CTOR is a CONSTRUCTOR of a record or union type.  Fold a reference of SIZE
    9940                 :             :    bits to the memory at bit OFFSET.  If non-null, TYPE is the expected type of
    9941                 :             :    the reference; otherwise the type of the referenced member is used instead.
    9942                 :             :    When SIZE is zero, attempt to fold a reference to the entire member OFFSET
    9943                 :             :    refers to.  Increment *SUBOFF by the bit offset of the accessed member.  */
    9944                 :             : 
    9945                 :             : static tree
    9946                 :       64139 : fold_nonarray_ctor_reference (tree type, tree ctor,
    9947                 :             :                               unsigned HOST_WIDE_INT offset,
    9948                 :             :                               unsigned HOST_WIDE_INT size,
    9949                 :             :                               tree from_decl,
    9950                 :             :                               unsigned HOST_WIDE_INT *suboff)
    9951                 :             : {
    9952                 :       64139 :   unsigned HOST_WIDE_INT cnt;
    9953                 :       64139 :   tree cfield, cval;
    9954                 :             : 
    9955                 :      100620 :   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
    9956                 :             :     {
    9957                 :       91664 :       tree byte_offset = DECL_FIELD_OFFSET (cfield);
    9958                 :       91664 :       tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
    9959                 :       91664 :       tree field_size = DECL_SIZE (cfield);
    9960                 :             : 
    9961                 :       91664 :       if (!field_size)
    9962                 :             :         {
    9963                 :             :           /* Determine the size of the flexible array member from
    9964                 :             :              the size of the initializer provided for it.  */
    9965                 :         847 :           field_size = TYPE_SIZE (TREE_TYPE (cval));
    9966                 :             :         }
    9967                 :             : 
    9968                 :             :       /* Variable sized objects in static constructors makes no sense,
    9969                 :             :          but field_size can be NULL for flexible array members.  */
    9970                 :       91664 :       gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
    9971                 :             :                   && TREE_CODE (byte_offset) == INTEGER_CST
    9972                 :             :                   && (field_size != NULL_TREE
    9973                 :             :                       ? TREE_CODE (field_size) == INTEGER_CST
    9974                 :             :                       : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
    9975                 :             : 
    9976                 :             :       /* Compute bit offset of the field.  */
    9977                 :       91664 :       offset_int bitoffset
    9978                 :       91664 :         = (wi::to_offset (field_offset)
    9979                 :       91664 :            + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
    9980                 :             :       /* Compute bit offset where the field ends.  */
    9981                 :       91664 :       offset_int bitoffset_end;
    9982                 :       91664 :       if (field_size != NULL_TREE)
    9983                 :       91664 :         bitoffset_end = bitoffset + wi::to_offset (field_size);
    9984                 :             :       else
    9985                 :           0 :         bitoffset_end = 0;
    9986                 :             : 
    9987                 :             :       /* Compute the bit offset of the end of the desired access.
    9988                 :             :          As a special case, if the size of the desired access is
    9989                 :             :          zero, assume the access is to the entire field (and let
    9990                 :             :          the caller make any necessary adjustments by storing
    9991                 :             :          the actual bounds of the field in FIELDBOUNDS).  */
    9992                 :       91664 :       offset_int access_end = offset_int (offset);
    9993                 :       91664 :       if (size)
    9994                 :       49245 :         access_end += size;
    9995                 :             :       else
    9996                 :       42419 :         access_end = bitoffset_end;
    9997                 :             : 
    9998                 :             :       /* Is there any overlap between the desired access at
    9999                 :             :          [OFFSET, OFFSET+SIZE) and the offset of the field within
   10000                 :             :          the object at [BITOFFSET, BITOFFSET_END)?  */
   10001                 :       91664 :       if (wi::cmps (access_end, bitoffset) > 0
   10002                 :       91664 :           && (field_size == NULL_TREE
   10003                 :       89333 :               || wi::lts_p (offset, bitoffset_end)))
   10004                 :             :         {
   10005                 :       55183 :           *suboff += bitoffset.to_uhwi ();
   10006                 :             : 
   10007                 :       55183 :           if (!size && TREE_CODE (cval) != CONSTRUCTOR)
   10008                 :             :             {
   10009                 :             :               /* For the final reference to the entire accessed member
   10010                 :             :                  (SIZE is zero), reset OFFSET, disegard TYPE (which may
   10011                 :             :                  be null) in favor of the type of the member, and set
   10012                 :             :                  SIZE to the size of the accessed member.  */
   10013                 :       19759 :               offset = bitoffset.to_uhwi ();
   10014                 :       19759 :               type = TREE_TYPE (cval);
   10015                 :       19759 :               size = (bitoffset_end - bitoffset).to_uhwi ();
   10016                 :             :             }
   10017                 :             : 
   10018                 :             :           /* We do have overlap.  Now see if the field is large enough
   10019                 :             :              to cover the access.  Give up for accesses that extend
   10020                 :             :              beyond the end of the object or that span multiple fields.  */
   10021                 :       55183 :           if (wi::cmps (access_end, bitoffset_end) > 0)
   10022                 :             :             return NULL_TREE;
   10023                 :       54558 :           if (offset < bitoffset)
   10024                 :             :             return NULL_TREE;
   10025                 :             : 
   10026                 :       54558 :           offset_int inner_offset = offset_int (offset) - bitoffset;
   10027                 :             : 
   10028                 :             :           /* Integral bit-fields are left-justified on big-endian targets, so
   10029                 :             :              we must arrange for native_encode_int to start at their MSB.  */
   10030                 :       54558 :           if (DECL_BIT_FIELD (cfield) && INTEGRAL_TYPE_P (TREE_TYPE (cfield)))
   10031                 :             :             {
   10032                 :        1115 :               if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
   10033                 :             :                 return NULL_TREE;
   10034                 :        1115 :               const unsigned int encoding_size
   10035                 :        1115 :                 = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (cfield)));
   10036                 :        1115 :               if (BYTES_BIG_ENDIAN)
   10037                 :             :                 inner_offset += encoding_size - wi::to_offset (field_size);
   10038                 :             :             }
   10039                 :             : 
   10040                 :       54558 :           return fold_ctor_reference (type, cval,
   10041                 :       54558 :                                       inner_offset.to_uhwi (), size,
   10042                 :             :                                       from_decl, suboff);
   10043                 :             :         }
   10044                 :             :     }
   10045                 :             : 
   10046                 :        8956 :   if (!type)
   10047                 :             :     return NULL_TREE;
   10048                 :             : 
   10049                 :        8956 :   return build_zero_cst (type);
   10050                 :             : }
   10051                 :             : 
   10052                 :             : /* CTOR is a value initializing memory.  Fold a reference of TYPE and
   10053                 :             :    bit size POLY_SIZE to the memory at bit POLY_OFFSET.  When POLY_SIZE
   10054                 :             :    is zero, attempt to fold a reference to the entire subobject
   10055                 :             :    which OFFSET refers to.  This is used when folding accesses to
   10056                 :             :    string members of aggregates.  When non-null, set *SUBOFF to
   10057                 :             :    the bit offset of the accessed subobject.  */
   10058                 :             : 
   10059                 :             : tree
   10060                 :     1594880 : fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset,
   10061                 :             :                      const poly_uint64 &poly_size, tree from_decl,
   10062                 :             :                      unsigned HOST_WIDE_INT *suboff /* = NULL */)
   10063                 :             : {
   10064                 :     1594880 :   tree ret;
   10065                 :             : 
   10066                 :             :   /* We found the field with exact match.  */
   10067                 :     1594880 :   if (type
   10068                 :     1594880 :       && useless_type_conversion_p (type, TREE_TYPE (ctor))
   10069                 :     2274631 :       && known_eq (poly_offset, 0U))
   10070                 :      678316 :     return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
   10071                 :             : 
   10072                 :             :   /* The remaining optimizations need a constant size and offset.  */
   10073                 :      916564 :   unsigned HOST_WIDE_INT size, offset;
   10074                 :      916564 :   if (!poly_size.is_constant (&size) || !poly_offset.is_constant (&offset))
   10075                 :             :     return NULL_TREE;
   10076                 :             : 
   10077                 :             :   /* We are at the end of walk, see if we can view convert the
   10078                 :             :      result.  */
   10079                 :      916564 :   if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
   10080                 :             :       /* VIEW_CONVERT_EXPR is defined only for matching sizes.  */
   10081                 :       21962 :       && known_eq (wi::to_poly_widest (TYPE_SIZE (type)), size)
   10082                 :       21855 :       && known_eq (wi::to_poly_widest (TYPE_SIZE (TREE_TYPE (ctor))), size))
   10083                 :             :     {
   10084                 :       13894 :       ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
   10085                 :       13894 :       if (ret)
   10086                 :             :         {
   10087                 :       13894 :           ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
   10088                 :       13894 :           if (ret)
   10089                 :       13834 :             STRIP_USELESS_TYPE_CONVERSION (ret);
   10090                 :             :         }
   10091                 :       13894 :       return ret;
   10092                 :             :     }
   10093                 :             : 
   10094                 :             :   /* For constants and byte-aligned/sized reads, try to go through
   10095                 :             :      native_encode/interpret.  */
   10096                 :      902670 :   if (CONSTANT_CLASS_P (ctor)
   10097                 :             :       && BITS_PER_UNIT == 8
   10098                 :      133692 :       && offset % BITS_PER_UNIT == 0
   10099                 :      133688 :       && offset / BITS_PER_UNIT <= INT_MAX
   10100                 :      133656 :       && size % BITS_PER_UNIT == 0
   10101                 :      133647 :       && size <= MAX_BITSIZE_MODE_ANY_MODE
   10102                 :     1036034 :       && can_native_interpret_type_p (type))
   10103                 :             :     {
   10104                 :       90365 :       unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
   10105                 :      180730 :       int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
   10106                 :       90365 :                                     offset / BITS_PER_UNIT);
   10107                 :       90365 :       if (len > 0)
   10108                 :       89667 :         return native_interpret_expr (type, buf, len);
   10109                 :             :     }
   10110                 :             : 
   10111                 :             :   /* For constructors, try first a recursive local processing, but in any case
   10112                 :             :      this requires the native storage order.  */
   10113                 :      813003 :   if (TREE_CODE (ctor) == CONSTRUCTOR
   10114                 :      813003 :       && !(AGGREGATE_TYPE_P (TREE_TYPE (ctor))
   10115                 :      768404 :            && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (ctor))))
   10116                 :             :     {
   10117                 :      768174 :       unsigned HOST_WIDE_INT dummy = 0;
   10118                 :      768174 :       if (!suboff)
   10119                 :      654541 :         suboff = &dummy;
   10120                 :             : 
   10121                 :      768174 :       tree ret;
   10122                 :      768174 :       if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
   10123                 :      768174 :           || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
   10124                 :      704035 :         ret = fold_array_ctor_reference (type, ctor, offset, size,
   10125                 :             :                                          from_decl, suboff);
   10126                 :             :       else
   10127                 :       64139 :         ret = fold_nonarray_ctor_reference (type, ctor, offset, size,
   10128                 :             :                                             from_decl, suboff);
   10129                 :             : 
   10130                 :             :       /* Otherwise fall back to native_encode_initializer.  This may be done
   10131                 :             :          only from the outermost fold_ctor_reference call (because it itself
   10132                 :             :          recurses into CONSTRUCTORs and doesn't update suboff).  */
   10133                 :      768174 :       if (ret == NULL_TREE
   10134                 :      250178 :           && suboff == &dummy
   10135                 :             :           && BITS_PER_UNIT == 8
   10136                 :      249247 :           && offset % BITS_PER_UNIT == 0
   10137                 :      249245 :           && offset / BITS_PER_UNIT <= INT_MAX
   10138                 :      249245 :           && size % BITS_PER_UNIT == 0
   10139                 :      249236 :           && size <= MAX_BITSIZE_MODE_ANY_MODE
   10140                 :     1015919 :           && can_native_interpret_type_p (type))
   10141                 :             :         {
   10142                 :      207093 :           unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
   10143                 :      414186 :           int len = native_encode_initializer (ctor, buf, size / BITS_PER_UNIT,
   10144                 :      207093 :                                                offset / BITS_PER_UNIT);
   10145                 :      207093 :           if (len > 0)
   10146                 :         837 :             return native_interpret_expr (type, buf, len);
   10147                 :             :         }
   10148                 :             : 
   10149                 :      767337 :       return ret;
   10150                 :             :     }
   10151                 :             : 
   10152                 :             :   return NULL_TREE;
   10153                 :             : }
   10154                 :             : 
   10155                 :             : /* Return the tree representing the element referenced by T if T is an
   10156                 :             :    ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
   10157                 :             :    names using VALUEIZE.  Return NULL_TREE otherwise.  */
   10158                 :             : 
   10159                 :             : tree
   10160                 :   133015380 : fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
   10161                 :             : {
   10162                 :   133015380 :   tree ctor, idx, base;
   10163                 :   133015380 :   poly_int64 offset, size, max_size;
   10164                 :   133015380 :   tree tem;
   10165                 :   133015380 :   bool reverse;
   10166                 :             : 
   10167                 :   133015380 :   if (TREE_THIS_VOLATILE (t))
   10168                 :             :     return NULL_TREE;
   10169                 :             : 
   10170                 :   132703164 :   if (DECL_P (t))
   10171                 :      255654 :     return get_symbol_constant_value (t);
   10172                 :             : 
   10173                 :   132447510 :   tem = fold_read_from_constant_string (t);
   10174                 :   132447510 :   if (tem)
   10175                 :             :     return tem;
   10176                 :             : 
   10177                 :   132444531 :   switch (TREE_CODE (t))
   10178                 :             :     {
   10179                 :     9973071 :     case ARRAY_REF:
   10180                 :     9973071 :     case ARRAY_RANGE_REF:
   10181                 :             :       /* Constant indexes are handled well by get_base_constructor.
   10182                 :             :          Only special case variable offsets.
   10183                 :             :          FIXME: This code can't handle nested references with variable indexes
   10184                 :             :          (they will be handled only by iteration of ccp).  Perhaps we can bring
   10185                 :             :          get_ref_base_and_extent here and make it use a valueize callback.  */
   10186                 :     9973071 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
   10187                 :     5805805 :           && valueize
   10188                 :     3835087 :           && (idx = (*valueize) (TREE_OPERAND (t, 1)))
   10189                 :    13808158 :           && poly_int_tree_p (idx))
   10190                 :             :         {
   10191                 :     1566679 :           tree low_bound, unit_size;
   10192                 :             : 
   10193                 :             :           /* If the resulting bit-offset is constant, track it.  */
   10194                 :     1566679 :           if ((low_bound = array_ref_low_bound (t),
   10195                 :     1566679 :                poly_int_tree_p (low_bound))
   10196                 :     1566679 :               && (unit_size = array_ref_element_size (t),
   10197                 :     1566679 :                   tree_fits_uhwi_p (unit_size)))
   10198                 :             :             {
   10199                 :     1566679 :               poly_offset_int woffset
   10200                 :     1566679 :                 = wi::sext (wi::to_poly_offset (idx)
   10201                 :     3133358 :                             - wi::to_poly_offset (low_bound),
   10202                 :     1566679 :                             TYPE_PRECISION (sizetype));
   10203                 :     1566679 :               woffset *= tree_to_uhwi (unit_size);
   10204                 :     1566679 :               woffset *= BITS_PER_UNIT;
   10205                 :     1566679 :               if (woffset.to_shwi (&offset))
   10206                 :             :                 {
   10207                 :     1566577 :                   base = TREE_OPERAND (t, 0);
   10208                 :     1566577 :                   ctor = get_base_constructor (base, &offset, valueize);
   10209                 :             :                   /* Empty constructor.  Always fold to 0.  */
   10210                 :     1566577 :                   if (ctor == error_mark_node)
   10211                 :     1566577 :                     return build_zero_cst (TREE_TYPE (t));
   10212                 :             :                   /* Out of bound array access.  Value is undefined,
   10213                 :             :                      but don't fold.  */
   10214                 :     1566500 :                   if (maybe_lt (offset, 0))
   10215                 :             :                     return NULL_TREE;
   10216                 :             :                   /* We cannot determine ctor.  */
   10217                 :     1565993 :                   if (!ctor)
   10218                 :             :                     return NULL_TREE;
   10219                 :      168127 :                   return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
   10220                 :      168127 :                                               tree_to_uhwi (unit_size)
   10221                 :      168127 :                                               * BITS_PER_UNIT,
   10222                 :             :                                               base);
   10223                 :             :                 }
   10224                 :             :             }
   10225                 :             :         }
   10226                 :             :       /* Fallthru.  */
   10227                 :             : 
   10228                 :   125748076 :     case COMPONENT_REF:
   10229                 :   125748076 :     case BIT_FIELD_REF:
   10230                 :   125748076 :     case TARGET_MEM_REF:
   10231                 :   125748076 :     case MEM_REF:
   10232                 :   125748076 :       base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
   10233                 :   125748076 :       ctor = get_base_constructor (base, &offset, valueize);
   10234                 :             : 
   10235                 :             :       /* Empty constructor.  Always fold to 0.  */
   10236                 :   125748076 :       if (ctor == error_mark_node)
   10237                 :        1056 :         return build_zero_cst (TREE_TYPE (t));
   10238                 :             :       /* We do not know precise address.  */
   10239                 :   125747020 :       if (!known_size_p (max_size) || maybe_ne (max_size, size))
   10240                 :             :         return NULL_TREE;
   10241                 :             :       /* We cannot determine ctor.  */
   10242                 :   118374465 :       if (!ctor)
   10243                 :             :         return NULL_TREE;
   10244                 :             : 
   10245                 :             :       /* Out of bound array access.  Value is undefined, but don't fold.  */
   10246                 :      543871 :       if (maybe_lt (offset, 0))
   10247                 :             :         return NULL_TREE;
   10248                 :             : 
   10249                 :      541260 :       tem = fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, base);
   10250                 :      541260 :       if (tem)
   10251                 :             :         return tem;
   10252                 :             : 
   10253                 :             :       /* For bit field reads try to read the representative and
   10254                 :             :          adjust.  */
   10255                 :      249025 :       if (TREE_CODE (t) == COMPONENT_REF
   10256                 :         145 :           && DECL_BIT_FIELD (TREE_OPERAND (t, 1))
   10257                 :      249109 :           && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)))
   10258                 :             :         {
   10259                 :          84 :           HOST_WIDE_INT csize, coffset;
   10260                 :          84 :           tree field = TREE_OPERAND (t, 1);
   10261                 :          84 :           tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
   10262                 :         168 :           if (INTEGRAL_TYPE_P (TREE_TYPE (repr))
   10263                 :          83 :               && size.is_constant (&csize)
   10264                 :          83 :               && offset.is_constant (&coffset)
   10265                 :          83 :               && (coffset % BITS_PER_UNIT != 0
   10266                 :          81 :                   || csize % BITS_PER_UNIT != 0)
   10267                 :          10 :               && !reverse
   10268                 :          84 :               && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
   10269                 :             :             {
   10270                 :          10 :               poly_int64 bitoffset;
   10271                 :          10 :               poly_uint64 field_offset, repr_offset;
   10272                 :          10 :               if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
   10273                 :          20 :                   && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
   10274                 :          10 :                 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
   10275                 :             :               else
   10276                 :             :                 bitoffset = 0;
   10277                 :          10 :               bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
   10278                 :          10 :                             - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
   10279                 :          10 :               HOST_WIDE_INT bitoff;
   10280                 :          10 :               int diff = (TYPE_PRECISION (TREE_TYPE (repr))
   10281                 :          10 :                           - TYPE_PRECISION (TREE_TYPE (field)));
   10282                 :          10 :               if (bitoffset.is_constant (&bitoff)
   10283                 :          10 :                   && bitoff >= 0
   10284                 :          10 :                   && bitoff <= diff)
   10285                 :             :                 {
   10286                 :          10 :                   offset -= bitoff;
   10287                 :          10 :                   size = tree_to_uhwi (DECL_SIZE (repr));
   10288                 :             : 
   10289                 :          10 :                   tem = fold_ctor_reference (TREE_TYPE (repr), ctor, offset,
   10290                 :             :                                              size, base);
   10291                 :          10 :                   if (tem && TREE_CODE (tem) == INTEGER_CST)
   10292                 :             :                     {
   10293                 :          10 :                       if (!BYTES_BIG_ENDIAN)
   10294                 :          10 :                         tem = wide_int_to_tree (TREE_TYPE (field),
   10295                 :          10 :                                                 wi::lrshift (wi::to_wide (tem),
   10296                 :             :                                                              bitoff));
   10297                 :             :                       else
   10298                 :             :                         tem = wide_int_to_tree (TREE_TYPE (field),
   10299                 :             :                                                 wi::lrshift (wi::to_wide (tem),
   10300                 :             :                                                              diff - bitoff));
   10301                 :          10 :                       return tem;
   10302                 :             :                     }
   10303                 :             :                 }
   10304                 :             :             }
   10305                 :             :         }
   10306                 :             :       break;
   10307                 :             : 
   10308                 :     1592242 :     case REALPART_EXPR:
   10309                 :     1592242 :     case IMAGPART_EXPR:
   10310                 :     1592242 :       {
   10311                 :     1592242 :         tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
   10312                 :     1592242 :         if (c && TREE_CODE (c) == COMPLEX_CST)
   10313                 :        2674 :           return fold_build1_loc (EXPR_LOCATION (t),
   10314                 :        5348 :                                   TREE_CODE (t), TREE_TYPE (t), c);
   10315                 :             :         break;
   10316                 :             :       }
   10317                 :             : 
   10318                 :             :     default:
   10319                 :             :       break;
   10320                 :             :     }
   10321                 :             : 
   10322                 :             :   return NULL_TREE;
   10323                 :             : }
   10324                 :             : 
   10325                 :             : tree
   10326                 :    62193087 : fold_const_aggregate_ref (tree t)
   10327                 :             : {
   10328                 :    62193087 :   return fold_const_aggregate_ref_1 (t, NULL);
   10329                 :             : }
   10330                 :             : 
   10331                 :             : /* Lookup virtual method with index TOKEN in a virtual table V
   10332                 :             :    at OFFSET.
   10333                 :             :    Set CAN_REFER if non-NULL to false if method
   10334                 :             :    is not referable or if the virtual table is ill-formed (such as rewriten
   10335                 :             :    by non-C++ produced symbol). Otherwise just return NULL in that calse.  */
   10336                 :             : 
   10337                 :             : tree
   10338                 :      368740 : gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
   10339                 :             :                                    tree v,
   10340                 :             :                                    unsigned HOST_WIDE_INT offset,
   10341                 :             :                                    bool *can_refer)
   10342                 :             : {
   10343                 :      368740 :   tree vtable = v, init, fn;
   10344                 :      368740 :   unsigned HOST_WIDE_INT size;
   10345                 :      368740 :   unsigned HOST_WIDE_INT elt_size, access_index;
   10346                 :      368740 :   tree domain_type;
   10347                 :             : 
   10348                 :      368740 :   if (can_refer)
   10349                 :      368740 :     *can_refer = true;
   10350                 :             : 
   10351                 :             :   /* First of all double check we have virtual table.  */
   10352                 :      368740 :   if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
   10353                 :             :     {
   10354                 :             :       /* Pass down that we lost track of the target.  */
   10355                 :           0 :       if (can_refer)
   10356                 :           0 :         *can_refer = false;
   10357                 :           0 :       return NULL_TREE;
   10358                 :             :     }
   10359                 :             : 
   10360                 :      368740 :   init = ctor_for_folding (v);
   10361                 :             : 
   10362                 :             :   /* The virtual tables should always be born with constructors
   10363                 :             :      and we always should assume that they are avaialble for
   10364                 :             :      folding.  At the moment we do not stream them in all cases,
   10365                 :             :      but it should never happen that ctor seem unreachable.  */
   10366                 :      368740 :   gcc_assert (init);
   10367                 :      368740 :   if (init == error_mark_node)
   10368                 :             :     {
   10369                 :             :       /* Pass down that we lost track of the target.  */
   10370                 :         176 :       if (can_refer)
   10371                 :         176 :         *can_refer = false;
   10372                 :         176 :       return NULL_TREE;
   10373                 :             :     }
   10374                 :      368564 :   gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
   10375                 :      368564 :   size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
   10376                 :      368564 :   offset *= BITS_PER_UNIT;
   10377                 :      368564 :   offset += token * size;
   10378                 :             : 
   10379                 :             :   /* Lookup the value in the constructor that is assumed to be array.
   10380                 :             :      This is equivalent to
   10381                 :             :      fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
   10382                 :             :                                offset, size, NULL);
   10383                 :             :      but in a constant time.  We expect that frontend produced a simple
   10384                 :             :      array without indexed initializers.  */
   10385                 :             : 
   10386                 :      368564 :   gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
   10387                 :      368564 :   domain_type = TYPE_DOMAIN (TREE_TYPE (init));
   10388                 :      368564 :   gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
   10389                 :      368564 :   elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
   10390                 :             : 
   10391                 :      368564 :   access_index = offset / BITS_PER_UNIT / elt_size;
   10392                 :      368564 :   gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
   10393                 :             : 
   10394                 :             :   /* The C++ FE can now produce indexed fields, and we check if the indexes
   10395                 :             :      match.  */
   10396                 :      368564 :   if (access_index < CONSTRUCTOR_NELTS (init))
   10397                 :             :     {
   10398                 :      368563 :       fn = CONSTRUCTOR_ELT (init, access_index)->value;
   10399                 :      368563 :       tree idx = CONSTRUCTOR_ELT (init, access_index)->index;
   10400                 :      368563 :       gcc_checking_assert (!idx || tree_to_uhwi (idx) == access_index);
   10401                 :      368563 :       STRIP_NOPS (fn);
   10402                 :             :     }
   10403                 :             :   else
   10404                 :             :     fn = NULL;
   10405                 :             : 
   10406                 :             :   /* For type inconsistent program we may end up looking up virtual method
   10407                 :             :      in virtual table that does not contain TOKEN entries.  We may overrun
   10408                 :             :      the virtual table and pick up a constant or RTTI info pointer.
   10409                 :             :      In any case the call is undefined.  */
   10410                 :      368563 :   if (!fn
   10411                 :      368563 :       || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
   10412                 :      730716 :       || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
   10413                 :        6411 :     fn = builtin_decl_unreachable ();
   10414                 :             :   else
   10415                 :             :     {
   10416                 :      362153 :       fn = TREE_OPERAND (fn, 0);
   10417                 :             : 
   10418                 :             :       /* When cgraph node is missing and function is not public, we cannot
   10419                 :             :          devirtualize.  This can happen in WHOPR when the actual method
   10420                 :             :          ends up in other partition, because we found devirtualization
   10421                 :             :          possibility too late.  */
   10422                 :      362153 :       if (!can_refer_decl_in_current_unit_p (fn, vtable))
   10423                 :             :         {
   10424                 :       52486 :           if (can_refer)
   10425                 :             :             {
   10426                 :       52486 :               *can_refer = false;
   10427                 :       52486 :               return fn;
   10428                 :             :             }
   10429                 :             :           return NULL_TREE;
   10430                 :             :         }
   10431                 :             :     }
   10432                 :             : 
   10433                 :             :   /* Make sure we create a cgraph node for functions we'll reference.
   10434                 :             :      They can be non-existent if the reference comes from an entry
   10435                 :             :      of an external vtable for example.  */
   10436                 :      316078 :   cgraph_node::get_create (fn);
   10437                 :             : 
   10438                 :      316078 :   return fn;
   10439                 :             : }
   10440                 :             : 
   10441                 :             : /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
   10442                 :             :    is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
   10443                 :             :    KNOWN_BINFO carries the binfo describing the true type of
   10444                 :             :    OBJ_TYPE_REF_OBJECT(REF).
   10445                 :             :    Set CAN_REFER if non-NULL to false if method
   10446                 :             :    is not referable or if the virtual table is ill-formed (such as rewriten
   10447                 :             :    by non-C++ produced symbol). Otherwise just return NULL in that calse.  */
   10448                 :             : 
   10449                 :             : tree
   10450                 :      347440 : gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
   10451                 :             :                                   bool *can_refer)
   10452                 :             : {
   10453                 :      347440 :   unsigned HOST_WIDE_INT offset;
   10454                 :      347440 :   tree v;
   10455                 :             : 
   10456                 :      347440 :   v = BINFO_VTABLE (known_binfo);
   10457                 :             :   /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone.  */
   10458                 :      347440 :   if (!v)
   10459                 :             :     return NULL_TREE;
   10460                 :             : 
   10461                 :      347440 :   if (!vtable_pointer_value_to_vtable (v, &v, &offset))
   10462                 :             :     {
   10463                 :           0 :       if (can_refer)
   10464                 :           0 :         *can_refer = false;
   10465                 :           0 :       return NULL_TREE;
   10466                 :             :     }
   10467                 :      347440 :   return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
   10468                 :             : }
   10469                 :             : 
   10470                 :             : /* Given a pointer value T, return a simplified version of an
   10471                 :             :    indirection through T, or NULL_TREE if no simplification is
   10472                 :             :    possible.  Note that the resulting type may be different from
   10473                 :             :    the type pointed to in the sense that it is still compatible
   10474                 :             :    from the langhooks point of view. */
   10475                 :             : 
   10476                 :             : tree
   10477                 :     1921446 : gimple_fold_indirect_ref (tree t)
   10478                 :             : {
   10479                 :     1921446 :   tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
   10480                 :     1921446 :   tree sub = t;
   10481                 :     1921446 :   tree subtype;
   10482                 :             : 
   10483                 :     1921446 :   STRIP_NOPS (sub);
   10484                 :     1921446 :   subtype = TREE_TYPE (sub);
   10485                 :     1921446 :   if (!POINTER_TYPE_P (subtype)
   10486                 :     1921446 :       || TYPE_REF_CAN_ALIAS_ALL (ptype))
   10487                 :             :     return NULL_TREE;
   10488                 :             : 
   10489                 :     1919727 :   if (TREE_CODE (sub) == ADDR_EXPR)
   10490                 :             :     {
   10491                 :      101356 :       tree op = TREE_OPERAND (sub, 0);
   10492                 :      101356 :       tree optype = TREE_TYPE (op);
   10493                 :             :       /* *&p => p */
   10494                 :      101356 :       if (useless_type_conversion_p (type, optype))
   10495                 :             :         return op;
   10496                 :             : 
   10497                 :             :       /* *(foo *)&fooarray => fooarray[0] */
   10498                 :         912 :       if (TREE_CODE (optype) == ARRAY_TYPE
   10499                 :         321 :           && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
   10500                 :        1233 :           && useless_type_conversion_p (type, TREE_TYPE (optype)))
   10501                 :             :        {
   10502                 :          52 :          tree type_domain = TYPE_DOMAIN (optype);
   10503                 :          52 :          tree min_val = size_zero_node;
   10504                 :          52 :          if (type_domain && TYPE_MIN_VALUE (type_domain))
   10505                 :          52 :            min_val = TYPE_MIN_VALUE (type_domain);
   10506                 :          52 :          if (TREE_CODE (min_val) == INTEGER_CST)
   10507                 :          52 :            return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
   10508                 :             :        }
   10509                 :             :       /* *(foo *)&complexfoo => __real__ complexfoo */
   10510                 :         860 :       else if (TREE_CODE (optype) == COMPLEX_TYPE
   10511                 :         860 :                && useless_type_conversion_p (type, TREE_TYPE (optype)))
   10512                 :           4 :         return fold_build1 (REALPART_EXPR, type, op);
   10513                 :             :       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
   10514                 :         856 :       else if (TREE_CODE (optype) == VECTOR_TYPE
   10515                 :         856 :                && useless_type_conversion_p (type, TREE_TYPE (optype)))
   10516                 :             :         {
   10517                 :          26 :           tree part_width = TYPE_SIZE (type);
   10518                 :          26 :           tree index = bitsize_int (0);
   10519                 :          26 :           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
   10520                 :             :         }
   10521                 :             :     }
   10522                 :             : 
   10523                 :             :   /* *(p + CST) -> ...  */
   10524                 :     1819201 :   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
   10525                 :     1819201 :       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
   10526                 :             :     {
   10527                 :       34195 :       tree addr = TREE_OPERAND (sub, 0);
   10528                 :       34195 :       tree off = TREE_OPERAND (sub, 1);
   10529                 :       34195 :       tree addrtype;
   10530                 :             : 
   10531                 :       34195 :       STRIP_NOPS (addr);
   10532                 :       34195 :       addrtype = TREE_TYPE (addr);
   10533                 :             : 
   10534                 :             :       /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
   10535                 :       34195 :       if (TREE_CODE (addr) == ADDR_EXPR
   10536                 :          92 :           && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
   10537                 :          39 :           && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
   10538                 :       34223 :           && tree_fits_uhwi_p (off))
   10539                 :             :         {
   10540                 :          28 :           unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
   10541                 :          28 :           tree part_width = TYPE_SIZE (type);
   10542                 :          28 :           unsigned HOST_WIDE_INT part_widthi
   10543                 :          28 :             = tree_to_shwi (part_width) / BITS_PER_UNIT;
   10544                 :          28 :           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
   10545                 :          28 :           tree index = bitsize_int (indexi);
   10546                 :          28 :           if (known_lt (offset / part_widthi,
   10547                 :             :                         TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))))
   10548                 :          28 :             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
   10549                 :             :                                 part_width, index);
   10550                 :             :         }
   10551                 :             : 
   10552                 :             :       /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
   10553                 :       34167 :       if (TREE_CODE (addr) == ADDR_EXPR
   10554                 :          64 :           && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
   10555                 :       34168 :           && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
   10556                 :             :         {
   10557                 :           1 :           tree size = TYPE_SIZE_UNIT (type);
   10558                 :           1 :           if (tree_int_cst_equal (size, off))
   10559                 :           1 :             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
   10560                 :             :         }
   10561                 :             : 
   10562                 :             :       /* *(p + CST) -> MEM_REF <p, CST>.  */
   10563                 :       34166 :       if (TREE_CODE (addr) != ADDR_EXPR
   10564                 :       34166 :           || DECL_P (TREE_OPERAND (addr, 0)))
   10565                 :       34148 :         return fold_build2 (MEM_REF, type,
   10566                 :             :                             addr,
   10567                 :             :                             wide_int_to_tree (ptype, wi::to_wide (off)));
   10568                 :             :     }
   10569                 :             : 
   10570                 :             :   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
   10571                 :     1785024 :   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
   10572                 :        1958 :       && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
   10573                 :     1786964 :       && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
   10574                 :             :     {
   10575                 :           1 :       tree type_domain;
   10576                 :           1 :       tree min_val = size_zero_node;
   10577                 :           1 :       tree osub = sub;
   10578                 :           1 :       sub = gimple_fold_indirect_ref (sub);
   10579                 :           1 :       if (! sub)
   10580                 :           1 :         sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
   10581                 :           1 :       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
   10582                 :           1 :       if (type_domain && TYPE_MIN_VALUE (type_domain))
   10583                 :           1 :         min_val = TYPE_MIN_VALUE (type_domain);
   10584                 :           1 :       if (TREE_CODE (min_val) == INTEGER_CST)
   10585                 :           1 :         return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
   10586                 :             :     }
   10587                 :             : 
   10588                 :             :   return NULL_TREE;
   10589                 :             : }
   10590                 :             : 
   10591                 :             : /* Return true if CODE is an operation that when operating on signed
   10592                 :             :    integer types involves undefined behavior on overflow and the
   10593                 :             :    operation can be expressed with unsigned arithmetic.  */
   10594                 :             : 
   10595                 :             : static bool
   10596                 :      281455 : arith_code_with_undefined_signed_overflow (tree_code code)
   10597                 :             : {
   10598                 :           0 :   switch (code)
   10599                 :             :     {
   10600                 :             :     case ABS_EXPR:
   10601                 :             :     case PLUS_EXPR:
   10602                 :             :     case MINUS_EXPR:
   10603                 :             :     case MULT_EXPR:
   10604                 :             :     case NEGATE_EXPR:
   10605                 :             :     case POINTER_PLUS_EXPR:
   10606                 :             :       return true;
   10607                 :           0 :     default:
   10608                 :           0 :       return false;
   10609                 :             :     }
   10610                 :             : }
   10611                 :             : 
   10612                 :             : /* Return true if STMT has an operation that operates on a signed
   10613                 :             :    integer types involves undefined behavior on overflow and the
   10614                 :             :    operation can be expressed with unsigned arithmetic.
   10615                 :             :    Also returns true if STMT is a VCE that needs to be rewritten
   10616                 :             :    if moved to be executed unconditionally.   */
   10617                 :             : 
   10618                 :             : bool
   10619                 :     1028891 : gimple_needing_rewrite_undefined (gimple *stmt)
   10620                 :             : {
   10621                 :     1028891 :   if (!is_gimple_assign (stmt))
   10622                 :             :     return false;
   10623                 :      864232 :   tree lhs = gimple_assign_lhs (stmt);
   10624                 :      864232 :   if (!lhs)
   10625                 :             :     return false;
   10626                 :      864232 :   tree lhs_type = TREE_TYPE (lhs);
   10627                 :      864232 :   if (!INTEGRAL_TYPE_P (lhs_type)
   10628                 :      103663 :       && !POINTER_TYPE_P (lhs_type))
   10629                 :             :     return false;
   10630                 :      827846 :   tree rhs = gimple_assign_rhs1 (stmt);
   10631                 :             :   /* VCE from integral types to a integral types but with
   10632                 :             :      a smaller precision need to be changed into casts
   10633                 :             :      to be well defined. */
   10634                 :      827846 :   if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
   10635                 :         190 :       && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
   10636                 :         167 :       && is_gimple_val (TREE_OPERAND (rhs, 0))
   10637                 :      828013 :       && TYPE_PRECISION (lhs_type)
   10638                 :         167 :           < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (rhs, 0))))
   10639                 :             :     return true;
   10640                 :      827679 :   if (!TYPE_OVERFLOW_UNDEFINED (lhs_type))
   10641                 :             :     return false;
   10642                 :      281455 :   if (!arith_code_with_undefined_signed_overflow
   10643                 :     1028724 :         (gimple_assign_rhs_code (stmt)))
   10644                 :             :     return false;
   10645                 :             :   return true;
   10646                 :             : }
   10647                 :             : 
   10648                 :             : /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
   10649                 :             :    operation that can be transformed to unsigned arithmetic by converting
   10650                 :             :    its operand, carrying out the operation in the corresponding unsigned
   10651                 :             :    type and converting the result back to the original type.
   10652                 :             : 
   10653                 :             :    If IN_PLACE is true, *GSI points to STMT, adjust the stmt in place and
   10654                 :             :    return NULL.
   10655                 :             :    Otherwise returns a sequence of statements that replace STMT and also
   10656                 :             :    contain a modified form of STMT itself.  */
   10657                 :             : 
   10658                 :             : static gimple_seq
   10659                 :       32664 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi, gimple *stmt,
   10660                 :             :                                   bool in_place)
   10661                 :             : {
   10662                 :       32664 :   gcc_assert (gimple_needing_rewrite_undefined (stmt));
   10663                 :       32664 :   if (dump_file && (dump_flags & TDF_DETAILS))
   10664                 :             :     {
   10665                 :          23 :       fprintf (dump_file, "rewriting stmt for being uncondtional defined");
   10666                 :          23 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
   10667                 :             :     }
   10668                 :       32664 :   gimple_seq stmts = NULL;
   10669                 :             :   /* VCE from integral types to another integral types but with
   10670                 :             :      smaller precisions need to be changed into casts
   10671                 :             :      to be well defined. */
   10672                 :       32664 :   if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
   10673                 :             :     {
   10674                 :          60 :       tree rhs = gimple_assign_rhs1 (stmt);
   10675                 :          60 :       tree new_rhs = TREE_OPERAND (rhs, 0);
   10676                 :          60 :       gcc_assert (TYPE_PRECISION (TREE_TYPE (rhs))
   10677                 :             :                   < TYPE_PRECISION (TREE_TYPE (new_rhs)));
   10678                 :          60 :       gcc_assert (is_gimple_val (new_rhs));
   10679                 :          60 :       gimple_assign_set_rhs_code (stmt, NOP_EXPR);
   10680                 :          60 :       gimple_assign_set_rhs1 (stmt, new_rhs);
   10681                 :          60 :       if (in_place)
   10682                 :          51 :           update_stmt (stmt);
   10683                 :             :       else
   10684                 :             :         {
   10685                 :           9 :           gimple_set_modified (stmt, true);
   10686                 :           9 :           gimple_seq_add_stmt (&stmts, stmt);
   10687                 :             :         }
   10688                 :          60 :       return stmts;
   10689                 :             :     }
   10690                 :       32604 :   tree lhs = gimple_assign_lhs (stmt);
   10691                 :       32604 :   tree type = unsigned_type_for (TREE_TYPE (lhs));
   10692                 :       32604 :   if (gimple_assign_rhs_code (stmt) == ABS_EXPR)
   10693                 :          17 :     gimple_assign_set_rhs_code (stmt, ABSU_EXPR);
   10694                 :             :   else
   10695                 :       96671 :     for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
   10696                 :             :       {
   10697                 :       64084 :         tree op = gimple_op (stmt, i);
   10698                 :       64084 :         op = gimple_convert (&stmts, type, op);
   10699                 :       64084 :         gimple_set_op (stmt, i, op);
   10700                 :             :       }
   10701                 :       32604 :   gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
   10702                 :       32604 :   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
   10703                 :        6478 :     gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
   10704                 :       32604 :   gimple_set_modified (stmt, true);
   10705                 :       32604 :   if (in_place)
   10706                 :             :     {
   10707                 :       10916 :       if (stmts)
   10708                 :       10901 :         gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
   10709                 :       10916 :       stmts = NULL;
   10710                 :             :     }
   10711                 :             :   else
   10712                 :       21688 :     gimple_seq_add_stmt (&stmts, stmt);
   10713                 :       32604 :   gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
   10714                 :       32604 :   if (in_place)
   10715                 :             :     {
   10716                 :       10916 :       gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
   10717                 :       10916 :       update_stmt (stmt);
   10718                 :             :     }
   10719                 :             :   else
   10720                 :       21688 :     gimple_seq_add_stmt (&stmts, cvt);
   10721                 :             : 
   10722                 :       32604 :   return stmts;
   10723                 :             : }
   10724                 :             : 
   10725                 :             : void
   10726                 :       10967 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi)
   10727                 :             : {
   10728                 :       10967 :   rewrite_to_defined_unconditional (gsi, gsi_stmt (*gsi), true);
   10729                 :       10967 : }
   10730                 :             : 
   10731                 :             : gimple_seq
   10732                 :       21697 : rewrite_to_defined_unconditional (gimple *stmt)
   10733                 :             : {
   10734                 :       21697 :   return rewrite_to_defined_unconditional (nullptr, stmt, false);
   10735                 :             : }
   10736                 :             : 
   10737                 :             : /* The valueization hook we use for the gimple_build API simplification.
   10738                 :             :    This makes us match fold_buildN behavior by only combining with
   10739                 :             :    statements in the sequence(s) we are currently building.  */
   10740                 :             : 
   10741                 :             : static tree
   10742                 :    17654554 : gimple_build_valueize (tree op)
   10743                 :             : {
   10744                 :    17654554 :   if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
   10745                 :     3909578 :     return op;
   10746                 :             :   return NULL_TREE;
   10747                 :             : }
   10748                 :             : 
   10749                 :             : /* Helper for gimple_build to perform the final insertion of stmts on SEQ.  */
   10750                 :             : 
   10751                 :             : static inline void
   10752                 :     1242710 : gimple_build_insert_seq (gimple_stmt_iterator *gsi,
   10753                 :             :                          bool before, gsi_iterator_update update,
   10754                 :             :                          gimple_seq seq)
   10755                 :             : {
   10756                 :     1242710 :   if (before)
   10757                 :             :     {
   10758                 :       57686 :       if (gsi->bb)
   10759                 :       57686 :         gsi_insert_seq_before (gsi, seq, update);
   10760                 :             :       else
   10761                 :           0 :         gsi_insert_seq_before_without_update (gsi, seq, update);
   10762                 :             :     }
   10763                 :             :   else
   10764                 :             :     {
   10765                 :     1185024 :       if (gsi->bb)
   10766                 :          23 :         gsi_insert_seq_after (gsi, seq, update);
   10767                 :             :       else
   10768                 :     1185001 :         gsi_insert_seq_after_without_update (gsi, seq, update);
   10769                 :             :     }
   10770                 :     1242710 : }
   10771                 :             : 
   10772                 :             : /* Build the expression CODE OP0 of type TYPE with location LOC,
   10773                 :             :    simplifying it first if possible.  Returns the built
   10774                 :             :    expression value and inserts statements possibly defining it
   10775                 :             :    before GSI if BEFORE is true or after GSI if false and advance
   10776                 :             :    the iterator accordingly.
   10777                 :             :    If gsi refers to a basic block simplifying is allowed to look
   10778                 :             :    at all SSA defs while when it does not it is restricted to
   10779                 :             :    SSA defs that are not associated with a basic block yet,
   10780                 :             :    indicating they belong to the currently building sequence.  */
   10781                 :             : 
   10782                 :             : tree
   10783                 :      247781 : gimple_build (gimple_stmt_iterator *gsi,
   10784                 :             :               bool before, gsi_iterator_update update,
   10785                 :             :               location_t loc, enum tree_code code, tree type, tree op0)
   10786                 :             : {
   10787                 :      247781 :   gimple_seq seq = NULL;
   10788                 :      247781 :   tree res
   10789                 :      247781 :     = gimple_simplify (code, type, op0, &seq,
   10790                 :      247781 :                        gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   10791                 :      247781 :   if (!res)
   10792                 :             :     {
   10793                 :      226691 :       res = create_tmp_reg_or_ssa_name (type);
   10794                 :      226691 :       gimple *stmt;
   10795                 :      226691 :       if (code == REALPART_EXPR
   10796                 :             :           || code == IMAGPART_EXPR
   10797                 :      226691 :           || code == VIEW_CONVERT_EXPR)
   10798                 :       12158 :         stmt = gimple_build_assign (res, code, build1 (code, type, op0));
   10799                 :             :       else
   10800                 :      214533 :         stmt = gimple_build_assign (res, code, op0);
   10801                 :      226691 :       gimple_set_location (stmt, loc);
   10802                 :      226691 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   10803                 :             :     }
   10804                 :      247781 :   gimple_build_insert_seq (gsi, before, update, seq);
   10805                 :      247781 :   return res;
   10806                 :             : }
   10807                 :             : 
   10808                 :             : /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
   10809                 :             :    simplifying it first if possible.  Returns the built
   10810                 :             :    expression value inserting any new statements at GSI honoring BEFORE
   10811                 :             :    and UPDATE.  */
   10812                 :             : 
   10813                 :             : tree
   10814                 :      744277 : gimple_build (gimple_stmt_iterator *gsi,
   10815                 :             :               bool before, gsi_iterator_update update,
   10816                 :             :               location_t loc, enum tree_code code, tree type,
   10817                 :             :               tree op0, tree op1)
   10818                 :             : {
   10819                 :      744277 :   gimple_seq seq = NULL;
   10820                 :      744277 :   tree res
   10821                 :      744277 :     = gimple_simplify (code, type, op0, op1, &seq,
   10822                 :      744277 :                        gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   10823                 :      744277 :   if (!res)
   10824                 :             :     {
   10825                 :      655280 :       res = create_tmp_reg_or_ssa_name (type);
   10826                 :      655280 :       gimple *stmt = gimple_build_assign (res, code, op0, op1);
   10827                 :      655280 :       gimple_set_location (stmt, loc);
   10828                 :      655280 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   10829                 :             :     }
   10830                 :      744277 :   gimple_build_insert_seq (gsi, before, update, seq);
   10831                 :      744277 :   return res;
   10832                 :             : }
   10833                 :             : 
   10834                 :             : /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
   10835                 :             :    simplifying it first if possible.  Returns the built
   10836                 :             :    expression value inserting any new statements at GSI honoring BEFORE
   10837                 :             :    and UPDATE.  */
   10838                 :             : 
   10839                 :             : tree
   10840                 :       41960 : gimple_build (gimple_stmt_iterator *gsi,
   10841                 :             :               bool before, gsi_iterator_update update,
   10842                 :             :               location_t loc, enum tree_code code, tree type,
   10843                 :             :               tree op0, tree op1, tree op2)
   10844                 :             : {
   10845                 :             : 
   10846                 :       41960 :   gimple_seq seq = NULL;
   10847                 :       41960 :   tree res
   10848                 :       41960 :     = gimple_simplify (code, type, op0, op1, op2, &seq,
   10849                 :       41960 :                        gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   10850                 :       41960 :   if (!res)
   10851                 :             :     {
   10852                 :       30398 :       res = create_tmp_reg_or_ssa_name (type);
   10853                 :       30398 :       gimple *stmt;
   10854                 :       30398 :       if (code == BIT_FIELD_REF)
   10855                 :       24011 :         stmt = gimple_build_assign (res, code,
   10856                 :             :                                     build3 (code, type, op0, op1, op2));
   10857                 :             :       else
   10858                 :        6387 :         stmt = gimple_build_assign (res, code, op0, op1, op2);
   10859                 :       30398 :       gimple_set_location (stmt, loc);
   10860                 :       30398 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   10861                 :             :     }
   10862                 :       41960 :   gimple_build_insert_seq (gsi, before, update, seq);
   10863                 :       41960 :   return res;
   10864                 :             : }
   10865                 :             : 
   10866                 :             : /* Build the call FN () with a result of type TYPE (or no result if TYPE is
   10867                 :             :    void) with a location LOC.  Returns the built expression value (or NULL_TREE
   10868                 :             :    if TYPE is void) inserting any new statements at GSI honoring BEFORE
   10869                 :             :    and UPDATE.  */
   10870                 :             : 
   10871                 :             : tree
   10872                 :           0 : gimple_build (gimple_stmt_iterator *gsi,
   10873                 :             :               bool before, gsi_iterator_update update,
   10874                 :             :               location_t loc, combined_fn fn, tree type)
   10875                 :             : {
   10876                 :           0 :   tree res = NULL_TREE;
   10877                 :           0 :   gimple_seq seq = NULL;
   10878                 :           0 :   gcall *stmt;
   10879                 :           0 :   if (internal_fn_p (fn))
   10880                 :           0 :     stmt = gimple_build_call_internal (as_internal_fn (fn), 0);
   10881                 :             :   else
   10882                 :             :     {
   10883                 :           0 :       tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   10884                 :           0 :       stmt = gimple_build_call (decl, 0);
   10885                 :             :     }
   10886                 :           0 :   if (!VOID_TYPE_P (type))
   10887                 :             :     {
   10888                 :           0 :       res = create_tmp_reg_or_ssa_name (type);
   10889                 :           0 :       gimple_call_set_lhs (stmt, res);
   10890                 :             :     }
   10891                 :           0 :   gimple_set_location (stmt, loc);
   10892                 :           0 :   gimple_seq_add_stmt_without_update (&seq, stmt);
   10893                 :           0 :   gimple_build_insert_seq (gsi, before, update, seq);
   10894                 :           0 :   return res;
   10895                 :             : }
   10896                 :             : 
   10897                 :             : /* Build the call FN (ARG0) with a result of type TYPE
   10898                 :             :    (or no result if TYPE is void) with location LOC,
   10899                 :             :    simplifying it first if possible.  Returns the built
   10900                 :             :    expression value (or NULL_TREE if TYPE is void) inserting any new
   10901                 :             :    statements at GSI honoring BEFORE and UPDATE.  */
   10902                 :             : 
   10903                 :             : tree
   10904                 :       23834 : gimple_build (gimple_stmt_iterator *gsi,
   10905                 :             :               bool before, gsi_iterator_update update,
   10906                 :             :               location_t loc, combined_fn fn,
   10907                 :             :               tree type, tree arg0)
   10908                 :             : {
   10909                 :       23834 :   gimple_seq seq = NULL;
   10910                 :       23834 :   tree res = gimple_simplify (fn, type, arg0, &seq, gimple_build_valueize);
   10911                 :       23834 :   if (!res)
   10912                 :             :     {
   10913                 :       23834 :       gcall *stmt;
   10914                 :       23834 :       if (internal_fn_p (fn))
   10915                 :       23461 :         stmt = gimple_build_call_internal (as_internal_fn (fn), 1, arg0);
   10916                 :             :       else
   10917                 :             :         {
   10918                 :         373 :           tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   10919                 :         373 :           stmt = gimple_build_call (decl, 1, arg0);
   10920                 :             :         }
   10921                 :       23834 :       if (!VOID_TYPE_P (type))
   10922                 :             :         {
   10923                 :       23461 :           res = create_tmp_reg_or_ssa_name (type);
   10924                 :       23461 :           gimple_call_set_lhs (stmt, res);
   10925                 :             :         }
   10926                 :       23834 :       gimple_set_location (stmt, loc);
   10927                 :       23834 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   10928                 :             :     }
   10929                 :       23834 :   gimple_build_insert_seq  (gsi, before, update, seq);
   10930                 :       23834 :   return res;
   10931                 :             : }
   10932                 :             : 
   10933                 :             : /* Build the call FN (ARG0, ARG1) with a result of type TYPE
   10934                 :             :    (or no result if TYPE is void) with location LOC,
   10935                 :             :    simplifying it first if possible.  Returns the built
   10936                 :             :    expression value (or NULL_TREE if TYPE is void) inserting any new
   10937                 :             :    statements at GSI honoring BEFORE and UPDATE.  */
   10938                 :             : 
   10939                 :             : tree
   10940                 :           0 : gimple_build (gimple_stmt_iterator *gsi,
   10941                 :             :               bool before, gsi_iterator_update update,
   10942                 :             :               location_t loc, combined_fn fn,
   10943                 :             :               tree type, tree arg0, tree arg1)
   10944                 :             : {
   10945                 :           0 :   gimple_seq seq = NULL;
   10946                 :           0 :   tree res = gimple_simplify (fn, type, arg0, arg1, &seq,
   10947                 :             :                               gimple_build_valueize);
   10948                 :           0 :   if (!res)
   10949                 :             :     {
   10950                 :           0 :       gcall *stmt;
   10951                 :           0 :       if (internal_fn_p (fn))
   10952                 :           0 :         stmt = gimple_build_call_internal (as_internal_fn (fn), 2, arg0, arg1);
   10953                 :             :       else
   10954                 :             :         {
   10955                 :           0 :           tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   10956                 :           0 :           stmt = gimple_build_call (decl, 2, arg0, arg1);
   10957                 :             :         }
   10958                 :           0 :       if (!VOID_TYPE_P (type))
   10959                 :             :         {
   10960                 :           0 :           res = create_tmp_reg_or_ssa_name (type);
   10961                 :           0 :           gimple_call_set_lhs (stmt, res);
   10962                 :             :         }
   10963                 :           0 :       gimple_set_location (stmt, loc);
   10964                 :           0 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   10965                 :             :     }
   10966                 :           0 :   gimple_build_insert_seq (gsi, before, update, seq);
   10967                 :           0 :   return res;
   10968                 :             : }
   10969                 :             : 
   10970                 :             : /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
   10971                 :             :    (or no result if TYPE is void) with location LOC,
   10972                 :             :    simplifying it first if possible.  Returns the built
   10973                 :             :    expression value (or NULL_TREE if TYPE is void) inserting any new
   10974                 :             :    statements at GSI honoring BEFORE and UPDATE.  */
   10975                 :             : 
   10976                 :             : tree
   10977                 :           0 : gimple_build (gimple_stmt_iterator *gsi,
   10978                 :             :               bool before, gsi_iterator_update update,
   10979                 :             :               location_t loc, combined_fn fn,
   10980                 :             :               tree type, tree arg0, tree arg1, tree arg2)
   10981                 :             : {
   10982                 :           0 :   gimple_seq seq = NULL;
   10983                 :           0 :   tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
   10984                 :             :                               &seq, gimple_build_valueize);
   10985                 :           0 :   if (!res)
   10986                 :             :     {
   10987                 :           0 :       gcall *stmt;
   10988                 :           0 :       if (internal_fn_p (fn))
   10989                 :           0 :         stmt = gimple_build_call_internal (as_internal_fn (fn),
   10990                 :             :                                            3, arg0, arg1, arg2);
   10991                 :             :       else
   10992                 :             :         {
   10993                 :           0 :           tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   10994                 :           0 :           stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
   10995                 :             :         }
   10996                 :           0 :       if (!VOID_TYPE_P (type))
   10997                 :             :         {
   10998                 :           0 :           res = create_tmp_reg_or_ssa_name (type);
   10999                 :           0 :           gimple_call_set_lhs (stmt, res);
   11000                 :             :         }
   11001                 :           0 :       gimple_set_location (stmt, loc);
   11002                 :           0 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11003                 :             :     }
   11004                 :           0 :   gimple_build_insert_seq (gsi, before, update, seq);
   11005                 :           0 :   return res;
   11006                 :             : }
   11007                 :             : 
   11008                 :             : /* Build CODE (OP0) with a result of type TYPE (or no result if TYPE is
   11009                 :             :    void) with location LOC, simplifying it first if possible.  Returns the
   11010                 :             :    built expression value (or NULL_TREE if TYPE is void) inserting any new
   11011                 :             :    statements at GSI honoring BEFORE and UPDATE.  */
   11012                 :             : 
   11013                 :             : tree
   11014                 :          21 : gimple_build (gimple_stmt_iterator *gsi,
   11015                 :             :               bool before, gsi_iterator_update update,
   11016                 :             :               location_t loc, code_helper code, tree type, tree op0)
   11017                 :             : {
   11018                 :          21 :   if (code.is_tree_code ())
   11019                 :           0 :     return gimple_build (gsi, before, update, loc, tree_code (code), type, op0);
   11020                 :          21 :   return gimple_build (gsi, before, update, loc, combined_fn (code), type, op0);
   11021                 :             : }
   11022                 :             : 
   11023                 :             : /* Build CODE (OP0, OP1) with a result of type TYPE (or no result if TYPE is
   11024                 :             :    void) with location LOC, simplifying it first if possible.  Returns the
   11025                 :             :    built expression value (or NULL_TREE if TYPE is void) inserting any new
   11026                 :             :    statements at GSI honoring BEFORE and UPDATE.  */
   11027                 :             : 
   11028                 :             : tree
   11029                 :       22266 : gimple_build (gimple_stmt_iterator *gsi,
   11030                 :             :               bool before, gsi_iterator_update update,
   11031                 :             :               location_t loc, code_helper code, tree type, tree op0, tree op1)
   11032                 :             : {
   11033                 :       22266 :   if (code.is_tree_code ())
   11034                 :       22266 :     return gimple_build (gsi, before, update,
   11035                 :       22266 :                          loc, tree_code (code), type, op0, op1);
   11036                 :           0 :   return gimple_build (gsi, before, update,
   11037                 :           0 :                        loc, combined_fn (code), type, op0, op1);
   11038                 :             : }
   11039                 :             : 
   11040                 :             : /* Build CODE (OP0, OP1, OP2) with a result of type TYPE (or no result if TYPE
   11041                 :             :    is void) with location LOC, simplifying it first if possible.  Returns the
   11042                 :             :    built expression value (or NULL_TREE if TYPE is void) inserting any new
   11043                 :             :    statements at GSI honoring BEFORE and UPDATE.  */
   11044                 :             : 
   11045                 :             : tree
   11046                 :           0 : gimple_build (gimple_stmt_iterator *gsi,
   11047                 :             :               bool before, gsi_iterator_update update,
   11048                 :             :               location_t loc, code_helper code,
   11049                 :             :               tree type, tree op0, tree op1, tree op2)
   11050                 :             : {
   11051                 :           0 :   if (code.is_tree_code ())
   11052                 :           0 :     return gimple_build (gsi, before, update,
   11053                 :           0 :                          loc, tree_code (code), type, op0, op1, op2);
   11054                 :           0 :   return gimple_build (gsi, before, update,
   11055                 :           0 :                        loc, combined_fn (code), type, op0, op1, op2);
   11056                 :             : }
   11057                 :             : 
   11058                 :             : /* Build the conversion (TYPE) OP with a result of type TYPE
   11059                 :             :    with location LOC if such conversion is neccesary in GIMPLE,
   11060                 :             :    simplifying it first.
   11061                 :             :    Returns the built expression inserting any new statements
   11062                 :             :    at GSI honoring BEFORE and UPDATE.  */
   11063                 :             : 
   11064                 :             : tree
   11065                 :     1788053 : gimple_convert (gimple_stmt_iterator *gsi,
   11066                 :             :                 bool before, gsi_iterator_update update,
   11067                 :             :                 location_t loc, tree type, tree op)
   11068                 :             : {
   11069                 :     1788053 :   if (useless_type_conversion_p (type, TREE_TYPE (op)))
   11070                 :             :     return op;
   11071                 :       94716 :   return gimple_build (gsi, before, update, loc, NOP_EXPR, type, op);
   11072                 :             : }
   11073                 :             : 
   11074                 :             : /* Build the conversion (ptrofftype) OP with a result of a type
   11075                 :             :    compatible with ptrofftype with location LOC if such conversion
   11076                 :             :    is neccesary in GIMPLE, simplifying it first.
   11077                 :             :    Returns the built expression value inserting any new statements
   11078                 :             :    at GSI honoring BEFORE and UPDATE.  */
   11079                 :             : 
   11080                 :             : tree
   11081                 :         203 : gimple_convert_to_ptrofftype (gimple_stmt_iterator *gsi,
   11082                 :             :                               bool before, gsi_iterator_update update,
   11083                 :             :                               location_t loc, tree op)
   11084                 :             : {
   11085                 :         203 :   if (ptrofftype_p (TREE_TYPE (op)))
   11086                 :             :     return op;
   11087                 :           0 :   return gimple_convert (gsi, before, update, loc, sizetype, op);
   11088                 :             : }
   11089                 :             : 
   11090                 :             : /* Build a vector of type TYPE in which each element has the value OP.
   11091                 :             :    Return a gimple value for the result, inserting any new statements
   11092                 :             :    at GSI honoring BEFORE and UPDATE.  */
   11093                 :             : 
   11094                 :             : tree
   11095                 :      318041 : gimple_build_vector_from_val (gimple_stmt_iterator *gsi,
   11096                 :             :                               bool before, gsi_iterator_update update,
   11097                 :             :                               location_t loc, tree type, tree op)
   11098                 :             : {
   11099                 :      318041 :   if (!TYPE_VECTOR_SUBPARTS (type).is_constant ()
   11100                 :             :       && !CONSTANT_CLASS_P (op))
   11101                 :             :     return gimple_build (gsi, before, update,
   11102                 :             :                          loc, VEC_DUPLICATE_EXPR, type, op);
   11103                 :             : 
   11104                 :      318041 :   tree res, vec = build_vector_from_val (type, op);
   11105                 :      318041 :   if (is_gimple_val (vec))
   11106                 :             :     return vec;
   11107                 :       38150 :   if (gimple_in_ssa_p (cfun))
   11108                 :       38150 :     res = make_ssa_name (type);
   11109                 :             :   else
   11110                 :           0 :     res = create_tmp_reg (type);
   11111                 :       38150 :   gimple_seq seq = NULL;
   11112                 :       38150 :   gimple *stmt = gimple_build_assign (res, vec);
   11113                 :       38150 :   gimple_set_location (stmt, loc);
   11114                 :       38150 :   gimple_seq_add_stmt_without_update (&seq, stmt);
   11115                 :       38150 :   gimple_build_insert_seq (gsi, before, update, seq);
   11116                 :       38150 :   return res;
   11117                 :             : }
   11118                 :             : 
   11119                 :             : /* Build a vector from BUILDER, handling the case in which some elements
   11120                 :             :    are non-constant.  Return a gimple value for the result, inserting
   11121                 :             :    any new instructions to GSI honoring BEFORE and UPDATE.
   11122                 :             : 
   11123                 :             :    BUILDER must not have a stepped encoding on entry.  This is because
   11124                 :             :    the function is not geared up to handle the arithmetic that would
   11125                 :             :    be needed in the variable case, and any code building a vector that
   11126                 :             :    is known to be constant should use BUILDER->build () directly.  */
   11127                 :             : 
   11128                 :             : tree
   11129                 :      389414 : gimple_build_vector (gimple_stmt_iterator *gsi,
   11130                 :             :                      bool before, gsi_iterator_update update,
   11131                 :             :                      location_t loc, tree_vector_builder *builder)
   11132                 :             : {
   11133                 :      389414 :   gcc_assert (builder->nelts_per_pattern () <= 2);
   11134                 :      389414 :   unsigned int encoded_nelts = builder->encoded_nelts ();
   11135                 :     1284278 :   for (unsigned int i = 0; i < encoded_nelts; ++i)
   11136                 :     1041572 :     if (!CONSTANT_CLASS_P ((*builder)[i]))
   11137                 :             :       {
   11138                 :      146708 :         gimple_seq seq = NULL;
   11139                 :      146708 :         tree type = builder->type ();
   11140                 :      146708 :         unsigned int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
   11141                 :      146708 :         vec<constructor_elt, va_gc> *v;
   11142                 :      146708 :         vec_alloc (v, nelts);
   11143                 :      468284 :         for (i = 0; i < nelts; ++i)
   11144                 :      321576 :           CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
   11145                 :             : 
   11146                 :      146708 :         tree res;
   11147                 :      146708 :         if (gimple_in_ssa_p (cfun))
   11148                 :      146708 :           res = make_ssa_name (type);
   11149                 :             :         else
   11150                 :           0 :           res = create_tmp_reg (type);
   11151                 :      146708 :         gimple *stmt = gimple_build_assign (res, build_constructor (type, v));
   11152                 :      146708 :         gimple_set_location (stmt, loc);
   11153                 :      146708 :         gimple_seq_add_stmt_without_update (&seq, stmt);
   11154                 :      146708 :         gimple_build_insert_seq (gsi, before, update, seq);
   11155                 :      146708 :         return res;
   11156                 :             :       }
   11157                 :      242706 :   return builder->build ();
   11158                 :             : }
   11159                 :             : 
   11160                 :             : /* Emit gimple statements into &stmts that take a value given in OLD_SIZE
   11161                 :             :    and generate a value guaranteed to be rounded upwards to ALIGN.
   11162                 :             : 
   11163                 :             :    Return the tree node representing this size, it is of TREE_TYPE TYPE.  */
   11164                 :             : 
   11165                 :             : tree
   11166                 :           0 : gimple_build_round_up (gimple_stmt_iterator *gsi,
   11167                 :             :                        bool before, gsi_iterator_update update,
   11168                 :             :                        location_t loc, tree type,
   11169                 :             :                        tree old_size, unsigned HOST_WIDE_INT align)
   11170                 :             : {
   11171                 :           0 :   unsigned HOST_WIDE_INT tg_mask = align - 1;
   11172                 :             :   /* tree new_size = (old_size + tg_mask) & ~tg_mask;  */
   11173                 :           0 :   gcc_assert (INTEGRAL_TYPE_P (type));
   11174                 :           0 :   tree tree_mask = build_int_cst (type, tg_mask);
   11175                 :           0 :   tree oversize = gimple_build (gsi, before, update,
   11176                 :             :                                 loc, PLUS_EXPR, type, old_size, tree_mask);
   11177                 :             : 
   11178                 :           0 :   tree mask = build_int_cst (type, -align);
   11179                 :           0 :   return gimple_build (gsi, before, update,
   11180                 :           0 :                        loc, BIT_AND_EXPR, type, oversize, mask);
   11181                 :             : }
   11182                 :             : 
   11183                 :             : /* Return true if the result of assignment STMT is known to be non-negative.
   11184                 :             :    If the return value is based on the assumption that signed overflow is
   11185                 :             :    undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
   11186                 :             :    *STRICT_OVERFLOW_P.  DEPTH is the current nesting depth of the query.  */
   11187                 :             : 
   11188                 :             : static bool
   11189                 :    52774067 : gimple_assign_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
   11190                 :             :                                    int depth)
   11191                 :             : {
   11192                 :    52774067 :   enum tree_code code = gimple_assign_rhs_code (stmt);
   11193                 :    52774067 :   tree type = TREE_TYPE (gimple_assign_lhs (stmt));
   11194                 :    52774067 :   switch (get_gimple_rhs_class (code))
   11195                 :             :     {
   11196                 :    11145539 :     case GIMPLE_UNARY_RHS:
   11197                 :    11145539 :       return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
   11198                 :             :                                              type,
   11199                 :             :                                              gimple_assign_rhs1 (stmt),
   11200                 :    11145539 :                                              strict_overflow_p, depth);
   11201                 :    36618606 :     case GIMPLE_BINARY_RHS:
   11202                 :    36618606 :       return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
   11203                 :             :                                               type,
   11204                 :             :                                               gimple_assign_rhs1 (stmt),
   11205                 :             :                                               gimple_assign_rhs2 (stmt),
   11206                 :    36618606 :                                               strict_overflow_p, depth);
   11207                 :             :     case GIMPLE_TERNARY_RHS:
   11208                 :             :       return false;
   11209                 :     4974492 :     case GIMPLE_SINGLE_RHS:
   11210                 :     4974492 :       return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
   11211                 :     4974492 :                                               strict_overflow_p, depth);
   11212                 :             :     case GIMPLE_INVALID_RHS:
   11213                 :             :       break;
   11214                 :             :     }
   11215                 :           0 :   gcc_unreachable ();
   11216                 :             : }
   11217                 :             : 
   11218                 :             : /* Return true if return value of call STMT is known to be non-negative.
   11219                 :             :    If the return value is based on the assumption that signed overflow is
   11220                 :             :    undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
   11221                 :             :    *STRICT_OVERFLOW_P.  DEPTH is the current nesting depth of the query.  */
   11222                 :             : 
   11223                 :             : static bool
   11224                 :    20299156 : gimple_call_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
   11225                 :             :                                  int depth)
   11226                 :             : {
   11227                 :    20299156 :   tree arg0
   11228                 :    20299156 :     = gimple_call_num_args (stmt) > 0 ? gimple_call_arg (stmt, 0) : NULL_TREE;
   11229                 :    20299156 :   tree arg1
   11230                 :    20299156 :     = gimple_call_num_args (stmt) > 1 ? gimple_call_arg (stmt, 1) : NULL_TREE;
   11231                 :    20299156 :   tree lhs = gimple_call_lhs (stmt);
   11232                 :    20299156 :   return (lhs
   11233                 :    20299156 :           && tree_call_nonnegative_warnv_p (TREE_TYPE (lhs),
   11234                 :             :                                             gimple_call_combined_fn (stmt),
   11235                 :             :                                             arg0, arg1,
   11236                 :    20299156 :                                             strict_overflow_p, depth));
   11237                 :             : }
   11238                 :             : 
   11239                 :             : /* Return true if return value of call STMT is known to be non-negative.
   11240                 :             :    If the return value is based on the assumption that signed overflow is
   11241                 :             :    undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
   11242                 :             :    *STRICT_OVERFLOW_P.  DEPTH is the current nesting depth of the query.  */
   11243                 :             : 
   11244                 :             : static bool
   11245                 :    12422392 : gimple_phi_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
   11246                 :             :                                 int depth)
   11247                 :             : {
   11248                 :    25046296 :   for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
   11249                 :             :     {
   11250                 :    20232757 :       tree arg = gimple_phi_arg_def (stmt, i);
   11251                 :    20232757 :       if (!tree_single_nonnegative_warnv_p (arg, strict_overflow_p, depth + 1))
   11252                 :             :         return false;
   11253                 :             :     }
   11254                 :             :   return true;
   11255                 :             : }
   11256                 :             : 
   11257                 :             : /* Return true if STMT is known to compute a non-negative value.
   11258                 :             :    If the return value is based on the assumption that signed overflow is
   11259                 :             :    undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
   11260                 :             :    *STRICT_OVERFLOW_P.  DEPTH is the current nesting depth of the query.  */
   11261                 :             : 
   11262                 :             : bool
   11263                 :   137905849 : gimple_stmt_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
   11264                 :             :                                  int depth)
   11265                 :             : {
   11266                 :   137905849 :   tree type = gimple_range_type (stmt);
   11267                 :   137905849 :   if (type && frange::supports_p (type))
   11268                 :             :     {
   11269                 :     1491188 :       frange r;
   11270                 :     1491188 :       bool sign;
   11271                 :     1491188 :       if (get_global_range_query ()->range_of_stmt (r, stmt)
   11272                 :     1491188 :           && r.signbit_p (sign))
   11273                 :       16893 :         return !sign;
   11274                 :     1491188 :     }
   11275                 :   137888956 :   switch (gimple_code (stmt))
   11276                 :             :     {
   11277                 :    52774067 :     case GIMPLE_ASSIGN:
   11278                 :    52774067 :       return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p,
   11279                 :    52774067 :                                                 depth);
   11280                 :    20299156 :     case GIMPLE_CALL:
   11281                 :    20299156 :       return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p,
   11282                 :    20299156 :                                               depth);
   11283                 :    12422392 :     case GIMPLE_PHI:
   11284                 :    12422392 :       return gimple_phi_nonnegative_warnv_p (stmt, strict_overflow_p,
   11285                 :    12422392 :                                              depth);
   11286                 :             :     default:
   11287                 :             :       return false;
   11288                 :             :     }
   11289                 :             : }
   11290                 :             : 
   11291                 :             : /* Return true if the floating-point value computed by assignment STMT
   11292                 :             :    is known to have an integer value.  We also allow +Inf, -Inf and NaN
   11293                 :             :    to be considered integer values. Return false for signaling NaN.
   11294                 :             : 
   11295                 :             :    DEPTH is the current nesting depth of the query.  */
   11296                 :             : 
   11297                 :             : static bool
   11298                 :       59604 : gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
   11299                 :             : {
   11300                 :       59604 :   enum tree_code code = gimple_assign_rhs_code (stmt);
   11301                 :       59604 :   switch (get_gimple_rhs_class (code))
   11302                 :             :     {
   11303                 :       14787 :     case GIMPLE_UNARY_RHS:
   11304                 :       14787 :       return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
   11305                 :       14787 :                                           gimple_assign_rhs1 (stmt), depth);
   11306                 :       13700 :     case GIMPLE_BINARY_RHS:
   11307                 :       13700 :       return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
   11308                 :             :                                            gimple_assign_rhs1 (stmt),
   11309                 :       13700 :                                            gimple_assign_rhs2 (stmt), depth);
   11310                 :             :     case GIMPLE_TERNARY_RHS:
   11311                 :             :       return false;
   11312                 :       30040 :     case GIMPLE_SINGLE_RHS:
   11313                 :       30040 :       return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
   11314                 :             :     case GIMPLE_INVALID_RHS:
   11315                 :             :       break;
   11316                 :             :     }
   11317                 :           0 :   gcc_unreachable ();
   11318                 :             : }
   11319                 :             : 
   11320                 :             : /* Return true if the floating-point value computed by call STMT is known
   11321                 :             :    to have an integer value.  We also allow +Inf, -Inf and NaN to be
   11322                 :             :    considered integer values. Return false for signaling NaN.
   11323                 :             : 
   11324                 :             :    DEPTH is the current nesting depth of the query.  */
   11325                 :             : 
   11326                 :             : static bool
   11327                 :         916 : gimple_call_integer_valued_real_p (gimple *stmt, int depth)
   11328                 :             : {
   11329                 :         916 :   tree arg0 = (gimple_call_num_args (stmt) > 0
   11330                 :         916 :                ? gimple_call_arg (stmt, 0)
   11331                 :         916 :                : NULL_TREE);
   11332                 :         916 :   tree arg1 = (gimple_call_num_args (stmt) > 1
   11333                 :         916 :                ? gimple_call_arg (stmt, 1)
   11334                 :         916 :                : NULL_TREE);
   11335                 :         916 :   return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
   11336                 :         916 :                                      arg0, arg1, depth);
   11337                 :             : }
   11338                 :             : 
   11339                 :             : /* Return true if the floating-point result of phi STMT is known to have
   11340                 :             :    an integer value.  We also allow +Inf, -Inf and NaN to be considered
   11341                 :             :    integer values. Return false for signaling NaN.
   11342                 :             : 
   11343                 :             :    DEPTH is the current nesting depth of the query.  */
   11344                 :             : 
   11345                 :             : static bool
   11346                 :        1509 : gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
   11347                 :             : {
   11348                 :        1689 :   for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
   11349                 :             :     {
   11350                 :        1683 :       tree arg = gimple_phi_arg_def (stmt, i);
   11351                 :        1683 :       if (!integer_valued_real_single_p (arg, depth + 1))
   11352                 :             :         return false;
   11353                 :             :     }
   11354                 :             :   return true;
   11355                 :             : }
   11356                 :             : 
   11357                 :             : /* Return true if the floating-point value computed by STMT is known
   11358                 :             :    to have an integer value.  We also allow +Inf, -Inf and NaN to be
   11359                 :             :    considered integer values. Return false for signaling NaN.
   11360                 :             : 
   11361                 :             :    DEPTH is the current nesting depth of the query.  */
   11362                 :             : 
   11363                 :             : bool
   11364                 :       88486 : gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
   11365                 :             : {
   11366                 :       88486 :   switch (gimple_code (stmt))
   11367                 :             :     {
   11368                 :       59604 :     case GIMPLE_ASSIGN:
   11369                 :       59604 :       return gimple_assign_integer_valued_real_p (stmt, depth);
   11370                 :         916 :     case GIMPLE_CALL:
   11371                 :         916 :       return gimple_call_integer_valued_real_p (stmt, depth);
   11372                 :        1509 :     case GIMPLE_PHI:
   11373                 :        1509 :       return gimple_phi_integer_valued_real_p (stmt, depth);
   11374                 :             :     default:
   11375                 :             :       return false;
   11376                 :             :     }
   11377                 :             : }
        

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