LCOV - code coverage report
Current view: top level - gcc - gimple-fold.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.0 % 5509 5066
Test Date: 2026-05-30 15:37:04 Functions: 96.0 % 150 144
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Statement simplification on GIMPLE.
       2              :    Copyright (C) 2010-2026 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 "tree-pass.h"
      42              : #include "gimple-fold.h"
      43              : #include "gimplify.h"
      44              : #include "tree-into-ssa.h"
      45              : #include "tree-dfa.h"
      46              : #include "tree-object-size.h"
      47              : #include "tree-ssa.h"
      48              : #include "tree-ssa-propagate.h"
      49              : #include "ipa-utils.h"
      50              : #include "tree-ssa-address.h"
      51              : #include "langhooks.h"
      52              : #include "gimplify-me.h"
      53              : #include "dbgcnt.h"
      54              : #include "builtins.h"
      55              : #include "tree-eh.h"
      56              : #include "gimple-match.h"
      57              : #include "gomp-constants.h"
      58              : #include "optabs-query.h"
      59              : #include "omp-general.h"
      60              : #include "tree-cfg.h"
      61              : #include "fold-const-call.h"
      62              : #include "stringpool.h"
      63              : #include "attribs.h"
      64              : #include "asan.h"
      65              : #include "diagnostic-core.h"
      66              : #include "intl.h"
      67              : #include "calls.h"
      68              : #include "tree-vector-builder.h"
      69              : #include "tree-ssa-strlen.h"
      70              : #include "varasm.h"
      71              : #include "internal-fn.h"
      72              : #include "gimple-range.h"
      73              : 
      74              : enum strlen_range_kind {
      75              :   /* Compute the exact constant string length.  */
      76              :   SRK_STRLEN,
      77              :   /* Compute the maximum constant string length.  */
      78              :   SRK_STRLENMAX,
      79              :   /* Compute a range of string lengths bounded by object sizes.  When
      80              :      the length of a string cannot be determined, consider as the upper
      81              :      bound the size of the enclosing object the string may be a member
      82              :      or element of.  Also determine the size of the largest character
      83              :      array the string may refer to.  */
      84              :   SRK_LENRANGE,
      85              :   /* Determine the integer value of the argument (not string length).  */
      86              :   SRK_INT_VALUE
      87              : };
      88              : 
      89              : static bool
      90              : get_range_strlen (tree, bitmap, strlen_range_kind, c_strlen_data *, unsigned);
      91              : 
      92              : /* Return true when DECL can be referenced from current unit.
      93              :    FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
      94              :    We can get declarations that are not possible to reference for various
      95              :    reasons:
      96              : 
      97              :      1) When analyzing C++ virtual tables.
      98              :         C++ virtual tables do have known constructors even
      99              :         when they are keyed to other compilation unit.
     100              :         Those tables can contain pointers to methods and vars
     101              :         in other units.  Those methods have both STATIC and EXTERNAL
     102              :         set.
     103              :      2) In WHOPR mode devirtualization might lead to reference
     104              :         to method that was partitioned elsehwere.
     105              :         In this case we have static VAR_DECL or FUNCTION_DECL
     106              :         that has no corresponding callgraph/varpool node
     107              :         declaring the body.
     108              :      3) COMDAT functions referred by external vtables that
     109              :         we devirtualize only during final compilation stage.
     110              :         At this time we already decided that we will not output
     111              :         the function body and thus we can't reference the symbol
     112              :         directly.  */
     113              : 
     114              : static bool
     115      4439470 : can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
     116              : {
     117      4439470 :   varpool_node *vnode;
     118      4439470 :   struct cgraph_node *node;
     119      4439470 :   symtab_node *snode;
     120              : 
     121      4439470 :   if (DECL_ABSTRACT_P (decl))
     122              :     return false;
     123              : 
     124              :   /* We are concerned only about static/external vars and functions.  */
     125      1501140 :   if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
     126      5536534 :       || !VAR_OR_FUNCTION_DECL_P (decl))
     127              :     return true;
     128              : 
     129              :   /* Static objects can be referred only if they are defined and not optimized
     130              :      out yet.  */
     131      4035394 :   if (!TREE_PUBLIC (decl))
     132              :     {
     133      1094887 :       if (DECL_EXTERNAL (decl))
     134              :         return false;
     135              :       /* Before we start optimizing unreachable code we can be sure all
     136              :          static objects are defined.  */
     137      1094842 :       if (symtab->function_flags_ready)
     138              :         return true;
     139      1059535 :       snode = symtab_node::get (decl);
     140      1059535 :       if (!snode || !snode->definition)
     141              :         return false;
     142      1059480 :       node = dyn_cast <cgraph_node *> (snode);
     143      1068122 :       return !node || !node->inlined_to;
     144              :     }
     145              : 
     146              :   /* We will later output the initializer, so we can refer to it.
     147              :      So we are concerned only when DECL comes from initializer of
     148              :      external var or var that has been optimized out.  */
     149      2940507 :   if (!from_decl
     150       487155 :       || !VAR_P (from_decl)
     151       486020 :       || (!DECL_EXTERNAL (from_decl)
     152       204979 :           && (vnode = varpool_node::get (from_decl)) != NULL
     153       138478 :           && vnode->definition)
     154      3288055 :       || (flag_ltrans
     155            3 :           && (vnode = varpool_node::get (from_decl)) != NULL
     156            3 :           && vnode->in_other_partition))
     157              :     return true;
     158              :   /* We are folding reference from external vtable.  The vtable may reffer
     159              :      to a symbol keyed to other compilation unit.  The other compilation
     160              :      unit may be in separate DSO and the symbol may be hidden.  */
     161       347545 :   if (DECL_VISIBILITY_SPECIFIED (decl)
     162       340740 :       && DECL_EXTERNAL (decl)
     163       272595 :       && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
     164       536471 :       && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition))
     165              :     return false;
     166              :   /* When function is public, we always can introduce new reference.
     167              :      Exception are the COMDAT functions where introducing a direct
     168              :      reference imply need to include function body in the curren tunit.  */
     169       158619 :   if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
     170              :     return true;
     171              :   /* We have COMDAT.  We are going to check if we still have definition
     172              :      or if the definition is going to be output in other partition.
     173              :      Bypass this when gimplifying; all needed functions will be produced.
     174              : 
     175              :      As observed in PR20991 for already optimized out comdat virtual functions
     176              :      it may be tempting to not necessarily give up because the copy will be
     177              :      output elsewhere when corresponding vtable is output.
     178              :      This is however not possible - ABI specify that COMDATs are output in
     179              :      units where they are used and when the other unit was compiled with LTO
     180              :      it is possible that vtable was kept public while the function itself
     181              :      was privatized. */
     182       112448 :   if (!symtab->function_flags_ready)
     183              :     return true;
     184              : 
     185        99768 :   snode = symtab_node::get (decl);
     186        99768 :   if (!snode
     187        99768 :       || ((!snode->definition || DECL_EXTERNAL (decl))
     188        11809 :           && (!snode->in_other_partition
     189            0 :               || (!snode->forced_by_abi && !snode->force_output))))
     190              :     return false;
     191        62923 :   node = dyn_cast <cgraph_node *> (snode);
     192        62923 :   return !node || !node->inlined_to;
     193              : }
     194              : 
     195              : /* CVAL is value taken from DECL_INITIAL of variable.  Try to transform it into
     196              :    acceptable form for is_gimple_min_invariant.
     197              :    FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL.  */
     198              : 
     199              : tree
     200     15445774 : canonicalize_constructor_val (tree cval, tree from_decl)
     201              : {
     202     15445774 :   if (CONSTANT_CLASS_P (cval))
     203              :     return cval;
     204              : 
     205      9379259 :   tree orig_cval = cval;
     206      9379259 :   STRIP_NOPS (cval);
     207      9379259 :   if (TREE_CODE (cval) == POINTER_PLUS_EXPR
     208      9379259 :       && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
     209              :     {
     210        69939 :       tree ptr = TREE_OPERAND (cval, 0);
     211        69939 :       if (is_gimple_min_invariant (ptr))
     212       209079 :         cval = build1_loc (EXPR_LOCATION (cval),
     213        69693 :                            ADDR_EXPR, TREE_TYPE (ptr),
     214       139386 :                            fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
     215              :                                         ptr,
     216              :                                         fold_convert (ptr_type_node,
     217              :                                                       TREE_OPERAND (cval, 1))));
     218              :     }
     219      9379259 :   if (TREE_CODE (cval) == ADDR_EXPR)
     220              :     {
     221      5150234 :       tree base = NULL_TREE;
     222      5150234 :       if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
     223              :         {
     224          193 :           base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
     225          193 :           if (base)
     226          193 :             TREE_OPERAND (cval, 0) = base;
     227              :         }
     228              :       else
     229      5150041 :         base = get_base_address (TREE_OPERAND (cval, 0));
     230      5150234 :       if (!base)
     231            0 :         return NULL_TREE;
     232              : 
     233      2280377 :       if (VAR_OR_FUNCTION_DECL_P (base)
     234      6446850 :           && !can_refer_decl_in_current_unit_p (base, from_decl))
     235              :         return NULL_TREE;
     236      4960410 :       if (TREE_TYPE (base) == error_mark_node)
     237              :         return NULL_TREE;
     238      4960410 :       if (VAR_P (base))
     239              :         /* ???  We should be able to assert that TREE_ADDRESSABLE is set,
     240              :            but since the use can be in a debug stmt we can't.  */
     241              :         ;
     242      2279374 :       else if (TREE_CODE (base) == FUNCTION_DECL)
     243              :         {
     244              :           /* Make sure we create a cgraph node for functions we'll reference.
     245              :              They can be non-existent if the reference comes from an entry
     246              :              of an external vtable for example.  */
     247      1295613 :           cgraph_node::get_create (base);
     248              :         }
     249              :       /* Fixup types in global initializers.  */
     250      4960410 :       if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
     251        38302 :         cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
     252              : 
     253      4960410 :       if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
     254       212536 :         cval = fold_convert (TREE_TYPE (orig_cval), cval);
     255      4960410 :       return cval;
     256              :     }
     257              :   /* In CONSTRUCTORs we may see unfolded constants like (int (*) ()) 0.  */
     258      4229025 :   if (TREE_CODE (cval) == INTEGER_CST)
     259              :     {
     260        64515 :       if (TREE_OVERFLOW_P (cval))
     261            0 :         cval = drop_tree_overflow (cval);
     262        64515 :       if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
     263        61314 :         cval = fold_convert (TREE_TYPE (orig_cval), cval);
     264        64515 :       return cval;
     265              :     }
     266              :   return orig_cval;
     267              : }
     268              : 
     269              : /* If SYM is a constant variable with known value, return the value.
     270              :    NULL_TREE is returned otherwise.  */
     271              : 
     272              : tree
     273     20931908 : get_symbol_constant_value (tree sym)
     274              : {
     275     20931908 :   tree val = ctor_for_folding (sym);
     276     20931908 :   if (val != error_mark_node)
     277              :     {
     278        39583 :       if (val)
     279              :         {
     280        37309 :           val = canonicalize_constructor_val (unshare_expr (val), sym);
     281        37309 :           if (val
     282        37309 :               && is_gimple_min_invariant (val)
     283        65870 :               && useless_type_conversion_p (TREE_TYPE (sym), TREE_TYPE (val)))
     284              :             return val;
     285              :           else
     286         8854 :             return NULL_TREE;
     287              :         }
     288              :       /* Variables declared 'const' without an initializer
     289              :          have zero as the initializer if they may not be
     290              :          overridden at link or run time.  */
     291         2274 :       if (!val
     292         2274 :           && is_gimple_reg_type (TREE_TYPE (sym)))
     293         1937 :         return build_zero_cst (TREE_TYPE (sym));
     294              :     }
     295              : 
     296              :   return NULL_TREE;
     297              : }
     298              : 
     299              : 
     300              : 
     301              : /* Subroutine of fold_stmt.  We perform constant folding of the
     302              :    memory reference tree EXPR.  */
     303              : 
     304              : static tree
     305     63345438 : maybe_fold_reference (tree expr)
     306              : {
     307     63345438 :   tree result = NULL_TREE;
     308              : 
     309     63345438 :   if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
     310     61315728 :        || TREE_CODE (expr) == REALPART_EXPR
     311     60639311 :        || TREE_CODE (expr) == IMAGPART_EXPR)
     312     64833757 :       && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
     313         2995 :     result = fold_unary_loc (EXPR_LOCATION (expr),
     314              :                              TREE_CODE (expr),
     315         2995 :                              TREE_TYPE (expr),
     316         2995 :                              TREE_OPERAND (expr, 0));
     317     63342443 :   else if (TREE_CODE (expr) == BIT_FIELD_REF
     318     63342443 :            && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
     319           27 :     result = fold_ternary_loc (EXPR_LOCATION (expr),
     320              :                                TREE_CODE (expr),
     321           27 :                                TREE_TYPE (expr),
     322           27 :                                TREE_OPERAND (expr, 0),
     323           27 :                                TREE_OPERAND (expr, 1),
     324           27 :                                TREE_OPERAND (expr, 2));
     325              :   else
     326     63342416 :     result = fold_const_aggregate_ref (expr);
     327              : 
     328     63345438 :   if (result && is_gimple_min_invariant (result))
     329              :     return result;
     330              : 
     331              :   return NULL_TREE;
     332              : }
     333              : 
     334              : /* Return true if EXPR is an acceptable right-hand-side for a
     335              :    GIMPLE assignment.  We validate the entire tree, not just
     336              :    the root node, thus catching expressions that embed complex
     337              :    operands that are not permitted in GIMPLE.  This function
     338              :    is needed because the folding routines in fold-const.cc
     339              :    may return such expressions in some cases, e.g., an array
     340              :    access with an embedded index addition.  It may make more
     341              :    sense to have folding routines that are sensitive to the
     342              :    constraints on GIMPLE operands, rather than abandoning any
     343              :    any attempt to fold if the usual folding turns out to be too
     344              :    aggressive.  */
     345              : 
     346              : bool
     347            0 : valid_gimple_rhs_p (tree expr)
     348              : {
     349            0 :   enum tree_code code = TREE_CODE (expr);
     350              : 
     351            0 :   switch (TREE_CODE_CLASS (code))
     352              :     {
     353            0 :     case tcc_declaration:
     354            0 :       if (!is_gimple_variable (expr))
     355              :         return false;
     356              :       break;
     357              : 
     358              :     case tcc_constant:
     359              :       /* All constants are ok.  */
     360              :       break;
     361              : 
     362            0 :     case tcc_comparison:
     363              :       /* GENERIC allows comparisons with non-boolean types, reject
     364              :          those for GIMPLE.  Let vector-typed comparisons pass - rules
     365              :          for GENERIC and GIMPLE are the same here.  */
     366            0 :       if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
     367            0 :             && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
     368            0 :                 || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
     369            0 :           && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
     370              :         return false;
     371              : 
     372              :       /* Fallthru.  */
     373            0 :     case tcc_binary:
     374            0 :       if (!is_gimple_val (TREE_OPERAND (expr, 0))
     375            0 :           || !is_gimple_val (TREE_OPERAND (expr, 1)))
     376            0 :         return false;
     377              :       break;
     378              : 
     379            0 :     case tcc_unary:
     380            0 :       if (!is_gimple_val (TREE_OPERAND (expr, 0)))
     381              :         return false;
     382              :       break;
     383              : 
     384            0 :     case tcc_expression:
     385            0 :       switch (code)
     386              :         {
     387            0 :         case ADDR_EXPR:
     388            0 :           {
     389            0 :             tree t;
     390            0 :             if (is_gimple_min_invariant (expr))
     391              :               return true;
     392            0 :             t = TREE_OPERAND (expr, 0);
     393            0 :             while (handled_component_p (t))
     394              :               {
     395              :                 /* ??? More checks needed, see the GIMPLE verifier.  */
     396            0 :                 if ((TREE_CODE (t) == ARRAY_REF
     397            0 :                      || TREE_CODE (t) == ARRAY_RANGE_REF)
     398            0 :                     && !is_gimple_val (TREE_OPERAND (t, 1)))
     399              :                   return false;
     400            0 :                 t = TREE_OPERAND (t, 0);
     401              :               }
     402            0 :             if (!is_gimple_id (t))
     403              :               return false;
     404              :           }
     405              :           break;
     406              : 
     407            0 :         default:
     408            0 :           if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
     409              :             {
     410            0 :               if (!is_gimple_val (TREE_OPERAND (expr, 0))
     411            0 :                   || !is_gimple_val (TREE_OPERAND (expr, 1))
     412            0 :                   || !is_gimple_val (TREE_OPERAND (expr, 2)))
     413            0 :                 return false;
     414              :               break;
     415              :             }
     416              :           return false;
     417              :         }
     418              :       break;
     419              : 
     420              :     case tcc_vl_exp:
     421              :       return false;
     422              : 
     423            0 :     case tcc_exceptional:
     424            0 :       if (code == CONSTRUCTOR)
     425              :         {
     426              :           unsigned i;
     427              :           tree elt;
     428            0 :           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
     429            0 :             if (!is_gimple_val (elt))
     430              :               return false;
     431              :           return true;
     432              :         }
     433            0 :       if (code != SSA_NAME)
     434              :         return false;
     435              :       break;
     436              : 
     437            0 :     case tcc_reference:
     438            0 :       if (code == BIT_FIELD_REF)
     439            0 :         return is_gimple_val (TREE_OPERAND (expr, 0));
     440              :       return false;
     441              : 
     442              :     default:
     443              :       return false;
     444              :     }
     445              : 
     446              :   return true;
     447              : }
     448              : 
     449              : 
     450              : /* Attempt to fold an assignment statement pointed-to by SI.  Returns a
     451              :    replacement rhs for the statement or NULL_TREE if no simplification
     452              :    could be made.  It is assumed that the operands have been previously
     453              :    folded.  */
     454              : 
     455              : static tree
     456    253155899 : fold_gimple_assign (gimple_stmt_iterator *si)
     457              : {
     458    253155899 :   gimple *stmt = gsi_stmt (*si);
     459    253155899 :   enum tree_code subcode = gimple_assign_rhs_code (stmt);
     460    253155899 :   location_t loc = gimple_location (stmt);
     461              : 
     462    253155899 :   tree result = NULL_TREE;
     463              : 
     464    253155899 :   switch (get_gimple_rhs_class (subcode))
     465              :     {
     466    167523273 :     case GIMPLE_SINGLE_RHS:
     467    167523273 :       {
     468    167523273 :         tree rhs = gimple_assign_rhs1 (stmt);
     469              : 
     470    167523273 :         if (TREE_CLOBBER_P (rhs))
     471              :           return NULL_TREE;
     472              : 
     473    154287665 :         if (REFERENCE_CLASS_P (rhs))
     474     60858838 :           return maybe_fold_reference (rhs);
     475              : 
     476     93428827 :         else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
     477              :           {
     478       165656 :             tree val = OBJ_TYPE_REF_EXPR (rhs);
     479       165656 :             if (is_gimple_min_invariant (val))
     480              :               return val;
     481       165634 :             else if (flag_devirtualize && virtual_method_call_p (rhs))
     482              :               {
     483       165594 :                 bool final;
     484       165594 :                 vec <cgraph_node *>targets
     485       165594 :                   = possible_polymorphic_call_targets (rhs, stmt, &final);
     486       165868 :                 if (final && targets.length () <= 1 && dbg_cnt (devirt))
     487              :                   {
     488           44 :                     if (dump_enabled_p ())
     489              :                       {
     490            0 :                         dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
     491              :                                          "resolving virtual function address "
     492              :                                          "reference to function %s\n",
     493            0 :                                          targets.length () == 1
     494            0 :                                          ? targets[0]->name ()
     495              :                                          : "NULL");
     496              :                       }
     497           44 :                     if (targets.length () == 1)
     498              :                       {
     499           33 :                         val = fold_convert (TREE_TYPE (val),
     500              :                                             build_fold_addr_expr_loc
     501              :                                               (loc, targets[0]->decl));
     502           33 :                         STRIP_USELESS_TYPE_CONVERSION (val);
     503              :                       }
     504              :                     else
     505              :                       /* We cannot use __builtin_unreachable here because it
     506              :                          cannot have address taken.  */
     507           11 :                       val = build_int_cst (TREE_TYPE (val), 0);
     508           44 :                     return val;
     509              :                   }
     510              :               }
     511              :           }
     512              : 
     513     93263171 :         else if (TREE_CODE (rhs) == ADDR_EXPR)
     514              :           {
     515     14673596 :             tree ref = TREE_OPERAND (rhs, 0);
     516     14673596 :             if (TREE_CODE (ref) == MEM_REF
     517     14673596 :                 && integer_zerop (TREE_OPERAND (ref, 1)))
     518              :               {
     519         2488 :                 result = TREE_OPERAND (ref, 0);
     520         2488 :                 if (!useless_type_conversion_p (TREE_TYPE (rhs),
     521         2488 :                                                 TREE_TYPE (result)))
     522            0 :                   result = build1 (NOP_EXPR, TREE_TYPE (rhs), result);
     523         2488 :                 return result;
     524              :               }
     525              :           }
     526              : 
     527     78589575 :         else if (TREE_CODE (rhs) == CONSTRUCTOR
     528     78589575 :                  && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
     529              :           {
     530              :             /* Fold a constant vector CONSTRUCTOR to VECTOR_CST.  */
     531              :             unsigned i;
     532              :             tree val;
     533              : 
     534       426846 :             FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
     535       422561 :               if (! CONSTANT_CLASS_P (val))
     536              :                 return NULL_TREE;
     537              : 
     538         4285 :             return build_vector_from_ctor (TREE_TYPE (rhs),
     539         8570 :                                            CONSTRUCTOR_ELTS (rhs));
     540              :           }
     541              : 
     542     78253263 :         else if (DECL_P (rhs)
     543     78253263 :                  && is_gimple_reg_type (TREE_TYPE (rhs)))
     544     12016723 :           return get_symbol_constant_value (rhs);
     545              :       }
     546              :       break;
     547              : 
     548              :     case GIMPLE_UNARY_RHS:
     549              :       break;
     550              : 
     551              :     case GIMPLE_BINARY_RHS:
     552              :       break;
     553              : 
     554       511320 :     case GIMPLE_TERNARY_RHS:
     555      1022640 :       result = fold_ternary_loc (loc, subcode,
     556       511320 :                                  TREE_TYPE (gimple_assign_lhs (stmt)),
     557              :                                  gimple_assign_rhs1 (stmt),
     558              :                                  gimple_assign_rhs2 (stmt),
     559              :                                  gimple_assign_rhs3 (stmt));
     560              : 
     561       511320 :       if (result)
     562              :         {
     563            0 :           STRIP_USELESS_TYPE_CONVERSION (result);
     564            0 :           if (valid_gimple_rhs_p (result))
     565              :             return result;
     566              :         }
     567              :       break;
     568              : 
     569            0 :     case GIMPLE_INVALID_RHS:
     570            0 :       gcc_unreachable ();
     571              :     }
     572              : 
     573              :   return NULL_TREE;
     574              : }
     575              : 
     576              : 
     577              : /* Replace a statement at *SI_P with a sequence of statements in STMTS,
     578              :    adjusting the replacement stmts location and virtual operands.
     579              :    If the statement has a lhs the last stmt in the sequence is expected
     580              :    to assign to that lhs.  */
     581              : 
     582              : void
     583       129493 : gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
     584              : {
     585       129493 :   gimple *stmt = gsi_stmt (*si_p);
     586              : 
     587       129493 :   if (gimple_has_location (stmt))
     588       107409 :     annotate_all_with_location (stmts, gimple_location (stmt));
     589              : 
     590              :   /* First iterate over the replacement statements backward, assigning
     591              :      virtual operands to their defining statements.  */
     592       129493 :   gimple *laststore = NULL;
     593       258986 :   for (gimple_stmt_iterator i = gsi_last (stmts);
     594       529867 :        !gsi_end_p (i); gsi_prev (&i))
     595              :     {
     596       200187 :       gimple *new_stmt = gsi_stmt (i);
     597       200187 :       if ((gimple_assign_single_p (new_stmt)
     598       125758 :            && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
     599       325675 :           || (is_gimple_call (new_stmt)
     600        14695 :               && (gimple_call_flags (new_stmt)
     601        14695 :                   & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
     602              :         {
     603         2299 :           tree vdef;
     604         2299 :           if (!laststore)
     605         2293 :             vdef = gimple_vdef (stmt);
     606              :           else
     607            6 :             vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
     608         2299 :           gimple_set_vdef (new_stmt, vdef);
     609         2299 :           if (vdef && TREE_CODE (vdef) == SSA_NAME)
     610         1377 :             SSA_NAME_DEF_STMT (vdef) = new_stmt;
     611              :           laststore = new_stmt;
     612              :         }
     613              :     }
     614              : 
     615              :   /* Second iterate over the statements forward, assigning virtual
     616              :      operands to their uses.  */
     617       129493 :   tree reaching_vuse = gimple_vuse (stmt);
     618       129493 :   for (gimple_stmt_iterator i = gsi_start (stmts);
     619       329680 :        !gsi_end_p (i); gsi_next (&i))
     620              :     {
     621       200187 :       gimple *new_stmt = gsi_stmt (i);
     622              :       /* If the new statement possibly has a VUSE, update it with exact SSA
     623              :          name we know will reach this one.  */
     624       200187 :       if (gimple_has_mem_ops (new_stmt))
     625       200185 :         gimple_set_vuse (new_stmt, reaching_vuse);
     626       200187 :       gimple_set_modified (new_stmt, true);
     627       599176 :       if (gimple_vdef (new_stmt))
     628       200187 :         reaching_vuse = gimple_vdef (new_stmt);
     629              :     }
     630              : 
     631              :   /* If the new sequence does not do a store release the virtual
     632              :      definition of the original statement.  */
     633       129493 :   if (reaching_vuse
     634       215937 :       && reaching_vuse == gimple_vuse (stmt))
     635              :     {
     636        85073 :       tree vdef = gimple_vdef (stmt);
     637        85073 :       if (vdef
     638         1473 :           && TREE_CODE (vdef) == SSA_NAME)
     639              :         {
     640         1417 :           unlink_stmt_vdef (stmt);
     641         1417 :           release_ssa_name (vdef);
     642              :         }
     643              :     }
     644              : 
     645              :   /* Finally replace the original statement with the sequence.  */
     646       129493 :   gsi_replace_with_seq (si_p, stmts, false);
     647       129493 : }
     648              : 
     649              : /* Helper function for update_gimple_call and
     650              :    gimplify_and_update_call_from_tree.  A GIMPLE_CALL STMT is being replaced
     651              :    with GIMPLE_CALL NEW_STMT.  */
     652              : 
     653              : static void
     654         2443 : finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple *new_stmt,
     655              :                            gimple *stmt)
     656              : {
     657         2443 :   tree lhs = gimple_call_lhs (stmt);
     658         2443 :   gimple_call_set_lhs (new_stmt, lhs);
     659         2443 :   if (lhs && TREE_CODE (lhs) == SSA_NAME)
     660          809 :     SSA_NAME_DEF_STMT (lhs) = new_stmt;
     661         2443 :   gimple_move_vops (new_stmt, stmt);
     662         2443 :   gimple_set_location (new_stmt, gimple_location (stmt));
     663         2443 :   if (gimple_block (new_stmt) == NULL_TREE)
     664            1 :     gimple_set_block (new_stmt, gimple_block (stmt));
     665         2443 :   gsi_replace (si_p, new_stmt, false);
     666         2443 : }
     667              : 
     668              : /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
     669              :    with number of arguments NARGS, where the arguments in GIMPLE form
     670              :    follow NARGS argument.  */
     671              : 
     672              : bool
     673         2440 : update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
     674              : {
     675         2440 :   va_list ap;
     676         2440 :   gcall *new_stmt, *stmt = as_a <gcall *> (gsi_stmt (*si_p));
     677              : 
     678         2440 :   gcc_assert (is_gimple_call (stmt));
     679         2440 :   va_start (ap, nargs);
     680         2440 :   new_stmt = gimple_build_call_valist (fn, nargs, ap);
     681         2440 :   finish_update_gimple_call (si_p, new_stmt, stmt);
     682         2440 :   va_end (ap);
     683         2440 :   return true;
     684              : }
     685              : 
     686              : /* Return true if EXPR is a CALL_EXPR suitable for representation
     687              :    as a single GIMPLE_CALL statement.  If the arguments require
     688              :    further gimplification, return false.  */
     689              : 
     690              : static bool
     691        59262 : valid_gimple_call_p (tree expr)
     692              : {
     693        59262 :   unsigned i, nargs;
     694              : 
     695        59262 :   if (TREE_CODE (expr) != CALL_EXPR)
     696              :     return false;
     697              : 
     698            3 :   nargs = call_expr_nargs (expr);
     699            6 :   for (i = 0; i < nargs; i++)
     700              :     {
     701            3 :       tree arg = CALL_EXPR_ARG (expr, i);
     702            3 :       if (is_gimple_reg_type (TREE_TYPE (arg)))
     703              :         {
     704            3 :           if (!is_gimple_val (arg))
     705              :             return false;
     706              :         }
     707              :       else
     708            0 :         if (!is_gimple_lvalue (arg))
     709              :           return false;
     710              :     }
     711              : 
     712              :   return true;
     713              : }
     714              : 
     715              : /* Convert EXPR into a GIMPLE value suitable for substitution on the
     716              :    RHS of an assignment.  Insert the necessary statements before
     717              :    iterator *SI_P.  The statement at *SI_P, which must be a GIMPLE_CALL
     718              :    is replaced.  If the call is expected to produces a result, then it
     719              :    is replaced by an assignment of the new RHS to the result variable.
     720              :    If the result is to be ignored, then the call is replaced by a
     721              :    GIMPLE_NOP.  A proper VDEF chain is retained by making the first
     722              :    VUSE and the last VDEF of the whole sequence be the same as the replaced
     723              :    statement and using new SSA names for stores in between.  */
     724              : 
     725              : void
     726        59262 : gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
     727              : {
     728        59262 :   tree lhs;
     729        59262 :   gimple *stmt, *new_stmt;
     730        59262 :   gimple_stmt_iterator i;
     731        59262 :   gimple_seq stmts = NULL;
     732              : 
     733        59262 :   stmt = gsi_stmt (*si_p);
     734              : 
     735        59262 :   gcc_assert (is_gimple_call (stmt));
     736              : 
     737        59262 :   if (valid_gimple_call_p (expr))
     738              :     {
     739              :       /* The call has simplified to another call.  */
     740            3 :       tree fn = CALL_EXPR_FN (expr);
     741            3 :       unsigned i;
     742            3 :       unsigned nargs = call_expr_nargs (expr);
     743            3 :       vec<tree> args = vNULL;
     744            3 :       gcall *new_stmt;
     745              : 
     746            3 :       if (nargs > 0)
     747              :         {
     748            3 :           args.create (nargs);
     749            3 :           args.safe_grow_cleared (nargs, true);
     750              : 
     751            9 :           for (i = 0; i < nargs; i++)
     752            3 :             args[i] = CALL_EXPR_ARG (expr, i);
     753              :         }
     754              : 
     755            3 :       new_stmt = gimple_build_call_vec (fn, args);
     756            3 :       finish_update_gimple_call (si_p, new_stmt, stmt);
     757            3 :       args.release ();
     758            3 :       return;
     759              :     }
     760              : 
     761        59259 :   lhs = gimple_call_lhs (stmt);
     762        59259 :   if (lhs == NULL_TREE)
     763              :     {
     764         2446 :       push_gimplify_context (gimple_in_ssa_p (cfun));
     765         1223 :       gimplify_and_add (expr, &stmts);
     766         1223 :       pop_gimplify_context (NULL);
     767              : 
     768              :       /* We can end up with folding a memcpy of an empty class assignment
     769              :          which gets optimized away by C++ gimplification.  */
     770         1223 :       if (gimple_seq_empty_p (stmts))
     771              :         {
     772         1088 :           if (gimple_in_ssa_p (cfun))
     773              :             {
     774         1088 :               unlink_stmt_vdef (stmt);
     775         1088 :               release_defs (stmt);
     776              :             }
     777         1088 :           gsi_replace (si_p, gimple_build_nop (), false);
     778         1088 :           return;
     779              :         }
     780              :     }
     781              :   else
     782              :     {
     783        58036 :       tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
     784        58036 :       new_stmt = gimple_build_assign (lhs, tmp);
     785        58036 :       i = gsi_last (stmts);
     786        58036 :       gsi_insert_after_without_update (&i, new_stmt,
     787              :                                        GSI_CONTINUE_LINKING);
     788              :     }
     789              : 
     790        58171 :   gsi_replace_with_seq_vops (si_p, stmts);
     791              : }
     792              : 
     793              : /* Print a message in the dump file recording transformation of FROM to TO.  */
     794              : 
     795              : static void
     796        40480 : dump_transformation (gcall *from, gcall *to)
     797              : {
     798        40480 :   if (dump_enabled_p ())
     799           12 :     dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, from, "simplified %T to %T\n",
     800              :                      gimple_call_fn (from), gimple_call_fn (to));
     801        40480 : }
     802              : 
     803              : /* Replace the call at *GSI with the gimple value VAL.  */
     804              : 
     805              : void
     806        84917 : replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
     807              : {
     808        84917 :   gimple *stmt = gsi_stmt (*gsi);
     809        84917 :   tree lhs = gimple_call_lhs (stmt);
     810        84917 :   gimple *repl;
     811        84917 :   if (lhs)
     812              :     {
     813        79910 :       if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
     814         2091 :         val = fold_convert (TREE_TYPE (lhs), val);
     815        79910 :       repl = gimple_build_assign (lhs, val);
     816              :     }
     817              :   else
     818         5007 :     repl = gimple_build_nop ();
     819        84917 :   tree vdef = gimple_vdef (stmt);
     820        84917 :   if (vdef && TREE_CODE (vdef) == SSA_NAME)
     821              :     {
     822         5584 :       unlink_stmt_vdef (stmt);
     823         5584 :       release_ssa_name (vdef);
     824              :     }
     825        84917 :   gsi_replace (gsi, repl, false);
     826        84917 : }
     827              : 
     828              : /* Replace the call at *GSI with the new call REPL and fold that
     829              :    again.  */
     830              : 
     831              : static void
     832        40480 : replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
     833              : {
     834        40480 :   gimple *stmt = gsi_stmt (*gsi);
     835        40480 :   dump_transformation (as_a <gcall *> (stmt), as_a <gcall *> (repl));
     836        40480 :   gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
     837        40480 :   gimple_set_location (repl, gimple_location (stmt));
     838        40480 :   gimple_move_vops (repl, stmt);
     839        40480 :   gsi_replace (gsi, repl, false);
     840        40480 :   fold_stmt (gsi);
     841        40480 : }
     842              : 
     843              : /* Return true if VAR is a VAR_DECL or a component thereof.  */
     844              : 
     845              : static bool
     846       423863 : var_decl_component_p (tree var)
     847              : {
     848       423863 :   tree inner = var;
     849       618045 :   while (handled_component_p (inner))
     850       194182 :     inner = TREE_OPERAND (inner, 0);
     851       423863 :   return (DECL_P (inner)
     852       423863 :           || (TREE_CODE (inner) == MEM_REF
     853        49931 :               && TREE_CODE (TREE_OPERAND (inner, 0)) == ADDR_EXPR));
     854              : }
     855              : 
     856              : /* Return TRUE if the SIZE argument, representing the size of an
     857              :    object, is in a range of values of which exactly zero is valid.  */
     858              : 
     859              : static bool
     860      1028310 : size_must_be_zero_p (tree size)
     861              : {
     862      1028310 :   if (integer_zerop (size))
     863              :     return true;
     864              : 
     865      1025665 :   if (TREE_CODE (size) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (size)))
     866              :     return false;
     867              : 
     868       614340 :   tree type = TREE_TYPE (size);
     869       614340 :   int prec = TYPE_PRECISION (type);
     870              : 
     871              :   /* Compute the value of SSIZE_MAX, the largest positive value that
     872              :      can be stored in ssize_t, the signed counterpart of size_t.  */
     873       614340 :   wide_int ssize_max = wi::lshift (wi::one (prec), prec - 1) - 1;
     874       614340 :   wide_int zero = wi::zero (TYPE_PRECISION (type));
     875       614340 :   int_range_max valid_range (type, zero, ssize_max);
     876       614340 :   int_range_max vr;
     877      1228680 :   get_range_query (cfun)->range_of_expr (vr, size);
     878              : 
     879       614340 :   if (vr.undefined_p ())
     880           88 :     vr.set_varying (TREE_TYPE (size));
     881       614340 :   vr.intersect (valid_range);
     882       614340 :   return vr.zero_p ();
     883       614340 : }
     884              : 
     885              : /* Fold function call to builtin mem{{,p}cpy,move}.  Try to detect and
     886              :    diagnose (otherwise undefined) overlapping copies without preventing
     887              :    folding.  When folded, GCC guarantees that overlapping memcpy has
     888              :    the same semantics as memmove.  Call to the library memcpy need not
     889              :    provide the same guarantee.  Return false if no simplification can
     890              :    be made.  */
     891              : 
     892              : static bool
     893      1028310 : gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
     894              :                                tree dest, tree src, enum built_in_function code)
     895              : {
     896      1028310 :   gimple *stmt = gsi_stmt (*gsi);
     897      1028310 :   tree lhs = gimple_call_lhs (stmt);
     898      1028310 :   tree len = gimple_call_arg (stmt, 2);
     899      1028310 :   location_t loc = gimple_location (stmt);
     900              : 
     901              :   /* If the LEN parameter is a constant zero or in range where
     902              :      the only valid value is zero, return DEST.  */
     903      1028310 :   if (size_must_be_zero_p (len))
     904              :     {
     905         2677 :       gimple *repl;
     906         2677 :       if (gimple_call_lhs (stmt))
     907           58 :         repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
     908              :       else
     909         2619 :         repl = gimple_build_nop ();
     910         2677 :       tree vdef = gimple_vdef (stmt);
     911         2677 :       if (vdef && TREE_CODE (vdef) == SSA_NAME)
     912              :         {
     913          571 :           unlink_stmt_vdef (stmt);
     914          571 :           release_ssa_name (vdef);
     915              :         }
     916         2677 :       gsi_replace (gsi, repl, false);
     917         2677 :       return true;
     918              :     }
     919              : 
     920              :   /* If SRC and DEST are the same (and not volatile), return
     921              :      DEST{,+LEN,+LEN-1}.  */
     922      1025633 :   if (operand_equal_p (src, dest, 0))
     923              :     {
     924              :       /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
     925              :          It's safe and may even be emitted by GCC itself (see bug
     926              :          32667).  */
     927           73 :       unlink_stmt_vdef (stmt);
     928          146 :       if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
     929           33 :         release_ssa_name (gimple_vdef (stmt));
     930           73 :       if (!lhs)
     931              :         {
     932           52 :           gsi_replace (gsi, gimple_build_nop (), false);
     933           52 :           return true;
     934              :         }
     935           21 :       goto done;
     936              :     }
     937      2051120 :   else if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
     938              :     return false;
     939              :   else
     940              :     {
     941              :       /* We cannot (easily) change the type of the copy if it is a storage
     942              :          order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that can
     943              :          modify the storage order of objects (see storage_order_barrier_p).  */
     944      1025560 :       tree srctype
     945      1038823 :         = POINTER_TYPE_P (TREE_TYPE (src))
     946      1038823 :           ? TREE_TYPE (TREE_TYPE (src)) : NULL_TREE;
     947      1025560 :       tree desttype
     948      1045674 :         = POINTER_TYPE_P (TREE_TYPE (dest))
     949      1045674 :           ? TREE_TYPE (TREE_TYPE (dest)) : NULL_TREE;
     950      1025560 :       tree destvar, srcvar, srcoff;
     951      1025560 :       unsigned int src_align, dest_align;
     952      1025560 :       unsigned HOST_WIDE_INT tmp_len;
     953      1025560 :       const char *tmp_str;
     954              : 
     955              :       /* Build accesses at offset zero with a ref-all character type.  */
     956      1025560 :       tree off0
     957      1025560 :         = build_int_cst (build_pointer_type_for_mode (char_type_node,
     958              :                                                       ptr_mode, true), 0);
     959              : 
     960              :       /* If we can perform the copy efficiently with first doing all loads
     961              :          and then all stores inline it that way.  Currently efficiently
     962              :          means that we can load all the memory into a single integer
     963              :          register which is what MOVE_MAX gives us.  */
     964      1025560 :       src_align = get_pointer_alignment (src);
     965      1025560 :       dest_align = get_pointer_alignment (dest);
     966      1025560 :       if (tree_fits_uhwi_p (len)
     967       401166 :           && compare_tree_int (len, MOVE_MAX) <= 0
     968              :           /* FIXME: Don't transform copies from strings with known length.
     969              :              Until GCC 9 this prevented a case in gcc.dg/strlenopt-8.c
     970              :              from being handled, and the case was XFAILed for that reason.
     971              :              Now that it is handled and the XFAIL removed, as soon as other
     972              :              strlenopt tests that rely on it for passing are adjusted, this
     973              :              hack can be removed.  */
     974       303992 :           && !c_strlen (src, 1)
     975       188265 :           && !((tmp_str = getbyterep (src, &tmp_len)) != NULL
     976        79855 :                && memchr (tmp_str, 0, tmp_len) == NULL)
     977       121727 :           && !(srctype
     978       121727 :                && AGGREGATE_TYPE_P (srctype)
     979        58292 :                && TYPE_REVERSE_STORAGE_ORDER (srctype))
     980      1147154 :           && !(desttype
     981       121594 :                && AGGREGATE_TYPE_P (desttype)
     982        67460 :                && TYPE_REVERSE_STORAGE_ORDER (desttype)))
     983              :         {
     984       121561 :           unsigned ilen = tree_to_uhwi (len);
     985       121561 :           if (pow2p_hwi (ilen))
     986              :             {
     987              :               /* Detect out-of-bounds accesses without issuing warnings.
     988              :                  Avoid folding out-of-bounds copies but to avoid false
     989              :                  positives for unreachable code defer warning until after
     990              :                  DCE has worked its magic.
     991              :                  -Wrestrict is still diagnosed.  */
     992        24407 :               if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
     993              :                                                          dest, src, len, len,
     994        24407 :                                                          false, false))
     995          980 :                 if (warning != OPT_Wrestrict)
     996        21049 :                   return false;
     997              : 
     998        23485 :               scalar_int_mode imode;
     999        23485 :               machine_mode mode;
    1000        23485 :               if (int_mode_for_size (ilen * BITS_PER_UNIT, 0).exists (&imode)
    1001        23485 :                   && bitwise_mode_for_size (ilen
    1002        23485 :                                             * BITS_PER_UNIT).exists (&mode)
    1003        46970 :                   && known_eq (GET_MODE_BITSIZE (mode), ilen * BITS_PER_UNIT)
    1004              :                   /* If the destination pointer is not aligned we must be able
    1005              :                      to emit an unaligned store.  */
    1006        23485 :                   && (dest_align >= GET_MODE_ALIGNMENT (mode)
    1007        12670 :                       || !targetm.slow_unaligned_access (mode, dest_align)
    1008            0 :                       || (optab_handler (movmisalign_optab, mode)
    1009              :                           != CODE_FOR_nothing)))
    1010              :                 {
    1011        23485 :                   tree type = bitwise_type_for_mode (mode);
    1012        23485 :                   tree srctype = type;
    1013        23485 :                   tree desttype = type;
    1014        23485 :                   if (src_align < GET_MODE_ALIGNMENT (mode))
    1015        12139 :                     srctype = build_aligned_type (type, src_align);
    1016        23485 :                   tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
    1017        23485 :                   tree tem = fold_const_aggregate_ref (srcmem);
    1018        23485 :                   if (tem)
    1019              :                     srcmem = tem;
    1020        22435 :                   else if (src_align < GET_MODE_ALIGNMENT (mode)
    1021        11882 :                            && targetm.slow_unaligned_access (mode, src_align)
    1022        22435 :                            && (optab_handler (movmisalign_optab, mode)
    1023              :                                == CODE_FOR_nothing))
    1024              :                     srcmem = NULL_TREE;
    1025        22435 :                   if (srcmem)
    1026              :                     {
    1027        23485 :                       gimple *new_stmt;
    1028        23485 :                       if (is_gimple_reg_type (TREE_TYPE (srcmem)))
    1029              :                         {
    1030        23485 :                           new_stmt = gimple_build_assign (NULL_TREE, srcmem);
    1031        23485 :                           srcmem
    1032        23485 :                             = make_ssa_name (TREE_TYPE (srcmem), new_stmt);
    1033        23485 :                           gimple_assign_set_lhs (new_stmt, srcmem);
    1034        46970 :                           gimple_set_vuse (new_stmt, gimple_vuse (stmt));
    1035        23485 :                           gimple_set_location (new_stmt, loc);
    1036        23485 :                           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1037              :                         }
    1038        23485 :                       if (dest_align < GET_MODE_ALIGNMENT (mode))
    1039        12670 :                         desttype = build_aligned_type (type, dest_align);
    1040        23485 :                       new_stmt
    1041        23485 :                         = gimple_build_assign (fold_build2 (MEM_REF, desttype,
    1042              :                                                             dest, off0),
    1043              :                                                srcmem);
    1044        23485 :                       gimple_move_vops (new_stmt, stmt);
    1045        23485 :                       if (!lhs)
    1046              :                         {
    1047        20127 :                           gsi_replace (gsi, new_stmt, false);
    1048        20127 :                           return true;
    1049              :                         }
    1050         3358 :                       gimple_set_location (new_stmt, loc);
    1051         3358 :                       gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1052         3358 :                       goto done;
    1053              :                     }
    1054              :                 }
    1055              :             }
    1056              :         }
    1057              : 
    1058      1001153 :       if (code == BUILT_IN_MEMMOVE)
    1059              :         {
    1060              :           /* Both DEST and SRC must be pointer types.
    1061              :              ??? This is what old code did.  Is the testing for pointer types
    1062              :              really mandatory?
    1063              : 
    1064              :              If either SRC is readonly or length is 1, we can use memcpy.  */
    1065       201761 :           if (!dest_align || !src_align)
    1066              :             return false;
    1067       201761 :           if (readonly_data_expr (src)
    1068       201761 :               || (tree_fits_uhwi_p (len)
    1069        32855 :                   && (MIN (src_align, dest_align) / BITS_PER_UNIT
    1070        32855 :                       >= tree_to_uhwi (len))))
    1071              :             {
    1072       968064 :               tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    1073        19929 :               if (!fn)
    1074              :                 return false;
    1075        19929 :               gimple_call_set_fndecl (stmt, fn);
    1076        19929 :               gimple_call_set_arg (stmt, 0, dest);
    1077        19929 :               gimple_call_set_arg (stmt, 1, src);
    1078        19929 :               fold_stmt (gsi);
    1079        19929 :               return true;
    1080              :             }
    1081              : 
    1082              :           /* If *src and *dest can't overlap, optimize into memcpy as well.  */
    1083       181832 :           if (TREE_CODE (src) == ADDR_EXPR
    1084         5543 :               && TREE_CODE (dest) == ADDR_EXPR)
    1085              :             {
    1086         1794 :               tree src_base, dest_base, fn;
    1087         1794 :               poly_int64 src_offset = 0, dest_offset = 0;
    1088         1794 :               poly_uint64 maxsize;
    1089              : 
    1090         1794 :               srcvar = TREE_OPERAND (src, 0);
    1091         1794 :               src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
    1092         1794 :               if (src_base == NULL)
    1093            0 :                 src_base = srcvar;
    1094         1794 :               destvar = TREE_OPERAND (dest, 0);
    1095         1794 :               dest_base = get_addr_base_and_unit_offset (destvar,
    1096              :                                                          &dest_offset);
    1097         1794 :               if (dest_base == NULL)
    1098            0 :                 dest_base = destvar;
    1099         1794 :               if (!poly_int_tree_p (len, &maxsize))
    1100          232 :                 maxsize = -1;
    1101         1794 :               if (SSA_VAR_P (src_base)
    1102         1784 :                   && SSA_VAR_P (dest_base))
    1103              :                 {
    1104         1784 :                   if (operand_equal_p (src_base, dest_base, 0)
    1105         1784 :                       && ranges_maybe_overlap_p (src_offset, maxsize,
    1106              :                                                  dest_offset, maxsize))
    1107              :                     return false;
    1108              :                 }
    1109           10 :               else if (TREE_CODE (src_base) == MEM_REF
    1110            0 :                        && TREE_CODE (dest_base) == MEM_REF)
    1111              :                 {
    1112            0 :                   if (! operand_equal_p (TREE_OPERAND (src_base, 0),
    1113            0 :                                          TREE_OPERAND (dest_base, 0), 0))
    1114            0 :                     return false;
    1115            0 :                   poly_offset_int full_src_offset
    1116            0 :                     = mem_ref_offset (src_base) + src_offset;
    1117            0 :                   poly_offset_int full_dest_offset
    1118            0 :                     = mem_ref_offset (dest_base) + dest_offset;
    1119            0 :                   if (ranges_maybe_overlap_p (full_src_offset, maxsize,
    1120              :                                               full_dest_offset, maxsize))
    1121              :                     return false;
    1122            0 :                 }
    1123              :               else
    1124              :                 return false;
    1125              : 
    1126         1794 :               fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    1127         1382 :               if (!fn)
    1128              :                 return false;
    1129         1382 :               gimple_call_set_fndecl (stmt, fn);
    1130         1382 :               gimple_call_set_arg (stmt, 0, dest);
    1131         1382 :               gimple_call_set_arg (stmt, 1, src);
    1132         1382 :               fold_stmt (gsi);
    1133         1382 :               return true;
    1134              :             }
    1135              : 
    1136              :           /* If the destination and source do not alias optimize into
    1137              :              memcpy as well.  */
    1138       180038 :           if ((is_gimple_min_invariant (dest)
    1139       176330 :                || TREE_CODE (dest) == SSA_NAME)
    1140       338027 :               && (is_gimple_min_invariant (src)
    1141       157969 :                   || TREE_CODE (src) == SSA_NAME))
    1142              :             {
    1143       161254 :               ao_ref destr, srcr;
    1144       161254 :               ao_ref_init_from_ptr_and_size (&destr, dest, len);
    1145       161254 :               ao_ref_init_from_ptr_and_size (&srcr, src, len);
    1146       161254 :               if (!refs_may_alias_p_1 (&destr, &srcr, false))
    1147              :                 {
    1148        10060 :                   tree fn;
    1149        10060 :                   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    1150        10060 :                   if (!fn)
    1151        10060 :                     return false;
    1152        10060 :                   gimple_call_set_fndecl (stmt, fn);
    1153        10060 :                   gimple_call_set_arg (stmt, 0, dest);
    1154        10060 :                   gimple_call_set_arg (stmt, 1, src);
    1155        10060 :                   fold_stmt (gsi);
    1156        10060 :                   return true;
    1157              :                 }
    1158              :             }
    1159              : 
    1160       169978 :           return false;
    1161              :         }
    1162              : 
    1163       799392 :       if (!tree_fits_shwi_p (len))
    1164              :         return false;
    1165       326997 :       if (!srctype
    1166       326997 :           || (AGGREGATE_TYPE_P (srctype)
    1167       209072 :               && TYPE_REVERSE_STORAGE_ORDER (srctype)))
    1168              :         return false;
    1169       326864 :       if (!desttype
    1170       326864 :           || (AGGREGATE_TYPE_P (desttype)
    1171       195209 :               && TYPE_REVERSE_STORAGE_ORDER (desttype)))
    1172              :         return false;
    1173              :       /* In the following try to find a type that is most natural to be
    1174              :          used for the memcpy source and destination and that allows
    1175              :          the most optimization when memcpy is turned into a plain assignment
    1176              :          using that type.  In theory we could always use a char[len] type
    1177              :          but that only gains us that the destination and source possibly
    1178              :          no longer will have their address taken.  */
    1179       326831 :       if (TREE_CODE (srctype) == ARRAY_TYPE
    1180       326831 :           && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
    1181       135142 :         srctype = TREE_TYPE (srctype);
    1182       326831 :       if (TREE_CODE (desttype) == ARRAY_TYPE
    1183       326831 :           && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
    1184       109001 :         desttype = TREE_TYPE (desttype);
    1185       326831 :       if (TREE_ADDRESSABLE (srctype)
    1186       326800 :           || TREE_ADDRESSABLE (desttype))
    1187              :         return false;
    1188              : 
    1189              :       /* Make sure we are not copying using a floating-point mode or
    1190              :          a type whose size possibly does not match its precision.  */
    1191       652983 :       if (FLOAT_MODE_P (TYPE_MODE (desttype))
    1192       325964 :           || TREE_CODE (desttype) == BOOLEAN_TYPE
    1193       652715 :           || TREE_CODE (desttype) == ENUMERAL_TYPE)
    1194          839 :         desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
    1195       653115 :       if (FLOAT_MODE_P (TYPE_MODE (srctype))
    1196       326230 :           || TREE_CODE (srctype) == BOOLEAN_TYPE
    1197       652985 :           || TREE_CODE (srctype) == ENUMERAL_TYPE)
    1198          569 :         srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
    1199       326773 :       if (!srctype)
    1200          120 :         srctype = desttype;
    1201       326773 :       if (!desttype)
    1202            0 :         desttype = srctype;
    1203       326773 :       if (!srctype)
    1204              :         return false;
    1205              : 
    1206       326773 :       src_align = get_pointer_alignment (src);
    1207       326773 :       dest_align = get_pointer_alignment (dest);
    1208              : 
    1209              :       /* Choose between src and destination type for the access based
    1210              :          on alignment, whether the access constitutes a register access
    1211              :          and whether it may actually expose a declaration for SSA rewrite
    1212              :          or SRA decomposition.  Also try to expose a string constant, we
    1213              :          might be able to concatenate several of them later into a single
    1214              :          string store.  */
    1215       326773 :       destvar = NULL_TREE;
    1216       326773 :       srcvar = NULL_TREE;
    1217       326773 :       if (TREE_CODE (dest) == ADDR_EXPR
    1218       120053 :           && var_decl_component_p (TREE_OPERAND (dest, 0))
    1219       120049 :           && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)
    1220        24490 :           && dest_align >= TYPE_ALIGN (desttype)
    1221       351263 :           && (is_gimple_reg_type (desttype)
    1222        24077 :               || src_align >= TYPE_ALIGN (desttype)))
    1223        19403 :         destvar = fold_build2 (MEM_REF, desttype, dest, off0);
    1224       307370 :       else if (TREE_CODE (src) == ADDR_EXPR
    1225       243394 :                && var_decl_component_p (TREE_OPERAND (src, 0))
    1226        43306 :                && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
    1227         8535 :                && src_align >= TYPE_ALIGN (srctype)
    1228       315889 :                && (is_gimple_reg_type (srctype)
    1229         8356 :                    || dest_align >= TYPE_ALIGN (srctype)))
    1230         3084 :         srcvar = fold_build2 (MEM_REF, srctype, src, off0);
    1231              :       /* FIXME: Don't transform copies from strings with known original length.
    1232              :          As soon as strlenopt tests that rely on it for passing are adjusted,
    1233              :          this hack can be removed.  */
    1234       304286 :       else if (gimple_call_alloca_for_var_p (stmt)
    1235          115 :                && (srcvar = string_constant (src, &srcoff, NULL, NULL))
    1236            3 :                && integer_zerop (srcoff)
    1237            3 :                && tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (srcvar)), len)
    1238       304289 :                && dest_align >= TYPE_ALIGN (TREE_TYPE (srcvar)))
    1239            3 :         srctype = TREE_TYPE (srcvar);
    1240              :       else
    1241       304283 :         return false;
    1242              : 
    1243              :       /* Now that we chose an access type express the other side in
    1244              :          terms of it if the target allows that with respect to alignment
    1245              :          constraints.  */
    1246        22490 :       if (srcvar == NULL_TREE)
    1247              :         {
    1248        19403 :           if (src_align >= TYPE_ALIGN (desttype))
    1249        19387 :             srcvar = fold_build2 (MEM_REF, desttype, src, off0);
    1250              :           else
    1251              :             {
    1252           16 :               enum machine_mode mode = TYPE_MODE (desttype);
    1253           16 :               if ((mode == BLKmode && STRICT_ALIGNMENT)
    1254           16 :                   || (targetm.slow_unaligned_access (mode, src_align)
    1255           16 :                       && (optab_handler (movmisalign_optab, mode)
    1256              :                           == CODE_FOR_nothing)))
    1257              :                 return false;
    1258           16 :               srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
    1259              :                                             src_align);
    1260           16 :               srcvar = fold_build2 (MEM_REF, srctype, src, off0);
    1261              :             }
    1262              :         }
    1263         3087 :       else if (destvar == NULL_TREE)
    1264              :         {
    1265         3087 :           if (dest_align >= TYPE_ALIGN (srctype))
    1266         3087 :             destvar = fold_build2 (MEM_REF, srctype, dest, off0);
    1267              :           else
    1268              :             {
    1269            0 :               enum machine_mode mode = TYPE_MODE (srctype);
    1270            0 :               if ((mode == BLKmode && STRICT_ALIGNMENT)
    1271            0 :                   || (targetm.slow_unaligned_access (mode, dest_align)
    1272            0 :                       && (optab_handler (movmisalign_optab, mode)
    1273              :                           == CODE_FOR_nothing)))
    1274              :                 return false;
    1275            0 :               desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
    1276              :                                              dest_align);
    1277            0 :               destvar = fold_build2 (MEM_REF, desttype, dest, off0);
    1278              :             }
    1279              :         }
    1280              : 
    1281              :       /* Same as above, detect out-of-bounds accesses without issuing
    1282              :          warnings.  Avoid folding out-of-bounds copies but to avoid
    1283              :          false positives for unreachable code defer warning until
    1284              :          after DCE has worked its magic.
    1285              :          -Wrestrict is still diagnosed.  */
    1286        22490 :       if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
    1287              :                                                  dest, src, len, len,
    1288        22490 :                                                  false, false))
    1289         1263 :         if (warning != OPT_Wrestrict)
    1290              :           return false;
    1291              : 
    1292        21235 :       gimple *new_stmt;
    1293        21235 :       if (is_gimple_reg_type (TREE_TYPE (srcvar)))
    1294              :         {
    1295          536 :           tree tem = fold_const_aggregate_ref (srcvar);
    1296          536 :           if (tem)
    1297          517 :             srcvar = tem;
    1298          536 :           if (! is_gimple_min_invariant (srcvar))
    1299              :             {
    1300           19 :               new_stmt = gimple_build_assign (NULL_TREE, srcvar);
    1301           19 :               srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt);
    1302           19 :               gimple_assign_set_lhs (new_stmt, srcvar);
    1303           38 :               gimple_set_vuse (new_stmt, gimple_vuse (stmt));
    1304           19 :               gimple_set_location (new_stmt, loc);
    1305           19 :               gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1306              :             }
    1307          536 :           new_stmt = gimple_build_assign (destvar, srcvar);
    1308          536 :           goto set_vop_and_replace;
    1309              :         }
    1310              : 
    1311              :       /* We get an aggregate copy.  If the source is a STRING_CST, then
    1312              :          directly use its type to perform the copy.  */
    1313        20699 :       if (TREE_CODE (srcvar) == STRING_CST)
    1314              :           desttype = srctype;
    1315              : 
    1316              :       /* Or else, use an unsigned char[] type to perform the copy in order
    1317              :          to preserve padding and to avoid any issues with TREE_ADDRESSABLE
    1318              :          types or float modes behavior on copying.  */
    1319              :       else
    1320              :         {
    1321        41392 :           desttype = build_array_type_nelts (unsigned_char_type_node,
    1322        20696 :                                              tree_to_uhwi (len));
    1323        20696 :           srctype = desttype;
    1324        20696 :           if (src_align > TYPE_ALIGN (srctype))
    1325        12390 :             srctype = build_aligned_type (srctype, src_align);
    1326        20696 :           srcvar = fold_build2 (MEM_REF, srctype, src, off0);
    1327              :         }
    1328              : 
    1329        20699 :       if (dest_align > TYPE_ALIGN (desttype))
    1330        13030 :         desttype = build_aligned_type (desttype, dest_align);
    1331        20699 :       destvar = fold_build2 (MEM_REF, desttype, dest, off0);
    1332        20699 :       new_stmt = gimple_build_assign (destvar, srcvar);
    1333              : 
    1334        21235 : set_vop_and_replace:
    1335        21235 :       gimple_move_vops (new_stmt, stmt);
    1336        21235 :       if (!lhs)
    1337              :         {
    1338        21048 :           gsi_replace (gsi, new_stmt, false);
    1339        21048 :           return true;
    1340              :         }
    1341          187 :       gimple_set_location (new_stmt, loc);
    1342          187 :       gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    1343              :     }
    1344              : 
    1345         3566 : done:
    1346         3566 :   gimple_seq stmts = NULL;
    1347         3566 :   if (code == BUILT_IN_MEMCPY || code == BUILT_IN_MEMMOVE)
    1348         3566 :     len = NULL_TREE;
    1349          198 :   else if (code == BUILT_IN_MEMPCPY)
    1350              :     {
    1351          198 :       len = gimple_convert_to_ptrofftype (&stmts, loc, len);
    1352          198 :       dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
    1353          198 :                            TREE_TYPE (dest), dest, len);
    1354              :     }
    1355              :   else
    1356            0 :     gcc_unreachable ();
    1357              : 
    1358         3566 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    1359         3566 :   gimple *repl = gimple_build_assign (lhs, dest);
    1360         3566 :   gsi_replace (gsi, repl, false);
    1361         3566 :   return true;
    1362              : }
    1363              : 
    1364              : /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
    1365              :    to built-in memcmp (a, b, len).  */
    1366              : 
    1367              : static bool
    1368          148 : gimple_fold_builtin_bcmp (gimple_stmt_iterator *gsi)
    1369              : {
    1370          148 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMCMP);
    1371              : 
    1372          148 :   if (!fn)
    1373              :     return false;
    1374              : 
    1375              :   /* Transform bcmp (a, b, len) into memcmp (a, b, len).  */
    1376              : 
    1377          148 :   gimple *stmt = gsi_stmt (*gsi);
    1378          296 :   if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
    1379              :     return false;
    1380          148 :   tree a = gimple_call_arg (stmt, 0);
    1381          148 :   tree b = gimple_call_arg (stmt, 1);
    1382          148 :   tree len = gimple_call_arg (stmt, 2);
    1383              : 
    1384          148 :   gimple *repl = gimple_build_call (fn, 3, a, b, len);
    1385          148 :   replace_call_with_call_and_fold (gsi, repl);
    1386              : 
    1387          148 :   return true;
    1388              : }
    1389              : 
    1390              : /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
    1391              :    to built-in memmove (dest, src, len).  */
    1392              : 
    1393              : static bool
    1394          367 : gimple_fold_builtin_bcopy (gimple_stmt_iterator *gsi)
    1395              : {
    1396          367 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMMOVE);
    1397              : 
    1398          367 :   if (!fn)
    1399              :     return false;
    1400              : 
    1401              :   /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
    1402              :      it's quivalent to memmove (not memcpy).  Transform bcopy (src, dest,
    1403              :      len) into memmove (dest, src, len).  */
    1404              : 
    1405          367 :   gimple *stmt = gsi_stmt (*gsi);
    1406          734 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1407              :     return false;
    1408          367 :   tree src = gimple_call_arg (stmt, 0);
    1409          367 :   tree dest = gimple_call_arg (stmt, 1);
    1410          367 :   tree len = gimple_call_arg (stmt, 2);
    1411              : 
    1412          367 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    1413          367 :   gimple_call_set_fntype (as_a <gcall *> (stmt), TREE_TYPE (fn));
    1414          367 :   replace_call_with_call_and_fold (gsi, repl);
    1415              : 
    1416          367 :   return true;
    1417              : }
    1418              : 
    1419              : /* Transform a call to built-in bzero (dest, len) at *GSI into one
    1420              :    to built-in memset (dest, 0, len).  */
    1421              : 
    1422              : static bool
    1423          250 : gimple_fold_builtin_bzero (gimple_stmt_iterator *gsi)
    1424              : {
    1425          250 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
    1426              : 
    1427          250 :   if (!fn)
    1428              :     return false;
    1429              : 
    1430              :   /* Transform bzero (dest, len) into memset (dest, 0, len).  */
    1431              : 
    1432          250 :   gimple *stmt = gsi_stmt (*gsi);
    1433          500 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1434              :     return false;
    1435          250 :   tree dest = gimple_call_arg (stmt, 0);
    1436          250 :   tree len = gimple_call_arg (stmt, 1);
    1437              : 
    1438          250 :   gimple_seq seq = NULL;
    1439          250 :   gimple *repl = gimple_build_call (fn, 3, dest, integer_zero_node, len);
    1440          250 :   gimple_seq_add_stmt_without_update (&seq, repl);
    1441          250 :   gsi_replace_with_seq_vops (gsi, seq);
    1442          250 :   fold_stmt (gsi);
    1443              : 
    1444          250 :   return true;
    1445              : }
    1446              : 
    1447              : /* Fold function call to builtin memset or bzero at *GSI setting the
    1448              :    memory of size LEN to VAL.  Return whether a simplification was made.  */
    1449              : 
    1450              : static bool
    1451       306420 : gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
    1452              : {
    1453       306420 :   gimple *stmt = gsi_stmt (*gsi);
    1454       306420 :   tree etype;
    1455       306420 :   unsigned HOST_WIDE_INT length, cval;
    1456              : 
    1457              :   /* If the LEN parameter is zero, return DEST.  */
    1458       306420 :   if (integer_zerop (len))
    1459              :     {
    1460          831 :       replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
    1461          831 :       return true;
    1462              :     }
    1463              : 
    1464       915014 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    1465              :     return false;
    1466              : 
    1467       305511 :   if (! tree_fits_uhwi_p (len))
    1468              :     return false;
    1469              : 
    1470       201874 :   if (TREE_CODE (c) != INTEGER_CST)
    1471              :     return false;
    1472              : 
    1473       196092 :   tree dest = gimple_call_arg (stmt, 0);
    1474       196092 :   tree var = dest;
    1475       196092 :   if (TREE_CODE (var) != ADDR_EXPR)
    1476              :     return false;
    1477              : 
    1478       157313 :   var = TREE_OPERAND (var, 0);
    1479       157313 :   if (TREE_THIS_VOLATILE (var))
    1480              :     return false;
    1481              : 
    1482       157270 :   etype = TREE_TYPE (var);
    1483       157270 :   if (TREE_CODE (etype) == ARRAY_TYPE)
    1484        81509 :     etype = TREE_TYPE (etype);
    1485              : 
    1486       157270 :   if ((!INTEGRAL_TYPE_P (etype)
    1487        96076 :        && !POINTER_TYPE_P (etype))
    1488       157795 :       || BITINT_TYPE_P (etype))
    1489              :     return false;
    1490              : 
    1491        60416 :   if (! var_decl_component_p (var))
    1492              :     return false;
    1493              : 
    1494        60416 :   length = tree_to_uhwi (len);
    1495        60416 :   if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype)) != length
    1496         1753 :       || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (etype))
    1497         3506 :           != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (etype)))
    1498        62169 :       || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
    1499        58663 :     return false;
    1500              : 
    1501         1753 :   if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
    1502              :     return false;
    1503              : 
    1504         1753 :   if (!type_has_mode_precision_p (etype))
    1505            7 :     etype = lang_hooks.types.type_for_mode (SCALAR_INT_TYPE_MODE (etype),
    1506            7 :                                             TYPE_UNSIGNED (etype));
    1507              : 
    1508         1753 :   if (integer_zerop (c))
    1509              :     cval = 0;
    1510              :   else
    1511              :     {
    1512          337 :       if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
    1513              :         return NULL_TREE;
    1514              : 
    1515          337 :       cval = TREE_INT_CST_LOW (c);
    1516          337 :       cval &= 0xff;
    1517          337 :       cval |= cval << 8;
    1518          337 :       cval |= cval << 16;
    1519          337 :       cval |= (cval << 31) << 1;
    1520              :     }
    1521              : 
    1522         1753 :   var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
    1523         1753 :   gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
    1524         1753 :   gimple_move_vops (store, stmt);
    1525         1753 :   gimple_set_location (store, gimple_location (stmt));
    1526         1753 :   gsi_insert_before (gsi, store, GSI_SAME_STMT);
    1527         1753 :   if (gimple_call_lhs (stmt))
    1528              :     {
    1529            2 :       gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
    1530            2 :       gsi_replace (gsi, asgn, false);
    1531              :     }
    1532              :   else
    1533              :     {
    1534         1751 :       gimple_stmt_iterator gsi2 = *gsi;
    1535         1751 :       gsi_prev (gsi);
    1536         1751 :       gsi_remove (&gsi2, true);
    1537              :     }
    1538              : 
    1539              :   return true;
    1540              : }
    1541              : 
    1542              : /* Helper of get_range_strlen for ARG that is not an SSA_NAME.  */
    1543              : 
    1544              : static bool
    1545       430424 : get_range_strlen_tree (tree arg, bitmap visited, strlen_range_kind rkind,
    1546              :                        c_strlen_data *pdata, unsigned eltsize)
    1547              : {
    1548       430424 :   gcc_assert (TREE_CODE (arg) != SSA_NAME);
    1549              : 
    1550              :   /* The length computed by this invocation of the function.  */
    1551       430424 :   tree val = NULL_TREE;
    1552              : 
    1553              :   /* True if VAL is an optimistic (tight) bound determined from
    1554              :      the size of the character array in which the string may be
    1555              :      stored.  In that case, the computed VAL is used to set
    1556              :      PDATA->MAXBOUND.  */
    1557       430424 :   bool tight_bound = false;
    1558              : 
    1559              :   /* We can end up with &(*iftmp_1)[0] here as well, so handle it.  */
    1560       430424 :   if (TREE_CODE (arg) == ADDR_EXPR
    1561       430424 :       && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
    1562              :     {
    1563        28379 :       tree op = TREE_OPERAND (arg, 0);
    1564        28379 :       if (integer_zerop (TREE_OPERAND (op, 1)))
    1565              :         {
    1566        12112 :           tree aop0 = TREE_OPERAND (op, 0);
    1567        12112 :           if (TREE_CODE (aop0) == INDIRECT_REF
    1568        12112 :               && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
    1569            0 :             return get_range_strlen (TREE_OPERAND (aop0, 0), visited, rkind,
    1570            0 :                                      pdata, eltsize);
    1571              :         }
    1572        16267 :       else if (TREE_CODE (TREE_OPERAND (op, 0)) == COMPONENT_REF
    1573        16267 :                && rkind == SRK_LENRANGE)
    1574              :         {
    1575              :           /* Fail if an array is the last member of a struct object
    1576              :              since it could be treated as a (fake) flexible array
    1577              :              member.  */
    1578         4800 :           tree idx = TREE_OPERAND (op, 1);
    1579              : 
    1580         4800 :           arg = TREE_OPERAND (op, 0);
    1581         4800 :           tree optype = TREE_TYPE (arg);
    1582         4800 :           if (tree dom = TYPE_DOMAIN (optype))
    1583         4800 :             if (tree bound = TYPE_MAX_VALUE (dom))
    1584         4800 :               if (TREE_CODE (bound) == INTEGER_CST
    1585         4800 :                   && TREE_CODE (idx) == INTEGER_CST
    1586         8030 :                   && tree_int_cst_lt (bound, idx))
    1587              :                 return false;
    1588              :         }
    1589              :     }
    1590              : 
    1591       430216 :   if (rkind == SRK_INT_VALUE)
    1592              :     {
    1593              :       /* We are computing the maximum value (not string length).  */
    1594         3083 :       val = arg;
    1595         3083 :       if (TREE_CODE (val) != INTEGER_CST
    1596         3083 :           || tree_int_cst_sgn (val) < 0)
    1597         2573 :         return false;
    1598              :     }
    1599              :   else
    1600              :     {
    1601       427133 :       c_strlen_data lendata = { };
    1602       427133 :       val = c_strlen (arg, 1, &lendata, eltsize);
    1603              : 
    1604       427133 :       if (!val && lendata.decl)
    1605              :         {
    1606              :           /* ARG refers to an unterminated const character array.
    1607              :              DATA.DECL with size DATA.LEN.  */
    1608         4193 :           val = lendata.minlen;
    1609         4193 :           pdata->decl = lendata.decl;
    1610              :         }
    1611              :     }
    1612              : 
    1613              :   /* Set if VAL represents the maximum length based on array size (set
    1614              :      when exact length cannot be determined).  */
    1615       427643 :   bool maxbound = false;
    1616              : 
    1617       427643 :   if (!val && rkind == SRK_LENRANGE)
    1618              :     {
    1619       226994 :       if (TREE_CODE (arg) == ADDR_EXPR)
    1620        80043 :         return get_range_strlen (TREE_OPERAND (arg, 0), visited, rkind,
    1621        80043 :                                  pdata, eltsize);
    1622              : 
    1623       146951 :       if (TREE_CODE (arg) == ARRAY_REF)
    1624              :         {
    1625        18487 :           tree optype = TREE_TYPE (TREE_OPERAND (arg, 0));
    1626              : 
    1627              :           /* Determine the "innermost" array type.  */
    1628        18487 :           while (TREE_CODE (optype) == ARRAY_TYPE
    1629        25433 :                  && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
    1630         6946 :             optype = TREE_TYPE (optype);
    1631              : 
    1632              :           /* Avoid arrays of pointers.  */
    1633        18487 :           tree eltype = TREE_TYPE (optype);
    1634        18487 :           if (TREE_CODE (optype) != ARRAY_TYPE
    1635        18487 :               || !INTEGRAL_TYPE_P (eltype))
    1636              :             return false;
    1637              : 
    1638              :           /* Fail when the array bound is unknown or zero.  */
    1639        13617 :           val = TYPE_SIZE_UNIT (optype);
    1640        13617 :           if (!val
    1641        13545 :               || TREE_CODE (val) != INTEGER_CST
    1642        27134 :               || integer_zerop (val))
    1643          105 :             return false;
    1644              : 
    1645        13512 :           val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
    1646              :                               integer_one_node);
    1647              : 
    1648              :           /* Set the minimum size to zero since the string in
    1649              :              the array could have zero length.  */
    1650        13512 :           pdata->minlen = ssize_int (0);
    1651              : 
    1652        13512 :           tight_bound = true;
    1653              :         }
    1654       128464 :       else if (TREE_CODE (arg) == COMPONENT_REF
    1655       128464 :                && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1)))
    1656              :                    == ARRAY_TYPE))
    1657              :         {
    1658              :           /* Use the type of the member array to determine the upper
    1659              :              bound on the length of the array.  This may be overly
    1660              :              optimistic if the array itself isn't NUL-terminated and
    1661              :              the caller relies on the subsequent member to contain
    1662              :              the NUL but that would only be considered valid if
    1663              :              the array were the last member of a struct.  */
    1664              : 
    1665         9640 :           tree fld = TREE_OPERAND (arg, 1);
    1666              : 
    1667         9640 :           tree optype = TREE_TYPE (fld);
    1668              : 
    1669              :           /* Determine the "innermost" array type.  */
    1670         9640 :           while (TREE_CODE (optype) == ARRAY_TYPE
    1671        10202 :                  && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
    1672          562 :             optype = TREE_TYPE (optype);
    1673              : 
    1674              :           /* Fail when the array bound is unknown or zero.  */
    1675         9640 :           val = TYPE_SIZE_UNIT (optype);
    1676         9640 :           if (!val
    1677         9404 :               || TREE_CODE (val) != INTEGER_CST
    1678        19009 :               || integer_zerop (val))
    1679          350 :             return false;
    1680         9290 :           val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
    1681              :                              integer_one_node);
    1682              : 
    1683              :           /* Set the minimum size to zero since the string in
    1684              :              the array could have zero length.  */
    1685         9290 :           pdata->minlen = ssize_int (0);
    1686              : 
    1687              :           /* The array size determined above is an optimistic bound
    1688              :              on the length.  If the array isn't nul-terminated the
    1689              :              length computed by the library function would be greater.
    1690              :              Even though using strlen to cross the subobject boundary
    1691              :              is undefined, avoid drawing conclusions from the member
    1692              :              type about the length here.  */
    1693         9290 :           tight_bound = true;
    1694              :         }
    1695       118824 :       else if (TREE_CODE (arg) == MEM_REF
    1696        28135 :                && TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
    1697         3939 :                && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == INTEGER_TYPE
    1698       122325 :                && TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
    1699              :         {
    1700              :           /* Handle a MEM_REF into a DECL accessing an array of integers,
    1701              :              being conservative about references to extern structures with
    1702              :              flexible array members that can be initialized to arbitrary
    1703              :              numbers of elements as an extension (static structs are okay).  */
    1704         3501 :           tree ref = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
    1705         3501 :           if ((TREE_CODE (ref) == PARM_DECL || VAR_P (ref))
    1706         6989 :               && (decl_binds_to_current_def_p (ref)
    1707          438 :                   || !array_ref_flexible_size_p (arg)))
    1708              :             {
    1709              :               /* Fail if the offset is out of bounds.  Such accesses
    1710              :                  should be diagnosed at some point.  */
    1711         3375 :               val = DECL_SIZE_UNIT (ref);
    1712         3375 :               if (!val
    1713         3203 :                   || TREE_CODE (val) != INTEGER_CST
    1714         6578 :                   || integer_zerop (val))
    1715          371 :                 return false;
    1716              : 
    1717         3201 :               poly_offset_int psiz = wi::to_offset (val);
    1718         3201 :               poly_offset_int poff = mem_ref_offset (arg);
    1719         3201 :               if (known_le (psiz, poff))
    1720              :                 return false;
    1721              : 
    1722         3004 :               pdata->minlen = ssize_int (0);
    1723              : 
    1724              :               /* Subtract the offset and one for the terminating nul.  */
    1725         3004 :               psiz -= poff;
    1726         3004 :               psiz -= 1;
    1727         3004 :               val = wide_int_to_tree (TREE_TYPE (val), psiz);
    1728              :               /* Since VAL reflects the size of a declared object
    1729              :                  rather the type of the access it is not a tight bound.  */
    1730              :             }
    1731              :         }
    1732       115323 :       else if (TREE_CODE (arg) == PARM_DECL || VAR_P (arg))
    1733              :         {
    1734              :           /* Avoid handling pointers to arrays.  GCC might misuse
    1735              :              a pointer to an array of one bound to point to an array
    1736              :              object of a greater bound.  */
    1737        70187 :           tree argtype = TREE_TYPE (arg);
    1738        70187 :           if (TREE_CODE (argtype) == ARRAY_TYPE)
    1739              :             {
    1740        42184 :               val = TYPE_SIZE_UNIT (argtype);
    1741        42184 :               if (!val
    1742        41418 :                   || TREE_CODE (val) != INTEGER_CST
    1743        83602 :                   || integer_zerop (val))
    1744          881 :                 return false;
    1745        41303 :               val = wide_int_to_tree (TREE_TYPE (val),
    1746        41303 :                                       wi::sub (wi::to_wide (val), 1));
    1747              : 
    1748              :               /* Set the minimum size to zero since the string in
    1749              :                  the array could have zero length.  */
    1750        41303 :               pdata->minlen = ssize_int (0);
    1751              :             }
    1752              :         }
    1753              :       maxbound = true;
    1754              :     }
    1755              : 
    1756       341023 :   if (!val)
    1757              :     return false;
    1758              : 
    1759              :   /* Adjust the lower bound on the string length as necessary.  */
    1760       243859 :   if (!pdata->minlen
    1761       243859 :       || (rkind != SRK_STRLEN
    1762        71585 :           && TREE_CODE (pdata->minlen) == INTEGER_CST
    1763        71585 :           && TREE_CODE (val) == INTEGER_CST
    1764        71580 :           && tree_int_cst_lt (val, pdata->minlen)))
    1765       172366 :     pdata->minlen = val;
    1766              : 
    1767       243859 :   if (pdata->maxbound && TREE_CODE (pdata->maxbound) == INTEGER_CST)
    1768              :     {
    1769              :       /* Adjust the tighter (more optimistic) string length bound
    1770              :          if necessary and proceed to adjust the more conservative
    1771              :          bound.  */
    1772         1507 :       if (TREE_CODE (val) == INTEGER_CST)
    1773              :         {
    1774         1507 :           if (tree_int_cst_lt (pdata->maxbound, val))
    1775          657 :             pdata->maxbound = val;
    1776              :         }
    1777              :       else
    1778            0 :         pdata->maxbound = val;
    1779              :     }
    1780       242352 :   else if (pdata->maxbound || maxbound)
    1781              :     /* Set PDATA->MAXBOUND only if it either isn't INTEGER_CST or
    1782              :        if VAL corresponds to the maximum length determined based
    1783              :        on the type of the object.  */
    1784        70346 :     pdata->maxbound = val;
    1785              : 
    1786       243859 :   if (tight_bound)
    1787              :     {
    1788              :       /* VAL computed above represents an optimistically tight bound
    1789              :          on the length of the string based on the referenced object's
    1790              :          or subobject's type.  Determine the conservative upper bound
    1791              :          based on the enclosing object's size if possible.  */
    1792        22802 :       if (rkind == SRK_LENRANGE)
    1793              :         {
    1794        22802 :           poly_int64 offset;
    1795        22802 :           tree base = get_addr_base_and_unit_offset (arg, &offset);
    1796        22802 :           if (!base)
    1797              :             {
    1798              :               /* When the call above fails due to a non-constant offset
    1799              :                  assume the offset is zero and use the size of the whole
    1800              :                  enclosing object instead.  */
    1801         7837 :               base = get_base_address (arg);
    1802         7837 :               offset = 0;
    1803              :             }
    1804              :           /* If the base object is a pointer no upper bound on the length
    1805              :              can be determined.  Otherwise the maximum length is equal to
    1806              :              the size of the enclosing object minus the offset of
    1807              :              the referenced subobject minus 1 (for the terminating nul).  */
    1808        22802 :           tree type = TREE_TYPE (base);
    1809        22802 :           if (POINTER_TYPE_P (type)
    1810        22798 :               || (TREE_CODE (base) != PARM_DECL && !VAR_P (base))
    1811        41283 :               || !(val = DECL_SIZE_UNIT (base)))
    1812         5568 :             val = build_all_ones_cst (size_type_node);
    1813              :           else
    1814              :             {
    1815        17234 :               val = DECL_SIZE_UNIT (base);
    1816        17234 :               val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
    1817              :                                  size_int (offset + 1));
    1818              :             }
    1819              :         }
    1820              :       else
    1821              :         return false;
    1822              :     }
    1823              : 
    1824       243859 :   if (pdata->maxlen)
    1825              :     {
    1826              :       /* Adjust the more conservative bound if possible/necessary
    1827              :          and fail otherwise.  */
    1828         8334 :       if (rkind != SRK_STRLEN)
    1829              :         {
    1830         7403 :           if (TREE_CODE (pdata->maxlen) != INTEGER_CST
    1831         7403 :               || TREE_CODE (val) != INTEGER_CST)
    1832              :             return false;
    1833              : 
    1834         7398 :           if (tree_int_cst_lt (pdata->maxlen, val))
    1835         1376 :             pdata->maxlen = val;
    1836         7398 :           return true;
    1837              :         }
    1838          931 :       else if (simple_cst_equal (val, pdata->maxlen) != 1)
    1839              :         {
    1840              :           /* Fail if the length of this ARG is different from that
    1841              :              previously determined from another ARG.  */
    1842              :           return false;
    1843              :         }
    1844              :     }
    1845              : 
    1846       235649 :   pdata->maxlen = val;
    1847       235649 :   return rkind == SRK_LENRANGE || !integer_all_onesp (val);
    1848              : }
    1849              : 
    1850              : /* For an ARG referencing one or more strings, try to obtain the range
    1851              :    of their lengths, or the size of the largest array ARG referes to if
    1852              :    the range of lengths cannot be determined, and store all in *PDATA.
    1853              :    For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
    1854              :    the maximum constant value.
    1855              :    If ARG is an SSA_NAME, follow its use-def chains.  When RKIND ==
    1856              :    SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
    1857              :    length or if we are unable to determine the length, return false.
    1858              :    VISITED is a bitmap of visited variables.
    1859              :    RKIND determines the kind of value or range to obtain (see
    1860              :    strlen_range_kind).
    1861              :    Set PDATA->DECL if ARG refers to an unterminated constant array.
    1862              :    On input, set ELTSIZE to 1 for normal single byte character strings,
    1863              :    and either 2 or 4 for wide characer strings (the size of wchar_t).
    1864              :    Return true if *PDATA was successfully populated and false otherwise.  */
    1865              : 
    1866              : static bool
    1867      1343225 : get_range_strlen (tree arg, bitmap visited,
    1868              :                   strlen_range_kind rkind,
    1869              :                   c_strlen_data *pdata, unsigned eltsize)
    1870              : {
    1871              : 
    1872      1421049 :   if (TREE_CODE (arg) != SSA_NAME)
    1873       430424 :     return get_range_strlen_tree (arg, visited, rkind, pdata, eltsize);
    1874              : 
    1875              :   /* If ARG is registered for SSA update we cannot look at its defining
    1876              :      statement.  */
    1877       990625 :   if (name_registered_for_update_p (arg))
    1878              :     return false;
    1879              : 
    1880              :   /* If we were already here, break the infinite cycle.  */
    1881       990625 :   if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg)))
    1882              :     return true;
    1883              : 
    1884       986084 :   tree var = arg;
    1885       986084 :   gimple *def_stmt = SSA_NAME_DEF_STMT (var);
    1886              : 
    1887       986084 :   switch (gimple_code (def_stmt))
    1888              :     {
    1889       121253 :       case GIMPLE_ASSIGN:
    1890              :         /* The RHS of the statement defining VAR must either have a
    1891              :            constant length or come from another SSA_NAME with a constant
    1892              :            length.  */
    1893       121253 :         if (gimple_assign_single_p (def_stmt)
    1894       121253 :             || gimple_assign_unary_nop_p (def_stmt))
    1895              :           {
    1896        77824 :             tree rhs = gimple_assign_rhs1 (def_stmt);
    1897        77824 :             return get_range_strlen (rhs, visited, rkind, pdata, eltsize);
    1898              :           }
    1899        43429 :         else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
    1900              :           {
    1901          246 :             tree ops[2] = { gimple_assign_rhs2 (def_stmt),
    1902          246 :                             gimple_assign_rhs3 (def_stmt) };
    1903              : 
    1904          738 :             for (unsigned int i = 0; i < 2; i++)
    1905          492 :               if (!get_range_strlen (ops[i], visited, rkind, pdata, eltsize))
    1906              :                 {
    1907           28 :                   if (rkind != SRK_LENRANGE)
    1908              :                     return false;
    1909              :                   /* Set the upper bound to the maximum to prevent
    1910              :                      it from being adjusted in the next iteration but
    1911              :                      leave MINLEN and the more conservative MAXBOUND
    1912              :                      determined so far alone (or leave them null if
    1913              :                      they haven't been set yet).  That the MINLEN is
    1914              :                      in fact zero can be determined from MAXLEN being
    1915              :                      unbounded but the discovered minimum is used for
    1916              :                      diagnostics.  */
    1917           28 :                   pdata->maxlen = build_all_ones_cst (size_type_node);
    1918              :                 }
    1919              :             return true;
    1920              :           }
    1921              :         return false;
    1922              : 
    1923              :       case GIMPLE_PHI:
    1924              :         /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
    1925              :            must have a constant length.  */
    1926        71961 :         for (unsigned i = 0; i < gimple_phi_num_args (def_stmt); i++)
    1927              :           {
    1928        50909 :             tree arg = gimple_phi_arg (def_stmt, i)->def;
    1929              : 
    1930              :             /* If this PHI has itself as an argument, we cannot
    1931              :                determine the string length of this argument.  However,
    1932              :                if we can find a constant string length for the other
    1933              :                PHI args then we can still be sure that this is a
    1934              :                constant string length.  So be optimistic and just
    1935              :                continue with the next argument.  */
    1936        50909 :             if (arg == gimple_phi_result (def_stmt))
    1937            0 :               continue;
    1938              : 
    1939        50909 :             if (!get_range_strlen (arg, visited, rkind, pdata, eltsize))
    1940              :               {
    1941        28525 :                 if (rkind != SRK_LENRANGE)
    1942              :                   return false;
    1943              :                 /* Set the upper bound to the maximum to prevent
    1944              :                    it from being adjusted in the next iteration but
    1945              :                    leave MINLEN and the more conservative MAXBOUND
    1946              :                    determined so far alone (or leave them null if
    1947              :                    they haven't been set yet).  That the MINLEN is
    1948              :                    in fact zero can be determined from MAXLEN being
    1949              :                    unbounded but the discovered minimum is used for
    1950              :                    diagnostics.  */
    1951        26798 :                 pdata->maxlen = build_all_ones_cst (size_type_node);
    1952              :               }
    1953              :           }
    1954              :         return true;
    1955              : 
    1956              :       default:
    1957              :         return false;
    1958              :     }
    1959              : }
    1960              : 
    1961              : /* Try to obtain the range of the lengths of the string(s) referenced
    1962              :    by ARG, or the size of the largest array ARG refers to if the range
    1963              :    of lengths cannot be determined, and store all in *PDATA which must
    1964              :    be zero-initialized on input except PDATA->MAXBOUND may be set to
    1965              :    a non-null tree node other than INTEGER_CST to request to have it
    1966              :    set to the length of the longest string in a PHI.  ELTSIZE is
    1967              :    the expected size of the string element in bytes: 1 for char and
    1968              :    some power of 2 for wide characters.
    1969              :    Return true if the range [PDATA->MINLEN, PDATA->MAXLEN] is suitable
    1970              :    for optimization.  Returning false means that a nonzero PDATA->MINLEN
    1971              :    doesn't reflect the true lower bound of the range when PDATA->MAXLEN
    1972              :    is -1 (in that case, the actual range is indeterminate, i.e.,
    1973              :    [0, PTRDIFF_MAX - 2].  */
    1974              : 
    1975              : bool
    1976      1138816 : get_range_strlen (tree arg, c_strlen_data *pdata, unsigned eltsize)
    1977              : {
    1978      1138816 :   auto_bitmap visited;
    1979      1138816 :   tree maxbound = pdata->maxbound;
    1980              : 
    1981      1138816 :   if (!get_range_strlen (arg, visited, SRK_LENRANGE, pdata, eltsize))
    1982              :     {
    1983              :       /* On failure extend the length range to an impossible maximum
    1984              :          (a valid MAXLEN must be less than PTRDIFF_MAX - 1).  Other
    1985              :          members can stay unchanged regardless.  */
    1986       915898 :       pdata->minlen = ssize_int (0);
    1987       915898 :       pdata->maxlen = build_all_ones_cst (size_type_node);
    1988              :     }
    1989       222918 :   else if (!pdata->minlen)
    1990         9018 :     pdata->minlen = ssize_int (0);
    1991              : 
    1992              :   /* If it's unchanged from it initial non-null value, set the conservative
    1993              :      MAXBOUND to SIZE_MAX.  Otherwise leave it null (if it is null).  */
    1994      1138816 :   if (maxbound && pdata->maxbound == maxbound)
    1995       650717 :     pdata->maxbound = build_all_ones_cst (size_type_node);
    1996              : 
    1997      1138816 :   return !integer_all_onesp (pdata->maxlen);
    1998      1138816 : }
    1999              : 
    2000              : /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
    2001              :    For ARG of pointer types, NONSTR indicates if the caller is prepared
    2002              :    to handle unterminated strings.   For integer ARG and when RKIND ==
    2003              :    SRK_INT_VALUE, NONSTR must be null.
    2004              : 
    2005              :    If an unterminated array is discovered and our caller handles
    2006              :    unterminated arrays, then bubble up the offending DECL and
    2007              :    return the maximum size.  Otherwise return NULL.  */
    2008              : 
    2009              : static tree
    2010        95570 : get_maxval_strlen (tree arg, strlen_range_kind rkind, tree *nonstr = NULL)
    2011              : {
    2012              :   /* A non-null NONSTR is meaningless when determining the maximum
    2013              :      value of an integer ARG.  */
    2014        95570 :   gcc_assert (rkind != SRK_INT_VALUE || nonstr == NULL);
    2015              : 
    2016              :   // If arg is already a constant, simply return it.
    2017        95570 :   if (TREE_CODE (arg) == INTEGER_CST && rkind == SRK_INT_VALUE)
    2018              :     return arg;
    2019              : 
    2020              :   /* ARG must have an integral type when RKIND says so.  */
    2021        72866 :   gcc_assert (rkind != SRK_INT_VALUE || INTEGRAL_TYPE_P (TREE_TYPE (arg)));
    2022              : 
    2023        72965 :   auto_bitmap visited;
    2024              : 
    2025              :   /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
    2026              :      is unbounded.  */
    2027        72965 :   c_strlen_data lendata = { };
    2028        72965 :   if (!get_range_strlen (arg, visited, rkind, &lendata, /* eltsize = */1))
    2029        49845 :     lendata.maxlen = NULL_TREE;
    2030        23120 :   else if (lendata.maxlen && integer_all_onesp (lendata.maxlen))
    2031            0 :     lendata.maxlen = NULL_TREE;
    2032              : 
    2033        72965 :   if (nonstr)
    2034              :     {
    2035              :       /* For callers prepared to handle unterminated arrays set
    2036              :          *NONSTR to point to the declaration of the array and return
    2037              :          the maximum length/size. */
    2038        23916 :       *nonstr = lendata.decl;
    2039        23916 :       return lendata.maxlen;
    2040              :     }
    2041              : 
    2042              :   /* Fail if the constant array isn't nul-terminated.  */
    2043        49049 :   return lendata.decl ? NULL_TREE : lendata.maxlen;
    2044        72965 : }
    2045              : 
    2046              : /* Return true if LEN is known to be less than or equal to (or if STRICT is
    2047              :    true, strictly less than) the lower bound of SIZE at compile time and false
    2048              :    otherwise.  */
    2049              : 
    2050              : static bool
    2051        62989 : known_lower (gimple *stmt, tree len, tree size, bool strict = false)
    2052              : {
    2053        62989 :   if (len == NULL_TREE)
    2054              :     return false;
    2055              : 
    2056       234835 :   wide_int size_range[2];
    2057       234835 :   wide_int len_range[2];
    2058        46967 :   if (get_range (len, stmt, len_range) && get_range (size, stmt, size_range))
    2059              :     {
    2060        16517 :       if (strict)
    2061         1883 :         return wi::ltu_p (len_range[1], size_range[0]);
    2062              :       else
    2063        14634 :        return wi::leu_p (len_range[1], size_range[0]);
    2064              :     }
    2065              : 
    2066              :   return false;
    2067       281802 : }
    2068              : 
    2069              : /* Fold function call to builtin strcpy with arguments DEST and SRC.
    2070              :    If LEN is not NULL, it represents the length of the string to be
    2071              :    copied.  Return NULL_TREE if no simplification can be made.  */
    2072              : 
    2073              : static bool
    2074        26000 : gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
    2075              :                             tree dest, tree src)
    2076              : {
    2077        26000 :   gimple *stmt = gsi_stmt (*gsi);
    2078        26000 :   location_t loc = gimple_location (stmt);
    2079        26000 :   tree fn;
    2080              : 
    2081              :   /* If SRC and DEST are the same (and not volatile), return DEST.  */
    2082        26000 :   if (operand_equal_p (src, dest, 0))
    2083              :     {
    2084              :       /* Issue -Wrestrict unless the pointers are null (those do
    2085              :          not point to objects and so do not indicate an overlap;
    2086              :          such calls could be the result of sanitization and jump
    2087              :          threading).  */
    2088           86 :       if (!integer_zerop (dest) && !warning_suppressed_p (stmt, OPT_Wrestrict))
    2089              :         {
    2090           51 :           tree func = gimple_call_fndecl (stmt);
    2091              : 
    2092           51 :           warning_at (loc, OPT_Wrestrict,
    2093              :                       "%qD source argument is the same as destination",
    2094              :                       func);
    2095              :         }
    2096              : 
    2097           86 :       replace_call_with_value (gsi, dest);
    2098           86 :       return true;
    2099              :     }
    2100              : 
    2101        25914 :   if (optimize_function_for_size_p (cfun))
    2102              :     return false;
    2103              : 
    2104        23916 :   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    2105        23916 :   if (!fn)
    2106              :     return false;
    2107              : 
    2108              :   /* Set to non-null if ARG refers to an unterminated array.  */
    2109        23916 :   tree nonstr = NULL;
    2110        23916 :   tree len = get_maxval_strlen (src, SRK_STRLEN, &nonstr);
    2111              : 
    2112        23916 :   if (nonstr)
    2113              :     {
    2114              :       /* Avoid folding calls with unterminated arrays.  */
    2115          531 :       if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
    2116           69 :         warn_string_no_nul (loc, stmt, "strcpy", src, nonstr);
    2117          531 :       suppress_warning (stmt, OPT_Wstringop_overread);
    2118          531 :       return false;
    2119              :     }
    2120              : 
    2121        28822 :   if (!len || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    2122              :     return false;
    2123              : 
    2124         2908 :   len = fold_convert_loc (loc, size_type_node, len);
    2125         2908 :   len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
    2126         2908 :   len = force_gimple_operand_gsi (gsi, len, true,
    2127              :                                   NULL_TREE, true, GSI_SAME_STMT);
    2128         2908 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    2129         2908 :   replace_call_with_call_and_fold (gsi, repl);
    2130         2908 :   return true;
    2131              : }
    2132              : 
    2133              : /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
    2134              :    If SLEN is not NULL, it represents the length of the source string.
    2135              :    Return NULL_TREE if no simplification can be made.  */
    2136              : 
    2137              : static bool
    2138        17266 : gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
    2139              :                              tree dest, tree src, tree len)
    2140              : {
    2141        17266 :   gimple *stmt = gsi_stmt (*gsi);
    2142        17266 :   location_t loc = gimple_location (stmt);
    2143        17266 :   bool nonstring = get_attr_nonstring_decl (dest) != NULL_TREE;
    2144              : 
    2145              :   /* If the LEN parameter is zero, return DEST.  */
    2146        17266 :   if (integer_zerop (len))
    2147              :     {
    2148              :       /* Avoid warning if the destination refers to an array/pointer
    2149              :          decorate with attribute nonstring.  */
    2150          167 :       if (!nonstring)
    2151              :         {
    2152          155 :           tree fndecl = gimple_call_fndecl (stmt);
    2153              : 
    2154              :           /* Warn about the lack of nul termination: the result is not
    2155              :              a (nul-terminated) string.  */
    2156          155 :           tree slen = get_maxval_strlen (src, SRK_STRLEN);
    2157          155 :           if (slen && !integer_zerop (slen))
    2158           24 :             warning_at (loc, OPT_Wstringop_truncation,
    2159              :                         "%qD destination unchanged after copying no bytes "
    2160              :                         "from a string of length %E",
    2161              :                         fndecl, slen);
    2162              :           else
    2163          131 :             warning_at (loc, OPT_Wstringop_truncation,
    2164              :                         "%qD destination unchanged after copying no bytes",
    2165              :                         fndecl);
    2166              :         }
    2167              : 
    2168          167 :       replace_call_with_value (gsi, dest);
    2169          167 :       return true;
    2170              :     }
    2171              : 
    2172              :   /* We can't compare slen with len as constants below if len is not a
    2173              :      constant.  */
    2174        17099 :   if (TREE_CODE (len) != INTEGER_CST)
    2175              :     return false;
    2176              : 
    2177              :   /* Now, we must be passed a constant src ptr parameter.  */
    2178        10744 :   tree slen = get_maxval_strlen (src, SRK_STRLEN);
    2179        10744 :   if (!slen || TREE_CODE (slen) != INTEGER_CST)
    2180              :     return false;
    2181              : 
    2182              :   /* The size of the source string including the terminating nul.  */
    2183         1780 :   tree ssize = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
    2184              : 
    2185              :   /* We do not support simplification of this case, though we do
    2186              :      support it when expanding trees into RTL.  */
    2187              :   /* FIXME: generate a call to __builtin_memset.  */
    2188         1780 :   if (tree_int_cst_lt (ssize, len))
    2189              :     return false;
    2190              : 
    2191              :   /* Diagnose truncation that leaves the copy unterminated.  */
    2192          695 :   maybe_diag_stxncpy_trunc (*gsi, src, len);
    2193              : 
    2194              :   /* OK transform into builtin memcpy.  */
    2195          695 :   tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    2196        17794 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    2197              :     return false;
    2198              : 
    2199          695 :   len = fold_convert_loc (loc, size_type_node, len);
    2200          695 :   len = force_gimple_operand_gsi (gsi, len, true,
    2201              :                                   NULL_TREE, true, GSI_SAME_STMT);
    2202          695 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    2203          695 :   replace_call_with_call_and_fold (gsi, repl);
    2204              : 
    2205          695 :   return true;
    2206              : }
    2207              : 
    2208              : /* Fold function call to builtin strchr or strrchr.
    2209              :    If both arguments are constant, evaluate and fold the result,
    2210              :    otherwise simplify str(r)chr (str, 0) into str + strlen (str).
    2211              :    In general strlen is significantly faster than strchr
    2212              :    due to being a simpler operation.  */
    2213              : static bool
    2214         5536 : gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
    2215              : {
    2216         5536 :   gimple *stmt = gsi_stmt (*gsi);
    2217         5536 :   tree str = gimple_call_arg (stmt, 0);
    2218         5536 :   tree c = gimple_call_arg (stmt, 1);
    2219         5536 :   location_t loc = gimple_location (stmt);
    2220         5536 :   const char *p;
    2221         5536 :   char ch;
    2222              : 
    2223         5536 :   if (!gimple_call_lhs (stmt))
    2224              :     return false;
    2225              : 
    2226              :   /* Avoid folding if the first argument is not a nul-terminated array.
    2227              :      Defer warning until later.  */
    2228         5526 :   if (!check_nul_terminated_array (NULL_TREE, str))
    2229              :     return false;
    2230              : 
    2231         5442 :   if ((p = c_getstr (str)) && target_char_cst_p (c, &ch))
    2232              :     {
    2233           41 :       const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch);
    2234              : 
    2235           41 :       if (p1 == NULL)
    2236              :         {
    2237            1 :           replace_call_with_value (gsi, integer_zero_node);
    2238            1 :           return true;
    2239              :         }
    2240              : 
    2241           40 :       tree len = build_int_cst (size_type_node, p1 - p);
    2242           40 :       gimple_seq stmts = NULL;
    2243           40 :       gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
    2244              :                                               POINTER_PLUS_EXPR, str, len);
    2245           40 :       gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2246           40 :       gsi_replace_with_seq_vops (gsi, stmts);
    2247           40 :       return true;
    2248              :     }
    2249              : 
    2250         5483 :   if (!integer_zerop (c) || (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun)))
    2251              :     return false;
    2252              : 
    2253              :   /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size.  */
    2254           82 :   if (is_strrchr && optimize_function_for_size_p (cfun))
    2255              :     {
    2256            3 :       tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
    2257              : 
    2258            3 :       if (strchr_fn)
    2259              :         {
    2260            3 :           gimple *repl = gimple_build_call (strchr_fn, 2, str, c);
    2261            3 :           replace_call_with_call_and_fold (gsi, repl);
    2262            3 :           return true;
    2263              :         }
    2264              : 
    2265              :       return false;
    2266              :     }
    2267              : 
    2268           79 :   tree len;
    2269         5492 :   tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
    2270              : 
    2271           79 :   if (!strlen_fn)
    2272              :     return false;
    2273              : 
    2274              :   /* Create newstr = strlen (str).  */
    2275           79 :   gimple_seq stmts = NULL;
    2276           79 :   gimple *new_stmt = gimple_build_call (strlen_fn, 1, str);
    2277           79 :   gimple_set_location (new_stmt, loc);
    2278           79 :   len = make_ssa_name (size_type_node);
    2279           79 :   gimple_call_set_lhs (new_stmt, len);
    2280           79 :   gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2281              : 
    2282              :   /* Create (str p+ strlen (str)).  */
    2283           79 :   new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
    2284              :                                   POINTER_PLUS_EXPR, str, len);
    2285           79 :   gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2286           79 :   gsi_replace_with_seq_vops (gsi, stmts);
    2287              :   /* gsi now points at the assignment to the lhs, get a
    2288              :      stmt iterator to the strlen.
    2289              :      ???  We can't use gsi_for_stmt as that doesn't work when the
    2290              :      CFG isn't built yet.  */
    2291           79 :   gimple_stmt_iterator gsi2 = *gsi;
    2292           79 :   gsi_prev (&gsi2);
    2293           79 :   fold_stmt (&gsi2);
    2294           79 :   return true;
    2295              : }
    2296              : 
    2297              : /* Fold function call to builtin strstr.
    2298              :    If both arguments are constant, evaluate and fold the result,
    2299              :    additionally fold strstr (x, "") into x and strstr (x, "c")
    2300              :    into strchr (x, 'c').  */
    2301              : static bool
    2302         4211 : gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
    2303              : {
    2304         4211 :   gimple *stmt = gsi_stmt (*gsi);
    2305         4211 :   if (!gimple_call_lhs (stmt))
    2306              :     return false;
    2307              : 
    2308         4208 :   tree haystack = gimple_call_arg (stmt, 0);
    2309         4208 :   tree needle = gimple_call_arg (stmt, 1);
    2310              : 
    2311              :   /* Avoid folding if either argument is not a nul-terminated array.
    2312              :      Defer warning until later.  */
    2313         4208 :   if (!check_nul_terminated_array (NULL_TREE, haystack)
    2314         4208 :       || !check_nul_terminated_array (NULL_TREE, needle))
    2315           19 :     return false;
    2316              : 
    2317         4189 :   const char *q = c_getstr (needle);
    2318         4189 :   if (q == NULL)
    2319              :     return false;
    2320              : 
    2321         3052 :   if (const char *p = c_getstr (haystack))
    2322              :     {
    2323           14 :       const char *r = strstr (p, q);
    2324              : 
    2325           14 :       if (r == NULL)
    2326              :         {
    2327            1 :           replace_call_with_value (gsi, integer_zero_node);
    2328            1 :           return true;
    2329              :         }
    2330              : 
    2331           13 :       tree len = build_int_cst (size_type_node, r - p);
    2332           13 :       gimple_seq stmts = NULL;
    2333           13 :       gimple *new_stmt
    2334           13 :         = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR,
    2335              :                                haystack, len);
    2336           13 :       gimple_seq_add_stmt_without_update (&stmts, new_stmt);
    2337           13 :       gsi_replace_with_seq_vops (gsi, stmts);
    2338           13 :       return true;
    2339              :     }
    2340              : 
    2341              :   /* For strstr (x, "") return x.  */
    2342         3038 :   if (q[0] == '\0')
    2343              :     {
    2344            6 :       replace_call_with_value (gsi, haystack);
    2345            6 :       return true;
    2346              :     }
    2347              : 
    2348        10233 :   if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
    2349              :     return false;
    2350              : 
    2351              :   /* Transform strstr (x, "c") into strchr (x, 'c').  */
    2352         3032 :   if (q[1] == '\0')
    2353              :     {
    2354           22 :       tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
    2355           22 :       if (strchr_fn)
    2356              :         {
    2357           22 :           tree c = build_int_cst (integer_type_node, q[0]);
    2358           22 :           gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c);
    2359           22 :           replace_call_with_call_and_fold (gsi, repl);
    2360           22 :           return true;
    2361              :         }
    2362              :     }
    2363              : 
    2364              :   return false;
    2365              : }
    2366              : 
    2367              : /* Simplify a call to the strcat builtin.  DST and SRC are the arguments
    2368              :    to the call.
    2369              : 
    2370              :    Return NULL_TREE if no simplification was possible, otherwise return the
    2371              :    simplified form of the call as a tree.
    2372              : 
    2373              :    The simplified form may be a constant or other expression which
    2374              :    computes the same value, but in a more efficient manner (including
    2375              :    calls to other builtin functions).
    2376              : 
    2377              :    The call may contain arguments which need to be evaluated, but
    2378              :    which are not useful to determine the result of the call.  In
    2379              :    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
    2380              :    COMPOUND_EXPR will be an argument which must be evaluated.
    2381              :    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
    2382              :    COMPOUND_EXPR in the chain will contain the tree for the simplified
    2383              :    form of the builtin function call.  */
    2384              : 
    2385              : static bool
    2386         7328 : gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
    2387              : {
    2388         7328 :   gimple *stmt = gsi_stmt (*gsi);
    2389         7328 :   location_t loc = gimple_location (stmt);
    2390              : 
    2391         7328 :   const char *p = c_getstr (src);
    2392              : 
    2393              :   /* If the string length is zero, return the dst parameter.  */
    2394         7328 :   if (p && *p == '\0')
    2395              :     {
    2396           72 :       replace_call_with_value (gsi, dst);
    2397           72 :       return true;
    2398              :     }
    2399              : 
    2400         7256 :   if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
    2401              :     return false;
    2402              : 
    2403        19865 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    2404              :     return false;
    2405              : 
    2406              :   /* See if we can store by pieces into (dst + strlen(dst)).  */
    2407         6673 :   tree newdst;
    2408         6673 :   tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
    2409         6673 :   tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    2410              : 
    2411         6673 :   if (!strlen_fn || !memcpy_fn)
    2412              :     return false;
    2413              : 
    2414              :   /* If the length of the source string isn't computable don't
    2415              :      split strcat into strlen and memcpy.  */
    2416         6673 :   tree len = get_maxval_strlen (src, SRK_STRLEN);
    2417         6673 :   if (! len)
    2418              :     return false;
    2419              : 
    2420              :   /* Create strlen (dst).  */
    2421          737 :   gimple_seq stmts = NULL, stmts2;
    2422          737 :   gimple *repl = gimple_build_call (strlen_fn, 1, dst);
    2423          737 :   gimple_set_location (repl, loc);
    2424          737 :   newdst = make_ssa_name (size_type_node);
    2425          737 :   gimple_call_set_lhs (repl, newdst);
    2426          737 :   gimple_seq_add_stmt_without_update (&stmts, repl);
    2427              : 
    2428              :   /* Create (dst p+ strlen (dst)).  */
    2429          737 :   newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
    2430          737 :   newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
    2431          737 :   gimple_seq_add_seq_without_update (&stmts, stmts2);
    2432              : 
    2433          737 :   len = fold_convert_loc (loc, size_type_node, len);
    2434          737 :   len = size_binop_loc (loc, PLUS_EXPR, len,
    2435              :                         build_int_cst (size_type_node, 1));
    2436          737 :   len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
    2437          737 :   gimple_seq_add_seq_without_update (&stmts, stmts2);
    2438              : 
    2439          737 :   repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
    2440          737 :   gimple_seq_add_stmt_without_update (&stmts, repl);
    2441          737 :   if (gimple_call_lhs (stmt))
    2442              :     {
    2443          165 :       repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
    2444          165 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    2445          165 :       gsi_replace_with_seq_vops (gsi, stmts);
    2446              :       /* gsi now points at the assignment to the lhs, get a
    2447              :          stmt iterator to the memcpy call.
    2448              :          ???  We can't use gsi_for_stmt as that doesn't work when the
    2449              :          CFG isn't built yet.  */
    2450          165 :       gimple_stmt_iterator gsi2 = *gsi;
    2451          165 :       gsi_prev (&gsi2);
    2452          165 :       fold_stmt (&gsi2);
    2453              :     }
    2454              :   else
    2455              :     {
    2456          572 :       gsi_replace_with_seq_vops (gsi, stmts);
    2457          572 :       fold_stmt (gsi);
    2458              :     }
    2459              :   return true;
    2460              : }
    2461              : 
    2462              : /* Fold a call to the __strcat_chk builtin FNDECL.  DEST, SRC, and SIZE
    2463              :    are the arguments to the call.  */
    2464              : 
    2465              : static bool
    2466         1714 : gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
    2467              : {
    2468         1714 :   gimple *stmt = gsi_stmt (*gsi);
    2469         1714 :   tree dest = gimple_call_arg (stmt, 0);
    2470         1714 :   tree src = gimple_call_arg (stmt, 1);
    2471         1714 :   tree size = gimple_call_arg (stmt, 2);
    2472         1714 :   tree fn;
    2473         1714 :   const char *p;
    2474              : 
    2475         1714 :   p = c_getstr (src);
    2476              :   /* If the SRC parameter is "", return DEST.  */
    2477         1714 :   if (p && *p == '\0')
    2478              :     {
    2479           60 :       replace_call_with_value (gsi, dest);
    2480           60 :       return true;
    2481              :     }
    2482              : 
    2483         1654 :   if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
    2484         1572 :     return false;
    2485              : 
    2486         1736 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    2487              :     return false;
    2488              : 
    2489              :   /* If __builtin_strcat_chk is used, assume strcat is available.  */
    2490           82 :   fn = builtin_decl_explicit (BUILT_IN_STRCAT);
    2491           82 :   if (!fn)
    2492              :     return false;
    2493              : 
    2494           82 :   gimple *repl = gimple_build_call (fn, 2, dest, src);
    2495           82 :   replace_call_with_call_and_fold (gsi, repl);
    2496           82 :   return true;
    2497              : }
    2498              : 
    2499              : /* Simplify a call to the strncat builtin.  */
    2500              : 
    2501              : static bool
    2502         6786 : gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
    2503              : {
    2504         6786 :   gimple *stmt = gsi_stmt (*gsi);
    2505         6786 :   tree dst = gimple_call_arg (stmt, 0);
    2506         6786 :   tree src = gimple_call_arg (stmt, 1);
    2507         6786 :   tree len = gimple_call_arg (stmt, 2);
    2508         6786 :   tree src_len = c_strlen (src, 1);
    2509              : 
    2510              :   /* If the requested length is zero, or the src parameter string
    2511              :      length is zero, return the dst parameter.  */
    2512         6786 :   if (integer_zerop (len) || (src_len && integer_zerop (src_len)))
    2513              :     {
    2514          119 :       replace_call_with_value (gsi, dst);
    2515          119 :       return true;
    2516              :     }
    2517              : 
    2518              :   /* Return early if the requested len is less than the string length.
    2519              :      Warnings will be issued elsewhere later.  */
    2520         6667 :   if (!src_len || known_lower (stmt, len, src_len, true))
    2521         6099 :     return false;
    2522              : 
    2523              :   /* Warn on constant LEN.  */
    2524          568 :   if (TREE_CODE (len) == INTEGER_CST)
    2525              :     {
    2526          131 :       bool nowarn = warning_suppressed_p (stmt, OPT_Wstringop_overflow_);
    2527          131 :       tree dstsize;
    2528              : 
    2529          131 :       if (!nowarn && compute_builtin_object_size (dst, 1, &dstsize)
    2530          175 :           && TREE_CODE (dstsize) == INTEGER_CST)
    2531              :         {
    2532           44 :           int cmpdst = tree_int_cst_compare (len, dstsize);
    2533              : 
    2534           44 :           if (cmpdst >= 0)
    2535              :             {
    2536           19 :               tree fndecl = gimple_call_fndecl (stmt);
    2537              : 
    2538              :               /* Strncat copies (at most) LEN bytes and always appends
    2539              :                  the terminating NUL so the specified bound should never
    2540              :                  be equal to (or greater than) the size of the destination.
    2541              :                  If it is, the copy could overflow.  */
    2542           19 :               location_t loc = gimple_location (stmt);
    2543           37 :               nowarn = warning_at (loc, OPT_Wstringop_overflow_,
    2544              :                                    cmpdst == 0
    2545              :                                    ? G_("%qD specified bound %E equals "
    2546              :                                         "destination size")
    2547              :                                    : G_("%qD specified bound %E exceeds "
    2548              :                                         "destination size %E"),
    2549              :                                    fndecl, len, dstsize);
    2550           19 :               if (nowarn)
    2551            0 :                 suppress_warning (stmt, OPT_Wstringop_overflow_);
    2552              :             }
    2553              :         }
    2554              : 
    2555          131 :       if (!nowarn && TREE_CODE (src_len) == INTEGER_CST
    2556          243 :           && tree_int_cst_compare (src_len, len) == 0)
    2557              :         {
    2558           20 :           tree fndecl = gimple_call_fndecl (stmt);
    2559           20 :           location_t loc = gimple_location (stmt);
    2560              : 
    2561              :           /* To avoid possible overflow the specified bound should also
    2562              :              not be equal to the length of the source, even when the size
    2563              :              of the destination is unknown (it's not an uncommon mistake
    2564              :              to specify as the bound to strncpy the length of the source).  */
    2565           20 :           if (warning_at (loc, OPT_Wstringop_overflow_,
    2566              :                           "%qD specified bound %E equals source length",
    2567              :                           fndecl, len))
    2568            6 :             suppress_warning (stmt, OPT_Wstringop_overflow_);
    2569              :         }
    2570              :     }
    2571              : 
    2572          568 :   if (!known_lower (stmt, src_len, len))
    2573              :     return false;
    2574              : 
    2575          136 :   tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
    2576              : 
    2577              :   /* If the replacement _DECL isn't initialized, don't do the
    2578              :      transformation.  */
    2579         6803 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    2580              :     return false;
    2581              : 
    2582              :   /* Otherwise, emit a call to strcat.  */
    2583          136 :   gcall *repl = gimple_build_call (fn, 2, dst, src);
    2584          136 :   replace_call_with_call_and_fold (gsi, repl);
    2585          136 :   return true;
    2586              : }
    2587              : 
    2588              : /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
    2589              :    LEN, and SIZE.  */
    2590              : 
    2591              : static bool
    2592         1143 : gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
    2593              : {
    2594         1143 :   gimple *stmt = gsi_stmt (*gsi);
    2595         1143 :   tree dest = gimple_call_arg (stmt, 0);
    2596         1143 :   tree src = gimple_call_arg (stmt, 1);
    2597         1143 :   tree len = gimple_call_arg (stmt, 2);
    2598         1143 :   tree size = gimple_call_arg (stmt, 3);
    2599         1143 :   tree fn;
    2600         1143 :   const char *p;
    2601              : 
    2602         1143 :   p = c_getstr (src);
    2603              :   /* If the SRC parameter is "" or if LEN is 0, return DEST.  */
    2604          302 :   if ((p && *p == '\0')
    2605         1394 :       || integer_zerop (len))
    2606              :     {
    2607           78 :       replace_call_with_value (gsi, dest);
    2608           78 :       return true;
    2609              :     }
    2610              : 
    2611         3043 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    2612              :     return false;
    2613              : 
    2614         1065 :   if (! integer_all_onesp (size))
    2615              :     {
    2616          978 :       tree src_len = c_strlen (src, 1);
    2617          978 :       if (known_lower (stmt, src_len, len))
    2618              :         {
    2619              :           /* If LEN >= strlen (SRC), optimize into __strcat_chk.  */
    2620           65 :           fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
    2621           65 :           if (!fn)
    2622              :             return false;
    2623              : 
    2624           65 :           gimple *repl = gimple_build_call (fn, 3, dest, src, size);
    2625           65 :           replace_call_with_call_and_fold (gsi, repl);
    2626           65 :           return true;
    2627              :         }
    2628              :       return false;
    2629              :     }
    2630              : 
    2631              :   /* If __builtin_strncat_chk is used, assume strncat is available.  */
    2632           87 :   fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
    2633           87 :   if (!fn)
    2634              :     return false;
    2635              : 
    2636           87 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    2637           87 :   replace_call_with_call_and_fold (gsi, repl);
    2638           87 :   return true;
    2639              : }
    2640              : 
    2641              : /* Build and append gimple statements to STMTS that would load a first
    2642              :    character of a memory location identified by STR.  LOC is location
    2643              :    of the statement.  */
    2644              : 
    2645              : static tree
    2646          469 : gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts)
    2647              : {
    2648          469 :   tree var;
    2649              : 
    2650          469 :   tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
    2651          469 :   tree cst_uchar_ptr_node
    2652          469 :     = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
    2653          469 :   tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
    2654              : 
    2655          469 :   tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0);
    2656          469 :   gassign *stmt = gimple_build_assign (NULL_TREE, temp);
    2657          469 :   var = make_ssa_name (cst_uchar_node, stmt);
    2658              : 
    2659          469 :   gimple_assign_set_lhs (stmt, var);
    2660          469 :   gimple_seq_add_stmt_without_update (stmts, stmt);
    2661              : 
    2662          469 :   return var;
    2663              : }
    2664              : 
    2665              : /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.  */
    2666              : 
    2667              : static bool
    2668      1250415 : gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
    2669              : {
    2670      1250415 :   gimple *stmt = gsi_stmt (*gsi);
    2671      1250415 :   tree callee = gimple_call_fndecl (stmt);
    2672      1250415 :   enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
    2673              : 
    2674      1250415 :   tree type = integer_type_node;
    2675      1250415 :   tree str1 = gimple_call_arg (stmt, 0);
    2676      1250415 :   tree str2 = gimple_call_arg (stmt, 1);
    2677      1250415 :   tree lhs = gimple_call_lhs (stmt);
    2678              : 
    2679      1250415 :   tree bound_node = NULL_TREE;
    2680      1250415 :   unsigned HOST_WIDE_INT bound = HOST_WIDE_INT_M1U;
    2681              : 
    2682              :   /* Handle strncmp and strncasecmp functions.  */
    2683      1250415 :   if (gimple_call_num_args (stmt) == 3)
    2684              :     {
    2685        22892 :       bound_node = gimple_call_arg (stmt, 2);
    2686        22892 :       if (tree_fits_uhwi_p (bound_node))
    2687        17173 :         bound = tree_to_uhwi (bound_node);
    2688              :     }
    2689              : 
    2690              :   /* If the BOUND parameter is zero, return zero.  */
    2691        17173 :   if (bound == 0)
    2692              :     {
    2693            4 :       replace_call_with_value (gsi, integer_zero_node);
    2694            4 :       return true;
    2695              :     }
    2696              : 
    2697              :   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
    2698      1250411 :   if (operand_equal_p (str1, str2, 0))
    2699              :     {
    2700           41 :       replace_call_with_value (gsi, integer_zero_node);
    2701           41 :       return true;
    2702              :     }
    2703              : 
    2704      2500740 :   if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
    2705              :     return false;
    2706              : 
    2707              :   /* Initially set to the number of characters, including the terminating
    2708              :      nul if each array has one.   LENx == strnlen (Sx, LENx) implies that
    2709              :      the array Sx is not terminated by a nul.
    2710              :      For nul-terminated strings then adjusted to their length so that
    2711              :      LENx == NULPOSx holds.  */
    2712      1250370 :   unsigned HOST_WIDE_INT len1 = HOST_WIDE_INT_MAX, len2 = len1;
    2713      1250370 :   const char *p1 = getbyterep (str1, &len1);
    2714      1250370 :   const char *p2 = getbyterep (str2, &len2);
    2715              : 
    2716              :   /* The position of the terminating nul character if one exists, otherwise
    2717              :      a value greater than LENx.  */
    2718      1250370 :   unsigned HOST_WIDE_INT nulpos1 = HOST_WIDE_INT_MAX, nulpos2 = nulpos1;
    2719              : 
    2720      1250370 :   if (p1)
    2721              :     {
    2722        42809 :       size_t n = strnlen (p1, len1);
    2723        42809 :       if (n < len1)
    2724        42702 :         len1 = nulpos1 = n;
    2725              :     }
    2726              : 
    2727      1250370 :   if (p2)
    2728              :     {
    2729      1219583 :       size_t n = strnlen (p2, len2);
    2730      1219583 :       if (n < len2)
    2731      1219524 :         len2 = nulpos2 = n;
    2732              :     }
    2733              : 
    2734              :   /* For known strings, return an immediate value.  */
    2735      1250370 :   if (p1 && p2)
    2736              :     {
    2737        39245 :       int r = 0;
    2738        39245 :       bool known_result = false;
    2739              : 
    2740        39245 :       switch (fcode)
    2741              :         {
    2742        38169 :         case BUILT_IN_STRCMP:
    2743        38169 :         case BUILT_IN_STRCMP_EQ:
    2744        38169 :           if (len1 != nulpos1 || len2 != nulpos2)
    2745              :             break;
    2746              : 
    2747        38144 :           r = strcmp (p1, p2);
    2748        38144 :           known_result = true;
    2749        38144 :           break;
    2750              : 
    2751         1002 :         case BUILT_IN_STRNCMP:
    2752         1002 :         case BUILT_IN_STRNCMP_EQ:
    2753         1002 :           {
    2754         1002 :             if (bound == HOST_WIDE_INT_M1U)
    2755              :               break;
    2756              : 
    2757              :             /* Reduce the bound to be no more than the length
    2758              :                of the shorter of the two strings, or the sizes
    2759              :                of the unterminated arrays.  */
    2760           38 :             unsigned HOST_WIDE_INT n = bound;
    2761              : 
    2762           38 :             if (len1 == nulpos1 && len1 < n)
    2763            4 :               n = len1 + 1;
    2764           38 :             if (len2 == nulpos2 && len2 < n)
    2765           11 :               n = len2 + 1;
    2766              : 
    2767           38 :             if (MIN (nulpos1, nulpos2) + 1 < n)
    2768              :               break;
    2769              : 
    2770           38 :             r = strncmp (p1, p2, n);
    2771           38 :             known_result = true;
    2772           38 :             break;
    2773              :           }
    2774              :         /* Only handleable situation is where the string are equal (result 0),
    2775              :            which is already handled by operand_equal_p case.  */
    2776              :         case BUILT_IN_STRCASECMP:
    2777              :           break;
    2778           37 :         case BUILT_IN_STRNCASECMP:
    2779           37 :           {
    2780           37 :             if (bound == HOST_WIDE_INT_M1U)
    2781              :               break;
    2782           37 :             r = strncmp (p1, p2, bound);
    2783           37 :             if (r == 0)
    2784              :               known_result = true;
    2785              :             break;
    2786              :           }
    2787            0 :         default:
    2788            0 :           gcc_unreachable ();
    2789              :         }
    2790              : 
    2791        38182 :       if (known_result)
    2792              :         {
    2793        38182 :           replace_call_with_value (gsi, build_cmp_result (type, r));
    2794        38182 :           return true;
    2795              :         }
    2796              :     }
    2797              : 
    2798      2424376 :   bool nonzero_bound = (bound >= 1 && bound < HOST_WIDE_INT_M1U)
    2799      1195159 :     || fcode == BUILT_IN_STRCMP
    2800      1195159 :     || fcode == BUILT_IN_STRCMP_EQ
    2801      1218085 :     || fcode == BUILT_IN_STRCASECMP;
    2802              : 
    2803      1212188 :   location_t loc = gimple_location (stmt);
    2804              : 
    2805              :   /* If the second arg is "", return *(const unsigned char*)arg1.  */
    2806      1212188 :   if (p2 && *p2 == '\0' && nonzero_bound)
    2807              :     {
    2808          150 :       gimple_seq stmts = NULL;
    2809          150 :       tree var = gimple_load_first_char (loc, str1, &stmts);
    2810          150 :       if (lhs)
    2811              :         {
    2812          150 :           stmt = gimple_build_assign (lhs, NOP_EXPR, var);
    2813          150 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2814              :         }
    2815              : 
    2816          150 :       gsi_replace_with_seq_vops (gsi, stmts);
    2817          150 :       return true;
    2818              :     }
    2819              : 
    2820              :   /* If the first arg is "", return -*(const unsigned char*)arg2.  */
    2821      1212038 :   if (p1 && *p1 == '\0' && nonzero_bound)
    2822              :     {
    2823           99 :       gimple_seq stmts = NULL;
    2824           99 :       tree var = gimple_load_first_char (loc, str2, &stmts);
    2825              : 
    2826           99 :       if (lhs)
    2827              :         {
    2828           99 :           tree c = make_ssa_name (integer_type_node);
    2829           99 :           stmt = gimple_build_assign (c, NOP_EXPR, var);
    2830           99 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2831              : 
    2832           99 :           stmt = gimple_build_assign (lhs, NEGATE_EXPR, c);
    2833           99 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2834              :         }
    2835              : 
    2836           99 :       gsi_replace_with_seq_vops (gsi, stmts);
    2837           99 :       return true;
    2838              :     }
    2839              : 
    2840              :   /* If BOUND is one, return an expression corresponding to
    2841              :      (*(const unsigned char*)arg2 - *(const unsigned char*)arg1).  */
    2842      1211939 :   if (fcode == BUILT_IN_STRNCMP && bound == 1)
    2843              :     {
    2844          110 :       gimple_seq stmts = NULL;
    2845          110 :       tree temp1 = gimple_load_first_char (loc, str1, &stmts);
    2846          110 :       tree temp2 = gimple_load_first_char (loc, str2, &stmts);
    2847              : 
    2848          110 :       if (lhs)
    2849              :         {
    2850          107 :           tree c1 = make_ssa_name (integer_type_node);
    2851          107 :           gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1);
    2852          107 :           gimple_seq_add_stmt_without_update (&stmts, convert1);
    2853              : 
    2854          107 :           tree c2 = make_ssa_name (integer_type_node);
    2855          107 :           gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2);
    2856          107 :           gimple_seq_add_stmt_without_update (&stmts, convert2);
    2857              : 
    2858          107 :           stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2);
    2859          107 :           gimple_seq_add_stmt_without_update (&stmts, stmt);
    2860              :         }
    2861              : 
    2862          110 :       gsi_replace_with_seq_vops (gsi, stmts);
    2863          110 :       return true;
    2864              :     }
    2865              : 
    2866              :   /* If BOUND is greater than the length of one constant string,
    2867              :      and the other argument is also a nul-terminated string, replace
    2868              :      strncmp with strcmp.  */
    2869      1211829 :   if (fcode == BUILT_IN_STRNCMP
    2870        17626 :       && bound > 0 && bound < HOST_WIDE_INT_M1U
    2871        12035 :       && ((p2 && len2 < bound && len2 == nulpos2)
    2872        11803 :           || (p1 && len1 < bound && len1 == nulpos1)))
    2873              :     {
    2874      1211829 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCMP);
    2875          310 :       if (!fn)
    2876              :         return false;
    2877          310 :       gimple *repl = gimple_build_call (fn, 2, str1, str2);
    2878          310 :       replace_call_with_call_and_fold (gsi, repl);
    2879          310 :       return true;
    2880              :     }
    2881              : 
    2882              :   return false;
    2883              : }
    2884              : 
    2885              : /* Fold a call to the memchr pointed by GSI iterator.  */
    2886              : 
    2887              : static bool
    2888        35505 : gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
    2889              : {
    2890        35505 :   gimple *stmt = gsi_stmt (*gsi);
    2891        35505 :   tree lhs = gimple_call_lhs (stmt);
    2892        35505 :   tree arg1 = gimple_call_arg (stmt, 0);
    2893        35505 :   tree arg2 = gimple_call_arg (stmt, 1);
    2894        35505 :   tree len = gimple_call_arg (stmt, 2);
    2895              : 
    2896              :   /* If the LEN parameter is zero, return zero.  */
    2897        35505 :   if (integer_zerop (len))
    2898              :     {
    2899            1 :       replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
    2900            1 :       return true;
    2901              :     }
    2902              : 
    2903        35504 :   char c;
    2904        35504 :   if (TREE_CODE (arg2) != INTEGER_CST
    2905        20452 :       || !tree_fits_uhwi_p (len)
    2906        36216 :       || !target_char_cst_p (arg2, &c))
    2907        34792 :     return false;
    2908              : 
    2909          712 :   unsigned HOST_WIDE_INT length = tree_to_uhwi (len);
    2910          712 :   unsigned HOST_WIDE_INT string_length;
    2911          712 :   const char *p1 = getbyterep (arg1, &string_length);
    2912              : 
    2913          712 :   if (p1)
    2914              :     {
    2915           94 :       const char *r = (const char *)memchr (p1, c, MIN (length, string_length));
    2916           94 :       if (r == NULL)
    2917              :         {
    2918           14 :           tree mem_size, offset_node;
    2919           14 :           byte_representation (arg1, &offset_node, &mem_size, NULL);
    2920           14 :           unsigned HOST_WIDE_INT offset = (offset_node == NULL_TREE)
    2921           14 :                                           ? 0 : tree_to_uhwi (offset_node);
    2922              :           /* MEM_SIZE is the size of the array the string literal
    2923              :              is stored in.  */
    2924           14 :           unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size) - offset;
    2925           14 :           gcc_checking_assert (string_length <= string_size);
    2926           14 :           if (length <= string_size)
    2927              :             {
    2928            4 :               replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
    2929            4 :               return true;
    2930              :             }
    2931              :         }
    2932              :       else
    2933              :         {
    2934           80 :           unsigned HOST_WIDE_INT offset = r - p1;
    2935           80 :           gimple_seq stmts = NULL;
    2936           80 :           if (lhs != NULL_TREE)
    2937              :             {
    2938           78 :               tree offset_cst = build_int_cst (sizetype, offset);
    2939           78 :               gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR,
    2940              :                                                    arg1, offset_cst);
    2941           78 :               gimple_seq_add_stmt_without_update (&stmts, stmt);
    2942              :             }
    2943              :           else
    2944            2 :             gimple_seq_add_stmt_without_update (&stmts,
    2945              :                                                 gimple_build_nop ());
    2946              : 
    2947           80 :           gsi_replace_with_seq_vops (gsi, stmts);
    2948           80 :           return true;
    2949              :         }
    2950              :     }
    2951              : 
    2952              :   return false;
    2953              : }
    2954              : 
    2955              : /* Fold a call to the fputs builtin.  ARG0 and ARG1 are the arguments
    2956              :    to the call.  IGNORE is true if the value returned
    2957              :    by the builtin will be ignored.  UNLOCKED is true is true if this
    2958              :    actually a call to fputs_unlocked.  If LEN in non-NULL, it represents
    2959              :    the known length of the string.  Return NULL_TREE if no simplification
    2960              :    was possible.  */
    2961              : 
    2962              : static bool
    2963        20686 : gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
    2964              :                            tree arg0, tree arg1,
    2965              :                            bool unlocked)
    2966              : {
    2967        20686 :   gimple *stmt = gsi_stmt (*gsi);
    2968              : 
    2969              :   /* If we're using an unlocked function, assume the other unlocked
    2970              :      functions exist explicitly.  */
    2971        20686 :   tree const fn_fputc = (unlocked
    2972        20686 :                          ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
    2973        20643 :                          : builtin_decl_implicit (BUILT_IN_FPUTC));
    2974        20643 :   tree const fn_fwrite = (unlocked
    2975           43 :                           ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
    2976        20686 :                           : builtin_decl_implicit (BUILT_IN_FWRITE));
    2977              : 
    2978              :   /* If the return value is used, don't do the transformation.  */
    2979        20686 :   if (gimple_call_lhs (stmt))
    2980              :     return false;
    2981              : 
    2982              :   /* Get the length of the string passed to fputs.  If the length
    2983              :      can't be determined, punt.  */
    2984        20615 :   tree len = get_maxval_strlen (arg0, SRK_STRLEN);
    2985        20615 :   if (!len || TREE_CODE (len) != INTEGER_CST)
    2986              :     return false;
    2987              : 
    2988        16211 :   switch (compare_tree_int (len, 1))
    2989              :     {
    2990           91 :     case -1: /* length is 0, delete the call entirely .  */
    2991           91 :       replace_call_with_value (gsi, integer_zero_node);
    2992           91 :       return true;
    2993              : 
    2994         1060 :     case 0: /* length is 1, call fputc.  */
    2995         1060 :       {
    2996         1060 :         const char *p = c_getstr (arg0);
    2997         1060 :         if (p != NULL)
    2998              :           {
    2999         2092 :             if (!fn_fputc || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3000              :               return false;
    3001              : 
    3002         1046 :             gimple *repl
    3003         1046 :               = gimple_build_call (fn_fputc, 2,
    3004         1046 :                                    build_int_cst (integer_type_node, p[0]),
    3005              :                                    arg1);
    3006         1046 :             replace_call_with_call_and_fold (gsi, repl);
    3007         1046 :             return true;
    3008              :           }
    3009              :       }
    3010              :       /* FALLTHROUGH */
    3011        15074 :     case 1: /* length is greater than 1, call fwrite.  */
    3012        15074 :       {
    3013              :         /* If optimizing for size keep fputs.  */
    3014        15074 :         if (optimize_function_for_size_p (cfun))
    3015              :           return false;
    3016              :         /* New argument list transforming fputs(string, stream) to
    3017              :            fwrite(string, 1, len, stream).  */
    3018        27780 :         if (!fn_fwrite || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3019              :           return false;
    3020              : 
    3021         8231 :         gimple *repl
    3022         8231 :           = gimple_build_call (fn_fwrite, 4, arg0, size_one_node,
    3023              :                                fold_convert (size_type_node, len), arg1);
    3024         8231 :         replace_call_with_call_and_fold (gsi, repl);
    3025         8231 :         return true;
    3026              :       }
    3027            0 :     default:
    3028            0 :       gcc_unreachable ();
    3029              :     }
    3030              : }
    3031              : 
    3032              : /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
    3033              :    DEST, SRC, LEN, and SIZE are the arguments to the call.
    3034              :    IGNORE is true, if return value can be ignored.  FCODE is the BUILT_IN_*
    3035              :    code of the builtin.  If MAXLEN is not NULL, it is maximum length
    3036              :    passed as third argument.  */
    3037              : 
    3038              : static bool
    3039        25617 : gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
    3040              :                                 tree dest, tree src, tree len, tree size,
    3041              :                                 enum built_in_function fcode)
    3042              : {
    3043        25617 :   gimple *stmt = gsi_stmt (*gsi);
    3044        25617 :   location_t loc = gimple_location (stmt);
    3045        25617 :   bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
    3046        25617 :   tree fn;
    3047              : 
    3048              :   /* If SRC and DEST are the same (and not volatile), return DEST
    3049              :      (resp. DEST+LEN for __mempcpy_chk).  */
    3050        25617 :   if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
    3051              :     {
    3052           13 :       if (fcode != BUILT_IN_MEMPCPY_CHK)
    3053              :         {
    3054            7 :           replace_call_with_value (gsi, dest);
    3055            7 :           return true;
    3056              :         }
    3057              :       else
    3058              :         {
    3059            6 :           gimple_seq stmts = NULL;
    3060            6 :           len = gimple_convert_to_ptrofftype (&stmts, loc, len);
    3061            6 :           tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
    3062            6 :                                     TREE_TYPE (dest), dest, len);
    3063            6 :           gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3064            6 :           replace_call_with_value (gsi, temp);
    3065            6 :           return true;
    3066              :         }
    3067              :     }
    3068              : 
    3069        68251 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3070              :     return false;
    3071              : 
    3072        25604 :   tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
    3073        25604 :   if (! integer_all_onesp (size)
    3074        24535 :       && !known_lower (stmt, len, size)
    3075        42767 :       && !known_lower (stmt, maxlen, size))
    3076              :     {
    3077              :       /* MAXLEN and LEN both cannot be proved to be less than SIZE, at
    3078              :          least try to optimize (void) __mempcpy_chk () into
    3079              :          (void) __memcpy_chk () */
    3080        17086 :       if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
    3081              :         {
    3082           43 :           fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
    3083           43 :           if (!fn)
    3084              :             return false;
    3085              : 
    3086           43 :           gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
    3087           43 :           replace_call_with_call_and_fold (gsi, repl);
    3088           43 :           return true;
    3089              :         }
    3090              :       return false;
    3091              :     }
    3092              : 
    3093         8518 :   fn = NULL_TREE;
    3094              :   /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
    3095              :      mem{cpy,pcpy,move,set} is available.  */
    3096         8518 :   switch (fcode)
    3097              :     {
    3098         1768 :     case BUILT_IN_MEMCPY_CHK:
    3099         1768 :       fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
    3100         1768 :       break;
    3101         1068 :     case BUILT_IN_MEMPCPY_CHK:
    3102         1068 :       fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
    3103         1068 :       break;
    3104         1657 :     case BUILT_IN_MEMMOVE_CHK:
    3105         1657 :       fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
    3106         1657 :       break;
    3107         4025 :     case BUILT_IN_MEMSET_CHK:
    3108         4025 :       fn = builtin_decl_explicit (BUILT_IN_MEMSET);
    3109         4025 :       break;
    3110              :     default:
    3111              :       break;
    3112              :     }
    3113              : 
    3114         8518 :   if (!fn)
    3115              :     return false;
    3116              : 
    3117         8518 :   gimple *repl = gimple_build_call (fn, 3, dest, src, len);
    3118         8518 :   replace_call_with_call_and_fold (gsi, repl);
    3119         8518 :   return true;
    3120              : }
    3121              : 
    3122              : /* Fold a call to the __st[rp]cpy_chk builtin.
    3123              :    DEST, SRC, and SIZE are the arguments to the call.
    3124              :    IGNORE is true if return value can be ignored.  FCODE is the BUILT_IN_*
    3125              :    code of the builtin.  If MAXLEN is not NULL, it is maximum length of
    3126              :    strings passed as second argument.  */
    3127              : 
    3128              : static bool
    3129         2592 : gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
    3130              :                                 tree dest,
    3131              :                                 tree src, tree size,
    3132              :                                 enum built_in_function fcode)
    3133              : {
    3134         2592 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3135         2592 :   location_t loc = gimple_location (stmt);
    3136         2592 :   bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
    3137         2592 :   tree len, fn;
    3138              : 
    3139              :   /* If SRC and DEST are the same (and not volatile), return DEST.  */
    3140         2592 :   if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
    3141              :     {
    3142              :       /* Issue -Wrestrict unless the pointers are null (those do
    3143              :          not point to objects and so do not indicate an overlap;
    3144              :          such calls could be the result of sanitization and jump
    3145              :          threading).  */
    3146            0 :       if (!integer_zerop (dest)
    3147            0 :           && !warning_suppressed_p (stmt, OPT_Wrestrict))
    3148              :         {
    3149            0 :           tree func = gimple_call_fndecl (stmt);
    3150              : 
    3151            0 :           warning_at (loc, OPT_Wrestrict,
    3152              :                       "%qD source argument is the same as destination",
    3153              :                       func);
    3154              :         }
    3155              : 
    3156            0 :       replace_call_with_value (gsi, dest);
    3157            0 :       return true;
    3158              :     }
    3159              : 
    3160         5184 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3161              :     return false;
    3162              : 
    3163         2592 :   tree maxlen = get_maxval_strlen (src, SRK_STRLENMAX);
    3164         2592 :   if (! integer_all_onesp (size))
    3165              :     {
    3166         2523 :       len = c_strlen (src, 1);
    3167         2523 :       if (!known_lower (stmt, len, size, true)
    3168         2523 :           && !known_lower (stmt, maxlen, size, true))
    3169              :         {
    3170         2187 :           if (fcode == BUILT_IN_STPCPY_CHK)
    3171              :             {
    3172         1077 :               if (! ignore)
    3173              :                 return false;
    3174              : 
    3175              :               /* If return value of __stpcpy_chk is ignored,
    3176              :                  optimize into __strcpy_chk.  */
    3177           35 :               fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
    3178           35 :               if (!fn)
    3179              :                 return false;
    3180              : 
    3181           35 :               gimple *repl = gimple_build_call (fn, 3, dest, src, size);
    3182           35 :               replace_call_with_call_and_fold (gsi, repl);
    3183           35 :               return true;
    3184              :             }
    3185              : 
    3186         1110 :           if (! len || TREE_SIDE_EFFECTS (len))
    3187              :             return false;
    3188              : 
    3189              :           /* If c_strlen returned something, but not provably less than size,
    3190              :              transform __strcpy_chk into __memcpy_chk.  */
    3191          106 :           fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
    3192          106 :           if (!fn)
    3193              :             return false;
    3194              : 
    3195          106 :           gimple_seq stmts = NULL;
    3196          106 :           len = force_gimple_operand (len, &stmts, true, NULL_TREE);
    3197          106 :           len = gimple_convert (&stmts, loc, size_type_node, len);
    3198          106 :           len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len,
    3199              :                               build_int_cst (size_type_node, 1));
    3200          106 :           gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3201          106 :           gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
    3202          106 :           replace_call_with_call_and_fold (gsi, repl);
    3203          106 :           return true;
    3204              :         }
    3205              :     }
    3206              : 
    3207              :   /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available.  */
    3208          666 :   fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK && !ignore
    3209              :                               ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
    3210          405 :   if (!fn)
    3211              :     return false;
    3212              : 
    3213          405 :   gcall *repl = gimple_build_call (fn, 2, dest, src);
    3214          405 :   replace_call_with_call_and_fold (gsi, repl);
    3215          405 :   return true;
    3216              : }
    3217              : 
    3218              : /* Fold a call to the __st{r,p}ncpy_chk builtin.  DEST, SRC, LEN, and SIZE
    3219              :    are the arguments to the call.  If MAXLEN is not NULL, it is maximum
    3220              :    length passed as third argument. IGNORE is true if return value can be
    3221              :    ignored. FCODE is the BUILT_IN_* code of the builtin. */
    3222              : 
    3223              : static bool
    3224         2721 : gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
    3225              :                                  tree dest, tree src,
    3226              :                                  tree len, tree size,
    3227              :                                  enum built_in_function fcode)
    3228              : {
    3229         2721 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3230         2721 :   bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
    3231         2721 :   tree fn;
    3232              : 
    3233         2721 :   tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
    3234         2721 :   if (! integer_all_onesp (size)
    3235         2721 :       && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
    3236              :     {
    3237         2264 :       if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
    3238              :         {
    3239              :           /* If return value of __stpncpy_chk is ignored,
    3240              :              optimize into __strncpy_chk.  */
    3241           39 :           fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
    3242           39 :           if (fn)
    3243              :             {
    3244           39 :               gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
    3245           39 :               replace_call_with_call_and_fold (gsi, repl);
    3246           39 :               return true;
    3247              :             }
    3248              :         }
    3249              :       return false;
    3250              :     }
    3251              : 
    3252              :   /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available.  */
    3253          717 :   fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK && !ignore
    3254              :                               ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
    3255         3139 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3256              :     return false;
    3257              : 
    3258          457 :   gcall *repl = gimple_build_call (fn, 3, dest, src, len);
    3259          457 :   replace_call_with_call_and_fold (gsi, repl);
    3260          457 :   return true;
    3261              : }
    3262              : 
    3263              : /* Fold function call to builtin stpcpy with arguments DEST and SRC.
    3264              :    Return NULL_TREE if no simplification can be made.  */
    3265              : 
    3266              : static bool
    3267         3674 : gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
    3268              : {
    3269         3674 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3270         3674 :   location_t loc = gimple_location (stmt);
    3271         3674 :   tree dest = gimple_call_arg (stmt, 0);
    3272         3674 :   tree src = gimple_call_arg (stmt, 1);
    3273         3674 :   tree fn, lenp1;
    3274              : 
    3275         7348 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3276              :     return false;
    3277              : 
    3278              :   /* If the result is unused, replace stpcpy with strcpy.  */
    3279         3674 :   if (gimple_call_lhs (stmt) == NULL_TREE)
    3280              :     {
    3281           29 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3282           29 :       if (!fn)
    3283              :         return false;
    3284           29 :       gimple_call_set_fndecl (stmt, fn);
    3285           29 :       fold_stmt (gsi);
    3286           29 :       return true;
    3287              :     }
    3288              : 
    3289              :   /* Set to non-null if ARG refers to an unterminated array.  */
    3290         3645 :   c_strlen_data data = { };
    3291              :   /* The size of the unterminated array if SRC referes to one.  */
    3292         3645 :   tree size;
    3293              :   /* True if the size is exact/constant, false if it's the lower bound
    3294              :      of a range.  */
    3295         3645 :   bool exact;
    3296         3645 :   tree len = c_strlen (src, 1, &data, 1);
    3297         3645 :   if (!len
    3298          703 :       || TREE_CODE (len) != INTEGER_CST)
    3299              :     {
    3300         3174 :       data.decl = unterminated_array (src, &size, &exact);
    3301         3174 :       if (!data.decl)
    3302              :         return false;
    3303              :     }
    3304              : 
    3305         1076 :   if (data.decl)
    3306              :     {
    3307              :       /* Avoid folding calls with unterminated arrays.  */
    3308          605 :       if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
    3309           75 :         warn_string_no_nul (loc, stmt, "stpcpy", src, data.decl, size,
    3310              :                             exact);
    3311          605 :       suppress_warning (stmt, OPT_Wstringop_overread);
    3312          605 :       return false;
    3313              :     }
    3314              : 
    3315          471 :   if (optimize_function_for_size_p (cfun)
    3316              :       /* If length is zero it's small enough.  */
    3317          471 :       && !integer_zerop (len))
    3318              :     return false;
    3319              : 
    3320              :   /* If the source has a known length replace stpcpy with memcpy.  */
    3321         3645 :   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
    3322          287 :   if (!fn)
    3323              :     return false;
    3324              : 
    3325          287 :   gimple_seq stmts = NULL;
    3326          287 :   tree tem = gimple_convert (&stmts, loc, size_type_node, len);
    3327          287 :   lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
    3328              :                         tem, build_int_cst (size_type_node, 1));
    3329          287 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3330          287 :   gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
    3331          287 :   gimple_move_vops (repl, stmt);
    3332          287 :   gsi_insert_before (gsi, repl, GSI_SAME_STMT);
    3333              :   /* Replace the result with dest + len.  */
    3334          287 :   stmts = NULL;
    3335          287 :   tem = gimple_convert (&stmts, loc, sizetype, len);
    3336          287 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    3337          287 :   gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
    3338              :                                       POINTER_PLUS_EXPR, dest, tem);
    3339          287 :   gsi_replace (gsi, ret, false);
    3340              :   /* Finally fold the memcpy call.  */
    3341          287 :   gimple_stmt_iterator gsi2 = *gsi;
    3342          287 :   gsi_prev (&gsi2);
    3343          287 :   fold_stmt (&gsi2);
    3344          287 :   return true;
    3345              : }
    3346              : 
    3347              : /* Simplify mempcpy call stmt at GSI, returning true if simplified.
    3348              :    Currently only handling mempcpy -> memcpy when the return value
    3349              :    is ignored.  */
    3350              : 
    3351              : static bool
    3352         9536 : gimple_fold_builtin_mempcpy (gimple_stmt_iterator *gsi)
    3353              : {
    3354         9536 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3355              : 
    3356         9536 :   if (gimple_call_lhs (stmt) != NULL_TREE)
    3357              :     return false;
    3358              : 
    3359          385 :   tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
    3360          385 :   if (!fn)
    3361              :     return false;
    3362              : 
    3363          385 :   tree dest = gimple_call_arg (stmt, 0);
    3364          385 :   tree src = gimple_call_arg (stmt, 1);
    3365          385 :   tree n = gimple_call_arg (stmt, 2);
    3366              : 
    3367          385 :   gcall *repl = gimple_build_call (fn, 3, dest, src, n);
    3368          385 :   replace_call_with_call_and_fold (gsi, repl);
    3369              : 
    3370          385 :   return true;
    3371              : }
    3372              : 
    3373              : /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS.  Return
    3374              :    NULL_TREE if a normal call should be emitted rather than expanding
    3375              :    the function inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
    3376              :    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
    3377              :    passed as second argument.  */
    3378              : 
    3379              : static bool
    3380         2359 : gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
    3381              :                                   enum built_in_function fcode)
    3382              : {
    3383         2359 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3384         2359 :   tree dest, size, len, fn, fmt, flag;
    3385         2359 :   const char *fmt_str;
    3386              : 
    3387              :   /* Verify the required arguments in the original call.  */
    3388         2359 :   if (gimple_call_num_args (stmt) < 5)
    3389              :     return false;
    3390              : 
    3391         2359 :   dest = gimple_call_arg (stmt, 0);
    3392         2359 :   len = gimple_call_arg (stmt, 1);
    3393         2359 :   flag = gimple_call_arg (stmt, 2);
    3394         2359 :   size = gimple_call_arg (stmt, 3);
    3395         2359 :   fmt = gimple_call_arg (stmt, 4);
    3396              : 
    3397         2359 :   tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
    3398         2359 :   if (! integer_all_onesp (size)
    3399         2359 :       && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
    3400              :     return false;
    3401              : 
    3402          308 :   if (!init_target_chars ())
    3403              :     return false;
    3404              : 
    3405              :   /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
    3406              :      or if format doesn't contain % chars or is "%s".  */
    3407          308 :   if (! integer_zerop (flag))
    3408              :     {
    3409           52 :       fmt_str = c_getstr (fmt);
    3410           52 :       if (fmt_str == NULL)
    3411              :         return false;
    3412           52 :       if (strchr (fmt_str, target_percent) != NULL
    3413           51 :           && strcmp (fmt_str, target_percent_s))
    3414              :         return false;
    3415              :     }
    3416              : 
    3417              :   /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
    3418              :      available.  */
    3419          415 :   fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
    3420              :                               ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
    3421         2618 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3422              :     return false;
    3423              : 
    3424              :   /* Replace the called function and the first 5 argument by 3 retaining
    3425              :      trailing varargs.  */
    3426          259 :   gimple_call_set_fndecl (stmt, fn);
    3427          259 :   gimple_call_set_fntype (stmt, TREE_TYPE (fn));
    3428          259 :   gimple_call_set_arg (stmt, 0, dest);
    3429          259 :   gimple_call_set_arg (stmt, 1, len);
    3430          259 :   gimple_call_set_arg (stmt, 2, fmt);
    3431          546 :   for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
    3432          287 :     gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
    3433          259 :   gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
    3434          259 :   fold_stmt (gsi);
    3435          259 :   return true;
    3436              : }
    3437              : 
    3438              : /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
    3439              :    Return NULL_TREE if a normal call should be emitted rather than
    3440              :    expanding the function inline.  FCODE is either BUILT_IN_SPRINTF_CHK
    3441              :    or BUILT_IN_VSPRINTF_CHK.  */
    3442              : 
    3443              : static bool
    3444         4459 : gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
    3445              :                                  enum built_in_function fcode)
    3446              : {
    3447         4459 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3448         4459 :   tree dest, size, len, fn, fmt, flag;
    3449         4459 :   const char *fmt_str;
    3450         4459 :   unsigned nargs = gimple_call_num_args (stmt);
    3451              : 
    3452              :   /* Verify the required arguments in the original call.  */
    3453         4459 :   if (nargs < 4)
    3454              :     return false;
    3455         4459 :   dest = gimple_call_arg (stmt, 0);
    3456         4459 :   flag = gimple_call_arg (stmt, 1);
    3457         4459 :   size = gimple_call_arg (stmt, 2);
    3458         4459 :   fmt = gimple_call_arg (stmt, 3);
    3459              : 
    3460         4459 :   len = NULL_TREE;
    3461              : 
    3462         4459 :   if (!init_target_chars ())
    3463              :     return false;
    3464              : 
    3465              :   /* Check whether the format is a literal string constant.  */
    3466         4459 :   fmt_str = c_getstr (fmt);
    3467         4459 :   if (fmt_str != NULL)
    3468              :     {
    3469              :       /* If the format doesn't contain % args or %%, we know the size.  */
    3470         4069 :       if (strchr (fmt_str, target_percent) == 0)
    3471              :         {
    3472          251 :           if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
    3473          251 :             len = build_int_cstu (size_type_node, strlen (fmt_str));
    3474              :         }
    3475              :       /* If the format is "%s" and first ... argument is a string literal,
    3476              :          we know the size too.  */
    3477         3818 :       else if (fcode == BUILT_IN_SPRINTF_CHK
    3478         2962 :                && strcmp (fmt_str, target_percent_s) == 0)
    3479              :         {
    3480          395 :           tree arg;
    3481              : 
    3482          395 :           if (nargs == 5)
    3483              :             {
    3484          395 :               arg = gimple_call_arg (stmt, 4);
    3485          395 :               if (POINTER_TYPE_P (TREE_TYPE (arg)))
    3486          363 :                 len = c_strlen (arg, 1);
    3487              :             }
    3488              :         }
    3489              :     }
    3490              : 
    3491         4459 :   if (! integer_all_onesp (size) && !known_lower (stmt, len, size, true))
    3492              :     return false;
    3493              : 
    3494              :   /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
    3495              :      or if format doesn't contain % chars or is "%s".  */
    3496          202 :   if (! integer_zerop (flag))
    3497              :     {
    3498            1 :       if (fmt_str == NULL)
    3499              :         return false;
    3500            1 :       if (strchr (fmt_str, target_percent) != NULL
    3501            0 :           && strcmp (fmt_str, target_percent_s))
    3502              :         return false;
    3503              :     }
    3504              : 
    3505              :   /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available.  */
    3506          347 :   fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
    3507              :                               ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
    3508         4661 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3509              :     return false;
    3510              : 
    3511              :   /* Replace the called function and the first 4 argument by 2 retaining
    3512              :      trailing varargs.  */
    3513          202 :   gimple_call_set_fndecl (stmt, fn);
    3514          202 :   gimple_call_set_fntype (stmt, TREE_TYPE (fn));
    3515          202 :   gimple_call_set_arg (stmt, 0, dest);
    3516          202 :   gimple_call_set_arg (stmt, 1, fmt);
    3517          400 :   for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
    3518          198 :     gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
    3519          202 :   gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
    3520          202 :   fold_stmt (gsi);
    3521          202 :   return true;
    3522              : }
    3523              : 
    3524              : /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
    3525              :    ORIG may be null if this is a 2-argument call.  We don't attempt to
    3526              :    simplify calls with more than 3 arguments.
    3527              : 
    3528              :    Return true if simplification was possible, otherwise false.  */
    3529              : 
    3530              : bool
    3531         2280 : gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
    3532              : {
    3533         2280 :   gimple *stmt = gsi_stmt (*gsi);
    3534              : 
    3535              :   /* Verify the required arguments in the original call.  We deal with two
    3536              :      types of sprintf() calls: 'sprintf (str, fmt)' and
    3537              :      'sprintf (dest, "%s", orig)'.  */
    3538         2280 :   if (gimple_call_num_args (stmt) > 3)
    3539              :     return false;
    3540              : 
    3541         1879 :   tree orig = NULL_TREE;
    3542         1879 :   if (gimple_call_num_args (stmt) == 3)
    3543         1782 :     orig = gimple_call_arg (stmt, 2);
    3544              : 
    3545              :   /* Check whether the format is a literal string constant.  */
    3546         1879 :   tree fmt = gimple_call_arg (stmt, 1);
    3547         1879 :   const char *fmt_str = c_getstr (fmt);
    3548         1879 :   if (fmt_str == NULL)
    3549              :     return false;
    3550              : 
    3551         1879 :   tree dest = gimple_call_arg (stmt, 0);
    3552              : 
    3553         1879 :   if (!init_target_chars ())
    3554              :     return false;
    3555              : 
    3556         1879 :   tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3557         5183 :   if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3558              :     return false;
    3559              : 
    3560              :   /* If the format doesn't contain % args or %%, use strcpy.  */
    3561         1879 :   if (strchr (fmt_str, target_percent) == NULL)
    3562              :     {
    3563              :       /* Don't optimize sprintf (buf, "abc", ptr++).  */
    3564          109 :       if (orig)
    3565              :         return false;
    3566              : 
    3567              :       /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
    3568              :          'format' is known to contain no % formats.  */
    3569           96 :       gimple_seq stmts = NULL;
    3570           96 :       gimple *repl = gimple_build_call (fn, 2, dest, fmt);
    3571              : 
    3572              :       /* Propagate the NO_WARNING bit to avoid issuing the same
    3573              :          warning more than once.  */
    3574           96 :       copy_warning (repl, stmt);
    3575              : 
    3576           96 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3577           96 :       if (tree lhs = gimple_call_lhs (stmt))
    3578              :         {
    3579            0 :           repl = gimple_build_assign (lhs, build_int_cst (TREE_TYPE (lhs),
    3580            0 :                                                           strlen (fmt_str)));
    3581            0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3582            0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3583              :           /* gsi now points at the assignment to the lhs, get a
    3584              :              stmt iterator to the memcpy call.
    3585              :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3586              :              CFG isn't built yet.  */
    3587            0 :           gimple_stmt_iterator gsi2 = *gsi;
    3588            0 :           gsi_prev (&gsi2);
    3589            0 :           fold_stmt (&gsi2);
    3590              :         }
    3591              :       else
    3592              :         {
    3593           96 :           gsi_replace_with_seq_vops (gsi, stmts);
    3594           96 :           fold_stmt (gsi);
    3595              :         }
    3596           96 :       return true;
    3597              :     }
    3598              : 
    3599              :   /* If the format is "%s", use strcpy if the result isn't used.  */
    3600         1770 :   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
    3601              :     {
    3602              :       /* Don't crash on sprintf (str1, "%s").  */
    3603          746 :       if (!orig)
    3604              :         return false;
    3605              : 
    3606              :       /* Don't fold calls with source arguments of invalid (nonpointer)
    3607              :          types.  */
    3608          745 :       if (!POINTER_TYPE_P (TREE_TYPE (orig)))
    3609              :         return false;
    3610              : 
    3611          739 :       tree orig_len = NULL_TREE;
    3612          739 :       if (gimple_call_lhs (stmt))
    3613              :         {
    3614           17 :           orig_len = get_maxval_strlen (orig, SRK_STRLEN);
    3615           17 :           if (!orig_len)
    3616              :             return false;
    3617              :         }
    3618              : 
    3619              :       /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2).  */
    3620          722 :       gimple_seq stmts = NULL;
    3621          722 :       gimple *repl = gimple_build_call (fn, 2, dest, orig);
    3622              : 
    3623              :       /* Propagate the NO_WARNING bit to avoid issuing the same
    3624              :          warning more than once.  */
    3625          722 :       copy_warning (repl, stmt);
    3626              : 
    3627          722 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3628          722 :       if (tree lhs = gimple_call_lhs (stmt))
    3629              :         {
    3630            0 :           if (!useless_type_conversion_p (TREE_TYPE (lhs),
    3631            0 :                                           TREE_TYPE (orig_len)))
    3632            0 :             orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
    3633            0 :           repl = gimple_build_assign (lhs, orig_len);
    3634            0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3635            0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3636              :           /* gsi now points at the assignment to the lhs, get a
    3637              :              stmt iterator to the memcpy call.
    3638              :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3639              :              CFG isn't built yet.  */
    3640            0 :           gimple_stmt_iterator gsi2 = *gsi;
    3641            0 :           gsi_prev (&gsi2);
    3642            0 :           fold_stmt (&gsi2);
    3643              :         }
    3644              :       else
    3645              :         {
    3646          722 :           gsi_replace_with_seq_vops (gsi, stmts);
    3647          722 :           fold_stmt (gsi);
    3648              :         }
    3649          722 :       return true;
    3650              :     }
    3651              :   return false;
    3652              : }
    3653              : 
    3654              : /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
    3655              :    FMT, and ORIG.  ORIG may be null if this is a 3-argument call.  We don't
    3656              :    attempt to simplify calls with more than 4 arguments.
    3657              : 
    3658              :    Return true if simplification was possible, otherwise false.  */
    3659              : 
    3660              : bool
    3661         1725 : gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
    3662              : {
    3663         1725 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3664         1725 :   tree dest = gimple_call_arg (stmt, 0);
    3665         1725 :   tree destsize = gimple_call_arg (stmt, 1);
    3666         1725 :   tree fmt = gimple_call_arg (stmt, 2);
    3667         1725 :   tree orig = NULL_TREE;
    3668         1725 :   const char *fmt_str = NULL;
    3669              : 
    3670         1725 :   if (gimple_call_num_args (stmt) > 4
    3671         2975 :       || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
    3672              :     return false;
    3673              : 
    3674          808 :   if (gimple_call_num_args (stmt) == 4)
    3675          609 :     orig = gimple_call_arg (stmt, 3);
    3676              : 
    3677              :   /* Check whether the format is a literal string constant.  */
    3678          808 :   fmt_str = c_getstr (fmt);
    3679          808 :   if (fmt_str == NULL)
    3680              :     return false;
    3681              : 
    3682          808 :   if (!init_target_chars ())
    3683              :     return false;
    3684              : 
    3685              :   /* If the format doesn't contain % args or %%, use strcpy.  */
    3686          808 :   if (strchr (fmt_str, target_percent) == NULL)
    3687              :     {
    3688          228 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3689          198 :       if (!fn)
    3690              :         return false;
    3691              : 
    3692              :       /* Don't optimize snprintf (buf, 4, "abc", ptr++).  */
    3693          198 :       if (orig)
    3694              :         return false;
    3695              : 
    3696          198 :       tree len = build_int_cstu (TREE_TYPE (destsize), strlen (fmt_str));
    3697              : 
    3698              :       /* We could expand this as
    3699              :          memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
    3700              :          or to
    3701              :          memcpy (str, fmt_with_nul_at_cstm1, cst);
    3702              :          but in the former case that might increase code size
    3703              :          and in the latter case grow .rodata section too much.
    3704              :          So punt for now.  */
    3705          198 :       if (!known_lower (stmt, len, destsize, true))
    3706              :         return false;
    3707              : 
    3708          168 :       gimple_seq stmts = NULL;
    3709          168 :       gimple *repl = gimple_build_call (fn, 2, dest, fmt);
    3710          168 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3711          168 :       if (tree lhs = gimple_call_lhs (stmt))
    3712              :         {
    3713            0 :           repl = gimple_build_assign (lhs,
    3714            0 :                                       fold_convert (TREE_TYPE (lhs), len));
    3715            0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3716            0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3717              :           /* gsi now points at the assignment to the lhs, get a
    3718              :              stmt iterator to the memcpy call.
    3719              :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3720              :              CFG isn't built yet.  */
    3721            0 :           gimple_stmt_iterator gsi2 = *gsi;
    3722            0 :           gsi_prev (&gsi2);
    3723            0 :           fold_stmt (&gsi2);
    3724              :         }
    3725              :       else
    3726              :         {
    3727          168 :           gsi_replace_with_seq_vops (gsi, stmts);
    3728          168 :           fold_stmt (gsi);
    3729              :         }
    3730          168 :       return true;
    3731              :     }
    3732              : 
    3733              :   /* If the format is "%s", use strcpy if the result isn't used.  */
    3734          610 :   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
    3735              :     {
    3736          292 :       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
    3737          174 :       if (!fn)
    3738              :         return false;
    3739              : 
    3740              :       /* Don't crash on snprintf (str1, cst, "%s").  */
    3741          174 :       if (!orig)
    3742              :         return false;
    3743              : 
    3744          174 :       tree orig_len = get_maxval_strlen (orig, SRK_STRLEN);
    3745              : 
    3746              :       /* We could expand this as
    3747              :          memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
    3748              :          or to
    3749              :          memcpy (str1, str2_with_nul_at_cstm1, cst);
    3750              :          but in the former case that might increase code size
    3751              :          and in the latter case grow .rodata section too much.
    3752              :          So punt for now.  */
    3753          174 :       if (!known_lower (stmt, orig_len, destsize, true))
    3754              :         return false;
    3755              : 
    3756              :       /* Convert snprintf (str1, cst, "%s", str2) into
    3757              :          strcpy (str1, str2) if strlen (str2) < cst.  */
    3758           56 :       gimple_seq stmts = NULL;
    3759           56 :       gimple *repl = gimple_build_call (fn, 2, dest, orig);
    3760           56 :       gimple_seq_add_stmt_without_update (&stmts, repl);
    3761           56 :       if (tree lhs = gimple_call_lhs (stmt))
    3762              :         {
    3763            0 :           if (!useless_type_conversion_p (TREE_TYPE (lhs),
    3764            0 :                                           TREE_TYPE (orig_len)))
    3765            0 :             orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
    3766            0 :           repl = gimple_build_assign (lhs, orig_len);
    3767            0 :           gimple_seq_add_stmt_without_update (&stmts, repl);
    3768            0 :           gsi_replace_with_seq_vops (gsi, stmts);
    3769              :           /* gsi now points at the assignment to the lhs, get a
    3770              :              stmt iterator to the memcpy call.
    3771              :              ???  We can't use gsi_for_stmt as that doesn't work when the
    3772              :              CFG isn't built yet.  */
    3773            0 :           gimple_stmt_iterator gsi2 = *gsi;
    3774            0 :           gsi_prev (&gsi2);
    3775            0 :           fold_stmt (&gsi2);
    3776              :         }
    3777              :       else
    3778              :         {
    3779           56 :           gsi_replace_with_seq_vops (gsi, stmts);
    3780           56 :           fold_stmt (gsi);
    3781              :         }
    3782           56 :       return true;
    3783              :     }
    3784              :   return false;
    3785              : }
    3786              : 
    3787              : /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
    3788              :    FP, FMT, and ARG are the arguments to the call.  We don't fold calls with
    3789              :    more than 3 arguments, and ARG may be null in the 2-argument case.
    3790              : 
    3791              :    Return NULL_TREE if no simplification was possible, otherwise return the
    3792              :    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
    3793              :    code of the function to be simplified.  */
    3794              : 
    3795              : static bool
    3796        54431 : gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
    3797              :                              tree fp, tree fmt, tree arg,
    3798              :                              enum built_in_function fcode)
    3799              : {
    3800        54431 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3801        54431 :   tree fn_fputc, fn_fputs;
    3802        54431 :   const char *fmt_str = NULL;
    3803              : 
    3804              :   /* If the return value is used, don't do the transformation.  */
    3805        54431 :   if (gimple_call_lhs (stmt) != NULL_TREE)
    3806              :     return false;
    3807              : 
    3808              :   /* Check whether the format is a literal string constant.  */
    3809        50259 :   fmt_str = c_getstr (fmt);
    3810        50259 :   if (fmt_str == NULL)
    3811              :     return false;
    3812              : 
    3813        49949 :   if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
    3814              :     {
    3815              :       /* If we're using an unlocked function, assume the other
    3816              :          unlocked functions exist explicitly.  */
    3817           80 :       fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
    3818           80 :       fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
    3819              :     }
    3820              :   else
    3821              :     {
    3822        49869 :       fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
    3823        49869 :       fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
    3824              :     }
    3825              : 
    3826        49949 :   if (!init_target_chars ())
    3827              :     return false;
    3828              : 
    3829       144029 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3830              :     return false;
    3831              : 
    3832              :   /* If the format doesn't contain % args or %%, use strcpy.  */
    3833        49949 :   if (strchr (fmt_str, target_percent) == NULL)
    3834              :     {
    3835         9612 :       if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
    3836         9542 :           && arg)
    3837              :         return false;
    3838              : 
    3839              :       /* If the format specifier was "", fprintf does nothing.  */
    3840         9612 :       if (fmt_str[0] == '\0')
    3841              :         {
    3842           58 :           replace_call_with_value (gsi, NULL_TREE);
    3843           58 :           return true;
    3844              :         }
    3845              : 
    3846              :       /* When "string" doesn't contain %, replace all cases of
    3847              :          fprintf (fp, string) with fputs (string, fp).  The fputs
    3848              :          builtin will take care of special cases like length == 1.  */
    3849         9554 :       if (fn_fputs)
    3850              :         {
    3851         9554 :           gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
    3852         9554 :           replace_call_with_call_and_fold (gsi, repl);
    3853         9554 :           return true;
    3854              :         }
    3855              :     }
    3856              : 
    3857              :   /* The other optimizations can be done only on the non-va_list variants.  */
    3858        40337 :   else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
    3859              :     return false;
    3860              : 
    3861              :   /* If the format specifier was "%s", call __builtin_fputs (arg, fp).  */
    3862        39288 :   else if (strcmp (fmt_str, target_percent_s) == 0)
    3863              :     {
    3864          639 :       if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
    3865              :         return false;
    3866          639 :       if (fn_fputs)
    3867              :         {
    3868          639 :           gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
    3869          639 :           replace_call_with_call_and_fold (gsi, repl);
    3870          639 :           return true;
    3871              :         }
    3872              :     }
    3873              : 
    3874              :   /* If the format specifier was "%c", call __builtin_fputc (arg, fp).  */
    3875        38649 :   else if (strcmp (fmt_str, target_percent_c) == 0)
    3876              :     {
    3877           49 :       if (!arg
    3878           49 :           || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
    3879            0 :         return false;
    3880           49 :       if (fn_fputc)
    3881              :         {
    3882           49 :           gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
    3883           49 :           replace_call_with_call_and_fold (gsi, repl);
    3884           49 :           return true;
    3885              :         }
    3886              :     }
    3887              : 
    3888              :   return false;
    3889              : }
    3890              : 
    3891              : /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
    3892              :    FMT and ARG are the arguments to the call; we don't fold cases with
    3893              :    more than 2 arguments, and ARG may be null if this is a 1-argument case.
    3894              : 
    3895              :    Return NULL_TREE if no simplification was possible, otherwise return the
    3896              :    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
    3897              :    code of the function to be simplified.  */
    3898              : 
    3899              : static bool
    3900       123358 : gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
    3901              :                             tree arg, enum built_in_function fcode)
    3902              : {
    3903       123358 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    3904       123358 :   tree fn_putchar, fn_puts, newarg;
    3905       123358 :   const char *fmt_str = NULL;
    3906              : 
    3907              :   /* If the return value is used, don't do the transformation.  */
    3908       123358 :   if (gimple_call_lhs (stmt) != NULL_TREE)
    3909              :     return false;
    3910              : 
    3911       358690 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    3912              :     return false;
    3913              : 
    3914              :   /* Check whether the format is a literal string constant.  */
    3915       120107 :   fmt_str = c_getstr (fmt);
    3916       120107 :   if (fmt_str == NULL)
    3917              :     return false;
    3918              : 
    3919       117046 :   if (fcode == BUILT_IN_PRINTF_UNLOCKED)
    3920              :     {
    3921              :       /* If we're using an unlocked function, assume the other
    3922              :          unlocked functions exist explicitly.  */
    3923           80 :       fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
    3924           80 :       fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
    3925              :     }
    3926              :   else
    3927              :     {
    3928       116966 :       fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
    3929       116966 :       fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
    3930              :     }
    3931              : 
    3932       117046 :   if (!init_target_chars ())
    3933              :     return false;
    3934              : 
    3935       117046 :   if (strcmp (fmt_str, target_percent_s) == 0
    3936       110660 :       || strchr (fmt_str, target_percent) == NULL)
    3937              :     {
    3938        14098 :       const char *str;
    3939              : 
    3940        14098 :       if (strcmp (fmt_str, target_percent_s) == 0)
    3941              :         {
    3942         6386 :           if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
    3943              :             return false;
    3944              : 
    3945         6098 :           if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
    3946              :             return false;
    3947              : 
    3948         6093 :           str = c_getstr (arg);
    3949         6093 :           if (str == NULL)
    3950              :             return false;
    3951              :         }
    3952              :       else
    3953              :         {
    3954              :           /* The format specifier doesn't contain any '%' characters.  */
    3955         7712 :           if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
    3956         7580 :               && arg)
    3957              :             return false;
    3958              :           str = fmt_str;
    3959              :         }
    3960              : 
    3961              :       /* If the string was "", printf does nothing.  */
    3962         5748 :       if (str[0] == '\0')
    3963              :         {
    3964          109 :           replace_call_with_value (gsi, NULL_TREE);
    3965          109 :           return true;
    3966              :         }
    3967              : 
    3968              :       /* If the string has length of 1, call putchar.  */
    3969         5639 :       if (str[1] == '\0')
    3970              :         {
    3971              :           /* Given printf("c"), (where c is any one character,)
    3972              :              convert "c"[0] to an int and pass that to the replacement
    3973              :              function.  */
    3974          559 :           newarg = build_int_cst (integer_type_node, str[0]);
    3975          559 :           if (fn_putchar)
    3976              :             {
    3977          559 :               gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
    3978          559 :               replace_call_with_call_and_fold (gsi, repl);
    3979          559 :               return true;
    3980              :             }
    3981              :         }
    3982              :       else
    3983              :         {
    3984              :           /* If the string was "string\n", call puts("string").  */
    3985         5080 :           size_t len = strlen (str);
    3986         5080 :           if ((unsigned char)str[len - 1] == target_newline
    3987         3988 :               && (size_t) (int) len == len
    3988         3988 :               && (int) len > 0)
    3989              :             {
    3990         3988 :               char *newstr;
    3991              : 
    3992              :               /* Create a NUL-terminated string that's one char shorter
    3993              :                  than the original, stripping off the trailing '\n'.  */
    3994         3988 :               newstr = xstrdup (str);
    3995         3988 :               newstr[len - 1] = '\0';
    3996         3988 :               newarg = build_string_literal (len, newstr);
    3997         3988 :               free (newstr);
    3998         3988 :               if (fn_puts)
    3999              :                 {
    4000         3988 :                   gcall *repl = gimple_build_call (fn_puts, 1, newarg);
    4001         3988 :                   replace_call_with_call_and_fold (gsi, repl);
    4002         3988 :                   return true;
    4003              :                 }
    4004              :             }
    4005              :           else
    4006              :             /* We'd like to arrange to call fputs(string,stdout) here,
    4007              :                but we need stdout and don't have a way to get it yet.  */
    4008              :             return false;
    4009              :         }
    4010              :     }
    4011              : 
    4012              :   /* The other optimizations can be done only on the non-va_list variants.  */
    4013       102948 :   else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
    4014              :     return false;
    4015              : 
    4016              :   /* If the format specifier was "%s\n", call __builtin_puts(arg).  */
    4017       102698 :   else if (strcmp (fmt_str, target_percent_s_newline) == 0)
    4018              :     {
    4019          179 :       if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
    4020              :         return false;
    4021          179 :       if (fn_puts)
    4022              :         {
    4023          179 :           gcall *repl = gimple_build_call (fn_puts, 1, arg);
    4024          179 :           replace_call_with_call_and_fold (gsi, repl);
    4025          179 :           return true;
    4026              :         }
    4027              :     }
    4028              : 
    4029              :   /* If the format specifier was "%c", call __builtin_putchar(arg).  */
    4030       102519 :   else if (strcmp (fmt_str, target_percent_c) == 0)
    4031              :     {
    4032           94 :       if (!arg || ! useless_type_conversion_p (integer_type_node,
    4033           47 :                                                TREE_TYPE (arg)))
    4034            0 :         return false;
    4035           47 :       if (fn_putchar)
    4036              :         {
    4037           47 :           gcall *repl = gimple_build_call (fn_putchar, 1, arg);
    4038           47 :           replace_call_with_call_and_fold (gsi, repl);
    4039           47 :           return true;
    4040              :         }
    4041              :     }
    4042              : 
    4043              :   return false;
    4044              : }
    4045              : 
    4046              : 
    4047              : 
    4048              : /* Fold a call to __builtin_strlen with known length LEN.  */
    4049              : 
    4050              : static bool
    4051       142361 : gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
    4052              : {
    4053       142361 :   gimple *stmt = gsi_stmt (*gsi);
    4054       142361 :   tree arg = gimple_call_arg (stmt, 0);
    4055              : 
    4056       142361 :   wide_int minlen;
    4057       142361 :   wide_int maxlen;
    4058              : 
    4059       142361 :   c_strlen_data lendata = { };
    4060       142361 :   if (get_range_strlen (arg, &lendata, /* eltsize = */ 1)
    4061        34526 :       && !lendata.decl
    4062        31563 :       && lendata.minlen && TREE_CODE (lendata.minlen) == INTEGER_CST
    4063       173819 :       && lendata.maxlen && TREE_CODE (lendata.maxlen) == INTEGER_CST)
    4064              :     {
    4065              :       /* The range of lengths refers to either a single constant
    4066              :          string or to the longest and shortest constant string
    4067              :          referenced by the argument of the strlen() call, or to
    4068              :          the strings that can possibly be stored in the arrays
    4069              :          the argument refers to.  */
    4070        31458 :       minlen = wi::to_wide (lendata.minlen);
    4071        31458 :       maxlen = wi::to_wide (lendata.maxlen);
    4072              :     }
    4073              :   else
    4074              :     {
    4075       110903 :       unsigned prec = TYPE_PRECISION (sizetype);
    4076              : 
    4077       110903 :       minlen = wi::shwi (0, prec);
    4078       110903 :       maxlen = wi::to_wide (max_object_size (), prec) - 2;
    4079              :     }
    4080              : 
    4081              :   /* For -fsanitize=address, don't optimize the upper bound of the
    4082              :      length to be able to diagnose UB on non-zero terminated arrays.  */
    4083       142361 :   if (sanitize_flags_p (SANITIZE_ADDRESS))
    4084          278 :     maxlen = wi::max_value (TYPE_PRECISION (sizetype), UNSIGNED);
    4085              : 
    4086       142361 :   if (minlen == maxlen)
    4087              :     {
    4088              :       /* Fold the strlen call to a constant.  */
    4089         1535 :       tree type = TREE_TYPE (lendata.minlen);
    4090         3070 :       tree len = force_gimple_operand_gsi (gsi,
    4091         1535 :                                            wide_int_to_tree (type, minlen),
    4092              :                                            true, NULL, true, GSI_SAME_STMT);
    4093         1535 :       replace_call_with_value (gsi, len);
    4094         1535 :       return true;
    4095              :     }
    4096              : 
    4097              :   /* Set the strlen() range to [0, MAXLEN].  */
    4098       140826 :   if (tree lhs = gimple_call_lhs (stmt))
    4099       140821 :     set_strlen_range (lhs, minlen, maxlen);
    4100              : 
    4101              :   return false;
    4102       142361 : }
    4103              : 
    4104              : static bool
    4105          234 : gimple_fold_builtin_omp_is_initial_device (gimple_stmt_iterator *gsi)
    4106              : {
    4107              : #if ACCEL_COMPILER
    4108              :   replace_call_with_value (gsi, integer_zero_node);
    4109              :   return true;
    4110              : #else
    4111          234 :   if (!ENABLE_OFFLOADING || symtab->state == EXPANSION)
    4112              :     {
    4113            0 :       replace_call_with_value (gsi, integer_one_node);
    4114          234 :       return true;
    4115              :     }
    4116              : #endif
    4117              :   return false;
    4118              : }
    4119              : 
    4120              : /* omp_get_initial_device was in OpenMP 5.0/5.1 explicitly and in
    4121              :    5.0 implicitly the same as omp_get_num_devices; since 6.0 it is
    4122              :    unspecified whether -1 or omp_get_num_devices() is returned.  For
    4123              :    better backward compatibility, use omp_get_num_devices() on the
    4124              :    host - and -1 on the device (where the result is unspecified).  */
    4125              : 
    4126              : static bool
    4127          103 : gimple_fold_builtin_omp_get_initial_device (gimple_stmt_iterator *gsi)
    4128              : {
    4129              : #if ACCEL_COMPILER
    4130              :   replace_call_with_value (gsi, build_int_cst (integer_type_node, -1));
    4131              : #else
    4132          103 :   if (!ENABLE_OFFLOADING)
    4133            0 :     replace_call_with_value (gsi, integer_zero_node);
    4134              :   else
    4135              :     {
    4136              :       tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
    4137              :       gcall *repl = gimple_build_call (fn, 0);
    4138              :       replace_call_with_call_and_fold (gsi, repl);
    4139              :     }
    4140              : #endif
    4141          103 :   return true;
    4142              : }
    4143              : 
    4144              : static bool
    4145          294 : gimple_fold_builtin_omp_get_num_devices (gimple_stmt_iterator *gsi)
    4146              : {
    4147          294 :   if (!ENABLE_OFFLOADING)
    4148              :     {
    4149            0 :       replace_call_with_value (gsi, integer_zero_node);
    4150          294 :       return true;
    4151              :     }
    4152              :   return false;
    4153              : }
    4154              : 
    4155              : /* Fold a call to __builtin_acc_on_device.  */
    4156              : 
    4157              : static bool
    4158         2866 : gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
    4159              : {
    4160              :   /* Defer folding until we know which compiler we're in.  */
    4161         2866 :   if (symtab->state != EXPANSION)
    4162              :     return false;
    4163              : 
    4164          554 :   unsigned val_host = GOMP_DEVICE_HOST;
    4165          554 :   unsigned val_dev = GOMP_DEVICE_NONE;
    4166              : 
    4167              : #ifdef ACCEL_COMPILER
    4168              :   val_host = GOMP_DEVICE_NOT_HOST;
    4169              :   val_dev = ACCEL_COMPILER_acc_device;
    4170              : #endif
    4171              : 
    4172          554 :   location_t loc = gimple_location (gsi_stmt (*gsi));
    4173              : 
    4174          554 :   tree host_eq = make_ssa_name (boolean_type_node);
    4175          554 :   gimple *host_ass = gimple_build_assign
    4176          554 :     (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host));
    4177          554 :   gimple_set_location (host_ass, loc);
    4178          554 :   gsi_insert_before (gsi, host_ass, GSI_SAME_STMT);
    4179              : 
    4180          554 :   tree dev_eq = make_ssa_name (boolean_type_node);
    4181          554 :   gimple *dev_ass = gimple_build_assign
    4182          554 :     (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev));
    4183          554 :   gimple_set_location (dev_ass, loc);
    4184          554 :   gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT);
    4185              : 
    4186          554 :   tree result = make_ssa_name (boolean_type_node);
    4187          554 :   gimple *result_ass = gimple_build_assign
    4188          554 :     (result, BIT_IOR_EXPR, host_eq, dev_eq);
    4189          554 :   gimple_set_location (result_ass, loc);
    4190          554 :   gsi_insert_before (gsi, result_ass, GSI_SAME_STMT);
    4191              : 
    4192          554 :   replace_call_with_value (gsi, result);
    4193              : 
    4194          554 :   return true;
    4195              : }
    4196              : 
    4197              : /* Fold realloc (0, n) -> malloc (n).  */
    4198              : 
    4199              : static bool
    4200        48789 : gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
    4201              : {
    4202        48789 :   gimple *stmt = gsi_stmt (*gsi);
    4203        48789 :   tree arg = gimple_call_arg (stmt, 0);
    4204        48789 :   tree size = gimple_call_arg (stmt, 1);
    4205              : 
    4206       144990 :   if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
    4207              :     return false;
    4208              : 
    4209        48789 :   if (operand_equal_p (arg, null_pointer_node, 0))
    4210              :     {
    4211         1377 :       tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
    4212         1377 :       if (fn_malloc)
    4213              :         {
    4214         1377 :           gcall *repl = gimple_build_call (fn_malloc, 1, size);
    4215         1377 :           replace_call_with_call_and_fold (gsi, repl);
    4216         1377 :           return true;
    4217              :         }
    4218              :     }
    4219              :   return false;
    4220              : }
    4221              : 
    4222              : /* Number of bytes into which any type but aggregate, vector or
    4223              :    _BitInt types should fit.  */
    4224              : static constexpr size_t clear_padding_unit
    4225              :   = MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT;
    4226              : /* Buffer size on which __builtin_clear_padding folding code works.  */
    4227              : static const size_t clear_padding_buf_size = 32 * clear_padding_unit;
    4228              : 
    4229              : /* Data passed through __builtin_clear_padding folding.  */
    4230              : struct clear_padding_struct {
    4231              :   location_t loc;
    4232              :   /* 0 during __builtin_clear_padding folding, nonzero during
    4233              :      clear_type_padding_in_mask.  In that case, instead of clearing the
    4234              :      non-padding bits in union_ptr array clear the padding bits in there.  */
    4235              :   bool clear_in_mask;
    4236              :   tree base;
    4237              :   tree alias_type;
    4238              :   gimple_stmt_iterator *gsi;
    4239              :   /* Alignment of buf->base + 0.  */
    4240              :   unsigned align;
    4241              :   /* Offset from buf->base.  Should be always a multiple of UNITS_PER_WORD.  */
    4242              :   HOST_WIDE_INT off;
    4243              :   /* Number of padding bytes before buf->off that don't have padding clear
    4244              :      code emitted yet.  */
    4245              :   HOST_WIDE_INT padding_bytes;
    4246              :   /* The size of the whole object.  Never emit code to touch
    4247              :      buf->base + buf->sz or following bytes.  */
    4248              :   HOST_WIDE_INT sz;
    4249              :   /* Number of bytes recorded in buf->buf.  */
    4250              :   size_t size;
    4251              :   /* When inside union, instead of emitting code we and bits inside of
    4252              :      the union_ptr array.  */
    4253              :   unsigned char *union_ptr;
    4254              :   /* Set bits mean padding bits that need to be cleared by the builtin.  */
    4255              :   unsigned char buf[clear_padding_buf_size + clear_padding_unit];
    4256              : };
    4257              : 
    4258              : /* Emit code to clear padding requested in BUF->buf - set bits
    4259              :    in there stand for padding that should be cleared.  FULL is true
    4260              :    if everything from the buffer should be flushed, otherwise
    4261              :    it can leave up to 2 * clear_padding_unit bytes for further
    4262              :    processing.  */
    4263              : 
    4264              : static void
    4265        35258 : clear_padding_flush (clear_padding_struct *buf, bool full)
    4266              : {
    4267        35258 :   gcc_assert ((clear_padding_unit % UNITS_PER_WORD) == 0);
    4268        35258 :   if (!full && buf->size < 2 * clear_padding_unit)
    4269        35258 :     return;
    4270        36306 :   gcc_assert ((buf->off % UNITS_PER_WORD) == 0);
    4271        35216 :   size_t end = buf->size;
    4272        35216 :   if (!full)
    4273           42 :     end = ((end - clear_padding_unit - 1) / clear_padding_unit
    4274              :            * clear_padding_unit);
    4275        35216 :   size_t padding_bytes = buf->padding_bytes;
    4276        35216 :   if (buf->union_ptr)
    4277              :     {
    4278        34450 :       if (buf->clear_in_mask)
    4279              :         {
    4280              :           /* During clear_type_padding_in_mask, clear the padding
    4281              :              bits set in buf->buf in the buf->union_ptr mask.  */
    4282       237760 :           for (size_t i = 0; i < end; i++)
    4283              :             {
    4284       203703 :               if (buf->buf[i] == (unsigned char) ~0)
    4285         9036 :                 padding_bytes++;
    4286              :               else
    4287              :                 {
    4288       194667 :                   memset (&buf->union_ptr[buf->off + i - padding_bytes],
    4289              :                           0, padding_bytes);
    4290       194667 :                   padding_bytes = 0;
    4291       194667 :                   buf->union_ptr[buf->off + i] &= ~buf->buf[i];
    4292              :                 }
    4293              :             }
    4294        34057 :           if (full)
    4295              :             {
    4296        34057 :               memset (&buf->union_ptr[buf->off + end - padding_bytes],
    4297              :                       0, padding_bytes);
    4298        34057 :               buf->off = 0;
    4299        34057 :               buf->size = 0;
    4300        34057 :               buf->padding_bytes = 0;
    4301              :             }
    4302              :           else
    4303              :             {
    4304            0 :               memmove (buf->buf, buf->buf + end, buf->size - end);
    4305            0 :               buf->off += end;
    4306            0 :               buf->size -= end;
    4307            0 :               buf->padding_bytes = padding_bytes;
    4308              :             }
    4309        34057 :           return;
    4310              :         }
    4311              :       /* Inside of a union, instead of emitting any code, instead
    4312              :          clear all bits in the union_ptr buffer that are clear
    4313              :          in buf.  Whole padding bytes don't clear anything.  */
    4314         3017 :       for (size_t i = 0; i < end; i++)
    4315              :         {
    4316         2624 :           if (buf->buf[i] == (unsigned char) ~0)
    4317         1424 :             padding_bytes++;
    4318              :           else
    4319              :             {
    4320         1200 :               padding_bytes = 0;
    4321         1200 :               buf->union_ptr[buf->off + i] &= buf->buf[i];
    4322              :             }
    4323              :         }
    4324          393 :       if (full)
    4325              :         {
    4326          393 :           buf->off = 0;
    4327          393 :           buf->size = 0;
    4328          393 :           buf->padding_bytes = 0;
    4329              :         }
    4330              :       else
    4331              :         {
    4332            0 :           memmove (buf->buf, buf->buf + end, buf->size - end);
    4333            0 :           buf->off += end;
    4334            0 :           buf->size -= end;
    4335            0 :           buf->padding_bytes = padding_bytes;
    4336              :         }
    4337          393 :       return;
    4338              :     }
    4339          766 :   size_t wordsize = UNITS_PER_WORD;
    4340        23505 :   for (size_t i = 0; i < end; i += wordsize)
    4341              :     {
    4342        22739 :       size_t nonzero_first = wordsize;
    4343        22739 :       size_t nonzero_last = 0;
    4344        22739 :       size_t zero_first = wordsize;
    4345        22739 :       size_t zero_last = 0;
    4346        22739 :       bool all_ones = true, bytes_only = true;
    4347        23025 :       if ((unsigned HOST_WIDE_INT) (buf->off + i + wordsize)
    4348        22739 :           > (unsigned HOST_WIDE_INT) buf->sz)
    4349              :         {
    4350          286 :           gcc_assert (wordsize > 1);
    4351          286 :           wordsize /= 2;
    4352          286 :           i -= wordsize;
    4353          286 :           continue;
    4354              :         }
    4355        22453 :       size_t endsize = end - i > wordsize ? wordsize : end - i;
    4356       200832 :       for (size_t j = i; j < i + endsize; j++)
    4357              :         {
    4358       178379 :           if (buf->buf[j])
    4359              :             {
    4360       168378 :               if (nonzero_first == wordsize)
    4361              :                 {
    4362        21511 :                   nonzero_first = j - i;
    4363        21511 :                   nonzero_last = j - i;
    4364              :                 }
    4365       168378 :               if (nonzero_last != j - i)
    4366          158 :                 all_ones = false;
    4367       168378 :               nonzero_last = j + 1 - i;
    4368              :             }
    4369              :           else
    4370              :             {
    4371        10001 :               if (zero_first == wordsize)
    4372         1938 :                 zero_first = j - i;
    4373        10001 :               zero_last = j + 1 - i;
    4374              :             }
    4375       178379 :           if (buf->buf[j] != 0 && buf->buf[j] != (unsigned char) ~0)
    4376              :             {
    4377           85 :               all_ones = false;
    4378           85 :               bytes_only = false;
    4379              :             }
    4380              :         }
    4381        22453 :       size_t padding_end = i;
    4382        22453 :       if (padding_bytes)
    4383              :         {
    4384        20849 :           if (nonzero_first == 0
    4385        20849 :               && nonzero_last == endsize
    4386        20400 :               && all_ones)
    4387              :             {
    4388              :               /* All bits are padding and we had some padding
    4389              :                  before too.  Just extend it.  */
    4390        20400 :               padding_bytes += endsize;
    4391        20400 :               continue;
    4392              :             }
    4393          449 :           if (all_ones && nonzero_first == 0)
    4394              :             {
    4395            4 :               padding_bytes += nonzero_last;
    4396            4 :               padding_end += nonzero_last;
    4397            4 :               nonzero_first = wordsize;
    4398            4 :               nonzero_last = 0;
    4399              :             }
    4400          445 :           else if (bytes_only && nonzero_first == 0)
    4401              :             {
    4402            0 :               gcc_assert (zero_first && zero_first != wordsize);
    4403            0 :               padding_bytes += zero_first;
    4404            0 :               padding_end += zero_first;
    4405              :             }
    4406          449 :           tree atype, src;
    4407          449 :           if (padding_bytes == 1)
    4408              :             {
    4409           33 :               atype = char_type_node;
    4410           33 :               src = build_zero_cst (char_type_node);
    4411              :             }
    4412              :           else
    4413              :             {
    4414          416 :               atype = build_array_type_nelts (char_type_node, padding_bytes);
    4415          416 :               src = build_constructor (atype, NULL);
    4416              :             }
    4417          449 :           tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
    4418              :                                  build_int_cst (buf->alias_type,
    4419          449 :                                                 buf->off + padding_end
    4420          449 :                                                 - padding_bytes));
    4421          449 :           gimple *g = gimple_build_assign (dst, src);
    4422          449 :           gimple_set_location (g, buf->loc);
    4423          449 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4424          449 :           padding_bytes = 0;
    4425          449 :           buf->padding_bytes = 0;
    4426              :         }
    4427         2053 :       if (nonzero_first == wordsize)
    4428              :         /* All bits in a word are 0, there are no padding bits.  */
    4429          946 :         continue;
    4430         1107 :       if (all_ones && nonzero_last == endsize)
    4431              :         {
    4432              :           /* All bits between nonzero_first and end of word are padding
    4433              :              bits, start counting padding_bytes.  */
    4434          841 :           padding_bytes = nonzero_last - nonzero_first;
    4435          841 :           continue;
    4436              :         }
    4437          266 :       if (bytes_only)
    4438              :         {
    4439              :           /* If bitfields aren't involved in this word, prefer storing
    4440              :              individual bytes or groups of them over performing a RMW
    4441              :              operation on the whole word.  */
    4442          227 :           gcc_assert (i + zero_last <= end);
    4443         1117 :           for (size_t j = padding_end; j < i + zero_last; j++)
    4444              :             {
    4445          890 :               if (buf->buf[j])
    4446              :                 {
    4447              :                   size_t k;
    4448          606 :                   for (k = j; k < i + zero_last; k++)
    4449          606 :                     if (buf->buf[k] == 0)
    4450              :                       break;
    4451          259 :                   HOST_WIDE_INT off = buf->off + j;
    4452          259 :                   tree atype, src;
    4453          259 :                   if (k - j == 1)
    4454              :                     {
    4455          215 :                       atype = char_type_node;
    4456          215 :                       src = build_zero_cst (char_type_node);
    4457              :                     }
    4458              :                   else
    4459              :                     {
    4460           44 :                       atype = build_array_type_nelts (char_type_node, k - j);
    4461           44 :                       src = build_constructor (atype, NULL);
    4462              :                     }
    4463          259 :                   tree dst = build2_loc (buf->loc, MEM_REF, atype,
    4464              :                                          buf->base,
    4465          259 :                                          build_int_cst (buf->alias_type, off));
    4466          259 :                   gimple *g = gimple_build_assign (dst, src);
    4467          259 :                   gimple_set_location (g, buf->loc);
    4468          259 :                   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4469          259 :                   j = k;
    4470              :                 }
    4471              :             }
    4472          227 :           if (nonzero_last == endsize)
    4473           98 :             padding_bytes = nonzero_last - zero_last;
    4474          227 :           continue;
    4475          227 :         }
    4476          126 :       for (size_t eltsz = 1; eltsz <= wordsize; eltsz <<= 1)
    4477              :         {
    4478          126 :           if (nonzero_last - nonzero_first <= eltsz
    4479           39 :               && ((nonzero_first & ~(eltsz - 1))
    4480           39 :                   == ((nonzero_last - 1) & ~(eltsz - 1))))
    4481              :             {
    4482           39 :               tree type;
    4483           39 :               if (eltsz == 1)
    4484            2 :                 type = char_type_node;
    4485              :               else
    4486           37 :                 type = lang_hooks.types.type_for_size (eltsz * BITS_PER_UNIT,
    4487              :                                                        0);
    4488           39 :               size_t start = nonzero_first & ~(eltsz - 1);
    4489           39 :               HOST_WIDE_INT off = buf->off + i + start;
    4490           39 :               tree atype = type;
    4491           39 :               if (eltsz > 1 && buf->align < TYPE_ALIGN (type))
    4492            0 :                 atype = build_aligned_type (type, buf->align);
    4493           39 :               tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
    4494           39 :                                      build_int_cst (buf->alias_type, off));
    4495           39 :               tree src;
    4496           39 :               gimple *g;
    4497           39 :               if (all_ones
    4498           39 :                   && nonzero_first == start
    4499            0 :                   && nonzero_last == start + eltsz)
    4500            0 :                 src = build_zero_cst (type);
    4501              :               else
    4502              :                 {
    4503           39 :                   src = make_ssa_name (type);
    4504           39 :                   tree tmp_dst = unshare_expr (dst);
    4505              :                   /* The folding introduces a read from the tmp_dst, we should
    4506              :                      prevent uninitialized warning analysis from issuing warning
    4507              :                      for such fake read.  In order to suppress warning only for
    4508              :                      this expr, we should set the location of tmp_dst to
    4509              :                      UNKNOWN_LOCATION first, then suppress_warning will call
    4510              :                      set_no_warning_bit to set the no_warning flag only for
    4511              :                      tmp_dst.  */
    4512           39 :                   SET_EXPR_LOCATION (tmp_dst, UNKNOWN_LOCATION);
    4513           39 :                   suppress_warning (tmp_dst, OPT_Wuninitialized);
    4514           39 :                   g = gimple_build_assign (src, tmp_dst);
    4515           39 :                   gimple_set_location (g, buf->loc);
    4516           39 :                   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4517           78 :                   tree mask = native_interpret_expr (type,
    4518           39 :                                                      buf->buf + i + start,
    4519              :                                                      eltsz);
    4520           39 :                   gcc_assert (mask && TREE_CODE (mask) == INTEGER_CST);
    4521           39 :                   mask = fold_build1 (BIT_NOT_EXPR, type, mask);
    4522           39 :                   tree src_masked = make_ssa_name (type);
    4523           39 :                   g = gimple_build_assign (src_masked, BIT_AND_EXPR,
    4524              :                                            src, mask);
    4525           39 :                   gimple_set_location (g, buf->loc);
    4526           39 :                   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4527           39 :                   src = src_masked;
    4528              :                 }
    4529           39 :               g = gimple_build_assign (dst, src);
    4530           39 :               gimple_set_location (g, buf->loc);
    4531           39 :               gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4532           39 :               break;
    4533              :             }
    4534              :         }
    4535              :     }
    4536          766 :   if (full)
    4537              :     {
    4538          724 :       if (padding_bytes)
    4539              :         {
    4540          490 :           tree atype, src;
    4541          490 :           if (padding_bytes == 1)
    4542              :             {
    4543          110 :               atype = char_type_node;
    4544          110 :               src = build_zero_cst (char_type_node);
    4545              :             }
    4546              :           else
    4547              :             {
    4548          380 :               atype = build_array_type_nelts (char_type_node, padding_bytes);
    4549          380 :               src = build_constructor (atype, NULL);
    4550              :             }
    4551          490 :           tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
    4552              :                                  build_int_cst (buf->alias_type,
    4553          490 :                                                 buf->off + end
    4554          490 :                                                 - padding_bytes));
    4555          490 :           gimple *g = gimple_build_assign (dst, src);
    4556          490 :           gimple_set_location (g, buf->loc);
    4557          490 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4558              :         }
    4559          724 :       size_t end_rem = end % UNITS_PER_WORD;
    4560          724 :       buf->off += end - end_rem;
    4561          724 :       buf->size = end_rem;
    4562          724 :       memset (buf->buf, 0, buf->size);
    4563          724 :       buf->padding_bytes = 0;
    4564              :     }
    4565              :   else
    4566              :     {
    4567           42 :       memmove (buf->buf, buf->buf + end, buf->size - end);
    4568           42 :       buf->off += end;
    4569           42 :       buf->size -= end;
    4570           42 :       buf->padding_bytes = padding_bytes;
    4571              :     }
    4572              : }
    4573              : 
    4574              : /* Append PADDING_BYTES padding bytes.  */
    4575              : 
    4576              : static void
    4577         5215 : clear_padding_add_padding (clear_padding_struct *buf,
    4578              :                            HOST_WIDE_INT padding_bytes)
    4579              : {
    4580         5215 :   if (padding_bytes == 0)
    4581              :     return;
    4582         1679 :   if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
    4583              :       > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
    4584           42 :     clear_padding_flush (buf, false);
    4585         1679 :   if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
    4586              :       > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
    4587              :     {
    4588           42 :       memset (buf->buf + buf->size, ~0, clear_padding_buf_size - buf->size);
    4589           42 :       padding_bytes -= clear_padding_buf_size - buf->size;
    4590           42 :       buf->size = clear_padding_buf_size;
    4591           42 :       clear_padding_flush (buf, false);
    4592           42 :       gcc_assert (buf->padding_bytes);
    4593              :       /* At this point buf->buf[0] through buf->buf[buf->size - 1]
    4594              :          is guaranteed to be all ones.  */
    4595           42 :       padding_bytes += buf->size;
    4596           42 :       buf->size = padding_bytes % UNITS_PER_WORD;
    4597           42 :       memset (buf->buf, ~0, buf->size);
    4598           42 :       buf->off += padding_bytes - buf->size;
    4599           42 :       buf->padding_bytes += padding_bytes - buf->size;
    4600              :     }
    4601              :   else
    4602              :     {
    4603         1637 :       memset (buf->buf + buf->size, ~0, padding_bytes);
    4604         1637 :       buf->size += padding_bytes;
    4605              :     }
    4606              : }
    4607              : 
    4608              : static void clear_padding_type (clear_padding_struct *, tree,
    4609              :                                 HOST_WIDE_INT, bool);
    4610              : 
    4611              : /* Clear padding bits of union type TYPE.  */
    4612              : 
    4613              : static void
    4614          128 : clear_padding_union (clear_padding_struct *buf, tree type,
    4615              :                      HOST_WIDE_INT sz, bool for_auto_init)
    4616              : {
    4617          128 :   clear_padding_struct *union_buf;
    4618          128 :   HOST_WIDE_INT start_off = 0, next_off = 0;
    4619          128 :   size_t start_size = 0;
    4620          128 :   if (buf->union_ptr)
    4621              :     {
    4622           42 :       start_off = buf->off + buf->size;
    4623           42 :       next_off = start_off + sz;
    4624           42 :       start_size = start_off % UNITS_PER_WORD;
    4625           42 :       start_off -= start_size;
    4626           42 :       clear_padding_flush (buf, true);
    4627           42 :       union_buf = buf;
    4628              :     }
    4629              :   else
    4630              :     {
    4631           86 :       if (sz + buf->size > clear_padding_buf_size)
    4632            0 :         clear_padding_flush (buf, false);
    4633           86 :       union_buf = XALLOCA (clear_padding_struct);
    4634           86 :       union_buf->loc = buf->loc;
    4635           86 :       union_buf->clear_in_mask = buf->clear_in_mask;
    4636           86 :       union_buf->base = NULL_TREE;
    4637           86 :       union_buf->alias_type = NULL_TREE;
    4638           86 :       union_buf->gsi = NULL;
    4639           86 :       union_buf->align = 0;
    4640           86 :       union_buf->off = 0;
    4641           86 :       union_buf->padding_bytes = 0;
    4642           86 :       union_buf->sz = sz;
    4643           86 :       union_buf->size = 0;
    4644           86 :       if (sz + buf->size <= clear_padding_buf_size)
    4645           86 :         union_buf->union_ptr = buf->buf + buf->size;
    4646              :       else
    4647            0 :         union_buf->union_ptr = XNEWVEC (unsigned char, sz);
    4648           86 :       memset (union_buf->union_ptr, ~0, sz);
    4649              :     }
    4650              : 
    4651         1193 :   for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    4652         1065 :     if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
    4653              :       {
    4654          359 :         if (DECL_SIZE_UNIT (field) == NULL_TREE)
    4655              :           {
    4656            8 :             if (TREE_TYPE (field) == error_mark_node)
    4657            0 :               continue;
    4658            8 :             gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
    4659              :                         && !COMPLETE_TYPE_P (TREE_TYPE (field)));
    4660            8 :             if (!buf->clear_in_mask && !for_auto_init)
    4661            8 :               error_at (buf->loc, "flexible array member %qD does not have "
    4662              :                                   "well defined padding bits for %qs",
    4663              :                         field, "__builtin_clear_padding");
    4664            8 :             continue;
    4665              :           }
    4666          351 :         HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
    4667          351 :         gcc_assert (union_buf->size == 0);
    4668          351 :         union_buf->off = start_off;
    4669          351 :         union_buf->size = start_size;
    4670          351 :         memset (union_buf->buf, ~0, start_size);
    4671          351 :         clear_padding_type (union_buf, TREE_TYPE (field), fldsz, for_auto_init);
    4672          351 :         clear_padding_add_padding (union_buf, sz - fldsz);
    4673          351 :         clear_padding_flush (union_buf, true);
    4674              :       }
    4675              : 
    4676          128 :   if (buf == union_buf)
    4677              :     {
    4678           42 :       buf->off = next_off;
    4679           42 :       buf->size = next_off % UNITS_PER_WORD;
    4680           42 :       buf->off -= buf->size;
    4681           42 :       memset (buf->buf, ~0, buf->size);
    4682              :     }
    4683           86 :   else if (sz + buf->size <= clear_padding_buf_size)
    4684           86 :     buf->size += sz;
    4685              :   else
    4686              :     {
    4687            0 :       unsigned char *union_ptr = union_buf->union_ptr;
    4688            0 :       while (sz)
    4689              :         {
    4690            0 :           clear_padding_flush (buf, false);
    4691            0 :           HOST_WIDE_INT this_sz
    4692            0 :             = MIN ((unsigned HOST_WIDE_INT) sz,
    4693              :                    clear_padding_buf_size - buf->size);
    4694            0 :           memcpy (buf->buf + buf->size, union_ptr, this_sz);
    4695            0 :           buf->size += this_sz;
    4696            0 :           union_ptr += this_sz;
    4697            0 :           sz -= this_sz;
    4698              :         }
    4699            0 :       XDELETE (union_buf->union_ptr);
    4700              :     }
    4701          128 : }
    4702              : 
    4703              : /* The only known floating point formats with padding bits are the
    4704              :    IEEE extended ones.  */
    4705              : 
    4706              : static bool
    4707        35842 : clear_padding_real_needs_padding_p (tree type)
    4708              : {
    4709        35842 :   const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
    4710        35842 :   return (fmt->b == 2
    4711        35383 :           && fmt->signbit_ro == fmt->signbit_rw
    4712        71225 :           && (fmt->signbit_ro == 79 || fmt->signbit_ro == 95));
    4713              : }
    4714              : 
    4715              : /* _BitInt has padding bits if it isn't extended in the ABI and has smaller
    4716              :    precision than bits in limb or corresponding number of limbs.  */
    4717              : 
    4718              : static bool
    4719           54 : clear_padding_bitint_needs_padding_p (tree type)
    4720              : {
    4721           54 :   struct bitint_info info;
    4722           54 :   bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
    4723           54 :   gcc_assert (ok);
    4724           54 :   if (info.extended)
    4725              :     return false;
    4726           54 :   scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.abi_limb_mode);
    4727           54 :   if (TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
    4728              :     return true;
    4729            4 :   else if (TYPE_PRECISION (type) == GET_MODE_PRECISION (limb_mode))
    4730              :     return false;
    4731              :   else
    4732            4 :     return (((unsigned) TYPE_PRECISION (type))
    4733            4 :             % GET_MODE_PRECISION (limb_mode)) != 0;
    4734              : }
    4735              : 
    4736              : /* Return true if TYPE might contain any padding bits.  */
    4737              : 
    4738              : bool
    4739       916508 : clear_padding_type_may_have_padding_p (tree type)
    4740              : {
    4741      1052888 :   switch (TREE_CODE (type))
    4742              :     {
    4743              :     case RECORD_TYPE:
    4744              :     case UNION_TYPE:
    4745              :       return true;
    4746       136380 :     case ARRAY_TYPE:
    4747       136380 :     case COMPLEX_TYPE:
    4748       136380 :     case VECTOR_TYPE:
    4749       136380 :       return clear_padding_type_may_have_padding_p (TREE_TYPE (type));
    4750         1799 :     case REAL_TYPE:
    4751         1799 :       return clear_padding_real_needs_padding_p (type);
    4752           61 :     case ENUMERAL_TYPE:
    4753           61 :       if (BITINT_TYPE_P (type))
    4754            0 :         return clear_padding_bitint_needs_padding_p (type);
    4755              :       return false;
    4756           54 :     case BITINT_TYPE:
    4757           54 :       return clear_padding_bitint_needs_padding_p (type);
    4758              :     default:
    4759              :       return false;
    4760              :     }
    4761              : }
    4762              : 
    4763              : /* Return true if TYPE has padding bits aside from those in fields,
    4764              :    elements, etc.  */
    4765              : 
    4766              : bool
    4767      1174400 : type_has_padding_at_level_p (tree type)
    4768              : {
    4769      1174400 :   switch (TREE_CODE (type))
    4770              :     {
    4771      1035856 :     case RECORD_TYPE:
    4772      1035856 :       {
    4773      1035856 :         tree bitpos = size_zero_node;
    4774              :         /* Expect fields to be sorted by bit position.  */
    4775      7663293 :         for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
    4776      6632134 :           if (TREE_CODE (f) == FIELD_DECL)
    4777              :             {
    4778      2270800 :               if (DECL_PADDING_P (f))
    4779              :                 return true;
    4780      2270797 :               tree pos = bit_position (f);
    4781      2270797 :               if (simple_cst_equal (bitpos, pos) != 1)
    4782              :                 return true;
    4783      2266126 :               if (!DECL_SIZE (f))
    4784              :                 return true;
    4785      2266103 :               bitpos = int_const_binop (PLUS_EXPR, pos, DECL_SIZE (f));
    4786              :             }
    4787      1031159 :         if (simple_cst_equal (bitpos, TYPE_SIZE (type)) != 1)
    4788              :           return true;
    4789              :         return false;
    4790              :       }
    4791            3 :     case UNION_TYPE:
    4792            3 :     case QUAL_UNION_TYPE:
    4793            3 :       bool any_fields;
    4794            3 :       any_fields = false;
    4795              :       /* If any of the fields is smaller than the whole, there is padding.  */
    4796            6 :       for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
    4797            3 :         if (TREE_CODE (f) != FIELD_DECL || TREE_TYPE (f) == error_mark_node)
    4798            3 :           continue;
    4799            0 :         else if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
    4800            0 :                                    TYPE_SIZE (type)) != 1)
    4801              :           return true;
    4802              :         else
    4803              :           any_fields = true;
    4804              :       /* If the union doesn't have any fields and still has non-zero size,
    4805              :          all of it is padding.  */
    4806            3 :       if (!any_fields && !integer_zerop (TYPE_SIZE (type)))
    4807              :         return true;
    4808              :       return false;
    4809              :     case ARRAY_TYPE:
    4810              :     case COMPLEX_TYPE:
    4811              :     case VECTOR_TYPE:
    4812              :       /* No recursing here, no padding at this level.  */
    4813              :       return false;
    4814            0 :     case REAL_TYPE:
    4815            0 :       return clear_padding_real_needs_padding_p (type);
    4816            0 :     case ENUMERAL_TYPE:
    4817            0 :       if (BITINT_TYPE_P (type))
    4818            0 :         return clear_padding_bitint_needs_padding_p (type);
    4819              :       return false;
    4820            0 :     case BITINT_TYPE:
    4821            0 :       return clear_padding_bitint_needs_padding_p (type);
    4822              :     default:
    4823              :       return false;
    4824              :     }
    4825              : }
    4826              : 
    4827              : /* Emit a runtime loop:
    4828              :    for (; buf.base != end; buf.base += sz)
    4829              :      __builtin_clear_padding (buf.base);  */
    4830              : 
    4831              : static void
    4832          114 : clear_padding_emit_loop (clear_padding_struct *buf, tree type,
    4833              :                          tree end, bool for_auto_init)
    4834              : {
    4835          114 :   tree l1 = create_artificial_label (buf->loc);
    4836          114 :   tree l2 = create_artificial_label (buf->loc);
    4837          114 :   tree l3 = create_artificial_label (buf->loc);
    4838          114 :   gimple *g = gimple_build_goto (l2);
    4839          114 :   gimple_set_location (g, buf->loc);
    4840          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4841          114 :   g = gimple_build_label (l1);
    4842          114 :   gimple_set_location (g, buf->loc);
    4843          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4844          114 :   clear_padding_type (buf, type, buf->sz, for_auto_init);
    4845          114 :   clear_padding_flush (buf, true);
    4846          114 :   g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR, buf->base,
    4847          114 :                            size_int (buf->sz));
    4848          114 :   gimple_set_location (g, buf->loc);
    4849          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4850          114 :   g = gimple_build_label (l2);
    4851          114 :   gimple_set_location (g, buf->loc);
    4852          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4853          114 :   g = gimple_build_cond (NE_EXPR, buf->base, end, l1, l3);
    4854          114 :   gimple_set_location (g, buf->loc);
    4855          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4856          114 :   g = gimple_build_label (l3);
    4857          114 :   gimple_set_location (g, buf->loc);
    4858          114 :   gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    4859          114 : }
    4860              : 
    4861              : /* Clear padding bits for TYPE.  Called recursively from
    4862              :    gimple_fold_builtin_clear_padding.  If FOR_AUTO_INIT is true,
    4863              :    the __builtin_clear_padding is not called by the end user,
    4864              :    instead, it's inserted by the compiler to initialize the
    4865              :    paddings of automatic variable.  Therefore, we should not
    4866              :    emit the error messages for flexible array members to confuse
    4867              :    the end user.  */
    4868              : 
    4869              : static void
    4870        39327 : clear_padding_type (clear_padding_struct *buf, tree type,
    4871              :                     HOST_WIDE_INT sz, bool for_auto_init)
    4872              : {
    4873        39327 :   switch (TREE_CODE (type))
    4874              :     {
    4875         1363 :     case RECORD_TYPE:
    4876         1363 :       HOST_WIDE_INT cur_pos;
    4877         1363 :       cur_pos = 0;
    4878        23099 :       for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    4879        21736 :         if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
    4880              :           {
    4881         3814 :             tree ftype = TREE_TYPE (field);
    4882         3814 :             if (DECL_BIT_FIELD (field))
    4883              :               {
    4884          260 :                 HOST_WIDE_INT fldsz = TYPE_PRECISION (ftype);
    4885          260 :                 if (fldsz == 0)
    4886            0 :                   continue;
    4887          260 :                 HOST_WIDE_INT pos = int_byte_position (field);
    4888          260 :                 if (pos >= sz)
    4889            0 :                   continue;
    4890          260 :                 HOST_WIDE_INT bpos
    4891          260 :                   = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
    4892          260 :                 bpos %= BITS_PER_UNIT;
    4893          260 :                 HOST_WIDE_INT end
    4894          260 :                   = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
    4895          260 :                 if (pos + end > cur_pos)
    4896              :                   {
    4897          199 :                     clear_padding_add_padding (buf, pos + end - cur_pos);
    4898          199 :                     cur_pos = pos + end;
    4899              :                   }
    4900          260 :                 gcc_assert (cur_pos > pos
    4901              :                             && ((unsigned HOST_WIDE_INT) buf->size
    4902              :                                 >= (unsigned HOST_WIDE_INT) cur_pos - pos));
    4903          260 :                 unsigned char *p = buf->buf + buf->size - (cur_pos - pos);
    4904          260 :                 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
    4905              :                   sorry_at (buf->loc, "PDP11 bit-field handling unsupported"
    4906              :                                       " in %qs", "__builtin_clear_padding");
    4907          260 :                 else if (BYTES_BIG_ENDIAN)
    4908              :                   {
    4909              :                     /* Big endian.  */
    4910              :                     if (bpos + fldsz <= BITS_PER_UNIT)
    4911              :                       *p &= ~(((1 << fldsz) - 1)
    4912              :                               << (BITS_PER_UNIT - bpos - fldsz));
    4913              :                     else
    4914              :                       {
    4915              :                         if (bpos)
    4916              :                           {
    4917              :                             *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
    4918              :                             p++;
    4919              :                             fldsz -= BITS_PER_UNIT - bpos;
    4920              :                           }
    4921              :                         memset (p, 0, fldsz / BITS_PER_UNIT);
    4922              :                         p += fldsz / BITS_PER_UNIT;
    4923              :                         fldsz %= BITS_PER_UNIT;
    4924              :                         if (fldsz)
    4925              :                           *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
    4926              :                       }
    4927              :                   }
    4928              :                 else
    4929              :                   {
    4930              :                     /* Little endian.  */
    4931          260 :                     if (bpos + fldsz <= BITS_PER_UNIT)
    4932          159 :                       *p &= ~(((1 << fldsz) - 1) << bpos);
    4933              :                     else
    4934              :                       {
    4935          101 :                         if (bpos)
    4936              :                           {
    4937           33 :                             *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
    4938           33 :                             p++;
    4939           33 :                             fldsz -= BITS_PER_UNIT - bpos;
    4940              :                           }
    4941          101 :                         memset (p, 0, fldsz / BITS_PER_UNIT);
    4942          101 :                         p += fldsz / BITS_PER_UNIT;
    4943          101 :                         fldsz %= BITS_PER_UNIT;
    4944          101 :                         if (fldsz)
    4945           56 :                           *p &= ~((1 << fldsz) - 1);
    4946              :                       }
    4947              :                   }
    4948              :               }
    4949         3554 :             else if (DECL_SIZE_UNIT (field) == NULL_TREE)
    4950              :               {
    4951           32 :                 if (ftype == error_mark_node)
    4952            0 :                   continue;
    4953           32 :                 gcc_assert (TREE_CODE (ftype) == ARRAY_TYPE
    4954              :                             && !COMPLETE_TYPE_P (ftype));
    4955           32 :                 if (!buf->clear_in_mask && !for_auto_init)
    4956           24 :                   error_at (buf->loc, "flexible array member %qD does not "
    4957              :                                       "have well defined padding bits for %qs",
    4958              :                             field, "__builtin_clear_padding");
    4959              :               }
    4960         3522 :             else if (is_empty_type (ftype))
    4961          220 :               continue;
    4962              :             else
    4963              :               {
    4964         3302 :                 HOST_WIDE_INT pos = int_byte_position (field);
    4965         3302 :                 if (pos >= sz)
    4966            0 :                   continue;
    4967         3302 :                 HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
    4968         3302 :                 gcc_assert (pos >= 0 && fldsz >= 0 && pos >= cur_pos);
    4969         3302 :                 clear_padding_add_padding (buf, pos - cur_pos);
    4970         3302 :                 cur_pos = pos;
    4971         3302 :                 if (tree asbase = lang_hooks.types.classtype_as_base (field))
    4972          240 :                   ftype = asbase;
    4973         3302 :                 clear_padding_type (buf, ftype, fldsz, for_auto_init);
    4974         3302 :                 cur_pos += fldsz;
    4975              :               }
    4976              :           }
    4977         1363 :       gcc_assert (sz >= cur_pos);
    4978         1363 :       clear_padding_add_padding (buf, sz - cur_pos);
    4979         1363 :       break;
    4980          325 :     case ARRAY_TYPE:
    4981          325 :       HOST_WIDE_INT nelts, fldsz;
    4982          325 :       fldsz = int_size_in_bytes (TREE_TYPE (type));
    4983          325 :       if (fldsz == 0)
    4984              :         break;
    4985          311 :       nelts = sz / fldsz;
    4986          311 :       if (nelts > 1
    4987          304 :           && sz > 8 * UNITS_PER_WORD
    4988           78 :           && buf->union_ptr == NULL
    4989          389 :           && clear_padding_type_may_have_padding_p (TREE_TYPE (type)))
    4990              :         {
    4991              :           /* For sufficiently large array of more than one elements,
    4992              :              emit a runtime loop to keep code size manageable.  */
    4993           66 :           tree base = buf->base;
    4994           66 :           unsigned int prev_align = buf->align;
    4995           66 :           HOST_WIDE_INT off = buf->off + buf->size;
    4996           66 :           HOST_WIDE_INT prev_sz = buf->sz;
    4997           66 :           clear_padding_flush (buf, true);
    4998           66 :           tree elttype = TREE_TYPE (type);
    4999           66 :           buf->base = create_tmp_var (build_pointer_type (elttype));
    5000           66 :           tree end = make_ssa_name (TREE_TYPE (buf->base));
    5001           66 :           gimple *g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR,
    5002           66 :                                            base, size_int (off));
    5003           66 :           gimple_set_location (g, buf->loc);
    5004           66 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5005           66 :           g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf->base,
    5006           66 :                                    size_int (sz));
    5007           66 :           gimple_set_location (g, buf->loc);
    5008           66 :           gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
    5009           66 :           buf->sz = fldsz;
    5010           66 :           buf->align = TYPE_ALIGN (elttype);
    5011           66 :           buf->off = 0;
    5012           66 :           buf->size = 0;
    5013           66 :           clear_padding_emit_loop (buf, elttype, end, for_auto_init);
    5014           66 :           off += sz;
    5015           66 :           buf->base = base;
    5016           66 :           buf->sz = prev_sz;
    5017           66 :           buf->align = prev_align;
    5018           66 :           buf->size = off % UNITS_PER_WORD;
    5019           66 :           buf->off = off - buf->size;
    5020           66 :           memset (buf->buf, 0, buf->size);
    5021           66 :           break;
    5022              :         }
    5023         1163 :       for (HOST_WIDE_INT i = 0; i < nelts; i++)
    5024          918 :         clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5025              :       break;
    5026          128 :     case UNION_TYPE:
    5027          128 :       clear_padding_union (buf, type, sz, for_auto_init);
    5028          128 :       break;
    5029        34043 :     case REAL_TYPE:
    5030        34043 :       gcc_assert ((size_t) sz <= clear_padding_unit);
    5031        34043 :       if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
    5032            0 :         clear_padding_flush (buf, false);
    5033        34043 :       if (clear_padding_real_needs_padding_p (type))
    5034              :         {
    5035              :           /* Use native_interpret_real + native_encode_expr to figure out
    5036              :              which bits are padding.  */
    5037         1587 :           memset (buf->buf + buf->size, ~0, sz);
    5038         1587 :           tree cst = native_interpret_real (type, buf->buf + buf->size, sz);
    5039         1587 :           gcc_assert (cst && TREE_CODE (cst) == REAL_CST);
    5040         1587 :           int len = native_encode_expr (cst, buf->buf + buf->size, sz);
    5041         1587 :           gcc_assert (len > 0 && (size_t) len == (size_t) sz);
    5042        26979 :           for (size_t i = 0; i < (size_t) sz; i++)
    5043        25392 :             buf->buf[buf->size + i] ^= ~0;
    5044              :         }
    5045              :       else
    5046        32456 :         memset (buf->buf + buf->size, 0, sz);
    5047        34043 :       buf->size += sz;
    5048        34043 :       break;
    5049            0 :     case COMPLEX_TYPE:
    5050            0 :       fldsz = int_size_in_bytes (TREE_TYPE (type));
    5051            0 :       clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5052            0 :       clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5053            0 :       break;
    5054            8 :     case VECTOR_TYPE:
    5055            8 :       nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
    5056            8 :       fldsz = int_size_in_bytes (TREE_TYPE (type));
    5057           40 :       for (HOST_WIDE_INT i = 0; i < nelts; i++)
    5058           32 :         clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
    5059              :       break;
    5060            7 :     case NULLPTR_TYPE:
    5061            7 :       gcc_assert ((size_t) sz <= clear_padding_unit);
    5062            7 :       if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
    5063            0 :         clear_padding_flush (buf, false);
    5064            7 :       memset (buf->buf + buf->size, ~0, sz);
    5065            7 :       buf->size += sz;
    5066            7 :       break;
    5067            4 :     case BITINT_TYPE:
    5068            4 :     do_bitint:
    5069            4 :       {
    5070            4 :         struct bitint_info info;
    5071            4 :         bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
    5072            4 :         gcc_assert (ok);
    5073            4 :         scalar_int_mode limb_mode
    5074            4 :           = as_a <scalar_int_mode> (info.abi_limb_mode);
    5075            4 :         if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode))
    5076              :           {
    5077            2 :             gcc_assert ((size_t) sz <= clear_padding_unit);
    5078            2 :             if ((unsigned HOST_WIDE_INT) sz + buf->size
    5079              :                 > clear_padding_buf_size)
    5080            0 :               clear_padding_flush (buf, false);
    5081            2 :             if (!info.extended
    5082            2 :                 && TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
    5083              :               {
    5084            2 :                 int tprec = GET_MODE_PRECISION (limb_mode);
    5085            2 :                 int prec = TYPE_PRECISION (type);
    5086            2 :                 tree t = build_nonstandard_integer_type (tprec, 1);
    5087            2 :                 tree cst = wide_int_to_tree (t, wi::mask (prec, true, tprec));
    5088            2 :                 int len = native_encode_expr (cst, buf->buf + buf->size, sz);
    5089            2 :                 gcc_assert (len > 0 && (size_t) len == (size_t) sz);
    5090              :               }
    5091              :             else
    5092            0 :               memset (buf->buf + buf->size, 0, sz);
    5093            2 :             buf->size += sz;
    5094            2 :             break;
    5095              :           }
    5096            2 :         tree limbtype
    5097            2 :           = build_nonstandard_integer_type (GET_MODE_PRECISION (limb_mode), 1);
    5098            2 :         fldsz = int_size_in_bytes (limbtype);
    5099            2 :         nelts = int_size_in_bytes (type) / fldsz;
    5100           13 :         for (HOST_WIDE_INT i = 0; i < nelts; i++)
    5101              :           {
    5102           11 :             if (!info.extended
    5103           11 :                 && i == (info.big_endian ? 0 : nelts - 1)
    5104           13 :                 && (((unsigned) TYPE_PRECISION (type))
    5105            2 :                     % TYPE_PRECISION (limbtype)) != 0)
    5106              :               {
    5107            2 :                 int tprec = GET_MODE_PRECISION (limb_mode);
    5108            2 :                 int prec = (((unsigned) TYPE_PRECISION (type)) % tprec);
    5109            2 :                 tree cst = wide_int_to_tree (limbtype,
    5110            2 :                                              wi::mask (prec, true, tprec));
    5111            2 :                 int len = native_encode_expr (cst, buf->buf + buf->size,
    5112              :                                               fldsz);
    5113            2 :                 gcc_assert (len > 0 && (size_t) len == (size_t) fldsz);
    5114            2 :                 buf->size += fldsz;
    5115              :               }
    5116              :             else
    5117            9 :               clear_padding_type (buf, limbtype, fldsz, for_auto_init);
    5118              :           }
    5119              :         break;
    5120              :       }
    5121            0 :     case ENUMERAL_TYPE:
    5122            0 :       if (BITINT_TYPE_P (type))
    5123            0 :         goto do_bitint;
    5124              :       /* FALLTHRU */
    5125         3449 :     default:
    5126         3449 :       gcc_assert ((size_t) sz <= clear_padding_unit);
    5127         3449 :       if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
    5128            0 :         clear_padding_flush (buf, false);
    5129         3449 :       memset (buf->buf + buf->size, 0, sz);
    5130         3449 :       buf->size += sz;
    5131         3449 :       break;
    5132              :     }
    5133        39327 : }
    5134              : 
    5135              : /* Clear padding bits of TYPE in MASK.  */
    5136              : 
    5137              : void
    5138        34057 : clear_type_padding_in_mask (tree type, unsigned char *mask)
    5139              : {
    5140        34057 :   clear_padding_struct buf;
    5141        34057 :   buf.loc = UNKNOWN_LOCATION;
    5142        34057 :   buf.clear_in_mask = true;
    5143        34057 :   buf.base = NULL_TREE;
    5144        34057 :   buf.alias_type = NULL_TREE;
    5145        34057 :   buf.gsi = NULL;
    5146        34057 :   buf.align = 0;
    5147        34057 :   buf.off = 0;
    5148        34057 :   buf.padding_bytes = 0;
    5149        34057 :   buf.sz = int_size_in_bytes (type);
    5150        34057 :   buf.size = 0;
    5151        34057 :   buf.union_ptr = mask;
    5152        34057 :   clear_padding_type (&buf, type, buf.sz, false);
    5153        34057 :   clear_padding_flush (&buf, true);
    5154        34057 : }
    5155              : 
    5156              : /* Fold __builtin_clear_padding builtin.  */
    5157              : 
    5158              : static bool
    5159          630 : gimple_fold_builtin_clear_padding (gimple_stmt_iterator *gsi)
    5160              : {
    5161          630 :   gimple *stmt = gsi_stmt (*gsi);
    5162          630 :   gcc_assert (gimple_call_num_args (stmt) == 2);
    5163          630 :   tree ptr = gimple_call_arg (stmt, 0);
    5164          630 :   tree typearg = gimple_call_arg (stmt, 1);
    5165              :   /* The 2nd argument of __builtin_clear_padding's value is used to
    5166              :      distinguish whether this call is made by the user or by the compiler
    5167              :      for automatic variable initialization.  */
    5168          630 :   bool for_auto_init = (bool) TREE_INT_CST_LOW (typearg);
    5169          630 :   tree type = TREE_TYPE (TREE_TYPE (typearg));
    5170          630 :   location_t loc = gimple_location (stmt);
    5171          630 :   clear_padding_struct buf;
    5172          630 :   gimple_stmt_iterator gsiprev = *gsi;
    5173              :   /* This should be folded during the lower pass.  */
    5174         1260 :   gcc_assert (!gimple_in_ssa_p (cfun) && cfun->cfg == NULL);
    5175          630 :   gcc_assert (COMPLETE_TYPE_P (type));
    5176          630 :   gsi_prev (&gsiprev);
    5177              : 
    5178          630 :   buf.loc = loc;
    5179          630 :   buf.clear_in_mask = false;
    5180          630 :   buf.base = ptr;
    5181          630 :   buf.alias_type = NULL_TREE;
    5182          630 :   buf.gsi = gsi;
    5183          630 :   buf.align = get_pointer_alignment (ptr);
    5184          630 :   unsigned int talign = min_align_of_type (type) * BITS_PER_UNIT;
    5185          630 :   buf.align = MAX (buf.align, talign);
    5186          630 :   buf.off = 0;
    5187          630 :   buf.padding_bytes = 0;
    5188          630 :   buf.size = 0;
    5189          630 :   buf.sz = int_size_in_bytes (type);
    5190          630 :   buf.union_ptr = NULL;
    5191          630 :   if (buf.sz < 0 && int_size_in_bytes (strip_array_types (type)) < 0)
    5192            1 :     sorry_at (loc, "%s not supported for variable length aggregates",
    5193              :               "__builtin_clear_padding");
    5194              :   /* The implementation currently assumes 8-bit host and target
    5195              :      chars which is the case for all currently supported targets
    5196              :      and hosts and is required e.g. for native_{encode,interpret}* APIs.  */
    5197          629 :   else if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
    5198              :     sorry_at (loc, "%s not supported on this target",
    5199              :               "__builtin_clear_padding");
    5200          629 :   else if (!clear_padding_type_may_have_padding_p (type))
    5201              :     ;
    5202          592 :   else if (TREE_CODE (type) == ARRAY_TYPE && buf.sz < 0)
    5203              :     {
    5204           48 :       tree sz = TYPE_SIZE_UNIT (type);
    5205           48 :       tree elttype = type;
    5206              :       /* Only supports C/C++ VLAs and flattens all the VLA levels.  */
    5207           48 :       while (TREE_CODE (elttype) == ARRAY_TYPE
    5208          144 :              && int_size_in_bytes (elttype) < 0)
    5209           96 :         elttype = TREE_TYPE (elttype);
    5210           48 :       HOST_WIDE_INT eltsz = int_size_in_bytes (elttype);
    5211           48 :       gcc_assert (eltsz >= 0);
    5212           48 :       if (eltsz)
    5213              :         {
    5214           48 :           buf.base = create_tmp_var (build_pointer_type (elttype));
    5215           48 :           tree end = make_ssa_name (TREE_TYPE (buf.base));
    5216           48 :           gimple *g = gimple_build_assign (buf.base, ptr);
    5217           48 :           gimple_set_location (g, loc);
    5218           48 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5219           48 :           g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf.base, sz);
    5220           48 :           gimple_set_location (g, loc);
    5221           48 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5222           48 :           buf.sz = eltsz;
    5223           48 :           buf.align = TYPE_ALIGN (elttype);
    5224           48 :           buf.alias_type = build_pointer_type (elttype);
    5225           48 :           clear_padding_emit_loop (&buf, elttype, end, for_auto_init);
    5226              :         }
    5227              :     }
    5228              :   else
    5229              :     {
    5230          544 :       if (!is_gimple_mem_ref_addr (buf.base))
    5231              :         {
    5232           28 :           buf.base = make_ssa_name (TREE_TYPE (ptr));
    5233           28 :           gimple *g = gimple_build_assign (buf.base, ptr);
    5234           28 :           gimple_set_location (g, loc);
    5235           28 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5236              :         }
    5237          544 :       buf.alias_type = build_pointer_type (type);
    5238          544 :       clear_padding_type (&buf, type, buf.sz, for_auto_init);
    5239          544 :       clear_padding_flush (&buf, true);
    5240              :     }
    5241              : 
    5242          630 :   gimple_stmt_iterator gsiprev2 = *gsi;
    5243          630 :   gsi_prev (&gsiprev2);
    5244          630 :   if (gsi_stmt (gsiprev) == gsi_stmt (gsiprev2))
    5245          126 :     gsi_replace (gsi, gimple_build_nop (), true);
    5246              :   else
    5247              :     {
    5248          504 :       gsi_remove (gsi, true);
    5249          504 :       *gsi = gsiprev2;
    5250              :     }
    5251          630 :   return true;
    5252              : }
    5253              : 
    5254              : /* Fold __builtin_constant_p builtin.  */
    5255              : 
    5256              : static bool
    5257        95091 : gimple_fold_builtin_constant_p (gimple_stmt_iterator *gsi)
    5258              : {
    5259        95091 :   gcall *call = as_a<gcall*>(gsi_stmt (*gsi));
    5260              : 
    5261        95091 :   if (gimple_call_num_args (call) != 1)
    5262              :     return false;
    5263              : 
    5264        95086 :   tree arg = gimple_call_arg (call, 0);
    5265        95086 :   tree result = fold_builtin_constant_p (arg);
    5266              : 
    5267              :   /* Resolve __builtin_constant_p.  If it hasn't been
    5268              :      folded to integer_one_node by now, it's fairly
    5269              :      certain that the value simply isn't constant.  */
    5270       189102 :   if (!result && fold_before_rtl_expansion_p ())
    5271            3 :     result = integer_zero_node;
    5272              : 
    5273        95086 :   if (!result)
    5274              :     return false;
    5275              : 
    5276         1073 :   gimplify_and_update_call_from_tree (gsi, result);
    5277         1073 :   return true;
    5278              : }
    5279              : 
    5280              : /* If va_list type is a simple pointer and nothing special is needed,
    5281              :    optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
    5282              :    __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
    5283              :    pointer assignment.  Returns true if a change happened.  */
    5284              : 
    5285              : static bool
    5286       113605 : gimple_fold_builtin_stdarg (gimple_stmt_iterator *gsi, gcall *call)
    5287              : {
    5288              :   /* These shouldn't be folded before pass_stdarg.  */
    5289       113605 :   if (!fold_before_rtl_expansion_p ())
    5290              :     return false;
    5291              : 
    5292        10714 :   tree callee, lhs, rhs, cfun_va_list;
    5293        10714 :   bool va_list_simple_ptr;
    5294        10714 :   location_t loc = gimple_location (call);
    5295        10714 :   gimple *nstmt0, *nstmt;
    5296        10714 :   tree tlhs, oldvdef, newvdef;
    5297              : 
    5298        10714 :   callee = gimple_call_fndecl (call);
    5299              : 
    5300        10714 :   cfun_va_list = targetm.fn_abi_va_list (callee);
    5301        21428 :   va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
    5302        10714 :                        && (TREE_TYPE (cfun_va_list) == void_type_node
    5303          436 :                            || TREE_TYPE (cfun_va_list) == char_type_node);
    5304              : 
    5305        10714 :   switch (DECL_FUNCTION_CODE (callee))
    5306              :     {
    5307         6949 :     case BUILT_IN_VA_START:
    5308         6949 :       if (!va_list_simple_ptr
    5309          172 :           || targetm.expand_builtin_va_start != NULL
    5310         7105 :           || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
    5311              :         return false;
    5312              : 
    5313          156 :       if (gimple_call_num_args (call) != 2)
    5314              :         return false;
    5315              : 
    5316          156 :       lhs = gimple_call_arg (call, 0);
    5317          156 :       if (!POINTER_TYPE_P (TREE_TYPE (lhs))
    5318          156 :           || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
    5319          156 :              != TYPE_MAIN_VARIANT (cfun_va_list))
    5320              :         return false;
    5321              :       /* Create `tlhs = __builtin_next_arg(0);`. */
    5322          156 :       tlhs = make_ssa_name (cfun_va_list);
    5323          156 :       nstmt0 = gimple_build_call (builtin_decl_explicit (BUILT_IN_NEXT_ARG), 1, integer_zero_node);
    5324          156 :       lhs = fold_build2 (MEM_REF, cfun_va_list, lhs, build_zero_cst (TREE_TYPE (lhs)));
    5325          156 :       gimple_call_set_lhs (nstmt0, tlhs);
    5326          156 :       gimple_set_location (nstmt0, loc);
    5327          156 :       gimple_move_vops (nstmt0, call);
    5328          156 :       gsi_replace (gsi, nstmt0, false);
    5329          156 :       oldvdef = gimple_vdef (nstmt0);
    5330          156 :       newvdef = make_ssa_name (gimple_vop (cfun), nstmt0);
    5331          156 :       gimple_set_vdef (nstmt0, newvdef);
    5332              : 
    5333              :       /* Create `*lhs = tlhs;`.  */
    5334          156 :       nstmt = gimple_build_assign (lhs, tlhs);
    5335          156 :       gimple_set_location (nstmt, loc);
    5336          156 :       gimple_set_vuse (nstmt, newvdef);
    5337          156 :       gimple_set_vdef (nstmt, oldvdef);
    5338          156 :       SSA_NAME_DEF_STMT (oldvdef) = nstmt;
    5339          156 :       gsi_insert_after (gsi, nstmt, GSI_NEW_STMT);
    5340              : 
    5341          156 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5342              :         {
    5343            0 :           fprintf (dump_file, "Simplified\n  ");
    5344            0 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    5345            0 :           fprintf (dump_file, "into\n  ");
    5346            0 :           print_gimple_stmt (dump_file, nstmt0, 0, dump_flags);
    5347            0 :           fprintf (dump_file, "  ");
    5348            0 :           print_gimple_stmt (dump_file, nstmt, 0, dump_flags);
    5349              :         }
    5350              :       return true;
    5351              : 
    5352          244 :     case BUILT_IN_VA_COPY:
    5353          244 :       if (!va_list_simple_ptr)
    5354              :         return false;
    5355              : 
    5356           47 :       if (gimple_call_num_args (call) != 2)
    5357              :         return false;
    5358              : 
    5359           47 :       lhs = gimple_call_arg (call, 0);
    5360           47 :       if (!POINTER_TYPE_P (TREE_TYPE (lhs))
    5361           47 :           || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
    5362           47 :              != TYPE_MAIN_VARIANT (cfun_va_list))
    5363              :         return false;
    5364           47 :       rhs = gimple_call_arg (call, 1);
    5365           47 :       if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
    5366           47 :           != TYPE_MAIN_VARIANT (cfun_va_list))
    5367              :         return false;
    5368              : 
    5369           47 :       lhs = fold_build2 (MEM_REF, cfun_va_list, lhs, build_zero_cst (TREE_TYPE (lhs)));
    5370           47 :       nstmt = gimple_build_assign (lhs, rhs);
    5371           47 :       gimple_set_location (nstmt, loc);
    5372           47 :       gimple_move_vops (nstmt, call);
    5373           47 :       gsi_replace (gsi, nstmt, false);
    5374              : 
    5375           47 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5376              :         {
    5377            0 :           fprintf (dump_file, "Simplified\n  ");
    5378            0 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    5379            0 :           fprintf (dump_file, "into\n  ");
    5380            0 :           print_gimple_stmt (dump_file, nstmt, 0, dump_flags);
    5381              :         }
    5382              :       return true;
    5383              : 
    5384         3521 :     case BUILT_IN_VA_END:
    5385              :       /* No effect, so the statement will be deleted.  */
    5386         3521 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5387              :         {
    5388            0 :           fprintf (dump_file, "Removed\n  ");
    5389            0 :           print_gimple_stmt (dump_file, call, 0, dump_flags);
    5390              :         }
    5391         3521 :       unlink_stmt_vdef (call);
    5392         3521 :       release_defs (call);
    5393         3521 :       gsi_replace (gsi, gimple_build_nop (), false);
    5394         3521 :       return true;
    5395              : 
    5396            0 :     default:
    5397            0 :       gcc_unreachable ();
    5398              :     }
    5399              : }
    5400              : 
    5401              : /* Fold the non-target builtin at *GSI and return whether any simplification
    5402              :    was made.  */
    5403              : 
    5404              : static bool
    5405      9382778 : gimple_fold_builtin (gimple_stmt_iterator *gsi)
    5406              : {
    5407      9382778 :   gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
    5408      9382778 :   tree callee = gimple_call_fndecl (stmt);
    5409              : 
    5410              :   /* Give up for always_inline inline builtins until they are
    5411              :      inlined.  */
    5412      9382778 :   if (avoid_folding_inline_builtin (callee))
    5413              :     return false;
    5414              : 
    5415      9381602 :   unsigned n = gimple_call_num_args (stmt);
    5416      9381602 :   enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
    5417      9381602 :   switch (fcode)
    5418              :     {
    5419       113605 :     case BUILT_IN_VA_START:
    5420       113605 :     case BUILT_IN_VA_END:
    5421       113605 :     case BUILT_IN_VA_COPY:
    5422       113605 :       return gimple_fold_builtin_stdarg (gsi, stmt);
    5423          148 :     case BUILT_IN_BCMP:
    5424          148 :       return gimple_fold_builtin_bcmp (gsi);
    5425          367 :     case BUILT_IN_BCOPY:
    5426          367 :       return gimple_fold_builtin_bcopy (gsi);
    5427          250 :     case BUILT_IN_BZERO:
    5428          250 :       return gimple_fold_builtin_bzero (gsi);
    5429              : 
    5430       306420 :     case BUILT_IN_MEMSET:
    5431       306420 :       return gimple_fold_builtin_memset (gsi,
    5432              :                                          gimple_call_arg (stmt, 1),
    5433       306420 :                                          gimple_call_arg (stmt, 2));
    5434        10276 :     case BUILT_IN_MEMPCPY:
    5435        10276 :       if (gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
    5436              :                                             gimple_call_arg (stmt, 1), fcode))
    5437              :         return true;
    5438         9536 :       return gimple_fold_builtin_mempcpy (gsi);
    5439      1018034 :     case BUILT_IN_MEMCPY:
    5440      1018034 :     case BUILT_IN_MEMMOVE:
    5441      1018034 :       return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
    5442      1018034 :                                             gimple_call_arg (stmt, 1), fcode);
    5443         4459 :     case BUILT_IN_SPRINTF_CHK:
    5444         4459 :     case BUILT_IN_VSPRINTF_CHK:
    5445         4459 :       return gimple_fold_builtin_sprintf_chk (gsi, fcode);
    5446         1714 :     case BUILT_IN_STRCAT_CHK:
    5447         1714 :       return gimple_fold_builtin_strcat_chk (gsi);
    5448         1143 :     case BUILT_IN_STRNCAT_CHK:
    5449         1143 :       return gimple_fold_builtin_strncat_chk (gsi);
    5450       142361 :     case BUILT_IN_STRLEN:
    5451       142361 :       return gimple_fold_builtin_strlen (gsi);
    5452        26000 :     case BUILT_IN_STRCPY:
    5453        26000 :       return gimple_fold_builtin_strcpy (gsi,
    5454              :                                          gimple_call_arg (stmt, 0),
    5455        26000 :                                          gimple_call_arg (stmt, 1));
    5456        17266 :     case BUILT_IN_STRNCPY:
    5457        17266 :       return gimple_fold_builtin_strncpy (gsi,
    5458              :                                           gimple_call_arg (stmt, 0),
    5459              :                                           gimple_call_arg (stmt, 1),
    5460        17266 :                                           gimple_call_arg (stmt, 2));
    5461         7328 :     case BUILT_IN_STRCAT:
    5462         7328 :       return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
    5463         7328 :                                          gimple_call_arg (stmt, 1));
    5464         6786 :     case BUILT_IN_STRNCAT:
    5465         6786 :       return gimple_fold_builtin_strncat (gsi);
    5466         4811 :     case BUILT_IN_INDEX:
    5467         4811 :     case BUILT_IN_STRCHR:
    5468         4811 :       return gimple_fold_builtin_strchr (gsi, false);
    5469          725 :     case BUILT_IN_RINDEX:
    5470          725 :     case BUILT_IN_STRRCHR:
    5471          725 :       return gimple_fold_builtin_strchr (gsi, true);
    5472         4211 :     case BUILT_IN_STRSTR:
    5473         4211 :       return gimple_fold_builtin_strstr (gsi);
    5474      1250415 :     case BUILT_IN_STRCMP:
    5475      1250415 :     case BUILT_IN_STRCMP_EQ:
    5476      1250415 :     case BUILT_IN_STRCASECMP:
    5477      1250415 :     case BUILT_IN_STRNCMP:
    5478      1250415 :     case BUILT_IN_STRNCMP_EQ:
    5479      1250415 :     case BUILT_IN_STRNCASECMP:
    5480      1250415 :       return gimple_fold_builtin_string_compare (gsi);
    5481        35505 :     case BUILT_IN_MEMCHR:
    5482        35505 :       return gimple_fold_builtin_memchr (gsi);
    5483        20643 :     case BUILT_IN_FPUTS:
    5484        20643 :       return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
    5485        20643 :                                         gimple_call_arg (stmt, 1), false);
    5486           43 :     case BUILT_IN_FPUTS_UNLOCKED:
    5487           43 :       return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
    5488           43 :                                         gimple_call_arg (stmt, 1), true);
    5489        25617 :     case BUILT_IN_MEMCPY_CHK:
    5490        25617 :     case BUILT_IN_MEMPCPY_CHK:
    5491        25617 :     case BUILT_IN_MEMMOVE_CHK:
    5492        25617 :     case BUILT_IN_MEMSET_CHK:
    5493        25617 :       return gimple_fold_builtin_memory_chk (gsi,
    5494              :                                              gimple_call_arg (stmt, 0),
    5495              :                                              gimple_call_arg (stmt, 1),
    5496              :                                              gimple_call_arg (stmt, 2),
    5497              :                                              gimple_call_arg (stmt, 3),
    5498        25617 :                                              fcode);
    5499         3674 :     case BUILT_IN_STPCPY:
    5500         3674 :       return gimple_fold_builtin_stpcpy (gsi);
    5501         2592 :     case BUILT_IN_STRCPY_CHK:
    5502         2592 :     case BUILT_IN_STPCPY_CHK:
    5503         2592 :       return gimple_fold_builtin_stxcpy_chk (gsi,
    5504              :                                              gimple_call_arg (stmt, 0),
    5505              :                                              gimple_call_arg (stmt, 1),
    5506              :                                              gimple_call_arg (stmt, 2),
    5507         2592 :                                              fcode);
    5508         2721 :     case BUILT_IN_STRNCPY_CHK:
    5509         2721 :     case BUILT_IN_STPNCPY_CHK:
    5510         2721 :       return gimple_fold_builtin_stxncpy_chk (gsi,
    5511              :                                               gimple_call_arg (stmt, 0),
    5512              :                                               gimple_call_arg (stmt, 1),
    5513              :                                               gimple_call_arg (stmt, 2),
    5514              :                                               gimple_call_arg (stmt, 3),
    5515         2721 :                                               fcode);
    5516         2359 :     case BUILT_IN_SNPRINTF_CHK:
    5517         2359 :     case BUILT_IN_VSNPRINTF_CHK:
    5518         2359 :       return gimple_fold_builtin_snprintf_chk (gsi, fcode);
    5519              : 
    5520       794675 :     case BUILT_IN_FPRINTF:
    5521       794675 :     case BUILT_IN_FPRINTF_UNLOCKED:
    5522       794675 :     case BUILT_IN_VFPRINTF:
    5523       794675 :       if (n == 2 || n == 3)
    5524        94809 :         return gimple_fold_builtin_fprintf (gsi,
    5525              :                                             gimple_call_arg (stmt, 0),
    5526              :                                             gimple_call_arg (stmt, 1),
    5527              :                                             n == 3
    5528        42445 :                                             ? gimple_call_arg (stmt, 2)
    5529              :                                             : NULL_TREE,
    5530        52364 :                                             fcode);
    5531              :       break;
    5532         2226 :     case BUILT_IN_FPRINTF_CHK:
    5533         2226 :     case BUILT_IN_VFPRINTF_CHK:
    5534         2226 :       if (n == 3 || n == 4)
    5535         3811 :         return gimple_fold_builtin_fprintf (gsi,
    5536              :                                             gimple_call_arg (stmt, 0),
    5537              :                                             gimple_call_arg (stmt, 2),
    5538              :                                             n == 4
    5539         1744 :                                             ? gimple_call_arg (stmt, 3)
    5540              :                                             : NULL_TREE,
    5541         2067 :                                             fcode);
    5542              :       break;
    5543       199036 :     case BUILT_IN_PRINTF:
    5544       199036 :     case BUILT_IN_PRINTF_UNLOCKED:
    5545       199036 :     case BUILT_IN_VPRINTF:
    5546       199036 :       if (n == 1 || n == 2)
    5547       233381 :         return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
    5548              :                                            n == 2
    5549       112145 :                                            ? gimple_call_arg (stmt, 1)
    5550       121236 :                                            : NULL_TREE, fcode);
    5551              :       break;
    5552         2273 :     case BUILT_IN_PRINTF_CHK:
    5553         2273 :     case BUILT_IN_VPRINTF_CHK:
    5554         2273 :       if (n == 2 || n == 3)
    5555         3893 :         return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
    5556              :                                            n == 3
    5557         1771 :                                            ? gimple_call_arg (stmt, 2)
    5558         2122 :                                            : NULL_TREE, fcode);
    5559              :       break;
    5560         2866 :     case BUILT_IN_ACC_ON_DEVICE:
    5561         2866 :       return gimple_fold_builtin_acc_on_device (gsi,
    5562         2866 :                                                 gimple_call_arg (stmt, 0));
    5563          234 :     case BUILT_IN_OMP_IS_INITIAL_DEVICE:
    5564          234 :       return gimple_fold_builtin_omp_is_initial_device (gsi);
    5565              : 
    5566          103 :     case BUILT_IN_OMP_GET_INITIAL_DEVICE:
    5567          103 :       return gimple_fold_builtin_omp_get_initial_device (gsi);
    5568              : 
    5569          294 :     case BUILT_IN_OMP_GET_NUM_DEVICES:
    5570          294 :       return gimple_fold_builtin_omp_get_num_devices (gsi);
    5571              : 
    5572        48789 :     case BUILT_IN_REALLOC:
    5573        48789 :       return gimple_fold_builtin_realloc (gsi);
    5574              : 
    5575          630 :     case BUILT_IN_CLEAR_PADDING:
    5576          630 :       return gimple_fold_builtin_clear_padding (gsi);
    5577              : 
    5578        95091 :     case BUILT_IN_CONSTANT_P:
    5579        95091 :       return gimple_fold_builtin_constant_p (gsi);
    5580              : 
    5581      6046333 :     default:;
    5582              :     }
    5583              : 
    5584              :   /* Try the generic builtin folder.  */
    5585      6046333 :   bool ignore = (gimple_call_lhs (stmt) == NULL);
    5586      6046333 :   tree result = fold_call_stmt (stmt, ignore);
    5587      6046333 :   if (result)
    5588              :     {
    5589         4895 :       if (ignore)
    5590         1226 :         STRIP_NOPS (result);
    5591              :       else
    5592         3669 :         result = fold_convert (gimple_call_return_type (stmt), result);
    5593         4895 :       gimplify_and_update_call_from_tree (gsi, result);
    5594         4895 :       return true;
    5595              :     }
    5596              : 
    5597              :   return false;
    5598              : }
    5599              : 
    5600              : /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
    5601              :    function calls to constants, where possible.  */
    5602              : 
    5603              : static tree
    5604        20589 : fold_internal_goacc_dim (const gimple *call)
    5605              : {
    5606        20589 :   int axis = oacc_get_ifn_dim_arg (call);
    5607        20589 :   int size = oacc_get_fn_dim_size (current_function_decl, axis);
    5608        20589 :   tree result = NULL_TREE;
    5609        20589 :   tree type = TREE_TYPE (gimple_call_lhs (call));
    5610              : 
    5611        20589 :   switch (gimple_call_internal_fn (call))
    5612              :     {
    5613         8915 :     case IFN_GOACC_DIM_POS:
    5614              :       /* If the size is 1, we know the answer.  */
    5615         8915 :       if (size == 1)
    5616         8915 :         result = build_int_cst (type, 0);
    5617              :       break;
    5618        11674 :     case IFN_GOACC_DIM_SIZE:
    5619              :       /* If the size is not dynamic, we know the answer.  */
    5620        11674 :       if (size)
    5621        11674 :         result = build_int_cst (type, size);
    5622              :       break;
    5623              :     default:
    5624              :       break;
    5625              :     }
    5626              : 
    5627        20589 :   return result;
    5628              : }
    5629              : 
    5630              : /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
    5631              :    for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
    5632              :    &var where var is only addressable because of such calls.  */
    5633              : 
    5634              : bool
    5635     58941610 : optimize_atomic_compare_exchange_p (gimple *stmt)
    5636              : {
    5637     58941610 :   if (gimple_call_num_args (stmt) != 6
    5638      1583927 :       || !flag_inline_atomics
    5639      1583927 :       || !optimize
    5640      1583927 :       || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS)
    5641      1583846 :       || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
    5642      1044512 :       || !gimple_vdef (stmt)
    5643     59873166 :       || !gimple_vuse (stmt))
    5644     58010054 :     return false;
    5645              : 
    5646       931556 :   tree fndecl = gimple_call_fndecl (stmt);
    5647       931556 :   switch (DECL_FUNCTION_CODE (fndecl))
    5648              :     {
    5649        51771 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
    5650        51771 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
    5651        51771 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
    5652        51771 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
    5653        51771 :     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
    5654        51771 :       break;
    5655              :     default:
    5656              :       return false;
    5657              :     }
    5658              : 
    5659        51771 :   tree expected = gimple_call_arg (stmt, 1);
    5660        51771 :   if (TREE_CODE (expected) != ADDR_EXPR
    5661        51771 :       || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
    5662              :     return false;
    5663              : 
    5664        49430 :   tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
    5665        49430 :   if (!is_gimple_reg_type (etype)
    5666        49003 :       || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
    5667        46598 :       || TREE_THIS_VOLATILE (etype)
    5668        46598 :       || VECTOR_TYPE_P (etype)
    5669              :       || TREE_CODE (etype) == COMPLEX_TYPE
    5670              :       /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
    5671              :          might not preserve all the bits.  See PR71716.  */
    5672              :       || SCALAR_FLOAT_TYPE_P (etype)
    5673        67404 :       || maybe_ne (TYPE_PRECISION (etype),
    5674        35948 :                    GET_MODE_BITSIZE (TYPE_MODE (etype))))
    5675        37864 :     return false;
    5676              : 
    5677        11566 :   tree weak = gimple_call_arg (stmt, 3);
    5678        11566 :   if (!integer_zerop (weak) && !integer_onep (weak))
    5679              :     return false;
    5680              : 
    5681        11566 :   tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    5682        11566 :   tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
    5683        11566 :   machine_mode mode = TYPE_MODE (itype);
    5684              : 
    5685        11566 :   if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
    5686              :       == CODE_FOR_nothing
    5687        11566 :       && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
    5688              :     return false;
    5689              : 
    5690        23132 :   if (maybe_ne (int_size_in_bytes (etype), GET_MODE_SIZE (mode)))
    5691              :     return false;
    5692              : 
    5693              :   return true;
    5694              : }
    5695              : 
    5696              : /* Fold
    5697              :      r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
    5698              :    into
    5699              :      _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
    5700              :      i = IMAGPART_EXPR <t>;
    5701              :      r = (_Bool) i;
    5702              :      e = REALPART_EXPR <t>;  */
    5703              : 
    5704              : void
    5705         5708 : fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
    5706              : {
    5707         5708 :   gimple *stmt = gsi_stmt (*gsi);
    5708         5708 :   tree fndecl = gimple_call_fndecl (stmt);
    5709         5708 :   tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    5710         5708 :   tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
    5711         5708 :   tree ctype = build_complex_type (itype);
    5712         5708 :   tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
    5713         5708 :   bool throws = false;
    5714         5708 :   edge e = NULL;
    5715         5708 :   gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
    5716              :                                    expected);
    5717         5708 :   gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5718         5708 :   gimple_stmt_iterator gsiret = gsi_for_stmt (g);
    5719         5708 :   if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
    5720              :     {
    5721         2628 :       g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
    5722              :                                build1 (VIEW_CONVERT_EXPR, itype,
    5723              :                                        gimple_assign_lhs (g)));
    5724         2628 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    5725              :     }
    5726         5708 :   int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
    5727        11051 :              + int_size_in_bytes (itype);
    5728         5708 :   g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
    5729              :                                   gimple_call_arg (stmt, 0),
    5730              :                                   gimple_assign_lhs (g),
    5731              :                                   gimple_call_arg (stmt, 2),
    5732         5708 :                                   build_int_cst (integer_type_node, flag),
    5733              :                                   gimple_call_arg (stmt, 4),
    5734              :                                   gimple_call_arg (stmt, 5));
    5735         5708 :   tree lhs = make_ssa_name (ctype);
    5736         5708 :   gimple_call_set_lhs (g, lhs);
    5737         5708 :   gimple_move_vops (g, stmt);
    5738         5708 :   tree oldlhs = gimple_call_lhs (stmt);
    5739         5708 :   if (stmt_can_throw_internal (cfun, stmt))
    5740              :     {
    5741          203 :       throws = true;
    5742          203 :       e = find_fallthru_edge (gsi_bb (*gsi)->succs);
    5743              :     }
    5744         5708 :   gimple_call_set_nothrow (as_a <gcall *> (g),
    5745         5708 :                            gimple_call_nothrow_p (as_a <gcall *> (stmt)));
    5746         5708 :   gimple_call_set_lhs (stmt, NULL_TREE);
    5747         5708 :   gsi_replace (gsi, g, true);
    5748         5708 :   if (oldlhs)
    5749              :     {
    5750         5661 :       g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
    5751              :                                build1 (IMAGPART_EXPR, itype, lhs));
    5752         5661 :       if (throws)
    5753              :         {
    5754          197 :           gsi_insert_on_edge_immediate (e, g);
    5755          197 :           *gsi = gsi_for_stmt (g);
    5756              :         }
    5757              :       else
    5758         5464 :         gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5759         5661 :       g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
    5760         5661 :       gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5761              :     }
    5762         5708 :   g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
    5763              :                            build1 (REALPART_EXPR, itype, lhs));
    5764         5708 :   if (throws && oldlhs == NULL_TREE)
    5765              :     {
    5766            6 :       gsi_insert_on_edge_immediate (e, g);
    5767            6 :       *gsi = gsi_for_stmt (g);
    5768              :     }
    5769              :   else
    5770         5702 :     gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5771         5708 :   if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
    5772              :     {
    5773         5256 :       g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
    5774              :                                VIEW_CONVERT_EXPR,
    5775         2628 :                                build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
    5776              :                                        gimple_assign_lhs (g)));
    5777         2628 :       gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5778              :     }
    5779         5708 :   g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
    5780         5708 :   gsi_insert_after (gsi, g, GSI_NEW_STMT);
    5781         5708 :   *gsi = gsiret;
    5782         5708 : }
    5783              : 
    5784              : /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
    5785              :    doesn't fit into TYPE.  The test for overflow should be regardless of
    5786              :    -fwrapv, and even for unsigned types.  */
    5787              : 
    5788              : bool
    5789       408014 : arith_overflowed_p (enum tree_code code, const_tree type,
    5790              :                     const_tree arg0, const_tree arg1)
    5791              : {
    5792       408014 :   widest2_int warg0 = widest2_int_cst (arg0);
    5793       408014 :   widest2_int warg1 = widest2_int_cst (arg1);
    5794       408014 :   widest2_int wres;
    5795       408014 :   switch (code)
    5796              :     {
    5797        96102 :     case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
    5798       115516 :     case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
    5799       197791 :     case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
    5800            0 :     default: gcc_unreachable ();
    5801              :     }
    5802       408014 :   signop sign = TYPE_SIGN (type);
    5803       408014 :   if (sign == UNSIGNED && wi::neg_p (wres))
    5804              :     return true;
    5805       336129 :   return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
    5806       408154 : }
    5807              : 
    5808              : /* Mask state for partial load/store operations (mask and length).  */
    5809              : enum mask_load_store_state {
    5810              :   MASK_ALL_INACTIVE,  /* All lanes/elements are inactive (can be elided).  */
    5811              :   MASK_ALL_ACTIVE,    /* All lanes/elements are active (unconditional).  */
    5812              :   MASK_UNKNOWN
    5813              : };
    5814              : 
    5815              : /* Check the mask/length state of IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL.
    5816              :    Returns whether all elements are active, all inactive, or mixed.
    5817              :    VECTYPE is the vector type of the operation.  */
    5818              : 
    5819              : static enum mask_load_store_state
    5820         8093 : partial_load_store_mask_state (gcall *call, tree vectype)
    5821              : {
    5822         8093 :   internal_fn ifn = gimple_call_internal_fn (call);
    5823         8093 :   int mask_index = internal_fn_mask_index (ifn);
    5824         8093 :   int len_index = internal_fn_len_index (ifn);
    5825              : 
    5826              :   /* Extract length and mask arguments up front.  */
    5827         8093 :   tree len = len_index != -1 ? gimple_call_arg (call, len_index) : NULL_TREE;
    5828            0 :   tree bias = len ? gimple_call_arg (call, len_index + 1) : NULL_TREE;
    5829         8093 :   tree mask = mask_index != -1 ? gimple_call_arg (call, mask_index) : NULL_TREE;
    5830              : 
    5831        16186 :   poly_int64 nelts = GET_MODE_NUNITS (TYPE_MODE (vectype));
    5832              : 
    5833         8093 :   poly_widest_int wlen = -1;
    5834         8093 :   bool full_length_p = !len;  /* No length means full length.  */
    5835              : 
    5836              :   /* Compute effective length.  */
    5837         8093 :   if (len && poly_int_tree_p (len))
    5838              :     {
    5839            0 :       gcc_assert (TREE_CODE (bias) == INTEGER_CST);
    5840            0 :       wlen = wi::to_poly_widest (len) + wi::to_widest (bias);
    5841              : 
    5842            0 :       if (known_eq (wlen, 0))
    5843              :         return MASK_ALL_INACTIVE;
    5844              : 
    5845            0 :       if (known_eq (wlen, nelts))
    5846              :         full_length_p = true;
    5847              :       else
    5848              :         full_length_p = false;
    5849              :     }
    5850              : 
    5851              :   /* Check mask for early return cases.  */
    5852         8093 :   if (mask)
    5853              :     {
    5854         8093 :       if (integer_zerop (mask))
    5855              :         return MASK_ALL_INACTIVE;
    5856              : 
    5857         8078 :       if (full_length_p && integer_all_onesp (mask))
    5858              :         return MASK_ALL_ACTIVE;
    5859              :     }
    5860            0 :   else if (full_length_p)
    5861              :     /* No mask and full length means all active.  */
    5862              :     return MASK_ALL_ACTIVE;
    5863              : 
    5864              :   /* For VLA vectors, we can't do much more.  */
    5865         7936 :   if (!nelts.is_constant ())
    5866              :     return MASK_UNKNOWN;
    5867              : 
    5868              :   /* Same for VLS vectors with non-constant mask.  */
    5869         7936 :   if (mask && TREE_CODE (mask) != VECTOR_CST)
    5870              :     return MASK_UNKNOWN;
    5871              : 
    5872              :   /* Check VLS vector elements.  */
    5873          480 :   gcc_assert (wlen.is_constant ());
    5874              : 
    5875          480 :   HOST_WIDE_INT active_len = wlen.to_constant ().to_shwi ();
    5876          480 :   if (active_len == -1)
    5877          480 :     active_len = nelts.to_constant ();
    5878              : 
    5879              :   /* Check if all elements in the active range match the mask.  */
    5880          996 :   for (HOST_WIDE_INT i = 0; i < active_len; i++)
    5881              :     {
    5882          996 :       bool elt_active = !mask || !integer_zerop (vector_cst_elt (mask, i));
    5883          516 :       if (!elt_active)
    5884              :         {
    5885              :           /* Found an inactive element.  Check if all are inactive.  */
    5886          972 :           for (HOST_WIDE_INT j = 0; j < active_len; j++)
    5887          972 :             if (!mask || !integer_zerop (vector_cst_elt (mask, j)))
    5888              :               return MASK_UNKNOWN;  /* Mixed state.  */
    5889              :           return MASK_ALL_INACTIVE;
    5890              :         }
    5891              :     }
    5892              : 
    5893              :   /* All elements in active range are active.  */
    5894            0 :   return full_length_p ? MASK_ALL_ACTIVE : MASK_UNKNOWN;
    5895         8093 : }
    5896              : 
    5897              : 
    5898              : /* If IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL is unconditional
    5899              :    (all lanes active), return a MEM_REF for the memory it references.
    5900              :    Otherwise return NULL_TREE.  VECTYPE is the type of the memory vector.  */
    5901              : 
    5902              : static tree
    5903         4039 : gimple_fold_partial_load_store_mem_ref (gcall *call, tree vectype)
    5904              : {
    5905              :   /* Only fold if all lanes are active (unconditional).  */
    5906         4039 :   if (partial_load_store_mask_state (call, vectype) != MASK_ALL_ACTIVE)
    5907              :     return NULL_TREE;
    5908              : 
    5909           71 :   tree ptr = gimple_call_arg (call, 0);
    5910           71 :   tree alias_align = gimple_call_arg (call, 1);
    5911           71 :   if (!tree_fits_uhwi_p (alias_align))
    5912              :     return NULL_TREE;
    5913              : 
    5914           71 :   unsigned HOST_WIDE_INT align = tree_to_uhwi (alias_align);
    5915           71 :   if (TYPE_ALIGN (vectype) != align)
    5916           14 :     vectype = build_aligned_type (vectype, align);
    5917           71 :   tree offset = build_zero_cst (TREE_TYPE (alias_align));
    5918           71 :   return fold_build2 (MEM_REF, vectype, ptr, offset);
    5919              : }
    5920              : 
    5921              : /* Try to fold IFN_{MASK,LEN}_LOAD/STORE call CALL.  Return true on success.  */
    5922              : 
    5923              : static bool
    5924         4054 : gimple_fold_partial_load_store (gimple_stmt_iterator *gsi, gcall *call)
    5925              : {
    5926         4054 :   internal_fn ifn = gimple_call_internal_fn (call);
    5927         4054 :   tree lhs = gimple_call_lhs (call);
    5928         4054 :   bool is_load = (lhs != NULL_TREE);
    5929         4054 :   tree vectype;
    5930              : 
    5931         4054 :   if (is_load)
    5932         1979 :     vectype = TREE_TYPE (lhs);
    5933              :   else
    5934              :     {
    5935         2075 :       tree rhs = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
    5936         2075 :       vectype = TREE_TYPE (rhs);
    5937              :     }
    5938              : 
    5939         4054 :   enum mask_load_store_state state
    5940         4054 :     = partial_load_store_mask_state (call, vectype);
    5941              : 
    5942              :   /* Handle all-inactive case.  */
    5943         4054 :   if (state == MASK_ALL_INACTIVE)
    5944              :     {
    5945           15 :       if (is_load)
    5946              :         {
    5947              :           /* Replace load with else value.  */
    5948           15 :           int else_index = internal_fn_else_index (ifn);
    5949           15 :           tree else_value = gimple_call_arg (call, else_index);
    5950           15 :           if (!is_gimple_reg (lhs))
    5951              :             {
    5952            0 :               if (!zerop (else_value))
    5953              :                 return false;
    5954            0 :               else_value = build_constructor (TREE_TYPE (lhs), NULL);
    5955              :             }
    5956           15 :           gassign *new_stmt = gimple_build_assign (lhs, else_value);
    5957           15 :           gimple_set_location (new_stmt, gimple_location (call));
    5958              :           /* When the lhs is an array for LANES version, then there is still
    5959              :              a store, move the vops from the old stmt to the new one.  */
    5960           15 :           if (!is_gimple_reg (lhs))
    5961            0 :             gimple_move_vops (new_stmt, call);
    5962           15 :           gsi_replace (gsi, new_stmt, false);
    5963           15 :           return true;
    5964              :         }
    5965              :       else
    5966              :         {
    5967              :           /* Remove inactive store altogether.  */
    5968            0 :           unlink_stmt_vdef (call);
    5969            0 :           release_defs (call);
    5970            0 :           gsi_replace (gsi, gimple_build_nop (), true);
    5971            0 :           return true;
    5972              :         }
    5973              :     }
    5974              : 
    5975              :   /* We cannot simplify a gather/scatter or load/store lanes further.  */
    5976         4039 :   if (internal_gather_scatter_fn_p (ifn)
    5977         4039 :       || TREE_CODE (vectype) == ARRAY_TYPE)
    5978              :     return false;
    5979              : 
    5980              :   /* Handle all-active case by folding to regular memory operation.  */
    5981         4039 :   if (tree mem_ref = gimple_fold_partial_load_store_mem_ref (call, vectype))
    5982              :     {
    5983           71 :       gassign *new_stmt;
    5984           71 :       if (is_load)
    5985           17 :         new_stmt = gimple_build_assign (lhs, mem_ref);
    5986              :       else
    5987              :         {
    5988           54 :           tree rhs
    5989           54 :             = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
    5990           54 :           new_stmt = gimple_build_assign (mem_ref, rhs);
    5991              :         }
    5992              : 
    5993           71 :       gimple_set_location (new_stmt, gimple_location (call));
    5994           71 :       gimple_move_vops (new_stmt, call);
    5995           71 :       gsi_replace (gsi, new_stmt, false);
    5996           71 :       return true;
    5997              :     }
    5998              :   return false;
    5999              : }
    6000              : 
    6001              : /* Attempt to fold a call statement referenced by the statement iterator GSI.
    6002              :    The statement may be replaced by another statement, e.g., if the call
    6003              :    simplifies to a constant value. Return true if any changes were made.
    6004              :    It is assumed that the operands have been previously folded.  */
    6005              : 
    6006              : static bool
    6007     55964972 : gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
    6008              : {
    6009     55964972 :   gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
    6010     55964972 :   tree callee;
    6011     55964972 :   bool changed = false;
    6012              : 
    6013              :   /* Check for virtual calls that became direct calls.  */
    6014     55964972 :   callee = gimple_call_fn (stmt);
    6015     55964972 :   if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
    6016              :     {
    6017       446876 :       if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
    6018              :         {
    6019            6 :           if (dump_file && virtual_method_call_p (callee)
    6020          376 :               && !possible_polymorphic_call_target_p
    6021            6 :                     (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
    6022            6 :                                                      (OBJ_TYPE_REF_EXPR (callee)))))
    6023              :             {
    6024            0 :               fprintf (dump_file,
    6025              :                        "Type inheritance inconsistent devirtualization of ");
    6026            0 :               print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    6027            0 :               fprintf (dump_file, " to ");
    6028            0 :               print_generic_expr (dump_file, callee, TDF_SLIM);
    6029            0 :               fprintf (dump_file, "\n");
    6030              :             }
    6031              : 
    6032          370 :           gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
    6033          370 :           changed = true;
    6034              :         }
    6035       446506 :       else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
    6036              :         {
    6037       441547 :           bool final;
    6038       441547 :           vec <cgraph_node *>targets
    6039       441547 :             = possible_polymorphic_call_targets (callee, stmt, &final);
    6040       444206 :           if (final && targets.length () <= 1 && dbg_cnt (devirt))
    6041              :             {
    6042         2025 :               tree lhs = gimple_call_lhs (stmt);
    6043         2025 :               if (dump_enabled_p ())
    6044              :                 {
    6045           34 :                   dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
    6046              :                                    "folding virtual function call to %s\n",
    6047           34 :                                    targets.length () == 1
    6048           17 :                                    ? targets[0]->name ()
    6049              :                                    : "__builtin_unreachable");
    6050              :                 }
    6051         2025 :               if (targets.length () == 1)
    6052              :                 {
    6053         1988 :                   tree fndecl = targets[0]->decl;
    6054         1988 :                   gimple_call_set_fndecl (stmt, fndecl);
    6055         1988 :                   changed = true;
    6056              :                   /* If changing the call to __cxa_pure_virtual
    6057              :                      or similar noreturn function, adjust gimple_call_fntype
    6058              :                      too.  */
    6059         1988 :                   if (gimple_call_noreturn_p (stmt)
    6060           19 :                       && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
    6061           13 :                       && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
    6062         2001 :                       && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
    6063           13 :                           == void_type_node))
    6064           13 :                     gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
    6065              :                   /* If the call becomes noreturn, remove the lhs.  */
    6066         1988 :                   if (lhs
    6067         1668 :                       && gimple_call_noreturn_p (stmt)
    6068         2003 :                       && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
    6069            6 :                           || should_remove_lhs_p (lhs)))
    6070              :                     {
    6071           12 :                       if (TREE_CODE (lhs) == SSA_NAME)
    6072              :                         {
    6073            0 :                           tree var = create_tmp_var (TREE_TYPE (lhs));
    6074            0 :                           tree def = get_or_create_ssa_default_def (cfun, var);
    6075            0 :                           gimple *new_stmt = gimple_build_assign (lhs, def);
    6076            0 :                           gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    6077              :                         }
    6078           12 :                       gimple_call_set_lhs (stmt, NULL_TREE);
    6079              :                     }
    6080         1988 :                   maybe_remove_unused_call_args (cfun, stmt);
    6081              :                 }
    6082              :               else
    6083              :                 {
    6084           37 :                   location_t loc = gimple_location (stmt);
    6085           37 :                   gimple *new_stmt = gimple_build_builtin_unreachable (loc);
    6086           37 :                   gimple_call_set_ctrl_altering (new_stmt, false);
    6087              :                   /* If the call had a SSA name as lhs morph that into
    6088              :                      an uninitialized value.  */
    6089           37 :                   if (lhs && TREE_CODE (lhs) == SSA_NAME)
    6090              :                     {
    6091           12 :                       tree var = create_tmp_var (TREE_TYPE (lhs));
    6092           12 :                       SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
    6093           12 :                       SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
    6094           12 :                       set_ssa_default_def (cfun, var, lhs);
    6095              :                     }
    6096           37 :                   gimple_move_vops (new_stmt, stmt);
    6097           37 :                   gsi_replace (gsi, new_stmt, false);
    6098           37 :                   return true;
    6099              :                 }
    6100              :             }
    6101              :         }
    6102              :     }
    6103              : 
    6104              :   /* Check for indirect calls that became direct calls, and then
    6105              :      no longer require a static chain.  */
    6106     55964935 :   if (gimple_call_chain (stmt))
    6107              :     {
    6108       246037 :       tree fn = gimple_call_fndecl (stmt);
    6109       294616 :       if (fn && !DECL_STATIC_CHAIN (fn))
    6110              :         {
    6111         2024 :           gimple_call_set_chain (stmt, NULL);
    6112         2024 :           changed = true;
    6113              :         }
    6114              :     }
    6115              : 
    6116     55964935 :   if (inplace)
    6117              :     return changed;
    6118              : 
    6119              :   /* Don't constant fold functions which can change the control. */
    6120     55962189 :   if (gimple_call_ctrl_altering_p (stmt))
    6121              :     return changed;
    6122              : 
    6123              :   /* Check for builtins that CCP can handle using information not
    6124              :      available in the generic fold routines.  */
    6125     48326834 :   if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
    6126              :     {
    6127      9382778 :       if (gimple_fold_builtin (gsi))
    6128       207189 :         changed = true;
    6129              :     }
    6130     38944056 :   else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
    6131              :     {
    6132      1121511 :         changed |= targetm.gimple_fold_builtin (gsi);
    6133              :     }
    6134     37822545 :   else if (gimple_call_internal_p (stmt))
    6135              :     {
    6136      1774720 :       enum tree_code subcode = ERROR_MARK;
    6137      1774720 :       tree result = NULL_TREE;
    6138      1774720 :       bool cplx_result = false;
    6139      1774720 :       bool uaddc_usubc = false;
    6140      1774720 :       tree overflow = NULL_TREE;
    6141      1774720 :       switch (gimple_call_internal_fn (stmt))
    6142              :         {
    6143          832 :         case IFN_ASSUME:
    6144              :           /* Remove .ASSUME calls during the last fold since it is no
    6145              :              longer needed.  */
    6146          832 :           if (fold_before_rtl_expansion_p ())
    6147          114 :             replace_call_with_value (gsi, NULL_TREE);
    6148              :           break;
    6149       162736 :         case IFN_BUILTIN_EXPECT:
    6150       162736 :           result = fold_builtin_expect (gimple_location (stmt),
    6151              :                                         gimple_call_arg (stmt, 0),
    6152              :                                         gimple_call_arg (stmt, 1),
    6153              :                                         gimple_call_arg (stmt, 2),
    6154              :                                         NULL_TREE);
    6155       162736 :           break;
    6156         8660 :         case IFN_UBSAN_OBJECT_SIZE:
    6157         8660 :           {
    6158         8660 :             tree offset = gimple_call_arg (stmt, 1);
    6159         8660 :             tree objsize = gimple_call_arg (stmt, 2);
    6160         8660 :             if (integer_all_onesp (objsize)
    6161         8660 :                 || (TREE_CODE (offset) == INTEGER_CST
    6162         4787 :                     && TREE_CODE (objsize) == INTEGER_CST
    6163         1126 :                     && tree_int_cst_le (offset, objsize)))
    6164              :               {
    6165         1539 :                 replace_call_with_value (gsi, NULL_TREE);
    6166         1539 :                 return true;
    6167              :               }
    6168              :           }
    6169              :           break;
    6170        11383 :         case IFN_UBSAN_PTR:
    6171        11383 :           if (integer_zerop (gimple_call_arg (stmt, 1)))
    6172              :             {
    6173           30 :               replace_call_with_value (gsi, NULL_TREE);
    6174           30 :               return true;
    6175              :             }
    6176              :           break;
    6177         8491 :         case IFN_UBSAN_BOUNDS:
    6178         8491 :           {
    6179         8491 :             tree index = gimple_call_arg (stmt, 1);
    6180         8491 :             tree bound = gimple_call_arg (stmt, 2);
    6181         8491 :             if (TREE_CODE (index) == INTEGER_CST
    6182         5469 :                 && TREE_CODE (bound) == INTEGER_CST)
    6183              :               {
    6184         4340 :                 index = fold_convert (TREE_TYPE (bound), index);
    6185         4340 :                 if (TREE_CODE (index) == INTEGER_CST
    6186         4340 :                     && tree_int_cst_lt (index, bound))
    6187              :                   {
    6188          286 :                     replace_call_with_value (gsi, NULL_TREE);
    6189          286 :                     return true;
    6190              :                   }
    6191              :               }
    6192              :           }
    6193              :           break;
    6194        20589 :         case IFN_GOACC_DIM_SIZE:
    6195        20589 :         case IFN_GOACC_DIM_POS:
    6196        20589 :           result = fold_internal_goacc_dim (stmt);
    6197        20589 :           break;
    6198              :         case IFN_UBSAN_CHECK_ADD:
    6199              :           subcode = PLUS_EXPR;
    6200              :           break;
    6201              :         case IFN_UBSAN_CHECK_SUB:
    6202              :           subcode = MINUS_EXPR;
    6203              :           break;
    6204              :         case IFN_UBSAN_CHECK_MUL:
    6205              :           subcode = MULT_EXPR;
    6206              :           break;
    6207              :         case IFN_ADD_OVERFLOW:
    6208              :           subcode = PLUS_EXPR;
    6209              :           cplx_result = true;
    6210              :           break;
    6211              :         case IFN_SUB_OVERFLOW:
    6212              :           subcode = MINUS_EXPR;
    6213              :           cplx_result = true;
    6214              :           break;
    6215              :         case IFN_MUL_OVERFLOW:
    6216              :           subcode = MULT_EXPR;
    6217              :           cplx_result = true;
    6218              :           break;
    6219              :         case IFN_UADDC:
    6220              :           subcode = PLUS_EXPR;
    6221              :           cplx_result = true;
    6222              :           uaddc_usubc = true;
    6223              :           break;
    6224              :         case IFN_USUBC:
    6225              :           subcode = MINUS_EXPR;
    6226              :           cplx_result = true;
    6227              :           uaddc_usubc = true;
    6228              :           break;
    6229         4054 :         case IFN_LEN_LOAD:
    6230         4054 :         case IFN_MASK_LOAD:
    6231         4054 :         case IFN_MASK_LEN_LOAD:
    6232         4054 :         case IFN_MASK_GATHER_LOAD:
    6233         4054 :         case IFN_MASK_LEN_GATHER_LOAD:
    6234         4054 :         case IFN_MASK_LOAD_LANES:
    6235         4054 :         case IFN_MASK_LEN_LOAD_LANES:
    6236         4054 :         case IFN_LEN_STORE:
    6237         4054 :         case IFN_MASK_STORE:
    6238         4054 :         case IFN_MASK_LEN_STORE:
    6239         4054 :         case IFN_MASK_SCATTER_STORE:
    6240         4054 :         case IFN_MASK_LEN_SCATTER_STORE:
    6241         4054 :         case IFN_MASK_STORE_LANES:
    6242         4054 :         case IFN_MASK_LEN_STORE_LANES:
    6243         4054 :           changed |= gimple_fold_partial_load_store (gsi, stmt);
    6244         4054 :           break;
    6245              :         default:
    6246              :           break;
    6247              :         }
    6248       187493 :       if (subcode != ERROR_MARK)
    6249              :         {
    6250       493366 :           tree arg0 = gimple_call_arg (stmt, 0);
    6251       493366 :           tree arg1 = gimple_call_arg (stmt, 1);
    6252       493366 :           tree arg2 = NULL_TREE;
    6253       493366 :           tree type = TREE_TYPE (arg0);
    6254       493366 :           if (cplx_result)
    6255              :             {
    6256       474287 :               tree lhs = gimple_call_lhs (stmt);
    6257       474287 :               if (lhs == NULL_TREE)
    6258              :                 type = NULL_TREE;
    6259              :               else
    6260       474287 :                 type = TREE_TYPE (TREE_TYPE (lhs));
    6261       474287 :               if (uaddc_usubc)
    6262        31855 :                 arg2 = gimple_call_arg (stmt, 2);
    6263              :             }
    6264       493366 :           if (type == NULL_TREE)
    6265              :             ;
    6266       493366 :           else if (uaddc_usubc)
    6267              :             {
    6268        31855 :               if (!integer_zerop (arg2))
    6269              :                 ;
    6270              :               /* x = y + 0 + 0; x = y - 0 - 0; */
    6271         4825 :               else if (integer_zerop (arg1))
    6272              :                 result = arg0;
    6273              :               /* x = 0 + y + 0; */
    6274         4201 :               else if (subcode != MINUS_EXPR && integer_zerop (arg0))
    6275              :                 result = arg1;
    6276              :               /* x = y - y - 0; */
    6277         4201 :               else if (subcode == MINUS_EXPR
    6278         4201 :                        && operand_equal_p (arg0, arg1, 0))
    6279            0 :                 result = integer_zero_node;
    6280              :             }
    6281              :           /* x = y + 0; x = y - 0; x = y * 0; */
    6282       461511 :           else if (integer_zerop (arg1))
    6283        10107 :             result = subcode == MULT_EXPR ? integer_zero_node : arg0;
    6284              :           /* x = 0 + y; x = 0 * y; */
    6285       451404 :           else if (subcode != MINUS_EXPR && integer_zerop (arg0))
    6286            0 :             result = subcode == MULT_EXPR ? integer_zero_node : arg1;
    6287              :           /* x = y - y; */
    6288       451404 :           else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
    6289            7 :             result = integer_zero_node;
    6290              :           /* x = y * 1; x = 1 * y; */
    6291       451397 :           else if (subcode == MULT_EXPR && integer_onep (arg1))
    6292              :             result = arg0;
    6293       446132 :           else if (subcode == MULT_EXPR && integer_onep (arg0))
    6294              :             result = arg1;
    6295       493366 :           if (result)
    6296              :             {
    6297        16003 :               if (result == integer_zero_node)
    6298         2140 :                 result = build_zero_cst (type);
    6299        13863 :               else if (cplx_result && TREE_TYPE (result) != type)
    6300              :                 {
    6301         9638 :                   if (TREE_CODE (result) == INTEGER_CST)
    6302              :                     {
    6303            0 :                       if (arith_overflowed_p (PLUS_EXPR, type, result,
    6304              :                                               integer_zero_node))
    6305            0 :                         overflow = build_one_cst (type);
    6306              :                     }
    6307         9638 :                   else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
    6308         6920 :                             && TYPE_UNSIGNED (type))
    6309         9783 :                            || (TYPE_PRECISION (type)
    6310         2863 :                                < (TYPE_PRECISION (TREE_TYPE (result))
    6311         2863 :                                   + (TYPE_UNSIGNED (TREE_TYPE (result))
    6312         3219 :                                      && !TYPE_UNSIGNED (type)))))
    6313              :                     result = NULL_TREE;
    6314           62 :                   if (result)
    6315           62 :                     result = fold_convert (type, result);
    6316              :                 }
    6317              :             }
    6318              :         }
    6319              : 
    6320      1285926 :       if (result)
    6321              :         {
    6322        28922 :           if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
    6323            0 :             result = drop_tree_overflow (result);
    6324        28922 :           if (cplx_result)
    6325              :             {
    6326         6416 :               if (overflow == NULL_TREE)
    6327         6416 :                 overflow = build_zero_cst (TREE_TYPE (result));
    6328         6416 :               tree ctype = build_complex_type (TREE_TYPE (result));
    6329         6416 :               if (TREE_CODE (result) == INTEGER_CST
    6330         2140 :                   && TREE_CODE (overflow) == INTEGER_CST)
    6331         2140 :                 result = build_complex (ctype, result, overflow);
    6332              :               else
    6333         4276 :                 result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
    6334              :                                      ctype, result, overflow);
    6335              :             }
    6336        28922 :           gimplify_and_update_call_from_tree (gsi, result);
    6337        28922 :           changed = true;
    6338              :         }
    6339              :     }
    6340              : 
    6341              :   return changed;
    6342              : }
    6343              : 
    6344              : 
    6345              : /* Return true whether NAME has a use on STMT.  Note this can return
    6346              :    false even though there's a use on STMT if SSA operands are not
    6347              :    up-to-date.  */
    6348              : 
    6349              : static bool
    6350         1655 : has_use_on_stmt (tree name, gimple *stmt)
    6351              : {
    6352         1655 :   ssa_op_iter iter;
    6353         1655 :   tree op;
    6354         3335 :   FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
    6355         1726 :     if (op == name)
    6356              :       return true;
    6357              :   return false;
    6358              : }
    6359              : 
    6360              : /* Add the lhs of each statement of SEQ to DCE_WORKLIST. */
    6361              : 
    6362              : void
    6363      4683429 : mark_lhs_in_seq_for_dce (bitmap dce_worklist, gimple_seq seq)
    6364              : {
    6365      4683429 :   if (!dce_worklist)
    6366              :     return;
    6367              : 
    6368      1621651 :   for (gimple_stmt_iterator i = gsi_start (seq);
    6369      1898973 :        !gsi_end_p (i); gsi_next (&i))
    6370              :     {
    6371       277322 :       gimple *stmt = gsi_stmt (i);
    6372       277322 :       tree name = gimple_get_lhs (stmt);
    6373       277322 :       if (name && TREE_CODE (name) == SSA_NAME)
    6374       277322 :         bitmap_set_bit (dce_worklist, SSA_NAME_VERSION (name));
    6375              :     }
    6376              : }
    6377              : 
    6378              : /* Worker for fold_stmt_1 dispatch to pattern based folding with
    6379              :    gimple_simplify.
    6380              : 
    6381              :    Replaces *GSI with the simplification result in RCODE and OPS
    6382              :    and the associated statements in *SEQ.  Does the replacement
    6383              :    according to INPLACE and returns true if the operation succeeded.  */
    6384              : 
    6385              : static bool
    6386      9041476 : replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
    6387              :                                   gimple_match_op *res_op,
    6388              :                                   gimple_seq *seq, bool inplace,
    6389              :                                   bitmap dce_worklist)
    6390              : {
    6391      9041476 :   gimple *stmt = gsi_stmt (*gsi);
    6392      9041476 :   tree *ops = res_op->ops;
    6393      9041476 :   unsigned int num_ops = res_op->num_ops;
    6394              : 
    6395              :   /* Play safe and do not allow abnormals to be mentioned in
    6396              :      newly created statements.  See also maybe_push_res_to_seq.
    6397              :      As an exception allow such uses if there was a use of the
    6398              :      same SSA name on the old stmt.  */
    6399     20050590 :   for (unsigned int i = 0; i < num_ops; ++i)
    6400     11010723 :     if (TREE_CODE (ops[i]) == SSA_NAME
    6401      6683691 :         && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[i])
    6402     11012378 :         && !has_use_on_stmt (ops[i], stmt))
    6403              :       return false;
    6404              : 
    6405      9039867 :   if (num_ops > 0 && COMPARISON_CLASS_P (ops[0]))
    6406            0 :     for (unsigned int i = 0; i < 2; ++i)
    6407            0 :       if (TREE_CODE (TREE_OPERAND (ops[0], i)) == SSA_NAME
    6408            0 :           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], i))
    6409            0 :           && !has_use_on_stmt (TREE_OPERAND (ops[0], i), stmt))
    6410              :         return false;
    6411              : 
    6412              :   /* Don't insert new statements when INPLACE is true, even if we could
    6413              :      reuse STMT for the final statement.  */
    6414      9039867 :   if (inplace && !gimple_seq_empty_p (*seq))
    6415              :     return false;
    6416              : 
    6417      9039867 :   if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
    6418              :     {
    6419      6810380 :       gcc_assert (res_op->code.is_tree_code ());
    6420      6810380 :       auto code = tree_code (res_op->code);
    6421      6810380 :       if (TREE_CODE_CLASS (code) == tcc_comparison
    6422              :           /* GIMPLE_CONDs condition may not throw.  */
    6423      6810380 :           && ((cfun
    6424      1131744 :                && (!flag_exceptions
    6425       770755 :                    || !cfun->can_throw_non_call_exceptions))
    6426       291677 :               || !operation_could_trap_p (code,
    6427       291677 :                                           FLOAT_TYPE_P (TREE_TYPE (ops[0])),
    6428              :                                           false, NULL_TREE)))
    6429      1120453 :         gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
    6430      5689927 :       else if (code == SSA_NAME)
    6431              :         {
    6432              :           /* If setting the gimple cond to the same thing,
    6433              :              return false as nothing changed.  */
    6434      3936682 :           if (gimple_cond_code (cond_stmt) == NE_EXPR
    6435      3912466 :               && operand_equal_p (gimple_cond_lhs (cond_stmt), ops[0])
    6436      7846628 :               && integer_zerop (gimple_cond_rhs (cond_stmt)))
    6437              :             return false;
    6438        26736 :           gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
    6439        26736 :                                      build_zero_cst (TREE_TYPE (ops[0])));
    6440              :         }
    6441      1753245 :       else if (code == INTEGER_CST)
    6442              :         {
    6443              :           /* Make into the canonical form `1 != 0` and `0 != 0`.
    6444              :              If already in the canonical form return false
    6445              :              saying nothing has been done.  */
    6446      1219173 :           if (integer_zerop (ops[0]))
    6447              :             {
    6448      5292922 :               if (gimple_cond_false_canonical_p (cond_stmt))
    6449              :                 return false;
    6450       501787 :               gimple_cond_make_false (cond_stmt);
    6451              :             }
    6452              :           else
    6453              :             {
    6454       370078 :               if (gimple_cond_true_canonical_p (cond_stmt))
    6455              :                 return false;
    6456       197388 :               gimple_cond_make_true (cond_stmt);
    6457              :             }
    6458              :         }
    6459       534072 :       else if (!inplace)
    6460              :         {
    6461              :           /* For throwing comparisons, see if the GIMPLE_COND is the same as
    6462              :              the comparison would be.
    6463              :              This can happen due to the match pattern for
    6464              :              `(ne (cmp @0 @1) integer_zerop)` which creates a new expression
    6465              :              for the comparison.  */
    6466       534072 :           if (TREE_CODE_CLASS (code) == tcc_comparison
    6467        11291 :               && (!cfun
    6468        11291 :                   || (flag_exceptions
    6469        11291 :                       && cfun->can_throw_non_call_exceptions))
    6470       545363 :               && operation_could_trap_p (code,
    6471        11291 :                                          FLOAT_TYPE_P (TREE_TYPE (ops[0])),
    6472              :                                          false, NULL_TREE))
    6473              :             {
    6474        11291 :               tree lhs = gimple_cond_lhs (cond_stmt);
    6475        11291 :               if (gimple_cond_code (cond_stmt) == NE_EXPR
    6476        11291 :                   && TREE_CODE (lhs) == SSA_NAME
    6477        11291 :                   && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
    6478        22582 :                   && integer_zerop (gimple_cond_rhs (cond_stmt)))
    6479              :                 {
    6480        11291 :                   gimple *s = SSA_NAME_DEF_STMT (lhs);
    6481        11291 :                   if (is_gimple_assign (s)
    6482        11291 :                       && gimple_assign_rhs_code (s) == code
    6483        11291 :                       && operand_equal_p (gimple_assign_rhs1 (s), ops[0])
    6484        22582 :                       && operand_equal_p (gimple_assign_rhs2 (s), ops[1]))
    6485              :                     return false;
    6486              :                 }
    6487              :             }
    6488       522781 :           tree res = maybe_push_res_to_seq (res_op, seq);
    6489       522781 :           if (!res)
    6490              :             return false;
    6491       522781 :           gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
    6492       522781 :                                      build_zero_cst (TREE_TYPE (res)));
    6493              :         }
    6494              :       else
    6495              :         return false;
    6496      2369145 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6497              :         {
    6498          857 :           fprintf (dump_file, "gimple_simplified to ");
    6499          857 :           if (!gimple_seq_empty_p (*seq))
    6500            0 :             print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6501          857 :           print_gimple_stmt (dump_file, gsi_stmt (*gsi),
    6502              :                              0, TDF_SLIM);
    6503              :         }
    6504              :       // Mark the lhs of the new statements maybe for dce
    6505      2369145 :       mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6506      2369145 :       gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
    6507      2369145 :       return true;
    6508              :     }
    6509      2229487 :   else if (is_gimple_assign (stmt)
    6510      2229487 :            && res_op->code.is_tree_code ())
    6511              :     {
    6512      2151712 :       auto code = tree_code (res_op->code);
    6513      2151712 :       if (!inplace
    6514      2151712 :           || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (code))
    6515              :         {
    6516      2151712 :           maybe_build_generic_op (res_op);
    6517      5083470 :           gimple_assign_set_rhs_with_ops (gsi, code,
    6518              :                                           res_op->op_or_null (0),
    6519              :                                           res_op->op_or_null (1),
    6520              :                                           res_op->op_or_null (2));
    6521      2151712 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6522              :             {
    6523        16594 :               fprintf (dump_file, "gimple_simplified to ");
    6524        16594 :               if (!gimple_seq_empty_p (*seq))
    6525           48 :                 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6526        16594 :               print_gimple_stmt (dump_file, gsi_stmt (*gsi),
    6527              :                                  0, TDF_SLIM);
    6528              :             }
    6529              :           // Mark the lhs of the new statements maybe for dce
    6530      2151712 :           mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6531      2151712 :           gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
    6532      2151712 :           return true;
    6533              :         }
    6534              :     }
    6535        77775 :   else if (res_op->code.is_fn_code ()
    6536        77775 :            && gimple_call_combined_fn (stmt) == combined_fn (res_op->code))
    6537              :     {
    6538         8070 :       gcc_assert (num_ops == gimple_call_num_args (stmt));
    6539        23886 :       for (unsigned int i = 0; i < num_ops; ++i)
    6540        15816 :         gimple_call_set_arg (stmt, i, ops[i]);
    6541         8070 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6542              :         {
    6543            0 :           fprintf (dump_file, "gimple_simplified to ");
    6544            0 :           if (!gimple_seq_empty_p (*seq))
    6545            0 :             print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6546            0 :           print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
    6547              :         }
    6548              :       // Mark the lhs of the new statements maybe for dce
    6549         8070 :       mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6550         8070 :       gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
    6551         8070 :       return true;
    6552              :     }
    6553        69705 :   else if (!inplace)
    6554              :     {
    6555       137420 :       if (gimple_has_lhs (stmt))
    6556              :         {
    6557        69705 :           tree lhs = gimple_get_lhs (stmt);
    6558        69705 :           if (!maybe_push_res_to_seq (res_op, seq, lhs))
    6559              :             return false;
    6560        68722 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6561              :             {
    6562           10 :               fprintf (dump_file, "gimple_simplified to ");
    6563           10 :               print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
    6564              :             }
    6565              :           // Mark the lhs of the new statements maybe for dce
    6566        68722 :           mark_lhs_in_seq_for_dce (dce_worklist, *seq);
    6567        68722 :           gsi_replace_with_seq_vops (gsi, *seq);
    6568        68722 :           return true;
    6569              :         }
    6570              :       else
    6571            0 :         gcc_unreachable ();
    6572              :     }
    6573              : 
    6574              :   return false;
    6575              : }
    6576              : 
    6577              : /* Canonicalize MEM_REFs invariant address operand after propagation.  */
    6578              : 
    6579              : static bool
    6580    189024242 : maybe_canonicalize_mem_ref_addr (tree *t, bool is_debug = false)
    6581              : {
    6582    189024242 :   bool res = false;
    6583    189024242 :   tree *orig_t = t;
    6584              : 
    6585    189024242 :   if (TREE_CODE (*t) == ADDR_EXPR)
    6586     62844991 :     t = &TREE_OPERAND (*t, 0);
    6587              : 
    6588              :   /* The C and C++ frontends use an ARRAY_REF for indexing with their
    6589              :      generic vector extension.  The actual vector referenced is
    6590              :      view-converted to an array type for this purpose.  If the index
    6591              :      is constant the canonical representation in the middle-end is a
    6592              :      BIT_FIELD_REF so re-write the former to the latter here.  */
    6593    189024242 :   if (TREE_CODE (*t) == ARRAY_REF
    6594     10641413 :       && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
    6595       160208 :       && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
    6596    189073465 :       && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
    6597              :     {
    6598        19972 :       tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
    6599        19972 :       if (VECTOR_TYPE_P (vtype))
    6600              :         {
    6601        19972 :           tree low = array_ref_low_bound (*t);
    6602        19972 :           if (TREE_CODE (low) == INTEGER_CST)
    6603              :             {
    6604        19972 :               if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
    6605              :                 {
    6606        39900 :                   widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
    6607        39900 :                                             wi::to_widest (low));
    6608        19950 :                   idx = wi::mul (idx, wi::to_widest
    6609        39900 :                                          (TYPE_SIZE (TREE_TYPE (*t))));
    6610        19950 :                   widest_int ext
    6611        19950 :                     = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
    6612        19950 :                   if (maybe_le (ext, wi::to_poly_widest (TYPE_SIZE (vtype))))
    6613              :                     {
    6614        39004 :                       *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
    6615        19502 :                                        TREE_TYPE (*t),
    6616        19502 :                                        TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
    6617        19502 :                                        TYPE_SIZE (TREE_TYPE (*t)),
    6618        19502 :                                        wide_int_to_tree (bitsizetype, idx));
    6619        19502 :                       res = true;
    6620              :                     }
    6621        19950 :                 }
    6622              :             }
    6623              :         }
    6624              :     }
    6625              : 
    6626    356720818 :   while (handled_component_p (*t))
    6627    167696576 :     t = &TREE_OPERAND (*t, 0);
    6628              : 
    6629              :   /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
    6630              :      of invariant addresses into a SSA name MEM_REF address.  */
    6631    189024242 :   if (TREE_CODE (*t) == MEM_REF
    6632    189024242 :       || TREE_CODE (*t) == TARGET_MEM_REF)
    6633              :     {
    6634    100202413 :       tree addr = TREE_OPERAND (*t, 0);
    6635    100202413 :       if (TREE_CODE (addr) == ADDR_EXPR
    6636    100202413 :           && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
    6637     30653842 :               || handled_component_p (TREE_OPERAND (addr, 0))))
    6638              :         {
    6639       534121 :           tree base;
    6640       534121 :           poly_int64 coffset;
    6641       534121 :           base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
    6642              :                                                 &coffset);
    6643       534121 :           if (!base)
    6644              :             {
    6645           18 :               if (is_debug)
    6646           18 :                 return false;
    6647            0 :               gcc_unreachable ();
    6648              :             }
    6649              : 
    6650       534103 :           TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
    6651       534103 :           TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
    6652       534103 :                                                   TREE_OPERAND (*t, 1),
    6653       534103 :                                                   size_int (coffset));
    6654       534103 :           res = true;
    6655              :         }
    6656    100202395 :       gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
    6657              :                            || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
    6658              :     }
    6659              : 
    6660              :   /* Canonicalize back MEM_REFs to plain reference trees if the object
    6661              :      accessed is a decl that has the same access semantics as the MEM_REF.  */
    6662    189024224 :   if (TREE_CODE (*t) == MEM_REF
    6663     98226296 :       && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
    6664     30341738 :       && integer_zerop (TREE_OPERAND (*t, 1))
    6665    205316089 :       && MR_DEPENDENCE_CLIQUE (*t) == 0)
    6666              :     {
    6667     11555198 :       tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
    6668     11555198 :       tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
    6669     11555198 :       if (/* Same volatile qualification.  */
    6670     11555198 :           TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
    6671              :           /* Same TBAA behavior with -fstrict-aliasing.  */
    6672     11552188 :           && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
    6673     11217135 :           && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
    6674     11217135 :               == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
    6675              :           /* Same alignment.  */
    6676      4594952 :           && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
    6677              :           /* We have to look out here to not drop a required conversion
    6678              :              from the rhs to the lhs if *t appears on the lhs or vice-versa
    6679              :              if it appears on the rhs.  Thus require strict type
    6680              :              compatibility.  */
    6681     15870552 :           && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
    6682              :         {
    6683      2369608 :           *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
    6684      2369608 :           res = true;
    6685              :         }
    6686              :     }
    6687              : 
    6688    177469026 :   else if (TREE_CODE (*orig_t) == ADDR_EXPR
    6689     60239424 :            && TREE_CODE (*t) == MEM_REF
    6690    198657029 :            && TREE_CODE (TREE_OPERAND (*t, 0)) == INTEGER_CST)
    6691              :     {
    6692          853 :       tree base;
    6693          853 :       poly_int64 coffset;
    6694          853 :       base = get_addr_base_and_unit_offset (TREE_OPERAND (*orig_t, 0),
    6695              :                                             &coffset);
    6696          853 :       if (base)
    6697              :         {
    6698          732 :           gcc_assert (TREE_CODE (base) == MEM_REF);
    6699          732 :           poly_int64 moffset;
    6700          732 :           if (mem_ref_offset (base).to_shwi (&moffset))
    6701              :             {
    6702          732 :               coffset += moffset;
    6703          732 :               if (wi::to_poly_wide (TREE_OPERAND (base, 0)).to_shwi (&moffset))
    6704              :                 {
    6705          732 :                   coffset += moffset;
    6706          732 :                   *orig_t = build_int_cst (TREE_TYPE (*orig_t), coffset);
    6707          732 :                   return true;
    6708              :                 }
    6709              :             }
    6710              :         }
    6711              :     }
    6712              : 
    6713              :   /* Canonicalize TARGET_MEM_REF in particular with respect to
    6714              :      the indexes becoming constant.  */
    6715    177468173 :   else if (TREE_CODE (*t) == TARGET_MEM_REF)
    6716              :     {
    6717      1976099 :       tree tem = maybe_fold_tmr (*t);
    6718      1976099 :       if (tem)
    6719              :         {
    6720         1662 :           *t = tem;
    6721         1662 :           if (TREE_CODE (*orig_t) == ADDR_EXPR)
    6722            0 :             recompute_tree_invariant_for_addr_expr (*orig_t);
    6723              :           res = true;
    6724              :         }
    6725              :     }
    6726              : 
    6727              :   return res;
    6728              : }
    6729              : 
    6730              : /* Worker for both fold_stmt and fold_stmt_inplace.  The INPLACE argument
    6731              :    distinguishes both cases.  */
    6732              : 
    6733              : static bool
    6734    759406844 : fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree),
    6735              :              bitmap dce_worklist = nullptr)
    6736              : {
    6737    759406844 :   bool changed = false;
    6738    759406844 :   gimple *stmt = gsi_stmt (*gsi);
    6739    759406844 :   bool nowarning = warning_suppressed_p (stmt, OPT_Wstrict_overflow);
    6740    759406844 :   unsigned i;
    6741    759406844 :   fold_defer_overflow_warnings ();
    6742              : 
    6743              :   /* First do required canonicalization of [TARGET_]MEM_REF addresses
    6744              :      after propagation.
    6745              :      ???  This shouldn't be done in generic folding but in the
    6746              :      propagation helpers which also know whether an address was
    6747              :      propagated.
    6748              :      Also canonicalize operand order.  */
    6749    759406844 :   switch (gimple_code (stmt))
    6750              :     {
    6751    253104701 :     case GIMPLE_ASSIGN:
    6752    253104701 :       if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
    6753              :         {
    6754    166701795 :           tree *rhs = gimple_assign_rhs1_ptr (stmt);
    6755    166701795 :           if ((REFERENCE_CLASS_P (*rhs)
    6756    105415155 :                || TREE_CODE (*rhs) == ADDR_EXPR)
    6757    181432407 :               && maybe_canonicalize_mem_ref_addr (rhs))
    6758              :             changed = true;
    6759    166701795 :           tree *lhs = gimple_assign_lhs_ptr (stmt);
    6760    166701795 :           if (REFERENCE_CLASS_P (*lhs)
    6761    166701795 :               && maybe_canonicalize_mem_ref_addr (lhs))
    6762              :             changed = true;
    6763              :           /* Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST.
    6764              :              This cannot be done in maybe_canonicalize_mem_ref_addr
    6765              :              as the gimple now has two operands rather than one.
    6766              :              The same reason why this can't be done in
    6767              :              maybe_canonicalize_mem_ref_addr is the same reason why
    6768              :              this can't be done inplace.  */
    6769    166701795 :           if (!inplace && TREE_CODE (*rhs) == ADDR_EXPR)
    6770              :             {
    6771     14534845 :               tree inner = TREE_OPERAND (*rhs, 0);
    6772     14534845 :               if (TREE_CODE (inner) == MEM_REF
    6773      1006720 :                   && TREE_CODE (TREE_OPERAND (inner, 0)) == SSA_NAME
    6774     14597451 :                   && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
    6775              :                 {
    6776        62606 :                   tree ptr = TREE_OPERAND (inner, 0);
    6777        62606 :                   tree addon = TREE_OPERAND (inner, 1);
    6778        62606 :                   addon = fold_convert (sizetype, addon);
    6779        62606 :                   gimple_assign_set_rhs_with_ops (gsi, POINTER_PLUS_EXPR,
    6780              :                                                   ptr, addon);
    6781        62606 :                   changed = true;
    6782        62606 :                   stmt = gsi_stmt (*gsi);
    6783              :                 }
    6784              :             }
    6785              :         }
    6786              :       else
    6787              :         {
    6788              :           /* Canonicalize operand order.  */
    6789     86402906 :           enum tree_code code = gimple_assign_rhs_code (stmt);
    6790     86402906 :           if (TREE_CODE_CLASS (code) == tcc_comparison
    6791     80358552 :               || commutative_tree_code (code)
    6792    129751788 :               || commutative_ternary_tree_code (code))
    6793              :             {
    6794     43054978 :               tree rhs1 = gimple_assign_rhs1 (stmt);
    6795     43054978 :               tree rhs2 = gimple_assign_rhs2 (stmt);
    6796     43054978 :               if (tree_swap_operands_p (rhs1, rhs2))
    6797              :                 {
    6798      2571113 :                   gimple_assign_set_rhs1 (stmt, rhs2);
    6799      2571113 :                   gimple_assign_set_rhs2 (stmt, rhs1);
    6800      2571113 :                   if (TREE_CODE_CLASS (code) == tcc_comparison)
    6801       303700 :                     gimple_assign_set_rhs_code (stmt,
    6802              :                                                 swap_tree_comparison (code));
    6803              :                   changed = true;
    6804              :                 }
    6805              :             }
    6806              :         }
    6807              :       break;
    6808     56020892 :     case GIMPLE_CALL:
    6809     56020892 :       {
    6810     56020892 :         gcall *call = as_a<gcall *> (stmt);
    6811    167706605 :         for (i = 0; i < gimple_call_num_args (call); ++i)
    6812              :           {
    6813    111685713 :             tree *arg = gimple_call_arg_ptr (call, i);
    6814    111685713 :             if (REFERENCE_CLASS_P (*arg)
    6815    111685713 :                 && maybe_canonicalize_mem_ref_addr (arg))
    6816              :               changed = true;
    6817              :           }
    6818     56020892 :         tree *lhs = gimple_call_lhs_ptr (call);
    6819     56020892 :         if (*lhs
    6820     22595450 :             && REFERENCE_CLASS_P (*lhs)
    6821     56138402 :             && maybe_canonicalize_mem_ref_addr (lhs))
    6822              :           changed = true;
    6823     56020892 :         if (*lhs)
    6824              :           {
    6825     22595450 :             combined_fn cfn = gimple_call_combined_fn (call);
    6826     22595450 :             internal_fn ifn = associated_internal_fn (cfn, TREE_TYPE (*lhs));
    6827     22595450 :             int opno = first_commutative_argument (ifn);
    6828     22595450 :             if (opno >= 0)
    6829              :               {
    6830       354192 :                 tree arg1 = gimple_call_arg (call, opno);
    6831       354192 :                 tree arg2 = gimple_call_arg (call, opno + 1);
    6832       354192 :                 if (tree_swap_operands_p (arg1, arg2))
    6833              :                   {
    6834        22299 :                     gimple_call_set_arg (call, opno, arg2);
    6835        22299 :                     gimple_call_set_arg (call, opno + 1, arg1);
    6836        22299 :                     changed = true;
    6837              :                   }
    6838              :               }
    6839              :           }
    6840              :         break;
    6841              :       }
    6842       585868 :     case GIMPLE_ASM:
    6843       585868 :       {
    6844       585868 :         gasm *asm_stmt = as_a <gasm *> (stmt);
    6845      1232411 :         for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
    6846              :           {
    6847       646543 :             tree link = gimple_asm_output_op (asm_stmt, i);
    6848       646543 :             tree op = TREE_VALUE (link);
    6849       646543 :             if (REFERENCE_CLASS_P (op)
    6850       646543 :                 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
    6851              :               changed = true;
    6852              :           }
    6853       986243 :         for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
    6854              :           {
    6855       400375 :             tree link = gimple_asm_input_op (asm_stmt, i);
    6856       400375 :             tree op = TREE_VALUE (link);
    6857       400375 :             if ((REFERENCE_CLASS_P (op)
    6858       385926 :                  || TREE_CODE (op) == ADDR_EXPR)
    6859       433968 :                 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
    6860              :               changed = true;
    6861              :           }
    6862              :       }
    6863              :       break;
    6864    384938869 :     case GIMPLE_DEBUG:
    6865    384938869 :       if (gimple_debug_bind_p (stmt))
    6866              :         {
    6867    296470367 :           tree *val = gimple_debug_bind_get_value_ptr (stmt);
    6868    296470367 :           if (*val
    6869    168751070 :               && (REFERENCE_CLASS_P (*val)
    6870    166262638 :                   || TREE_CODE (*val) == ADDR_EXPR)
    6871    347039585 :               && maybe_canonicalize_mem_ref_addr (val, true))
    6872              :             changed = true;
    6873              :         }
    6874              :       break;
    6875     44729742 :     case GIMPLE_COND:
    6876     44729742 :       {
    6877              :         /* Canonicalize operand order.  */
    6878     44729742 :         tree lhs = gimple_cond_lhs (stmt);
    6879     44729742 :         tree rhs = gimple_cond_rhs (stmt);
    6880     44729742 :         if (tree_swap_operands_p (lhs, rhs))
    6881              :           {
    6882      1406476 :             gcond *gc = as_a <gcond *> (stmt);
    6883      1406476 :             gimple_cond_set_lhs (gc, rhs);
    6884      1406476 :             gimple_cond_set_rhs (gc, lhs);
    6885      1406476 :             gimple_cond_set_code (gc,
    6886              :                                   swap_tree_comparison (gimple_cond_code (gc)));
    6887      1406476 :             changed = true;
    6888              :           }
    6889              :       }
    6890    757907785 :     default:;
    6891              :     }
    6892              : 
    6893              :   /* Dispatch to pattern-based folding.  */
    6894    757907785 :   if (!inplace
    6895      3143524 :       || is_gimple_assign (stmt)
    6896    758794064 :       || gimple_code (stmt) == GIMPLE_COND)
    6897              :     {
    6898    758520565 :       gimple_seq seq = NULL;
    6899    758520565 :       gimple_match_op res_op;
    6900   1514783885 :       if (gimple_simplify (stmt, &res_op, inplace ? NULL : &seq,
    6901              :                            valueize, valueize)
    6902    758520565 :           && replace_stmt_with_simplification (gsi, &res_op, &seq, inplace,
    6903              :                                                dce_worklist))
    6904              :         changed = true;
    6905              :       else
    6906    753922916 :         gimple_seq_discard (seq);
    6907              :     }
    6908              : 
    6909    759406844 :   stmt = gsi_stmt (*gsi);
    6910              : 
    6911              :   /* Fold the main computation performed by the statement.  */
    6912    759406844 :   switch (gimple_code (stmt))
    6913              :     {
    6914    253160621 :     case GIMPLE_ASSIGN:
    6915    253160621 :       {
    6916              :         /* Try to canonicalize for boolean-typed X the comparisons
    6917              :            X == 0, X == 1, X != 0, and X != 1.  */
    6918    253160621 :         if (gimple_assign_rhs_code (stmt) == EQ_EXPR
    6919    253160621 :             || gimple_assign_rhs_code (stmt) == NE_EXPR)
    6920              :           {
    6921      3287925 :             tree lhs = gimple_assign_lhs (stmt);
    6922      3287925 :             tree op1 = gimple_assign_rhs1 (stmt);
    6923      3287925 :             tree op2 = gimple_assign_rhs2 (stmt);
    6924      3287925 :             tree type = TREE_TYPE (op1);
    6925              : 
    6926              :             /* Check whether the comparison operands are of the same boolean
    6927              :                type as the result type is.
    6928              :                Check that second operand is an integer-constant with value
    6929              :                one or zero.  */
    6930      3287925 :             if (TREE_CODE (op2) == INTEGER_CST
    6931      2256644 :                 && (integer_zerop (op2) || integer_onep (op2))
    6932      4996448 :                 && useless_type_conversion_p (TREE_TYPE (lhs), type))
    6933              :               {
    6934         4722 :                 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
    6935         4722 :                 bool is_logical_not = false;
    6936              : 
    6937              :                 /* X == 0 and X != 1 is a logical-not.of X
    6938              :                    X == 1 and X != 0 is X  */
    6939         3993 :                 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
    6940         4722 :                     || (cmp_code == NE_EXPR && integer_onep (op2)))
    6941         4680 :                   is_logical_not = true;
    6942              : 
    6943         4722 :                 if (is_logical_not == false)
    6944           42 :                   gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
    6945              :                 /* Only for one-bit precision typed X the transformation
    6946              :                    !X -> ~X is valied.  */
    6947         4680 :                 else if (TYPE_PRECISION (type) == 1)
    6948         4680 :                   gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
    6949              :                 /* Otherwise we use !X -> X ^ 1.  */
    6950              :                 else
    6951            0 :                   gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
    6952              :                                                   build_int_cst (type, 1));
    6953              :                 changed = true;
    6954              :                 break;
    6955              :               }
    6956              :           }
    6957              : 
    6958    253155899 :         unsigned old_num_ops = gimple_num_ops (stmt);
    6959    253155899 :         tree lhs = gimple_assign_lhs (stmt);
    6960    253155899 :         tree new_rhs = fold_gimple_assign (gsi);
    6961    253155899 :         if (new_rhs
    6962    253275939 :             && !useless_type_conversion_p (TREE_TYPE (lhs),
    6963       120040 :                                            TREE_TYPE (new_rhs)))
    6964            0 :           new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
    6965    253155899 :         if (new_rhs
    6966    253155899 :             && (!inplace
    6967          987 :                 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
    6968              :           {
    6969       120040 :             gimple_assign_set_rhs_from_tree (gsi, new_rhs);
    6970       120040 :             changed = true;
    6971              :           }
    6972              :         break;
    6973              :       }
    6974              : 
    6975     55964972 :     case GIMPLE_CALL:
    6976     55964972 :       changed |= gimple_fold_call (gsi, inplace);
    6977     55964972 :       break;
    6978              : 
    6979    384938869 :     case GIMPLE_DEBUG:
    6980    384938869 :       if (gimple_debug_bind_p (stmt))
    6981              :         {
    6982    296470367 :           tree val = gimple_debug_bind_get_value (stmt);
    6983    296470367 :           if (val && REFERENCE_CLASS_P (val))
    6984              :             {
    6985      2486600 :               tree tem = maybe_fold_reference (val);
    6986      2486600 :               if (tem)
    6987              :                 {
    6988         1591 :                   gimple_debug_bind_set_value (stmt, tem);
    6989         1591 :                   changed = true;
    6990              :                 }
    6991              :             }
    6992              :         }
    6993              :       break;
    6994              : 
    6995     10624746 :     case GIMPLE_RETURN:
    6996     10624746 :       {
    6997     10624746 :         greturn *ret_stmt = as_a<greturn *> (stmt);
    6998     10624746 :         tree ret = gimple_return_retval(ret_stmt);
    6999              : 
    7000     10624746 :         if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
    7001              :           {
    7002      4473780 :             tree val = valueize (ret);
    7003      4473780 :             if (val && val != ret
    7004      4473780 :                 && may_propagate_copy (ret, val))
    7005              :               {
    7006            0 :                 gimple_return_set_retval (ret_stmt, val);
    7007            0 :                 changed = true;
    7008              :               }
    7009              :           }
    7010              :       }
    7011              :       break;
    7012              : 
    7013    759406844 :     default:;
    7014              :     }
    7015              : 
    7016    759406844 :   stmt = gsi_stmt (*gsi);
    7017              : 
    7018    759406844 :   fold_undefer_overflow_warnings (changed && !nowarning, stmt, 0);
    7019    759406844 :   return changed;
    7020              : }
    7021              : 
    7022              : /* Valueziation callback that ends up not following SSA edges.  */
    7023              : 
    7024              : tree
    7025   5764149792 : no_follow_ssa_edges (tree)
    7026              : {
    7027   5764149792 :   return NULL_TREE;
    7028              : }
    7029              : 
    7030              : /* Valueization callback that ends up following single-use SSA edges only.  */
    7031              : 
    7032              : tree
    7033    880636256 : follow_single_use_edges (tree val)
    7034              : {
    7035    880636256 :   if (TREE_CODE (val) == SSA_NAME
    7036    880636256 :       && !has_single_use (val))
    7037    451511679 :     return NULL_TREE;
    7038              :   return val;
    7039              : }
    7040              : 
    7041              : /* Valueization callback that follows all SSA edges.  */
    7042              : 
    7043              : tree
    7044    196119239 : follow_all_ssa_edges (tree val)
    7045              : {
    7046    196119239 :   return val;
    7047              : }
    7048              : 
    7049              : /* Fold the statement pointed to by GSI.  In some cases, this function may
    7050              :    replace the whole statement with a new one.  Returns true iff folding
    7051              :    makes any changes.
    7052              :    The statement pointed to by GSI should be in valid gimple form but may
    7053              :    be in unfolded state as resulting from for example constant propagation
    7054              :    which can produce *&x = 0.  */
    7055              : 
    7056              : bool
    7057    150611557 : fold_stmt (gimple_stmt_iterator *gsi, bitmap dce_bitmap)
    7058              : {
    7059    150611557 :   return fold_stmt_1 (gsi, false, no_follow_ssa_edges, dce_bitmap);
    7060              : }
    7061              : 
    7062              : bool
    7063    605651763 : fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree), bitmap dce_bitmap)
    7064              : {
    7065    605651763 :   return fold_stmt_1 (gsi, false, valueize, dce_bitmap);
    7066              : }
    7067              : 
    7068              : /* Perform the minimal folding on statement *GSI.  Only operations like
    7069              :    *&x created by constant propagation are handled.  The statement cannot
    7070              :    be replaced with a new one.  Return true if the statement was
    7071              :    changed, false otherwise.
    7072              :    The statement *GSI should be in valid gimple form but may
    7073              :    be in unfolded state as resulting from for example constant propagation
    7074              :    which can produce *&x = 0.  */
    7075              : 
    7076              : bool
    7077      3143524 : fold_stmt_inplace (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
    7078              : {
    7079      3143524 :   gimple *stmt = gsi_stmt (*gsi);
    7080      3143524 :   bool changed = fold_stmt_1 (gsi, true, valueize);
    7081      3143524 :   gcc_assert (gsi_stmt (*gsi) == stmt);
    7082      3143524 :   return changed;
    7083              : }
    7084              : 
    7085              : /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
    7086              :    if EXPR is null or we don't know how.
    7087              :    If non-null, the result always has boolean type.  */
    7088              : 
    7089              : static tree
    7090       263347 : canonicalize_bool (tree expr, bool invert)
    7091              : {
    7092       263347 :   if (!expr)
    7093              :     return NULL_TREE;
    7094           50 :   else if (invert)
    7095              :     {
    7096           36 :       if (integer_nonzerop (expr))
    7097            0 :         return boolean_false_node;
    7098           36 :       else if (integer_zerop (expr))
    7099            0 :         return boolean_true_node;
    7100           36 :       else if (TREE_CODE (expr) == SSA_NAME)
    7101            0 :         return fold_build2 (EQ_EXPR, boolean_type_node, expr,
    7102              :                             build_int_cst (TREE_TYPE (expr), 0));
    7103           36 :       else if (COMPARISON_CLASS_P (expr))
    7104           36 :         return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
    7105              :                             boolean_type_node,
    7106              :                             TREE_OPERAND (expr, 0),
    7107              :                             TREE_OPERAND (expr, 1));
    7108              :       else
    7109              :         return NULL_TREE;
    7110              :     }
    7111              :   else
    7112              :     {
    7113           14 :       if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
    7114              :         return expr;
    7115            0 :       if (integer_nonzerop (expr))
    7116            0 :         return boolean_true_node;
    7117            0 :       else if (integer_zerop (expr))
    7118            0 :         return boolean_false_node;
    7119            0 :       else if (TREE_CODE (expr) == SSA_NAME)
    7120            0 :         return fold_build2 (NE_EXPR, boolean_type_node, expr,
    7121              :                             build_int_cst (TREE_TYPE (expr), 0));
    7122            0 :       else if (COMPARISON_CLASS_P (expr))
    7123            0 :         return fold_build2 (TREE_CODE (expr),
    7124              :                             boolean_type_node,
    7125              :                             TREE_OPERAND (expr, 0),
    7126              :                             TREE_OPERAND (expr, 1));
    7127              :       else
    7128              :         return NULL_TREE;
    7129              :     }
    7130              : }
    7131              : 
    7132              : /* Check to see if a boolean expression EXPR is logically equivalent to the
    7133              :    comparison (OP1 CODE OP2).  Check for various identities involving
    7134              :    SSA_NAMEs.  */
    7135              : 
    7136              : static bool
    7137         1454 : same_bool_comparison_p (const_tree expr, enum tree_code code,
    7138              :                         const_tree op1, const_tree op2)
    7139              : {
    7140         1454 :   gimple *s;
    7141              : 
    7142              :   /* The obvious case.  */
    7143         1454 :   if (TREE_CODE (expr) == code
    7144           33 :       && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
    7145         1487 :       && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
    7146              :     return true;
    7147              : 
    7148              :   /* Check for comparing (name, name != 0) and the case where expr
    7149              :      is an SSA_NAME with a definition matching the comparison.  */
    7150         1437 :   if (TREE_CODE (expr) == SSA_NAME
    7151         1437 :       && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
    7152              :     {
    7153            0 :       if (operand_equal_p (expr, op1, 0))
    7154            0 :         return ((code == NE_EXPR && integer_zerop (op2))
    7155            0 :                 || (code == EQ_EXPR && integer_nonzerop (op2)));
    7156            0 :       s = SSA_NAME_DEF_STMT (expr);
    7157            0 :       if (is_gimple_assign (s)
    7158            0 :           && gimple_assign_rhs_code (s) == code
    7159            0 :           && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
    7160            0 :           && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
    7161              :         return true;
    7162              :     }
    7163              : 
    7164              :   /* If op1 is of the form (name != 0) or (name == 0), and the definition
    7165              :      of name is a comparison, recurse.  */
    7166         1437 :   if (TREE_CODE (op1) == SSA_NAME
    7167         1437 :       && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
    7168              :     {
    7169          448 :       s = SSA_NAME_DEF_STMT (op1);
    7170          448 :       if (is_gimple_assign (s)
    7171          448 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
    7172              :         {
    7173            0 :           enum tree_code c = gimple_assign_rhs_code (s);
    7174            0 :           if ((c == NE_EXPR && integer_zerop (op2))
    7175            0 :               || (c == EQ_EXPR && integer_nonzerop (op2)))
    7176            0 :             return same_bool_comparison_p (expr, c,
    7177            0 :                                            gimple_assign_rhs1 (s),
    7178            0 :                                            gimple_assign_rhs2 (s));
    7179            0 :           if ((c == EQ_EXPR && integer_zerop (op2))
    7180            0 :               || (c == NE_EXPR && integer_nonzerop (op2)))
    7181            0 :             return same_bool_comparison_p (expr,
    7182              :                                            invert_tree_comparison (c, false),
    7183            0 :                                            gimple_assign_rhs1 (s),
    7184            0 :                                            gimple_assign_rhs2 (s));
    7185              :         }
    7186              :     }
    7187              :   return false;
    7188              : }
    7189              : 
    7190              : /* Check to see if two boolean expressions OP1 and OP2 are logically
    7191              :    equivalent.  */
    7192              : 
    7193              : static bool
    7194           15 : same_bool_result_p (const_tree op1, const_tree op2)
    7195              : {
    7196              :   /* Simple cases first.  */
    7197           15 :   if (operand_equal_p (op1, op2, 0))
    7198              :     return true;
    7199              : 
    7200              :   /* Check the cases where at least one of the operands is a comparison.
    7201              :      These are a bit smarter than operand_equal_p in that they apply some
    7202              :      identifies on SSA_NAMEs.  */
    7203            8 :   if (COMPARISON_CLASS_P (op2)
    7204           16 :       && same_bool_comparison_p (op1, TREE_CODE (op2),
    7205            8 :                                  TREE_OPERAND (op2, 0),
    7206            8 :                                  TREE_OPERAND (op2, 1)))
    7207              :     return true;
    7208            8 :   if (COMPARISON_CLASS_P (op1)
    7209           16 :       && same_bool_comparison_p (op2, TREE_CODE (op1),
    7210            8 :                                  TREE_OPERAND (op1, 0),
    7211            8 :                                  TREE_OPERAND (op1, 1)))
    7212              :     return true;
    7213              : 
    7214              :   /* Default case.  */
    7215              :   return false;
    7216              : }
    7217              : 
    7218              : /* Forward declarations for some mutually recursive functions.  */
    7219              : 
    7220              : static tree
    7221              : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
    7222              :                    enum tree_code code2, tree op2a, tree op2b, basic_block);
    7223              : static tree
    7224              : and_var_with_comparison (tree type, tree var, bool invert,
    7225              :                          enum tree_code code2, tree op2a, tree op2b,
    7226              :                          basic_block);
    7227              : static tree
    7228              : and_var_with_comparison_1 (tree type, gimple *stmt,
    7229              :                            enum tree_code code2, tree op2a, tree op2b,
    7230              :                            basic_block);
    7231              : static tree
    7232              : or_comparisons_1 (tree, enum tree_code code1, tree op1a, tree op1b,
    7233              :                   enum tree_code code2, tree op2a, tree op2b,
    7234              :                   basic_block);
    7235              : static tree
    7236              : or_var_with_comparison (tree, tree var, bool invert,
    7237              :                         enum tree_code code2, tree op2a, tree op2b,
    7238              :                         basic_block);
    7239              : static tree
    7240              : or_var_with_comparison_1 (tree, gimple *stmt,
    7241              :                           enum tree_code code2, tree op2a, tree op2b,
    7242              :                           basic_block);
    7243              : 
    7244              : /* Helper function for and_comparisons_1:  try to simplify the AND of the
    7245              :    ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
    7246              :    If INVERT is true, invert the value of the VAR before doing the AND.
    7247              :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    7248              : 
    7249              : static tree
    7250       224034 : and_var_with_comparison (tree type, tree var, bool invert,
    7251              :                          enum tree_code code2, tree op2a, tree op2b,
    7252              :                          basic_block outer_cond_bb)
    7253              : {
    7254       224034 :   tree t;
    7255       224034 :   gimple *stmt = SSA_NAME_DEF_STMT (var);
    7256              : 
    7257              :   /* We can only deal with variables whose definitions are assignments.  */
    7258       224034 :   if (!is_gimple_assign (stmt))
    7259              :     return NULL_TREE;
    7260              : 
    7261              :   /* If we have an inverted comparison, apply DeMorgan's law and rewrite
    7262              :      !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
    7263              :      Then we only have to consider the simpler non-inverted cases.  */
    7264       223632 :   if (invert)
    7265        83009 :     t = or_var_with_comparison_1 (type, stmt,
    7266              :                                   invert_tree_comparison (code2, false),
    7267              :                                   op2a, op2b, outer_cond_bb);
    7268              :   else
    7269       140623 :     t = and_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
    7270              :                                    outer_cond_bb);
    7271       223632 :   return canonicalize_bool (t, invert);
    7272              : }
    7273              : 
    7274              : /* Try to simplify the AND of the ssa variable defined by the assignment
    7275              :    STMT with the comparison specified by (OP2A CODE2 OP2B).
    7276              :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    7277              : 
    7278              : static tree
    7279       161240 : and_var_with_comparison_1 (tree type, gimple *stmt,
    7280              :                            enum tree_code code2, tree op2a, tree op2b,
    7281              :                            basic_block outer_cond_bb)
    7282              : {
    7283       161240 :   tree var = gimple_assign_lhs (stmt);
    7284       161240 :   tree true_test_var = NULL_TREE;
    7285       161240 :   tree false_test_var = NULL_TREE;
    7286       161240 :   enum tree_code innercode = gimple_assign_rhs_code (stmt);
    7287              : 
    7288              :   /* Check for identities like (var AND (var == 0)) => false.  */
    7289       161240 :   if (TREE_CODE (op2a) == SSA_NAME
    7290       161240 :       && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
    7291              :     {
    7292         8678 :       if ((code2 == NE_EXPR && integer_zerop (op2b))
    7293        22678 :           || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
    7294              :         {
    7295         8072 :           true_test_var = op2a;
    7296         8072 :           if (var == true_test_var)
    7297              :             return var;
    7298              :         }
    7299         3872 :       else if ((code2 == EQ_EXPR && integer_zerop (op2b))
    7300        14895 :                || (code2 == NE_EXPR && integer_nonzerop (op2b)))
    7301              :         {
    7302         3095 :           false_test_var = op2a;
    7303         3095 :           if (var == false_test_var)
    7304            0 :             return boolean_false_node;
    7305              :         }
    7306              :     }
    7307              : 
    7308              :   /* If the definition is a comparison, recurse on it.  */
    7309       161240 :   if (TREE_CODE_CLASS (innercode) == tcc_comparison)
    7310              :     {
    7311         1942 :       tree t = and_comparisons_1 (type, innercode,
    7312              :                                   gimple_assign_rhs1 (stmt),
    7313              :                                   gimple_assign_rhs2 (stmt),
    7314              :                                   code2,
    7315              :                                   op2a,
    7316              :                                   op2b, outer_cond_bb);
    7317         1942 :       if (t)
    7318              :         return t;
    7319              :     }
    7320              : 
    7321              :   /* If the definition is an AND or OR expression, we may be able to
    7322              :      simplify by reassociating.  */
    7323       161234 :   if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
    7324       161234 :       && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
    7325              :     {
    7326        14437 :       tree inner1 = gimple_assign_rhs1 (stmt);
    7327        14437 :       tree inner2 = gimple_assign_rhs2 (stmt);
    7328        14437 :       gimple *s;
    7329        14437 :       tree t;
    7330        14437 :       tree partial = NULL_TREE;
    7331        14437 :       bool is_and = (innercode == BIT_AND_EXPR);
    7332              : 
    7333              :       /* Check for boolean identities that don't require recursive examination
    7334              :          of inner1/inner2:
    7335              :          inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
    7336              :          inner1 AND (inner1 OR inner2) => inner1
    7337              :          !inner1 AND (inner1 AND inner2) => false
    7338              :          !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
    7339              :          Likewise for similar cases involving inner2.  */
    7340        14437 :       if (inner1 == true_test_var)
    7341            0 :         return (is_and ? var : inner1);
    7342        14437 :       else if (inner2 == true_test_var)
    7343            0 :         return (is_and ? var : inner2);
    7344        14437 :       else if (inner1 == false_test_var)
    7345            0 :         return (is_and
    7346            0 :                 ? boolean_false_node
    7347            0 :                 : and_var_with_comparison (type, inner2, false, code2, op2a,
    7348            0 :                                            op2b, outer_cond_bb));
    7349        14437 :       else if (inner2 == false_test_var)
    7350            0 :         return (is_and
    7351            0 :                 ? boolean_false_node
    7352            0 :                 : and_var_with_comparison (type, inner1, false, code2, op2a,
    7353            0 :                                            op2b, outer_cond_bb));
    7354              : 
    7355              :       /* Next, redistribute/reassociate the AND across the inner tests.
    7356              :          Compute the first partial result, (inner1 AND (op2a code op2b))  */
    7357        14437 :       if (TREE_CODE (inner1) == SSA_NAME
    7358        14437 :           && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
    7359        13644 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    7360        26923 :           && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
    7361              :                                               gimple_assign_rhs1 (s),
    7362              :                                               gimple_assign_rhs2 (s),
    7363              :                                               code2, op2a, op2b,
    7364              :                                               outer_cond_bb)))
    7365              :         {
    7366              :           /* Handle the AND case, where we are reassociating:
    7367              :              (inner1 AND inner2) AND (op2a code2 op2b)
    7368              :              => (t AND inner2)
    7369              :              If the partial result t is a constant, we win.  Otherwise
    7370              :              continue on to try reassociating with the other inner test.  */
    7371           33 :           if (is_and)
    7372              :             {
    7373            5 :               if (integer_onep (t))
    7374              :                 return inner2;
    7375            5 :               else if (integer_zerop (t))
    7376            0 :                 return boolean_false_node;
    7377              :             }
    7378              : 
    7379              :           /* Handle the OR case, where we are redistributing:
    7380              :              (inner1 OR inner2) AND (op2a code2 op2b)
    7381              :              => (t OR (inner2 AND (op2a code2 op2b)))  */
    7382           28 :           else if (integer_onep (t))
    7383            0 :             return boolean_true_node;
    7384              : 
    7385              :           /* Save partial result for later.  */
    7386              :           partial = t;
    7387              :         }
    7388              : 
    7389              :       /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
    7390        14437 :       if (TREE_CODE (inner2) == SSA_NAME
    7391        14437 :           && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
    7392        14058 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    7393        27591 :           && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
    7394              :                                               gimple_assign_rhs1 (s),
    7395              :                                               gimple_assign_rhs2 (s),
    7396              :                                               code2, op2a, op2b,
    7397              :                                               outer_cond_bb)))
    7398              :         {
    7399              :           /* Handle the AND case, where we are reassociating:
    7400              :              (inner1 AND inner2) AND (op2a code2 op2b)
    7401              :              => (inner1 AND t)  */
    7402          475 :           if (is_and)
    7403              :             {
    7404           20 :               if (integer_onep (t))
    7405              :                 return inner1;
    7406           20 :               else if (integer_zerop (t))
    7407            1 :                 return boolean_false_node;
    7408              :               /* If both are the same, we can apply the identity
    7409              :                  (x AND x) == x.  */
    7410           19 :               else if (partial && same_bool_result_p (t, partial))
    7411              :                 return t;
    7412              :             }
    7413              : 
    7414              :           /* Handle the OR case. where we are redistributing:
    7415              :              (inner1 OR inner2) AND (op2a code2 op2b)
    7416              :              => (t OR (inner1 AND (op2a code2 op2b)))
    7417              :              => (t OR partial)  */
    7418              :           else
    7419              :             {
    7420          455 :               if (integer_onep (t))
    7421            0 :                 return boolean_true_node;
    7422          455 :               else if (partial)
    7423              :                 {
    7424              :                   /* We already got a simplification for the other
    7425              :                      operand to the redistributed OR expression.  The
    7426              :                      interesting case is when at least one is false.
    7427              :                      Or, if both are the same, we can apply the identity
    7428              :                      (x OR x) == x.  */
    7429            6 :                   if (integer_zerop (partial))
    7430              :                     return t;
    7431            6 :                   else if (integer_zerop (t))
    7432              :                     return partial;
    7433            4 :                   else if (same_bool_result_p (t, partial))
    7434              :                     return t;
    7435              :                 }
    7436              :             }
    7437              :         }
    7438              :     }
    7439              :   return NULL_TREE;
    7440              : }
    7441              : 
    7442              : /* Try to simplify the AND of two comparisons defined by
    7443              :    (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
    7444              :    If this can be done without constructing an intermediate value,
    7445              :    return the resulting tree; otherwise NULL_TREE is returned.
    7446              :    This function is deliberately asymmetric as it recurses on SSA_DEFs
    7447              :    in the first comparison but not the second.  */
    7448              : 
    7449              : static tree
    7450       828634 : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
    7451              :                    enum tree_code code2, tree op2a, tree op2b,
    7452              :                    basic_block outer_cond_bb)
    7453              : {
    7454       828634 :   tree truth_type = truth_type_for (TREE_TYPE (op1a));
    7455              : 
    7456              :   /* First check for ((x CODE1 y) AND (x CODE2 y)).  */
    7457       828634 :   if (operand_equal_p (op1a, op2a, 0)
    7458       828634 :       && operand_equal_p (op1b, op2b, 0))
    7459              :     {
    7460              :       /* Result will be either NULL_TREE, or a combined comparison.  */
    7461         4185 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    7462              :                                     TRUTH_ANDIF_EXPR, code1, code2,
    7463              :                                     truth_type, op1a, op1b);
    7464         4185 :       if (t)
    7465              :         return t;
    7466              :     }
    7467              : 
    7468              :   /* Likewise the swapped case of the above.  */
    7469       827293 :   if (operand_equal_p (op1a, op2b, 0)
    7470       827293 :       && operand_equal_p (op1b, op2a, 0))
    7471              :     {
    7472              :       /* Result will be either NULL_TREE, or a combined comparison.  */
    7473           28 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    7474              :                                     TRUTH_ANDIF_EXPR, code1,
    7475              :                                     swap_tree_comparison (code2),
    7476              :                                     truth_type, op1a, op1b);
    7477           28 :       if (t)
    7478              :         return t;
    7479              :     }
    7480              : 
    7481              :   /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
    7482              :      NAME's definition is a truth value.  See if there are any simplifications
    7483              :      that can be done against the NAME's definition.  */
    7484       827293 :   if (TREE_CODE (op1a) == SSA_NAME
    7485       827272 :       && (code1 == NE_EXPR || code1 == EQ_EXPR)
    7486      1407473 :       && (integer_zerop (op1b) || integer_onep (op1b)))
    7487              :     {
    7488       118273 :       bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
    7489       301679 :                      || (code1 == NE_EXPR && integer_onep (op1b)));
    7490       277281 :       gimple *stmt = SSA_NAME_DEF_STMT (op1a);
    7491       277281 :       switch (gimple_code (stmt))
    7492              :         {
    7493       221465 :         case GIMPLE_ASSIGN:
    7494              :           /* Try to simplify by copy-propagating the definition.  */
    7495       221465 :           return and_var_with_comparison (type, op1a, invert, code2, op2a,
    7496       221465 :                                           op2b, outer_cond_bb);
    7497              : 
    7498        26393 :         case GIMPLE_PHI:
    7499              :           /* If every argument to the PHI produces the same result when
    7500              :              ANDed with the second comparison, we win.
    7501              :              Do not do this unless the type is bool since we need a bool
    7502              :              result here anyway.  */
    7503        26393 :           if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
    7504              :             {
    7505              :               tree result = NULL_TREE;
    7506              :               unsigned i;
    7507        10382 :               for (i = 0; i < gimple_phi_num_args (stmt); i++)
    7508              :                 {
    7509        10382 :                   tree arg = gimple_phi_arg_def (stmt, i);
    7510              : 
    7511              :                   /* If this PHI has itself as an argument, ignore it.
    7512              :                      If all the other args produce the same result,
    7513              :                      we're still OK.  */
    7514        10382 :                   if (arg == gimple_phi_result (stmt))
    7515            0 :                     continue;
    7516        10382 :                   else if (TREE_CODE (arg) == INTEGER_CST)
    7517              :                     {
    7518         6910 :                       if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
    7519              :                         {
    7520         3844 :                           if (!result)
    7521         1768 :                             result = boolean_false_node;
    7522         2076 :                           else if (!integer_zerop (result))
    7523              :                             return NULL_TREE;
    7524              :                         }
    7525         3066 :                       else if (!result)
    7526         1834 :                         result = fold_build2 (code2, boolean_type_node,
    7527              :                                               op2a, op2b);
    7528         1232 :                       else if (!same_bool_comparison_p (result,
    7529              :                                                         code2, op2a, op2b))
    7530              :                         return NULL_TREE;
    7531              :                     }
    7532         3472 :                   else if (TREE_CODE (arg) == SSA_NAME
    7533         3472 :                            && !SSA_NAME_IS_DEFAULT_DEF (arg))
    7534              :                     {
    7535         3469 :                       tree temp;
    7536         3469 :                       gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
    7537              :                       /* In simple cases we can look through PHI nodes,
    7538              :                          but we have to be careful with loops.
    7539              :                          See PR49073.  */
    7540         3469 :                       if (! dom_info_available_p (CDI_DOMINATORS)
    7541         3469 :                           || gimple_bb (def_stmt) == gimple_bb (stmt)
    7542         6938 :                           || dominated_by_p (CDI_DOMINATORS,
    7543         3469 :                                              gimple_bb (def_stmt),
    7544         3469 :                                              gimple_bb (stmt)))
    7545          900 :                         return NULL_TREE;
    7546         2569 :                       temp = and_var_with_comparison (type, arg, invert, code2,
    7547              :                                                       op2a, op2b,
    7548              :                                                       outer_cond_bb);
    7549         2569 :                       if (!temp)
    7550              :                         return NULL_TREE;
    7551            0 :                       else if (!result)
    7552              :                         result = temp;
    7553            0 :                       else if (!same_bool_result_p (result, temp))
    7554              :                         return NULL_TREE;
    7555              :                     }
    7556              :                   else
    7557              :                     return NULL_TREE;
    7558              :                 }
    7559              :               return result;
    7560              :             }
    7561              : 
    7562              :         default:
    7563              :           break;
    7564              :         }
    7565              :     }
    7566              :   return NULL_TREE;
    7567              : }
    7568              : 
    7569              : static basic_block fosa_bb;
    7570              : static vec<std::pair<tree, flow_sensitive_info_storage> > *fosa_unwind;
    7571              : static tree
    7572     31417308 : follow_outer_ssa_edges (tree val)
    7573              : {
    7574     31417308 :   if (TREE_CODE (val) == SSA_NAME
    7575     31417308 :       && !SSA_NAME_IS_DEFAULT_DEF (val))
    7576              :     {
    7577     30948515 :       basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
    7578     30948515 :       if (!def_bb
    7579      9215184 :           || def_bb == fosa_bb
    7580     36025405 :           || (dom_info_available_p (CDI_DOMINATORS)
    7581      5076890 :               && (def_bb == fosa_bb
    7582      5076890 :                   || dominated_by_p (CDI_DOMINATORS, fosa_bb, def_bb))))
    7583     28276423 :         return val;
    7584              :       /* We cannot temporarily rewrite stmts with undefined overflow
    7585              :          behavior, so avoid expanding them.  */
    7586      5326473 :       if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (val))
    7587       256160 :            || POINTER_TYPE_P (TREE_TYPE (val)))
    7588      5222198 :           && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (val)))
    7589              :         return NULL_TREE;
    7590      1082960 :       flow_sensitive_info_storage storage;
    7591      1082960 :       storage.save_and_clear (val);
    7592              :       /* If the definition does not dominate fosa_bb temporarily reset
    7593              :          flow-sensitive info.  */
    7594      1082960 :       fosa_unwind->safe_push (std::make_pair (val, storage));
    7595      1082960 :       return val;
    7596              :     }
    7597              :   return val;
    7598              : }
    7599              : 
    7600              : /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
    7601              :    : try to simplify the AND/OR of the ssa variable VAR with the comparison
    7602              :    specified by (OP2A CODE2 OP2B) from match.pd.  Return NULL_EXPR if we can't
    7603              :    simplify this to a single expression.  As we are going to lower the cost
    7604              :    of building SSA names / gimple stmts significantly, we need to allocate
    7605              :    them ont the stack.  This will cause the code to be a bit ugly.  */
    7606              : 
    7607              : static tree
    7608       892251 : maybe_fold_comparisons_from_match_pd (tree type, enum tree_code code,
    7609              :                                       enum tree_code code1,
    7610              :                                       tree op1a, tree op1b,
    7611              :                                       enum tree_code code2, tree op2a,
    7612              :                                       tree op2b,
    7613              :                                       basic_block outer_cond_bb)
    7614              : {
    7615              :   /* Allocate gimple stmt1 on the stack.  */
    7616       892251 :   gassign *stmt1
    7617       892251 :     = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
    7618       892251 :   gimple_init (stmt1, GIMPLE_ASSIGN, 3);
    7619       892251 :   gimple_assign_set_rhs_code (stmt1, code1);
    7620       892251 :   gimple_assign_set_rhs1 (stmt1, op1a);
    7621       892251 :   gimple_assign_set_rhs2 (stmt1, op1b);
    7622       892251 :   gimple_set_bb (stmt1, NULL);
    7623              : 
    7624              :   /* Allocate gimple stmt2 on the stack.  */
    7625       892251 :   gassign *stmt2
    7626       892251 :     = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
    7627       892251 :   gimple_init (stmt2, GIMPLE_ASSIGN, 3);
    7628       892251 :   gimple_assign_set_rhs_code (stmt2, code2);
    7629       892251 :   gimple_assign_set_rhs1 (stmt2, op2a);
    7630       892251 :   gimple_assign_set_rhs2 (stmt2, op2b);
    7631       892251 :   gimple_set_bb (stmt2, NULL);
    7632              : 
    7633              :   /* Allocate SSA names(lhs1) on the stack.  */
    7634       892251 :   alignas (tree_node) unsigned char lhs1buf[sizeof (tree_ssa_name)];
    7635       892251 :   tree lhs1 = (tree) &lhs1buf[0];
    7636       892251 :   memset (lhs1, 0, sizeof (tree_ssa_name));
    7637       892251 :   TREE_SET_CODE (lhs1, SSA_NAME);
    7638       892251 :   TREE_TYPE (lhs1) = type;
    7639       892251 :   init_ssa_name_imm_use (lhs1);
    7640              : 
    7641              :   /* Allocate SSA names(lhs2) on the stack.  */
    7642       892251 :   alignas (tree_node) unsigned char lhs2buf[sizeof (tree_ssa_name)];
    7643       892251 :   tree lhs2 = (tree) &lhs2buf[0];
    7644       892251 :   memset (lhs2, 0, sizeof (tree_ssa_name));
    7645       892251 :   TREE_SET_CODE (lhs2, SSA_NAME);
    7646       892251 :   TREE_TYPE (lhs2) = type;
    7647       892251 :   init_ssa_name_imm_use (lhs2);
    7648              : 
    7649       892251 :   gimple_assign_set_lhs (stmt1, lhs1);
    7650       892251 :   gimple_assign_set_lhs (stmt2, lhs2);
    7651              : 
    7652       892251 :   gimple_match_op op (gimple_match_cond::UNCOND, code,
    7653              :                       type, gimple_assign_lhs (stmt1),
    7654       892251 :                       gimple_assign_lhs (stmt2));
    7655       892251 :   fosa_bb = outer_cond_bb;
    7656       892251 :   auto_vec<std::pair<tree, flow_sensitive_info_storage>, 8> unwind_stack;
    7657       892251 :   fosa_unwind = &unwind_stack;
    7658      1239335 :   if (op.resimplify (NULL, (!outer_cond_bb
    7659              :                             ? follow_all_ssa_edges : follow_outer_ssa_edges)))
    7660              :     {
    7661         2430 :       fosa_unwind = NULL;
    7662         8689 :       for (auto p : unwind_stack)
    7663         1399 :         p.second.restore (p.first);
    7664         2430 :       if (gimple_simplified_result_is_gimple_val (&op))
    7665              :         {
    7666         1835 :           tree res = op.ops[0];
    7667         1835 :           if (res == lhs1)
    7668         1467 :             return build2 (code1, type, op1a, op1b);
    7669          368 :           else if (res == lhs2)
    7670          326 :             return build2 (code2, type, op2a, op2b);
    7671              :           else
    7672              :             return res;
    7673              :         }
    7674          595 :       else if (op.code.is_tree_code ()
    7675          595 :                && TREE_CODE_CLASS ((tree_code)op.code) == tcc_comparison)
    7676              :         {
    7677          595 :           tree op0 = op.ops[0];
    7678          595 :           tree op1 = op.ops[1];
    7679          595 :           if (op0 == lhs1 || op0 == lhs2 || op1 == lhs1 || op1 == lhs2)
    7680              :             return NULL_TREE;  /* not simple */
    7681              : 
    7682          595 :           return build2 ((enum tree_code)op.code, op.type, op0, op1);
    7683              :         }
    7684              :     }
    7685       889821 :   fosa_unwind = NULL;
    7686      3751024 :   for (auto p : unwind_stack)
    7687      1081561 :     p.second.restore (p.first);
    7688              : 
    7689       889821 :   return NULL_TREE;
    7690       892251 : }
    7691              : 
    7692              : /* Return TRUE and set op[0] if T, following all SSA edges, is a type
    7693              :    conversion.  Reject loads if LOAD is NULL, otherwise set *LOAD if a
    7694              :    converting load is found.  */
    7695              : 
    7696              : static bool
    7697      1786860 : gimple_convert_def_p (tree t, tree op[1], gimple **load = NULL)
    7698              : {
    7699      1786860 :   bool ret = false;
    7700              : 
    7701      1786860 :   if (TREE_CODE (t) == SSA_NAME
    7702      1786860 :       && !SSA_NAME_IS_DEFAULT_DEF (t))
    7703       988045 :     if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
    7704              :       {
    7705       919623 :         bool load_p = gimple_assign_load_p (def);
    7706       919623 :         if (load_p && !load)
    7707              :           return false;
    7708       904385 :         switch (gimple_assign_rhs_code (def))
    7709              :           {
    7710         9034 :           CASE_CONVERT:
    7711         9034 :             op[0] = gimple_assign_rhs1 (def);
    7712         9034 :             ret = true;
    7713         9034 :             break;
    7714              : 
    7715         2289 :           case VIEW_CONVERT_EXPR:
    7716         2289 :             op[0] = TREE_OPERAND (gimple_assign_rhs1 (def), 0);
    7717         2289 :             ret = true;
    7718         2289 :             break;
    7719              : 
    7720              :           default:
    7721              :             break;
    7722              :           }
    7723              : 
    7724        11323 :         if (ret && load_p)
    7725            0 :           *load = def;
    7726              :       }
    7727              : 
    7728              :   return ret;
    7729              : }
    7730              : 
    7731              : /* Return TRUE and set op[*] if T, following all SSA edges, resolves to a
    7732              :    binary expression with code CODE.  */
    7733              : 
    7734              : static bool
    7735      1808782 : gimple_binop_def_p (enum tree_code code, tree t, tree op[2])
    7736              : {
    7737      1808782 :   if (TREE_CODE (t) == SSA_NAME
    7738      1808782 :       && !SSA_NAME_IS_DEFAULT_DEF (t))
    7739      1008934 :     if (gimple *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
    7740       938771 :       if (gimple_assign_rhs_code (def) == code)
    7741              :         {
    7742        36849 :           op[0] = gimple_assign_rhs1 (def);
    7743        36849 :           op[1] = gimple_assign_rhs2 (def);
    7744        36849 :           return true;
    7745              :         }
    7746              :   return false;
    7747              : }
    7748              : /* Subroutine for fold_truth_andor_1: decode a field reference.
    7749              : 
    7750              :    If *PEXP is a comparison reference, we return the innermost reference.
    7751              : 
    7752              :    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
    7753              :    set to the starting bit number.
    7754              : 
    7755              :    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
    7756              :    otherwise it is not changed.
    7757              : 
    7758              :    *PUNSIGNEDP is set to the signedness of the field.
    7759              : 
    7760              :    *PREVERSEP is set to the storage order of the field.
    7761              : 
    7762              :    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.  If
    7763              :    *PAND_MASK is initially set to a mask with nonzero precision, that mask is
    7764              :    combined with the found mask, or adjusted in precision to match.
    7765              : 
    7766              :    *PSIGNBIT is set to TRUE if, before clipping to *PBITSIZE, the mask
    7767              :    encompassed bits that corresponded to extensions of the sign bit.
    7768              : 
    7769              :    *PXORP is to be FALSE if EXP might be a XOR used in a compare, in which
    7770              :    case, if PXOR_CMP_OP is a zero constant, it will be overridden with *PEXP,
    7771              :    *PXORP will be set to TRUE, *PXOR_AND_MASK will be copied from *PAND_MASK,
    7772              :    and the left-hand operand of the XOR will be decoded.  If *PXORP is TRUE,
    7773              :    PXOR_CMP_OP and PXOR_AND_MASK are supposed to be NULL, and then the
    7774              :    right-hand operand of the XOR will be decoded.
    7775              : 
    7776              :    *LOAD is set to the load stmt of the innermost reference, if any,
    7777              :    *and NULL otherwise.
    7778              : 
    7779              :    LOC[0..3] are filled in as conversion, masking, shifting and loading
    7780              :    operations are located.
    7781              : 
    7782              :    Return 0 if this is not a component reference or is one that we can't
    7783              :    do anything with.  */
    7784              : 
    7785              : static tree
    7786       640325 : decode_field_reference (tree *pexp, HOST_WIDE_INT *pbitsize,
    7787              :                         HOST_WIDE_INT *pbitpos,
    7788              :                         bool *punsignedp, bool *preversep, bool *pvolatilep,
    7789              :                         wide_int *pand_mask, bool *psignbit,
    7790              :                         bool *pxorp, tree *pxor_cmp_op, wide_int *pxor_and_mask,
    7791              :                         gimple **pload, location_t loc[4])
    7792              : {
    7793       640325 :   tree exp = *pexp;
    7794       640325 :   tree outer_type = 0;
    7795       640325 :   wide_int and_mask;
    7796       640325 :   tree inner, offset;
    7797       640325 :   int shiftrt = 0;
    7798       640325 :   tree res_ops[2];
    7799       640325 :   machine_mode mode;
    7800       640325 :   bool convert_before_shift = false;
    7801       640325 :   bool signbit = false;
    7802       640325 :   bool xorp = false;
    7803       640325 :   tree xor_cmp_op;
    7804       640325 :   wide_int xor_and_mask;
    7805       640325 :   gimple *load = NULL;
    7806              : 
    7807              :   /* All the optimizations using this function assume integer fields.
    7808              :      There are problems with FP fields since the type_for_size call
    7809              :      below can fail for, e.g., XFmode.  */
    7810       640325 :   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
    7811              :     return NULL_TREE;
    7812              : 
    7813              :   /* Drop casts, saving only the outermost type, effectively used in
    7814              :      the compare.  We can deal with at most one conversion, and it may
    7815              :      appear at various points in the chain of recognized preparation
    7816              :      statements.  Earlier optimizers will often have already dropped
    7817              :      unneeded extensions, but they may survive, as in PR118046.  ???
    7818              :      Can we do better and allow multiple conversions, perhaps taking
    7819              :      note of the narrowest intermediate type, sign extensions and
    7820              :      whatnot?  */
    7821       602928 :   if (!outer_type && gimple_convert_def_p (exp, res_ops))
    7822              :     {
    7823        10607 :       outer_type = TREE_TYPE (exp);
    7824        10607 :       loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7825        10607 :       exp = res_ops[0];
    7826              :     }
    7827              : 
    7828              :   /* Recognize and save a masking operation.  Combine it with an
    7829              :      incoming mask.  */
    7830       602928 :   if (gimple_binop_def_p (BIT_AND_EXPR, exp, res_ops)
    7831       602928 :       && TREE_CODE (res_ops[1]) == INTEGER_CST)
    7832              :     {
    7833        22569 :       loc[1] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7834        22569 :       exp = res_ops[0];
    7835        22569 :       and_mask = wi::to_wide (res_ops[1]);
    7836        22569 :       unsigned prec_in = pand_mask->get_precision ();
    7837        22569 :       if (prec_in)
    7838              :         {
    7839           52 :           unsigned prec_op = and_mask.get_precision ();
    7840           52 :           if (prec_in >= prec_op)
    7841              :             {
    7842           52 :               if (prec_in > prec_op)
    7843            0 :                 and_mask = wide_int::from (and_mask, prec_in, UNSIGNED);
    7844           52 :               and_mask &= *pand_mask;
    7845              :             }
    7846              :           else
    7847            0 :             and_mask &= wide_int::from (*pand_mask, prec_op, UNSIGNED);
    7848              :         }
    7849              :     }
    7850              :   else
    7851       580359 :     and_mask = *pand_mask;
    7852              : 
    7853              :   /* Turn (a ^ b) [!]= 0 into a [!]= b.  */
    7854       602928 :   if (pxorp && gimple_binop_def_p (BIT_XOR_EXPR, exp, res_ops))
    7855              :     {
    7856              :       /* No location recorded for this one, it's entirely subsumed by the
    7857              :          compare.  */
    7858         8348 :       if (*pxorp)
    7859              :         {
    7860         4171 :           exp = res_ops[1];
    7861         4171 :           gcc_checking_assert (!pxor_cmp_op && !pxor_and_mask);
    7862              :         }
    7863         4177 :       else if (!pxor_cmp_op)
    7864              :         /* Not much we can do when xor appears in the right-hand compare
    7865              :            operand.  */
    7866              :         return NULL_TREE;
    7867         4175 :       else if (integer_zerop (*pxor_cmp_op))
    7868              :         {
    7869         4171 :           xorp = true;
    7870         4171 :           exp = res_ops[0];
    7871         4171 :           xor_cmp_op = *pexp;
    7872         4171 :           xor_and_mask = *pand_mask;
    7873              :         }
    7874              :     }
    7875              : 
    7876              :   /* Another chance to drop conversions.  */
    7877       602926 :   if (!outer_type && gimple_convert_def_p (exp, res_ops))
    7878              :     {
    7879          706 :       outer_type = TREE_TYPE (exp);
    7880          706 :       loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7881          706 :       exp = res_ops[0];
    7882              :     }
    7883              : 
    7884              :   /* Take note of shifts.  */
    7885       602926 :   if (gimple_binop_def_p (RSHIFT_EXPR, exp, res_ops)
    7886       602926 :       && TREE_CODE (res_ops[1]) == INTEGER_CST)
    7887              :     {
    7888          258 :       loc[2] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7889          258 :       exp = res_ops[0];
    7890          258 :       if (!tree_fits_shwi_p (res_ops[1]))
    7891              :         return NULL_TREE;
    7892          258 :       shiftrt = tree_to_shwi (res_ops[1]);
    7893          258 :       if (shiftrt <= 0)
    7894              :         return NULL_TREE;
    7895              :     }
    7896              : 
    7897              :   /* Yet another chance to drop conversions.  This one is allowed to
    7898              :      match a converting load, subsuming the load identification block
    7899              :      below.  */
    7900       602926 :   if (!outer_type && gimple_convert_def_p (exp, res_ops, &load))
    7901              :     {
    7902           10 :       outer_type = TREE_TYPE (exp);
    7903           10 :       loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
    7904           10 :       if (load)
    7905            0 :         loc[3] = gimple_location (load);
    7906           10 :       exp = res_ops[0];
    7907              :       /* This looks backwards, but we're going back the def chain, so if we
    7908              :          find the conversion here, after finding a shift, that's because the
    7909              :          convert appears before the shift, and we should thus adjust the bit
    7910              :          pos and size because of the shift after adjusting it due to type
    7911              :          conversion.  */
    7912           10 :       convert_before_shift = true;
    7913              :     }
    7914              : 
    7915              :   /* Identify the load, if there is one.  */
    7916       602926 :   if (!load && TREE_CODE (exp) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (exp))
    7917              :     {
    7918       335719 :       gimple *def = SSA_NAME_DEF_STMT (exp);
    7919       335719 :       if (gimple_assign_load_p (def))
    7920              :         {
    7921       213589 :           loc[3] = gimple_location (def);
    7922       213589 :           load = def;
    7923       213589 :           exp = gimple_assign_rhs1 (def);
    7924              :         }
    7925              :     }
    7926              : 
    7927              :   /* Identify the relevant bits.  */
    7928       602926 :   poly_int64 poly_bitsize, poly_bitpos;
    7929       602926 :   int unsignedp, reversep = *preversep, volatilep = *pvolatilep;
    7930       602926 :   inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
    7931              :                                &mode, &unsignedp, &reversep, &volatilep);
    7932              : 
    7933       602926 :   HOST_WIDE_INT bs, bp;
    7934       602926 :   if (!poly_bitsize.is_constant (&bs)
    7935       602926 :       || !poly_bitpos.is_constant (&bp)
    7936       602926 :       || bs <= shiftrt
    7937       602926 :       || offset != 0
    7938       601691 :       || TREE_CODE (inner) == PLACEHOLDER_EXPR
    7939              :       /* Reject out-of-bound accesses (PR79731, PR118514).  */
    7940       601691 :       || !access_in_bounds_of_type_p (TREE_TYPE (inner), bs, bp)
    7941       601665 :       || (INTEGRAL_TYPE_P (TREE_TYPE (inner))
    7942       413834 :           && !type_has_mode_precision_p (TREE_TYPE (inner))))
    7943        28836 :     return NULL_TREE;
    7944              : 
    7945              :   /* Adjust shifts...  */
    7946       574090 :   if (convert_before_shift
    7947       574090 :       && outer_type && bs > TYPE_PRECISION (outer_type))
    7948              :     {
    7949            3 :       HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
    7950            3 :       if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    7951            0 :         bp += excess;
    7952              :       bs -= excess;
    7953              :     }
    7954              : 
    7955       574090 :   if (shiftrt)
    7956              :     {
    7957              :       /* Punt if we're shifting by more than the loaded bitfield (after
    7958              :          adjustment), or if there's a shift after a change of signedness, punt.
    7959              :          When comparing this field with a constant, we'll check that the
    7960              :          constant is a proper sign- or zero-extension (depending on signedness)
    7961              :          of a value that would fit in the selected portion of the bitfield.  A
    7962              :          shift after a change of signedness would make the extension
    7963              :          non-uniform, and we can't deal with that (yet ???).  See
    7964              :          gcc.dg/field-merge-22.c for a test that would go wrong.  */
    7965          258 :       if (bs <= shiftrt
    7966          258 :           || (convert_before_shift
    7967           10 :               && outer_type && unsignedp != TYPE_UNSIGNED (outer_type)))
    7968              :         return NULL_TREE;
    7969          250 :       if (!reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    7970          250 :         bp += shiftrt;
    7971          250 :       bs -= shiftrt;
    7972              :     }
    7973              : 
    7974              :   /* ... and bit position.  */
    7975       574082 :   if (!convert_before_shift
    7976       574082 :       && outer_type && bs > TYPE_PRECISION (outer_type))
    7977              :     {
    7978         5202 :       HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
    7979         5202 :       if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    7980            0 :         bp += excess;
    7981              :       bs -= excess;
    7982              :     }
    7983              : 
    7984              :   /* If the number of bits in the reference is the same as the bitsize of
    7985              :      the outer type, then the outer type gives the signedness. Otherwise
    7986              :      (in case of a small bitfield) the signedness is unchanged.  */
    7987       574082 :   if (outer_type && bs == TYPE_PRECISION (outer_type))
    7988         8826 :     unsignedp = TYPE_UNSIGNED (outer_type);
    7989              : 
    7990              :   /* Make the mask the expected width.  */
    7991       574082 :   if (and_mask.get_precision () != 0)
    7992              :     {
    7993              :       /* If the AND_MASK encompasses bits that would be extensions of
    7994              :          the sign bit, set SIGNBIT.  */
    7995        26298 :       if (!unsignedp
    7996         2629 :           && and_mask.get_precision () > bs
    7997        28969 :           && (and_mask & wi::mask (bs, true, and_mask.get_precision ())) != 0)
    7998              :         signbit = true;
    7999        26298 :       and_mask = wide_int::from (and_mask, bs, UNSIGNED);
    8000              :     }
    8001              : 
    8002       574082 :   *pexp = exp;
    8003       574082 :   *pload = load;
    8004       574082 :   *pbitsize = bs;
    8005       574082 :   *pbitpos = bp;
    8006       574082 :   *punsignedp = unsignedp;
    8007       574082 :   *preversep = reversep;
    8008       574082 :   *pvolatilep = volatilep;
    8009       574082 :   *psignbit = signbit;
    8010       574082 :   *pand_mask = and_mask;
    8011       574082 :   if (xorp)
    8012              :     {
    8013         4171 :       *pxorp = xorp;
    8014         4171 :       *pxor_cmp_op = xor_cmp_op;
    8015         4171 :       *pxor_and_mask = xor_and_mask;
    8016              :     }
    8017              : 
    8018              :   return inner;
    8019       640325 : }
    8020              : 
    8021              : /* Return the one bitpos within bit extents L or R that is at an
    8022              :    ALIGN-bit alignment boundary, or -1 if there is more than one such
    8023              :    boundary, if there isn't any, or if there is any such boundary
    8024              :    between the extents.  L and R are given by bitpos and bitsize.  If
    8025              :    it doesn't return -1, there are two consecutive ALIGN-bit words
    8026              :    that contain both extents, and at least one of the extents
    8027              :    straddles across the returned alignment boundary.  */
    8028              : 
    8029              : static inline HOST_WIDE_INT
    8030        28176 : compute_split_boundary_from_align (HOST_WIDE_INT align,
    8031              :                                    HOST_WIDE_INT l_bitpos,
    8032              :                                    HOST_WIDE_INT l_bitsize,
    8033              :                                    HOST_WIDE_INT r_bitpos,
    8034              :                                    HOST_WIDE_INT r_bitsize)
    8035              : {
    8036        28176 :   HOST_WIDE_INT amask = ~(align - 1);
    8037              : 
    8038        28176 :   HOST_WIDE_INT first_bit = MIN (l_bitpos, r_bitpos);
    8039        28176 :   HOST_WIDE_INT end_bit = MAX (l_bitpos + l_bitsize, r_bitpos + r_bitsize);
    8040              : 
    8041        28176 :   HOST_WIDE_INT boundary = (end_bit - 1) & amask;
    8042              : 
    8043              :   /* Make sure we're crossing no more than one alignment boundary.
    8044              : 
    8045              :      ??? We don't have logic to recombine loads of two adjacent
    8046              :      fields that each crosses a different alignment boundary, so
    8047              :      as to load the middle word only once, if other words can't be
    8048              :      otherwise recombined.  */
    8049        28176 :   if (boundary - first_bit > align)
    8050              :     return -1;
    8051              : 
    8052        10894 :   HOST_WIDE_INT l_start_word = l_bitpos & amask;
    8053        10894 :   HOST_WIDE_INT l_end_word = (l_bitpos + l_bitsize - 1) & amask;
    8054              : 
    8055        10894 :   HOST_WIDE_INT r_start_word = r_bitpos & amask;
    8056        10894 :   HOST_WIDE_INT r_end_word = (r_bitpos + r_bitsize - 1) & amask;
    8057              : 
    8058              :   /* If neither field straddles across an alignment boundary, it's no
    8059              :      use to even try to merge them.  */
    8060        10894 :   if (l_start_word == l_end_word && r_start_word == r_end_word)
    8061        10587 :     return -1;
    8062              : 
    8063              :   return boundary;
    8064              : }
    8065              : 
    8066              : /* Make a bit_field_ref.  If POINT is NULL, return the BIT_FIELD_REF.
    8067              :    Otherwise, build and insert a load stmt before POINT, and return
    8068              :    the SSA_NAME.  ???  Rewrite LOAD in terms of the bitfield?  */
    8069              : 
    8070              : static tree
    8071         4509 : make_bit_field_load (location_t loc, tree inner, tree orig_inner, tree type,
    8072              :                      HOST_WIDE_INT bitsize, poly_int64 bitpos,
    8073              :                      bool unsignedp, bool reversep, gimple *point)
    8074              : {
    8075         4509 :   if (point && loc == UNKNOWN_LOCATION)
    8076           18 :     loc = gimple_location (point);
    8077              : 
    8078         4509 :   tree ref = make_bit_field_ref (loc, unshare_expr (inner),
    8079              :                                  unshare_expr (orig_inner),
    8080              :                                  type, bitsize, bitpos,
    8081              :                                  unsignedp, reversep);
    8082         4509 :   if (!point)
    8083              :     return ref;
    8084              : 
    8085              :   /* If we're remaking the same load, reuse the SSA NAME it is already loaded
    8086              :      into.  */
    8087         4358 :   if (gimple_assign_load_p (point)
    8088         4358 :       && operand_equal_p (ref, gimple_assign_rhs1 (point)))
    8089              :     {
    8090         1657 :       gcc_checking_assert (TREE_CODE (gimple_assign_lhs (point)) == SSA_NAME);
    8091              :       return gimple_assign_lhs (point);
    8092              :     }
    8093              : 
    8094         2701 :   gimple_seq stmts = NULL;
    8095         2701 :   tree ret = force_gimple_operand (ref, &stmts, true, NULL_TREE);
    8096              : 
    8097              :   /* We know the vuse is supposed to end up being the same as that at the
    8098              :      original load at the insertion point, but if we don't set it, it will be a
    8099              :      generic placeholder that only the global SSA update at the end of the pass
    8100              :      would make equal, too late for us to use in further combinations.  So go
    8101              :      ahead and copy the vuse.  */
    8102              : 
    8103         2701 :   tree reaching_vuse = gimple_vuse (point);
    8104         2701 :   for (gimple_stmt_iterator i = gsi_start (stmts);
    8105         5812 :        !gsi_end_p (i); gsi_next (&i))
    8106              :     {
    8107         3111 :       gimple *new_stmt = gsi_stmt (i);
    8108         6222 :       if (gimple_has_mem_ops (new_stmt))
    8109         3111 :         gimple_set_vuse (new_stmt, reaching_vuse);
    8110              :     }
    8111              : 
    8112         2701 :   gimple_stmt_iterator gsi = gsi_for_stmt (point);
    8113         2701 :   gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
    8114         2701 :   return ret;
    8115              : }
    8116              : 
    8117              : /* Initialize ln_arg[0] and ln_arg[1] to a pair of newly-created (at
    8118              :    LOC) loads from INNER (from ORIG_INNER), of modes MODE and MODE2,
    8119              :    respectively, starting at BIT_POS, using reversed endianness if
    8120              :    REVERSEP.  Also initialize BITPOS (the starting position of each
    8121              :    part into INNER), BITSIZ (the bit count starting at BITPOS),
    8122              :    TOSHIFT[1] (the amount by which the part and its mask are to be
    8123              :    shifted right to bring its least-significant bit to bit zero) and
    8124              :    SHIFTED (the amount by which the part, by separate loading, has
    8125              :    already been shifted right, but that the mask needs shifting to
    8126              :    match).  */
    8127              : 
    8128              : static inline void
    8129          307 : build_split_load (tree /* out */ ln_arg[2],
    8130              :                   HOST_WIDE_INT /* out */ bitpos[2],
    8131              :                   HOST_WIDE_INT /* out */ bitsiz[2],
    8132              :                   HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
    8133              :                   HOST_WIDE_INT /* out */ shifted[2],
    8134              :                   location_t loc, tree inner, tree orig_inner,
    8135              :                   scalar_int_mode mode, scalar_int_mode mode2,
    8136              :                   HOST_WIDE_INT bit_pos, bool reversep,
    8137              :                   gimple *point[2])
    8138              : {
    8139          307 :   scalar_int_mode modes[2] = { mode, mode2 };
    8140          307 :   bitsiz[0] = GET_MODE_BITSIZE (mode);
    8141          307 :   bitsiz[1] = GET_MODE_BITSIZE (mode2);
    8142              : 
    8143          921 :   for (int i = 0; i < 2; i++)
    8144              :     {
    8145          614 :       tree type = lang_hooks.types.type_for_mode (modes[i], 1);
    8146          614 :       if (!type)
    8147              :         {
    8148            0 :           type = build_nonstandard_integer_type (bitsiz[0], 1);
    8149            0 :           gcc_assert (type);
    8150              :         }
    8151          614 :       bitpos[i] = bit_pos;
    8152         1228 :       ln_arg[i] = make_bit_field_load (loc, inner, orig_inner,
    8153          614 :                                        type, bitsiz[i],
    8154          614 :                                        bit_pos, 1, reversep, point[i]);
    8155          614 :       bit_pos += bitsiz[i];
    8156              :     }
    8157              : 
    8158          307 :   toshift[1] = toshift[0];
    8159          307 :   if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8160              :     {
    8161            3 :       shifted[0] = bitsiz[1];
    8162            3 :       shifted[1] = 0;
    8163            3 :       toshift[0] = 0;
    8164              :     }
    8165              :   else
    8166              :     {
    8167          304 :       shifted[1] = bitsiz[0];
    8168          304 :       shifted[0] = 0;
    8169          304 :       toshift[1] = 0;
    8170              :     }
    8171          307 : }
    8172              : 
    8173              : /* Make arrangements to split at bit BOUNDARY a single loaded word
    8174              :    (with REVERSEP bit order) LN_ARG[0], to be shifted right by
    8175              :    TOSHIFT[0] to bring the field of interest to the least-significant
    8176              :    bit.  The expectation is that the same loaded word will be
    8177              :    propagated from part 0 to part 1, with just different shifting and
    8178              :    masking to extract both parts.  MASK is not expected to do more
    8179              :    than masking out the bits that belong to the other part.  See
    8180              :    build_split_load for more information on the other fields.  */
    8181              : 
    8182              : static inline void
    8183           51 : reuse_split_load (tree /* in[0] out[1] */ ln_arg[2],
    8184              :                   HOST_WIDE_INT /* in[0] out[1] */ bitpos[2],
    8185              :                   HOST_WIDE_INT /* in[0] out[1] */ bitsiz[2],
    8186              :                   HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
    8187              :                   HOST_WIDE_INT /* out */ shifted[2],
    8188              :                   wide_int /* out */ mask[2],
    8189              :                   HOST_WIDE_INT boundary, bool reversep)
    8190              : {
    8191           51 :   unsigned prec = TYPE_PRECISION (TREE_TYPE (ln_arg[0]));
    8192              : 
    8193           51 :   ln_arg[1] = ln_arg[0];
    8194           51 :   bitpos[1] = bitpos[0];
    8195           51 :   bitsiz[1] = bitsiz[0];
    8196           51 :   shifted[1] = shifted[0] = 0;
    8197              : 
    8198           51 :   if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8199              :     {
    8200            3 :       toshift[1] = toshift[0];
    8201            3 :       toshift[0] = bitpos[0] + bitsiz[0] - boundary;
    8202            3 :       mask[0] = wi::mask (toshift[0], true, prec);
    8203            3 :       mask[1] = wi::mask (toshift[0], false, prec);
    8204              :     }
    8205              :   else
    8206              :     {
    8207           48 :       toshift[1] = boundary - bitpos[1];
    8208           48 :       mask[1] = wi::mask (toshift[1], true, prec);
    8209           48 :       mask[0] = wi::mask (toshift[1], false, prec);
    8210              :     }
    8211           51 : }
    8212              : 
    8213              : /* Find ways of folding logical expressions of LHS and RHS:
    8214              : 
    8215              :    Try to merge two comparisons to nearby fields.
    8216              : 
    8217              :    For example, if we have p->a == 2 && p->b == 4 and we can load both A and B
    8218              :    at once, we can do this with a comparison against the object ANDed with the
    8219              :    a mask.
    8220              : 
    8221              :    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
    8222              :    operations to do this with one comparison, loading both fields from P at
    8223              :    once, and likewise from Q.
    8224              : 
    8225              :    Herein, loading at once means loading from within the same alignment
    8226              :    boundary for the enclosing object.  If (packed) fields cross such alignment
    8227              :    boundaries, we may still recombine the compares, so that loads do not cross
    8228              :    the boundaries.
    8229              : 
    8230              :    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
    8231              :    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
    8232              : 
    8233              :    TRUTH_TYPE is the type of the logical operand.
    8234              : 
    8235              :    LHS is denoted as LL_ARG LCODE LR_ARG.
    8236              : 
    8237              :    RHS is denoted as RL_ARG RCODE RR_ARG.
    8238              : 
    8239              :    LHS is assumed to dominate RHS.
    8240              : 
    8241              :    Combined loads are inserted next to preexisting loads, once we determine
    8242              :    that the combination is viable, and the combined condition references new
    8243              :    SSA_NAMEs that hold the loaded values.  Since the original loads are
    8244              :    verified to have the same gimple_vuse, the insertion point doesn't matter
    8245              :    for correctness.  ??? The loads may be a lot earlier than the compares, and
    8246              :    it's conceivable that one or two loads for RHS appear before those for LHS.
    8247              :    It could be advantageous to try to place the loads optimally, taking
    8248              :    advantage of knowing whether RHS is accessed before LHS, or that both are
    8249              :    accessed before both compares, but we don't do that (yet?).
    8250              : 
    8251              :    SEPARATEP should be NULL if the combined condition must be returned as a
    8252              :    single expression, even if it is a compound condition.  This must only be
    8253              :    done if LHS and RHS are adjacent, without intervening conditions, and the
    8254              :    combined condition is to replace RHS, while LHS is dropped altogether.
    8255              : 
    8256              :    Otherwise, SEPARATEP must be a non-NULL pointer to a NULL_TREE, that may be
    8257              :    replaced by a part of the compound condition that could replace RHS, while
    8258              :    the returned expression replaces LHS.  This works whether or not LHS and RHS
    8259              :    are adjacent, as long as there aren't VDEFs or other side effects between
    8260              :    them.
    8261              : 
    8262              :    If the "words" accessed by RHS are already accessed by LHS, this won't
    8263              :    matter, but if RHS accesses "words" that LHS doesn't, then *SEPARATEP will
    8264              :    be set to the compares that should take RHS's place.  By "words" we mean
    8265              :    contiguous bits that do not cross a an TYPE_ALIGN boundary of the accessed
    8266              :    object's type.
    8267              : 
    8268              :    We return the simplified tree or 0 if no optimization is possible.  */
    8269              : 
    8270              : tree
    8271       260108 : fold_truth_andor_for_ifcombine (enum tree_code code, tree truth_type,
    8272              :                                 location_t lloc, enum tree_code lcode,
    8273              :                                 tree ll_arg, tree lr_arg,
    8274              :                                 location_t rloc, enum tree_code rcode,
    8275              :                                 tree rl_arg, tree rr_arg,
    8276              :                                 tree *separatep)
    8277              : {
    8278              :   /* If this is the "or" of two comparisons, we can do something if
    8279              :      the comparisons are NE_EXPR.  If this is the "and", we can do something
    8280              :      if the comparisons are EQ_EXPR.  I.e.,
    8281              :         (a->b == 2 && a->c == 4) can become (a->new == NEW).
    8282              : 
    8283              :      WANTED_CODE is this operation code.  For single bit fields, we can
    8284              :      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
    8285              :      comparison for one-bit fields.  */
    8286              : 
    8287       260108 :   enum tree_code orig_code = code;
    8288       260108 :   enum tree_code wanted_code;
    8289       260108 :   tree ll_inner, lr_inner, rl_inner, rr_inner;
    8290       260108 :   gimple *ll_load, *lr_load, *rl_load, *rr_load;
    8291       260108 :   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
    8292       260108 :   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
    8293       260108 :   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
    8294       260108 :   HOST_WIDE_INT lnbitsize, lnbitpos, lnprec;
    8295       260108 :   HOST_WIDE_INT rnbitsize, rnbitpos, rnprec;
    8296       260108 :   bool ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
    8297       260108 :   bool ll_reversep, lr_reversep, rl_reversep, rr_reversep;
    8298       260108 :   bool ll_signbit, lr_signbit, rl_signbit, rr_signbit;
    8299       260108 :   scalar_int_mode lnmode, lnmode2, rnmode;
    8300       260108 :   wide_int ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
    8301       260108 :   wide_int l_const, r_const;
    8302       260108 :   tree lntype, rntype, result;
    8303       260108 :   HOST_WIDE_INT first_bit, end_bit;
    8304       260108 :   bool volatilep;
    8305       260108 :   bool l_split_load;
    8306              : 
    8307              :   /* These are indexed by: conv, mask, shft, load.  */
    8308       260108 :   location_t ll_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
    8309       260108 :   location_t lr_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
    8310       260108 :   location_t rl_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
    8311       260108 :   location_t rr_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
    8312              : 
    8313       260108 :   gcc_checking_assert (!separatep || !*separatep);
    8314              : 
    8315              :   /* Start by getting the comparison codes.  Fail if anything is volatile.
    8316              :      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
    8317              :      it were surrounded with a NE_EXPR.  */
    8318              : 
    8319       260108 :   if (TREE_CODE_CLASS (lcode) != tcc_comparison
    8320       260108 :       || TREE_CODE_CLASS (rcode) != tcc_comparison)
    8321              :     return 0;
    8322              : 
    8323              :   /* We don't normally find TRUTH_*IF_EXPR in gimple, but these codes may be
    8324              :      given by our caller to denote conditions from different blocks.  */
    8325       260108 :   switch (code)
    8326              :     {
    8327              :     case TRUTH_AND_EXPR:
    8328              :     case TRUTH_ANDIF_EXPR:
    8329              :       code = TRUTH_AND_EXPR;
    8330              :       break;
    8331              : 
    8332            0 :     case TRUTH_OR_EXPR:
    8333            0 :     case TRUTH_ORIF_EXPR:
    8334            0 :       code = TRUTH_OR_EXPR;
    8335            0 :       break;
    8336              : 
    8337              :     default:
    8338              :       return 0;
    8339              :     }
    8340              : 
    8341              :   /* Prepare to turn compares of signed quantities with zero into sign-bit
    8342              :      tests.  We need not worry about *_reversep here for these compare
    8343              :      rewrites: loads will have already been reversed before compares.  Save the
    8344              :      precision, because [lr]l_arg may change and we won't be able to tell how
    8345              :      wide it was originally.  */
    8346       260108 :   unsigned lsignbit = 0, rsignbit = 0;
    8347       260108 :   if ((lcode == LT_EXPR || lcode == GE_EXPR)
    8348        11516 :       && integer_zerop (lr_arg)
    8349         3204 :       && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
    8350       263312 :       && !TYPE_UNSIGNED (TREE_TYPE (ll_arg)))
    8351              :     {
    8352         3204 :       lsignbit = TYPE_PRECISION (TREE_TYPE (ll_arg));
    8353         3204 :       lcode = (lcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
    8354              :     }
    8355              :   /* Turn compares of unsigned quantities with powers of two into
    8356              :      equality tests of masks.  */
    8357       256904 :   else if ((lcode == LT_EXPR || lcode == GE_EXPR)
    8358         8312 :            && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
    8359         7647 :            && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
    8360         5712 :            && TREE_CODE (lr_arg) == INTEGER_CST
    8361       256904 :            && wi::popcount (wi::to_wide (lr_arg)) == 1)
    8362              :     {
    8363            0 :       ll_and_mask = ~(wi::to_wide (lr_arg) - 1);
    8364            0 :       lcode = (lcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
    8365            0 :       lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
    8366              :     }
    8367              :   /* Turn compares of unsigned quantities with powers of two minus one
    8368              :      into equality tests of masks.  */
    8369       513808 :   else if ((lcode == LE_EXPR || lcode == GT_EXPR)
    8370        27305 :            && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
    8371        27156 :            && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
    8372        22314 :            && TREE_CODE (lr_arg) == INTEGER_CST
    8373       541113 :            && wi::popcount (wi::to_wide (lr_arg) + 1) == 1)
    8374              :     {
    8375         3116 :       ll_and_mask = ~wi::to_wide (lr_arg);
    8376         3116 :       lcode = (lcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
    8377         3116 :       lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
    8378              :     }
    8379              :   /* Likewise for the second compare.  */
    8380       260108 :   if ((rcode == LT_EXPR || rcode == GE_EXPR)
    8381        18381 :       && integer_zerop (rr_arg)
    8382         1730 :       && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
    8383       261838 :       && !TYPE_UNSIGNED (TREE_TYPE (rl_arg)))
    8384              :     {
    8385         1730 :       rsignbit = TYPE_PRECISION (TREE_TYPE (rl_arg));
    8386         1730 :       rcode = (rcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
    8387              :     }
    8388       258378 :   else if ((rcode == LT_EXPR || rcode == GE_EXPR)
    8389        16651 :            && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
    8390        15683 :            && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
    8391         2697 :            && TREE_CODE (rr_arg) == INTEGER_CST
    8392       258378 :            && wi::popcount (wi::to_wide (rr_arg)) == 1)
    8393              :     {
    8394            0 :       rl_and_mask = ~(wi::to_wide (rr_arg) - 1);
    8395            0 :       rcode = (rcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
    8396            0 :       rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
    8397              :     }
    8398       516756 :   else if ((rcode == LE_EXPR || rcode == GT_EXPR)
    8399        37551 :            && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
    8400        37327 :            && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
    8401        27727 :            && TREE_CODE (rr_arg) == INTEGER_CST
    8402       554307 :            && wi::popcount (wi::to_wide (rr_arg) + 1) == 1)
    8403              :     {
    8404         4693 :       rl_and_mask = ~wi::to_wide (rr_arg);
    8405         4693 :       rcode = (rcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
    8406         4693 :       rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
    8407              :     }
    8408              : 
    8409              :   /* See if the comparisons can be merged.  Then get all the parameters for
    8410              :      each side.  */
    8411              : 
    8412       260108 :   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
    8413       226817 :       || (rcode != EQ_EXPR && rcode != NE_EXPR))
    8414              :     return 0;
    8415              : 
    8416       203733 :   ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
    8417       203733 :   volatilep = 0;
    8418       203733 :   bool l_xor = false, r_xor = false;
    8419       203733 :   ll_inner = decode_field_reference (&ll_arg, &ll_bitsize, &ll_bitpos,
    8420              :                                      &ll_unsignedp, &ll_reversep, &volatilep,
    8421              :                                      &ll_and_mask, &ll_signbit,
    8422              :                                      &l_xor, &lr_arg, &lr_and_mask,
    8423              :                                      &ll_load, ll_loc);
    8424       203733 :   if (!ll_inner)
    8425              :     return 0;
    8426       150620 :   lr_inner = decode_field_reference (&lr_arg, &lr_bitsize, &lr_bitpos,
    8427              :                                      &lr_unsignedp, &lr_reversep, &volatilep,
    8428              :                                      &lr_and_mask, &lr_signbit, &l_xor, 0, 0,
    8429              :                                      &lr_load, lr_loc);
    8430       150620 :   if (!lr_inner)
    8431              :     return 0;
    8432       147757 :   rl_inner = decode_field_reference (&rl_arg, &rl_bitsize, &rl_bitpos,
    8433              :                                      &rl_unsignedp, &rl_reversep, &volatilep,
    8434              :                                      &rl_and_mask, &rl_signbit,
    8435              :                                      &r_xor, &rr_arg, &rr_and_mask,
    8436              :                                      &rl_load, rl_loc);
    8437       147757 :   if (!rl_inner)
    8438              :     return 0;
    8439       138215 :   rr_inner = decode_field_reference (&rr_arg, &rr_bitsize, &rr_bitpos,
    8440              :                                      &rr_unsignedp, &rr_reversep, &volatilep,
    8441              :                                      &rr_and_mask, &rr_signbit, &r_xor, 0, 0,
    8442              :                                      &rr_load, rr_loc);
    8443       138215 :   if (!rr_inner)
    8444              :     return 0;
    8445              : 
    8446              :   /* It must be true that the inner operation on the lhs of each
    8447              :      comparison must be the same if we are to be able to do anything.
    8448              :      Then see if we have constants.  If not, the same must be true for
    8449              :      the rhs's.  If one is a load and the other isn't, we have to be
    8450              :      conservative and avoid the optimization, otherwise we could get
    8451              :      SRAed fields wrong.  */
    8452       137490 :   if (volatilep)
    8453              :     return 0;
    8454              : 
    8455       137490 :   if (ll_reversep != rl_reversep
    8456       137490 :       || ! operand_equal_p (ll_inner, rl_inner, 0))
    8457              :     {
    8458              :       /* Try swapping the operands.  */
    8459       102444 :       if (ll_reversep != rr_reversep || rsignbit
    8460       204426 :           || !operand_equal_p (ll_inner, rr_inner, 0))
    8461       101298 :         return 0;
    8462              : 
    8463         1173 :       rcode = swap_tree_comparison (rcode);
    8464         1173 :       std::swap (rl_arg, rr_arg);
    8465         1173 :       std::swap (rl_inner, rr_inner);
    8466         1173 :       std::swap (rl_bitsize, rr_bitsize);
    8467         1173 :       std::swap (rl_bitpos, rr_bitpos);
    8468         1173 :       std::swap (rl_unsignedp, rr_unsignedp);
    8469         1173 :       std::swap (rl_reversep, rr_reversep);
    8470         1173 :       std::swap (rl_and_mask, rr_and_mask);
    8471         1173 :       std::swap (rl_signbit, rr_signbit);
    8472         1173 :       std::swap (rl_load, rr_load);
    8473         1173 :       std::swap (rl_loc, rr_loc);
    8474              :     }
    8475              : 
    8476        70307 :   if ((ll_load && rl_load)
    8477       138537 :       ? gimple_vuse (ll_load) != gimple_vuse (rl_load)
    8478         2077 :       : (!ll_load != !rl_load))
    8479              :     return 0;
    8480              : 
    8481              :   /* ??? Can we do anything with these?  */
    8482        35784 :   if (lr_signbit || rr_signbit)
    8483              :     return 0;
    8484              : 
    8485              :   /* If the mask encompassed extensions of the sign bit before
    8486              :      clipping, try to include the sign bit in the test.  If we're not
    8487              :      comparing with zero, don't even try to deal with it (for now?).
    8488              :      If we've already commited to a sign test, the extended (before
    8489              :      clipping) mask could already be messing with it.  */
    8490        35784 :   if (ll_signbit)
    8491              :     {
    8492            4 :       if (!integer_zerop (lr_arg) || lsignbit)
    8493            0 :         return 0;
    8494            4 :       wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
    8495            4 :       if (!ll_and_mask.get_precision ())
    8496            0 :         ll_and_mask = sign;
    8497              :       else
    8498            4 :         ll_and_mask |= sign;
    8499            4 :     }
    8500              : 
    8501        35784 :   if (rl_signbit)
    8502              :     {
    8503            4 :       if (!integer_zerop (rr_arg) || rsignbit)
    8504            1 :         return 0;
    8505            3 :       wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
    8506            3 :       if (!rl_and_mask.get_precision ())
    8507            0 :         rl_and_mask = sign;
    8508              :       else
    8509            3 :         rl_and_mask |= sign;
    8510            3 :     }
    8511              : 
    8512        35783 :   if (TREE_CODE (lr_arg) == INTEGER_CST
    8513        29240 :       && TREE_CODE (rr_arg) == INTEGER_CST)
    8514              :     {
    8515        28836 :       l_const = wi::to_wide (lr_arg);
    8516              :       /* We don't expect masks on constants, but if there are any, apply
    8517              :          them now.  */
    8518        28836 :       if (lr_and_mask.get_precision ())
    8519            0 :         l_const &= wide_int::from (lr_and_mask,
    8520            0 :                                    l_const.get_precision (), UNSIGNED);
    8521        28836 :       r_const = wi::to_wide (rr_arg);
    8522        28836 :       if (rr_and_mask.get_precision ())
    8523            0 :         r_const &= wide_int::from (rr_and_mask,
    8524            0 :                                    r_const.get_precision (), UNSIGNED);
    8525        28836 :       lr_reversep = ll_reversep;
    8526              :     }
    8527         6947 :   else if (lr_reversep != rr_reversep
    8528         6947 :            || ! operand_equal_p (lr_inner, rr_inner, 0)
    8529        12730 :            || ((lr_load && rr_load)
    8530        17268 :                ? gimple_vuse (lr_load) != gimple_vuse (rr_load)
    8531           27 :                : (!lr_load != !rr_load)))
    8532         1194 :     return 0;
    8533              : 
    8534              :   /* If we found sign tests, finish turning them into bit tests.  */
    8535              : 
    8536        34589 :   if (lsignbit)
    8537              :     {
    8538           44 :       wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
    8539              :       /* If ll_arg is zero-extended and we're testing the sign bit, we know
    8540              :          what the result should be.  Shifting the sign bit out of sign will get
    8541              :          us to mask the entire field out, yielding zero, i.e., the sign bit of
    8542              :          the zero-extended value.  We know the masked value is being compared
    8543              :          with zero, so the compare will get us the result we're looking
    8544              :          for: TRUE if EQ_EXPR, FALSE if NE_EXPR.  */
    8545           44 :       if (lsignbit > ll_bitsize && ll_unsignedp)
    8546            1 :         sign <<= 1;
    8547           44 :       if (!ll_and_mask.get_precision ())
    8548           43 :         ll_and_mask = sign;
    8549              :       else
    8550            1 :         ll_and_mask &= sign;
    8551           44 :       if (l_xor)
    8552              :         {
    8553            1 :           if (ll_bitsize != lr_bitsize)
    8554            1 :             return 0;
    8555            0 :           if (!lr_and_mask.get_precision ())
    8556            0 :             lr_and_mask = sign;
    8557              :           else
    8558            0 :             lr_and_mask &= sign;
    8559            0 :           if (l_const.get_precision ())
    8560            0 :             l_const &= wide_int::from (lr_and_mask,
    8561            0 :                                        l_const.get_precision (), UNSIGNED);
    8562              :         }
    8563           44 :     }
    8564              : 
    8565        34588 :   if (rsignbit)
    8566              :     {
    8567          166 :       wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
    8568          166 :       if (rsignbit > rl_bitsize && rl_unsignedp)
    8569            0 :         sign <<= 1;
    8570          166 :       if (!rl_and_mask.get_precision ())
    8571          166 :         rl_and_mask = sign;
    8572              :       else
    8573            0 :         rl_and_mask &= sign;
    8574          166 :       if (r_xor)
    8575              :         {
    8576           16 :           if (rl_bitsize != rr_bitsize)
    8577            0 :             return 0;
    8578           16 :           if (!rr_and_mask.get_precision ())
    8579           16 :             rr_and_mask = sign;
    8580              :           else
    8581            0 :             rr_and_mask &= sign;
    8582           16 :           if (r_const.get_precision ())
    8583           24 :             r_const &= wide_int::from (rr_and_mask,
    8584           12 :                                        r_const.get_precision (), UNSIGNED);
    8585              :         }
    8586          166 :     }
    8587              : 
    8588              :   /* If either comparison code is not correct for our logical operation,
    8589              :      fail.  However, we can convert a one-bit comparison against zero into
    8590              :      the opposite comparison against that bit being set in the field.  */
    8591              : 
    8592        34588 :   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
    8593        34588 :   if (lcode != wanted_code)
    8594              :     {
    8595         4502 :       if (l_const.get_precision ()
    8596         4456 :           && l_const == 0
    8597         1517 :           && ll_and_mask.get_precision ()
    8598         4915 :           && wi::popcount (ll_and_mask) == 1)
    8599              :         {
    8600              :           /* Make the left operand unsigned, since we are only interested
    8601              :              in the value of one bit.  Otherwise we are doing the wrong
    8602              :              thing below.  */
    8603          289 :           ll_unsignedp = 1;
    8604          289 :           l_const = ll_and_mask;
    8605              :         }
    8606              :       else
    8607         4213 :         return 0;
    8608              :     }
    8609              : 
    8610              :   /* This is analogous to the code for l_const above.  */
    8611        30375 :   if (rcode != wanted_code)
    8612              :     {
    8613          854 :       if (r_const.get_precision ()
    8614          854 :           && r_const == 0
    8615          829 :           && rl_and_mask.get_precision ()
    8616         1617 :           && wi::popcount (rl_and_mask) == 1)
    8617              :         {
    8618          601 :           rl_unsignedp = 1;
    8619          601 :           r_const = rl_and_mask;
    8620              :         }
    8621              :       else
    8622          253 :         return 0;
    8623              :     }
    8624              : 
    8625              :   /* This will be bumped to 2 if any of the field pairs crosses an
    8626              :      alignment boundary, so the merged compare has to be done in two
    8627              :      parts.  */
    8628        90366 :   int parts = 1;
    8629              :   /* Set to true if the second combined compare should come first,
    8630              :      e.g., because the second original compare accesses a word that
    8631              :      the first one doesn't, and the combined compares access those in
    8632              :      cmp[0].  */
    8633        90366 :   bool first1 = false;
    8634              :   /* Set to true if the first original compare is not the one being
    8635              :      split.  */
    8636        90366 :   bool maybe_separate = false;
    8637              : 
    8638              :   /* The following 2-dimensional arrays use the first index to
    8639              :      identify left(0)- vs right(1)-hand compare operands, and the
    8640              :      second one to identify merged compare parts.  */
    8641              :   /* The memory loads or constants to be compared.  */
    8642              :   tree ld_arg[2][2];
    8643              :   /* The first bit of the corresponding inner object that the
    8644              :      corresponding LD_ARG covers.  */
    8645              :   HOST_WIDE_INT bitpos[2][2];
    8646              :   /* The bit count starting at BITPOS that the corresponding LD_ARG
    8647              :      covers.  */
    8648              :   HOST_WIDE_INT bitsiz[2][2];
    8649              :   /* The number of bits by which LD_ARG has already been shifted
    8650              :      right, WRT mask.  */
    8651              :   HOST_WIDE_INT shifted[2][2];
    8652              :   /* The number of bits by which both LD_ARG and MASK need shifting to
    8653              :      bring its least-significant bit to bit zero.  */
    8654              :   HOST_WIDE_INT toshift[2][2];
    8655              :   /* An additional mask to be applied to LD_ARG, to remove any bits
    8656              :      that may have been loaded for use in another compare, but that
    8657              :      don't belong in the corresponding compare.  */
    8658       361464 :   wide_int xmask[2][2] = {};
    8659              : 
    8660              :   /* The combined compare or compares.  */
    8661        30122 :   tree cmp[2];
    8662              : 
    8663              :   /* Consider we're comparing two non-contiguous fields of packed
    8664              :      structs, both aligned at 32-bit boundaries:
    8665              : 
    8666              :      ll_arg: an 8-bit field at offset 0
    8667              :      lr_arg: a 16-bit field at offset 2
    8668              : 
    8669              :      rl_arg: an 8-bit field at offset 1
    8670              :      rr_arg: a 16-bit field at offset 3
    8671              : 
    8672              :      We'll have r_split_load, because rr_arg straddles across an
    8673              :      alignment boundary.
    8674              : 
    8675              :      We'll want to have:
    8676              : 
    8677              :      bitpos  = { {  0,  0 }, {  0, 32 } }
    8678              :      bitsiz  = { { 32, 32 }, { 32,  8 } }
    8679              : 
    8680              :      And, for little-endian:
    8681              : 
    8682              :      shifted = { {  0,  0 }, {  0, 32 } }
    8683              :      toshift = { {  0, 24 }, {  0,  0 } }
    8684              : 
    8685              :      Or, for big-endian:
    8686              : 
    8687              :      shifted = { {  0,  0 }, {  8,  0 } }
    8688              :      toshift = { {  8,  0 }, {  0,  0 } }
    8689              :   */
    8690              : 
    8691              :   /* See if we can find a mode that contains both fields being compared on
    8692              :      the left.  If we can't, fail.  Otherwise, update all constants and masks
    8693              :      to be relative to a field of that size.  */
    8694        30122 :   first_bit = MIN (ll_bitpos, rl_bitpos);
    8695        30122 :   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
    8696        30122 :   HOST_WIDE_INT ll_align = TYPE_ALIGN (TREE_TYPE (ll_inner));
    8697        30122 :   poly_uint64 ll_end_region = 0;
    8698        30122 :   if (TYPE_SIZE (TREE_TYPE (ll_inner))
    8699        30122 :       && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (ll_inner))))
    8700        30122 :     ll_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (ll_inner)));
    8701        30122 :   if (get_best_mode (end_bit - first_bit, first_bit, 0, ll_end_region,
    8702        30122 :                      ll_align, BITS_PER_WORD, volatilep, &lnmode))
    8703              :     l_split_load = false;
    8704              :   /* ??? If ll and rl share the same load, reuse that?
    8705              :      See PR 118206 -> gcc.dg/field-merge-18.c  */
    8706              :   else
    8707              :     {
    8708              :       /* Consider the possibility of recombining loads if any of the
    8709              :          fields straddles across an alignment boundary, so that either
    8710              :          part can be loaded along with the other field.  Since we
    8711              :          limit access modes to BITS_PER_WORD, don't exceed that,
    8712              :          otherwise on a 32-bit host and a 64-bit-aligned data
    8713              :          structure, we'll fail the above for a field that straddles
    8714              :          across two words, and would fail here for not even trying to
    8715              :          split it at between 32-bit words.  */
    8716        26789 :       HOST_WIDE_INT boundary = compute_split_boundary_from_align
    8717        28371 :         (MIN (ll_align, BITS_PER_WORD),
    8718              :          ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
    8719              : 
    8720        26789 :       if (boundary < 0
    8721          219 :           || !get_best_mode (boundary - first_bit, first_bit, 0, ll_end_region,
    8722              :                              ll_align, BITS_PER_WORD, volatilep, &lnmode)
    8723        26966 :           || !get_best_mode (end_bit - boundary, boundary, 0, ll_end_region,
    8724          177 :                              ll_align, BITS_PER_WORD, volatilep, &lnmode2))
    8725              :         {
    8726        28152 :           if (ll_align <= BITS_PER_WORD)
    8727              :             return 0;
    8728              : 
    8729              :           /* As a last resort, try double-word access modes.  This
    8730              :              enables us to deal with misaligned double-word fields
    8731              :              that straddle across 3 separate words.  */
    8732         1253 :           boundary = compute_split_boundary_from_align
    8733         1343 :             (MIN (ll_align, 2 * BITS_PER_WORD),
    8734              :              ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
    8735         1253 :           if (boundary < 0
    8736            0 :               || !get_best_mode (boundary - first_bit, first_bit,
    8737              :                                  0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
    8738              :                                  volatilep, &lnmode)
    8739         1253 :               || !get_best_mode (end_bit - boundary, boundary,
    8740            0 :                                  0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
    8741              :                                  volatilep, &lnmode2))
    8742         1253 :             return 0;
    8743              :         }
    8744              : 
    8745              :       /* If we can't have a single load, but can with two, figure out whether
    8746              :          the two compares can be separated, i.e., whether the entirety of the
    8747              :          first original compare is encompassed by the entirety of the first
    8748              :          combined compare.  If the first original compare is past the alignment
    8749              :          boundary, arrange to compare that range first, by setting first1
    8750              :          (meaning make cmp[1] first, instead of cmp[0]).  */
    8751          177 :       l_split_load = true;
    8752          177 :       parts = 2;
    8753          177 :       if (ll_bitpos >= boundary)
    8754              :         maybe_separate = first1 = true;
    8755          132 :       else if (ll_bitpos + ll_bitsize <= boundary)
    8756           32 :         maybe_separate = true;
    8757              :     }
    8758              : 
    8759         3510 :   lnbitsize = GET_MODE_BITSIZE (lnmode);
    8760         3510 :   lnbitpos = first_bit & ~ (lnbitsize - 1);
    8761              :   /* Avoid situations that the code below can't handle.  */
    8762         3510 :   if (lnbitpos < 0)
    8763              :     return 0;
    8764              : 
    8765              :   /* Choose the type for the combined compare.  Even if we're splitting loads,
    8766              :      make it wide enough to hold both.  */
    8767         3510 :   if (l_split_load)
    8768          354 :     lnbitsize += GET_MODE_BITSIZE (lnmode2);
    8769         3510 :   lntype = build_nonstandard_integer_type (lnbitsize, 1);
    8770         3510 :   if (!lntype)
    8771              :     return NULL_TREE;
    8772         3510 :   lnprec = TYPE_PRECISION (lntype);
    8773         3510 :   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
    8774              : 
    8775              :   /* Adjust bit ranges for reverse endianness.  */
    8776         3510 :   if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8777              :     {
    8778            6 :       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
    8779            6 :       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
    8780              :     }
    8781              : 
    8782              :   /* Adjust masks to match the positions in the combined lntype.  */
    8783         7020 :   wide_int ll_mask, rl_mask, r_mask;
    8784         3510 :   if (ll_and_mask.get_precision ())
    8785         4278 :     ll_mask = wi::lshift (wide_int::from (ll_and_mask, lnprec, UNSIGNED),
    8786         2139 :                           xll_bitpos);
    8787              :   else
    8788         1371 :     ll_mask = wi::shifted_mask (xll_bitpos, ll_bitsize, false, lnprec);
    8789         3510 :   if (rl_and_mask.get_precision ())
    8790         4070 :     rl_mask = wi::lshift (wide_int::from (rl_and_mask, lnprec, UNSIGNED),
    8791         2035 :                           xrl_bitpos);
    8792              :   else
    8793         1475 :     rl_mask = wi::shifted_mask (xrl_bitpos, rl_bitsize, false, lnprec);
    8794              : 
    8795              :   /* When we set l_const, we also set r_const.  */
    8796         3510 :   gcc_checking_assert (!l_const.get_precision () == !r_const.get_precision ());
    8797              : 
    8798              :   /* Adjust right-hand constants in both original comparisons to match width
    8799              :      and bit position.  */
    8800         3510 :   if (l_const.get_precision ())
    8801              :     {
    8802              :       /* Before clipping upper bits of the right-hand operand of the compare,
    8803              :          check that they're sign or zero extensions, depending on how the
    8804              :          left-hand operand would be extended.  If it is unsigned, or if there's
    8805              :          a mask that zeroes out extension bits, whether because we've checked
    8806              :          for upper bits in the mask and did not set ll_signbit, or because the
    8807              :          sign bit itself is masked out, check that the right-hand operand is
    8808              :          zero-extended.  */
    8809         1888 :       bool l_non_ext_bits = false;
    8810         1888 :       if (ll_bitsize < lr_bitsize)
    8811              :         {
    8812           40 :           wide_int zext = wi::zext (l_const, ll_bitsize);
    8813           80 :           if ((ll_unsignedp
    8814           32 :                || (ll_and_mask.get_precision ()
    8815            4 :                    && (!ll_signbit
    8816           48 :                        || ((ll_and_mask & wi::mask (ll_bitsize - 1, true, ll_bitsize))
    8817            8 :                            == 0)))
    8818          152 :                ? zext : wi::sext (l_const, ll_bitsize)) == l_const)
    8819           40 :             l_const = zext;
    8820              :           else
    8821              :             l_non_ext_bits = true;
    8822           40 :         }
    8823              :       /* We're doing bitwise equality tests, so don't bother with sign
    8824              :          extensions.  */
    8825         1888 :       l_const = wide_int::from (l_const, lnprec, UNSIGNED);
    8826         1888 :       if (ll_and_mask.get_precision ())
    8827         1152 :         l_const &= wide_int::from (ll_and_mask, lnprec, UNSIGNED);
    8828         1888 :       l_const <<= xll_bitpos;
    8829         5664 :       if (l_non_ext_bits || (l_const & ~ll_mask) != 0)
    8830              :         {
    8831            0 :           warning_at (lloc, OPT_Wtautological_compare,
    8832              :                       "comparison is always %d", wanted_code == NE_EXPR);
    8833              : 
    8834            0 :           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
    8835              :         }
    8836              : 
    8837              :       /* Before clipping upper bits of the right-hand operand of the compare,
    8838              :          check that they're sign or zero extensions, depending on how the
    8839              :          left-hand operand would be extended.  */
    8840         1888 :       bool r_non_ext_bits = false;
    8841         1888 :       if (rl_bitsize < rr_bitsize)
    8842              :         {
    8843           18 :           wide_int zext = wi::zext (r_const, rl_bitsize);
    8844           36 :           if ((rl_unsignedp
    8845           17 :                || (rl_and_mask.get_precision ()
    8846           10 :                    && (!rl_signbit
    8847           24 :                        || ((rl_and_mask & wi::mask (rl_bitsize - 1, true, rl_bitsize))
    8848            6 :                            == 0)))
    8849           71 :                ? zext : wi::sext (r_const, rl_bitsize)) == r_const)
    8850           18 :             r_const = zext;
    8851              :           else
    8852              :             r_non_ext_bits = true;
    8853           18 :         }
    8854         1888 :       r_const = wide_int::from (r_const, lnprec, UNSIGNED);
    8855         1888 :       if (rl_and_mask.get_precision ())
    8856         1092 :         r_const &= wide_int::from (rl_and_mask, lnprec, UNSIGNED);
    8857         1888 :       r_const <<= xrl_bitpos;
    8858         5664 :       if (r_non_ext_bits || (r_const & ~rl_mask) != 0)
    8859              :         {
    8860            0 :           warning_at (rloc, OPT_Wtautological_compare,
    8861              :                       "comparison is always %d", wanted_code == NE_EXPR);
    8862              : 
    8863            0 :           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
    8864              :         }
    8865              : 
    8866              :       /* If there is something in common between the masks, those bits of the
    8867              :          constants must be the same.  If not, the combined condition cannot be
    8868              :          met, and the result is known.  Test for this to avoid generating
    8869              :          incorrect code below.  */
    8870         1888 :       wide_int mask = ll_mask & rl_mask;
    8871         1888 :       if (mask != 0
    8872         1933 :           && (l_const & mask) != (r_const & mask))
    8873              :         {
    8874            0 :           if (wanted_code == NE_EXPR)
    8875            0 :             return constant_boolean_node (true, truth_type);
    8876              :           else
    8877            0 :             return constant_boolean_node (false, truth_type);
    8878              :         }
    8879              : 
    8880              :       /* The constants are combined so as to line up with the loaded field, so
    8881              :          tentatively use the same parameters for the second combined
    8882              :          compare.  */
    8883         1888 :       ld_arg[1][0] = wide_int_to_tree (lntype, l_const | r_const);
    8884         1888 :       toshift[1][0] = MIN (xll_bitpos, xrl_bitpos);
    8885         1888 :       shifted[1][0] = 0;
    8886         1888 :       bitpos[1][0] = lnbitpos;
    8887         1888 :       bitsiz[1][0] = lnbitsize;
    8888              : 
    8889         1888 :       if (parts > 1)
    8890           49 :         reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
    8891              :                           shifted[1], xmask[1],
    8892           49 :                           lnbitpos + GET_MODE_BITSIZE (lnmode),
    8893              :                           lr_reversep);
    8894              : 
    8895              :       /* No masking needed, we know the full constants.  */
    8896         1888 :       r_mask = wi::mask (0, true, lnprec);
    8897              : 
    8898              :       /* If the compiler thinks this is used uninitialized below, it's
    8899              :          because it can't realize that parts can only be 2 when
    8900              :          comparing with constants if l_split_load is also true.  This
    8901              :          just silences the warning.  */
    8902         1888 :       rnbitpos = 0;
    8903         1888 :     }
    8904              : 
    8905              :   /* Likewise, if the right sides are not constant, align them for the combined
    8906              :      compare.  Also, disallow this optimization if a size, signedness or
    8907              :      storage order mismatch occurs between the left and right sides.  */
    8908              :   else
    8909              :     {
    8910         1622 :       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
    8911         1561 :           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
    8912         1561 :           || ll_reversep != lr_reversep
    8913              :           /* Make sure the two fields on the right
    8914              :              correspond to the left without being swapped.  */
    8915         1561 :           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
    8916          465 :         return 0;
    8917              : 
    8918         1161 :       bool r_split_load;
    8919         1161 :       scalar_int_mode rnmode2;
    8920              : 
    8921              :       /* Figure out how to load the bits for the right-hand size of the
    8922              :          combined compare.  As in the left-hand size, we may have to split it,
    8923              :          and then we use two separate compares.  */
    8924         1161 :       first_bit = MIN (lr_bitpos, rr_bitpos);
    8925         1161 :       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
    8926         1161 :       HOST_WIDE_INT lr_align = TYPE_ALIGN (TREE_TYPE (lr_inner));
    8927         1161 :       poly_uint64 lr_end_region = 0;
    8928         1161 :       if (TYPE_SIZE (TREE_TYPE (lr_inner))
    8929         1161 :           && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (lr_inner))))
    8930         1161 :         lr_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (lr_inner)));
    8931         1161 :       if (!get_best_mode (end_bit - first_bit, first_bit, 0, lr_end_region,
    8932         1161 :                           lr_align, BITS_PER_WORD, volatilep, &rnmode))
    8933              :         {
    8934              :           /* Consider the possibility of recombining loads if any of the
    8935              :              fields straddles across an alignment boundary, so that either
    8936              :              part can be loaded along with the other field.  */
    8937          134 :           HOST_WIDE_INT boundary = compute_split_boundary_from_align
    8938          134 :             (lr_align, lr_bitpos, lr_bitsize, rr_bitpos, rr_bitsize);
    8939              : 
    8940          134 :           if (boundary < 0
    8941              :               /* If we're to split both, make sure the split point is
    8942              :                  the same.  */
    8943          130 :               || (l_split_load
    8944          128 :                   && (boundary - lr_bitpos
    8945          128 :                       != (lnbitpos + GET_MODE_BITSIZE (lnmode)) - ll_bitpos))
    8946          130 :               || !get_best_mode (boundary - first_bit, first_bit,
    8947              :                                  0, lr_end_region,
    8948          130 :                                  lr_align, BITS_PER_WORD, volatilep, &rnmode)
    8949          264 :               || !get_best_mode (end_bit - boundary, boundary, 0, lr_end_region,
    8950          130 :                                  lr_align, BITS_PER_WORD, volatilep, &rnmode2))
    8951            4 :             return 0;
    8952              : 
    8953          130 :           r_split_load = true;
    8954          130 :           parts = 2;
    8955          130 :           if (lr_bitpos >= boundary)
    8956              :             maybe_separate = first1 = true;
    8957           88 :           else if (lr_bitpos + lr_bitsize <= boundary)
    8958           29 :             maybe_separate = true;
    8959              :         }
    8960              :       else
    8961              :         r_split_load = false;
    8962              : 
    8963              :       /* Find a type that can hold the entire right-hand operand.  */
    8964         1157 :       rnbitsize = GET_MODE_BITSIZE (rnmode);
    8965         1157 :       rnbitpos = first_bit & ~ (rnbitsize - 1);
    8966         1157 :       if (r_split_load)
    8967          260 :         rnbitsize += GET_MODE_BITSIZE (rnmode2);
    8968         1157 :       rntype = build_nonstandard_integer_type (rnbitsize, 1);
    8969         1157 :       if (!rntype)
    8970              :         return  0;
    8971         1157 :       rnprec = TYPE_PRECISION (rntype);
    8972         1157 :       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
    8973              : 
    8974              :       /* Adjust for reversed endianness.  */
    8975         1157 :       if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
    8976              :         {
    8977            0 :           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
    8978            0 :           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
    8979              :         }
    8980              : 
    8981              :       /* Adjust the masks to match the combined type, and combine them.  */
    8982         1157 :       wide_int lr_mask, rr_mask;
    8983         1157 :       if (lr_and_mask.get_precision ())
    8984         1972 :         lr_mask = wi::lshift (wide_int::from (lr_and_mask, rnprec, UNSIGNED),
    8985          986 :                               xlr_bitpos);
    8986              :       else
    8987          171 :         lr_mask = wi::shifted_mask (xlr_bitpos, lr_bitsize, false, rnprec);
    8988         1157 :       if (rr_and_mask.get_precision ())
    8989         1884 :         rr_mask = wi::lshift (wide_int::from (rr_and_mask, rnprec, UNSIGNED),
    8990          942 :                               xrr_bitpos);
    8991              :       else
    8992          215 :         rr_mask = wi::shifted_mask (xrr_bitpos, rr_bitsize, false, rnprec);
    8993         1157 :       r_mask = lr_mask | rr_mask;
    8994              : 
    8995              :       /* Load the right-hand operand of the combined compare.  */
    8996         1157 :       toshift[1][0] = MIN (xlr_bitpos, xrr_bitpos);
    8997         1157 :       shifted[1][0] = 0;
    8998              : 
    8999         1157 :       if (!r_split_load)
    9000              :         {
    9001         1027 :           bitpos[1][0] = rnbitpos;
    9002         1027 :           bitsiz[1][0] = rnbitsize;
    9003         1027 :           ld_arg[1][0] = make_bit_field_load (ll_loc[3], lr_inner, lr_arg,
    9004         1027 :                                               rntype, rnbitsize, rnbitpos,
    9005         1027 :                                               lr_unsignedp || rr_unsignedp,
    9006              :                                               lr_reversep, lr_load);
    9007              :         }
    9008              : 
    9009              :       /* ... and the second part of the right-hand operand if needed.  */
    9010         1157 :       if (parts > 1)
    9011              :         {
    9012          130 :           if (r_split_load)
    9013              :             {
    9014          130 :               gimple *point[2];
    9015          130 :               point[0] = lr_load;
    9016          130 :               point[1] = rr_load;
    9017          130 :               build_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
    9018              :                                 shifted[1], rl_loc[3], lr_inner, lr_arg,
    9019              :                                 rnmode, rnmode2, rnbitpos, lr_reversep, point);
    9020              :             }
    9021              :           else
    9022            0 :             reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
    9023              :                               shifted[1], xmask[1],
    9024            0 :                               lnbitpos + GET_MODE_BITSIZE (lnmode)
    9025            0 :                               - ll_bitpos + lr_bitpos, lr_reversep);
    9026              :         }
    9027         1157 :     }
    9028              : 
    9029              :   /* Now issue the loads for the left-hand combined operand/s.  */
    9030         6090 :   wide_int l_mask = ll_mask | rl_mask;
    9031         3045 :   toshift[0][0] = MIN (xll_bitpos, xrl_bitpos);
    9032         3045 :   shifted[0][0] = 0;
    9033              : 
    9034         3045 :   if (!l_split_load)
    9035              :     {
    9036         2868 :       bitpos[0][0] = lnbitpos;
    9037         2868 :       bitsiz[0][0] = lnbitsize;
    9038         2868 :       ld_arg[0][0] = make_bit_field_load (ll_loc[3], ll_inner, ll_arg,
    9039         2868 :                                           lntype, lnbitsize, lnbitpos,
    9040         2868 :                                           ll_unsignedp || rl_unsignedp,
    9041              :                                           ll_reversep, ll_load);
    9042              :     }
    9043              : 
    9044         3045 :   if (parts > 1)
    9045              :     {
    9046          179 :       if (l_split_load)
    9047              :             {
    9048          177 :               gimple *point[2];
    9049          177 :               point[0] = ll_load;
    9050          177 :               point[1] = rl_load;
    9051          177 :               build_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
    9052              :                                 shifted[0], rl_loc[3], ll_inner, ll_arg,
    9053              :                                 lnmode, lnmode2, lnbitpos, ll_reversep, point);
    9054              :             }
    9055              :       else
    9056            2 :         reuse_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
    9057              :                           shifted[0], xmask[0],
    9058            2 :                           rnbitpos + GET_MODE_BITSIZE (rnmode)
    9059            2 :                           - lr_bitpos + ll_bitpos, ll_reversep);
    9060              :     }
    9061              : 
    9062              :   /* Compute the compares.  */
    9063         6269 :   for (int i = 0; i < parts; i++)
    9064              :     {
    9065         3224 :       tree op[2] = { ld_arg[0][i], ld_arg[1][i] };
    9066         9672 :       wide_int mask[2] = { l_mask, r_mask };
    9067         3224 :       location_t *locs[2] = { i ? rl_loc : ll_loc, i ? rr_loc : lr_loc };
    9068              : 
    9069              :       /* Figure out the masks, and unshare the original operands.  */
    9070         9672 :       for (int j = 0; j < 2; j++)
    9071              :         {
    9072         6448 :           unsigned prec = TYPE_PRECISION (TREE_TYPE (op[j]));
    9073         6448 :           op[j] = unshare_expr (op[j]);
    9074              : 
    9075              :           /* Mask out the bits belonging to the other part.  */
    9076         6448 :           if (xmask[j][i].get_precision ())
    9077          102 :             mask[j] &= xmask[j][i];
    9078              : 
    9079         6448 :           if (shifted[j][i])
    9080              :             {
    9081          307 :               wide_int shift = wide_int::from (shifted[j][i], prec, UNSIGNED);
    9082          307 :               mask[j] = wi::lrshift (mask[j], shift);
    9083          307 :             }
    9084         6448 :           mask[j] = wide_int::from (mask[j], prec, UNSIGNED);
    9085              :         }
    9086              : 
    9087              :       /* Line up the operands for a compare.  */
    9088         3224 :       HOST_WIDE_INT shift = (toshift[0][i] - toshift[1][i]);
    9089              : 
    9090         3224 :       if (shift)
    9091              :         {
    9092           54 :           int j;
    9093           54 :           if (shift > 0)
    9094              :             j = 0;
    9095              :           else
    9096              :             {
    9097           52 :               j = 1;
    9098           52 :               shift = -shift;
    9099              :             }
    9100              : 
    9101           54 :           tree shiftsz = bitsize_int (shift);
    9102           54 :           op[j] = fold_build2_loc (locs[j][1], RSHIFT_EXPR, TREE_TYPE (op[j]),
    9103              :                                    op[j], shiftsz);
    9104           54 :           mask[j] = wi::lrshift (mask[j], shift);
    9105              :         }
    9106              : 
    9107              :       /* Convert to the smaller type before masking out unwanted
    9108              :          bits.  */
    9109         3224 :       tree type = TREE_TYPE (op[0]);
    9110         3224 :       if (type != TREE_TYPE (op[1]))
    9111              :         {
    9112          188 :           int j = (TYPE_PRECISION (type)
    9113          188 :                    < TYPE_PRECISION (TREE_TYPE (op[1])));
    9114          188 :           if (!j)
    9115           89 :             type = TREE_TYPE (op[1]);
    9116          188 :           op[j] = fold_convert_loc (locs[j][0], type, op[j]);
    9117          188 :           mask[j] = wide_int::from (mask[j], TYPE_PRECISION (type), UNSIGNED);
    9118              :         }
    9119              : 
    9120              :       /* Apply masks.  */
    9121         9672 :       for (int j = 0; j < 2; j++)
    9122         6448 :         if (mask[j] != wi::mask (0, true, mask[j].get_precision ()))
    9123         2598 :           op[j] = fold_build2_loc (locs[j][2], BIT_AND_EXPR, type,
    9124         5196 :                                    op[j], wide_int_to_tree (type, mask[j]));
    9125              : 
    9126         6269 :       cmp[i] = fold_build2_loc (i ? rloc : lloc, wanted_code, truth_type,
    9127              :                                 op[0], op[1]);
    9128         9672 :     }
    9129              : 
    9130              :   /* Reorder the compares if needed.  */
    9131         3045 :   if (first1)
    9132           45 :     std::swap (cmp[0], cmp[1]);
    9133              : 
    9134              :   /* Prepare to return the resulting compares.  Combine two parts if
    9135              :      needed.  */
    9136         3045 :   if (parts == 1)
    9137         2866 :     result = cmp[0];
    9138          179 :   else if (!separatep || !maybe_separate)
    9139              :     {
    9140              :       /* Only fold if any of the cmp is known, otherwise we may lose the
    9141              :          sequence point, and that may prevent further optimizations.  */
    9142          173 :       if (TREE_CODE (cmp[0]) == INTEGER_CST
    9143          137 :           || TREE_CODE (cmp[1]) == INTEGER_CST)
    9144           37 :         result = fold_build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
    9145              :       else
    9146          136 :         result = build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
    9147              :     }
    9148              :   else
    9149              :     {
    9150            6 :       result = cmp[0];
    9151            6 :       *separatep = cmp[1];
    9152              :     }
    9153              : 
    9154         3045 :   return result;
    9155       260108 : }
    9156              : 
    9157              : /* Try to simplify the AND of two comparisons, specified by
    9158              :    (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
    9159              :    If this can be simplified to a single expression (without requiring
    9160              :    introducing more SSA variables to hold intermediate values),
    9161              :    return the resulting tree.  Otherwise return NULL_TREE.
    9162              :    If the result expression is non-null, it has boolean type.  */
    9163              : 
    9164              : tree
    9165       414028 : maybe_fold_and_comparisons (tree type,
    9166              :                             enum tree_code code1, tree op1a, tree op1b,
    9167              :                             enum tree_code code2, tree op2a, tree op2b,
    9168              :                             basic_block outer_cond_bb)
    9169              : {
    9170       414028 :   if (tree t = and_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
    9171              :                                   outer_cond_bb))
    9172              :     return t;
    9173              : 
    9174       412664 :   if (tree t = and_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
    9175              :                                   outer_cond_bb))
    9176              :     return t;
    9177              : 
    9178       412648 :   if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_AND_EXPR, code1,
    9179              :                                                      op1a, op1b, code2, op2a,
    9180              :                                                      op2b, outer_cond_bb))
    9181              :     return t;
    9182              : 
    9183              :   return NULL_TREE;
    9184              : }
    9185              : 
    9186              : /* Helper function for or_comparisons_1:  try to simplify the OR of the
    9187              :    ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
    9188              :    If INVERT is true, invert the value of VAR before doing the OR.
    9189              :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    9190              : 
    9191              : static tree
    9192        39835 : or_var_with_comparison (tree type, tree var, bool invert,
    9193              :                         enum tree_code code2, tree op2a, tree op2b,
    9194              :                         basic_block outer_cond_bb)
    9195              : {
    9196        39835 :   tree t;
    9197        39835 :   gimple *stmt = SSA_NAME_DEF_STMT (var);
    9198              : 
    9199              :   /* We can only deal with variables whose definitions are assignments.  */
    9200        39835 :   if (!is_gimple_assign (stmt))
    9201              :     return NULL_TREE;
    9202              : 
    9203              :   /* If we have an inverted comparison, apply DeMorgan's law and rewrite
    9204              :      !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
    9205              :      Then we only have to consider the simpler non-inverted cases.  */
    9206        39715 :   if (invert)
    9207        20617 :     t = and_var_with_comparison_1 (type, stmt,
    9208              :                                    invert_tree_comparison (code2, false),
    9209              :                                    op2a, op2b, outer_cond_bb);
    9210              :   else
    9211        19098 :     t = or_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
    9212              :                                   outer_cond_bb);
    9213        39715 :   return canonicalize_bool (t, invert);
    9214              : }
    9215              : 
    9216              : /* Try to simplify the OR of the ssa variable defined by the assignment
    9217              :    STMT with the comparison specified by (OP2A CODE2 OP2B).
    9218              :    Return NULL_EXPR if we can't simplify this to a single expression.  */
    9219              : 
    9220              : static tree
    9221       102107 : or_var_with_comparison_1 (tree type, gimple *stmt,
    9222              :                           enum tree_code code2, tree op2a, tree op2b,
    9223              :                           basic_block outer_cond_bb)
    9224              : {
    9225       102107 :   tree var = gimple_assign_lhs (stmt);
    9226       102107 :   tree true_test_var = NULL_TREE;
    9227       102107 :   tree false_test_var = NULL_TREE;
    9228       102107 :   enum tree_code innercode = gimple_assign_rhs_code (stmt);
    9229              : 
    9230              :   /* Check for identities like (var OR (var != 0)) => true .  */
    9231       102107 :   if (TREE_CODE (op2a) == SSA_NAME
    9232       102107 :       && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
    9233              :     {
    9234        15833 :       if ((code2 == NE_EXPR && integer_zerop (op2b))
    9235        50756 :           || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
    9236              :         {
    9237        14019 :           true_test_var = op2a;
    9238        14019 :           if (var == true_test_var)
    9239              :             return var;
    9240              :         }
    9241         2982 :       else if ((code2 == EQ_EXPR && integer_zerop (op2b))
    9242        30357 :                || (code2 == NE_EXPR && integer_nonzerop (op2b)))
    9243              :         {
    9244         7266 :           false_test_var = op2a;
    9245         7266 :           if (var == false_test_var)
    9246            0 :             return boolean_true_node;
    9247              :         }
    9248              :     }
    9249              : 
    9250              :   /* If the definition is a comparison, recurse on it.  */
    9251       102107 :   if (TREE_CODE_CLASS (innercode) == tcc_comparison)
    9252              :     {
    9253          864 :       tree t = or_comparisons_1 (type, innercode,
    9254              :                                  gimple_assign_rhs1 (stmt),
    9255              :                                  gimple_assign_rhs2 (stmt),
    9256              :                                  code2, op2a, op2b, outer_cond_bb);
    9257          864 :       if (t)
    9258              :         return t;
    9259              :     }
    9260              : 
    9261              :   /* If the definition is an AND or OR expression, we may be able to
    9262              :      simplify by reassociating.  */
    9263       102084 :   if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
    9264       102084 :       && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
    9265              :     {
    9266        39634 :       tree inner1 = gimple_assign_rhs1 (stmt);
    9267        39634 :       tree inner2 = gimple_assign_rhs2 (stmt);
    9268        39634 :       gimple *s;
    9269        39634 :       tree t;
    9270        39634 :       tree partial = NULL_TREE;
    9271        39634 :       bool is_or = (innercode == BIT_IOR_EXPR);
    9272              : 
    9273              :       /* Check for boolean identities that don't require recursive examination
    9274              :          of inner1/inner2:
    9275              :          inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
    9276              :          inner1 OR (inner1 AND inner2) => inner1
    9277              :          !inner1 OR (inner1 OR inner2) => true
    9278              :          !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
    9279              :       */
    9280        39634 :       if (inner1 == true_test_var)
    9281            0 :         return (is_or ? var : inner1);
    9282        39634 :       else if (inner2 == true_test_var)
    9283            0 :         return (is_or ? var : inner2);
    9284        39634 :       else if (inner1 == false_test_var)
    9285            0 :         return (is_or
    9286            0 :                 ? boolean_true_node
    9287            0 :                 : or_var_with_comparison (type, inner2, false, code2, op2a,
    9288            0 :                                           op2b, outer_cond_bb));
    9289        39634 :       else if (inner2 == false_test_var)
    9290            0 :         return (is_or
    9291            0 :                 ? boolean_true_node
    9292            0 :                 : or_var_with_comparison (type, inner1, false, code2, op2a,
    9293            0 :                                           op2b, outer_cond_bb));
    9294              : 
    9295              :       /* Next, redistribute/reassociate the OR across the inner tests.
    9296              :          Compute the first partial result, (inner1 OR (op2a code op2b))  */
    9297        39634 :       if (TREE_CODE (inner1) == SSA_NAME
    9298        39634 :           && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
    9299        38629 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    9300        63774 :           && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
    9301              :                                              gimple_assign_rhs1 (s),
    9302              :                                              gimple_assign_rhs2 (s),
    9303              :                                              code2, op2a, op2b,
    9304              :                                              outer_cond_bb)))
    9305              :         {
    9306              :           /* Handle the OR case, where we are reassociating:
    9307              :              (inner1 OR inner2) OR (op2a code2 op2b)
    9308              :              => (t OR inner2)
    9309              :              If the partial result t is a constant, we win.  Otherwise
    9310              :              continue on to try reassociating with the other inner test.  */
    9311          739 :           if (is_or)
    9312              :             {
    9313           31 :               if (integer_onep (t))
    9314            0 :                 return boolean_true_node;
    9315           31 :               else if (integer_zerop (t))
    9316              :                 return inner2;
    9317              :             }
    9318              : 
    9319              :           /* Handle the AND case, where we are redistributing:
    9320              :              (inner1 AND inner2) OR (op2a code2 op2b)
    9321              :              => (t AND (inner2 OR (op2a code op2b)))  */
    9322          708 :           else if (integer_zerop (t))
    9323            0 :             return boolean_false_node;
    9324              : 
    9325              :           /* Save partial result for later.  */
    9326              :           partial = t;
    9327              :         }
    9328              : 
    9329              :       /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
    9330        39634 :       if (TREE_CODE (inner2) == SSA_NAME
    9331        39634 :           && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
    9332        38925 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
    9333        76791 :           && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
    9334              :                                              gimple_assign_rhs1 (s),
    9335              :                                              gimple_assign_rhs2 (s),
    9336              :                                              code2, op2a, op2b,
    9337              :                                              outer_cond_bb)))
    9338              :         {
    9339              :           /* Handle the OR case, where we are reassociating:
    9340              :              (inner1 OR inner2) OR (op2a code2 op2b)
    9341              :              => (inner1 OR t)
    9342              :              => (t OR partial)  */
    9343          494 :           if (is_or)
    9344              :             {
    9345           60 :               if (integer_zerop (t))
    9346              :                 return inner1;
    9347           60 :               else if (integer_onep (t))
    9348            1 :                 return boolean_true_node;
    9349              :               /* If both are the same, we can apply the identity
    9350              :                  (x OR x) == x.  */
    9351           59 :               else if (partial && same_bool_result_p (t, partial))
    9352              :                 return t;
    9353              :             }
    9354              : 
    9355              :           /* Handle the AND case, where we are redistributing:
    9356              :              (inner1 AND inner2) OR (op2a code2 op2b)
    9357              :              => (t AND (inner1 OR (op2a code2 op2b)))
    9358              :              => (t AND partial)  */
    9359              :           else
    9360              :             {
    9361          434 :               if (integer_zerop (t))
    9362            0 :                 return boolean_false_node;
    9363          434 :               else if (partial)
    9364              :                 {
    9365              :                   /* We already got a simplification for the other
    9366              :                      operand to the redistributed AND expression.  The
    9367              :                      interesting case is when at least one is true.
    9368              :                      Or, if both are the same, we can apply the identity
    9369              :                      (x AND x) == x.  */
    9370           14 :                   if (integer_onep (partial))
    9371              :                     return t;
    9372           14 :                   else if (integer_onep (t))
    9373              :                     return partial;
    9374            4 :                   else if (same_bool_result_p (t, partial))
    9375              :                     return t;
    9376              :                 }
    9377              :             }
    9378              :         }
    9379              :     }
    9380              :   return NULL_TREE;
    9381              : }
    9382              : 
    9383              : /* Try to simplify the OR of two comparisons defined by
    9384              :    (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
    9385              :    If this can be done without constructing an intermediate value,
    9386              :    return the resulting tree; otherwise NULL_TREE is returned.
    9387              :    This function is deliberately asymmetric as it recurses on SSA_DEFs
    9388              :    in the first comparison but not the second.  */
    9389              : 
    9390              : static tree
    9391       963163 : or_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
    9392              :                   enum tree_code code2, tree op2a, tree op2b,
    9393              :                   basic_block outer_cond_bb)
    9394              : {
    9395       963163 :   tree truth_type = truth_type_for (TREE_TYPE (op1a));
    9396              : 
    9397              :   /* First check for ((x CODE1 y) OR (x CODE2 y)).  */
    9398       963163 :   if (operand_equal_p (op1a, op2a, 0)
    9399       963163 :       && operand_equal_p (op1b, op2b, 0))
    9400              :     {
    9401              :       /* Result will be either NULL_TREE, or a combined comparison.  */
    9402         3138 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    9403              :                                     TRUTH_ORIF_EXPR, code1, code2,
    9404              :                                     truth_type, op1a, op1b);
    9405         3138 :       if (t)
    9406              :         return t;
    9407              :     }
    9408              : 
    9409              :   /* Likewise the swapped case of the above.  */
    9410       960057 :   if (operand_equal_p (op1a, op2b, 0)
    9411       960057 :       && operand_equal_p (op1b, op2a, 0))
    9412              :     {
    9413              :       /* Result will be either NULL_TREE, or a combined comparison.  */
    9414            0 :       tree t = combine_comparisons (UNKNOWN_LOCATION,
    9415              :                                     TRUTH_ORIF_EXPR, code1,
    9416              :                                     swap_tree_comparison (code2),
    9417              :                                     truth_type, op1a, op1b);
    9418            0 :       if (t)
    9419              :         return t;
    9420              :     }
    9421              : 
    9422              :   /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
    9423              :      NAME's definition is a truth value.  See if there are any simplifications
    9424              :      that can be done against the NAME's definition.  */
    9425       960057 :   if (TREE_CODE (op1a) == SSA_NAME
    9426       960054 :       && (code1 == NE_EXPR || code1 == EQ_EXPR)
    9427      1231638 :       && (integer_zerop (op1b) || integer_onep (op1b)))
    9428              :     {
    9429        35807 :       bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
    9430        72310 :                      || (code1 == NE_EXPR && integer_onep (op1b)));
    9431        68482 :       gimple *stmt = SSA_NAME_DEF_STMT (op1a);
    9432        68482 :       switch (gimple_code (stmt))
    9433              :         {
    9434        39715 :         case GIMPLE_ASSIGN:
    9435              :           /* Try to simplify by copy-propagating the definition.  */
    9436        39715 :           return or_var_with_comparison (type, op1a, invert, code2, op2a,
    9437        39715 :                                          op2b, outer_cond_bb);
    9438              : 
    9439        15676 :         case GIMPLE_PHI:
    9440              :           /* If every argument to the PHI produces the same result when
    9441              :              ORed with the second comparison, we win.
    9442              :              Do not do this unless the type is bool since we need a bool
    9443              :              result here anyway.  */
    9444        15676 :           if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
    9445              :             {
    9446              :               tree result = NULL_TREE;
    9447              :               unsigned i;
    9448          894 :               for (i = 0; i < gimple_phi_num_args (stmt); i++)
    9449              :                 {
    9450          894 :                   tree arg = gimple_phi_arg_def (stmt, i);
    9451              : 
    9452              :                   /* If this PHI has itself as an argument, ignore it.
    9453              :                      If all the other args produce the same result,
    9454              :                      we're still OK.  */
    9455          894 :                   if (arg == gimple_phi_result (stmt))
    9456            0 :                     continue;
    9457          894 :                   else if (TREE_CODE (arg) == INTEGER_CST)
    9458              :                     {
    9459          745 :                       if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
    9460              :                         {
    9461          339 :                           if (!result)
    9462          199 :                             result = boolean_true_node;
    9463          140 :                           else if (!integer_onep (result))
    9464              :                             return NULL_TREE;
    9465              :                         }
    9466          406 :                       else if (!result)
    9467          200 :                         result = fold_build2 (code2, boolean_type_node,
    9468              :                                               op2a, op2b);
    9469          206 :                       else if (!same_bool_comparison_p (result,
    9470              :                                                         code2, op2a, op2b))
    9471              :                         return NULL_TREE;
    9472              :                     }
    9473          149 :                   else if (TREE_CODE (arg) == SSA_NAME
    9474          149 :                            && !SSA_NAME_IS_DEFAULT_DEF (arg))
    9475              :                     {
    9476          149 :                       tree temp;
    9477          149 :                       gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
    9478              :                       /* In simple cases we can look through PHI nodes,
    9479              :                          but we have to be careful with loops.
    9480              :                          See PR49073.  */
    9481          149 :                       if (! dom_info_available_p (CDI_DOMINATORS)
    9482          149 :                           || gimple_bb (def_stmt) == gimple_bb (stmt)
    9483          298 :                           || dominated_by_p (CDI_DOMINATORS,
    9484          149 :                                              gimple_bb (def_stmt),
    9485          149 :                                              gimple_bb (stmt)))
    9486           29 :                         return NULL_TREE;
    9487          120 :                       temp = or_var_with_comparison (type, arg, invert, code2,
    9488              :                                                      op2a, op2b, outer_cond_bb);
    9489          120 :                       if (!temp)
    9490              :                         return NULL_TREE;
    9491            0 :                       else if (!result)
    9492              :                         result = temp;
    9493            0 :                       else if (!same_bool_result_p (result, temp))
    9494              :                         return NULL_TREE;
    9495              :                     }
    9496              :                   else
    9497              :                     return NULL_TREE;
    9498              :                 }
    9499              :               return result;
    9500              :             }
    9501              : 
    9502              :         default:
    9503              :           break;
    9504              :         }
    9505              :     }
    9506              :   return NULL_TREE;
    9507              : }
    9508              : 
    9509              : /* Try to simplify the OR of two comparisons, specified by
    9510              :    (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
    9511              :    If this can be simplified to a single expression (without requiring
    9512              :    introducing more SSA variables to hold intermediate values),
    9513              :    return the resulting tree.  Otherwise return NULL_TREE.
    9514              :    If the result expression is non-null, it has boolean type.  */
    9515              : 
    9516              : tree
    9517       482691 : maybe_fold_or_comparisons (tree type,
    9518              :                            enum tree_code code1, tree op1a, tree op1b,
    9519              :                            enum tree_code code2, tree op2a, tree op2b,
    9520              :                            basic_block outer_cond_bb)
    9521              : {
    9522       482691 :   if (tree t = or_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
    9523              :                                  outer_cond_bb))
    9524              :     return t;
    9525              : 
    9526       479608 :   if (tree t = or_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
    9527              :                                  outer_cond_bb))
    9528              :     return t;
    9529              : 
    9530       479603 :   if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_IOR_EXPR, code1,
    9531              :                                                      op1a, op1b, code2, op2a,
    9532              :                                                      op2b, outer_cond_bb))
    9533              :     return t;
    9534              : 
    9535              :   return NULL_TREE;
    9536              : }
    9537              : 
    9538              : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
    9539              : 
    9540              :    Either NULL_TREE, a simplified but non-constant or a constant
    9541              :    is returned.
    9542              : 
    9543              :    ???  This should go into a gimple-fold-inline.h file to be eventually
    9544              :    privatized with the single valueize function used in the various TUs
    9545              :    to avoid the indirect function call overhead.  */
    9546              : 
    9547              : tree
    9548    420435147 : gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
    9549              :                                 tree (*gvalueize) (tree))
    9550              : {
    9551    420435147 :   gimple_match_op res_op;
    9552              :   /* ???  The SSA propagators do not correctly deal with following SSA use-def
    9553              :      edges if there are intermediate VARYING defs.  For this reason
    9554              :      do not follow SSA edges here even though SCCVN can technically
    9555              :      just deal fine with that.  */
    9556    420435147 :   if (gimple_simplify (stmt, &res_op, NULL, gvalueize, valueize))
    9557              :     {
    9558     55280471 :       tree res = NULL_TREE;
    9559     55280471 :       if (gimple_simplified_result_is_gimple_val (&res_op))
    9560     33759259 :         res = res_op.ops[0];
    9561     21521212 :       else if (mprts_hook)
    9562      7530326 :         res = mprts_hook (&res_op);
    9563     41289585 :       if (res)
    9564              :         {
    9565     35611948 :           if (dump_file && dump_flags & TDF_DETAILS)
    9566              :             {
    9567         9593 :               fprintf (dump_file, "Match-and-simplified ");
    9568         9593 :               print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
    9569         9593 :               fprintf (dump_file, " to ");
    9570         9593 :               print_generic_expr (dump_file, res);
    9571         9593 :               fprintf (dump_file, "\n");
    9572              :             }
    9573     35611948 :           return res;
    9574              :         }
    9575              :     }
    9576              : 
    9577    384823199 :   location_t loc = gimple_location (stmt);
    9578    384823199 :   switch (gimple_code (stmt))
    9579              :     {
    9580    332743485 :     case GIMPLE_ASSIGN:
    9581    332743485 :       {
    9582    332743485 :         enum tree_code subcode = gimple_assign_rhs_code (stmt);
    9583              : 
    9584    332743485 :         switch (get_gimple_rhs_class (subcode))
    9585              :           {
    9586    120358903 :           case GIMPLE_SINGLE_RHS:
    9587    120358903 :             {
    9588    120358903 :               tree rhs = gimple_assign_rhs1 (stmt);
    9589    120358903 :               enum tree_code_class kind = TREE_CODE_CLASS (subcode);
    9590              : 
    9591    120358903 :               if (TREE_CODE (rhs) == SSA_NAME)
    9592              :                 {
    9593              :                   /* If the RHS is an SSA_NAME, return its known constant value,
    9594              :                      if any.  */
    9595      9439001 :                   return (*valueize) (rhs);
    9596              :                 }
    9597              :               /* Handle propagating invariant addresses into address
    9598              :                  operations.  */
    9599    110919902 :               else if (TREE_CODE (rhs) == ADDR_EXPR
    9600    110919902 :                        && !is_gimple_min_invariant (rhs))
    9601              :                 {
    9602      7562640 :                   poly_int64 offset = 0;
    9603      7562640 :                   tree base;
    9604      7562640 :                   base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
    9605              :                                                           &offset,
    9606              :                                                           valueize);
    9607      7562640 :                   if (base
    9608      7562640 :                       && (CONSTANT_CLASS_P (base)
    9609      6837989 :                           || decl_address_invariant_p (base)))
    9610       198419 :                     return build_invariant_address (TREE_TYPE (rhs),
    9611       198419 :                                                     base, offset);
    9612              :                 }
    9613    103357262 :               else if (TREE_CODE (rhs) == CONSTRUCTOR
    9614      1058889 :                        && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
    9615    104837874 :                        && known_eq (CONSTRUCTOR_NELTS (rhs),
    9616              :                                     TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
    9617              :                 {
    9618       406563 :                   unsigned i, nelts;
    9619       406563 :                   tree val;
    9620              : 
    9621       406563 :                   nelts = CONSTRUCTOR_NELTS (rhs);
    9622       406563 :                   tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
    9623       905301 :                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
    9624              :                     {
    9625       487553 :                       val = (*valueize) (val);
    9626       487553 :                       if (TREE_CODE (val) == INTEGER_CST
    9627       415739 :                           || TREE_CODE (val) == REAL_CST
    9628       395378 :                           || TREE_CODE (val) == FIXED_CST)
    9629        92175 :                         vec.quick_push (val);
    9630              :                       else
    9631              :                         return NULL_TREE;
    9632              :                     }
    9633              : 
    9634        11185 :                   return vec.build ();
    9635       406563 :                 }
    9636    110314920 :               if (subcode == OBJ_TYPE_REF)
    9637              :                 {
    9638       285581 :                   tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
    9639              :                   /* If callee is constant, we can fold away the wrapper.  */
    9640       285581 :                   if (is_gimple_min_invariant (val))
    9641              :                     return val;
    9642              :                 }
    9643              : 
    9644    110314737 :               if (kind == tcc_reference)
    9645              :                 {
    9646     73783802 :                   if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
    9647     71581657 :                        || TREE_CODE (rhs) == REALPART_EXPR
    9648     70731730 :                        || TREE_CODE (rhs) == IMAGPART_EXPR)
    9649     75637164 :                       && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
    9650              :                     {
    9651      3131543 :                       tree val = (*valueize) (TREE_OPERAND (rhs, 0));
    9652      3131543 :                       return fold_unary_loc (EXPR_LOCATION (rhs),
    9653      3131543 :                                              TREE_CODE (rhs),
    9654      6263086 :                                              TREE_TYPE (rhs), val);
    9655              :                     }
    9656     70652259 :                   else if (TREE_CODE (rhs) == BIT_FIELD_REF
    9657     70652259 :                            && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
    9658              :                     {
    9659       584880 :                       tree val = (*valueize) (TREE_OPERAND (rhs, 0));
    9660       584880 :                       return fold_ternary_loc (EXPR_LOCATION (rhs),
    9661       584880 :                                                TREE_CODE (rhs),
    9662       584880 :                                                TREE_TYPE (rhs), val,
    9663       584880 :                                                TREE_OPERAND (rhs, 1),
    9664      1169760 :                                                TREE_OPERAND (rhs, 2));
    9665              :                     }
    9666     70067379 :                   else if (TREE_CODE (rhs) == MEM_REF
    9667     70067379 :                            && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
    9668              :                     {
    9669     14351581 :                       tree val = (*valueize) (TREE_OPERAND (rhs, 0));
    9670     14351581 :                       if (TREE_CODE (val) == ADDR_EXPR
    9671     14351581 :                           && is_gimple_min_invariant (val))
    9672              :                         {
    9673       944062 :                           tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
    9674              :                                                   unshare_expr (val),
    9675              :                                                   TREE_OPERAND (rhs, 1));
    9676       944062 :                           if (tem)
    9677     70067379 :                             rhs = tem;
    9678              :                         }
    9679              :                     }
    9680     70067379 :                   return fold_const_aggregate_ref_1 (rhs, valueize);
    9681              :                 }
    9682     36530935 :               else if (kind == tcc_declaration)
    9683      8642503 :                 return get_symbol_constant_value (rhs);
    9684              :               return rhs;
    9685              :             }
    9686              : 
    9687              :           case GIMPLE_UNARY_RHS:
    9688              :             return NULL_TREE;
    9689              : 
    9690    158878019 :           case GIMPLE_BINARY_RHS:
    9691              :             /* Translate &x + CST into an invariant form suitable for
    9692              :                further propagation.  */
    9693    158878019 :             if (subcode == POINTER_PLUS_EXPR)
    9694              :               {
    9695     18760763 :                 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
    9696     18760763 :                 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
    9697     18760763 :                 if (TREE_CODE (op0) == ADDR_EXPR
    9698      5300048 :                     && TREE_CODE (op1) == INTEGER_CST)
    9699              :                   {
    9700       524125 :                     tree off = fold_convert (ptr_type_node, op1);
    9701       524125 :                     return build1_loc
    9702      1048250 :                         (loc, ADDR_EXPR, TREE_TYPE (op0),
    9703       524125 :                          fold_build2 (MEM_REF,
    9704              :                                       TREE_TYPE (TREE_TYPE (op0)),
    9705       524125 :                                       unshare_expr (op0), off));
    9706              :                   }
    9707              :               }
    9708              :             /* Canonicalize bool != 0 and bool == 0 appearing after
    9709              :                valueization.  While gimple_simplify handles this
    9710              :                it can get confused by the ~X == 1 -> X == 0 transform
    9711              :                which we cant reduce to a SSA name or a constant
    9712              :                (and we have no way to tell gimple_simplify to not
    9713              :                consider those transforms in the first place).  */
    9714    140117256 :             else if (subcode == EQ_EXPR
    9715    140117256 :                      || subcode == NE_EXPR)
    9716              :               {
    9717      3215463 :                 tree lhs = gimple_assign_lhs (stmt);
    9718      3215463 :                 tree op0 = gimple_assign_rhs1 (stmt);
    9719      3215463 :                 if (useless_type_conversion_p (TREE_TYPE (lhs),
    9720      3215463 :                                                TREE_TYPE (op0)))
    9721              :                   {
    9722        24475 :                     tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
    9723        24475 :                     op0 = (*valueize) (op0);
    9724        24475 :                     if (TREE_CODE (op0) == INTEGER_CST)
    9725          702 :                       std::swap (op0, op1);
    9726        24475 :                     if (TREE_CODE (op1) == INTEGER_CST
    9727        24475 :                         && ((subcode == NE_EXPR && integer_zerop (op1))
    9728         2309 :                             || (subcode == EQ_EXPR && integer_onep (op1))))
    9729          282 :                       return op0;
    9730              :                   }
    9731              :               }
    9732              :             return NULL_TREE;
    9733              : 
    9734       704461 :           case GIMPLE_TERNARY_RHS:
    9735       704461 :             {
    9736              :               /* Handle ternary operators that can appear in GIMPLE form.  */
    9737       704461 :               tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
    9738       704461 :               tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
    9739       704461 :               tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
    9740       704461 :               return fold_ternary_loc (loc, subcode,
    9741       704461 :                                        TREE_TYPE (gimple_assign_lhs (stmt)),
    9742       704461 :                                        op0, op1, op2);
    9743              :             }
    9744              : 
    9745            0 :           default:
    9746            0 :             gcc_unreachable ();
    9747              :           }
    9748              :       }
    9749              : 
    9750     14360089 :     case GIMPLE_CALL:
    9751     14360089 :       {
    9752     14360089 :         tree fn;
    9753     14360089 :         gcall *call_stmt = as_a <gcall *> (stmt);
    9754              : 
    9755     14360089 :         if (gimple_call_internal_p (stmt))
    9756              :           {
    9757      1300783 :             enum tree_code subcode = ERROR_MARK;
    9758      1300783 :             switch (gimple_call_internal_fn (stmt))
    9759              :               {
    9760              :               case IFN_UBSAN_CHECK_ADD:
    9761              :                 subcode = PLUS_EXPR;
    9762              :                 break;
    9763         7983 :               case IFN_UBSAN_CHECK_SUB:
    9764         7983 :                 subcode = MINUS_EXPR;
    9765         7983 :                 break;
    9766         6815 :               case IFN_UBSAN_CHECK_MUL:
    9767         6815 :                 subcode = MULT_EXPR;
    9768         6815 :                 break;
    9769       142632 :               case IFN_BUILTIN_EXPECT:
    9770       142632 :                   {
    9771       142632 :                     tree arg0 = gimple_call_arg (stmt, 0);
    9772       142632 :                     tree op0 = (*valueize) (arg0);
    9773       142632 :                     if (TREE_CODE (op0) == INTEGER_CST)
    9774              :                       return op0;
    9775              :                     return NULL_TREE;
    9776              :                   }
    9777              :               default:
    9778              :                 return NULL_TREE;
    9779              :               }
    9780        22924 :             tree arg0 = gimple_call_arg (stmt, 0);
    9781        22924 :             tree arg1 = gimple_call_arg (stmt, 1);
    9782        22924 :             tree op0 = (*valueize) (arg0);
    9783        22924 :             tree op1 = (*valueize) (arg1);
    9784              : 
    9785        22924 :             if (TREE_CODE (op0) != INTEGER_CST
    9786         2499 :                 || TREE_CODE (op1) != INTEGER_CST)
    9787              :               {
    9788        22402 :                 switch (subcode)
    9789              :                   {
    9790         6715 :                   case MULT_EXPR:
    9791              :                     /* x * 0 = 0 * x = 0 without overflow.  */
    9792         6715 :                     if (integer_zerop (op0) || integer_zerop (op1))
    9793           20 :                       return build_zero_cst (TREE_TYPE (arg0));
    9794              :                     break;
    9795         7641 :                   case MINUS_EXPR:
    9796              :                     /* y - y = 0 without overflow.  */
    9797         7641 :                     if (operand_equal_p (op0, op1, 0))
    9798            0 :                       return build_zero_cst (TREE_TYPE (arg0));
    9799              :                     break;
    9800              :                   default:
    9801              :                     break;
    9802              :                   }
    9803              :               }
    9804        22904 :             tree res
    9805        22904 :               = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
    9806        22904 :             if (res
    9807         2870 :                 && TREE_CODE (res) == INTEGER_CST
    9808        23426 :                 && !TREE_OVERFLOW (res))
    9809              :               return res;
    9810              :             return NULL_TREE;
    9811              :           }
    9812              : 
    9813     13059306 :         fn = (*valueize) (gimple_call_fn (stmt));
    9814     13059306 :         if (TREE_CODE (fn) == ADDR_EXPR
    9815     12422511 :             && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
    9816     12422447 :             && fndecl_built_in_p (TREE_OPERAND (fn, 0))
    9817     19113311 :             && gimple_builtin_call_types_compatible_p (stmt,
    9818      6054005 :                                                        TREE_OPERAND (fn, 0)))
    9819              :           {
    9820      5952210 :             tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
    9821      5952210 :             tree retval;
    9822      5952210 :             unsigned i;
    9823     18469346 :             for (i = 0; i < gimple_call_num_args (stmt); ++i)
    9824     12517136 :               args[i] = (*valueize) (gimple_call_arg (stmt, i));
    9825      5952210 :             retval = fold_builtin_call_array (loc,
    9826              :                                          gimple_call_return_type (call_stmt),
    9827              :                                          fn, gimple_call_num_args (stmt), args);
    9828      5952210 :             if (retval)
    9829              :               {
    9830              :                 /* fold_call_expr wraps the result inside a NOP_EXPR.  */
    9831        52447 :                 STRIP_NOPS (retval);
    9832        52447 :                 retval = fold_convert (gimple_call_return_type (call_stmt),
    9833              :                                        retval);
    9834              :               }
    9835      5952210 :             return retval;
    9836              :           }
    9837              :         return NULL_TREE;
    9838              :       }
    9839              : 
    9840              :     default:
    9841              :       return NULL_TREE;
    9842              :     }
    9843              : }
    9844              : 
    9845              : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
    9846              :    Returns NULL_TREE if folding to a constant is not possible, otherwise
    9847              :    returns a constant according to is_gimple_min_invariant.  */
    9848              : 
    9849              : tree
    9850         4415 : gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
    9851              : {
    9852         4415 :   tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
    9853         4415 :   if (res && is_gimple_min_invariant (res))
    9854              :     return res;
    9855              :   return NULL_TREE;
    9856              : }
    9857              : 
    9858              : 
    9859              : /* The following set of functions are supposed to fold references using
    9860              :    their constant initializers.  */
    9861              : 
    9862              : /* See if we can find constructor defining value of BASE.
    9863              :    When we know the consructor with constant offset (such as
    9864              :    base is array[40] and we do know constructor of array), then
    9865              :    BIT_OFFSET is adjusted accordingly.
    9866              : 
    9867              :    As a special case, return error_mark_node when constructor
    9868              :    is not explicitly available, but it is known to be zero
    9869              :    such as 'static const int a;'.  */
    9870              : static tree
    9871    129532512 : get_base_constructor (tree base, poly_int64 *bit_offset,
    9872              :                       tree (*valueize)(tree))
    9873              : {
    9874    129628350 :   poly_int64 bit_offset2, size, max_size;
    9875    129628350 :   bool reverse;
    9876              : 
    9877    129628350 :   if (TREE_CODE (base) == MEM_REF)
    9878              :     {
    9879    139009334 :       poly_offset_int boff = *bit_offset + mem_ref_offset (base) * BITS_PER_UNIT;
    9880     69504667 :       if (!boff.to_shwi (bit_offset))
    9881     69166107 :         return NULL_TREE;
    9882              : 
    9883     69504320 :       if (valueize
    9884     69504320 :           && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
    9885     37578514 :         base = valueize (TREE_OPERAND (base, 0));
    9886     69504320 :       if (!base || TREE_CODE (base) != ADDR_EXPR)
    9887              :         return NULL_TREE;
    9888       338560 :       base = TREE_OPERAND (base, 0);
    9889              :     }
    9890     60123683 :   else if (valueize
    9891     32094842 :            && TREE_CODE (base) == SSA_NAME)
    9892            0 :     base = valueize (base);
    9893              : 
    9894              :   /* Get a CONSTRUCTOR.  If BASE is a VAR_DECL, get its
    9895              :      DECL_INITIAL.  If BASE is a nested reference into another
    9896              :      ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
    9897              :      the inner reference.  */
    9898     60462243 :   switch (TREE_CODE (base))
    9899              :     {
    9900     52750238 :     case VAR_DECL:
    9901     52750238 :     case CONST_DECL:
    9902     52750238 :       {
    9903     52750238 :         tree init = ctor_for_folding (base);
    9904              : 
    9905              :         /* Our semantic is exact opposite of ctor_for_folding;
    9906              :            NULL means unknown, while error_mark_node is 0.  */
    9907     52750238 :         if (init == error_mark_node)
    9908              :           return NULL_TREE;
    9909      1309675 :         if (!init)
    9910         1173 :           return error_mark_node;
    9911              :         return init;
    9912              :       }
    9913              : 
    9914        95838 :     case VIEW_CONVERT_EXPR:
    9915        95838 :       return get_base_constructor (TREE_OPERAND (base, 0),
    9916        95838 :                                    bit_offset, valueize);
    9917              : 
    9918       333939 :     case ARRAY_REF:
    9919       333939 :     case COMPONENT_REF:
    9920       333939 :       base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
    9921              :                                       &reverse);
    9922       333939 :       if (!known_size_p (max_size) || maybe_ne (size, max_size))
    9923              :         return NULL_TREE;
    9924       273368 :       *bit_offset +=  bit_offset2;
    9925       273368 :       return get_base_constructor (base, bit_offset, valueize);
    9926              : 
    9927              :     case CONSTRUCTOR:
    9928              :       return base;
    9929              : 
    9930      7282228 :     default:
    9931      7282228 :       if (CONSTANT_CLASS_P (base))
    9932              :         return base;
    9933              : 
    9934              :       return NULL_TREE;
    9935              :     }
    9936              : }
    9937              : 
    9938              : /* CTOR is a CONSTRUCTOR of an array or vector type.  Fold a reference of SIZE
    9939              :    bits to the memory at bit OFFSET.  If non-null, TYPE is the expected type of
    9940              :    the reference; otherwise the type of the referenced element is used instead.
    9941              :    When SIZE is zero, attempt to fold a reference to the entire element OFFSET
    9942              :    refers to.  Increment *SUBOFF by the bit offset of the accessed element.  */
    9943              : 
    9944              : static tree
    9945       718056 : fold_array_ctor_reference (tree type, tree ctor,
    9946              :                            unsigned HOST_WIDE_INT offset,
    9947              :                            unsigned HOST_WIDE_INT size,
    9948              :                            tree from_decl,
    9949              :                            unsigned HOST_WIDE_INT *suboff)
    9950              : {
    9951       718056 :   offset_int low_bound;
    9952       718056 :   offset_int elt_size;
    9953       718056 :   offset_int access_index;
    9954       718056 :   tree domain_type = NULL_TREE;
    9955       718056 :   HOST_WIDE_INT inner_offset;
    9956              : 
    9957              :   /* Compute low bound and elt size.  */
    9958       718056 :   if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
    9959       718056 :     domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
    9960       718056 :   if (domain_type && TYPE_MIN_VALUE (domain_type))
    9961              :     {
    9962              :       /* Static constructors for variably sized objects make no sense.  */
    9963       718056 :       if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
    9964              :         return NULL_TREE;
    9965       718056 :       low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
    9966              :     }
    9967              :   else
    9968            0 :     low_bound = 0;
    9969              :   /* Static constructors for variably sized objects make no sense.  */
    9970       718056 :   if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
    9971              :     return NULL_TREE;
    9972       718056 :   elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
    9973              : 
    9974              :   /* When TYPE is non-null, verify that it specifies a constant-sized
    9975              :      access of a multiple of the array element size.  Avoid division
    9976              :      by zero below when ELT_SIZE is zero, such as with the result of
    9977              :      an initializer for a zero-length array or an empty struct.  */
    9978       718056 :   if (elt_size == 0
    9979       718056 :       || (type
    9980       718020 :           && (!TYPE_SIZE_UNIT (type)
    9981       718020 :               || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)))
    9982           36 :     return NULL_TREE;
    9983              : 
    9984              :   /* Compute the array index we look for.  */
    9985       718020 :   access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
    9986              :                                  elt_size);
    9987       718020 :   access_index += low_bound;
    9988              : 
    9989              :   /* And offset within the access.  */
    9990       718020 :   inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
    9991              : 
    9992       718020 :   unsigned HOST_WIDE_INT elt_sz = elt_size.to_uhwi ();
    9993       718020 :   if (size > elt_sz * BITS_PER_UNIT)
    9994              :     {
    9995              :       /* native_encode_expr constraints.  */
    9996        51332 :       if (size > MAX_BITSIZE_MODE_ANY_MODE
    9997        40553 :           || size % BITS_PER_UNIT != 0
    9998        40553 :           || inner_offset % BITS_PER_UNIT != 0
    9999        40553 :           || elt_sz > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
   10000              :         return NULL_TREE;
   10001              : 
   10002        40553 :       unsigned ctor_idx;
   10003        40553 :       tree val = get_array_ctor_element_at_index (ctor, access_index,
   10004              :                                                   &ctor_idx);
   10005        40577 :       if (!val && ctor_idx >= CONSTRUCTOR_NELTS (ctor))
   10006           23 :         return build_zero_cst (type);
   10007              : 
   10008              :       /* native-encode adjacent ctor elements.  */
   10009        40530 :       unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
   10010        40530 :       unsigned bufoff = 0;
   10011        40530 :       offset_int index = 0;
   10012        40530 :       offset_int max_index = access_index;
   10013        40530 :       constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
   10014        40530 :       if (!val)
   10015            1 :         val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
   10016        40529 :       else if (!CONSTANT_CLASS_P (val))
   10017              :         return NULL_TREE;
   10018        40127 :       if (!elt->index)
   10019              :         ;
   10020        33836 :       else if (TREE_CODE (elt->index) == RANGE_EXPR)
   10021              :         {
   10022           20 :           index = wi::to_offset (TREE_OPERAND (elt->index, 0));
   10023           20 :           max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
   10024              :         }
   10025              :       else
   10026        33816 :         index = max_index = wi::to_offset (elt->index);
   10027        40127 :       index = wi::umax (index, access_index);
   10028       273629 :       do
   10029              :         {
   10030       273629 :           if (bufoff + elt_sz > sizeof (buf))
   10031            0 :             elt_sz = sizeof (buf) - bufoff;
   10032       273629 :           int len;
   10033       273629 :           if (TREE_CODE (val) == RAW_DATA_CST)
   10034              :             {
   10035           20 :               gcc_assert (inner_offset == 0);
   10036           20 :               if (!elt->index || TREE_CODE (elt->index) != INTEGER_CST)
   10037              :                 return NULL_TREE;
   10038           40 :               inner_offset = (access_index
   10039           20 :                               - wi::to_offset (elt->index)).to_uhwi ();
   10040           20 :               len = MIN (sizeof (buf) - bufoff,
   10041              :                          (unsigned) (RAW_DATA_LENGTH (val) - inner_offset));
   10042           20 :               memcpy (buf + bufoff, RAW_DATA_POINTER (val) + inner_offset,
   10043              :                       len);
   10044           20 :               access_index += len - 1;
   10045              :             }
   10046              :           else
   10047              :             {
   10048       547218 :               len = native_encode_expr (val, buf + bufoff, elt_sz,
   10049       273609 :                                         inner_offset / BITS_PER_UNIT);
   10050       273609 :               if (len != (int) elt_sz - inner_offset / BITS_PER_UNIT)
   10051              :                 return NULL_TREE;
   10052              :             }
   10053       273629 :           inner_offset = 0;
   10054       273629 :           bufoff += len;
   10055              : 
   10056       273629 :           access_index += 1;
   10057       273629 :           if (wi::cmpu (access_index, index) == 0)
   10058            2 :             val = elt->value;
   10059       273627 :           else if (wi::cmpu (access_index, max_index) > 0)
   10060              :             {
   10061       273387 :               ctor_idx++;
   10062       273387 :               if (ctor_idx >= CONSTRUCTOR_NELTS (ctor))
   10063              :                 {
   10064        38392 :                   val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
   10065        38392 :                   ++max_index;
   10066              :                 }
   10067              :               else
   10068              :                 {
   10069       234995 :                   elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
   10070       234995 :                   index = 0;
   10071       234995 :                   max_index = access_index;
   10072       234995 :                   if (!elt->index)
   10073              :                     ;
   10074       234187 :                   else if (TREE_CODE (elt->index) == RANGE_EXPR)
   10075              :                     {
   10076            0 :                       index = wi::to_offset (TREE_OPERAND (elt->index, 0));
   10077            0 :                       max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
   10078              :                     }
   10079              :                   else
   10080       234187 :                     index = max_index = wi::to_offset (elt->index);
   10081       234995 :                   index = wi::umax (index, access_index);
   10082       234995 :                   if (wi::cmpu (access_index, index) == 0)
   10083       234990 :                     val = elt->value;
   10084              :                   else
   10085            5 :                     val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
   10086              :                 }
   10087              :             }
   10088              :         }
   10089       273629 :       while (bufoff < size / BITS_PER_UNIT);
   10090        40127 :       *suboff += size;
   10091        40127 :       return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
   10092              :     }
   10093              : 
   10094       666688 :   unsigned ctor_idx;
   10095       666688 :   if (tree val = get_array_ctor_element_at_index (ctor, access_index,
   10096              :                                                   &ctor_idx))
   10097              :     {
   10098       665567 :       if (TREE_CODE (val) == RAW_DATA_CST)
   10099              :         {
   10100         2618 :           if (size != BITS_PER_UNIT || elt_sz != 1 || inner_offset != 0)
   10101              :             return NULL_TREE;
   10102         2610 :           constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
   10103         2610 :           if (elt->index == NULL_TREE || TREE_CODE (elt->index) != INTEGER_CST)
   10104              :             return NULL_TREE;
   10105         2610 :           unsigned o = (access_index - wi::to_offset (elt->index)).to_uhwi ();
   10106         2610 :           val = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
   10107              :         }
   10108       665559 :       if (!size && TREE_CODE (val) != CONSTRUCTOR)
   10109              :         {
   10110              :           /* For the final reference to the entire accessed element
   10111              :              (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
   10112              :              may be null) in favor of the type of the element, and set
   10113              :              SIZE to the size of the accessed element.  */
   10114        22947 :           inner_offset = 0;
   10115        22947 :           type = TREE_TYPE (val);
   10116        22947 :           size = elt_sz * BITS_PER_UNIT;
   10117              :         }
   10118      1716738 :       else if (size && access_index < CONSTRUCTOR_NELTS (ctor) - 1
   10119       469629 :                && TREE_CODE (val) == CONSTRUCTOR
   10120        16880 :                && (elt_sz * BITS_PER_UNIT - inner_offset) < size)
   10121              :         /* If this isn't the last element in the CTOR and a CTOR itself
   10122              :            and it does not cover the whole object we are requesting give up
   10123              :            since we're not set up for combining from multiple CTORs.  */
   10124           26 :         return NULL_TREE;
   10125              : 
   10126       665533 :       *suboff += access_index.to_uhwi () * elt_sz * BITS_PER_UNIT;
   10127       665533 :       return fold_ctor_reference (type, val, inner_offset, size, from_decl,
   10128              :                                   suboff);
   10129              :     }
   10130              : 
   10131              :   /* Memory not explicitly mentioned in constructor is 0 (or
   10132              :      the reference is out of range).  */
   10133         1121 :   return type ? build_zero_cst (type) : NULL_TREE;
   10134              : }
   10135              : 
   10136              : /* CTOR is a CONSTRUCTOR of a record or union type.  Fold a reference of SIZE
   10137              :    bits to the memory at bit OFFSET.  If non-null, TYPE is the expected type of
   10138              :    the reference; otherwise the type of the referenced member is used instead.
   10139              :    When SIZE is zero, attempt to fold a reference to the entire member OFFSET
   10140              :    refers to.  Increment *SUBOFF by the bit offset of the accessed member.  */
   10141              : 
   10142              : static tree
   10143        76353 : fold_nonarray_ctor_reference (tree type, tree ctor,
   10144              :                               unsigned HOST_WIDE_INT offset,
   10145              :                               unsigned HOST_WIDE_INT size,
   10146              :                               tree from_decl,
   10147              :                               unsigned HOST_WIDE_INT *suboff)
   10148              : {
   10149        76353 :   unsigned HOST_WIDE_INT cnt;
   10150        76353 :   tree cfield, cval;
   10151              : 
   10152       116452 :   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
   10153              :     {
   10154       107493 :       tree byte_offset = DECL_FIELD_OFFSET (cfield);
   10155       107493 :       tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
   10156       107493 :       tree field_size = DECL_SIZE (cfield);
   10157              : 
   10158       107493 :       if (!field_size)
   10159              :         {
   10160              :           /* Determine the size of the flexible array member from
   10161              :              the size of the initializer provided for it.  */
   10162          847 :           field_size = TYPE_SIZE (TREE_TYPE (cval));
   10163              :         }
   10164              : 
   10165              :       /* Variable sized objects in static constructors makes no sense,
   10166              :          but field_size can be NULL for flexible array members.  */
   10167       107493 :       gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
   10168              :                   && TREE_CODE (byte_offset) == INTEGER_CST
   10169              :                   && (field_size != NULL_TREE
   10170              :                       ? TREE_CODE (field_size) == INTEGER_CST
   10171              :                       : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
   10172              : 
   10173              :       /* Compute bit offset of the field.  */
   10174       107493 :       offset_int bitoffset
   10175       107493 :         = (wi::to_offset (field_offset)
   10176       107493 :            + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
   10177              :       /* Compute bit offset where the field ends.  */
   10178       107493 :       offset_int bitoffset_end;
   10179       107493 :       if (field_size != NULL_TREE)
   10180       107493 :         bitoffset_end = bitoffset + wi::to_offset (field_size);
   10181              :       else
   10182            0 :         bitoffset_end = 0;
   10183              : 
   10184              :       /* Compute the bit offset of the end of the desired access.
   10185              :          As a special case, if the size of the desired access is
   10186              :          zero, assume the access is to the entire field (and let
   10187              :          the caller make any necessary adjustments by storing
   10188              :          the actual bounds of the field in FIELDBOUNDS).  */
   10189       107493 :       offset_int access_end = offset_int (offset);
   10190       107493 :       if (size)
   10191        66065 :         access_end += size;
   10192              :       else
   10193        41428 :         access_end = bitoffset_end;
   10194              : 
   10195              :       /* Is there any overlap between the desired access at
   10196              :          [OFFSET, OFFSET+SIZE) and the offset of the field within
   10197              :          the object at [BITOFFSET, BITOFFSET_END)?  */
   10198       107493 :       if (wi::cmps (access_end, bitoffset) > 0
   10199       107493 :           && (field_size == NULL_TREE
   10200       105162 :               || wi::lts_p (offset, bitoffset_end)))
   10201              :         {
   10202        67394 :           *suboff += bitoffset.to_uhwi ();
   10203              : 
   10204        67394 :           if (!size && TREE_CODE (cval) != CONSTRUCTOR)
   10205              :             {
   10206              :               /* For the final reference to the entire accessed member
   10207              :                  (SIZE is zero), reset OFFSET, disegard TYPE (which may
   10208              :                  be null) in favor of the type of the member, and set
   10209              :                  SIZE to the size of the accessed member.  */
   10210        19305 :               offset = bitoffset.to_uhwi ();
   10211        19305 :               type = TREE_TYPE (cval);
   10212        19305 :               size = (bitoffset_end - bitoffset).to_uhwi ();
   10213              :             }
   10214              : 
   10215              :           /* We do have overlap.  Now see if the field is large enough
   10216              :              to cover the access.  Give up for accesses that extend
   10217              :              beyond the end of the object or that span multiple fields.  */
   10218        67394 :           if (wi::cmps (access_end, bitoffset_end) > 0)
   10219              :             return NULL_TREE;
   10220        66762 :           if (offset < bitoffset)
   10221              :             return NULL_TREE;
   10222              : 
   10223        66762 :           offset_int inner_offset = offset_int (offset) - bitoffset;
   10224              : 
   10225              :           /* Integral bit-fields are left-justified on big-endian targets, so
   10226              :              we must arrange for native_encode_int to start at their MSB.  */
   10227        66762 :           if (DECL_BIT_FIELD (cfield) && INTEGRAL_TYPE_P (TREE_TYPE (cfield)))
   10228              :             {
   10229        66762 :               if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
   10230              :                 return NULL_TREE;
   10231        66762 :               if (BYTES_BIG_ENDIAN)
   10232              :                 {
   10233              :                   tree ctype = TREE_TYPE (cfield);
   10234              :                   unsigned int encoding_size;
   10235              :                   if (TYPE_MODE (ctype) != BLKmode)
   10236              :                     encoding_size
   10237              :                       = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (ctype));
   10238              :                   else
   10239              :                     encoding_size = TREE_INT_CST_LOW (TYPE_SIZE (ctype));
   10240              :                   inner_offset += encoding_size - wi::to_offset (field_size);
   10241              :                 }
   10242              :             }
   10243              : 
   10244        66762 :           return fold_ctor_reference (type, cval,
   10245        66762 :                                       inner_offset.to_uhwi (), size,
   10246              :                                       from_decl, suboff);
   10247              :         }
   10248              :     }
   10249              : 
   10250         8959 :   if (!type)
   10251              :     return NULL_TREE;
   10252              : 
   10253         8959 :   return build_zero_cst (type);
   10254              : }
   10255              : 
   10256              : /* CTOR is a value initializing memory.  Fold a reference of TYPE and
   10257              :    bit size POLY_SIZE to the memory at bit POLY_OFFSET.  When POLY_SIZE
   10258              :    is zero, attempt to fold a reference to the entire subobject
   10259              :    which OFFSET refers to.  This is used when folding accesses to
   10260              :    string members of aggregates.  When non-null, set *SUBOFF to
   10261              :    the bit offset of the accessed subobject.  */
   10262              : 
   10263              : tree
   10264      1646325 : fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset,
   10265              :                      const poly_uint64 &poly_size, tree from_decl,
   10266              :                      unsigned HOST_WIDE_INT *suboff /* = NULL */)
   10267              : {
   10268      1646325 :   tree ret;
   10269              : 
   10270              :   /* We found the field with exact match.  */
   10271      1646325 :   if (type
   10272      1646325 :       && useless_type_conversion_p (type, TREE_TYPE (ctor))
   10273      2328502 :       && known_eq (poly_offset, 0U))
   10274       680923 :     return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
   10275              : 
   10276              :   /* The remaining optimizations need a constant size and offset.  */
   10277       965402 :   unsigned HOST_WIDE_INT size, offset;
   10278       965402 :   if (!poly_size.is_constant (&size) || !poly_offset.is_constant (&offset))
   10279              :     return NULL_TREE;
   10280              : 
   10281              :   /* We are at the end of walk, see if we can view convert the
   10282              :      result.  */
   10283       965402 :   if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
   10284              :       /* VIEW_CONVERT_EXPR is defined only for matching sizes.  */
   10285        23197 :       && known_eq (wi::to_poly_widest (TYPE_SIZE (type)), size)
   10286        23090 :       && known_eq (wi::to_poly_widest (TYPE_SIZE (TREE_TYPE (ctor))), size))
   10287              :     {
   10288        14390 :       ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
   10289        14390 :       if (ret)
   10290              :         {
   10291        14390 :           ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
   10292        14390 :           if (ret)
   10293        14330 :             STRIP_USELESS_TYPE_CONVERSION (ret);
   10294              :         }
   10295        14390 :       return ret;
   10296              :     }
   10297              : 
   10298              :   /* For constants and byte-aligned/sized reads, try to go through
   10299              :      native_encode/interpret.  */
   10300       951012 :   if (CONSTANT_CLASS_P (ctor)
   10301              :       && BITS_PER_UNIT == 8
   10302       147175 :       && offset % BITS_PER_UNIT == 0
   10303       147171 :       && offset / BITS_PER_UNIT <= INT_MAX
   10304       147139 :       && size % BITS_PER_UNIT == 0
   10305       147130 :       && size <= MAX_BITSIZE_MODE_ANY_MODE
   10306      1097337 :       && can_native_interpret_type_p (type))
   10307              :     {
   10308       103798 :       unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
   10309       207596 :       int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
   10310       103798 :                                     offset / BITS_PER_UNIT);
   10311       103798 :       if (len > 0)
   10312       103080 :         return native_interpret_expr (type, buf, len);
   10313              :     }
   10314              : 
   10315              :   /* For constructors, try first a recursive local processing, but in any case
   10316              :      this requires the native storage order.  */
   10317       847932 :   if (TREE_CODE (ctor) == CONSTRUCTOR
   10318       847932 :       && !(AGGREGATE_TYPE_P (TREE_TYPE (ctor))
   10319       794639 :            && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (ctor))))
   10320              :     {
   10321       794409 :       unsigned HOST_WIDE_INT dummy = 0;
   10322       794409 :       if (!suboff)
   10323       668979 :         suboff = &dummy;
   10324              : 
   10325       794409 :       tree ret;
   10326       794409 :       if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
   10327       794409 :           || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
   10328       718056 :         ret = fold_array_ctor_reference (type, ctor, offset, size,
   10329              :                                          from_decl, suboff);
   10330              :       else
   10331        76353 :         ret = fold_nonarray_ctor_reference (type, ctor, offset, size,
   10332              :                                             from_decl, suboff);
   10333              : 
   10334              :       /* Otherwise fall back to native_encode_initializer.  This may be done
   10335              :          only from the outermost fold_ctor_reference call (because it itself
   10336              :          recurses into CONSTRUCTORs and doesn't update suboff).  */
   10337       794409 :       if (ret == NULL_TREE
   10338       257090 :           && suboff == &dummy
   10339              :           && BITS_PER_UNIT == 8
   10340       247527 :           && offset % BITS_PER_UNIT == 0
   10341       247525 :           && offset / BITS_PER_UNIT <= INT_MAX
   10342       247525 :           && size % BITS_PER_UNIT == 0
   10343       247516 :           && size <= MAX_BITSIZE_MODE_ANY_MODE
   10344      1031146 :           && can_native_interpret_type_p (type))
   10345              :         {
   10346       199705 :           unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
   10347       399410 :           int len = native_encode_initializer (ctor, buf, size / BITS_PER_UNIT,
   10348       199705 :                                                offset / BITS_PER_UNIT);
   10349       199705 :           if (len > 0)
   10350         1302 :             return native_interpret_expr (type, buf, len);
   10351              :         }
   10352              : 
   10353       793107 :       return ret;
   10354              :     }
   10355              : 
   10356              :   return NULL_TREE;
   10357              : }
   10358              : 
   10359              : /* Return the tree representing the element referenced by T if T is an
   10360              :    ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
   10361              :    names using VALUEIZE.  Return NULL_TREE otherwise.  */
   10362              : 
   10363              : tree
   10364    135041148 : fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
   10365              : {
   10366    135041148 :   tree ctor, idx, base;
   10367    135041148 :   poly_int64 offset, size, max_size;
   10368    135041148 :   tree tem;
   10369    135041148 :   bool reverse;
   10370              : 
   10371    135041148 :   if (TREE_THIS_VOLATILE (t))
   10372              :     return NULL_TREE;
   10373              : 
   10374    134722146 :   if (DECL_P (t))
   10375       260168 :     return get_symbol_constant_value (t);
   10376              : 
   10377    134461978 :   tem = fold_read_from_constant_string (t);
   10378    134461978 :   if (tem)
   10379              :     return tem;
   10380              : 
   10381    134460111 :   switch (TREE_CODE (t))
   10382              :     {
   10383     10434931 :     case ARRAY_REF:
   10384     10434931 :     case ARRAY_RANGE_REF:
   10385              :       /* Constant indexes are handled well by get_base_constructor.
   10386              :          Only special case variable offsets.
   10387              :          FIXME: This code can't handle nested references with variable indexes
   10388              :          (they will be handled only by iteration of ccp).  Perhaps we can bring
   10389              :          get_ref_base_and_extent here and make it use a valueize callback.  */
   10390     10434931 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
   10391      6123178 :           && valueize
   10392      4100820 :           && (idx = (*valueize) (TREE_OPERAND (t, 1)))
   10393     14535751 :           && poly_int_tree_p (idx))
   10394              :         {
   10395      1637503 :           tree low_bound, unit_size;
   10396              : 
   10397              :           /* If the resulting bit-offset is constant, track it.  */
   10398      1637503 :           if ((low_bound = array_ref_low_bound (t),
   10399      1637503 :                poly_int_tree_p (low_bound))
   10400      1637503 :               && (unit_size = array_ref_element_size (t),
   10401      1637503 :                   tree_fits_uhwi_p (unit_size)))
   10402              :             {
   10403      1637503 :               poly_offset_int woffset
   10404      1637503 :                 = wi::sext (wi::to_poly_offset (idx)
   10405      3275006 :                             - wi::to_poly_offset (low_bound),
   10406      1637503 :                             TYPE_PRECISION (sizetype));
   10407      1637503 :               woffset *= tree_to_uhwi (unit_size);
   10408      1637503 :               woffset *= BITS_PER_UNIT;
   10409      1637503 :               if (woffset.to_shwi (&offset))
   10410              :                 {
   10411      1637372 :                   base = TREE_OPERAND (t, 0);
   10412      1637372 :                   ctor = get_base_constructor (base, &offset, valueize);
   10413              :                   /* Empty constructor.  Always fold to 0.  */
   10414      1637372 :                   if (ctor == error_mark_node)
   10415      1637372 :                     return build_zero_cst (TREE_TYPE (t));
   10416              :                   /* Out of bound array access.  Value is undefined,
   10417              :                      but don't fold.  */
   10418      1637294 :                   if (maybe_lt (offset, 0))
   10419              :                     return NULL_TREE;
   10420              :                   /* We cannot determine ctor.  */
   10421      1636824 :                   if (!ctor)
   10422              :                     return NULL_TREE;
   10423       170700 :                   return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
   10424       170700 :                                               tree_to_uhwi (unit_size)
   10425       341400 :                                               * BITS_PER_UNIT,
   10426              :                                               base);
   10427              :                 }
   10428              :             }
   10429              :         }
   10430              :       /* Fallthru.  */
   10431              : 
   10432    127621772 :     case COMPONENT_REF:
   10433    127621772 :     case BIT_FIELD_REF:
   10434    127621772 :     case TARGET_MEM_REF:
   10435    127621772 :     case MEM_REF:
   10436    127621772 :       base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
   10437    127621772 :       ctor = get_base_constructor (base, &offset, valueize);
   10438              : 
   10439              :       /* We cannot determine ctor.  */
   10440    127621772 :       if (!ctor)
   10441              :         return NULL_TREE;
   10442              :       /* Empty constructor.  Always fold to 0.  */
   10443      1255573 :       if (ctor == error_mark_node)
   10444         1095 :         return build_zero_cst (TREE_TYPE (t));
   10445              :       /* We do not know precise access.  */
   10446      1254478 :       if (!known_size_p (max_size) || maybe_ne (max_size, size))
   10447              :         return NULL_TREE;
   10448              :       /* Out of bound array access.  Value is undefined, but don't fold.  */
   10449       563001 :       if (maybe_lt (offset, 0))
   10450              :         return NULL_TREE;
   10451              :       /* Access with reverse storage order.  */
   10452       562620 :       if (reverse)
   10453              :         return NULL_TREE;
   10454              : 
   10455       562620 :       tem = fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, base);
   10456       562620 :       if (tem)
   10457              :         return tem;
   10458              : 
   10459              :       /* For bit field reads try to read the representative and
   10460              :          adjust.  */
   10461       247818 :       if (TREE_CODE (t) == COMPONENT_REF
   10462         6353 :           && DECL_BIT_FIELD (TREE_OPERAND (t, 1))
   10463       247902 :           && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)))
   10464              :         {
   10465           84 :           HOST_WIDE_INT csize, coffset;
   10466           84 :           tree field = TREE_OPERAND (t, 1);
   10467           84 :           tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
   10468          168 :           if (INTEGRAL_TYPE_P (TREE_TYPE (repr))
   10469           83 :               && size.is_constant (&csize)
   10470           83 :               && offset.is_constant (&coffset)
   10471           83 :               && (coffset % BITS_PER_UNIT != 0
   10472           81 :                   || csize % BITS_PER_UNIT != 0)
   10473           84 :               && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
   10474              :             {
   10475           10 :               poly_int64 bitoffset;
   10476           10 :               poly_uint64 field_offset, repr_offset;
   10477           10 :               if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
   10478           20 :                   && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
   10479           10 :                 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
   10480              :               else
   10481              :                 bitoffset = 0;
   10482           10 :               bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
   10483           10 :                             - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
   10484           10 :               HOST_WIDE_INT bitoff;
   10485           10 :               int diff = (TYPE_PRECISION (TREE_TYPE (repr))
   10486           10 :                           - TYPE_PRECISION (TREE_TYPE (field)));
   10487           10 :               if (bitoffset.is_constant (&bitoff)
   10488           10 :                   && bitoff >= 0
   10489           10 :                   && bitoff <= diff)
   10490              :                 {
   10491           10 :                   offset -= bitoff;
   10492           10 :                   size = tree_to_uhwi (DECL_SIZE (repr));
   10493              : 
   10494           10 :                   tem = fold_ctor_reference (TREE_TYPE (repr), ctor, offset,
   10495           10 :                                              size, base);
   10496           10 :                   if (tem && TREE_CODE (tem) == INTEGER_CST)
   10497              :                     {
   10498           10 :                       if (!BYTES_BIG_ENDIAN)
   10499           10 :                         tem = wide_int_to_tree (TREE_TYPE (field),
   10500           10 :                                                 wi::lrshift (wi::to_wide (tem),
   10501              :                                                              bitoff));
   10502              :                       else
   10503              :                         tem = wide_int_to_tree (TREE_TYPE (field),
   10504              :                                                 wi::lrshift (wi::to_wide (tem),
   10505              :                                                              diff - bitoff));
   10506           10 :                       return tem;
   10507              :                     }
   10508              :                 }
   10509              :             }
   10510              :         }
   10511              :       break;
   10512              : 
   10513      1569110 :     case REALPART_EXPR:
   10514      1569110 :     case IMAGPART_EXPR:
   10515      1569110 :       {
   10516      1569110 :         tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
   10517      1569110 :         if (c && TREE_CODE (c) == COMPLEX_CST)
   10518         2774 :           return fold_build1_loc (EXPR_LOCATION (t),
   10519         5548 :                                   TREE_CODE (t), TREE_TYPE (t), c);
   10520              :         break;
   10521              :       }
   10522              : 
   10523              :     default:
   10524              :       break;
   10525              :     }
   10526              : 
   10527              :   return NULL_TREE;
   10528              : }
   10529              : 
   10530              : tree
   10531     63404659 : fold_const_aggregate_ref (tree t)
   10532              : {
   10533     63404659 :   return fold_const_aggregate_ref_1 (t, NULL);
   10534              : }
   10535              : 
   10536              : /* Lookup virtual method with index TOKEN in a virtual table V
   10537              :    at OFFSET.
   10538              :    Set CAN_REFER if non-NULL to false if method
   10539              :    is not referable or if the virtual table is ill-formed (such as rewriten
   10540              :    by non-C++ produced symbol). Otherwise just return NULL in that calse.  */
   10541              : 
   10542              : tree
   10543       279085 : gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
   10544              :                                    tree v,
   10545              :                                    unsigned HOST_WIDE_INT offset,
   10546              :                                    bool *can_refer)
   10547              : {
   10548       279085 :   tree vtable = v, init, fn;
   10549       279085 :   unsigned HOST_WIDE_INT size;
   10550       279085 :   unsigned HOST_WIDE_INT elt_size, access_index;
   10551       279085 :   tree domain_type;
   10552              : 
   10553       279085 :   if (can_refer)
   10554       279085 :     *can_refer = true;
   10555              : 
   10556              :   /* First of all double check we have virtual table.  */
   10557       279085 :   if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
   10558              :     {
   10559              :       /* Pass down that we lost track of the target.  */
   10560            0 :       if (can_refer)
   10561            0 :         *can_refer = false;
   10562            0 :       return NULL_TREE;
   10563              :     }
   10564              : 
   10565       279085 :   init = ctor_for_folding (v);
   10566              : 
   10567              :   /* The virtual tables should always be born with constructors
   10568              :      and we always should assume that they are avaialble for
   10569              :      folding.  At the moment we do not stream them in all cases,
   10570              :      but it should never happen that ctor seem unreachable.  */
   10571       279085 :   gcc_assert (init);
   10572       279085 :   if (init == error_mark_node)
   10573              :     {
   10574              :       /* Pass down that we lost track of the target.  */
   10575          209 :       if (can_refer)
   10576          209 :         *can_refer = false;
   10577          209 :       return NULL_TREE;
   10578              :     }
   10579       278876 :   gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
   10580       278876 :   size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
   10581       278876 :   offset *= BITS_PER_UNIT;
   10582       278876 :   offset += token * size;
   10583              : 
   10584              :   /* Lookup the value in the constructor that is assumed to be array.
   10585              :      This is equivalent to
   10586              :      fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
   10587              :                                offset, size, NULL);
   10588              :      but in a constant time.  We expect that frontend produced a simple
   10589              :      array without indexed initializers.  */
   10590              : 
   10591       278876 :   gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
   10592       278876 :   domain_type = TYPE_DOMAIN (TREE_TYPE (init));
   10593       278876 :   gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
   10594       278876 :   elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
   10595              : 
   10596       278876 :   access_index = offset / BITS_PER_UNIT / elt_size;
   10597       278876 :   gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
   10598              : 
   10599              :   /* This code makes an assumption that there are no
   10600              :      indexed fileds produced by C++ FE, so we can directly index the array.  */
   10601       278876 :   if (access_index < CONSTRUCTOR_NELTS (init))
   10602              :     {
   10603       278875 :       fn = CONSTRUCTOR_ELT (init, access_index)->value;
   10604       278875 :       gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
   10605       278875 :       STRIP_NOPS (fn);
   10606              :     }
   10607              :   else
   10608              :     fn = NULL;
   10609              : 
   10610              :   /* For type inconsistent program we may end up looking up virtual method
   10611              :      in virtual table that does not contain TOKEN entries.  We may overrun
   10612              :      the virtual table and pick up a constant or RTTI info pointer.
   10613              :      In any case the call is undefined.  */
   10614       278875 :   if (!fn
   10615       278875 :       || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
   10616       551872 :       || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
   10617         5879 :     fn = builtin_decl_unreachable ();
   10618              :   else
   10619              :     {
   10620       272997 :       fn = TREE_OPERAND (fn, 0);
   10621              : 
   10622              :       /* When cgraph node is missing and function is not public, we cannot
   10623              :          devirtualize.  This can happen in WHOPR when the actual method
   10624              :          ends up in other partition, because we found devirtualization
   10625              :          possibility too late.  */
   10626       272997 :       if (!can_refer_decl_in_current_unit_p (fn, vtable))
   10627              :         {
   10628        36051 :           if (can_refer)
   10629              :             {
   10630        36051 :               *can_refer = false;
   10631        36051 :               return fn;
   10632              :             }
   10633              :           return NULL_TREE;
   10634              :         }
   10635              :     }
   10636              : 
   10637              :   /* Make sure we create a cgraph node for functions we'll reference.
   10638              :      They can be non-existent if the reference comes from an entry
   10639              :      of an external vtable for example.  */
   10640       242825 :   cgraph_node::get_create (fn);
   10641              : 
   10642       242825 :   return fn;
   10643              : }
   10644              : 
   10645              : /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
   10646              :    is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
   10647              :    KNOWN_BINFO carries the binfo describing the true type of
   10648              :    OBJ_TYPE_REF_OBJECT(REF).
   10649              :    Set CAN_REFER if non-NULL to false if method
   10650              :    is not referable or if the virtual table is ill-formed (such as rewriten
   10651              :    by non-C++ produced symbol). Otherwise just return NULL in that calse.  */
   10652              : 
   10653              : tree
   10654       271165 : gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
   10655              :                                   bool *can_refer)
   10656              : {
   10657       271165 :   unsigned HOST_WIDE_INT offset;
   10658       271165 :   tree v;
   10659              : 
   10660       271165 :   v = BINFO_VTABLE (known_binfo);
   10661              :   /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone.  */
   10662       271165 :   if (!v)
   10663              :     return NULL_TREE;
   10664              : 
   10665       271165 :   if (!vtable_pointer_value_to_vtable (v, &v, &offset))
   10666              :     {
   10667            0 :       if (can_refer)
   10668            0 :         *can_refer = false;
   10669            0 :       return NULL_TREE;
   10670              :     }
   10671       271165 :   return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
   10672              : }
   10673              : 
   10674              : /* Given a pointer value T, return a simplified version of an
   10675              :    indirection through T, or NULL_TREE if no simplification is
   10676              :    possible.  Note that the resulting type may be different from
   10677              :    the type pointed to in the sense that it is still compatible
   10678              :    from the langhooks point of view. */
   10679              : 
   10680              : tree
   10681      2363407 : gimple_fold_indirect_ref (tree t)
   10682              : {
   10683      2363407 :   tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
   10684      2363407 :   tree sub = t;
   10685      2363407 :   tree subtype;
   10686              : 
   10687      2363407 :   STRIP_NOPS (sub);
   10688      2363407 :   subtype = TREE_TYPE (sub);
   10689      2363407 :   if (!POINTER_TYPE_P (subtype)
   10690      2363407 :       || TYPE_REF_CAN_ALIAS_ALL (ptype))
   10691              :     return NULL_TREE;
   10692              : 
   10693      2361702 :   if (TREE_CODE (sub) == ADDR_EXPR)
   10694              :     {
   10695        88255 :       tree op = TREE_OPERAND (sub, 0);
   10696        88255 :       tree optype = TREE_TYPE (op);
   10697              :       /* *&p => p */
   10698        88255 :       if (useless_type_conversion_p (type, optype))
   10699              :         return op;
   10700              : 
   10701              :       /* *(foo *)&fooarray => fooarray[0] */
   10702          992 :       if (TREE_CODE (optype) == ARRAY_TYPE
   10703          307 :           && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
   10704         1299 :           && useless_type_conversion_p (type, TREE_TYPE (optype)))
   10705              :        {
   10706           54 :          tree type_domain = TYPE_DOMAIN (optype);
   10707           54 :          tree min_val = size_zero_node;
   10708           54 :          if (type_domain && TYPE_MIN_VALUE (type_domain))
   10709           54 :            min_val = TYPE_MIN_VALUE (type_domain);
   10710           54 :          if (TREE_CODE (min_val) == INTEGER_CST)
   10711           54 :            return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
   10712              :        }
   10713              :       /* *(foo *)&complexfoo => __real__ complexfoo */
   10714          938 :       else if (TREE_CODE (optype) == COMPLEX_TYPE
   10715          938 :                && useless_type_conversion_p (type, TREE_TYPE (optype)))
   10716            4 :         return fold_build1 (REALPART_EXPR, type, op);
   10717              :       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
   10718          934 :       else if (TREE_CODE (optype) == VECTOR_TYPE
   10719          934 :                && useless_type_conversion_p (type, TREE_TYPE (optype)))
   10720              :         {
   10721           26 :           tree part_width = TYPE_SIZE (type);
   10722           26 :           tree index = bitsize_int (0);
   10723           26 :           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
   10724              :         }
   10725              :     }
   10726              : 
   10727              :   /* *(p + CST) -> ...  */
   10728      2274355 :   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
   10729      2274355 :       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
   10730              :     {
   10731        33924 :       tree addr = TREE_OPERAND (sub, 0);
   10732        33924 :       tree off = TREE_OPERAND (sub, 1);
   10733        33924 :       tree addrtype;
   10734              : 
   10735        33924 :       STRIP_NOPS (addr);
   10736        33924 :       addrtype = TREE_TYPE (addr);
   10737              : 
   10738              :       /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
   10739        33924 :       if (TREE_CODE (addr) == ADDR_EXPR
   10740           92 :           && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
   10741           39 :           && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
   10742        33952 :           && tree_fits_uhwi_p (off))
   10743              :         {
   10744           28 :           unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
   10745           28 :           tree part_width = TYPE_SIZE (type);
   10746           28 :           unsigned HOST_WIDE_INT part_widthi
   10747           28 :             = tree_to_shwi (part_width) / BITS_PER_UNIT;
   10748           28 :           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
   10749           28 :           tree index = bitsize_int (indexi);
   10750           28 :           if (known_lt (offset / part_widthi,
   10751              :                         TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))))
   10752           28 :             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
   10753              :                                 part_width, index);
   10754              :         }
   10755              : 
   10756              :       /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
   10757        33896 :       if (TREE_CODE (addr) == ADDR_EXPR
   10758           64 :           && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
   10759        33897 :           && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
   10760              :         {
   10761            1 :           tree size = TYPE_SIZE_UNIT (type);
   10762            1 :           if (tree_int_cst_equal (size, off))
   10763            1 :             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
   10764              :         }
   10765              : 
   10766              :       /* *(p + CST) -> MEM_REF <p, CST>.  */
   10767        33895 :       if (TREE_CODE (addr) != ADDR_EXPR
   10768        33895 :           || DECL_P (TREE_OPERAND (addr, 0)))
   10769        33877 :         return fold_build2 (MEM_REF, type,
   10770              :                             addr,
   10771              :                             wide_int_to_tree (ptype, wi::to_wide (off)));
   10772              :     }
   10773              : 
   10774              :   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
   10775      2240449 :   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
   10776         2517 :       && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
   10777      2242936 :       && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
   10778              :     {
   10779            1 :       tree type_domain;
   10780            1 :       tree min_val = size_zero_node;
   10781            1 :       tree osub = sub;
   10782            1 :       sub = gimple_fold_indirect_ref (sub);
   10783            1 :       if (! sub)
   10784            1 :         sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
   10785            1 :       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
   10786            1 :       if (type_domain && TYPE_MIN_VALUE (type_domain))
   10787            1 :         min_val = TYPE_MIN_VALUE (type_domain);
   10788            1 :       if (TREE_CODE (min_val) == INTEGER_CST)
   10789            1 :         return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
   10790              :     }
   10791              : 
   10792              :   return NULL_TREE;
   10793              : }
   10794              : 
   10795              : /* Return true if CODE is an operation that when operating on signed
   10796              :    integer types involves undefined behavior on overflow and the
   10797              :    operation can be expressed with unsigned arithmetic.  */
   10798              : 
   10799              : bool
   10800       510262 : arith_code_with_undefined_signed_overflow (tree_code code)
   10801              : {
   10802       510262 :   switch (code)
   10803              :     {
   10804              :     case ABS_EXPR:
   10805              :     case PLUS_EXPR:
   10806              :     case MINUS_EXPR:
   10807              :     case MULT_EXPR:
   10808              :     case NEGATE_EXPR:
   10809              :     case POINTER_PLUS_EXPR:
   10810              :       return true;
   10811       191220 :     default:
   10812       191220 :       return false;
   10813              :     }
   10814              : }
   10815              : 
   10816              : /* Return true if STMT has an operation that operates on a signed
   10817              :    integer types involves undefined behavior on overflow and the
   10818              :    operation can be expressed with unsigned arithmetic.
   10819              :    Also returns true if STMT is a VCE that needs to be rewritten
   10820              :    if moved to be executed unconditionally.   */
   10821              : 
   10822              : bool
   10823      1221726 : gimple_needing_rewrite_undefined (gimple *stmt)
   10824              : {
   10825      1221726 :   if (!is_gimple_assign (stmt))
   10826              :     return false;
   10827      1062444 :   tree lhs = gimple_assign_lhs (stmt);
   10828      1062444 :   if (!lhs)
   10829              :     return false;
   10830      1062444 :   tree lhs_type = TREE_TYPE (lhs);
   10831      1062444 :   if (!INTEGRAL_TYPE_P (lhs_type)
   10832       124464 :       && !POINTER_TYPE_P (lhs_type))
   10833              :     return false;
   10834      1025389 :   tree rhs = gimple_assign_rhs1 (stmt);
   10835              :   /* Boolean loads need special handling as they are treated as a full MODE load
   10836              :      and don't mask off the bits for the precision.  */
   10837      1025389 :   if (gimple_assign_load_p (stmt)
   10838              :       /* Booleans are the integral type which has this non-masking issue. */
   10839        95954 :       && TREE_CODE (lhs_type) == BOOLEAN_TYPE
   10840              :       /* Only non mode precision booleans are need the masking.  */
   10841          405 :       && !type_has_mode_precision_p (lhs_type)
   10842              :       /* BFR should be the correct thing and just grab the precision.  */
   10843          405 :       && TREE_CODE (rhs) != BIT_FIELD_REF
   10844              :       /* Bit-fields loads don't need a rewrite as the masking
   10845              :          happens for them.  */
   10846      1025794 :       && (TREE_CODE (rhs) != COMPONENT_REF
   10847          139 :           || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1))))
   10848              :     return true;
   10849              :   /* VCE from integral types to a integral types but with
   10850              :      a smaller precision need to be changed into casts
   10851              :      to be well defined. */
   10852      1024984 :   if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
   10853          196 :       && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
   10854          169 :       && is_gimple_val (TREE_OPERAND (rhs, 0))
   10855      1025153 :       && TYPE_PRECISION (lhs_type)
   10856          169 :           < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (rhs, 0))))
   10857              :     return true;
   10858      1024819 :   if (!TYPE_OVERFLOW_UNDEFINED (lhs_type))
   10859              :     return false;
   10860       390739 :   if (!arith_code_with_undefined_signed_overflow
   10861       390739 :         (gimple_assign_rhs_code (stmt)))
   10862              :     return false;
   10863              :   return true;
   10864              : }
   10865              : 
   10866              : /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
   10867              :    operation that can be transformed to unsigned arithmetic by converting
   10868              :    its operand, carrying out the operation in the corresponding unsigned
   10869              :    type and converting the result back to the original type.
   10870              : 
   10871              :    If IN_PLACE is true, *GSI points to STMT, adjust the stmt in place and
   10872              :    return NULL.
   10873              :    Otherwise returns a sequence of statements that replace STMT and also
   10874              :    contain a modified form of STMT itself.  */
   10875              : 
   10876              : static gimple_seq
   10877        67107 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi, gimple *stmt,
   10878              :                                   bool in_place)
   10879              : {
   10880        67107 :   gcc_assert (gimple_needing_rewrite_undefined (stmt));
   10881        67107 :   if (dump_file && (dump_flags & TDF_DETAILS))
   10882              :     {
   10883           21 :       fprintf (dump_file, "rewriting stmt for being uncondtional defined");
   10884           21 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
   10885              :     }
   10886        67107 :   gimple_seq stmts = NULL;
   10887        67107 :   tree lhs = gimple_assign_lhs (stmt);
   10888              : 
   10889              :   /* Boolean loads need to be rewritten to be a load from the same mode
   10890              :      and then a cast to the other type so the other bits are masked off
   10891              :      correctly since the load was done conditionally. It is similar to the VCE
   10892              :      case below.  */
   10893        67107 :   if (gimple_assign_load_p (stmt)
   10894        67107 :       && TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE)
   10895              :     {
   10896          113 :       tree rhs = gimple_assign_rhs1 (stmt);
   10897              : 
   10898              :       /* Double check that gimple_needing_rewrite_undefined was called.  */
   10899              :       /* Bit-fields loads will do the masking so don't need the rewriting.  */
   10900          113 :       gcc_assert (TREE_CODE (rhs) != COMPONENT_REF
   10901              :                   || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1)));
   10902              :       /* BFR is like a bit field load and will do the correct thing.  */
   10903          113 :       gcc_assert (TREE_CODE (lhs) != BIT_FIELD_REF);
   10904              :       /* Complex boolean types are not valid so REAL/IMAG part will
   10905              :          never show up. */
   10906          113 :       gcc_assert (TREE_CODE (rhs) != REALPART_EXPR
   10907              :                   && TREE_CODE (lhs) != IMAGPART_EXPR);
   10908              : 
   10909          113 :       auto bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (rhs)));
   10910          113 :       tree new_type = build_nonstandard_integer_type (bits, true);
   10911          113 :       location_t loc = gimple_location (stmt);
   10912          113 :       tree mem_ref = fold_build1_loc (loc, VIEW_CONVERT_EXPR, new_type, rhs);
   10913              :       /* Replace the original load with a new load and a new lhs. */
   10914          113 :       tree new_lhs = make_ssa_name (new_type);
   10915          113 :       gimple_assign_set_rhs1 (stmt, mem_ref);
   10916          113 :       gimple_assign_set_lhs (stmt, new_lhs);
   10917              : 
   10918          113 :       if (in_place)
   10919           49 :           update_stmt (stmt);
   10920              :       else
   10921              :         {
   10922           64 :           gimple_set_modified (stmt, true);
   10923           64 :           gimple_seq_add_stmt (&stmts, stmt);
   10924              :         }
   10925              : 
   10926              :       /* Build the conversion statement.  */
   10927          113 :       gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, new_lhs);
   10928          113 :       if (in_place)
   10929              :         {
   10930           49 :           gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
   10931           49 :           update_stmt (stmt);
   10932              :         }
   10933              :       else
   10934           64 :         gimple_seq_add_stmt (&stmts, cvt);
   10935          113 :       return stmts;
   10936              :     }
   10937              : 
   10938              :   /* VCE from integral types to another integral types but with
   10939              :      smaller precisions need to be changed into casts
   10940              :      to be well defined. */
   10941        66994 :   if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
   10942              :     {
   10943           60 :       tree rhs = gimple_assign_rhs1 (stmt);
   10944           60 :       tree new_rhs = TREE_OPERAND (rhs, 0);
   10945           60 :       gcc_assert (TYPE_PRECISION (TREE_TYPE (rhs))
   10946              :                   < TYPE_PRECISION (TREE_TYPE (new_rhs)));
   10947           60 :       gcc_assert (is_gimple_val (new_rhs));
   10948           60 :       gimple_assign_set_rhs_code (stmt, NOP_EXPR);
   10949           60 :       gimple_assign_set_rhs1 (stmt, new_rhs);
   10950           60 :       if (in_place)
   10951           51 :           update_stmt (stmt);
   10952              :       else
   10953              :         {
   10954            9 :           gimple_set_modified (stmt, true);
   10955            9 :           gimple_seq_add_stmt (&stmts, stmt);
   10956              :         }
   10957           60 :       return stmts;
   10958              :     }
   10959        66934 :   tree type = unsigned_type_for (TREE_TYPE (lhs));
   10960        66934 :   if (gimple_assign_rhs_code (stmt) == ABS_EXPR)
   10961           24 :     gimple_assign_set_rhs_code (stmt, ABSU_EXPR);
   10962              :   else
   10963       200225 :     for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
   10964              :       {
   10965       133315 :         tree op = gimple_op (stmt, i);
   10966       133315 :         op = gimple_convert (&stmts, type, op);
   10967       133315 :         gimple_set_op (stmt, i, op);
   10968              :       }
   10969        66934 :   gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
   10970        66934 :   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
   10971        11272 :     gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
   10972        66934 :   gimple_set_modified (stmt, true);
   10973        66934 :   if (in_place)
   10974              :     {
   10975        46550 :       if (stmts)
   10976        46158 :         gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
   10977        46550 :       stmts = NULL;
   10978              :     }
   10979              :   else
   10980        20384 :     gimple_seq_add_stmt (&stmts, stmt);
   10981        66934 :   gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
   10982        66934 :   if (in_place)
   10983              :     {
   10984        46550 :       gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
   10985        46550 :       update_stmt (stmt);
   10986              :     }
   10987              :   else
   10988        20384 :     gimple_seq_add_stmt (&stmts, cvt);
   10989              : 
   10990        66934 :   return stmts;
   10991              : }
   10992              : 
   10993              : void
   10994        46650 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi)
   10995              : {
   10996        46650 :   rewrite_to_defined_unconditional (gsi, gsi_stmt (*gsi), true);
   10997        46650 : }
   10998              : 
   10999              : gimple_seq
   11000        20457 : rewrite_to_defined_unconditional (gimple *stmt)
   11001              : {
   11002        20457 :   return rewrite_to_defined_unconditional (nullptr, stmt, false);
   11003              : }
   11004              : 
   11005              : /* The valueization hook we use for the gimple_build API simplification.
   11006              :    This makes us match fold_buildN behavior by only combining with
   11007              :    statements in the sequence(s) we are currently building.  */
   11008              : 
   11009              : static tree
   11010     20089203 : gimple_build_valueize (tree op)
   11011              : {
   11012     20089203 :   if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
   11013      4276092 :     return op;
   11014              :   return NULL_TREE;
   11015              : }
   11016              : 
   11017              : /* Helper for gimple_build to perform the final insertion of stmts on SEQ.  */
   11018              : 
   11019              : static inline void
   11020      1318590 : gimple_build_insert_seq (gimple_stmt_iterator *gsi,
   11021              :                          bool before, gsi_iterator_update update,
   11022              :                          gimple_seq seq)
   11023              : {
   11024      1318590 :   if (before)
   11025              :     {
   11026        94385 :       if (gsi->bb)
   11027        94385 :         gsi_insert_seq_before (gsi, seq, update);
   11028              :       else
   11029            0 :         gsi_insert_seq_before_without_update (gsi, seq, update);
   11030              :     }
   11031              :   else
   11032              :     {
   11033      1224205 :       if (gsi->bb)
   11034          131 :         gsi_insert_seq_after (gsi, seq, update);
   11035              :       else
   11036      1224074 :         gsi_insert_seq_after_without_update (gsi, seq, update);
   11037              :     }
   11038      1318590 : }
   11039              : 
   11040              : /* Build the expression CODE OP0 of type TYPE with location LOC,
   11041              :    simplifying it first if possible.  Returns the built
   11042              :    expression value and inserts statements possibly defining it
   11043              :    before GSI if BEFORE is true or after GSI if false and advance
   11044              :    the iterator accordingly.
   11045              :    If gsi refers to a basic block simplifying is allowed to look
   11046              :    at all SSA defs while when it does not it is restricted to
   11047              :    SSA defs that are not associated with a basic block yet,
   11048              :    indicating they belong to the currently building sequence.  */
   11049              : 
   11050              : tree
   11051       339472 : gimple_build (gimple_stmt_iterator *gsi,
   11052              :               bool before, gsi_iterator_update update,
   11053              :               location_t loc, enum tree_code code, tree type, tree op0)
   11054              : {
   11055       339472 :   gimple_seq seq = NULL;
   11056       339472 :   tree res
   11057       339472 :     = gimple_simplify (code, type, op0, &seq,
   11058       339472 :                        gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   11059       339472 :   if (!res)
   11060              :     {
   11061       298467 :       res = make_ssa_name (type);
   11062       298467 :       gimple *stmt;
   11063       298467 :       if (code == REALPART_EXPR
   11064              :           || code == IMAGPART_EXPR
   11065       298467 :           || code == VIEW_CONVERT_EXPR)
   11066        15110 :         stmt = gimple_build_assign (res, code, build1 (code, type, op0));
   11067              :       else
   11068       283357 :         stmt = gimple_build_assign (res, code, op0);
   11069       298467 :       gimple_set_location (stmt, loc);
   11070       298467 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11071              :     }
   11072       339472 :   gimple_build_insert_seq (gsi, before, update, seq);
   11073       339472 :   return res;
   11074              : }
   11075              : 
   11076              : /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
   11077              :    simplifying it first if possible.  Returns the built
   11078              :    expression value inserting any new statements at GSI honoring BEFORE
   11079              :    and UPDATE.  */
   11080              : 
   11081              : tree
   11082       769494 : gimple_build (gimple_stmt_iterator *gsi,
   11083              :               bool before, gsi_iterator_update update,
   11084              :               location_t loc, enum tree_code code, tree type,
   11085              :               tree op0, tree op1)
   11086              : {
   11087       769494 :   gimple_seq seq = NULL;
   11088       769494 :   tree res
   11089       769494 :     = gimple_simplify (code, type, op0, op1, &seq,
   11090       769494 :                        gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   11091       769494 :   if (!res)
   11092              :     {
   11093       679317 :       res = make_ssa_name (type);
   11094       679317 :       gimple *stmt = gimple_build_assign (res, code, op0, op1);
   11095       679317 :       gimple_set_location (stmt, loc);
   11096       679317 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11097              :     }
   11098       769494 :   gimple_build_insert_seq (gsi, before, update, seq);
   11099       769494 :   return res;
   11100              : }
   11101              : 
   11102              : /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
   11103              :    simplifying it first if possible.  Returns the built
   11104              :    expression value inserting any new statements at GSI honoring BEFORE
   11105              :    and UPDATE.  */
   11106              : 
   11107              : tree
   11108        44348 : gimple_build (gimple_stmt_iterator *gsi,
   11109              :               bool before, gsi_iterator_update update,
   11110              :               location_t loc, enum tree_code code, tree type,
   11111              :               tree op0, tree op1, tree op2)
   11112              : {
   11113              : 
   11114        44348 :   gimple_seq seq = NULL;
   11115        44348 :   tree res
   11116        44348 :     = gimple_simplify (code, type, op0, op1, op2, &seq,
   11117        44348 :                        gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   11118        44348 :   if (!res)
   11119              :     {
   11120        32202 :       res = make_ssa_name (type);
   11121        32202 :       gimple *stmt;
   11122        32202 :       if (code == BIT_FIELD_REF)
   11123        24793 :         stmt = gimple_build_assign (res, code,
   11124              :                                     build3 (code, type, op0, op1, op2));
   11125              :       else
   11126         7409 :         stmt = gimple_build_assign (res, code, op0, op1, op2);
   11127        32202 :       gimple_set_location (stmt, loc);
   11128        32202 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11129              :     }
   11130        44348 :   gimple_build_insert_seq (gsi, before, update, seq);
   11131        44348 :   return res;
   11132              : }
   11133              : 
   11134              : /* Build the call FN () with a result of type TYPE (or no result if TYPE is
   11135              :    void) with a location LOC.  Returns the built expression value (or NULL_TREE
   11136              :    if TYPE is void) inserting any new statements at GSI honoring BEFORE
   11137              :    and UPDATE.  */
   11138              : 
   11139              : tree
   11140            0 : gimple_build (gimple_stmt_iterator *gsi,
   11141              :               bool before, gsi_iterator_update update,
   11142              :               location_t loc, combined_fn fn, tree type)
   11143              : {
   11144            0 :   tree res = NULL_TREE;
   11145            0 :   gimple_seq seq = NULL;
   11146            0 :   gcall *stmt;
   11147            0 :   if (internal_fn_p (fn))
   11148            0 :     stmt = gimple_build_call_internal (as_internal_fn (fn), 0);
   11149              :   else
   11150              :     {
   11151            0 :       tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   11152            0 :       stmt = gimple_build_call (decl, 0);
   11153              :     }
   11154            0 :   if (!VOID_TYPE_P (type))
   11155              :     {
   11156            0 :       res = make_ssa_name (type);
   11157            0 :       gimple_call_set_lhs (stmt, res);
   11158              :     }
   11159            0 :   gimple_set_location (stmt, loc);
   11160            0 :   gimple_seq_add_stmt_without_update (&seq, stmt);
   11161            0 :   gimple_build_insert_seq (gsi, before, update, seq);
   11162            0 :   return res;
   11163              : }
   11164              : 
   11165              : /* Build the call FN (ARG0) with a result of type TYPE
   11166              :    (or no result if TYPE is void) with location LOC,
   11167              :    simplifying it first if possible.  Returns the built
   11168              :    expression value (or NULL_TREE if TYPE is void) inserting any new
   11169              :    statements at GSI honoring BEFORE and UPDATE.  */
   11170              : 
   11171              : tree
   11172        24440 : gimple_build (gimple_stmt_iterator *gsi,
   11173              :               bool before, gsi_iterator_update update,
   11174              :               location_t loc, combined_fn fn,
   11175              :               tree type, tree arg0)
   11176              : {
   11177        24440 :   gimple_seq seq = NULL;
   11178        24440 :   tree res = gimple_simplify (fn, type, arg0, &seq, gimple_build_valueize);
   11179        24440 :   if (!res)
   11180              :     {
   11181        24440 :       gcall *stmt;
   11182        24440 :       if (internal_fn_p (fn))
   11183        24066 :         stmt = gimple_build_call_internal (as_internal_fn (fn), 1, arg0);
   11184              :       else
   11185              :         {
   11186          374 :           tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   11187          374 :           stmt = gimple_build_call (decl, 1, arg0);
   11188              :         }
   11189        24440 :       if (!VOID_TYPE_P (type))
   11190              :         {
   11191        24066 :           res = make_ssa_name (type);
   11192        24066 :           gimple_call_set_lhs (stmt, res);
   11193              :         }
   11194        24440 :       gimple_set_location (stmt, loc);
   11195        24440 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11196              :     }
   11197        24440 :   gimple_build_insert_seq  (gsi, before, update, seq);
   11198        24440 :   return res;
   11199              : }
   11200              : 
   11201              : /* Build the call FN (ARG0, ARG1) with a result of type TYPE
   11202              :    (or no result if TYPE is void) with location LOC,
   11203              :    simplifying it first if possible.  Returns the built
   11204              :    expression value (or NULL_TREE if TYPE is void) inserting any new
   11205              :    statements at GSI honoring BEFORE and UPDATE.  */
   11206              : 
   11207              : tree
   11208            0 : gimple_build (gimple_stmt_iterator *gsi,
   11209              :               bool before, gsi_iterator_update update,
   11210              :               location_t loc, combined_fn fn,
   11211              :               tree type, tree arg0, tree arg1)
   11212              : {
   11213            0 :   gimple_seq seq = NULL;
   11214            0 :   tree res = gimple_simplify (fn, type, arg0, arg1, &seq,
   11215              :                               gimple_build_valueize);
   11216            0 :   if (!res)
   11217              :     {
   11218            0 :       gcall *stmt;
   11219            0 :       if (internal_fn_p (fn))
   11220            0 :         stmt = gimple_build_call_internal (as_internal_fn (fn), 2, arg0, arg1);
   11221              :       else
   11222              :         {
   11223            0 :           tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   11224            0 :           stmt = gimple_build_call (decl, 2, arg0, arg1);
   11225              :         }
   11226            0 :       if (!VOID_TYPE_P (type))
   11227              :         {
   11228            0 :           res = make_ssa_name (type);
   11229            0 :           gimple_call_set_lhs (stmt, res);
   11230              :         }
   11231            0 :       gimple_set_location (stmt, loc);
   11232            0 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11233              :     }
   11234            0 :   gimple_build_insert_seq (gsi, before, update, seq);
   11235            0 :   return res;
   11236              : }
   11237              : 
   11238              : /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
   11239              :    (or no result if TYPE is void) with location LOC,
   11240              :    simplifying it first if possible.  Returns the built
   11241              :    expression value (or NULL_TREE if TYPE is void) inserting any new
   11242              :    statements at GSI honoring BEFORE and UPDATE.  */
   11243              : 
   11244              : tree
   11245            0 : gimple_build (gimple_stmt_iterator *gsi,
   11246              :               bool before, gsi_iterator_update update,
   11247              :               location_t loc, combined_fn fn,
   11248              :               tree type, tree arg0, tree arg1, tree arg2)
   11249              : {
   11250            0 :   gimple_seq seq = NULL;
   11251            0 :   tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
   11252              :                               &seq, gimple_build_valueize);
   11253            0 :   if (!res)
   11254              :     {
   11255            0 :       gcall *stmt;
   11256            0 :       if (internal_fn_p (fn))
   11257            0 :         stmt = gimple_build_call_internal (as_internal_fn (fn),
   11258              :                                            3, arg0, arg1, arg2);
   11259              :       else
   11260              :         {
   11261            0 :           tree decl = builtin_decl_implicit (as_builtin_fn (fn));
   11262            0 :           stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
   11263              :         }
   11264            0 :       if (!VOID_TYPE_P (type))
   11265              :         {
   11266            0 :           res = make_ssa_name (type);
   11267            0 :           gimple_call_set_lhs (stmt, res);
   11268              :         }
   11269            0 :       gimple_set_location (stmt, loc);
   11270            0 :       gimple_seq_add_stmt_without_update (&seq, stmt);
   11271              :     }
   11272            0 :   gimple_build_insert_seq (gsi, before, update, seq);
   11273            0 :   return res;
   11274              : }
   11275              : 
   11276              : /* Build CODE (OP0) with a result of type TYPE (or no result if TYPE is
   11277              :    void) with location LOC, simplifying it first if possible.  Returns the
   11278              :    built expression value (or NULL_TREE if TYPE is void) inserting any new
   11279              :    statements at GSI honoring BEFORE and UPDATE.  */
   11280              : 
   11281              : tree
   11282           21 : gimple_build (gimple_stmt_iterator *gsi,
   11283              :               bool before, gsi_iterator_update update,
   11284              :               location_t loc, code_helper code, tree type, tree op0)
   11285              : {
   11286           21 :   if (code.is_tree_code ())
   11287            0 :     return gimple_build (gsi, before, update, loc, tree_code (code), type, op0);
   11288           21 :   return gimple_build (gsi, before, update, loc, combined_fn (code), type, op0);
   11289              : }
   11290              : 
   11291              : /* Build CODE (OP0, OP1) with a result of type TYPE (or no result if TYPE is
   11292              :    void) with location LOC, simplifying it first if possible.  Returns the
   11293              :    built expression value (or NULL_TREE if TYPE is void) inserting any new
   11294              :    statements at GSI honoring BEFORE and UPDATE.  */
   11295              : 
   11296              : tree
   11297        23956 : gimple_build (gimple_stmt_iterator *gsi,
   11298              :               bool before, gsi_iterator_update update,
   11299              :               location_t loc, code_helper code, tree type, tree op0, tree op1)
   11300              : {
   11301        23956 :   if (code.is_tree_code ())
   11302        23956 :     return gimple_build (gsi, before, update,
   11303        23956 :                          loc, tree_code (code), type, op0, op1);
   11304            0 :   return gimple_build (gsi, before, update,
   11305            0 :                        loc, combined_fn (code), type, op0, op1);
   11306              : }
   11307              : 
   11308              : /* Build CODE (OP0, OP1, OP2) with a result of type TYPE (or no result if TYPE
   11309              :    is void) with location LOC, simplifying it first if possible.  Returns the
   11310              :    built expression value (or NULL_TREE if TYPE is void) inserting any new
   11311              :    statements at GSI honoring BEFORE and UPDATE.  */
   11312              : 
   11313              : tree
   11314            0 : gimple_build (gimple_stmt_iterator *gsi,
   11315              :               bool before, gsi_iterator_update update,
   11316              :               location_t loc, code_helper code,
   11317              :               tree type, tree op0, tree op1, tree op2)
   11318              : {
   11319            0 :   if (code.is_tree_code ())
   11320            0 :     return gimple_build (gsi, before, update,
   11321            0 :                          loc, tree_code (code), type, op0, op1, op2);
   11322            0 :   return gimple_build (gsi, before, update,
   11323            0 :                        loc, combined_fn (code), type, op0, op1, op2);
   11324              : }
   11325              : 
   11326              : /* Build the conversion (TYPE) OP with a result of type TYPE
   11327              :    with location LOC if such conversion is neccesary in GIMPLE,
   11328              :    simplifying it first.
   11329              :    Returns the built expression inserting any new statements
   11330              :    at GSI honoring BEFORE and UPDATE.  */
   11331              : 
   11332              : tree
   11333      2004707 : gimple_convert (gimple_stmt_iterator *gsi,
   11334              :                 bool before, gsi_iterator_update update,
   11335              :                 location_t loc, tree type, tree op)
   11336              : {
   11337      2004707 :   if (useless_type_conversion_p (type, TREE_TYPE (op)))
   11338              :     return op;
   11339       186792 :   return gimple_build (gsi, before, update, loc, NOP_EXPR, type, op);
   11340              : }
   11341              : 
   11342              : /* Build the conversion (ptrofftype) OP with a result of a type
   11343              :    compatible with ptrofftype with location LOC if such conversion
   11344              :    is neccesary in GIMPLE, simplifying it first.
   11345              :    Returns the built expression value inserting any new statements
   11346              :    at GSI honoring BEFORE and UPDATE.  */
   11347              : 
   11348              : tree
   11349          208 : gimple_convert_to_ptrofftype (gimple_stmt_iterator *gsi,
   11350              :                               bool before, gsi_iterator_update update,
   11351              :                               location_t loc, tree op)
   11352              : {
   11353          208 :   if (ptrofftype_p (TREE_TYPE (op)))
   11354              :     return op;
   11355            0 :   return gimple_convert (gsi, before, update, loc, sizetype, op);
   11356              : }
   11357              : 
   11358              : /* Build a vector of type TYPE in which each element has the value OP.
   11359              :    Return a gimple value for the result, inserting any new statements
   11360              :    at GSI honoring BEFORE and UPDATE.  */
   11361              : 
   11362              : tree
   11363       324498 : gimple_build_vector_from_val (gimple_stmt_iterator *gsi,
   11364              :                               bool before, gsi_iterator_update update,
   11365              :                               location_t loc, tree type, tree op)
   11366              : {
   11367       324498 :   if (!TYPE_VECTOR_SUBPARTS (type).is_constant ()
   11368              :       && !CONSTANT_CLASS_P (op))
   11369              :     return gimple_build (gsi, before, update,
   11370              :                          loc, VEC_DUPLICATE_EXPR, type, op);
   11371              : 
   11372       324498 :   tree res, vec = build_vector_from_val (type, op);
   11373       324498 :   if (is_gimple_val (vec))
   11374              :     return vec;
   11375        27557 :   if (gimple_in_ssa_p (cfun))
   11376        27557 :     res = make_ssa_name (type);
   11377              :   else
   11378            0 :     res = create_tmp_reg (type);
   11379        27557 :   gimple_seq seq = NULL;
   11380        27557 :   gimple *stmt = gimple_build_assign (res, vec);
   11381        27557 :   gimple_set_location (stmt, loc);
   11382        27557 :   gimple_seq_add_stmt_without_update (&seq, stmt);
   11383        27557 :   gimple_build_insert_seq (gsi, before, update, seq);
   11384        27557 :   return res;
   11385              : }
   11386              : 
   11387              : /* Build a vector from BUILDER, handling the case in which some elements
   11388              :    are non-constant.  Return a gimple value for the result, inserting
   11389              :    any new instructions to GSI honoring BEFORE and UPDATE.
   11390              : 
   11391              :    BUILDER must not have a stepped encoding on entry.  This is because
   11392              :    the function is not geared up to handle the arithmetic that would
   11393              :    be needed in the variable case, and any code building a vector that
   11394              :    is known to be constant should use BUILDER->build () directly.  */
   11395              : 
   11396              : tree
   11397       368477 : gimple_build_vector (gimple_stmt_iterator *gsi,
   11398              :                      bool before, gsi_iterator_update update,
   11399              :                      location_t loc, tree_vector_builder *builder)
   11400              : {
   11401       368477 :   gcc_assert (builder->nelts_per_pattern () <= 2);
   11402       368477 :   unsigned int encoded_nelts = builder->encoded_nelts ();
   11403      1301731 :   for (unsigned int i = 0; i < encoded_nelts; ++i)
   11404      1046533 :     if (!CONSTANT_CLASS_P ((*builder)[i]))
   11405              :       {
   11406       113279 :         gimple_seq seq = NULL;
   11407       113279 :         tree type = builder->type ();
   11408       113279 :         unsigned int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
   11409       113279 :         vec<constructor_elt, va_gc> *v;
   11410       113279 :         vec_alloc (v, nelts);
   11411       361585 :         for (i = 0; i < nelts; ++i)
   11412       248306 :           CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
   11413              : 
   11414       113279 :         tree res;
   11415       113279 :         if (gimple_in_ssa_p (cfun))
   11416       113279 :           res = make_ssa_name (type);
   11417              :         else
   11418            0 :           res = create_tmp_reg (type);
   11419       113279 :         gimple *stmt = gimple_build_assign (res, build_constructor (type, v));
   11420       113279 :         gimple_set_location (stmt, loc);
   11421       113279 :         gimple_seq_add_stmt_without_update (&seq, stmt);
   11422       113279 :         gimple_build_insert_seq (gsi, before, update, seq);
   11423       113279 :         return res;
   11424              :       }
   11425       255198 :   return builder->build ();
   11426              : }
   11427              : 
   11428              : /* Emit gimple statements into &stmts that take a value given in OLD_SIZE
   11429              :    and generate a value guaranteed to be rounded upwards to ALIGN.
   11430              : 
   11431              :    Return the tree node representing this size, it is of TREE_TYPE TYPE.  */
   11432              : 
   11433              : tree
   11434            0 : gimple_build_round_up (gimple_stmt_iterator *gsi,
   11435              :                        bool before, gsi_iterator_update update,
   11436              :                        location_t loc, tree type,
   11437              :                        tree old_size, unsigned HOST_WIDE_INT align)
   11438              : {
   11439            0 :   unsigned HOST_WIDE_INT tg_mask = align - 1;
   11440              :   /* tree new_size = (old_size + tg_mask) & ~tg_mask;  */
   11441            0 :   gcc_assert (INTEGRAL_TYPE_P (type));
   11442            0 :   tree tree_mask = build_int_cst (type, tg_mask);
   11443            0 :   tree oversize = gimple_build (gsi, before, update,
   11444              :                                 loc, PLUS_EXPR, type, old_size, tree_mask);
   11445              : 
   11446            0 :   tree mask = build_int_cst (type, -align);
   11447            0 :   return gimple_build (gsi, before, update,
   11448            0 :                        loc, BIT_AND_EXPR, type, oversize, mask);
   11449              : }
   11450              : 
   11451              : /* Return true if the result of assignment STMT is known to be non-negative.
   11452              :    DEPTH is the current nesting depth of the query.  */
   11453              : 
   11454              : static bool
   11455      6340173 : gimple_assign_nonnegative_p (gimple *stmt, int depth)
   11456              : {
   11457      6340173 :   enum tree_code code = gimple_assign_rhs_code (stmt);
   11458      6340173 :   tree type = TREE_TYPE (gimple_assign_lhs (stmt));
   11459      6340173 :   switch (get_gimple_rhs_class (code))
   11460              :     {
   11461       501252 :     case GIMPLE_UNARY_RHS:
   11462       501252 :       return tree_unary_nonnegative_p (gimple_assign_rhs_code (stmt),
   11463              :                                        type,
   11464              :                                        gimple_assign_rhs1 (stmt),
   11465       501252 :                                        depth);
   11466      3729160 :     case GIMPLE_BINARY_RHS:
   11467      3729160 :       return tree_binary_nonnegative_p (gimple_assign_rhs_code (stmt),
   11468              :                                         type,
   11469              :                                         gimple_assign_rhs1 (stmt),
   11470              :                                         gimple_assign_rhs2 (stmt),
   11471      3729160 :                                         depth);
   11472              :     case GIMPLE_TERNARY_RHS:
   11473              :       return false;
   11474      2095452 :     case GIMPLE_SINGLE_RHS:
   11475      2095452 :       return tree_single_nonnegative_p (gimple_assign_rhs1 (stmt),
   11476      2095452 :                                         depth);
   11477              :     case GIMPLE_INVALID_RHS:
   11478              :       break;
   11479              :     }
   11480            0 :   gcc_unreachable ();
   11481              : }
   11482              : 
   11483              : /* Return true if return value of call STMT is known to be non-negative.
   11484              :    DEPTH is the current nesting depth of the query.  */
   11485              : 
   11486              : static bool
   11487     13135360 : gimple_call_nonnegative_p (gimple *stmt, int depth)
   11488              : {
   11489     13135360 :   tree arg0
   11490     13135360 :     = gimple_call_num_args (stmt) > 0 ? gimple_call_arg (stmt, 0) : NULL_TREE;
   11491     13135360 :   tree arg1
   11492     13135360 :     = gimple_call_num_args (stmt) > 1 ? gimple_call_arg (stmt, 1) : NULL_TREE;
   11493     13135360 :   tree lhs = gimple_call_lhs (stmt);
   11494     13135360 :   return (lhs
   11495     13135360 :           && tree_call_nonnegative_p (TREE_TYPE (lhs),
   11496              :                                       gimple_call_combined_fn (stmt),
   11497     13135360 :                                       arg0, arg1, depth));
   11498              : }
   11499              : 
   11500              : /* Return true if return value of call STMT is known to be non-negative.
   11501              :    DEPTH is the current nesting depth of the query.  */
   11502              : 
   11503              : static bool
   11504      1211746 : gimple_phi_nonnegative_p (gimple *stmt, int depth)
   11505              : {
   11506      1640647 :   for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
   11507              :     {
   11508      1628166 :       tree arg = gimple_phi_arg_def (stmt, i);
   11509      1628166 :       if (!tree_single_nonnegative_p (arg, depth + 1))
   11510              :         return false;
   11511              :     }
   11512              :   return true;
   11513              : }
   11514              : 
   11515              : /* Return true if STMT is known to compute a non-negative value.
   11516              :    DEPTH is the current nesting depth of the query.  */
   11517              : 
   11518              : bool
   11519     21315982 : gimple_stmt_nonnegative_p (gimple *stmt, int depth)
   11520              : {
   11521     21315982 :   tree type = gimple_range_type (stmt);
   11522     21315982 :   if (type && frange::supports_p (type))
   11523              :     {
   11524      1455694 :       frange r;
   11525      1455694 :       bool sign;
   11526      1455694 :       if (get_global_range_query ()->range_of_stmt (r, stmt)
   11527      1455694 :           && r.signbit_p (sign))
   11528        27198 :         return !sign;
   11529      1455694 :     }
   11530     21288784 :   switch (gimple_code (stmt))
   11531              :     {
   11532      6340173 :     case GIMPLE_ASSIGN:
   11533      6340173 :       return gimple_assign_nonnegative_p (stmt, depth);
   11534     13135360 :     case GIMPLE_CALL:
   11535     13135360 :       return gimple_call_nonnegative_p (stmt, depth);
   11536      1211746 :     case GIMPLE_PHI:
   11537      1211746 :       return gimple_phi_nonnegative_p (stmt, depth);
   11538              :     default:
   11539              :       return false;
   11540              :     }
   11541              : }
   11542              : 
   11543              : /* Return true if the floating-point value computed by assignment STMT
   11544              :    is known to have an integer value.  We also allow +Inf, -Inf and NaN
   11545              :    to be considered integer values. Return false for signaling NaN.
   11546              : 
   11547              :    DEPTH is the current nesting depth of the query.  */
   11548              : 
   11549              : static bool
   11550        58554 : gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
   11551              : {
   11552        58554 :   enum tree_code code = gimple_assign_rhs_code (stmt);
   11553        58554 :   switch (get_gimple_rhs_class (code))
   11554              :     {
   11555        15024 :     case GIMPLE_UNARY_RHS:
   11556        15024 :       return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
   11557        15024 :                                           gimple_assign_rhs1 (stmt), depth);
   11558        13260 :     case GIMPLE_BINARY_RHS:
   11559        13260 :       return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
   11560              :                                            gimple_assign_rhs1 (stmt),
   11561        13260 :                                            gimple_assign_rhs2 (stmt), depth);
   11562              :     case GIMPLE_TERNARY_RHS:
   11563              :       return false;
   11564        29115 :     case GIMPLE_SINGLE_RHS:
   11565        29115 :       return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
   11566              :     case GIMPLE_INVALID_RHS:
   11567              :       break;
   11568              :     }
   11569            0 :   gcc_unreachable ();
   11570              : }
   11571              : 
   11572              : /* Return true if the floating-point value computed by call STMT is known
   11573              :    to have an integer value.  We also allow +Inf, -Inf and NaN to be
   11574              :    considered integer values. Return false for signaling NaN.
   11575              : 
   11576              :    DEPTH is the current nesting depth of the query.  */
   11577              : 
   11578              : static bool
   11579         1089 : gimple_call_integer_valued_real_p (gimple *stmt, int depth)
   11580              : {
   11581         1089 :   tree arg0 = (gimple_call_num_args (stmt) > 0
   11582         1089 :                ? gimple_call_arg (stmt, 0)
   11583              :                : NULL_TREE);
   11584         1089 :   tree arg1 = (gimple_call_num_args (stmt) > 1
   11585         1089 :                ? gimple_call_arg (stmt, 1)
   11586              :                : NULL_TREE);
   11587         1089 :   return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
   11588         1089 :                                      arg0, arg1, depth);
   11589              : }
   11590              : 
   11591              : /* Return true if the floating-point result of phi STMT is known to have
   11592              :    an integer value.  We also allow +Inf, -Inf and NaN to be considered
   11593              :    integer values. Return false for signaling NaN.
   11594              : 
   11595              :    DEPTH is the current nesting depth of the query.  */
   11596              : 
   11597              : static bool
   11598         1489 : gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
   11599              : {
   11600         1652 :   for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
   11601              :     {
   11602         1643 :       tree arg = gimple_phi_arg_def (stmt, i);
   11603         1643 :       if (!integer_valued_real_single_p (arg, depth + 1))
   11604              :         return false;
   11605              :     }
   11606              :   return true;
   11607              : }
   11608              : 
   11609              : /* Return true if the floating-point value computed by STMT is known
   11610              :    to have an integer value.  We also allow +Inf, -Inf and NaN to be
   11611              :    considered integer values. Return false for signaling NaN.
   11612              : 
   11613              :    DEPTH is the current nesting depth of the query.  */
   11614              : 
   11615              : bool
   11616        88509 : gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
   11617              : {
   11618        88509 :   switch (gimple_code (stmt))
   11619              :     {
   11620        58554 :     case GIMPLE_ASSIGN:
   11621        58554 :       return gimple_assign_integer_valued_real_p (stmt, depth);
   11622         1089 :     case GIMPLE_CALL:
   11623         1089 :       return gimple_call_integer_valued_real_p (stmt, depth);
   11624         1489 :     case GIMPLE_PHI:
   11625         1489 :       return gimple_phi_integer_valued_real_p (stmt, depth);
   11626              :     default:
   11627              :       return false;
   11628              :     }
   11629              : }
        

Generated by: LCOV version 2.4-beta

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